Main Content

rng

Control random number generator

Description

example

rng(seed) specifies the seed for the MATLAB® random number generator. For example, rng(1) initializes the Mersenne Twister generator using a seed of 1.

The rng function controls the global stream, which determines how the rand, randi, randn, and randperm functions produce a sequence of random numbers. To create one or more independent streams separate from the global stream, see RandStream and RandStream.create.

example

rng(seed,generator) also specifies the type of random number generator to use. For example, rng(0,'philox') initializes the Philox 4x32 random generator with a seed of 0.

example

s = rng returns the current random number generator settings in a structure s.

Examples

collapse all

Set the random number generator to the default seed (0) and algorithm (Mersenne Twister), then save the generator settings.

rng('default')
s = rng
s = struct with fields:
     Type: 'twister'
     Seed: 0
    State: [625x1 uint32]

Create a 1-by-5 row vector of random values between 0 and 1.

x = rand(1,5)
x = 1×5

    0.8147    0.9058    0.1270    0.9134    0.6324

Change the generator seed and algorithm, and create a new random row vector.

rng(1,'philox')
xnew = rand(1,5)
xnew = 1×5

    0.5361    0.2319    0.7753    0.2390    0.0036

Now restore the original generator settings and create a random vector. The result matches the original row vector x created with the default generator.

rng(s)
xold = rand(1,5)
xold = 1×5

    0.8147    0.9058    0.1270    0.9134    0.6324

Input Arguments

collapse all

Generator initialization, specified as one of the following options.

ValueDescription
0Initializes generator with seed 0.
positive integerInitializes generator with the specified positive integer seed, such as 1.
'default'Initializes Mersenne Twister generator with seed 0. This is the default setting at the start of each MATLAB session.
'shuffle'Initializes generator based on the current time, resulting in a different sequence of random numbers after each call to rng.
structureInitializes generator based on the settings contained in a structure with fields Type, Seed, and State.

Random number algorithm, specified as one of the options in the table. For more information on generator algorithms, see Creating and Controlling a Random Number Stream.

ValueGenerator NameGenerator Keyword
'twister'Mersenne Twistermt19937ar
'simdTwister'SIMD-oriented Fast Mersenne Twisterdsfmt19937
'combRecursive'Combined multiple recursivemrg32k3a
'multFibonacci'Multiplicative Lagged Fibonaccimlfg6331_64
'philox'Philox 4x32 generator with 10 roundsphilox4x32_10
'threefry'Threefry 4x64 generator with 20 roundsthreefry4x64_20

For legacy generators used in MATLAB versions 4.0 and 5.0, use one of these options.

ValueGenerator NameGenerator Keyword
'v4'Legacy MATLAB version 4.0 generatormcg16807
'v5uniform'Legacy MATLAB version 5.0 uniform generatorswb2712
'v5normal'Legacy MATLAB version 5.0 normal generatorshr3cong

Tips

  • When parallel processing, rng('shuffle') should not be used to set the random number stream on different workers to ensure independent streams since it seeds the random number generator based on the current time. This is especially true when the command is sent to multiple workers simultaneously, such as inside a parfor job. For independent streams on the workers, use the default behavior or consider using a unique substream on each worker using RandStream.

  • To use rng instead of the rand or randn functions with the 'seed', 'state', or 'twister' inputs, see Replace Discouraged Syntaxes of rand and randn.

Extended Capabilities

Introduced in R2011a