anchor.diff:         uniform_n=>fun exs1024_uniform/2},
        fun exs1024_seed/1}.
   
  +
  +-define(UINT21MASK, 16#00000000001fffff).
  +-define(UINT32MASK, 16#00000000ffffffff).
  +-define(UINT33MASK, 16#00000001ffffffff).
  +-define(UINT39MASK, 16#0000007fffffffff).
  +-define(UINT64MASK, 16#ffffffffffffffff).
  +
  +-type uint64() :: 0..16#ffffffffffffffff.
  +
   %% =====================================================================
   %% exs64 PRNG: Xorshift*64
   %% Algorithm by Sebastiano Vigna
   %% Reference URL: http://xorshift.di.unimi.it/
   %% =====================================================================
   
  -%% uint64(). 64bit unsigned integer type.
  -
  --type uint64() :: 0..16#ffffffffffffffff.
  -
  -%% exs64_state(). Internal state data type for exs64.
  -%% Internally represented as the record <code>#state{}</code>,
  -%% of the 128bit seed.
  -
   -type exs64_state() :: uint64().
   
  --define(UINT32MASK, 16#ffffffff).
  --define(UINT39MASK, 16#0000007fffffffff).
  --define(UINT64MASK, 16#ffffffffffffffff).
  +exs64_seed({A1, A2, A3}) ->
  +    {V1, _} = exs64_next(((A1 band ?UINT32MASK) * 4294967197 + 1)),
  +    {V2, _} = exs64_next(((A2 band ?UINT32MASK) * 4294967231 + 1)),
  +    {V3, _} = exs64_next(((A3 band ?UINT32MASK) * 4294967279 + 1)),
  +    ((V1 * V2 * V3) rem (?UINT64MASK - 1)) + 1.
   
   %% Advance xorshift64star state for one step.
   %% and generate 64bit unsigned integer from
   %% the xorshift64star internal state.
   
  --spec exs64_next(exs64_state()) ->
  -        {uint64(), exs64_state()}.
  +-spec exs64_next(exs64_state()) -> {uint64(), exs64_state()}.
   
   exs64_next(R) ->
       R1 = R bxor (R bsr 12),
  
positive.code: %% Reference URL: http://xorshift.di.unimi.it/
  %% =====================================================================
  
  %% exs1024_state(). Internal state data type for exs1024.
  %% Representing 16 64-bit numbers with a pair of
  %% the list and a reverse list.
  
  -type exs1024_state() :: {list(uint64()), list(uint64())}.
  
  -define(UINT33MASK, 16#1ffffffff).
  
  %% Calculation of xorshift1024star.
  %% exs1024_calc(S0, S1) -> {X, NS1}.
  %% X: random number output
  
  -spec exs1024_calc(uint64(), uint64()) -> {uint64(), uint64()}.
  
  exs1024_calc(S0, S1) ->
      S11 = S1 bxor ((S1 band ?UINT33MASK) bsl 31),
      S12 = S11 bxor (S11 bsr 11),
  
negative.code:     R3 = R2 bxor (R2 bsr 27),
      {(R3 * 2685821657736338717) band ?UINT64MASK, R3}.
  
  exs64_uniform(R0) ->
      {V, R1} = exs64_next(R0),
      {V / 18446744073709551616.0, R1}.
  
  exs64_uniform(Max, R) ->
      {V, R1} = exs64_next(R),
      {(V rem Max) + 1, R1}.
  