Main Content

Random number stream

`RandStream`

creates a random number stream
using a specified pseudorandom number generator algorithm.

You can generate pseudorandom numbers in MATLAB^{®} from one or more random number streams. The simplest way to generate arrays of
random numbers is to use `rand`

, `randi`

, `randn`

, and `randperm`

functions. These functions all rely on the same stream of uniformly
distributed random numbers, known as the *global stream*. Changing the
global stream can involve `RandStream`

, but it does not have to. If you
create a stream using `RandStream`

, you can use `RandStream.setGlobalStream`

to make it the global stream. However, the `rng`

function provides a simpler interface to create a global stream that is
sufficient for most use cases.

You can also use `RandStream`

to create streams and
`rand`

, `randi`

, `randn`

, or
`randperm`

to generate random numbers separately from those drawn from the
global stream of from other streams. For details, see Object Functions.

Use the following syntaxes to create a single random number stream. If you want to create
multiple independent streams simultaneously, use the `RandStream.create`

function.

creates a random number stream that uses the uniform pseudorandom number generator
algorithm specified by `s`

= RandStream(`gentype`

)`gentype`

.

also controls properties of the stream using one or more optional
`s`

= RandStream(`gentype`

,`Name,Value`

)`Name,Value`

pair arguments.

By default, random number generation functions, such as `rand`

, use the global random number stream. To specify a different stream, create
a `RandStream`

object and pass it as the first input argument. For instance,
create a 4-by-1 vector of random numbers using the SIMD-oriented fast Mersenne
twister.

s = RandStream('dsfmt19937'); r = rand(s,4,1);

These functions accept a `RandStream`

object:

`rand` | Uniformly distributed random numbers | Supported syntaxes, where X = rand(s) X = rand(s,n) X = rand(s,sz1,...,szN) X = rand(s,sz) X = rand(s,typename) `rand` , `randi` , and `randn` . |

`randi` | Uniformly distributed pseudorandom integers | |

`randn` | Normally distributed random numbers | |

`randperm` | Random permutation of integers | Supported syntaxes, where p = randperm(s,n) p = randperm(s,n,k) `randperm` . |

Other object functions of `RandStream`

are:

`RandStream.create` | Create statistically independent random number streams |

`RandStream.list` | List random number generator algorithms |

`RandStream.getGlobalStream` | Get current global random number stream |

`RandStream.setGlobalStream` | Set global random number stream |

`reset` | Reset random number stream |