Generate random integer numbers

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.


random(n1 .. n2)


random() returns a random integer number between 0 and 1012.

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 1012).

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.

Environment Interactions

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.


Example 1

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:

Example 2

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

Example 3

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:

Example 4

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:

Example 5

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)())
random(), random()
861209862222, 269921735546


n1, n2

Integers with n1 < n2


A positive integer

Return Values

random() returns a nonnegative integer. The calls random(n1..n2) and random(n) return a procedure of type DOM_PROC.


random implements a linear congruence generator. The sequence of pseudo-random numbers generated by calling random() over and over again is f(x), f(f(x)), …., where x is the initial value of SEED and f is the function mapping x to ax mod m with suitable integer constants a and m.

See Also

MuPAD Functions

Was this topic helpful?