Documentation

Batteries.Data.Random.MersenneTwister

Mersenne Twister #

Generic implementation for the Mersenne Twister pseudorandom number generator.

All choices of parameters from Matsumoto and Nishimura (1998) are supported, along with later refinements. Parameters for the standard 32-bit MT19937 and 64-bit MT19937-64 algorithms are provided. Both RandomGen and Stream interfaces are provided.

Use mt19937.init seed to create a MT19937 PRNG with a 32 bit seed value; use mt19937_64.init seed to create a MT19937-64 PRNG with a 64 bit seed value. If omitted, default seed choices will be used.

Sample usage:

import Batteries.Data.Random.MersenneTwister

open Batteries.Random.MersenneTwister

def mtgen := mt19937.init -- default seed 4357

#eval (Stream.take mtgen 5).fst -- [874448474, 2424656266, 2174085406, 1265871120, 3155244894]

References: #

Mersenne Twister configuration.

Letters in parentheses correspond to variable names used by Matsumoto and Nishimura (1998) and Nishimura (2000).

  • wordSize : Nat

    Word size (w).

  • stateSize : Nat

    Degree of recurrence (n).

  • shiftSize : Fin self.stateSize

    Middle word (m).

  • maskBits : Fin self.wordSize

    Twist value (r).

  • xorMask : BitVec self.wordSize

    Coefficients of the twist matrix (a).

  • temperingShifts : Nat ร— Nat ร— Nat ร— Nat

    Tempering shift parameters (u, s, t, l).

  • temperingMasks : BitVec self.wordSize ร— BitVec self.wordSize ร— BitVec self.wordSize

    Tempering mask parameters (d, b, c).

  • initMult : BitVec self.wordSize

    Initialization multiplier (f).

  • initSeed : BitVec self.wordSize

    Default initialization seed value.

Instances For

    Mersenne Twister State.

    Instances For
      @[specialize #[0]]
      def Batteries.Random.MersenneTwister.Config.init (cfg : Config) (seed : BitVec cfg.wordSize := cfg.initSeed) :
      State cfg

      Mersenne Twister initialization given an optional seed.

      Equations
      Instances For
        @[specialize #[0]]

        Apply the twisting transformation to the given state.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          def Batteries.Random.MersenneTwister.State.update {cfg : Config} (state : State cfg) (steps : Nat := 1) :
          State cfg

          Update the state by a number of generation steps (default 1).

          Equations
          Instances For
            @[specialize #[0]]
            def Batteries.Random.MersenneTwister.State.next {cfg : Config} (state : State cfg) :
            BitVec cfg.wordSize ร— State cfg

            Mersenne Twister iteration.

            Equations
            Instances For

              32 bit Mersenne Twister (MT19937) configuration.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For

                64 bit Mersenne Twister (MT19937-64) configuration.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For