Control random number generator
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
.
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.
rand
| randi
| randn
| randperm
| RandStream
| RandStream.create