mardi 13 mars 2018

Matlab: Generating different random numbers in each parfor loop

I have the following piece of code:

nruns=100
nloops=2
zz=zeros(nloops,nruns);

for wLoop=1:nloops
delete(gcp('nocreate'));
parpool(npools);
parfor wRun=1:nruns
zz(wLoop,wRun)=rand
end
end
disp('done')

size(unique(zz))

Every time the pool gets closed and reopened, it appears that the seed gets reinitialised to its default value, so that I get far fewer different numbers than the expected nruns*nloops. This behaviour does not change if I add

rng('shuffle','combRecursive')

either inside or just before the parfor. If I don't close the pool at every iteration there is no problem, however using a different pool on each iteration is necessary for the usage I have in mind. How can I get genuinely different random numbers upon each iteration of the parfor loop?




Aucun commentaire:

Enregistrer un commentaire