Class AbstractLcg

java.lang.Object
io.github.olyutorskii.aletojio.rng.lcg.AbstractLcg
Direct Known Subclasses:
LcgRndInt31, LcgRndInt32

public abstract class AbstractLcg extends Object
Common implementation of Linear congruential generator(LCG).

LCG is a commonly used random number generator in the past.

Recurrence relation sequences : X(n+1) = (X(n) * Mul + Inc) mod Mod

  • Mul : Multiplier
  • Inc : Increment
  • Mod : Modulus
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final long
    31bit bitmask.
    protected static final long
    32bit bitmask.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractLcg(long mulArg, long incArg, long modArg)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Get seed value.
    protected int
    Return next random number as int.
    protected long
    Calculate next seed value from current seed value.
    protected abstract int
    Calculate result number from seed.
    void
    setSeed(long seedArg)
    Set new seed value.

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • AbstractLcg

      protected AbstractLcg(long mulArg, long incArg, long modArg) throws IllegalArgumentException
      Constructor.
      • Multiplier must be 1 or greatrer.
      • Increment must be 0 or greatrer.
      • Modulus must be 2 or greater.
      Parameters:
      mulArg - multiplier
      incArg - increment
      modArg - modulus
      Throws:
      IllegalArgumentException - illegal argument
  • Method Details

    • nextIntImpl

      protected int nextIntImpl()
      Return next random number as int.

      Negative value returned if (and only) result bits are 32.

      Returns:
      random number
    • nextSeed

      protected long nextSeed() throws IllegalStateException
      Calculate next seed value from current seed value.
      Returns:
      next seed value
      Throws:
      IllegalStateException - seed overflow
    • seedToResult

      protected abstract int seedToResult()
      Calculate result number from seed.
      Returns:
      result number
    • setSeed

      public void setSeed(long seedArg) throws IllegalArgumentException
      Set new seed value.
      Parameters:
      seedArg - new seed value
      Throws:
      IllegalArgumentException - seed too small
    • getSeed

      public long getSeed()
      Get seed value.
      Returns:
      seed value