Seeding random number generator in parfor properly

29 views (last 30 days)
I was reading the Documentation article Repeat Random Numbers in parfor-Loops concerning getting repeatable random numbers when using parfor.
I am trying to set the seed for a number of repeats of a continuous-time model run for a number of individuals such that any given repeat of an individual is comparable to that repeat of another individual. The model uses random numbers for noise.
Is it acceptable to just set the random number generator's seed with code like the following:
parfor idx = 1:10
rng(idx, 'twister')
% Code continues...
end
or is it better to use the more complex setup with RandStreams as advised in the Documentation article?
Are there any shortcomings to setting the seed as I've shown that I'm missing?
Thanks in advance!
  1 Comment
Per  Hyldahl
Per Hyldahl on 24 Feb 2020
Hi THomas,
I do not have experience in this regards, but a simple approach comes to my mind; would it be possible to generate the random numbers in advance prior to entering the parfor-loop?
/ Per

Sign in to comment.

Accepted Answer

Edric Ellis
Edric Ellis on 24 Feb 2020
I'm not an expert by any means, but my understanding is that your proposed approach will give you reproducible results, but the statistical properties of the random numbers sampled inside your parfor loop using this approach will not be as "good" as if you followed the approach from the documentation.
My hand-waving understanding of the situation is this: every pseudo-random number generator (PRNG) algorithm has essentially a continuous cycle of numbers you can sample, and this cycle eventually repeats (for 'twister', the period is ). The generator also has a "state" that defines where in the cycle you're going to sample next. This state must be able to identify every point in the cycle. This state has more information in it than a single double-precision number - so therefore, not every "state" has a corresponding "seed" value, and the mapping from one to the other is essentially uni-directional - you can compute a state from a seed, but not vice versa. Therefore, using a seed for a PRNG means you don't know where on the cycle you really are. This means that the random samples you get aren't guaranteed to be statistically independent.
The approach in the doc using streams and sub-streams is essentially a more detailed way of dividing up the cycle of numbers, and if you use this approach, the random samples you get will not overlap (unless you take a huge number of samples) - the streams are statistically independent.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!