Name
Randomize Statement
Syntax
Randomize [number]-
number Use: Optional
Data Subtype: Numeric
Any valid numeric expression.
Description
Initializes the random number generator.
Rules at a Glance
Randomizeusesnumberas a new seed value to initialize the random number generator used by theRndfunction. The seed value is an initial value that generates a sequence of pseudo-random numbers.If you don’t pass
numberto theRandomizestatement, the value of the system timer is used as the new seed value.Repeatedly passing the same number to
Randomizedoesn’t causeRndto repeat the same sequence of random numbers.If
Randomizeis not used and theRndfunction is called either with no argument or with 1 as an argument, theRndfunction always uses the same number as the seed value the first time it is called, and subsequently uses the last generated number as a seed value.
Programming Tips & Gotchas
If you need to repeat a sequence of random numbers, you should call
the Rnd function with a negative number as an
argument immediately prior to using Randomize with
any numeric argument. This is illustrated in the example program.
Example
Public Sub RepeatNumbers( )
dim arr(9,3)
dim loopCtr, intctr
dim strMsg
for loopCtr = 0 to 3
Rnd(-1)
Randomize(100)
for intCtr = 0 to 9
strMsg = strMsg & rnd( ) & " "
next
strMsg = strMsg & vbcrlf
next
MsgBox strMsg
End SubSee Also
| Rnd Function |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access