Randomly find numbers from 1-12 without repeat [duplicate] - vb.net

This question already has an answer here:
Pick unique Random numbers
(1 answer)
Closed 5 years ago.
I have three variable integers, and I have the following code that randomizes their value:
Randomize()
number = Int(Rnd() * 12) + 1
AssignImagesToSquares()
number2 = Int(Rnd() * 12) + 1
AssignImagesToSquares()
number3 = Int(Rnd() * 12) + 1
AssignImagesToSquares()
And AssignImagesToSquares is a Private Sub where I use them.
However, the problem that I am facing is that numbers can be repeated. I could not figure out how to do it, but in psuedocode,
'Randomize the integer "number"
'Randomize the integer "number2" where number2 <> number
'Randomize the integer "number3" where number3 <> number2 <> number.
I thought of maybe using a loop to repeat the process until a match is found but how exactly can that be done?

As a simple solution you could just use a Do..Loop until the numbers do not match, for example
Randomize()
number = Int(Rnd() * 12) + 1
AssignImagesToSquares()
Do
number2 = Int(Rnd() * 12) + 1
If number2 <> number Then
AssignImagesToSquares()
Exit Do
End If
Loop
Do
number3 = Int(Rnd() * 12) + 1
If number3 <> number AndAlso number3 <> number2 Then
AssignImagesToSquares()
Exit Do
End If
Loop

Yes you could use loops, but alternatively given your situation you could store your values in array, and take values from this array, and as soon as you pick a value from array you remove it. Then you could use it again. Simple code provided (of course it would be better wrapped in a function):
Dim number1, number2, number3 as Integer
Dim numbers = New Integer() {1,2,3,4,5,6,7,8,9,10,11,12}
Dim indx As Integer = Int(Rnd() * numbers.Length)
number1=numbers(indx)
Console.WriteLine(number1)
System.Array.Clear(numbers, indx, 1)
indx=Int(Rnd() * numbers.Length) 'wrap in function
number2=numbers(indx) '
Console.WriteLine(number2) 'AssignImagesToSquares()
System.Array.Clear(numbers, indx, 1) '

MAYBE a little overkill ;) , but you can extend this for any number of numbers wanted:
Dim temp As New ConcurrentDictionary(Of Integer, Integer)
Dim count_actual As Integer = 0
Dim count_wanted As Integer = 3
Do
Dim number = Int(Rnd() * 12) + 1 'or whatever random function
If temp.TryAdd(number, count_actual) Then
count_actual += 1
End If
Loop While count_actual < count_wanted
Dim yourNumbers = temp.OrderBy(Function(v) v.Value).Select(Of Integer)(Function(v) v.Key).ToArray()
Now there are your wanted random different numbers in the yourNumbers array. Just use them.

Related

VB keeping track of high and low numbers

After running through the following program, lownum stays 0. It makes complete sense if you do not enter any negative numbers. However, what are my options to get the lowest number if all positive numbers are entered.
Sub Main()
Declaring variables
Dim number1 As Integer = 0
Dim number2 As Integer = 0
Dim lownum As Integer
Dim highnum As Integer
'For statement to run loop 10 times
For counter As Integer = 1 To 10
'Prompting user to enter two numbers
Console.Write("Enter the first number: ")
number1 = Console.ReadLine
Console.Write("Enter the second number: ")
number2 = Console.ReadLine
'If statements to determine and keep track of highest and lowest number
If number1 > number2 Then
Console.WriteLine("Number 1 is larger " & number1)
ElseIf number2 > number1 Then
Console.WriteLine("Number 2 is larger " & number2)
Else
Console.WriteLine("The two numbers are equal: " & number1 & " " & number2)
End If
If number1 > highnum Then
highnum = number1
End If
If number1 < lownum Then
lownum = number1
End If
If number2 > highnum Then
highnum = number2
End If
If number2 < lownum Then
lownum = number2
End If
Next
'Displaying highest and lowest numbers
Console.WriteLine("The highest number entered was " & highnum)
Console.WriteLine("The lowest number entered was " & lownum)
'Prompting user for input to continue
Console.WriteLine("Press any key to continue: ")
Console.ReadKey()
End Sub
Just check your counter variable. If it is 1, then set low and high to your values appropriately. If it is greater than 1, then do the comparisons:
For counter As Integer = 1 To 10
' ... other code ...
If counter = 1 Then
lownum = Math.Min(number1, number2)
highnum = Math.Max(number1, number2)
Else
lownum = Math.Min(lownum, Math.Min(number1, number2))
highnum = Math.Max(highnum, Math.Max(number1, number2))
End If
Next
You testing for values being less or greater then your two starting variables of 0.
If you don't give the min/max values and don't enter values < 0 or > 0 then the values of Min/Max are NEVER set.
So you want to set these to the max allowable and min allowable values.
Change you variable declares to this with initialization of the min/max and your code should work just fine.
Dim number1 As Integer = 0
Dim number2 As Integer = 0
Dim lownum As Integer = Integer.MaxValue
Dim highnum As Integer = Integer.MinValue

VBA Write a loop that creates Random Numbers converts them to ASCII, and concatenates it to create an eight character key

I'm able to create the random number between 33 and 126 and convert it to its corresponding character/ASCII code but I cannot repeat these steps to generate an eight character key, i know i need some sort of a loop.
This is what i have done so far:
RandomNumber = RandomClass.Next()
RandomNumber = RandomClass.Next(33, 126)
Key.Text = RandomNumber
Key2.Text = Chr(Int(Key.Text))
End Sub
Function Random()
Dim arr(7) As Integer
Dim i As Integer, j As Integer
i = 1
Do
RandomNumber = RandomClass.Next()
RandomNumber = RandomClass.Next(33, 126)
RandomNumber(i) = RandomNumber(i) + RandomNumber(i - 1)
i = i + 1
Loop Until i = 8
Return RandomNumber
End Function
End Class

Automatic Calculation with given numbers

I would like to make CPU to calculate declared result from the given numbers that are also declared.
So far:
Dim ArrayOperators() As String = {"+", "-", "*", "/", "(", ")"}
Dim GlavniBroj As Integer = GBRnb() 'Number between 1 and 999 that CPU needs to get from the numbers given below:
Dim OsnovniBrojevi() As Integer = {OBRnb(), OBRnb(), OBRnb(), OBRnb()} '4 numbers from 1 to 9
Dim SrednjiBroj As Integer = SBRnb() '1 number, 10, 15 or 20 chosen randomly
Dim KrajnjiBroj As Integer = KBRnb() '25, 50, 75 or 100 are chosen randomly
Private Function GBRnb()
Randomize()
Dim value As Integer = CInt(Int((999 * Rnd()) + 1))
Return value
End Function
Private Function OBRnb()
Dim value As Integer = CInt(Int((9 * Rnd()) + 1))
Return value
End Function
Private Function SBRnb()
Dim value As Integer = CInt(Int((3 * Rnd()) + 1))
If value = 1 Then
Return 10
ElseIf value = 2 Then
Return 15
ElseIf value = 3 Then
Return 20
End If
Return 0
End Function
Private Function KBRnb()
Dim value As Integer = CInt(Int((4 * Rnd()) + 1))
If value = 1 Then
Return 25
ElseIf value = 2 Then
Return 50
ElseIf value = 3 Then
Return 75
ElseIf value = 4 Then
Return 100
End If
Return 0
End Function
Is there any way to make a program to calculate GlavniBroj(that is GBRnb declared) with the help of the other numbers (also without repeating), and with help of the given operators? Result should be displayed in the textbox, in a form of the whole procedure of how computer got that calculation with that numbers and operators. I tried to make it work by coding operations one by one, but that's a lot of writing... I'm not looking exactly for the code answer, but mainly for the coding algorithm. Any idea? Thanks! :)

How to compare Strings for Percentage Match using vb.net?

I am banging my head against the wall for a while now trying different techniques.
None of them are working well.
I have two strings.
I need to compare them and get an exact percentage of match,
ie. "four score and seven years ago" TO "for scor and sevn yeres ago"
Well, I first started by comparing every word to every word, tracking every hit, and percentage = count \ numOfWords. Nope, didn't take into account misspelled words.
("four" <> "for" even though it is close)
Then I started by trying to compare every char in each char, incrementing the string char if not a match (to count for misspellings). But, I would get false hits because the first string could have every char in the second but not in the exact order of the second. ("stuff avail" <> "stu vail" (but it would come back as such, low percentage, but a hit. 9 \ 11 = 81%))
SO, I then tried comparing PAIRS of chars in each string. If string1[i] = string2[k] AND string1[i+1] = string2[k+1], increment the count, and increment the "k" when it doesn't match (to track mispellings. "for" and "four" should come back with a 75% hit.) That doesn't seem to work either. It is getting closer, but even with an exact match it is only returns 94%. And then it really gets screwed up when something is really misspelled. (Code at the bottom)
Any ideas or directions to go?
Code
count = 0
j = 0
k = 0
While j < strTempName.Length - 2 And k < strTempFile.Length - 2
' To ignore non letters or digits '
If Not strTempName(j).IsLetter(strTempName(j)) Then
j += 1
End If
' To ignore non letters or digits '
If Not strTempFile(k).IsLetter(strTempFile(k)) Then
k += 1
End If
' compare pair of chars '
While (strTempName(j) <> strTempFile(k) And _
strTempName(j + 1) <> strTempFile(k + 1) And _
k < strTempFile.Length - 2)
k += 1
End While
count += 1
j += 1
k += 1
End While
perc = count / (strTempName.Length - 1)
Edit: I have been doing some research and I think I initially found the code from here and translated it to vbnet years ago. It uses the Levenshtein string matching algorithm.
Here is the code I use for that, hope it helps:
Sub Main()
Dim string1 As String = "four score and seven years ago"
Dim string2 As String = "for scor and sevn yeres ago"
Dim similarity As Single =
GetSimilarity(string1, string2)
' RESULT : 0.8
End Sub
Public Function GetSimilarity(string1 As String, string2 As String) As Single
Dim dis As Single = ComputeDistance(string1, string2)
Dim maxLen As Single = string1.Length
If maxLen < string2.Length Then
maxLen = string2.Length
End If
If maxLen = 0.0F Then
Return 1.0F
Else
Return 1.0F - dis / maxLen
End If
End Function
Private Function ComputeDistance(s As String, t As String) As Integer
Dim n As Integer = s.Length
Dim m As Integer = t.Length
Dim distance As Integer(,) = New Integer(n, m) {}
' matrix
Dim cost As Integer = 0
If n = 0 Then
Return m
End If
If m = 0 Then
Return n
End If
'init1
Dim i As Integer = 0
While i <= n
distance(i, 0) = System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
Dim j As Integer = 0
While j <= m
distance(0, j) = System.Math.Max(System.Threading.Interlocked.Increment(j), j - 1)
End While
'find min distance
For i = 1 To n
For j = 1 To m
cost = (If(t.Substring(j - 1, 1) = s.Substring(i - 1, 1), 0, 1))
distance(i, j) = Math.Min(distance(i - 1, j) + 1, Math.Min(distance(i, j - 1) + 1, distance(i - 1, j - 1) + cost))
Next
Next
Return distance(n, m)
End Function
Did not work for me unless one (or both) of following are done:
1) use option compare statement "Option Compare Text" before any Import declarations and before Class definition (i.e. the very, very first line)
2) convert both strings to lowercase using .tolower
Xavier's code must be correct to:
While i <= n
distance(i, 0) = System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
Dim j As Integer = 0
While j <= m
distance(0, j) = System.Math.Min(System.Threading.Interlocked.Increment(j), j - 1)
End While

Do While - Overflow error

This code looks for the column with header "Quantity Dispensed," then convert the strings in the column by treating the right three digits as decimals, e.i. 00009102" = 9.102
Sub ConvertDec()
Dim colNum As Integer
Dim i As Integer
Dim x As Integer
colNum = WorksheetFunction.Match("Quantity Dispensed", ActiveWorkbook.ActiveSheet.Range("1:1"), 0)
i = 2
Do While ActiveWorkbook.ActiveSheet.Cells(i, colNum).Value <> ""
x = Evaluate(Cells(i, colNum).Value)
Cells(i, colNum) = Int(x / 1000) + (x Mod 1000) / 1000
i = i + 1
Loop
End Sub
I'm getting Overflow error on the line "x = Evaluate..." while executing.
The values in the column are in string form. e.g. "0000120000".
120000 is greater than the maximum value of integer 32768. Use the Long type instead.
Simoco