MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Rnd and Randomize


Posted by Russ on January 06, 2002 8:41 AM

I want to create a loop of 100 random numbers that will create the same 100 random numbers every time I run the program.

VBA help claims that it can be done, but I can't do it.

Any comments would be appreciated.


Posted by Dank on January 06, 2002 3:13 PM

Russ,

Try this code (untested):-

Sub CreateSameNums()
Dim cl As Long, x As Long

cl = ActiveSheet.UsedRange.Columns.Count + 3

Rnd -2

Randomize 5

For x = 1 To 100
Cells(x, cl) = Int(Rnd(1) * 50)
Next x

End Sub

HTH,
Daniel.

Posted by Dank on January 06, 2002 3:16 PM

Russ,
I've just tested that code. The Activesheet.Usedrange bit can be deleted. This works:-

Sub CreateSameNums()
Dim cl As Long, x As Long

Rnd -2
Randomize 5

For x = 1 To 100
Cells(x, 1) = Int(Rnd(1) * 50)
Next x


End Sub

Regards,
Daniel.

Posted by russ on January 06, 2002 4:55 PM

Daniel,

WOW.

Thank you so much. I won't tell you how long I spent trying to make this work ( it would embarrass me too much). With your example, I now understand what the VBA help on Rnd meant.

Thanks

Russ