# Documentation

### This is machine translation

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

# tallrng

Control random number generation for tall arrays

## Syntax

tallrng('default')
tallrng('shuffle')
tallrng(seed)
tallrng(seed,generator)
tallrng('shuffle',generator)
state = tallrng
tallrng(state)

## 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: 'mrg32k3a'
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 double column vector

-0.1894
-1.4426
-0.3592
0.8883
-0.4337
0.4167
0.9432
0.8624
:
:

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 double column vector

-0.1894
-1.4426
-0.3592
0.8883
-0.4337
0.4167
0.9432
0.8624
:
:

## 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 a character vector or string for any valid random number generator that supports streams and substreams. See Choosing a Random Number Generator for a list of available generators.

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'mrg32k3a'
SeedNumber indicating the starting point for the algorithm (for reproducibility)0
StreamIndexIndex indicating the associated global stream 1
SubstreamIndex indicating the assocated 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.