Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

# stats::empiricalRandom

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

### Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

## Syntax

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

## Description

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.

## Examples

### 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:`

## Parameters

 x1, x2, … The statistical data: real numerical values s A sample of domain type stats::sample c 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.

## Options

 Seed 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.

## See Also

### MuPAD Functions

Was this topic helpful?