Class GWTRNG

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

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

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
     
    int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new generator seeded using two calls to Math.random().
    GWTRNG(int seed)
    Constructs this GWTRNG by dispersing the bits of seed using setSeed(int) across the two parts of state this has.
    GWTRNG(int stateA, int stateB)
    Constructs this GWTRNG by calling setState(int, int) on stateA and stateB as given; see that method for the specific details (stateA and stateB are kept as-is unless they are both 0).
    GWTRNG(long seed)
    Constructs this GWTRNG by splitting the given seed across the two parts of state this has with setState(long).
    GWTRNG(String seed)
    Hashes seed using both CrossHash.hash(CharSequence) and String.hashCode() and uses those two results as the two states with setState(int, int).
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a copy of this GWTRNG; it will generate the same random numbers, given the same calls in order, as this GWTRNG at the point copy() is called.
    static long
    determine(int state)
    A deterministic random long generator that, given one int state as input, returns an almost-always-different long as a result.
    static int
    determineBounded(int state, int bound)
    A deterministic random int generator that, given one int state and an outer int bound as input, returns an int between 0 (inclusive) and bound (exclusive) as a result, which should have no noticeable correlation between state and the result.
    static double
    determineDouble(int state)
    A deterministic random double generator that, given one int state as input, returns an almost-always-different double between 0.0 and 1.0 as a result.
    static float
    determineFloat(int state)
    A deterministic random float generator that, given one int state as input, returns an almost-always-different float between 0.0f and 1.0f as a result.
    static int
    determineInt(int state)
    A deterministic random int generator that, given one int state as input, irreversibly returns an almost-always-different int as a result.
    boolean
     
    long
    getSelectedState(int selection)
    Identical to getState(), this gets both int states as one long value.
    long
    Get the current internal state of the StatefulRandomness as a long.
    int
     
    int
     
    int
    Always returns 1 (counting in terms of long states).
     
    int
     
    int
    next(int bits)
    Get up to 32 bits (inclusive) of random output; the int this produces will not require more than bits bits to represent.
    boolean
    Get a random bit of state, interpreted as true or false with approximately equal likelihood.
    double
    Gets a random double between 0.0 inclusive and 1.0 exclusive.
    float
    Gets a random float between 0.0f inclusive and 1.0f exclusive.
    int
    Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive).
    int
    nextInt(int bound)
    Returns a random non-negative integer below the given bound, or 0 if the bound is 0 or negative.
    long
    Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive).
    void
    setSeed(int seed)
    Sets the state of this generator using one int, running it through Zog32RNG's algorithm two times to get two ints.
    void
    setSeed(long seed)
    Sets both int states of the SilkRNG with one long value.
    void
    setSelectedState(int selection, long value)
    Sets both int states of the SilkRNG with one long value.
    void
    setState(int stateA, int stateB)
    Sets the current internal state of this GWTRNG with three ints, where stateA and stateB can each be any int unless they are both 0 (which will be treated as if stateA is 1 and stateB is 0).
    void
    setState(long state)
    Set the current internal state of this StatefulRandomness with a long.
    void
    setStateA(int stateA)
    Sets the first part of the state to the given int.
    void
    setStateB(int stateB)
    Sets the second part of the state to the given int.
     

    Methods inherited from class LegacyRandom

    nextSignedInt, nextSignedLong

    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, 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, 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, skip, 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

    • stateA

      public int stateA
    • stateB

      public int stateB
  • Constructor Details

    • GWTRNG

      public GWTRNG()
      Creates a new generator seeded using two calls to Math.random().
    • GWTRNG

      public GWTRNG(int seed)
      Constructs this GWTRNG by dispersing the bits of seed using setSeed(int) across the two parts of state this has.
      Parameters:
      seed - an int that won't be used exactly, but will affect both components of state
    • GWTRNG

      public GWTRNG(long seed)
      Constructs this GWTRNG by splitting the given seed across the two parts of state this has with setState(long).
      Parameters:
      seed - a long that will be split across both components of state
    • GWTRNG

      public GWTRNG(int stateA, int stateB)
      Constructs this GWTRNG by calling setState(int, int) on stateA and stateB as given; see that method for the specific details (stateA and stateB are kept as-is unless they are both 0).
      Parameters:
      stateA - the number to use as the first part of the state; this will be 1 instead if both seeds are 0
      stateB - the number to use as the second part of the state
    • GWTRNG

      public GWTRNG(String seed)
      Hashes seed using both CrossHash.hash(CharSequence) and String.hashCode() and uses those two results as the two states with setState(int, int). If seed is null, this won't call String.hashCode() on it and will instead use 1 as that state (to avoid the forbidden double-zero case).
      Parameters:
      seed - any String; may be null
  • Method Details

    • getTag

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

      public int next(int bits)
      Get up to 32 bits (inclusive) of random output; the int this produces will not require more than bits bits to represent.
      Overrides:
      next in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      bits - an int between 1 and 32, both inclusive
      Returns:
      a random number that fits in the specified number of bits
    • nextInt

      public int nextInt()
      Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive).
      Specified by:
      nextInt in interface RandomGenerator
      Overrides:
      nextInt in class com.github.tommyettinger.random.EnhancedRandom
      Returns:
      a 32-bit random int.
    • nextInt

      public int nextInt(int bound)
      Returns a random non-negative integer below the given bound, or 0 if the bound is 0 or negative.
      Specified by:
      nextInt in interface RandomGenerator
      Overrides:
      nextInt in class LegacyRandom
      Parameters:
      bound - the upper bound (exclusive)
      Returns:
      the found number
    • nextLong

      public long nextLong()
      Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive).
      Specified by:
      nextLong in interface RandomGenerator
      Specified by:
      nextLong in class com.github.tommyettinger.random.EnhancedRandom
      Returns:
      a 64-bit random long.
    • nextBoolean

      public boolean nextBoolean()
      Get a random bit of state, interpreted as true or false with approximately equal likelihood. This implementation uses a sign check as a safeguard, since its algorithm is based on (but is not equivalent to) xoroshiro, which recommends a sign check instead of using the least significant bit.
      Specified by:
      nextBoolean in interface RandomGenerator
      Overrides:
      nextBoolean in class com.github.tommyettinger.random.EnhancedRandom
      Returns:
      a random boolean.
    • nextDouble

      public double nextDouble()
      Gets a random double between 0.0 inclusive and 1.0 exclusive. This returns a maximum of 0.9999999999999999 because that is the largest double value that is less than 1.0 .
      Specified by:
      nextDouble in interface RandomGenerator
      Overrides:
      nextDouble in class LegacyRandom
      Returns:
      a double between 0.0 (inclusive) and 0.9999999999999999 (inclusive)
    • nextFloat

      public float nextFloat()
      Gets a random float between 0.0f inclusive and 1.0f exclusive. This returns a maximum of 0.99999994 because that is the largest float value that is less than 1.0f .
      Specified by:
      nextFloat in interface RandomGenerator
      Overrides:
      nextFloat in class LegacyRandom
      Returns:
      a float between 0f (inclusive) and 0.99999994f (inclusive)
    • copy

      public GWTRNG copy()
      Creates a copy of this GWTRNG; it will generate the same random numbers, given the same calls in order, as this GWTRNG at the point copy() is called. The copy will not share references with this GWTRNG.
      Specified by:
      copy in class com.github.tommyettinger.random.EnhancedRandom
      Returns:
      a copy of this GWTRNG
    • setSeed

      public void setSeed(int seed)
      Sets the state of this generator using one int, running it through Zog32RNG's algorithm two times to get two ints. If the states would both be 0, state A is assigned 1 instead.
      Parameters:
      seed - the int to use to produce this generator's state
    • getStateA

      public int getStateA()
    • setStateA

      public void setStateA(int stateA)
      Sets the first part of the state to the given int. As a special case, if the parameter is 0 and stateB is already 0, this will set stateA to 1 instead, since both states cannot be 0 at the same time. Usually, you should use setState(int, int) to set both states at once, but the result will be the same if you call setStateA() and then setStateB() or if you call setStateB() and then setStateA().
      Parameters:
      stateA - any int
    • getStateB

      public int getStateB()
    • setStateB

      public void setStateB(int stateB)
      Sets the second part of the state to the given int. As a special case, if the parameter is 0 and stateA is already 0, this will set stateA to 1 and stateB to 0, since both cannot be 0 at the same time. Usually, you should use setState(int, int) to set both states at once, but the result will be the same if you call setStateA() and then setStateB() or if you call setStateB() and then setStateA().
      Parameters:
      stateB - any int
    • setState

      public void setState(int stateA, int stateB)
      Sets the current internal state of this GWTRNG with three ints, where stateA and stateB can each be any int unless they are both 0 (which will be treated as if stateA is 1 and stateB is 0).
      Parameters:
      stateA - any int (if stateA and stateB are both 0, this will be treated as 1)
      stateB - any int
    • 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. You should avoid passing 0; this implementation will treat it as 1.
    • setSeed

      public void setSeed(long seed)
      Sets both int states of the SilkRNG with one long value.
      Specified by:
      setSeed in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      seed - any long value except 0 (this will handle 0 by changing it to 1)
    • getStateCount

      public int getStateCount()
      Always returns 1 (counting in terms of long states).
      Overrides:
      getStateCount in class com.github.tommyettinger.random.EnhancedRandom
      Returns:
      1 (one)
    • getSelectedState

      public long getSelectedState(int selection)
      Identical to getState(), this gets both int states as one long value.
      Overrides:
      getSelectedState in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      selection - doesn't matter; unused
      Returns:
      the combined states as one long
    • setSelectedState

      public void setSelectedState(int selection, long value)
      Sets both int states of the SilkRNG with one long value.
      Overrides:
      setSelectedState in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      selection - doesn't matter; unused
      value - the long value that will be split up into the two int states this uses
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

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

      public static int determineInt(int state)
      A deterministic random int generator that, given one int state as input, irreversibly returns an almost-always-different int as a result. Unlike the rest of GWTRNG, this will not produce all possible ints given all ints as inputs, and probably a third of all possible ints cannot be returned. You should call this with GWTRNG.determineInt(state = state + 1 | 0) (you can subtract 1 to go backwards instead of forwards), which will allow overflow in the incremented state to be handled the same on GWT as on desktop.
      Parameters:
      state - an int that should go up or down by 1 each call, as with GWTRNG.determineInt(state = state + 1 | 0) to handle overflow
      Returns:
      a not-necessarily-unique int that is usually very different from state
    • determineBounded

      public static int determineBounded(int state, int bound)
      A deterministic random int generator that, given one int state and an outer int bound as input, returns an int between 0 (inclusive) and bound (exclusive) as a result, which should have no noticeable correlation between state and the result. You should call this with GWTRNG.determineBound(state = state + 1 | 0, bound) (you can subtract 1 to go backwards instead of forwards), which will allow overflow in the incremented state to be handled the same on GWT as on desktop. Like most bounded int generation in SquidLib, this uses some long math, but most of the function uses ints.
      Parameters:
      state - an int that should go up or down by 1 each call, as with GWTRNG.determineBounded(state = state + 1 | 0, bound) to handle overflow
      bound - the outer exclusive bound, as an int; may be positive or negative
      Returns:
      an int between 0 (inclusive) and bound (exclusive)
    • determine

      public static long determine(int state)
      A deterministic random long generator that, given one int state as input, returns an almost-always-different long as a result. This can only return a tiny fraction of all possible longs, since there are at most 2 to the 32 possible ints and this doesn't even return different values for each of those. You should call this with GWTRNG.determine(state = state + 1 | 0) (you can subtract 1 to go backwards instead of forwards), which will allow overflow in the incremented state to be handled the same on GWT as on desktop.
      Parameters:
      state - an int that should go up or down by 1 each call, as with GWTRNG.determine(state = state + 1 | 0) to handle overflow
      Returns:
      a not-necessarily-unique long that is usually very different from state
    • determineFloat

      public static float determineFloat(int state)
      A deterministic random float generator that, given one int state as input, returns an almost-always-different float between 0.0f and 1.0f as a result. Unlike the rest of GWTRNG, this might not produce all possible floats given all ints as inputs, and some fraction of possible floats cannot be returned. You should call this with GWTRNG.determineFloat(state = state + 1 | 0) (you can subtract 1 to go backwards instead of forwards), which will allow overflow in the incremented state to be handled the same on GWT as on desktop.
      Parameters:
      state - an int that should go up or down by 1 each call, as with GWTRNG.determineFloat(state = state + 1 | 0) to handle overflow
      Returns:
      a not-necessarily-unique float from 0.0f to 1.0f that is usually very different from state
    • determineDouble

      public static double determineDouble(int state)
      A deterministic random double generator that, given one int state as input, returns an almost-always-different double between 0.0 and 1.0 as a result. This cannot produce more than a tiny fraction of all possible doubles because the input is 32 bits and at least 53 bits are needed to represent most doubles from 0.0 to 1.0. You should call this with GWTRNG.determineDouble(state = state + 1 | 0) (you can subtract 1 to go backwards instead of forwards), which will allow overflow in the incremented state to be handled the same on GWT as on desktop.
      Parameters:
      state - an int that should go up or down by 1 each call, as with GWTRNG.determineDouble(state = state + 1 | 0) to handle overflow
      Returns:
      a not-necessarily-unique double from 0.0 to 1.0 that is usually very different from state