Re: randomize -- why?????



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


.



Relevant Pages

  • Re: VB-6
    ... Private Sub Command2_Click ... and he wants to be able to do that for many different seed values: for the Randomize function. ... (I presume using a negative value in the Rnd: function **after** seeding the Randomize function took care of forcing: the repeated sequences. ...
    (microsoft.public.vb.general.discussion)
  • Re: VB-6
    ... > Private Sub Command2_Click ... > Debug.Print Rnd, ... the same running programming. ... function with a negative argument before running the Randomize statement ...
    (microsoft.public.vb.general.discussion)
  • Re: Is there something wrong with the random generator in Excel?
    ... "Rick Rothstein" wrote: ... Randomize or Rnd functions. ...
    (microsoft.public.excel.programming)
  • Re: VB-6
    ... Debug.Print Rnd ... for the Randomize function. ... function **after** seeding the Randomize function took care of forcing ... the repeated sequences. ...
    (microsoft.public.vb.general.discussion)
  • Re: Creating a button...
    ... Private Sub CommandButton1_Click ... Dim rand_dice As Double ... rand_dice = Rnd() ... Mark, just to make things better, please add randomize prior to the ...
    (microsoft.public.excel.misc)