Rnd

Generate a pseudorandom number, a value between 0 and 1 (less than 1 but greater than or equal to zero.)

Syntax
      Rnd[(number)]

Key
   number    This argument is a Single or any valid numeric
             expression, it determines how Rnd will generates
             a random number:

If number is

Rnd generates

Less than zero

The same number every time, using number as the seed.

Greater than zero

The next random number in the sequence.

Equal to zero

The most recently generated number.

Not supplied

The next random number in the sequence.

For any given initial seed, the same number sequence is generated because each successive call to the Rnd() function uses the previous number as a seed for the next number in the sequence.

Before calling Rnd, use the Randomize statement to initialize the random-number generator with a seed based on the system timer.

To produce random integers in a given range, use this formula:

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

The Rnd() function may be used in VBA or in an SQL query.

Examples

Dim intDemo as Integer
Randomize
'A random number between 1 and 6
intDemo = Int ((6 - 1 + 1) * Rnd + 1)

'A random number between 1 and 100
intDemo = Int ((100 - 1 + 1) * Rnd + 1)

'A random number between 50 and 100
intDemo = Int ((100 - 50 + 1) * Rnd + 50)

“All human actions have one or more of these seven causes: chance, nature, compulsions, habit, reason, passion and desire” ~ Aristotle

Related:

Asc - The Ascii code of a character.
Equivalent Powershell command: Get-Random
Random Numbers in CMD or .js


© Copyright SS64.com 1999-2014
Some rights reserved