# Rnd

## Synopsis

```
Rnd[(number)]
```

### Arguments

The optional number argument can be any valid numeric expression.

## Description

The Rnd function returns a value less than 1 but greater than or equal to 0. The number of digits in this number is platform-dependent. Trailing zeros are deleted.

Rnd generates a pseudo-random number by calculating successive numbers from a seed number supplied by the number argument. Thus, the value of number determines how Rnd generates a random number. Rnd with no argument or Rnd with a positive number generate random numbers from a randomized seed. Therefore, successive executions of Rnd with the same positive number return different values. However, if number is zero or a negative number, each successive call to the Rnd function uses the same seed, and thus generates a predictable value.

If Number Is | Rnd Generates |
---|---|

Less than zero | The same number every time, using number as the seed. Thus, for example, -7 always generates .5976062. |

Greater than zero | The next random number in the sequence. |

Equal to zero | The most recently generated random number. |

Not supplied | The next random number in the sequence. |

To maximize randomness, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer. Then call Rnd.

## Notes

To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.

## Examples

The following example generates twenty random numbers.

For I = 1 To 20 Println Rnd Next Println "Done"

The following example generates a random integer in the range 1 through 10, inclusive:

Dim upperbound,lowerbound upperbound = 10 lowerbound = 1 Println Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

The following example shows the effects of specifying 0 as the number argument:

For I = 1 To 10 Println Rnd Println Rnd(0) Next Println "Done"

In this case, the argumentless Rnd generates a random number, and the Rnd(0) repeats the most-recently-generated random number.

The following example shows the effects of specifying a negative number as the number argument:

For I = 1 To 10 Println Rnd Println Rnd(-7) Next Println "Done"

In this case, the first argumentless Rnd generates a random number, and the Rnd(-7) calculates its corresponding value and provides this as the seed for the next random number. Thus in the above example, the first call to Rnd is actually random; all subsequent calls are based on the seed of -7, and therefore repeat predictably in each loop.

## See Also

Randomize statement