rng
R2026bControl 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.
rng( specifies the seed for the random
number generator using the current generator algorithm.seed)
Specify
seedas a nonnegative integer, such asrng(1), to initialize the random number generator with that seed.Specify
seedas"shuffle"to initialize the generator seed based on the current time.
rng( specifies the algorithm for the
random number generator to use with a seed of 0. This syntax is equivalent to
generator)rng(0,generator). (since R2023b)
rng( restores the generator based on
previous settings contained in a structure sprev)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(__).
returns the current random number
generator settings in a structure s = rngs with fields
Type, Seed, and State.
sets the random
number generator settings using the specified arguments and returns the previous settings
in a structure s = rng(___)s. You can specify the output argument with any of the
input argument combinations in the previous syntaxes.
Examples
Input Arguments
Output Arguments
More About
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. Therngfunction uses the same seed when the command is sent to multiple workers simultaneously, such as inside aparforjob. For independent streams on the workers, use the default behavior or consider using a unique substream on each worker usingRandStream.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
rngwith the same generator algorithm and seed (or consider usingRandStreamwith the same generator algorithm, seed, and normal transformation algorithm). For more information, see Control Random Number Streams on Workers (Parallel Computing Toolbox).The
rngfunction controls random number generation for in-memory MATLAB arrays. To control random number generation for tall arrays, usetallrng. To control random number generation on the GPU, usegpurng(Parallel Computing Toolbox).To use
rnginstead of therandorrandnfunctions with the"seed","state", or"twister"inputs, see Replace Discouraged Syntaxes of rand and randn.