Clear Filters
Clear Filters

rng seed and cvpartition

4 views (last 30 days)
micholeodon on 14 Aug 2020
Commented: micholeodon on 15 Aug 2020
Dear Matlab Users,
Why cvpartition keeps choosing same test indices in folds from data, despite rng shuffle (thus different seed values assured) ?
When you run code below twice, you got same value of testInd. Why?
Thank you in advance for you help !
rng shuffle
parpool(parcluster('local'), 24)
tmp = rng;
seed = tmp.Seed;
nData = 89;
nFolds = 10;
selectedFold = 1;
tmp = cvpartition(nData, 'KFold', nFold)
x = find(tmp.test(selectedFold)); % extract test indices of fold 1
for ii = 1:3
testInd(ii,:) = x{ii};
% clear context and
clear x tmp

Answers (1)

Steven Lord
Steven Lord on 14 Aug 2020
You're calling rng shuffle on the client, not on the workers. See this documentation page.
  1 Comment
micholeodon on 15 Aug 2020
Thank you. I can read that:
"By default, the random numbers generated on each worker in a parfor loop are different from each other and from the random numbers generated on the client."
"each worker produces a unique sequence of random numbers.".
I have checked that and it is endeed true.
So in the context of my problem, it seems that each time I open MATLAB and run the code same set of random streams is produced and distributed to the workers. How to overcome that i.e. make sure that each time I open matlab and run that code I would get different set of random streams and thus different results? The article you pointed is mostly how to make things reproducible, same.
In RandStream documentation there is Example 7 - maybe this approach is recommended to my problem?

Sign in to comment.


Find more on Parallel for-Loops (parfor) in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!