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.

    • data : Vector (BitVec cfg.wordSize) cfg.stateSize

      Data for current state.

    • index : Fin cfg.stateSize

      Current data index.

    Instances For
      @[specialize #[0]]

      Mersenne Twister initialization given an optional seed.

      Equations
      Instances For
        @[irreducible]
        def Batteries.Random.MersenneTwister.Config.init.loop (cfg : Batteries.Random.MersenneTwister.Config) (w : BitVec cfg.wordSize) (v : Array (BitVec cfg.wordSize)) (h : v.size cfg.stateSize) :
        Vector (BitVec cfg.wordSize) cfg.stateSize

        Inner loop for Mersenne Twister initalization.

        Equations
        • One or more equations did not get rendered due to their size.
        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

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

            Equations
            • state.update 0 = state
            • state.update steps.succ = state.twist.update steps
            Instances For
              @[specialize #[0]]

              Mersenne Twister iteration.

              Equations
              Instances For
                @[inline]

                Tempering step for Mersenne Twister.

                Equations
                • One or more equations did not get rendered due to their size.
                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