Initializing random seeds

Ulf Wiger (AL/EAB) <>
Wed Feb 8 16:30:10 CET 2006


Corrado Santoro wrote:
> Yes! I saw the source code in random.erl, and maybe my 
> solution of using an ad hoc process for random number 
> generation is the key. Any other hint???

If you're always spawning from the same process,
perhaps you could call now() before each time, and
permute the microsec component from the right into
the seed of the parent process?

new_seed() -> 
  {S1,S2,S3} =
    case get(random_seed) of
      undefined ->
         random:seed(),
         get(random_seed);
      Seed -> Seed
    end,
  {_, _, MSecs} = now(),
  random:seed({S2,S3,MSecs}),
  get(random_seed).

A curious aspect of random.erl is that the
random:seed/1 function returns the _old_ value,
and there is no get_seed() function.

Anyway, something like the above should give you
more varying distributions.

Regards,
Uffe



More information about the erlang-questions mailing list