在并行循环中请求随机数总是返回相同的伪随机数。我该如何避免这种情况?
% workers initialization:
if matlabpool('size') == 0
matlabpool('open',2);
else
matlabpool('close');
matlabpool('open',2);
end
% parallel loop always give the same random numbers...
parfor k = 1:10
fprintf([num2str(rand(1,1)), ' ']);
end
一个理想的解决方案是通过 CPU 时间或类似方式在每个线程中初始化伪随机数生成器。像 rng('shuffle')
这样的东西似乎在这里无济于事...
控制台输出:
Sending a stop signal to all the workers ... stopped.
Starting matlabpool using the 'local' profile ... connected to 2 workers.
0.32457 0.66182 0.63488 0.64968
0.26459 0.096779 0.50518 0.48662 0.034895 0.85227
RandStream.shuffleSeed
方法的部分。 - chappjc