Random Numbers

To generate random numbers you can use the Randomize statement and the Rnd function.


Randomize Statement

RANDOMIZE - Initialises the random number generator.
The Randomize statement is used to set the seed value for VBA's random number generator.
Randomize can take a numeric argument as a seed.
If a seed value is not provided, Randomize will use the result of the Timer function as the seed.


Rnd Function

RND - Returns a random number between 0 and 1 (Single).
The Rnd function returns a number greater than or equal to 0 and less than 1 from a sequence of pseudo-random numbers.
When this function is called after starting the application it will always return the same sequence of numbers.
Using the Randomize statement improves the results and should be called every time before Rnd is called.
Restarting the application will restart the sequence.
Using Randomize with a specific number will alter the sequence in a predictable way.
Using Randomize with no argument uses the result of the Timer function as the seed value and makes the Rnd function less predictable.

Passing the Rnd function a number less than 0 will return the same number every time.

Passing the Rnd function the number 0 will return the previous number returned by the Rnd function.

Passing the Rnd function a number greater than 0 will return the next number in the pseudo-random number generator's sequence.

Passing no number into the Rnd function will return the next number in the random number generator's sequence.


Excel Function

Excel > Functions User Defined > RANDOMNUMBER


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext