Generate the same random number stream with for and parfor loop

2 visualizzazioni (ultimi 30 giorni)
Dear all,
I have a question concerning random number generation. Is it possible to generate the same random number stream with a for and a parfor loop? Consider the following code example:
clear variables;
spmd
rng(0,'combRecursive');
end
seed = 5;
rng(seed);
for i = 1:4
r(:,i) = randn(4,1);
end
r
Executing this the result will be reproducably:
r = -0.6241 -0.7559 -1.1331 -0.5142
-0.0756 1.4777 0.1639 -1.3629
-0.2618 0.3802 -0.3092 -0.9377
-0.5144 -0.5975 0.0484 -0.2263
On the other hand, if I exchange for with parfor i will get(considering setting the random number generator to 5)
clear variables;
spmd
rng(5,'combRecursive');
end
seed = 5;
rng(seed);
parfor i = 1:4
r(:,i) = randn(4,1);
end
r
r =
-0.7559 -0.6241 -0.6241 -1.1331
1.4777 -0.0756 -0.0756 0.1639
0.3802 -0.2618 -0.2618 -0.3092
-0.5975 -0.5144 -0.5144 0.0484
How can I configure the random number generator in such a way, that parfor and for will give the same random numbers? I did read the documentation in:
but unfortunately was not able to achieve this.
Thank you very much. Kind regards,

Risposta accettata

Stephen23
Stephen23 il 26 Apr 2016
You can't, because the parfor execution order is not specified.
The easiest way to avoid this problem would be to generate all random numbers before the loop, and then use indexing to pick the appropriate slice within each iteration.

Più risposte (1)

Titus Edelhofer
Titus Edelhofer il 26 Apr 2016
Hi,
this can't work by design: the number generator in the client generates 16 random numbers (and reshapes to 4x4 matrix).
The parfor can't generate the same because:
  • the seed would have to be set to the value at the end of the random number generation of the previous iteration
  • even worse: parfor ordering is arbitrary, such a dependency on order is not allowed
Maybe you can explain a little more why you want to do this ...
Titus
  1 Commento
Markus Hofer
Markus Hofer il 26 Apr 2016
Hi,
Thank you very much for your quick answer. Yes, that is what I thought as well. That is a pitty though. The main goal is to use the generated random variables for producing some testvectors and compare the results of a system that is implemented on a software basis and on a hardware basis on these testvectors. Generating the results in simulation mode quite takes some time, that is why i wanted to use parallel computing. I already produced some random variables and results with random variables in a for loop and wanted to check if the results are the same with parfor. And there i found the discrepancy. But what i do now is to produce the random variables in a for loop (later with an initialized parfor random generator as shown in the links above), generate the testvectors, and calculate the simulation results in a parfor.
Markus

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by