Class MultiplyWithCarry256

All Implemented Interfaces:
RandomIntSource, RestorableUniformRandomProvider, UniformRandomProvider

public class MultiplyWithCarry256 extends IntProvider
Port from Marsaglia's "Multiply-With-Carry" algorithm.

Implementation is based on the (non-portable!) C code reproduced on that page.

Since:
1.0
See Also:
  • Field Details

    • Q_SIZE

      private static final int Q_SIZE
      Length of the state array.
      See Also:
    • SEED_SIZE

      private static final int SEED_SIZE
      Size of the seed.
      See Also:
    • A

      private static final long A
      Multiply.
      See Also:
    • state

      private final int[] state
      State.
    • index

      private int index
      Current index in "state" array.
    • carry

      private int carry
      Carry.
  • Constructor Details

    • MultiplyWithCarry256

      public MultiplyWithCarry256(int[] seed)
      Creates a new instance.
      Parameters:
      seed - Seed. If the length is larger than 257, only the first 257 elements will be used; if smaller, the remaining elements will be automatically set.
  • Method Details

    • getStateInternal

      protected byte[] getStateInternal()
      Creates a snapshot of the RNG state.
      Overrides:
      getStateInternal in class IntProvider
      Returns:
      the internal state.
    • setStateInternal

      protected void setStateInternal(byte[] s)
      Resets the RNG to the given state.
      Overrides:
      setStateInternal in class IntProvider
      Parameters:
      s - State (previously obtained by a call to BaseProvider.getStateInternal()).
      See Also:
    • setSeedInternal

      private void setSeedInternal(int[] seed)
      Seeds the RNG.
      Parameters:
      seed - Seed.
    • next

      public int next()
      Return the next random value.
      Returns:
      the next random value.