random -- generate random
numbers
Introductionrandom() returns a random integer
number.
random(n1..n2) returns a procedure that
generates random integers between n1 and
n2.
Call(s)random()
random(n1..n2)
random(n)
Parametersn1, n2 |
- | integers with n1 <=
n2 |
n |
- | a positive integer |
Returnsrandom() returns a nonnegative integer.
The calls random(n1..n2) and
random(n) return a procedure of type DOM_PROC.
Side
Effectsrandom as well as the random number generators created
by it are sensitive to the environment variable SEED.
Related
Functions
Detailsrandom() returns a uniformly distributed
nonnegative random integer with 12 digits.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).SEED is used for initializing or
changing the sequence of random numbers. It may be assigned any
nonzero 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.
random may run
simultaneously. All generators make use of the same global variable
SEED.
Example
1The 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
427419669081, 321110693270, 343633073697, 474256143563,
558458718976, 746753830538, 32062222085, 722974121768
The following call produces a ``die'' that is rolled 20 times:
>> die := random(1..6): die() $ i = 1..20
2, 2, 2, 4, 4, 3, 3, 2, 1, 4, 4, 6, 1, 1, 1, 2, 4, 2, 1, 3
The following call produces a ``coin'' that produces ``head'' or ``tail'':
>> coin := random(2): coin() $ i = 1..10
1, 0, 1, 1, 0, 1, 0, 1, 0, 0
>> subs(%, [0 = head, 1 = tail])
tail, head, tail, tail, head, tail, head, tail, head, head
The following call produces a generator of uniformly
distributed floating point numbers between -1.0 and
1.0:
>> r := random(-10^DIGITS..10^DIGITS)/float(10^DIGITS):
>> r() $ i = 1..12;
0.1905754559, 0.2075358358, 0.5537108789, 0.1638155425,
0.2610874287, -0.7132768677, -0.7457691643, 0.9053675583,
-0.4759211428, 0.1898567228, 0.6881793744, -0.9192271682
>> delete dice, coin, r:
Example
2random 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
427419669081, 321110693270, 343633073697, 474256143563
>> SEED := 1: random() $ i = 1..4
427419669081, 321110693270, 343633073697, 474256143563
Example
3random allows 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
[1, 4], [0, 2], [1, 3], [0, 5], [2, 2], [4, 7]
>> delete r1, r2:
Backgroundrandom 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 x -> a*x mod
m with suitable integer constants a and
m.