Class IsaacRNG

java.lang.Object
squidpony.squidmath.IsaacRNG
All Implemented Interfaces:
Serializable, RandomnessSource

public class IsaacRNG
extends Object
implements RandomnessSource
This is a port of the public domain Isaac64 (cryptographic) random number generator to Java. It is a RandomnessSource here, so it should generally be used to make an RNG, which has more features. IsaacRNG is slower than the non-cryptographic RNGs in SquidLib, but much faster than cryptographic RNGs that need SecureRandom, plus it's compatible with GWT and Android! Created by Tommy Ettinger on 8/1/2016.
See Also:
Serialized Form
  • Constructor Summary

    Constructors 
    Constructor Description
    IsaacRNG()
    Constructs an IsaacRNG with no seed; this will produce one sequence of numbers as if the seed were 0 (which it essentially is, though passing 0 to the constructor that takes a long will produce a different sequence) instead of what the other RandomnessSources do (initialize with a low-quality random number from Math.random()).
    IsaacRNG​(long seed)
    Constructs an IsaacRNG with its state filled by the value of seed, run through the LightRNG algorithm.
    IsaacRNG​(long[] seed)
    Constructs an IsaacRNG with the given seed, which should be a rather large array of long values.
    IsaacRNG​(String seed)
    Constructs an IsaacRNG with its state filled by repeated hashing of seed.
  • Method Summary

    Modifier and Type Method Description
    IsaacRNG copy()
    Produces another RandomnessSource, but the new one will not produce the same data as this one.
    boolean equals​(Object o)  
    void fillBlock​(long[] data)
    Generates enough pseudo-random long values to fill data and assigns them to it.
    void init​(boolean flag)
    Initializes this IsaacRNG; typically used from the constructor but can be called externally.
    void init​(long seed)
    Can be used to re-initialize this IsaacRNG as if using the single-long constructor.
    void init​(long[] seed)
    Can be used to re-initialize this IsaacRNG as if using the long-array constructor.
    void init​(String seed)
    Can be used to re-initialize this IsaacRNG as if using the single-String constructor.
    int next​(int bits)
    Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.
    long[] nextBlock()
    Generates and returns a block of 256 pseudo-random long values.
    long nextLong()
    Using this method, any algorithm that needs to efficiently generate more than 32 bits of random data can interface with this randomness source.
    void regen()
    Generates 256 results to be used by later calls to next() or nextLong().
    String toString()  

    Methods inherited from class java.lang.Object

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

    • IsaacRNG

      public IsaacRNG()
      Constructs an IsaacRNG with no seed; this will produce one sequence of numbers as if the seed were 0 (which it essentially is, though passing 0 to the constructor that takes a long will produce a different sequence) instead of what the other RandomnessSources do (initialize with a low-quality random number from Math.random()).
    • IsaacRNG

      public IsaacRNG​(long[] seed)
      Constructs an IsaacRNG with the given seed, which should be a rather large array of long values. You should try to make seed a long[256], but smaller arrays will be tolerated without error. Arrays larger than 256 items will only have the first 256 used.
      Parameters:
      seed - an array of longs to use as a seed; ideally it should be 256 individual longs
    • IsaacRNG

      public IsaacRNG​(long seed)
      Constructs an IsaacRNG with its state filled by the value of seed, run through the LightRNG algorithm.
      Parameters:
      seed - any long; will have equal influence on all bits of state
    • IsaacRNG

      public IsaacRNG​(String seed)
      Constructs an IsaacRNG with its state filled by repeated hashing of seed.
      Parameters:
      seed - a String that should be exceptionally long to get the best results.
  • Method Details

    • regen

      public final void regen()
      Generates 256 results to be used by later calls to next() or nextLong(). This is a fast (not small) implementation.
    • init

      public void init​(long[] seed)
      Can be used to re-initialize this IsaacRNG as if using the long-array constructor. The given seed should be a rather large array of long values. You should try to make seed a long[256], but smaller arrays will be tolerated without error. Arrays larger than 256 items will only have the first 256 used.
      Parameters:
      seed - an array of longs to use as a seed; ideally it should be 256 individual longs
    • init

      public void init​(long seed)
      Can be used to re-initialize this IsaacRNG as if using the single-long constructor.
      Parameters:
      seed - any long; will have equal influence on all bits of state
    • init

      public final void init​(String seed)
      Can be used to re-initialize this IsaacRNG as if using the single-String constructor.
      Parameters:
      seed - a String; if non-null, its contents will be used as a seed
    • init

      public final void init​(boolean flag)
      Initializes this IsaacRNG; typically used from the constructor but can be called externally.
      Parameters:
      flag - if true, use data from seed; if false, initializes this to unseeded random state
    • nextLong

      public final long nextLong()
      Description copied from interface: RandomnessSource
      Using this method, any algorithm that needs to efficiently generate more than 32 bits of random data can interface with this randomness source. Get a random long between Long.MIN_VALUE and Long.MAX_VALUE (both inclusive).
      Specified by:
      nextLong in interface RandomnessSource
      Returns:
      a random long between Long.MIN_VALUE and Long.MAX_VALUE (both inclusive)
    • nextBlock

      public final long[] nextBlock()
      Generates and returns a block of 256 pseudo-random long values.
      Returns:
      a freshly-allocated array of 256 pseudo-random longs, with all bits possible
    • fillBlock

      public final void fillBlock​(long[] data)
      Generates enough pseudo-random long values to fill data and assigns them to it.
    • next

      public final int next​(int bits)
      Description copied from interface: RandomnessSource
      Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.
      Specified by:
      next in interface RandomnessSource
      Parameters:
      bits - the number of bits to be returned
      Returns:
      the integer containing the appropriate number of bits
    • copy

      public final IsaacRNG copy()
      Produces another RandomnessSource, but the new one will not produce the same data as this one. This is meant to be a "more-secure" generator, so this helps reduce the ability to guess future results from a given sequence of output.
      Specified by:
      copy in interface RandomnessSource
      Returns:
      another RandomnessSource with the same implementation but no guarantees as to generation
    • equals

      public boolean equals​(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object