[erlang-questions] Requirements for seed integers in the rand module

Raimo Niskanen raimo+erlang-questions@REDACTED
Wed Aug 15 14:48:19 CEST 2018

On Tue, Aug 07, 2018 at 01:23:04PM +0200, Krzysztof Jurewicz wrote:
> The seed/2 and seed_s/2 functions in the rand module take a 3-element tuple of integers as a seed, however it doesn’t document what are the requirements for those integers (how they need to be distributed). If seed is omitted (the seed/1 and seed_s/1 functions), then the following (undocumented) value is used:
> {erlang:phash2([{node(),self()}]),
>  erlang:system_time(),
>  erlang:unique_integer()}
> I have 64 bytes of entropy which I want to use as a seed. Is it ok to do it in the way below?
> {binary:decode_unsigned(Entropy), 0, 0}

The different algorithms use the 3-tuple seed in slightly different ways,
but in general it they expect the entropy to be spread over all integers.

The default algorithm (exrop) and the previous default (exsp) has a
slightly imperfect way to try to aviod zero seed that works if the supplied
integers have 26 bits or less.

The exs64 algorithm uses the 32 lowest bits of each integer.

The exs1024s algorithm uses the 21 lowest bits of each integer.

So I would say the most generic way to use a 64 bit binary as entropy
source for the current algorithms would be:

    <<A:22, B:21, C:21>> = Entropy,
    Seed = {A, B, C},

In my pull request https://github.com/erlang/otp/pull/1857 I have added two
new seed formats.  One that takes a 64-bit integer seed that is fed through
a SplitMix64 generator to seed all state words, and one that takes a
list of state words directly and just checks that not all are zero.
This can give better control over the seeding but will not appear
until OTP-22.0...

/ Raimo Niskanen, Erlang/OTP, Ericsson AB

More information about the erlang-questions mailing list