Class LinnormRNG

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

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

  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor that seeds the generator with two calls to Math.random.
    LinnormRNG(long seed)
    Constructor that uses the given seed as the state without changes; all long seeds are acceptable.
    Constructor that hashes seed with CrossHash.hash64(CharSequence) and uses the result as the state.
  • Method Summary

    Modifier and Type
    Method
    Description
    Produces a copy of this LinnormRNG 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)
    Static randomizing method that takes its state as a parameter; state is expected to change between calls to this.
    static int
    determineBounded(long state, int bound)
    Static randomizing method that takes its state as a parameter and limits output to an int between 0 (inclusive) and bound (exclusive); state is expected to change between calls to this.
    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.
    boolean
     
    long
    getSelectedState(int selection)
     
    long
    Gets the current state of this generator.
    int
     
     
    int
     
    int
    next(int bits)
     
    boolean
    Gets a random value, true or false.
    void
    nextBytes(byte[] bytes)
    Given a byte array as a parameter, this will fill the array with random bytes (modifying it in-place).
    double
    Gets a uniform random double in the range [0.0,1.0)
    double
    nextDouble(double outer)
    Gets a uniform random double in the range [0.0,outer) given a positive parameter outer.
    float
    Gets a uniform random float in the range [0.0,1.0)
    int
    Can return any int, positive or negative, of any size permissible in a 32-bit signed integer.
    int
    nextInt(int bound)
    Exclusive on the outer bound.
    int
    nextInt(int inner, int outer)
    Inclusive inner, exclusive outer.
    long
    Can return any long, positive or negative, of any size permissible in a 64-bit signed integer.
    long
    nextLong(long bound)
    Exclusive on bound (which may be positive or negative), with an inner bound of 0.
    long
    nextLong(long lower, long upper)
    Inclusive inner, exclusive outer; lower and upper can be positive or negative and there's no requirement for one to be greater than or less than the other.
    void
    setSeed(long seed)
     
    void
    setSelectedState(int selection, long value)
     
    void
    setState(long seed)
    Sets the seed (also the current state) of this generator.
     

    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, nextDouble, nextExclusiveDouble, nextExclusiveDouble, nextExclusiveDouble, nextExclusiveDoubleEquidistant, nextExclusiveFloat, nextExclusiveFloat, nextExclusiveFloat, nextExclusiveFloatEquidistant, nextExclusiveSignedDouble, nextExclusiveSignedFloat, nextExponential, nextFloat, nextFloat, nextGaussian, nextGaussian, nextGaussianFloat, nextGaussianFloat, nextInclusiveDouble, nextInclusiveDouble, nextInclusiveDouble, nextInclusiveFloat, nextInclusiveFloat, nextInclusiveFloat, 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
  • Constructor Details

    • LinnormRNG

      public LinnormRNG()
      Constructor that seeds the generator with two calls to Math.random.
    • LinnormRNG

      public LinnormRNG(long seed)
      Constructor that uses the given seed as the state without changes; all long seeds are acceptable.
      Parameters:
      seed - any long, will be used exactly
    • LinnormRNG

      public LinnormRNG(CharSequence seed)
      Constructor that hashes seed with CrossHash.hash64(CharSequence) and uses the result as the state.
      Parameters:
      seed - any CharSequence, such as a String or StringBuilder; should probably not be null (it might work?)
  • Method Details

    • getTag

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

      public int next(int bits)
      Overrides:
      next in class com.github.tommyettinger.random.EnhancedRandom
    • nextLong

      public long nextLong()
      Can return any long, positive or negative, of any size permissible in a 64-bit signed integer.
      Specified by:
      nextLong in interface RandomGenerator
      Specified by:
      nextLong in class com.github.tommyettinger.random.EnhancedRandom
      Returns:
      any long, all 64 bits are random
    • copy

      public LinnormRNG copy()
      Produces a copy of this LinnormRNG 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 LinnormRNG
    • nextInt

      public int nextInt()
      Can return any int, positive or negative, of any size permissible in a 32-bit signed integer.
      Specified by:
      nextInt in interface RandomGenerator
      Overrides:
      nextInt in class com.github.tommyettinger.random.EnhancedRandom
      Returns:
      any int, all 32 bits are random
    • nextInt

      public int nextInt(int bound)
      Exclusive on the outer bound. The inner bound is 0. The bound can be negative, which makes this produce either a negative int or 0.
      Specified by:
      nextInt in interface RandomGenerator
      Overrides:
      nextInt in class LegacyRandom
      Parameters:
      bound - the upper bound; should be positive
      Returns:
      a random int between 0 (inclusive) and bound (exclusive)
    • nextInt

      public int nextInt(int inner, int outer)
      Inclusive inner, exclusive outer.
      Specified by:
      nextInt in interface RandomGenerator
      Overrides:
      nextInt in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      inner - the inner bound, inclusive, can be positive or negative
      outer - the outer bound, exclusive, can be positive or negative, usually greater than inner
      Returns:
      a random int between inner (inclusive) and outer (exclusive)
    • nextLong

      public long nextLong(long bound)
      Exclusive on bound (which may be positive or negative), with an inner bound of 0. If bound is negative this returns a negative long; if bound is positive this returns a positive long. The bound can even be 0, which will cause this to return 0L every time. This uses a biased technique to get numbers from large ranges, but the amount of bias is incredibly small (expected to be under 1/1000 if enough random ranged numbers are requested, which is about the same as an unbiased method that was also considered). It may have noticeable bias if the LinnormRNG's period is exhausted by only calls to this method, which would take months on 2018-era consumer hardware. Unlike all unbiased methods, this advances the state by an equivalent to exactly one call to nextLong(), where rejection sampling would sometimes advance by one call, but other times by arbitrarily many more.
      Credit for this method goes to Rafael Baptista's blog for the original idea, and the JDK10 Math class' usage of Hacker's Delight code for the current algorithm. This method is drastically faster than the previous implementation when the bound varies often (roughly 4x faster, possibly more). It also always gets exactly one random long, so by default it advances the state as much as nextLong().
      Specified by:
      nextLong in interface RandomGenerator
      Overrides:
      nextLong in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      bound - the outer exclusive bound; can be positive or negative
      Returns:
      a random long between 0 (inclusive) and bound (exclusive)
    • nextLong

      public long nextLong(long lower, long upper)
      Inclusive inner, exclusive outer; lower and upper can be positive or negative and there's no requirement for one to be greater than or less than the other.
      Specified by:
      nextLong in interface RandomGenerator
      Overrides:
      nextLong in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      lower - the lower bound, inclusive, can be positive or negative
      upper - the upper bound, exclusive, can be positive or negative
      Returns:
      a random long that may be equal to lower and will otherwise be between lower and upper
    • nextDouble

      public double nextDouble()
      Gets a uniform random double in the range [0.0,1.0)
      Specified by:
      nextDouble in interface RandomGenerator
      Overrides:
      nextDouble in class LegacyRandom
      Returns:
      a random double at least equal to 0.0 and less than 1.0
    • nextDouble

      public double nextDouble(double outer)
      Gets a uniform random double in the range [0.0,outer) given a positive parameter outer. If outer is negative, it will be the (exclusive) lower bound and 0.0 will be the (inclusive) upper bound.
      Specified by:
      nextDouble in interface RandomGenerator
      Overrides:
      nextDouble in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      outer - the exclusive outer bound, can be negative
      Returns:
      a random double between 0.0 (inclusive) and outer (exclusive)
    • nextFloat

      public float nextFloat()
      Gets a uniform random float in the range [0.0,1.0)
      Specified by:
      nextFloat in interface RandomGenerator
      Overrides:
      nextFloat in class LegacyRandom
      Returns:
      a random float at least equal to 0.0 and less than 1.0
    • nextBoolean

      public boolean nextBoolean()
      Gets a random value, true or false. Calls nextLong() once.
      Specified by:
      nextBoolean in interface RandomGenerator
      Overrides:
      nextBoolean in class com.github.tommyettinger.random.EnhancedRandom
      Returns:
      a random true or false value.
    • nextBytes

      public void nextBytes(byte[] bytes)
      Given a byte array as a parameter, this will fill the array with random bytes (modifying it in-place). Calls nextLong() Math.ceil(bytes.length / 8.0) times.
      Specified by:
      nextBytes in interface RandomGenerator
      Overrides:
      nextBytes in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      bytes - a byte array that will have its contents overwritten with random bytes.
    • setState

      public void setState(long seed)
      Sets the seed (also the current state) of this generator.
      Specified by:
      setState in interface StatefulRandomness
      Overrides:
      setState in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      seed - the seed to use for this LinnormRNG, as if it was constructed with this seed.
    • getState

      public long getState()
      Gets the current state of this generator.
      Specified by:
      getState in interface StatefulRandomness
      Returns:
      the current seed of this LinnormRNG, changed once per call to nextLong()
    • 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
    • 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)
      Static randomizing method that takes its state as a parameter; state is expected to change between calls to this. It is recommended that you use LinnormRNG.determine(++state) or LinnormRNG.determine(--state) to produce a sequence of different numbers, but you can also use LinnormRNG.determine(state += 12345L) or any odd-number increment. All longs are accepted by this method, and all longs can be produced; unlike several other classes' determine() methods, passing 0 here does not return 0.
      Parameters:
      state - any long; subsequent calls should change by an odd number, such as with ++state
      Returns:
      any long
    • determineBounded

      public static int determineBounded(long state, int bound)
      Static randomizing method that takes its state as a parameter and limits output to an int between 0 (inclusive) and bound (exclusive); state is expected to change between calls to this. It is recommended that you use LinnormRNG.determineBounded(++state, bound) or LinnormRNG.determineBounded(--state, bound) to produce a sequence of different numbers, but you can also use LinnormRNG.determineBounded(state += 12345L, bound) or any odd-number increment. All longs are accepted by this method, but not all ints between 0 and bound are guaranteed to be produced with equal likelihood (for any odd-number values for bound, this isn't possible for most generators). The bound can be negative.
      Parameters:
      state - any long; subsequent calls should change by an odd number, such as with ++state
      bound - the outer exclusive bound, as an int
      Returns:
      an int between 0 (inclusive) and bound (exclusive)
    • 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 0x632BE59BD9B4E019L 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 less than 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 0x632BE59BD9B4E019L 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 less than 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