Originally Posted by
Micron
Don't have time to study this too much, but can make a suggestion. Step through the code and after a line is executed, evaluate the expression or variable in the immediate window, such as
?val(lucky(t) = Int((maxNum - minNum + 1) * Rnd) + minNum)
?lucky(t)
?rnd
?t-1
?lucky(i)
?lucky(t)
and so on. That might help you to figure it out. Can you post back when you figure it out? Don't forget - i and t represent positions in the arrays - not variables for values per se.
I apologize for not getting back sooner. I have been swamped and had not been able to look at the code. I stepped through it, and I think I get it. If someone could confirm my understanding, I would really appreciate it. By the way,
You can also turn on Locals and step through the code. You will see the values of variables change.
Was immensely helpful. I had never hear of Local, but finding it was easy enough and really gave me what I needed.
Code:
Sub Lotto()
Const spins = 6
Const minNum = 1
Const maxNum = 54
Dim t As Integer 'looping variable in outer loop
Dim i As Integer 'looping variable in inner loop
Dim myNumbers As String 'string to hold all picks
Dim lucky(spins) As String 'array to hold generated picks
myNumbers = ""
For t = 1 To spins
Randomize
lucky(t) = Int((maxNum - minNum + 1) * Rnd) + minNum
'check if this number was picked before
For i = 1 To (t - 1)
If lucky(t) = lucky(i) Then
lucky(t) = Int((maxNum - minNum + 1) * Rnd) + minNum
i = 0
End If
Next i
MsgBox "Lucky number is " & lucky(t), , "Luckynumber " & t
myNumbers = myNumbers & " - " & lucky(t)
Next t
MsgBox "Lucky numbers are " & myNumbers, , "6 Lucky Numbers"
End Sub
So, lucky is the array. t & i either feed the values into the array or query it. This code:
Code:
For t = 1 To spins
Randomize
lucky(t) = Int((maxNum - minNum + 1) * Rnd) + minNum
Sets the array to 1-6, due to the spins constant and creates a random number for each slot of the array, starting by itself for the first number and then again each time it reaches the "Next t" portion of the code until it fills slot 6.
This code:
Code:
For i = 1 To (t - 1)
If lucky(t) = lucky(i) Then
lucky(t) = Int((maxNum - minNum + 1) * Rnd) + minNum
i = 0
End If
Looks at the items in the lucky array to see if the currently held value of t matches any of the values stored there, and, if so, run the generator again. The biggest problem I was running into was understanding
Code:
For i = 1 To (t - 1)
Which now I get will look at each value in the array one at a time, and will do so t number of times, which t is the current number of times numbers have been fed into the array, minus one, because there will never need to look at the array the first time, because t currently has the only existing value, and will also never need to look at the array 6 times, because it will at most need to look at the previous 5 numbers stored there and the current value of t.
Next i causes the program to look at the next number in the array until it reaches t - 1.
If I got anything wrong and anyone has time to tell me what, I am happy to learn. I would like to thank all of you for forcing me to figure this out with just little hints instead of explaining it. Thinking this thing through was much more useful than any explanation would have been.
Thanks all!