rng
Control random number generator
Description
rng(
specifies the seed for the
MATLAB® random number generator. For example, seed
)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
.
s = rng
returns the current random number generator settings in a
structure s
.
Examples
Input Arguments
Tips
When you perform parallel processing, do not use
rng('shuffle')
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 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. If you need to generate the same random stream of numbers on the client and workers, you can use
rng
with the same generator type and seed (or consider usingRandStream
with the same generator type, 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.