[erlang-questions] Erlang random number generator weak?

Adam Kelly <>
Wed Nov 12 19:32:15 CET 2008


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.



More information about the erlang-questions mailing list