RandStream

Object that holds an independent random number stream.

Category: Mathematics > Random Number Generation

Syntax

s = RandStream(type)

Creates a stream with the generator named by type.

s = RandStream(type, 'Seed', seed)

Creates a stream initialized with a given seed.

rand(s, ___), randn(s, ___), randi(s, ___), randperm(s, ___)

Draw from s instead of the global stream.

Description

A stream is a generator with its own state. Streams let different parts of a program draw random numbers without disturbing each other: values taken from one stream do not change what another produces, and two streams built with the same seed yield the same numbers.

The global stream used by plain rand, randn, randi and randperm is itself a RandStream, available through RandStream.getGlobalStream.

Input arguments

type
Generator name. MatLivre supports 'mt19937ar' (also 'twister').
'Seed'
Nonnegative integer, or 'shuffle'/'default' for an arbitrary or the standard seed.

Output arguments

s
A RandStream handle with properties such as Type and Seed.

Example

A private stream

s = RandStream('mt19937ar', 'Seed', 1);
rand(s, 1, 3)
s.Type
s2 = RandStream('mt19937ar', 'Seed', 1);
isequal(rand(s2, 1, 3), rand(RandStream('mt19937ar', 'Seed', 1), 1, 3))
Output
ans = 1×3

    0.4170    0.7203    0.0001
ans = 'mt19937ar'
ans = logical
   1

Notes

Only the Mersenne Twister generator is available. Other types, such as 'threefry' or 'mrg32k3a', give an error. The State property is not exposed.

See also

RandStream.create RandStream.getGlobalStream rng reset (RandStream) rand