Class ISAACRandom

All Implemented Interfaces:
RandomIntSource, RestorableUniformRandomProvider, UniformRandomProvider

public class ISAACRandom extends IntProvider
A fast cryptographic pseudo-random number generator.

ISAAC (Indirection, Shift, Accumulate, Add, and Count) generates 32-bit random numbers. ISAAC has been designed to be cryptographically secure and is inspired by RC4. Cycles are guaranteed to be at least 240 values long, and they are 28295 values long on average. The results are uniformly distributed, unbiased, and unpredictable unless you know the seed.

This code is based (with minor changes and improvements) on the original implementation of the algorithm by Bob Jenkins.

Since:
1.0
See Also:
  • Field Details

    • SIZE_L

      private static final int SIZE_L
      Log of size of rsl[] and mem[].
      See Also:
    • SIZE

      private static final int SIZE
      Size of rsl[] and mem[].
      See Also:
    • H_SIZE

      private static final int H_SIZE
      Half-size of rsl[] and mem[].
      See Also:
    • MASK

      private static final int MASK
      For pseudo-random lookup.
      See Also:
    • GLD_RATIO

      private static final int GLD_RATIO
      The golden ratio.
      See Also:
    • rsl

      private final int[] rsl
      The results given to the user.
    • mem

      private final int[] mem
      The internal state.
    • count

      private int count
      Count through the results in rsl[].
    • isaacA

      private int isaacA
      Accumulator.
    • isaacB

      private int isaacB
      The last result.
    • isaacC

      private int isaacC
      Counter, guarantees cycle is at least 2^40.
    • arr

      private final int[] arr
      Service variable.
    • isaacX

      private int isaacX
      Service variable.
    • isaacI

      private int isaacI
      Service variable.
    • isaacJ

      private int isaacJ
      Service variable.
  • Constructor Details

    • ISAACRandom

      public ISAACRandom(int[] seed)
      Creates a new ISAAC random number generator.
      Parameters:
      seed - Initial seed
  • 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)
      Reseeds the RNG.
      Parameters:
      seed - Seed. Cannot be null.
    • next

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

      private void isaac()
      Generate 256 results.
    • isaac2

      private void isaac2()
      Intermediate internal loop.
    • isaac3

      private void isaac3()
      Lowest level internal loop.
    • initState

      private void initState()
      Initialize, or reinitialize, this instance of rand.
    • shuffle

      private void shuffle()
      Shuffle array.
    • setState

      private void setState(int start)
      Set the state by copying the internal arrays.
      Parameters:
      start - First index into mem array.