rng
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
.
rng(
specifies the seed for the random
number generator using the current generator algorithm.seed
)
Specify
seed
as a nonnegative integer, such asrng(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(
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(
initializes the generator based on
the settings contained in a structure s
)s
with fields
Type
, Seed
, and State
. The
structure s
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 t
= rngt
with fields
Type
, Seed
, and State
.
returns the current
random number generator settings in a structure t
= rng(___)t
before changing the
settings using the specified arguments. You can specify the output argument with any of
the input argument combinations in the previous syntaxes.
Examples
Input Arguments
More About
Tips
When you perform parallel processing, 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. Therng
function uses the same seed when the command is sent to multiple workers simultaneously, such as inside aparfor
job. 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 preferences 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 usingRandStream
with the same generator algorithm, seed, and normal transformation algorithm). For more information, see Control Random Number Streams on Workers (Parallel Computing Toolbox).To use
rng
instead of therand
orrandn
functions with the"seed"
,"state"
, or"twister"
inputs, see Replace Discouraged Syntaxes of rand and randn.