Seeding the pseudo random number generator
Samuel Rivas
samuel@REDACTED
Fri Jun 4 08:54:02 CEST 2004
Einar Karttunen wrote:
> 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.
>
Hello:
A computer can't generate truly random numbers, a random sequence is
simulated by means of which is called 'pseudorandom' generators. A
'pseudorandom' sequence of, for example, uniform numbers will pass the
statistical tests as a real random sequence will do, but the pseudorandom one
is not random at all, since it can be generated again starting with the same
seed value. If you seed the generator with erlang:now, you can be sure that
in different executions you always will start the random sequence with
different seed value, so all your pseudorandom sequences will be different.
1> {A, B, C} = erlang:now().
{1086,331220,447082}
2> random:seed(A,B,C).
undefined
3> random:uniform(10).
8
4> random:uniform(10).
4
5> random:uniform(10).
9
6> random:uniform(10).
2
7> random:uniform(10).
5
9> halt().
1> {A, B, C} = erlang:now().
{1086,331315,987603}
2> random:seed(A,B,C).
undefined
3> random:uniform(10).
6
4> random:uniform(10).
3
5> random:uniform(10).
1
6> random:uniform(10).
7
7>
Regards
--
Samuel
More information about the erlang-questions
mailing list