RandStream.setGlobalStream

Makes a given stream the global random stream.

Category: Mathematics > Random Number Generation

Syntax

RandStream.setGlobalStream(s)

Makes s the stream used by rand, randn, randi and randperm.

old = RandStream.setGlobalStream(s)

Also returns the stream that was global before.

Description

After the call, plain rand(1,2) continues from the state of s. This is useful to run a section of code with its own reproducible sequence and then put the old stream back by passing old to RandStream.setGlobalStream.

Input arguments

s
A RandStream object.

Output arguments

old
The previous global stream, so it can be restored later.

Example

Swap in a stream and restore

s = RandStream('mt19937ar', 'Seed', 4);
old = RandStream.setGlobalStream(s);
x = rand(1, 2);
RandStream.setGlobalStream(old);
class(old)
isequal(x, rand(RandStream('mt19937ar', 'Seed', 4), 1, 2))
Output
ans = 'RandStream'
ans = logical
   1

See also

RandStream.getGlobalStream RandStream rng