Documentation |
Generate random integer numbers
This functionality does not run in MATLAB.
random() random(n_{1} .. n_{2}) random(n)
random() returns a random integer number between 0 and 10^{12}.
random(n1..n2) returns a procedure that generates random integers between n1 and n2.
The calls random() return uniformly distributed random integers between 0 and 999999999988 (approximately 10^{12}).
r := random(n1..n2) produces a random number generator r. Subsequent calls r() generate uniformly distributed random integers between n1 and n2.
random(n) is equivalent to random(0 .. n - 1).
The global variable SEED is used for initializing or changing the sequence of random numbers. It may be assigned any non-zero integer. The value of SEED fixes the sequence of random numbers. This may be used to reset random generators and reproduce random sequences.
SEED is set to a default value when MuPAD^{®} is initialized. Thus, each time MuPAD is started or re-initialized with the reset function, the random generators produce the same sequence of numbers.
To get a non-predictable initial value, make it dependent on the current time. See Example 5.
Several random generators produced by random may run simultaneously. All generators make use of the same global variable SEED.
For producing uniformly distributed floating-points numbers, it is recommended to use the faster function frandom instead. The stats library provides random generators with various other distributions. Cf. Example 4.
random as well as the random number generators created by it are sensitive to the environment variable SEED.
random and the random number generators created by it change the environment variable SEED on each call.
The following call produces a sequence of random integers. Note that an index variable i must be used in the construction of the sequence. A call such as random() $8 would produce 8 copies of the same random value:
random() $ i = 1..8
The following call produces a "die" that is rolled 20 times:
die := random(1..6): die() $ i = 1..20
The following call produces a "coin" that produces "head" or "tail":
coin := random(2): coin() $ i = 1..10
subs(%, [0 = head, 1 = tail])
delete dice, coin:
random is sensitive to the global variable SEED which is set and reset when MuPAD is (re-)initialized. The seed may also be set by the user. Random sequences can be reproduced by starting with a fixed SEED:
SEED := 1: random() $ i = 1..4
SEED := 1: random() $ i = 1..4
random allows you to create several random number generators for different ranges of numbers, and to use them simultaneously:
r1 := random(0..4): r2 := random(2..9): [r1(), r2()] $ i = 1..6
delete r1, r2:
random can be used to build a random generator for uniformly distributed floating-point numbers. The following generator produces such numbers between -1.0 and 1.0:
r := float@random(-10^DIGITS..10^DIGITS)/10^DIGITS: r() $ i = 1..12;
However, it is strongly recommended to use the much more efficient functions frandom or stats::uniformRandom instead:
r := stats::uniformRandom(-1, 1, Seed = 10^10): r() $ i = 1..12
delete r:
Usually, random is used to generate experimental input or "random" examples. In these cases, reproducibility is a good thing. However, on occasion a "more random" sequence is desirable. The usual way to get a random seed in a program is to use the current system time:
SEED := round(1e10*frandom(CurrentTime)())
1035804049
random(), random()
861209862222, 269921735546
random() returns a nonnegative integer. The calls random(n1..n2) and random(n) return a procedure of type DOM_PROC.