Main Content

RandStream.setGlobalStream

R2026b

Set global random number stream

Description

prevstream = RandStream.setGlobalStream(stream) sets the specified random number stream as the global stream that the rand, randi, and randn functions draw values from. The function returns the previous global random number stream.

rand, randi, randn, and randperm all rely on the same stream of uniform random numbers, known as the global stream.

  • To generate uniform random numbers strictly between 0 and 1, rand draws one value from the global stream for each output value.

  • To generate random integers, randi draws one value from the global stream for each output value and applies a simple transformation.

  • To generate normally distributed random numbers, randn draws one or more values from the global stream for each output value and applies normal transformation algorithm specified in the NormalTransform property of stream.

  • To generate a random permutation of integers, randperm draws values from the global stream and applies a permutation algorithm. The number of values drawn depends on the syntax used to call randperm.

You can also specify an individual random number stream from which rand, randi, randn, and randperm draw values by creating a RandStream object and passing it as the first input argument.

Note

The rng function can be a simpler alternative for controlling the global stream.

Input Arguments

collapse all

Random number stream, specified as a RandStream object.

Output Arguments

collapse all

Previous global stream, returned as a RandStream object.

Extended Capabilities

expand all

Version History

Introduced in R2011a