Parfor random numbers are not quite random
3 次查看(过去 30 天)
显示 更早的评论
Suppose I run this code:
clear; clc;
k = 100000;
v = zeros(k, 1);
parfor i = 1:k
v(i) = rand();
end
size(unique(v))
I would expect all the elements of v to be unique because they are randomly generated.
However, I observe that many of them coincide.
How is it possible? How can I get random numbers as intended?
Follow-up question:
With the code above, the numbers are always the same when I restart MATLAB. But I want truly new random numbers whenever I run the script. Is it enough to do the following?
clear; clc;
k = 100000;
v = zeros(k, 1);
parfor i = 1:k
rng(i)
v(i) = rand();
end
回答(2 个)
Star Strider
2024-12-13
Sequences generated by computers are called ‘pseudorandom’ because they are just that. They are generated cyclically.
2 个评论
Star Strider
2024-12-13
That is most likely because you’re using a different seed each time. Given enough iterations, it will also prove to be pseudorandom.
Choose whatever approach give you the result you want.
clear; clc;
k = 100000;
v = zeros(k, 1);
parfor i = 1:k
v(i) = rand();
end
size(unique(v))
figure
histogram(v, 1E+4)
grid
clear; clc;
k = 100000;
v = zeros(k, 1);
for i = 1:k
v(i) = rand();
end
size(unique(v))
figure
histogram(v, 1E+4)
grid
I doubt that there is any statistically significant difference between these two results.
.
Steven Lord
2024-12-13
I would expect all the elements of v to be unique because they are randomly generated.
However, I observe that many of them coincide.
clear; clc;
k = 100000;
v = zeros(k, 1);
parfor i = 1:k
v(i) = rand();
end
size(unique(v))
That's not the behavior I see. Did you do something on the parallel pool before running this code?
3 个评论
Steven Lord
2024-12-16
I had actually modified the rng seed in earlier runs, and I was naively thinking that it would reset at each call of the script.
No, changing the state of the pseudorandom number generator does not reset automatically.
Are you changing the state once, once on each worker, or once per loop iteration? If the last of those, thinking it will make things "more random", don't. See the Note on this documentation page.
If you want a generator you could create, use, and then discard without affecting the global state you could create a RandStream object yourself. See this documentation page for more information, including how to use substreams (you could create one substream per worker.)
另请参阅
类别
在 Help Center 和 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!