Parfor random numbers are not quite random

62 views (last 30 days)
Quentin Rebjock
Quentin Rebjock on 13 Dec 2024 at 17:26
Commented: Steven Lord on 16 Dec 2024 at 14:26
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
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.
See More About for details.
  2 Comments
Quentin Rebjock
Quentin Rebjock on 13 Dec 2024 at 18:13
Edited: Quentin Rebjock on 13 Dec 2024 at 18:14
Thanks for your comment.
I agree that we shouldn't expect the numbers to be truly random.
However, the behavior with a standard for loop is different:
clear; clc;
k = 100000;
v = zeros(k, 1);
for i = 1:k
v(i) = rand();
end
size(unique(v))
With this code all the elements of v are different.
Star Strider
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))
ans = 1×2
100000 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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))
ans = 1×2
100000 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
histogram(v, 1E+4)
grid
I doubt that there is any statistically significant difference between these two results.
.

Sign in to comment.


Steven Lord
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))
ans = 1×2
100000 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
That's not the behavior I see. Did you do something on the parallel pool before running this code?
  3 Comments
Quentin Rebjock
Quentin Rebjock on 16 Dec 2024 at 12:10
Thanks for your answer.
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.
I get the same result as you do when I restart MATLAB and run the script.
I guess I will generate all random variables before the parfor loop to have more control.
Steven Lord
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.)

Sign in to comment.

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!