Documentation |
Random number stream
s = RandStream('gentype')
s = RandStream('gentype',Name,Value)
s = RandStream('gentype') creates a random number stream that uses the uniform pseudorandom number generator algorithm specified by gentype. RandStream.list returns all possible values for gentype, or see Choosing a Random Number Generator for details on generator algorithms.
s = RandStream('gentype',Name,Value) allows you to specify one or more optional Name,Value pairs to control creation of the stream.
Once you have created a random, you can use RandStream.setGlobalStream to make it the global stream, so that the functions rand, randi, and randn draw values from it.
Parameters for RandStream are:
Parameter | Description |
---|---|
Seed | Nonnegative scalar integer with which to initialize all streams. Seeds must be an integer between 0 and 2^{32} − 1 or 'shuffle' to create a seed based on the current time. Default is 0. |
NormalTransform | Transformation algorithm used by randn(s, ...) to generate normal pseudorandom values. Possible values are 'Ziggurat', 'Polar', or 'Inversion'. |
Create a random number stream, make it the global stream, and save and restore its state to reproduce the output of randn:
s = RandStream('mrg32k3a'); RandStream.setGlobalStream(s); savedState = s.State; z1 = randn(1,5) z1 = -0.1894 -1.4426 -0.3592 0.8883 -0.4337 s.State = savedState; z2 = randn(1,5) z2 = -0.1894 -1.4426 -0.3592 0.8883 -0.4337
z2 contains exactly the same values as z1.
Return rand, randi, and randn to their default startup settings:
s = RandStream('mt19937ar','Seed',0) RandStream.setGlobalStream(s);
Replace the current global random number stream with a stream whose seed is based on the current time, so rand, randi, and randn will return different values in different MATLAB^{®} sessions. It is usually not desirable to do this more than once per MATLAB session as it may affect the statistical properties of the random numbers MATLAB produces:
s = RandStream('mt19937ar','Seed','shuffle'); RandStream.setGlobalStream(s);