tallrng

Control random number generation for tall arrays

Description

tallrng('default') puts the settings of the random number generator used in tall array calculations to their default values. The random numbers produced are the same as if you restart MATLAB®.

tallrng('shuffle') sets the seed of the random number generator based on the current time. Use this syntax when you want different sequences of random numbers each time they are generated.

tallrng(seed) sets the starting point, or seed, of the random number generator. Use this syntax when you want to produce predictable sequences of numbers.

tallrng(seed,generator) or tallrng('shuffle',generator) selects the random number generator. For example, tallrng(0,'mlfg6331_64').

example

state = tallrng returns the current state of the random number generator as a structure. Use the structure to restore the random number generator to the captured state at a later time with tallrng(state).

example

tallrng(state) restores the state of the random number generator using settings previously captured with state = tallrng.

Examples

collapse all

Capture the generator settings, generate an array of random numbers, and then restore the generator to the initial settings to create predictable arrays of random numbers.

Save the current state of the random number generator.

state = tallrng
state = struct with fields:
Type: 'threefry'
Seed: 0
StreamIndex: 1
Substream: 1

Create a tall array of random numbers using arrayfun.

t = tall(zeros(10,1));
n1 = arrayfun(@(x) x+randn, t)
n1 =

10x1 tall array

?
?
?
?
?
?
?
?
:
:

Restore the generator to the previous state and regenerate the array of random numbers. The two arrays n1 and n2 are equal.

tallrng(state)
n2 = arrayfun(@(x) x+randn, t)
n2 =

10x1 tall array

?
?
?
?
?
?
?
?
:
:

Input Arguments

collapse all

Random number seed, specified as a nonnegative integer. The seed specifies the starting point for the algorithm to generate random numbers. Specify the seed when you want reproducible results.

The maximum value of the seed is 232-1.

Example: tallrng(3)

Random number generator, specified as one of the random number generators in this table. You can specify the regular or convenience name.

NameConvenience NameDescription
'threefry4x64_20' (default)'threefry'Threefry 4x64 generator with 20 rounds
'mrg32k3a' 'combRecursive'

Combined multiple recursive generator

'mlfg6331_64''multFibonacci'

Multiplicative lagged Fibonacci generator

'philox4x32_10''philox'Philox 4x32 generator with 10 rounds

tallrng supports random number generators that have multiple stream and substream support. See Creating and Controlling a Random Number Stream for a complete list of available generators in MATLAB.

Example: tallrng(0,'mlfg6331_64')

Previous random number generator state, specified as a structure previously created using state = tallrng.

Example: state = tallrng captures the current state of the random number generator, and tallrng(state) restores the generator to those settings.

Data Types: struct

Output Arguments

collapse all

Random number generator state, returned as a structure with these fields.

FieldDescriptionExample Value
TypeType of random number generator'threefry'
SeedNumber indicating the starting point for the algorithm (for reproducibility)0
StreamIndexIndex indicating the associated global stream 1
SubstreamIndex indicating the associated substream1

Example: state = tallrng captures the current state of the random number generator, and tallrng(state) restores the generator to those settings.

Data Types: struct

Tips

• If you have Statistics and Machine Learning Toolbox™, then tallrng controls the random numbers that functions such as datasample, cvpartition, and TreeBagger generate during tall array calculations.

Compatibility Considerations

expand all

Behavior changed in R2019a