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
Star Strider 2024-12-13
Sequences generated by computers are called ‘pseudorandom’ because they are just that. They are generated cyclically.
See More About for details.
  2 个评论
Quentin
Quentin 2024-12-13
编辑:Quentin 2024-12-13
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 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))
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.
.

请先登录,再进行评论。


Steven Lord
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))
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 个评论
Quentin
Quentin 2024-12-16
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 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 CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by