This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Generate random floating-point 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() returns a pseudo-random floating point number from the interval .

frandom(seed) returns a generator of pseudo-random floating-point numbers from the interval .

The calls frandom() produce uniformly distributed floating-point numbers from the interval .

r := frandom(seed) produces a random number generator r. Subsequent calls r() return uniformly distributed floating-point 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 re-initialized with the reset function, random generators not using CurrentTime produce the same sequence of numbers.

frandom is the recommended function for generating uniform random floating-point numbers. It is much faster than the function random which produces uniform integer numbers.

    Note:   In contrast to random, frandom does not react to the environment variable SEED.

The function stats::uniformRandom allows to produce uniformly distributed floating-point numbers on arbitrary finite intervals. The stats library also provides random generators with various other distributions.

Environment Interactions

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.


Example 1

The following call produces a sequence of pseudo-random 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

Example 2

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:

Example 3

frandom(seed), for some integer value of seed, returns a generator of floating-point 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:

Example 4

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 CurrentTime

Return Values

frandom() returns a floating point number; frandom(seed) returns a procedure (a pseudo-random number generator).


frandom uses a linear congruence generator to directly manipulate the internal representation of a DOM_FLOAT.

See Also

MuPAD Functions

Was this topic helpful?