Just write Randomize before rnd. That will solve the problem.
I want to load a label with a random selection from a listbox with 22 items in the list. Currently I am using
intRand = Int(Rnd * 22)
List1.ListIndex = intRand
List1.Selected(intRand) = True
Label1 = List1
the problem is, VB's RND function returns the same exact random number sequence every time the program is run - that's not very Random, is it?
5.33424
5.795186
2.895625
etc
etc
Anyone help me get a real Random selection??
Just write Randomize before rnd. That will solve the problem.
Ok, if I use
intRand = Int(randomize(Rnd * 22))
List1.ListIndex = intRand
List1.Selected(intRand) = True
Label1 = List1
which is what I think you are saying..
I get an error - Expected expression or variable for Randomize.
Now if do this,
Dim intRand As Integer
Dim strnewword As String
Dim intBoxIndex As Integer
intRand = Int(Rnd * 22)
List1.ListIndex = intRand
Randomize (intRand)
List1.Selected(intRand) = True
Label1 = List1
I get a different order the words come in, but its still the same exact order everytime I load my project. Is there still something I'm doing wrong, thanks for you help..
Seed the random number generator using a non deterministic source. Eg, system time.
Old Systems retired due to new car
New system at design/prototype stage on BeagleBoard.
actually what I decided to do was use a timer with a 50 millisecond interval to keep things random.
intRand = Int(Rnd * mintTimer)
List1.ListIndex = intRand
Label1 = List1
Private Sub Timer1_Timer()
'Generate random multiplier w/ timer
If mintTimer < 20 Then
mintTimer = mintTimer + 1
Else
mintTimer = 0
End If
End Sub
Thank you guys very much!
Nah dude with the randomize he means in you code write it like this:
Randomize
intRand = Int(Rnd * 22)
List1.ListIndex = intRand
List1.Selected(intRand) = True
Label1 = List1
I was about to recommend that as wellOriginally Posted by Rob Withey
i believe randomize starts the sequence somewhere new based on the system clock's miliseconds or something like that. so it isnt truely random, but the number of numbers in the sequence is in like the trillians or something
sorry to revive a dead thread but I have some insight to the problem to experience.
When you call the Rnd functions it keeps in memory the value that was used to generate the random number. Calling Rnd again will give you the same number. Calling Randomize resets the stored value for generating the random number just like Eubey has suggested.
If you take a look at C or C# as an example, to randomize a number in these enviroments you need to specify a seed and most programmers use the System Time to generate it. Since VB is just simplified and no built in seed method exists the Randomize functions is the way to reset the seed.
Bookmarks