![]() |
random?
Hi,
A friend asked me this question regarding random numbers in VBA If you run the following code: Sub rand() A = 1 For i = 1 To 10 Cells(A, 3) = rnd A = A + 1 Next i End Sub 10 random numbers will appear in A1:A10 Now hit the stop (reset) button in the VBA window and run again. The exact same 'random' numbers appear. Is this expected? Is there a work-around? Regards - Dave. |
random?
you need to use the keyword, randomize .
Sub rand() A = 1 For i = 1 To 10 Randomize Cells(A, 3) = Rnd A = A + 1 Next i End Sub -- Gary "Dave B" wrote in message ... Hi, A friend asked me this question regarding random numbers in VBA If you run the following code: Sub rand() A = 1 For i = 1 To 10 Cells(A, 3) = rnd A = A + 1 Next i End Sub 10 random numbers will appear in A1:A10 Now hit the stop (reset) button in the VBA window and run again. The exact same 'random' numbers appear. Is this expected? Is there a work-around? Regards - Dave. |
random?
Even better - place the randomize statement outside of the loop.
-- Jim Cone Portland, Oregon USA "Gary Keramidas" wrote in message you need to use the keyword, randomize . Sub rand() A = 1 For i = 1 To 10 Randomize Cells(A, 3) = Rnd A = A + 1 Next i End Sub -- Gary |
random?
Thanks guys.
I guess that randomizes the 'random' numbers. Regards - Dave. |
random?
As a side note, the Worksheet version doesn't need to be Randomized in Excel 2007. Not as efficient though if you have to do this often.
Note that since 'A' & 'i' are equal in your loop, you can eliminate one of the variables. Sub Demo() Dim R As Long '(R)ow For R = 1 To 10 Cells(R, 2) = [Rand()] Next R End Sub A = 1 For i = 1 To 10 A = A + 1 Next i -- Dana DeLouis "Dave" wrote in message ... Thanks guys. I guess that randomizes the 'random' numbers. Regards - Dave. |
random?
On Tue, 30 Sep 2008 05:51:01 -0700, Dave
wrote: Thanks guys. I guess that randomizes the 'random' numbers. Regards - Dave. It randomizes the initial seed for the random number generator. |
All times are GMT +1. The time now is 02:48 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com