Documentation |
MATLAB^{®} software includes generator algorithms that allow you to create multiple independent random number streams. The RandStream.create factory method allows you to create three streams that have the same generator algorithm and seed value but are statistically independent.
[s1,s2,s3]=RandStream.create('mlfg6331_64','NumStreams',3) s1 = mlfg6331_64 random stream StreamIndex: 1 NumStreams: 3 Seed: 0 NormalTransform: Ziggurat s2 = mlfg6331_64 random stream StreamIndex: 2 NumStreams: 3 Seed: 0 NormalTransform: Ziggurat s3 = mlfg6331_64 random stream StreamIndex: 3 NumStreams: 3 Seed: 0 NormalTransform: Ziggurat
As evidence of independence, you can see that these streams are largely uncorrelated.
r1=rand(s1,100000,1); r2=rand(s2,100000,1); r3=rand(s3,100000,1); corrcoef([r1,r2,r3]) ans = 1.0000 -0.0017 -0.0010 -0.0017 1.0000 -0.0050 -0.0010 -0.0050 1.0000
By using different seeds, you can create streams that return different values and act separately from one another.
s1=RandStream('mt19937ar','seed',1); s2=RandStream('mt19937ar','seed',2); s3=RandStream('mt19937ar','seed',3);
Seed values must be integers between 0 and $${2}^{32}-1$$. With different seeds, streams typically return values that are uncorrelated.
r1=rand(s1,100000,1); r2=rand(s2,100000,1); r3=rand(s3,100000,1); corrcoef([r1,r2,r3]) ans = 1.0000 0.0030 0.0045 0.0030 1.0000 -0.0015 0.0045 -0.0015 1.0000
For generator types that do not explicitly support independent streams, different seeds provide a method to create multiple streams. However, using a generator specifically designed for multiple independent streams is a better option, as the statistical properties across streams are better understood.
Depending on the application, it might be useful to create only some of the streams in a set of independent streams. The StreamIndex property returns the index of a specified stream from a set of factory-generated streams.
numLabs=256; labIndex=4; s1=RandStream.create('mlfg6331_64', 'NumStreams',numLabs,'StreamIndices',labIndex) s1= mlfg6331_64 random stream StreamIndex: 4 NumStreams: 256 Seed: 0 NormalTransform: Ziggurat
Multiple streams, since they are statistically independent, can be used to verify the precision of a simulation. For example, a set of independent streams can be used to repeat a Monte Carlo simulation several times in different MATLAB sessions or on different processors and determine the variance in the results. This makes multiple streams useful in large-scale parallel simulations.
Note: Not all generators algorithms support multiple streams. See the table of generator algorithms in Choosing a Random Number Generator for a summary of generator properties. |