Reproducible random number sequence with parfor

3 views (last 30 days)
I would like to produce a reproducible random number sequence with parfor. I do not want to create a new random stream for each worker, but supply the workers with distinct and deterministic segments of a global random stream. I tried using instructions on this page (<http://www.mathworks.com/help/stats/reproducibility-in-parallel-statistical-computations.html)>, but it did not work for me.
if true
s = RandStream('mlfg6331_64');
options = statset('UseParallel',true,'Streams',s,'UseSubstreams',true);
agent = zeros(3,2);
parfor ind = 1:indn
agent(ind,:) = [randi(10), randi(10)];
end
agent
end
I can use the following code to achieve my goal, but that is not efficient, and I fear creating correlations between the parfor index and random numbers generated.
if true
agent = zeros(3,2);
parfor ind = 1:indn
rng(ind);
agent(ind,:) = [randi(10), randi(10)];
end
agent
end

Accepted Answer

Brendan Hamm
Brendan Hamm on 10 Aug 2015
Edited: Brendan Hamm on 10 Aug 2015
In the first instance you are not passing the options (nor could you) to the random number generating process, so this information is not being used. This functionality was really meant for other Statistical and Machine Learning functions like bootstrapping and not for random number generation. By default the workers are each started at a different state, and this is the same state every time the parpool is created. In the second example, your concern about the correlation would be true if the RNG was using the Mersenne Twister Algorithm. This however was considered in the parallel computing toolbox and by default the algorithm is the Combined Multiple Recursive algorithm, so this is not a problem as long as you don't change the algorithm. Therefore your second solution is a perfectly valid solution.
a = nan(1e5,2);
parfor k = 1:2
rng(k);
a(:,k) = rand(1e5,1);
end
corr(a)
ans =
1.0000 -0.0019
-0.0019 1.0000

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!