issue with Repeating Random Numbers in parfor-Loops

조회 수: 1 (최근 30일)
Eli4ph
Eli4ph 2016년 10월 4일
댓글: Eli4ph 2016년 10월 6일
I want to get reproducible random number sequence in parfor-loops and tried the example in the documentation. However, the results are switching from two choices, which conflicts with the statement in the documentation.
what is the problem?
Here is the pilot code:
n = 3;
r = zeros(1,n);
parfor i = 1:n
stream = RandStream.getGlobalStream();
stream.Substream = i;
r(i) = rand;
end
r
one result is
r =
0.2646 0.2537 0.2276
another is
r =
0.2646 0.2537 0.3111
BTW, the results are produced by 2015a.
Creating random number stream explicitly can solve this problem, but it causes a significant increase in computing time when there are many parfor-loops. Is there a solution which is not expensive concerning computing time?

채택된 답변

Edric Ellis
Edric Ellis 2016년 10월 5일
I think you missed out a crucial piece of the example. In your code, you're accessing the current global stream without having explicitly set it first. You need the section:
spmd
rng(0,'combRecursive');
end
This ensures that RandStream.getGlobalStream() returns the same stream on each worker, and then setting the substream will act as it should.
  댓글 수: 1
Eli4ph
Eli4ph 2016년 10월 6일
Thanks! I thought that piece is another example and irrelevant with the parfor example. I should have checked the documentation again before asking. Sorry for that. Thanks again.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Gareth Thomas
Gareth Thomas 2016년 10월 4일

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by