rng - Control random number generator - MATLAB
Control random number generator
Syntax
Description
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(
also specifies the algorithm for the random number generator to use. For example,
seed,generator)rng(2,"philox") initializes the Philox 4x32 generator with a seed of
2.
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
collapse all
Initialize the random number generator using the default generator algorithm and seed.
Show the default random number generator settings. In this case, the random number generator is using the Mersenne Twister algorithm with seed 0.
s = struct with fields:
Type: 'twister'
Seed: 0
State: [625×1 uint32]
Create a 4-by-4 matrix of uniformly distributed random numbers between 0 and 1.
r = 4×4
0.8147 0.6324 0.9575 0.9572
0.9058 0.0975 0.9649 0.4854
0.1270 0.2785 0.1576 0.8003
0.9134 0.5469 0.9706 0.1419
Starting in R2023b, you can set the default algorithm and seed from the MATLAB Settings Window window. If you do not change the MATLAB settings, then rng uses the factory value of "twister" for the Mersenne Twister generator with seed 0, as in previous releases.
Specify the random number generator settings to make the results in this example repeatable. Set the generator seed to 2 and the algorithm to Mersenne Twister, and then save the generator settings.
s = struct with fields:
Type: 'twister'
Seed: 2
State: [625×1 uint32]
Create a 1-by-5 row vector of random values between 0 and 1.
x = 1×5
0.4360 0.0259 0.5497 0.4353 0.4204
Change the generator seed and algorithm, and create a new random row vector.
rng(1,"philox")
xnew = rand(1,5)xnew = 1×5
0.5361 0.2319 0.7753 0.2390 0.0036
Now restore the original generator settings and create a random vector. The result matches the original row vector x created with the original generator.
xold = 1×5
0.4360 0.0259 0.5497 0.4353 0.4204
Input Arguments
collapse all
Random number seed, specified as a nonnegative integer less than
2^32 or "shuffle". When you specify
seed as "shuffle", the rng
function initializes the generator seed based on the current time, resulting in a
different sequence of random numbers after each call to rng.
Random number algorithm, specified as one of the options in the table. For more information on generator algorithms, see Creating and Controlling a Random Number Stream.
| Value | Generator Name | Generator Keyword |
|---|---|---|
"twister" | Mersenne Twister | mt19937ar |
"simdTwister" | SIMD-Oriented Fast Mersenne Twister | dsfmt19937 |
"combRecursive" | Combined Multiple Recursive | mrg32k3a |
"multFibonacci" | Multiplicative Lagged Fibonacci | mlfg6331_64 |
"philox" | Philox 4x32 generator with 10 rounds | philox4x32_10 |
"threefry" | Threefry 4x64 generator with 20 rounds | threefry4x64_20 |
For legacy generators used in MATLAB versions 4.0 and 5.0, use one of these options.
| Value | Generator Name | Generator Keyword |
|---|---|---|
"v4" | Legacy MATLAB version 4.0 generator | mcg16807 |
"v5uniform" | Legacy MATLAB version 5.0 uniform generator | swb2712 |
"v5normal" | Legacy MATLAB version 5.0 normal generator | shr3cong |
Random number generator settings, specified as a structure with fields
Type, Seed, and State.
More About
collapse all
You can change the default algorithm and seed for the random number generator from the MATLAB Settings window. On the Home tab, in the Environment section, click
Settings. Select > , and then select a different option for Default
algorithm and select a different value for Default
seed in the Random Number Generation
setting. (since R2023b)When you first start a MATLAB session or call
rng("default"), MATLAB initializes the random number generator using the default algorithm and seed that you have set in the MATLAB settings. If you do not change the Random Number Generation setting, thenrnguses the factory value of"twister"for the Mersenne Twister generator with seed 0, as in previous releases.If you use parallel workers (requires Parallel Computing Toolbox™),
rng("default")initializes the Threefry 4x64 generator with 20 rounds and a seed value of 0. Changing the default generator settings in the MATLAB Settings window does not affect the default behavior of the parallel workers. (since R2023a)
Use rng("default") at the start of your program if you want results
to be repeatable within a MATLAB session. rng("default") uses the default algorithm and seed
that are specified in your MATLAB settings. However, this command does not guarantee the same results between
different MATLAB sessions with different settings.
Instead, use rng(seed,generator) or rng(generator)
at the start of your program if you want results to remain the same in future MATLAB releases or when the default algorithm and seed have been changed in your
MATLAB settings. For example, use rng("twister") to use the
Mersenne Twister generator with seed 0.
The rng function is a pseudorandom number generator, which creates a
deterministic sequence of numbers that appear random. These numbers are predictable if the
seed and the deterministic algorithm of the generator are known. While not truly random, the
generated numbers pass various statistical tests of randomness, satisfying the independent
and identically distributed (i.i.d.) condition, and justifying the name pseudorandom.
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. 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).To use
rnginstead of therandorrandnfunctions with the"seed","state", or"twister"inputs, see Replace Discouraged Syntaxes of rand and randn.
Extended Capabilities
expand all
Usage notes and limitations:
Only the
"twister","v5normal", and"v4"generators are supported.The generated code for
rng("shuffle")might produce different seeds than MATLAB produces.For a MEX target:
If extrinsic calls are disabled or
rngis called inside aparfor-loop, the output ofrngin the MEX function is not compatible with therngfunction in MATLAB. You cannot pass the output ofs = rngfrom the MEX function torngin MATLAB.If extrinsic calls are enabled and
rngis not called from inside aparfor-loop, onlyrngcan access the data in the structure thatrngreturns.If extrinsic calls are enabled and
rngis not called from inside aparfor-loop, generated MEX files use the same random number state as MATLAB in serial code. Otherwise, the generated MEX code and standalone code maintain their own random number state that is initialized to the same state as MATLAB.
Version History
Introduced in R2011a
expand all
Use the new syntax rng(generator) to specify the algorithm for the
random number generator to use. This syntax allows you to set the random number algorithm
without specifying the seed, where rng uses a seed of 0. This syntax is
equivalent to rng(0,generator). For example,
rng("philox") initializes the Philox 4x32 generator with a seed of
0.
You can change the default algorithm and seed for the random number generator from the
MATLAB
Settings Window
window. On the Home tab, in the Environment
section, click
Settings. Select > , and then select a different option for Default
algorithm and select a different value for Default
seed in the Random Number Generation
setting.
When you first start a MATLAB session or call rng("default"), MATLAB initializes the random number generator using the default algorithm and seed
that you have set in the MATLAB settings. If you do not change the Random Number
Generation setting, then rng uses the factory value of
"twister" for the Mersenne Twister generator with seed 0, as in
previous releases.
To access and modify
settings for the random number generator programmatically, you can access the
matlab.general.randomnumbers settings using the root
SettingsGroup object returned by the settings
function. For example, show the default algorithm and seed that you have set for the random
number generator.
s = settings; s.matlab.general.randomnumbers.DefaultAlgorithm s.matlab.general.randomnumbers.DefaultSeed
When you perform parallel processing (requires Parallel Computing Toolbox), by default, the MATLAB client uses the Mersenne Twister random number 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 window or using the matlab.general.randomnumbers
settings affects only the default behavior of the client and does not affect the default
behavior of the parallel workers.
When you use the syntax rng("default") on MATLAB parallel workers (requires Parallel Computing Toolbox), MATLAB resets the random number generator settings to the worker default values. The
default corresponds to the Threefry 4x64 generator with 20 rounds and a seed value of 0.
In previous releases, when you use rng("default") on parallel
workers, MATLAB changes the worker random number generator settings to the client default
values. The default corresponds to the Mersenne Twister generator with a seed value of
0.