[erlang-questions] random:seed in R14, R15, ...

Richard O'Keefe <>
Fri Jul 20 00:12:20 CEST 2012

On 19/07/2012, at 6:23 PM, Uwe Dauernheim wrote:

> When writing test cases that exercise e.g. random:uniform/0 one wants to use random:seed/1 with a fixed seed for deterministic test results, but providing the seed function with a fixed value seems to result in a different sequence for R14 and R15 (maybe for older or upcoming release as well). This is a problem when distributing your code and you are not sure which release will be used.
> What is the common solution for this?

The solution that is common across programming languages is to
use your own random number generator.

For example, here's Marsaglia's KISS in Erlang:

initial_state() ->
    { 123456789, 362436000, 521288629, 7654321 }.

kiss({X0,Y0,Z0,C0}) ->
    X  = 69069 * X0 + 12345,
    Y1 = ((Y0 band 524287) bsl 13) bxor Y0,
    Y2 = (Y1 bsr 17) band Y1,
    Y  = ((Y2 band 134217727) bsl 5) bxor Y2,
    T  = Z0 * 698769069 + C0,
    Z  = T band 4294967295,
    C  = T bsr 32,
    {(X + Y + Z) band 4294967295, {X,Y,Z,C}}.

This generates a stream of 32-bit integers
"with period > 2^125" according to Marsaglia.
(I hope I've transcribed it correctly.)  The
initial state is any four 32-bit integers.

kissf(State0) ->
    {I, State} = kiss(State),
    {(I+1)/4294967297.0, State}.

This gives you 32 bits worth of random floats
in the open interval (0.0,1.0).  With trickier
code one could do better.

More information about the erlang-questions mailing list