rand('seed',sd) randn('seed',sd) rand('state',s) randn('state',s) rand('twister',5489)
These syntaxes referred to different types of generators, and they are no longer recommended for the following reasons:
misleading names for the generators.
All of the generators except
They unnecessarily use different generators for
To assess the impact of replacing discouraged syntaxes in your existing code, execute the following commands at the start of your MATLAB session:
rng function to control the shared
generator used by
all other random number generation functions like
and so on. To learn how to use the
when replacing discouraged syntaxes, take a few moments to understand
their function. This should help you to see which new
best suits your needs.
The first input to
the type of the generator, as described here.
are no longer recommended unless you are trying to exactly reproduce
the random numbers generated in earlier versions of MATLAB. The
simplest way to update your code is to use
rng function replaces the names for the
|rand/randn Generator Name||rng Generator Name|
The most common uses of the integer seed
rand(Generator,sd) syntax were to:
Reproduce exactly the same random numbers each time (e.g., by using a seed such as 0, 1, or 3141879)
Try to ensure that MATLAB always gives different
random numbers in separate runs (for example, by using a seed such
The following table shows replacements for syntaxes with an
The first column shows the discouraged syntax with
The second column shows how to exactly reproduce the
discouraged behavior with the new
In most cases, this is done by specifying a legacy generator type
such as the v4 or v5 generators, which is no longer recommended.
The third column shows the recommended alternative,
which does not specify the optional generator type input to
Therefore, if you always omit the
randi just use the default Mersenne Twister
generator that is used at MATLAB startup. In future releases
when new generators supersede the Mersenne Twister, this code will
use the new default.
|Discouraged rand/randn Syntax||Not Recommended: Reproduce Discouraged Behavior Exactly By Specifying Generator Type||Recommended Alternative: Does Not Override Generator Type|
The most common use of the state vector (shown here as
rand(Generator,st) syntax was to reproduce
exactly the random numbers generated at a specific point in an algorithm
or iteration. For example, you could use this vector as an aid in
rng function changes the pattern of
saving and restoring the state of the random number generator as shown
in the next table. The example in the left column assumes that you
are using the
v5 uniform generator. The example
in the right column uses the new syntax, and works for any generator
|Discouraged Syntax Using rand/randn||New Syntax Using rng|
% Save v5 generator state. st = rand('state'); % Call rand. x = rand; % Restore v5 generator state. rand('state',st); % Call rand again and hope % for the same results. y = rand
% Get generator settings. s = rng; % Call rand. x = rand; % Restore previous generator % settings. rng(s); % Call rand again and % get the same results. y = rand
For a demonstration, see this instructional video.
If there is code that you are not able or not permitted to modify
and you know that it uses the discouraged random number generator
control syntaxes, it is important to remember that when you use that
code MATLAB will switch into legacy mode.
In legacy mode,
controlled by separate generators, each with their own settings.
rand in legacy mode use one of
'v4' generator, controlled
'v5uniform' generator, controlled
'twister' generator, controlled
randn in legacy mode use one of
'v4' generator, controlled
'v5normal' generator, controlled
If code that you rely on puts MATLAB into legacy mode, use the following command to escape legacy mode and get back to the default startup generator:
Alternatively, to guard around code that puts MATLAB into legacy mode, use:
s = rng % Save current settings of the generator. ... % Call code using legacy random number generator syntaxes. rng(s) % Restore previous settings of the generator.