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
Randomize
usesnumber
as a new seed value to initialize the random number generator used by theRnd
function. The seed value is an initial value that generates a sequence of pseudo-random numbers.If you don’t pass
number
to theRandomize
statement, the value of the system timer is used as the new seed value.Repeatedly passing the same number to
Randomize
doesn’t causeRnd
to repeat the same sequence of random numbers.If
Randomize
is not used and theRnd
function is called either with no argument or with 1 as an argument, theRnd
function 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 Sub
See Also
Rnd Function |
Get VBScript in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.