Seeding the pseudo random number generator
yerl@REDACTED
yerl@REDACTED
Wed Jun 9 02:10:32 CEST 2004
Hi!
Have a look to the Primes numbers generator from Joe's example at
(http://www.erlang.org/examples/examples-2.0.html).
The seed function is:
new_seed() ->
{_,_,X} = erlang:now(),
{H,M,S} = time(),
H1 = H * X rem 32767,
M1 = M * X rem 32767,
S1 = S * X rem 32767,
put(random_seed, {H1,M1,S1}).
This function generates different numbers each time when it's called.
Good luck.
/yerl
----Message d'origine----
>Date: Fri, 4 Jun 2004 00:18:32 +0300
>De: Einar Karttunen <ekarttun@REDACTED>
>A: Erlang ML <erlang-questions@REDACTED>
>Sujet: Seeding the pseudo random number generator
>
>Hello
>
>Is there a way to automatically seed the pseudo random number generator?
>Using random:uniform I get allways the same sequence of numbers. I could
>allways seed the generator when my module is called but that does not
>seem a good idea... And what is the preferred way of seeding the
>generator? Using erlang:now seems the easiest approach, but the
>generated numbers are not random by any means.
>
>seed() ->
> {A,B,C} = erlang:now(),
> random:seed(A,B,C).
>
>Using /dev/urandom seems possible, but I would like to avoid it if
>possible...
>
>- Einar Karttunen
>
>
More information about the erlang-questions
mailing list