Initializing random seeds

Daniel Luna <>
Wed Feb 8 11:40:53 CET 2006


Reposting since I noticed that this address was not subscribed to the 
list. If it shows up twice, you know why...

On Wed, 8 Feb 2006, Corrado Santoro (with some cutting by me) wrote:
> I'm trying to use the functions of the random module to generate random
> numbers in different processes. But I obtain the same random number
> sequence for each process.

> Number generated from proc <0.128.0> is 9
> Number generated from proc <0.129.0> is 9
> Number generated from proc <0.130.0> is 9
> Number generated from proc <0.131.0> is 9

> Do you have an hint to avoid this problem?

Yes. Patience is the key. Try to generate just two more numbers and they
should be different.

With a few more numbers generated we will see only the first numbers 
generated are the same. If we look at the seeds we see that the last 
seed numbers differs in your test:

> Seed of <0.128.0> is {1139,388083,514683}
> Seed of <0.129.0> is {1139,388083,514695}
> Seed of <0.130.0> is {1139,388083,514701}
> Seed of <0.131.0> is {1139,388083,514707}

This is not enough to make any difference in the first generated number 
(for small N). From random.erl:

uniform() = ... + A3/30323
uniform(N) = trunc(uniform() * N) + 1.

So for a small difference in A3 you will have the same random number the
first time around. Given time though (just twice more should be enough),
these numbers will separate[*].

/Luna
[*] But not that much unfortunately. They will give less "random" numbers
than 4 processer with differing A1s and A2s. But probably enough for most
applications.
--
Daniel Luna                           | Top reasons that I have a beard:
                     |  a) Laziness.
http://www.update.uu.se/~luna/        |  b) I can.
Don't look at my homepage (it stinks).|  c) I can get away with it.



More information about the erlang-questions mailing list