Syntax:
random[IZE] [expr]
Description:
This statement "seeds" the random number generator: this affects the sequence of values which are subsequently returned by the rnd
function and the maybe
function.
The numbers returned by rnd
and maybe
are not truly random, but follow a "pseudo-random" sequence which is uniquely determined by the seed number. If you use the same seed number on two different occasions, you'll get the same sequence of "random" numbers both times. For example:
cls
for i = 1 to 2
randomize 325
for j = 1 to 10
print rnd(50),
next
print
next
The program above seeds the random number generator twice with the same number (325). If you run this program, you'll find that it produces the same sequence of 10 random numbers after each seeding.
Seeding the random number generator with a pre-specified number can be useful in cases where you specifically want to produce a repeatable sequence of random numbers. In most cases, however, you will probably prefer a sequence that is unpredictable. In that case, you should omit the expr
parameter. When you execute random
without any expr
parameter, the system's current time is used to seed the random number generator. Since the system clock changes very quickly, it's essentially impossible to predict what value will be used as the seed-this is the best way to get the "most random" random numbers.
Normally, you will execute random
only once in your program, unless you wish to repeat a specific sequence of random numbers. If you don't execute random
at all, the random number generator is seeded with the current tick count. Remember that re-seeding with the same number will cause your program to generate the same sequence of random numbers each time it's run.
See Also:
rnd; maybe