Returning a pseudo-random numberΒΆ
rand() |
Return a pseudo-random number |
SYNTAX
rand(seed)
Returns a pseudo-random number in the range 0-32767.
If seed is zero, this function returns the next pseudo-random number in the current sequence.
If seed is non-zero, then the random number generator uses seed to set the starting point for the following pseudo-random number sequence, and returns the first number in that sequence. The sequence is followed until and unless another call of rand() with a non-zero value for seed is encountered within the program.
Normally, the first call to rand() within any program contains a non-zero value for seed, since unless the function has been seeded it will produce the same sequence of numbers every time. Similarly, the value of seed must itself be random if the sequence of numbers is to vary from one execution of the program to another. One method of generating a value for seed is to set it to the value of one of the Sculptor system variables sys.SysTime or sys.LocalTime.
Once the function has been seeded, rand(0) produces the next number in the sequence, and may be used for all subsequent calls to the function within the program.
NOTES
The number sequence generated by any particular seed is always identical. This is the case with all pseudo-random generators.
The actual pseudo-random number sequence for any given seed may vary from one machine to another.
To return a random number within a predefined range, use the modulus function. The formula is ((rand(0) % a) +b), where a is the total number of integers in the range, and b is the lowest number to be included. In practice b is usually 1. For example, (rand(0) % 50) + 1 generates a random number between 1 and 50.
EXAMPLE
!temp Lottery,,u1[6]
!temp RandNo,,i2
!temp Count,,u1
+textbox LotBox at 16,4 {
field = tmp.Lottery
title "Lottery number"
rows = 6
}
rand(sys.SysTime) /* seed the generator outside the loop */
for (tmp.Count = 1;tmp.Count < 7; tmp.Count = tmp.Count + 1) {
tmp.Lottery[tmp.Count] = (rand(0) % 49) + 1
display LotBox[tmp.Count]
}