Why do I get a list of identical numbers when I run randy:spawner()
below?
You must seed before generating random numbers.
Random number sequences generated from the same seed will be exactly the same.
If a process calls uniform/0
or uniform/1
without setting a seed first, seed/0
is called automatically.
seed/0
will seed random number generation with a default (fixed) value,
which can be accessed through seed0/0
. On my laptop it always returns {3172,9814,20125}
with a default process dictionary.
How could I change the code to achieve this?
In the simplest case, the solution from @EmilVikström is sufficient.
However, I do recommend to keep track of the random number generation state
so you can have a easier life when you're debugging.
A random number generation state is just a 3-tuple of integers, as returned by now()
. The seed is just the first state. Then you can use uniform_s/1
or uniform_s/2
to generate random numbers from specified states.
By using these functions with states, you can specify random number seeds outside your Erlang code, e.g. passing a seed through command-line options or environment variables.
- When you are testing/debugging, fix the seed so that each time you run your program will give the same result.
- When you are satisfied, change the seed in order to (probably) continue debugging :-)
- When you are ready for production, just pass the current time (or whatever) as the seed, so you can have some randomness.