[erlang-questions] Erlang random number generator weak?

Robert Virding <>
Wed Nov 12 22:40:59 CET 2008


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.

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!

Robert

2008/11/12 Adam Kelly <>

> Hi,
>
> While writing some test code, I noticed some extremely odd behaviour
> from the random module.  Given a pretty standard parallel map
> function, and remembering that erlang:now() never returns the same
> value twice, I'd expect this code to produce random looking output.
>
> 62> pmap:pmap(fun(_) -> {A, B, C} = erlang:now(), random:seed(A, B,
> C), random:uniform(1000) end, lists:seq(1,10)).
> [31,32,33,34,34,35,36,37,38,39]
>
> Throwing away the first ten numbers gives a sequence with a definite
> pattern.  (It increases and wraps around.)
>
> 63> pmap:pmap(fun(_) -> {A, B, C} = erlang:now(), random:seed(A, B,
> C), [random:uniform(1000) || X <- lists:seq(1, 10)],
> random:uniform(1000) end, lists:seq(1,10)).
> [724,993,935,204,354,451,652,853,951,152]
>
> It works just as well with a single process map, but then I'd have
> been able to initialise the random seed once and would never have
> noticed.
>
> 64> lists:map(fun(_) -> {A, B, C} = erlang:now(), random:seed(A, B,
> C), random:uniform(1000) end, lists:seq(1,10)).
> [288,289,290,291,292,292,293,294,294,295]
>
> So, is it an accepted fact that two processes started at almost the
> same time will produce highly correlated random sequences?
>
> Adam.
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081112/c53a6fcd/attachment.html>


More information about the erlang-questions mailing list