Generate random floatingpoint numbers
MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.
MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.
frandom()
frandom(seed
)
frandom()
returns a pseudorandom floating
point number from the interval
.
frandom(seed)
returns a generator of pseudorandom
floatingpoint numbers from the interval
.
The calls frandom()
produce uniformly distributed
floatingpoint numbers from the interval
.
r := frandom(seed)
produces a random number
generator r
. Subsequent calls r()
return
uniformly distributed floatingpoint numbers from the interval
.
Different generators created with the same integer seed generate the same sequences of numbers. See Example 3 and Example 4.
Generators created with CurrentTime
use the
time (in milliseconds) at their creation as their seed values. Generators
created shortly after one another may thus return the same numbers.
Generators created in separate calls to frandom
do
not influence one another.
As for all functions returning floating
point numbers, frandom
reacts to DIGITS
and
returns numbers with the precision set by this variable.
Each time MuPAD^{®} is started or reinitialized with the reset
function, random
generators not using CurrentTime
produce the same
sequence of numbers.
frandom
is the recommended function for
generating uniform random floatingpoint numbers. It is much faster
than the function random
which
produces uniform integer numbers.
The function stats::uniformRandom
allows
to produce uniformly distributed floatingpoint numbers on arbitrary
finite intervals. The stats library also provides random generators
with various other distributions.
frandom
and the procedures returned by frandom
are
sensitive to the environment variable DIGITS
which determines
the numerical working precision.
frandom
changes its internal state when
generating a number and will thus produce a different number on the
next call.
The following call produces a sequence of pseudorandom numbers.
Note that an index variable i
must be used in the
construction of the sequence. A call such as frandom() $8
would
produce 8 copies of the same
random value:
frandom() $ i = 1..8
frandom
reacts to DIGITS
, producing numbers
which are equally random in the later digits as in the beginning ones:
DIGITS := 200: frandom(), frandom()
delete DIGITS:
frandom(seed)
, for some integer value of seed
,
returns a generator of floatingpoint numbers. For different generators
created with the same seed, the sequences of numbers will be identical
(apart from the digits cut off when producing numbers at lower settings
of DIGITS
):
r1 := frandom(42): r2 := frandom(42): r3 := frandom(42): r1() $ i=1..4; r2() $ i=1..4; DIGITS := 20: r3() $ i=1..4;
delete r1, r2, r3, DIGITS:
Usually, frandom
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, which can be done by using CurrentTime
as
the value of seed
:
r := frandom(CurrentTime):
r(), r(), r(), r()

An initialization value for the generator: an integer or the
option 
frandom()
returns a floating
point number; frandom(seed)
returns a procedure (a pseudorandom number generator).
frandom
uses a linear congruence generator
to directly manipulate the internal representation of a DOM_FLOAT
.