Main Content

rng

R2026b

Control random number generator

Description

rng("default") initializes the MATLAB® random number generator using the default algorithm and seed. The factory default is the Mersenne Twister generator with seed 0. For information about changing the default settings and reproducibility, see Default Settings for Random Number Generator and Reproducibility for Random Number Generator.

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) specifies the seed for the random number generator using the current generator algorithm.

  • Specify seed as a nonnegative integer, such as rng(1), to initialize the random number generator with that seed.

  • Specify seed as "shuffle" to initialize the generator seed based on the current time.

rng(seed,generator) also specifies the algorithm for the random number generator to use. For example, rng(2,"philox") initializes the Philox 4x32 generator with a seed of 2.

example

rng(generator) specifies the algorithm for the random number generator to use with a seed of 0. This syntax is equivalent to rng(0,generator). (since R2023b)

rng(sprev) restores the generator based on previous settings contained in a structure sprev with fields Type, Seed, and State. The structure sprev must be a structure that is returned by a previous call to s = rng or s = rng(__).

example

s = rng returns the current random number generator settings in a structure s with fields Type, Seed, and State.

example

s = rng(___) sets the random number generator settings using the specified arguments and returns the previous settings in a structure s. You can specify the output argument with any of the input argument combinations in the previous syntaxes.

Examples

collapse all

Initialize the random number generator using the default generator algorithm and seed.

rng("default")

Show the default random number generator settings. In this case, the random number generator uses the Mersenne Twister algorithm with seed 0.

s = rng
s = struct with fields:
     Type: 'twister'
     Seed: 0
    State: [625×1 uint32]

Create a 4-by-4 matrix of uniformly distributed random numbers between 0 and 1.

R = rand(4)
R = 4×4

    0.8147    0.6324    0.9575    0.9572
    0.9058    0.0975    0.9649    0.4854
    0.1270    0.2785    0.1576    0.8003
    0.9134    0.5469    0.9706    0.1419

Starting in R2023b, you can set the default algorithm and seed from the MATLAB Settings Window. If you do not change the MATLAB settings, then rng uses the factory value of "twister" for the Mersenne Twister algorithm with seed 0, as in previous releases.

Specify the random number generator settings to make the results in this example repeatable. Set the generator seed to 2 and the algorithm to Mersenne Twister, and then save the generator settings.

rng(2,"twister")
s = rng
s = struct with fields:
     Type: 'twister'
     Seed: 2
    State: [625×1 uint32]

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

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

    0.4360    0.0259    0.5497    0.4353    0.4204

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

rng(1,"philox")
rnew = rand(1,5)
rnew = 1×5

    0.5361    0.2319    0.7753    0.2390    0.0036

Now restore the previous generator settings and create a random vector. The result matches the first row vector r that you created with the previous generator.

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

    0.4360    0.0259    0.5497    0.4353    0.4204

Input Arguments

collapse all

Random number generator seed, specified as a nonnegative integer less than 2^32 or "shuffle". When you specify seed as "shuffle", the rng function initializes the generator seed based on the current time, resulting in a different sequence of random numbers after each call to rng.

Random number generator algorithm, specified as one of the generator names in the table. For more information on the generator algorithms, see Create and Control Random Number Streams.

NameDescription
"twister" (default)Mersenne Twister
"combRecursive"Combined multiple recursive generator
"multFibonacci"Multiplicative lagged Fibonacci generator

"pcg" (since R2026b)

64-bit permuted congruential generator with double xor-shift multiply
"philox"Philox 4x32 generator with 10 rounds
"simdTwister"SIMD-oriented fast Mersenne Twister
"threefry"Threefry 4x64 generator with 20 rounds

"xoshiro" (since R2026b)

Xor-shift-rotate generator with 256-bit state and double addition

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

NameDescription
"v4"Legacy MATLAB version 4.0 generator
"v5normal"Legacy MATLAB version 5.0 normal generator
"v5uniform"Legacy MATLAB version 5.0 uniform generator

Previous random number generator settings, specified as a structure with fields Type, Seed, and State. The structure sprev must be a structure that is returned by a previous call to s = rng or s = rng(__).

Output Arguments

collapse all

Random number generator settings, returned as a structure with fields Type, Seed, and State.

More About

collapse all

Tips

  • When you perform parallel processing with Parallel Computing Toolbox, do not use rng("shuffle") to set the random number stream on different workers for independent streams because it seeds the random number generator based on the current time. The rng function uses the same seed 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.

  • When you perform parallel processing, the default random number generators on the MATLAB client and MATLAB workers are different. By default, the MATLAB client uses the Mersenne Twister generator with seed 0 and the MATLAB workers use the Threefry 4x64 generator with 20 rounds with seed 0. Changing the default generator settings in the MATLAB settings affects only the default behavior of the client and does not affect the default behavior of the parallel workers. If you need to generate the same random stream of numbers on the client and workers, you can use rng with the same generator algorithm and seed (or consider using RandStream with the same generator algorithm, seed, and normal transformation algorithm). For more information, see Control Random Number Streams on Workers (Parallel Computing Toolbox).

  • The rng function controls random number generation for in-memory MATLAB arrays. To control random number generation for tall arrays, use tallrng. To control random number generation on the GPU, use gpurng (Parallel Computing Toolbox).

  • 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

expand all

Version History

Introduced in R2011a

expand all