Randomize Statement

See AlsoB9B611              Example1JEI0MH>Low

Initializes the random-number generator.


Randomize [number]


The argument number can be any valid numeric expression71RISN.  Number is used to initialize the random-number generator by giving it a new seed3F5TPW7 value.  If you omit number, the value returned by the Timer function is used as the new seed value.

If Randomize is not used, the Rnd function returns the same sequence of random numbers every time the program is run.  To have the sequence of random numbers change each time the program is run, place a Randomize statement with no argument at the beginning of the program.

See Also

Rnd FunctionLANRND

Timer FunctionQ3GPQ6

Randomize Statement Example

The example simulates rolling a pair of dice by generating random values from 1 to 6.  Each time this program is run, Randomize uses the Timer function to generate a new random-number sequence.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

   Dim Dice1, Dice2, Msg              ' Declare variables.

   Randomize                          ' Seed random number generator.

   Dice1 = Int(6 * Rnd + 1)           ' Generate first die value.

   Dice2 = Int(6 * Rnd + 1)           ' Generate second die value.

   Msg = "You rolled a " & Dice1

   Msg = Msg & " and a " & Dice2

   Msg = Msg & " for a total of "

   Msg = Msg & (Dice1 + Dice2) & "."

   MsgBox Msg                         ' Display message.

End Sub