What you are running into is that the random module is erlang process based, it keeps a separate seed in each process which uses it. So when you start a number of processes using erlang:now as seed then they will get the sequence from erlang:now as first number. The subsequent numbers will diverge though.<br>
<br>Note that the number sequence from random is in fact quite good, it uses a good algorithm. *BUT* it is deterministic if you know one number/seed, so while it is perfectly ok for simulation and such, it is *NOT* safe to use for cryptographic purposes!<br>
<br>Robert<br><br><div class="gmail_quote">2008/11/12 Adam Kelly <span dir="ltr"><<a href="mailto:cthulahoops@gmail.com">cthulahoops@gmail.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
<br>
While writing some test code, I noticed some extremely odd behaviour<br>
from the random module.  Given a pretty standard parallel map<br>
function, and remembering that erlang:now() never returns the same<br>
value twice, I'd expect this code to produce random looking output.<br>
<br>
62> pmap:pmap(fun(_) -> {A, B, C} = erlang:now(), random:seed(A, B,<br>
C), random:uniform(1000) end, lists:seq(1,10)).<br>
[31,32,33,34,34,35,36,37,38,39]<br>
<br>
Throwing away the first ten numbers gives a sequence with a definite<br>
pattern.  (It increases and wraps around.)<br>
<br>
63> pmap:pmap(fun(_) -> {A, B, C} = erlang:now(), random:seed(A, B,<br>
C), [random:uniform(1000) || X <- lists:seq(1, 10)],<br>
random:uniform(1000) end, lists:seq(1,10)).<br>
[724,993,935,204,354,451,652,853,951,152]<br>
<br>
It works just as well with a single process map, but then I'd have<br>
been able to initialise the random seed once and would never have<br>
noticed.<br>
<br>
64> lists:map(fun(_) -> {A, B, C} = erlang:now(), random:seed(A, B,<br>
C), random:uniform(1000) end, lists:seq(1,10)).<br>
[288,289,290,291,292,292,293,294,294,295]<br>
<br>
So, is it an accepted fact that two processes started at almost the<br>
same time will produce highly correlated random sequences?<br>
<br>
Adam.<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br>