Class ThrustAltRNG

java.lang.Object
java.util.Random
com.github.tommyettinger.random.EnhancedRandom
com.github.yellowstonegames.old.v300.LegacyRandom
com.github.yellowstonegames.old.v300.ThrustAltRNG
All Implemented Interfaces:
StatefulRandomness, Externalizable, Serializable, RandomGenerator

public class ThrustAltRNG extends LegacyRandom implements StatefulRandomness
See Also:
  • Nested Class Summary

  • Field Summary

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

    Constructors
    Constructor
    Description
    Creates a new generator seeded using Math.random.
    ThrustAltRNG(long seed)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    static long
    determine(long state)
    Returns a random permutation of state; if state is the same on two calls to this, this will return the same number.
    static int
    determineBounded(long state, int bound)
    Given a state that should usually change each time this is called, and a bound that limits the result to some (typically fairly small) int, produces a pseudo-random int between 0 and bound (exclusive).
    static int
    determineBoundedShort(int state, int bound)
    Given an int state that should usually change each time this is called, and a bound that limits the result to some (typically fairly small) int, produces a pseudo-random int between 0 and bound (exclusive).
    static double
    determineDouble(long state)
    Returns a random double that is deterministic based on state; if state is the same on two calls to this, this will return the same float.
    static float
    determineFloat(long state)
    Returns a random float that is deterministic based on state; if state is the same on two calls to this, this will return the same float.
    static float
    Returns a random float that is deterministic based on an int state; if state is the same on two calls to this, this will return the same float.
    static int
    determineInt(int state)
    Returns a random permutation of state; if state is the same on two calls to this, this will return the same number.
    boolean
     
    long
    getSelectedState(int selection)
     
    long
    Get the current internal state of the StatefulRandomness as a long.
    int
     
     
    int
     
    int
    next(int bits)
    Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.
    long
    Using this method, any algorithm that needs to efficiently generate more than 32 bits of random data can interface with this randomness source.
    void
    setSeed(long seed)
     
    void
    setSelectedState(int selection, long value)
     
    void
    setState(long state)
    Set the current internal state of this StatefulRandomness with a long.
    long
    skip(long advance)
    Advances or rolls back the ThrustAltRNG's state without actually generating each number.
     

    Methods inherited from class com.github.tommyettinger.random.EnhancedRandom

    appendSerialized, appendSerialized, areEqual, fixGamma, fixGamma, getMinimumPeriod, lcm, mainlyGeneratesInt, maxDoubleOf, maxFloatOf, maxIntOf, maxLongOf, minDoubleOf, minFloatOf, minIntOf, minLongOf, nextBoolean, nextBoolean, nextBytes, nextDouble, nextDouble, nextExclusiveDouble, nextExclusiveDouble, nextExclusiveDouble, nextExclusiveDoubleEquidistant, nextExclusiveFloat, nextExclusiveFloat, nextExclusiveFloat, nextExclusiveFloatEquidistant, nextExclusiveSignedDouble, nextExclusiveSignedFloat, nextExponential, nextFloat, nextFloat, nextGaussian, nextGaussian, nextGaussianFloat, nextGaussianFloat, nextInclusiveDouble, nextInclusiveDouble, nextInclusiveDouble, nextInclusiveFloat, nextInclusiveFloat, nextInclusiveFloat, nextInt, nextInt, nextLong, nextLong, nextSign, nextSignedInt, nextSignedLong, nextTriangular, nextTriangular, nextTriangular, nextTriangular, nextUnsignedInt, previousInt, previousLong, probit, processSignedInt32, processUnsignedInt32, randomElement, randomElement, rateGamma, readExternal, seedFromMath, setState, setState, setState, setState, setState, setState, setState, setState, setState, setState, setWith, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, stringDeserialize, stringDeserialize, stringSerialize, stringSerialize, writeExternal

    Methods inherited from class Random

    doubles, doubles, doubles, doubles, from, ints, ints, ints, ints, longs, longs, longs, longs

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface RandomGenerator

    equiDoubles, isDeprecated
  • Field Details

    • state

      public long state
      Can be any long value.
  • Constructor Details

    • ThrustAltRNG

      public ThrustAltRNG()
      Creates a new generator seeded using Math.random.
    • ThrustAltRNG

      public ThrustAltRNG(long seed)
  • Method Details

    • getTag

      public String getTag()
      Specified by:
      getTag in class com.github.tommyettinger.random.EnhancedRandom
    • getState

      public long getState()
      Get the current internal state of the StatefulRandomness as a long.
      Specified by:
      getState in interface StatefulRandomness
      Returns:
      the current internal state of this object.
    • setState

      public void setState(long state)
      Set the current internal state of this StatefulRandomness with a long.
      Specified by:
      setState in interface StatefulRandomness
      Overrides:
      setState in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      state - a 64-bit long
    • setSeed

      public void setSeed(long seed)
      Specified by:
      setSeed in class com.github.tommyettinger.random.EnhancedRandom
    • getStateCount

      public int getStateCount()
      Overrides:
      getStateCount in class com.github.tommyettinger.random.EnhancedRandom
    • getSelectedState

      public long getSelectedState(int selection)
      Overrides:
      getSelectedState in class com.github.tommyettinger.random.EnhancedRandom
    • setSelectedState

      public void setSelectedState(int selection, long value)
      Overrides:
      setSelectedState in class com.github.tommyettinger.random.EnhancedRandom
    • next

      public int next(int bits)
      Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.
      Overrides:
      next in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      bits - the number of bits to be returned
      Returns:
      the integer containing the appropriate number of bits
    • nextLong

      public 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 RandomGenerator
      Specified by:
      nextLong in class com.github.tommyettinger.random.EnhancedRandom
      Returns:
      a random long between Long.MIN_VALUE and Long.MAX_VALUE (both inclusive)
    • skip

      public long skip(long advance)
      Advances or rolls back the ThrustAltRNG's state without actually generating each number. Skips forward or backward a number of steps specified by advance, where a step is equal to one call to nextLong(), and returns the random number produced at that step (you can get the state with getState()).
      Overrides:
      skip in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      advance - Number of future generations to skip over; can be negative to backtrack, 0 gets the most-recently-generated number
      Returns:
      the random long generated after skipping forward or backwards by advance numbers
    • copy

      public ThrustAltRNG 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 class com.github.tommyettinger.random.EnhancedRandom
      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
    • determine

      public static long determine(long state)
      Returns a random permutation of state; if state is the same on two calls to this, this will return the same number. This is expected to be called with some changing variable, e.g. determine(++state), where the increment for state should be odd but otherwise doesn't really matter. This multiplies state by 0x6C8E9CF570932BD5L within this method, so using a small increment won't be much different from using a very large one, as long as it is odd. The period is 2 to the 64 if you increment or decrement by 1.
      Parameters:
      state - a variable that should be different every time you want a different random result; using determine(++state) is recommended to go forwards or determine(--state) to generate numbers in reverse order
      Returns:
      a pseudo-random permutation of state
    • determineFloat

      public static float determineFloat(long state)
      Returns a random float that is deterministic based on state; if state is the same on two calls to this, this will return the same float. This is expected to be called with a changing variable, e.g. determine(++state), where the increment for state should be odd but otherwise doesn't really matter. This multiplies state by 0x6C8E9CF570932BD5L within this method, so using a small increment won't be much different from using a very large one, as long as it is odd. The period is 2 to the 64 if you increment or decrement by 1, but there are only 2 to the 30 possible floats between 0 and 1.
      Parameters:
      state - a variable that should be different every time you want a different random result; using determine(++state) is recommended to go forwards or determine(--state) to generate numbers in reverse order
      Returns:
      a pseudo-random float between 0f (inclusive) and 1f (exclusive), determined by state
    • determineDouble

      public static double determineDouble(long state)
      Returns a random double that is deterministic based on state; if state is the same on two calls to this, this will return the same float. This is expected to be called with a changing variable, e.g. determine(++state), where the increment for state should be odd but otherwise doesn't really matter. This multiplies state by 0x6C8E9CF570932BD5L within this method, so using a small increment won't be much different from using a very large one, as long as it is odd. The period is 2 to the 64 if you increment or decrement by 1, but there are only 2 to the 62 possible doubles between 0 and 1.
      Parameters:
      state - a variable that should be different every time you want a different random result; using determine(++state) is recommended to go forwards or determine(--state) to generate numbers in reverse order
      Returns:
      a pseudo-random double between 0.0 (inclusive) and 1.0 (exclusive), determined by state
    • determineBounded

      public static int determineBounded(long state, int bound)
      Given a state that should usually change each time this is called, and a bound that limits the result to some (typically fairly small) int, produces a pseudo-random int between 0 and bound (exclusive). The bound can be negative, which will cause this to produce 0 or a negative int; otherwise this produces 0 or a positive int. The state should change each time this is called, generally by incrementing by an odd number (not an even number, especially not 0). It's fine to use determineBounded(++state, bound) to get a different int each time. The period is usually 2 to the 64 when you increment or decrement by 1, but some bounds may reduce the period (in the extreme case, a bound of 1 would force only 0 to be generated, so that would make the period 1).
      Parameters:
      state - a variable that should be different every time you want a different random result; using determineBounded(++state, bound) is recommended to go forwards or determineBounded(--state, bound) to generate numbers in reverse order
      bound - the outer exclusive bound for the int this produces; can be negative or positive
      Returns:
      a pseudo-random int between 0 (inclusive) and bound (exclusive)
    • determineInt

      public static int determineInt(int state)
      Returns a random permutation of state; if state is the same on two calls to this, this will return the same number. This is expected to be called with some changing variable, e.g. determine(state = state + 1 | 0), where the increment for state should be odd but otherwise doesn't really matter (the | 0 is needed to force overflow to occur correctly on GWT; if you know you won't target GWT you can use determine(++state) instead). This multiplies state by 0x62BD5 within this method, so using a small increment won't be much different from using a very large one, as long as it is odd. The period is 2 to the 32 if you increment or decrement by 1 (and perform any bitwise operation, such as | 0, if you might target GWT). If you use this on GWT and don't perform a bitwise operation on the new value for state, then the period will gradually shrink as precision is lost by the JavaScript double that GWT will use for state as a Java int. If you know that state will start at 0 and you call with determine(++state), then on GWT you may not have to worry at all until 2 to the 34 calls have been made, after which state may cease to have the precision to represent an increase by 1 when the math inside this method is considered. The period will have been exhausted by that point anyway (4 times), so it's more of a concern if state may start at a much higher int.
      This only uses int math, and should be fast on GWT.
      Parameters:
      state - a variable that should be different every time you want a different random result; using determine(state = state + 1 | 0) is recommended to go forwards or determine(state = state - 1 | 0) to generate numbers in reverse order
      Returns:
      a pseudo-random permutation of state
    • determineBoundedShort

      public static int determineBoundedShort(int state, int bound)
      Given an int state that should usually change each time this is called, and a bound that limits the result to some (typically fairly small) int, produces a pseudo-random int between 0 and bound (exclusive). The bound should be between -65536 and 65535, that is, the range of a short. It can be negative, which will cause this to produce 0 or a negative int; otherwise this produces 0 or a positive int. The state should change each time this is called, generally by incrementing by an odd number (not an even number, especially not 0). It's fine to use determineBounded(++state, bound) to get a different int each time. The period is usually 2 to the 64 when you increment or decrement by 1, but some bounds may reduce the period (in the extreme case, a bound of 1 would force only 0 to be generated, so that would make the period 1).
      This only uses int math, unlike other bounded determine() methods, but this requires the bound to be small. It should be very fast on GWT.
      Parameters:
      state - an int variable that should be different every time you want a different random result; using determineBounded(++state, bound) is recommended to go forwards or determineBounded(--state, bound) to generate numbers in reverse order
      bound - the outer exclusive bound for the int this produces; should be between -65536 and 65535, inclusive
      Returns:
      a pseudo-random int between 0 (inclusive) and bound (exclusive)
    • determineFloatFromInt

      public static float determineFloatFromInt(int state)
      Returns a random float that is deterministic based on an int state; if state is the same on two calls to this, this will return the same float. This is expected to be called with a changing variable, e.g. determine(++state), where the increment for state should be odd but otherwise doesn't really matter. This multiplies state by 0x62BD5 within this method, so using a small increment won't be much different from using a very large one, as long as it is odd. The period is 2 to the 32 if you increment or decrement by 1, but there are only 2 to the 30 possible floats between 0 and 1, and this can generate less than 2 to the 24 of them.
      Except for the final conversion to float, this only uses int math, and should be fast on GWT.
      Parameters:
      state - an int variable that should be different every time you want a different random result; using determine(++state) is recommended to go forwards or determine(--state) to generate numbers in reverse order
      Returns:
      a pseudo-random float between 0f (inclusive) and 1f (exclusive), determined by state