Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
random?
Thanks guys.
I guess that randomizes the 'random' numbers. Regards - Dave. |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Random phantom breakpoints and random bogus compile errors | Excel Programming | |||
Sorting random Data created from a random formula | Excel Discussion (Misc queries) | |||
Non-random numbers generated by excel's data analysis random gener | Excel Worksheet Functions | |||
Non-random numbers generated by excel's data analysis random gener | Excel Discussion (Misc queries) | |||
How do I find random number in list of random alpha? (Position is. | Excel Discussion (Misc queries) |