I am a math geek so I like this example. In a form I added 9 buttons arranged like they might be on a calculator or phone and add the following code to the form:
CODE
Option Explicit
Private Sub Form_Load()
Dim i As Integer
Dim v As Integer
For i = 0 To 8
Command1(i).Caption = ""
Next i
AssignCaptions
End Sub
Private Sub Command1_Click(Index As Integer)
AssignCaptions
End Sub
'Randomly assign each button a number
'With no repetes...
Private Sub AssignCaptions()
Dim i As Integer
Dim v As Integer
Randomize Timer
v = Rnd * 8
For i = 0 To 8
v = (4 * v + 4) Mod 9
Command1(i).Caption = v + 1
Next i
End Sub
THe heart of this is something called a Linear Congruential Generator and they are the same things that are used is most psudo-random number generators out there.
The formula v=(4*v+4) mod 9 is a LCG and it will generate a nice sequence of scambled numbers.
Problem. In my rush to apply math I didn't think this though very well. Each time it generated the same sequence 1, 5, 3, 4, 8, 6, 7, 2, 9 but starting at different points (8, 6, 7, 2, 9, 1, 5, 3, 4 for example). The LCG generates a sequence of numbers and the initial value of v causes the shift, but if the other two values (v=(a*v+b ) mod m) in this case a and b do not change then the sequence is the same except the shift.
So here is another version. Yea, I know it is not the simple solution you were looking for but I am having too much fun to stop.
Here is the corrected sub:
CODE
'Randomly assign each button a number
'With no repetes...
Private Sub AssignCaptions()
Dim i As Integer
Dim v As Integer
Dim a As Integer
Dim b As Integer
Randomize Timer
b = Rnd * 8 + 1
a = (Int(Rnd * 2) + 1) * 3 + 1
If b = 1 Or b Mod 3 = 0 Then b = b + 1
v = Rnd * 9
For i = 0 To 8
v = (a * v + b) Mod 9
Command1(i).Caption = v + 1
Next i
End Sub
I know this is not helping. Sorry.
This post has been edited by NickDMax: 18 Feb, 2007 - 05:31 PM