PDA

View Full Version : Use of Randomize statement



mikerickson
10-22-2007, 06:45 PM
Considering the following code, am I correct that

1) Line A is not needed, but both B and C are.

2) This is equivilant to randomTotal = Int(50000 * Rnd())


Dim didget(0 To 4) As Integer, i As Integer, randomTotal As Long

Randomize: Rem line A
For i = 0 To 3
Randomize: Rem line B
didget(i) = Int(10 * Rnd())
Next i
Randomize: Rem line C
didget(4) = Int(5 * Rnd())

For i = 0 To 4
randomTotal = randomTotal + didget(i) * (10 ^ i)
Next iThanks in advance.

johnske
10-22-2007, 07:12 PM
yes line A is not really needed but B and C are. to obtain more truly random numbers, randomize is needed to initialize Rnd functions random number generator and get a new seed. if not used, the Rnd function uses the same number as a seed the first time it is called and thereafter uses the last generated number as a seed value.

herzberg
10-22-2007, 07:56 PM
Considering the following code, am I correct that
2) This is equivilant to randomTotal = Int(50000 * Rnd())

I've tried it out step-by-step and there's a small difference. The loop method will return a number that is larger than the single line method. My workings are in the attached workook; I hope I have interpreted the question correctly.

mikerickson
10-23-2007, 07:43 AM
Thanks all. I need to randomly select a number < 100! and this looks like the right technique.