This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Generate a random generator for uniformly distributed elements of a data sample

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.


stats::empiricalRandom(x1, x2, …, <Seed = n>)
stats::empiricalRandom([x1, x2, …], <Seed = n>)
stats::empiricalRandom(n, <c>, <Seed = n>)
stats::empiricalRandom(n, <[c]>, <Seed = n>)


stats::empiricalRandom([x1, x2, …, xn]) returns a procedure that picks out random elements from the data x1, x2 etc.

All data x1, x2, … must be convertible to real floating-point numbers.

The procedure f := stats::empiricalRandom([x1, x2, …]) can be called in the form f(). The call f() returns one of the data elements x1, x2, ….

The values produced by f() are distributed randomly. Each element of the sample is chosen with the same probability.

Without the option Seed = n, an initial seed is chosen internally. This initial seed is set to a default value when MuPAD® is started. Thus, each time MuPAD is started or re-initialized with the reset function, random generators produce the same sequences of numbers.

    Note:   In contrast to the function random, the generators produced by stats::empiricalRandom do not react to the environment variable SEED.

For efficiency, it is recommended to produce sequences of K random elements via

f := stats::empiricalRandom([x1, x2, …]):

f() $k = 1..K;

rather than by

stats::empiricalRandom([x_1, x_2, dots])() $k = 1..K;

The latter call produces a sequence of generators each of which is called once. Also note that

stats::empiricalRandom([x_1, x_2, dots], Seed = s)() $k = 1..K;

does not produce a random sequence, because a sequence of freshly initialized generators would be created each of them producing the same number.

stats::empiricalRandom is generalized by stats::finiteRandom, which allows to specify different probabilities for the elements of the sample. The call stats::empiricalRandom([x1, …, xn]) corresponds to stats::finiteRandom([x_1, dots, x_n], [1/n, dots, 1/n]).

Further, stats::finiteRandom does not only allow numerical values x1, x2, …, but arbitrary MuPAD objects.


Example 1

We pick out random elements of some data:

f := stats::empiricalRandom(1, 7, 4, PI, Seed = 234):
f(), f(), f(), f(), f(), f(), f(), f(), f()

Alternatively, the data may be passed as a list:

f := stats::empiricalRandom([1, 7, 4, PI], Seed = 234):
f(), f(), f(), f(), f(), f(), f(), f(), f()

Symbolic data are not accepted:

stats::empiricalRandom(1, 7, 4, x):
Error: Some data cannot be converted to floating-point numbers. [stats::empiricalRandom]
delete f:

Example 2

We create a sample of type stats::sample consisting of one string column and two non-string columns:

s := stats::sample(
  [["1996", 1242, 2/5],
   ["1997", 1353, 0.1],
   ["1998", 1142, 0.2],
   ["1999", 1201, 0.2],
   ["2001", 1201, 0.1]])
"1996"  1242  2/5
"1997"  1353  0.1
"1998"  1142  0.2
"1999"  1201  0.2
"2001"  1201  0.1

We pick random values using the data in the second and third column, respectively:

f := stats::empiricalRandom(s, 2, Seed = 12345):
f(), f(), f(), f(), f(), f(), f()

f := stats::empiricalRandom(s, 3, Seed = 12345):
f(), f(), f(), f(), f(), f(), f()

delete s, f:

Example 3

We toss a fair die:

f:= stats::empiricalRandom([1, 2, 3, 4, 5, 6], Seed = 12345):
f(), f(), f(), f(), f(), f(), f(), f(), f(), f()

We toss the die 6000 times and count the frequencies of the scores 1 through 6:

t := [f() $ k = 1..6000]:
i = nops(select(t, _equal, i)) $ i = 1..6

The routine stats::finiteRandom allows to model a loaded die:

f:= stats::finiteRandom(
    [[1, 0.1], 
     [2, 0.1], 
     [3, 0.1], 
     [4, 0.1], 
     [5, 0.1], 
     [6, 0.5]], 
     Seed = 12345):
t := [f() $ k = 1..6000]:
i = nops(select(t, _equal, i)) $ i = 1..6

delete f, t:


x1, x2, …

The statistical data: real numerical values


A sample of domain type stats::sample


A column index of the sample s: a positive integer. This column provides the data x1, x2 etc. There is no need to specify a column number c if the sample has only one non-string column.



Option, specified as Seed = n

Initializes the random generator with the integer seed n. n can also be the option CurrentTime, to make the seed depend on the current time.

This option serves for generating generators that return predictable sequences of pseudo-random values. The generator is initialized with the seed n which may be an arbitrary integer. Several generators with the same initial seed produce the same sequence of values.

Return Values


Was this topic helpful?