Class OrbitRNG

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

public final class OrbitRNG
extends Object
implements RandomnessSource, Serializable
A variant on ThrustAltRNG that gives up some speed to gain a much better period and the ability to produce all possible long values over that period. Its period is 2 to the 128, and it produces all long outputs with equal likelihood. OrbitRNG is close to ThrustAltRNG in implementation, and ThrustAltRNG passes PractRand and TestU01 just fine, but OrbitRNG should actually be more robust. For some purposes you may want to instead consider TangleRNG, which also has two states and uses a very similar algorithm, but it skips some work Orbit does and in doing so speeds up a lot and drops its period down to 2 to the 64. An individual TangleRNG can't produce all possible long outputs and can produce some duplicates, but each pair of states for a TangleRNG has a different set of which outputs will be skipped and which will be duplicated. Since it would require months of solid number generation to exhaust the period of a TangleRNG, and that's the only time an output can be confirmed as skipped, it's probably fine for some usage to use many different TangleRNGs, all seeded differently. In other cases you could use just one OrbitRNG; its period is large enough not to worry about exhausting it in a game, though it isn't quite as fast as TangleRNG. Orbit is extremely close in speed to a 64-bit variant on Lathe32RNG, and should have similarly high quality; it should be slightly slower than XoRoRNG but doesn't fail the statistical tests that XoRo fails rather badly.
The name comes from how the pair of states act like two planets orbiting a star at different rates, and also evokes the larger-scale period relative to TangleRNG.
Created by Tommy Ettinger on 7/9/2018.
See Also:
Serialized Form
  • Field Summary

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

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

    Modifier and Type Method Description
    OrbitRNG 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 OrbitRNG 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