Main Content

Managing the Global Stream Using RandStream

The rand, randn, randi, and randperm functions draw random numbers from an underlying random number stream, called the global stream. The global stream is a RandStream object. A simple way to control the global stream is to use the rng function. For more comprehensive control, the RandStream class enables you to create a separate stream from the global stream, get a handle to the global stream, and control random number generation.

Use rng to initialize the random number generator. Set the generator seed to 0 and the generator algorithm to Mersenne Twister. Save the generator settings.

rng(0,'twister')
s = rng
s = struct with fields:
     Type: 'twister'
     Seed: 0
    State: [625x1 uint32]

Create a 1-by-6 row vector of uniformly distributed random values between 0 and 1.

x = rand(1,6)
x = 1×6

    0.8147    0.9058    0.1270    0.9134    0.6324    0.0975

Use RandStream.getGlobalStream to return a handle to the global stream, that is, the current global stream that rand generates random numbers from. If you use RandStream.getGlobalStream to get a handle to the global stream, you can see the changes you made to the global stream using rng.

globalStream = RandStream.getGlobalStream
globalStream = 
mt19937ar random stream (current global stream)
             Seed: 0
  NormalTransform: Ziggurat

Change the generator seed and algorithm, and create a new random row vector. Show the current global stream that rand generates random numbers from.

rng(1,'philox')
xnew = rand(1,6)
xnew = 1×6

    0.5361    0.2319    0.7753    0.2390    0.0036    0.5262

globalStream = RandStream.getGlobalStream
globalStream = 
philox4x32_10 random stream (current global stream)
             Seed: 1
  NormalTransform: Inversion

Next, restore the original generator settings and create a random vector. The result matches the original row vector x created with the initial generator.

rng(s)
xold = rand(1,6)
xold = 1×6

    0.8147    0.9058    0.1270    0.9134    0.6324    0.0975

By default, random number generation functions, such as rand, use the global random number stream. To specify a different stream, create another RandStream object. Pass it as the first input argument to rand. For example, create a 1-by-6 vector of random numbers using the SIMD-oriented Fast Mersenne Twister.

myStream = RandStream('dsfmt19937')
myStream = 
dsfmt19937 random stream
             Seed: 0
  NormalTransform: Ziggurat

r = rand(myStream,1,6)
r = 1×6

    0.0306    0.2131    0.2990    0.3811    0.8635    0.1334

When you call the rand function with myStream as the first input argument, it draws numbers from myStream and does not affect the results of the global stream.

If you want to set myStream as a global stream, you can use the RandStream.setGlobalStream object function.

RandStream.setGlobalStream(myStream)
globalStream = RandStream.getGlobalStream
globalStream = 
dsfmt19937 random stream (current global stream)
             Seed: 0
  NormalTransform: Ziggurat

In many cases, the rng function is all you need to control the global stream, but the RandStream class allows control over some advanced features, such as the choice of algorithm used for normal random values.

For example, create a RandStream object and specify the transformation algorithm to generate normally distributed pseudorandom values when using randn. Generate normally distributed pseudorandom values using the Polar transformation algorithm, instead of the default Ziggurat transformation algorithm.

myStream = RandStream('mt19937ar','NormalTransform','Polar')
myStream = 
mt19937ar random stream
             Seed: 0
  NormalTransform: Polar

Set myStream as the global stream. Create 6 random numbers with normal distribution from the global stream.

RandStream.setGlobalStream(myStream)
randn(1,6)
ans = 1×6

    0.2543   -0.7733   -1.7416    0.3686    0.5965   -0.0191

See Also

|

Related Topics