java.lang.Object
io.github.olyutorskii.aletojio.rng.mwc.Mwc
All Implemented Interfaces:
RndInt32
Direct Known Subclasses:
Cmwc

public class Mwc extends Object implements RndInt32
lag-r Multiply-with-carry random number generator.

Random sequence X(n) is derived as follows with parameter a(multiplier), b(divisor).

  • T(n) = a * X(n-r) + C(n-1)
  • C(n) = T(n) / b
  • X(n) = T(n) mod b

r is the length of the record that stores past results. "carry" comes from C(n-1).

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Mwc()
    Constructor.
    Mwc(int recLen, long mulArg)
    Constructor.
    Mwc(int recLen, long mulArg, long divArg)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected long
    funcCarryFromTval(long tVal)
    Function that takes T-value as an argument and returns carry.
    protected int
    funcRndFromTval(long tVal)
    Function that takes T-value as an argument and returns random result.
    protected long
    funcTval(long oldest, long lastCarry)
    Function that takes oldest result and last carry as arguments and returns T-value.
    long
    Return carry value.
    protected int
    Get oldest result.
    int
    Return length of past records.
    int
    Return next random number as 32bit int with random sign-bit.
    protected void
    pushLatest(int iVal)
    Push latest result.
    void
    setCarry(long lVal)
    Set carry value.
    void
    setSeeds(int... seeds)
    Set seeds.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Mwc

      public Mwc(int recLen, long mulArg, long divArg) throws IllegalArgumentException
      Constructor.
      • record length must be greater than 0.
      • multiplier must be greater than 1.
      • divisor must be greater than 1.

      If you want 32-bit uniformly distributed random numbers, the divisor must be 2**32, but you can compromise with 2**32 -1.

      Parameters:
      recLen - records length
      mulArg - multiplier
      divArg - divisor
      Throws:
      IllegalArgumentException - illegal argument
    • Mwc

      public Mwc(int recLen, long mulArg) throws IllegalArgumentException
      Constructor.
      • record length must be greater than 0.
      • multiplier must be greater than 1.

      Default divisor value = 2**32.

      Parameters:
      recLen - records length
      mulArg - multiplier
      Throws:
      IllegalArgumentException - illegal argument
    • Mwc

      public Mwc() throws IllegalArgumentException
      Constructor.
      • Default record length = 1038
      • Default multiplier value = 611373678
      • Default divisor value = 2**32

      * Caution : If you don't provide your entropy to seeds, it takes over million trials until the seed is scrambled.

      Throws:
      IllegalArgumentException - illegal argument
      See Also:
  • Method Details

    • getRecords

      public int getRecords()
      Return length of past records.
      Returns:
      length of past records
    • getCarry

      public long getCarry()
      Return carry value.
      Returns:
      carry value
    • setCarry

      public void setCarry(long lVal)
      Set carry value.
      Parameters:
      lVal - new carry value
    • setSeeds

      public void setSeeds(int... seeds) throws NullPointerException, IllegalArgumentException
      Set seeds.

      Seeds length must be same as the result records.

      Younger index represent older results.

      Parameters:
      seeds - seeds
      Throws:
      NullPointerException - argument is null
      IllegalArgumentException - size unmatched
    • getOldest

      protected int getOldest()
      Get oldest result.

      It's mean X(n-r).

      Returns:
      oldest result
    • pushLatest

      protected void pushLatest(int iVal)
      Push latest result.

      The oldest result at this time is lost.

      Parameters:
      iVal - latest result
    • funcTval

      protected long funcTval(long oldest, long lastCarry)
      Function that takes oldest result and last carry as arguments and returns T-value.
      Parameters:
      oldest - oldest result
      lastCarry - last carry
      Returns:
      T-value
    • funcRndFromTval

      protected int funcRndFromTval(long tVal)
      Function that takes T-value as an argument and returns random result.
      Parameters:
      tVal - T-value
      Returns:
      random result
    • funcCarryFromTval

      protected long funcCarryFromTval(long tVal)
      Function that takes T-value as an argument and returns carry.
      Parameters:
      tVal - T-value
      Returns:
      carry
    • nextInt32

      public int nextInt32()
      Return next random number as 32bit int with random sign-bit.
      Specified by:
      nextInt32 in interface RndInt32
      Returns:
      random number