Returns a pseudo random number.
...Rnd ( seed )
Prerequisites
None
Parameters
seed
An optional expression that evaluates to any numerical data type, e.g. Integer, Single, Double.
Remarks
Returns a pseudo random number whose value is greater than or equal to 0 and less than 1.0.
The returned value is only pseudo random because the returned numbers are part of an extremely long sequence of values that only repeat after 2^32 numbers are generated. Each time that the controller is restarted, the starting point or seed in the sequence is determined by the system clock calendar. So, the sequence of values produced by this function appears quite random for normal testing purposes.
If it is desired to force the sequence of numbers to restart at a fixed value, thereby allowing a test to be exactly repeated, the optional seed parameter can be used as shown below.
seed | Effect on function |
---|---|
<0 |
The specified seed value is taken as the starting point for the pseudo random sequence and the sequence will be continued from this value. The number returned by this execution of the Rnd will always be the same. |
=0 |
The last value returned by the Rnd function will be returned again. |
>0 |
The next number in the pseudo random sequence will be returned. |
Not specified |
Same as specifying a seed value >0. |
Examples
Dim r_val As Single
r_val = Rnd() ' Sets r_val to some random value
r_val = Rnd(-1) ' Forces seed to –1, will return same number
' each time.
r_val = Rnd() ' Returns next value after seed
r_val = Rnd(0) ' Returns same value as last line above
See Also