Академический Документы
Профессиональный Документы
Культура Документы
Syntax
rng(seed)
rng('shuffle')
rng(seed, generator)
rng('shuffle', generator)
rng('default')
scurr = rng
rng(s)
sprev = rng(...)
Description
Note: To use the rng function instead of rand or randn with the 'seed', 'state', or 'twister' inputs, see the
documentation on Replace Discouraged Syntaxes of rand and randn.
rng(seed) seeds the random number generator using the nonnegative integer seed so that
rand, randi, and randn produce a predictable sequence of numbers.
rng('shuffle') seeds the random number generator based on the current time. Thus, rand,
randi, and randn produce a different sequence of numbers after each time you call rng.
rng(seed, generator) and rng('shuffle', generator) additionally specify the type of the
random number generator used by rand, randi, and randn. The generator input is one of:
rng('default') puts the settings of the random number generator used by rand, randi, and
randn to their default values. This way, the same random numbers are produced as if you
restarted MATLAB. The default settings are the Mersenne Twister with seed 0.
scurr = rng returns the current settings of the random number generator used by rand, randi,
and randn. The settings are returned in a structure scurr with fields 'Type', 'Seed', and 'State'.
rng(s) restores the settings of the random number generator used by rand, randi, and randn
back to the values captured previously with a command such as s = rng.
sprev = rng(...) returns the previous settings of the random number generator used by
rand, randi, and randn before changing the settings.
Examples
Example 1 Retrieve and Restore Generator Settings
Save the current generator settings in s:
s = rng;
0.9058
0.1270
0.9134
0.6324
0.9058
0.1270
0.9134
0.6324
Restore the original generator settings by calling rng. Generate a new set of random values and verify that x
and y are equal:
rng(s);
y = rand(1,5)
y =
0.8147
sprev = rng(0,'v5uniform')
sprev =
Type: 'twister'
Seed: 0
State: [625x1 uint32]
x = rand
x =
0.9501
Related Examples
More About
See Also