randn not random in parfor loops
이전 댓글 표시
I hope I'm wrong guys,
I try a PARFOR loop with r2011b and I get a non random result (same non random walks [differents in the result set but same if I relaunch the execution])
code pattern //////
matlabpool local 8; parfor i1 = 1:100 ret = randn(1,5000); .......
//////////////
is there any bugs with this version?
it's enough to take off "matlabpool local 8;" in order to get a random result
Reg
Lorenzo
댓글 수: 1
John Fox
2017년 7월 20일
I had the exact same problem. My for loops gave a different answer than my parfor loops. The reason is
As described in Control Random Number Streams, each worker in a cluster has an independent random number generator stream. By default, therefore, each worker in a pool, and each iteration in a parfor-loop has a unique, independent set of random numbers. Subsequent runs of the parfor-loop generate different numbers.
I fixed this with rng(123,'twister'). At least this worked for me.
답변 (1개)
Daniel Shub
2012년 11월 9일
You are correct and this is a good thing and consistent with the idea that MATLAB uses the same "seed" every time it starts. Peter Perkins gives a work around on Loren's blog
In that comment he says that
stream = RandStream('mrg32k3a');
parfor ii = 1:10
set(stream,'Substream',ii);
par(ii) = rand(stream);
end
will give the same result as
stream = RandStream('mrg32k3a');
for ii = 1:10
set(stream,'Substream',ii);
par(ii) = rand(stream);
end
he then goes on to talk about independent streams.
카테고리
도움말 센터 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!