Re: randomize -- why?????
- From: "Bill Plenge" <B_Plenge@xxxxxxxxxx>
- Date: Sat, 09 Sep 2006 02:31:52 GMT
Takashi Yamauchi wrote:
When I use "Randomize" multiple times in a procedure, I get not so
random numbers
********
Private Sub CreateRandomData()
Dim i As Integer
For i = 1 To 5000
Randomize
Cells(i, 1).Value = Rnd
Randomize
Cells(i, 2).Value = Rnd
Next
End Sub
****
But when I use "Randomize" once in a procedure, I get nice random
numbers
********
Private Sub CreateRandomData()
Dim i As Integer
Randomize
For i = 1 To 5000
Cells(i, 1).Value = Rnd
Cells(i, 2).Value = Rnd
Next
End Sub
****
Why is this the case? I should declare "Randomize" only once in a
procedure? I should declare "Radomize" once in a project too??????
Could someone tell me why?
Thank you
takashi yamauchi
As the docs say:
Syntax
Randomize [number]
The optional numberargument is aVariant or any validnumeric expression.
Remarks
Randomize uses number to initialize the Rnd function's random-number
generator, giving it a newseed value. If you omit number, the value returned
by the system timer is used as the new seed value.
The last line there is the cause of what you're seeing, the system timer is
only accurate to 1/18 of a second. In the following code from your example
....
Randomize
Cells(i, 1).Value = Rnd
Randomize
Cells(i, 2).Value = Rnd
It is very likely both Randomize statements are executing in the same 1/18
sec clock tick, since both Rnd satements are being generated from the same
seed the Cells(i, 1) and Cells(i, 2) will be the same value. The pattern of
diagnol lines in your post seems to show that this is what's happening. The
"stray dots" are from when the timing is such that the 2 Randomize
statements are executed in different system clock ticks. Which looks to be
happening about 1 in 18 times -- exactly as would be expected.
The solution: Use Randomize only once in the project, prefereably when it
initially loads.
Hope this helps.
Best,
Bill
.
- Follow-Ups:
- Re: randomize -- why?????
- From: Takashi Yamauchi
- Re: randomize -- why?????
- Prev by Date: Logic Question
- Next by Date: Re: How can i create 2-dimensional arrays of control button ?
- Previous by thread: Logic Question
- Next by thread: Re: randomize -- why?????
- Index(es):
Relevant Pages
|