RandStream constructor

Random number stream

Syntax

s = RandStream('gentype')
s = RandStream('gentype',Name,Value)

Description

s = RandStream('gentype') creates a random number stream that uses the uniform pseudorandom number generator algorithm specified by gentype. RandStream.list returns all possible values for gentype, or see Choosing a Random Number Generator for details on generator algorithms.

s = RandStream('gentype',Name,Value) allows you to specify one or more optional Name,Value pairs to control creation of the stream.

Once you have created a random, you can use RandStream.setGlobalStream to make it the global stream, so that the functions rand, randi, and randn draw values from it.

Parameters for RandStream are:

ParameterDescription
SeedNonnegative scalar integer with which to initialize all streams. Seeds must be an integer between 0 and 232 − 1 or 'shuffle' to create a seed based on the current time. Default is 0.
NormalTransformTransformation algorithm used by randn(s, ...) to generate normal pseudorandom values. Possible values are 'Ziggurat', 'Polar', or 'Inversion'.

Examples

Example 1

Create a random number stream, make it the global stream, and save and restore its state to reproduce the output of randn:

s = RandStream('mrg32k3a');
RandStream.setGlobalStream(s);
savedState = s.State;
z1 = randn(1,5)
z1 =
   -0.1894   -1.4426   -0.3592    0.8883   -0.4337
s.State = savedState;
z2 = randn(1,5)
z2 =
   -0.1894   -1.4426   -0.3592    0.8883   -0.4337

z2 contains exactly the same values as z1.

Example 2

Return rand, randi, and randn to their default startup settings:

s = RandStream('mt19937ar','Seed',0)
RandStream.setGlobalStream(s);

Example 3

Replace the current global random number stream with a stream whose seed is based on the current time, so rand, randi, and randn will return different values in different MATLAB® sessions. It is usually not desirable to do this more than once per MATLAB session as it may affect the statistical properties of the random numbers MATLAB produces:

s = RandStream('mt19937ar','Seed','shuffle');
RandStream.setGlobalStream(s);

More About

expand all

Tips

  • Streams created using RandStream might not be independent from each other. Use RandStream.create to create multiple streams that are independent.

Was this topic helpful?