Parfor random numbers are not quite random
62 views (last 30 days)
Show older comments
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
Answers (2)
Star Strider
on 13 Dec 2024 at 18:11
Sequences generated by computers are called ‘pseudorandom’ because they are just that. They are generated cyclically.
2 Comments
Quentin Rebjock
on 13 Dec 2024 at 18:13
Edited: Quentin Rebjock
on 13 Dec 2024 at 18:14
Star Strider
on 13 Dec 2024 at 18:28
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
on 13 Dec 2024 at 19:34
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 Comments
Steven Lord
on 16 Dec 2024 at 14:26
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.)
See Also
Categories
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!