Class GoatRNG

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

public final class GoatRNG
extends Object
implements RandomnessSource, Serializable
A larger-period generator with 128 bits of state, good speed, and high quality in PractRand testing; it is at least 1-dimensionally equidistributed. It appears to be slightly faster than a Xorshift128+ generator, like RandomXS128 in libGDX, with practically the same period (this has a period of 2 to the 128), but it isn't as fast as as OrbitRNG or GearRNG (Goat seems to have higher statistical quality than either of those late in testing, though). It passes 64TB of PractRand testing with no anomalies, and might be able to endure tests past even that (the default setting stops at 32TB); I had set up PractRand to test up to 128TB but it ran out of memory after 5 days of nonstop testing on all 6 cores. If 16GB of RAM isn't enough to find even an anomaly in GoatRNG, then it is probably quite good.
Unlike most RNGs from outside Goat, Gear, and Orbit's family, this relies on a conditional and comparison to achieve both its period and randomness goals. All of these generators use two additive sequences for their states, and stall one of the state updates when the other state matches a criterion. For Orbit, it stalls only when stateA is 0 (roughly 1 in 18 quintillion generated numbers); for Gear, stateA must be less than 1863783533 away from the minimum long value (roughly 1 in 10 billion generated numbers), and here, stateA must be less than 5096992405936522019L to stall (roughly 3 in 4 generated numbers).
Unlike GearRNG, this allows all long values for both states; unlike OrbitRNG, there shouldn't be significant correlation between an even value for stateB and the value 1 greater than that (such as 4 and 5, or 100 and 101).
The name comes from how goats are reliably hard to predict, like an RNG should be. It is called GhoulRNG in some tests run on it; an earlier version of GoatRNG also exists that does well in testing for the first 32TB and then suddenly has serious issues.
Created by Tommy Ettinger on 5/8/2020.
See Also:
Serialized Form
  • Field Summary

    Fields 
    Modifier and Type Field Description
    long stateA
    Can be any long value.
    long stateB
    Can be any long value.
  • Constructor Summary

    Constructors 
    Constructor Description
    GoatRNG()
    Creates a new generator seeded using Math.random.
    GoatRNG​(long seed)  
    GoatRNG​(long seedA, long seedB)  
  • Method Summary

    Modifier and Type Method Description
    GoatRNG copy()
    Produces a copy of this RandomnessSource that, if next() and/or nextLong() are called on this object and the copy, both will generate the same sequence of random numbers from the point copy() was called.
    boolean equals​(Object o)  
    long getStateA()
    Get the "A" part of the internal state as a long.
    long getStateB()
    Get the "B" part of the internal state as a long.
    int hashCode()  
    int next​(int bits)
    Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.
    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 setStateA​(long stateA)
    Set the "A" part of the internal state with a long.
    void setStateB​(long stateB)
    Set the "B" part of the internal state with a long.
    String toString()  

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • getStateA

      public long getStateA()
      Get the "A" part of the internal state as a long.
      Returns:
      the current internal state of this object.
    • setStateA

      public void setStateA​(long stateA)
      Set the "A" part of the internal state with a long.
      Parameters:
      stateA - any 64-bit long
    • getStateB

      public long getStateB()
      Get the "B" part of the internal state as a long.
      Returns:
      the current internal "B" state of this object.
    • setStateB

      public void setStateB​(long stateB)
      Set the "B" part of the internal state with a long.
      Parameters:
      stateB - any 64-bit long
    • next

      public final int next​(int bits)
      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
    • nextLong

      public final 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.

      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)
    • copy

      public GoatRNG copy()
      Produces a copy of this RandomnessSource that, if next() and/or nextLong() are called on this object and the copy, both will generate the same sequence of random numbers from the point copy() was called. This just need to copy the state so it isn't shared, usually, and produce a new value with the same exact state.
      Specified by:
      copy in interface RandomnessSource
      Returns:
      a copy of this RandomnessSource
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object