RandStream
R2026bRandom number stream
Description
RandStream creates a random number stream
by using a specified random number generator algorithm or by copying an existing
stream.
You can generate random numbers in MATLAB® from one or more random number streams. The simplest way to generate arrays of
random numbers is to use the rand, randi, randn, and randperm functions. These functions all rely on the same stream of uniformly
distributed random numbers, known as the global stream. To change the
global stream, create a stream using RandStream and then call RandStream.setGlobalStream. However, for most use cases, you can use the
rng function, which provides a simpler interface for controlling the global
stream.
You can also use RandStream to create individual streams and generate
random numbers from them by using rand, randi,
randn, or randperm. Numbers generated from these
streams are separate from those generated from the global stream or from other streams. For
details, see Object Functions.
Creation
Use the following syntaxes to create a single random number stream. To create multiple
independent streams simultaneously, use the RandStream.create function.
Description
specifies additional options using one or more name-value arguments.s = RandStream(generator,Name=Value)
creates a copy of an existing random number stream with the same properties and state.
When you generate random numbers using the newly copied stream and the existing stream,
sampling from one stream does not affect the other. You can also copy an existing random
number stream by using the s = RandStream(existingStream)copy method. (since R2026b)
Input Arguments
Name-Value Arguments
Output Arguments
Properties
Object Functions
Other object functions of RandStream are:
RandStream.create | Create statistically independent random number streams |
RandStream.list | List available random number generator algorithms |
RandStream.getGlobalStream | Get current global random number stream |
RandStream.setGlobalStream | Set global random number stream |
reset | Reset random number stream |
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);The functions in this table 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) X = rand(s,__,like=p) rand, randi, and randn. |
randi | Uniformly distributed random integers | |
randn | Normally distributed random numbers | |
randperm | Random permutation of integers | Supported syntaxes, where p = randperm(s,n) p = randperm(s,n,k) randperm. |
Examples
More About
Tips
RandStreamcreates a random number stream that you can use to generate random, in-memory MATLAB arrays. To create a random number stream that you can use to generate GPU arrays, useparallel.gpu.RandStream(Parallel Computing Toolbox).