| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
Prior to MATLAB Version 7.7, the rand and randn functions used a different syntax for controlling the random number generators. In addition, rand and randn had separate underlying random number streams. For example, in MATLAB Version 7.5, the syntax to reset the rand function to its default setting was:
rand('twister',5489)To reset the randn function to its default setting, the syntax was:
randn('state',0)All syntaxes from previous versions of MATLAB have been retained in the current version. Any use of the syntax rand('keyword', seedvalue), where seedvalue is an integer or a state vector, will activate legacy mode.
rand('twister',5489)
RandStream.getDefaultStream
ans =
legacy random stream (current default)
RAND algorithm: V7.4 (Mersenne Twister)
RANDN algorithm: V5 (Ziggurat)In legacy mode, rand and randn use separate generators. The generator used for each function is determined by the keyword argument of the function. For example, the twister keyword will activate the Mersenne Twister generator for rand and the ziggurat algorithm for randn. When in legacy mode, you can use the old control syntax to activate other legacy generators as well. The old control syntaxes for rand and randn and their equivalents with the RandStream constructor are given in the tables below. (The randi function is new in MATLAB Version 7.7 and does not accept legacy mode keyword arguments. It can be used while in legacy mode; in which case it uses the same generator as rand.)
When in legacy mode, you may still create random stream objects and generate values from those streams using @RandStream methods. However, the default stream used by the functions rand, randn, and randi has a special setting, known as the legacy stream. This stream is similar to other random streams, except that it acts as an interface to the legacy generators from previous versions of MATLAB. All of the generator and transformation algorithms that were available in MATLAB Version 7.6 and earlier are available in the current version.
The legacy mode keywords syntaxes for the rand function and their RandStream equivalents are:
| Legacy mode | Generator algorithm | RandStream syntax |
|---|---|---|
| rand('twister',5489) | Mersenne Twister (the default in MATLAB versions 7.4 and later). | RandStream('mt19937ar','Seed', 5489) |
| rand('state'0) | Modified subtract with borrow algorithm (the default in MATLAB versions 5 through 7.3). | RandStream('swb2712','Seed',0) |
| rand('seed',0) | Multiplicative congruential algorithm (the default in MATLAB version 4 and before). | RandStream('mcg16807','Seed', 0) |
The legacy mode keywords syntaxes for the randn function and their RandStream equivalents are:
| Legacy mode | Transformation algorithm | RandStream syntax |
|---|---|---|
| randn('seed',0) | Marsaglia's ziggurat algorithm (the default in MATLAB versions 5 and later). | RandStream('mcg16807', 'Seed',0) |
| randn('state',0) | Polar algorithm (the default in MATLAB version 4 and before). | RandStream('shr3cong') |
To get out of legacy mode, create a new random stream with the constructor and make it the default stream.
rand('twister',5489)
% Go into legacy mode
RandStream.getDefaultStream
ans =
legacy random stream (current default)
RAND algorithm: V7.4 (Mersenne Twister)
RANDN algorithm: V5 (Ziggurat)
s=RandStream('mt19937ar')
RandStream.setDefaultStream(s)
s
s =
mt19937ar random stream (current default)
Seed: 0
RandnAlg: Ziggurat
![]() | Multiple streams | Selected Bibliography | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |