Class DiverRNG

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

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

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new generator seeded using Math.random.
    DiverRNG(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)
    Fast 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)
    Fast 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)
    Gets the current state of this generator.
    long
     
    int
    Gets the number of possible state variables that can be selected with getSelectedState(int) or setSelectedState(int, long).
     
    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.
    static long
    randomize(long state)
    High-quality static randomizing method that takes its state as a parameter; state is expected to change between calls to this.
    static int
    randomizeBounded(long state, int bound)
    High-quality 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
    randomizeDouble(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
    randomizeFloat(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.
    void
    setSeed(long seed)
    Sets the seed of this random number generator using a single long seed.
    void
    setSelectedState(int selection, long seed)
    Sets the seed (also the current state) of this generator.
    void
    setState(long state)
     
     

    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

    • DiverRNG

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

      public DiverRNG(long seed)
    • DiverRNG

      public DiverRNG(String seed)
  • Method Details

    • getTag

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

      public long getState()
      Specified by:
      getState in interface StatefulRandomness
    • setState

      public void setState(long state)
      Specified by:
      setState in interface StatefulRandomness
      Overrides:
      setState 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 DiverRNG 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
    • 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.
      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.
    • setSelectedState

      public void setSelectedState(int selection, long seed)
      Sets the seed (also the current state) of this generator.
      Overrides:
      setSelectedState in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      seed - the seed to use for this LightRNG, as if it was constructed with this seed.
    • getSelectedState

      public long getSelectedState(int selection)
      Gets the current state of this generator.
      Overrides:
      getSelectedState in class com.github.tommyettinger.random.EnhancedRandom
      Returns:
      the current seed of this LightRNG, changed once per call to nextLong()
    • setSeed

      public void setSeed(long seed)
      Sets the seed of this random number generator using a single long seed. The general contract of setSeed is that it alters the state of this random number generator object so as to be in exactly the same state as if it had just been created with the argument seed as a seed. This does not necessarily assign the state variable(s) of the implementation with the exact contents of seed, so getSelectedState(int)] should not be expected to return seed after this, though it may.
      Specified by:
      setSeed in class com.github.tommyettinger.random.EnhancedRandom
      Parameters:
      seed - the initial seed
    • getStateCount

      public int getStateCount()
      Gets the number of possible state variables that can be selected with getSelectedState(int) or setSelectedState(int, long). This returns 1.
      Overrides:
      getStateCount in class com.github.tommyettinger.random.EnhancedRandom
      Returns:
      the non-negative number of selections possible for state variables (always 1 here)
    • 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)
      Fast 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 DiverRNG.determine(++state) or DiverRNG.determine(--state) to produce a sequence of different numbers, and you may have slightly worse quality with increments or decrements other than 1. 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.
      You have a choice between determine() and randomize() in this class. determine() will behave well when the inputs are sequential, while randomize() is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with the same testing requirements Evensen used for rrxmrrxmsx_0; it will have excellent quality regardless of patterns in input but will be about 30% slower than determine(). Each method will produce all long outputs if given all possible longs as input.
      Parameters:
      state - any long; subsequent calls should change by an odd number, such as with ++state
      Returns:
      any long
    • randomize

      public static long randomize(long state)
      High-quality static randomizing method that takes its state as a parameter; state is expected to change between calls to this. It is suggested that you use DiverRNG.randomize(++state) or DiverRNG.randomize(--state) to produce a sequence of different numbers, but any increments are allowed (even-number increments won't be able to produce all outputs, but their quality will be fine for the numbers they can produce). 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.
      You have a choice between determine() and randomize() in this class. determine() will behave well when the inputs are sequential, while randomize() is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with the same testing requirements Evensen used for rrxmrrxmsx_0; it will have excellent quality regardless of patterns in input but will be about 30% slower than determine(). Each method will produce all long outputs if given all possible longs as input.
      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)
      Fast 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 DiverRNG.determineBounded(++state, bound) or DiverRNG.determineBounded(--state, bound) to produce a sequence of different numbers. 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.
      You have a choice between determine() and randomize() in this class. determine() will behave well when the inputs are sequential, while randomize() is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with the same testing requirements Evensen used for rrxmrrxmsx_0; it will have excellent quality regardless of patterns in input but will be about 30% slower than determine(). Each method will produce all long outputs if given all possible longs as input.
      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)
    • randomizeBounded

      public static int randomizeBounded(long state, int bound)
      High-quality 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 suggested that you use DiverRNG.randomizeBounded(++state) or DiverRNG.randomize(--state) to produce a sequence of different numbers, but any increments are allowed (even-number increments won't be able to produce all outputs, but their quality will be fine for the numbers they can produce). 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.
      You have a choice between determine() and randomize() in this class. determine() will behave well when the inputs are sequential, while randomize() is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with the same testing requirements Evensen used for rrxmrrxmsx_0; it will have excellent quality regardless of patterns in input but will be about 30% slower than determine(). Each method will produce all long outputs if given all possible longs as input.
      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. determineFloat(++state), where the increment for state should generally be 1. 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.
      You have a choice between determine() and randomize() in this class. determine() will behave well when the inputs are sequential, while randomize() is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with the same testing requirements Evensen used for rrxmrrxmsx_0; it will have excellent quality regardless of patterns in input but will be about 30% slower than determine(). Each method will produce all long outputs if given all possible longs as input.
      Parameters:
      state - a variable that should be different every time you want a different random result; using determineFloat(++state) is recommended to go forwards or determineFloat(--state) to generate numbers in reverse order
      Returns:
      a pseudo-random float between 0f (inclusive) and 1f (exclusive), determined by state
    • randomizeFloat

      public static float randomizeFloat(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. randomizeFloat(++state), where the increment for state can be any value and should usually be odd (even-number increments reduce the period). The period is 2 to the 64 if you increment or decrement by any odd number, but there are only 2 to the 30 possible floats between 0 and 1.
      You have a choice between determine() and randomize() in this class. determine() will behave well when the inputs are sequential, while randomize() is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with the same testing requirements Evensen used for rrxmrrxmsx_0; it will have excellent quality regardless of patterns in input but will be about 30% slower than determine(). Each method will produce all long outputs if given all possible longs as input.
      Parameters:
      state - a variable that should be different every time you want a different random result; using randomizeFloat(++state) is recommended to go forwards or randomizeFloat(--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. determineDouble(++state), where the increment for state should generally be 1. 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.
      You have a choice between determine() and randomize() in this class. determine() will behave well when the inputs are sequential, while randomize() is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with the same testing requirements Evensen used for rrxmrrxmsx_0; it will have excellent quality regardless of patterns in input but will be about 30% slower than determine(). Each method will produce all long outputs if given all possible longs as input.
      Parameters:
      state - a variable that should be different every time you want a different random result; using determineDouble(++state) is recommended to go forwards or determineDouble(--state) to generate numbers in reverse order
      Returns:
      a pseudo-random double between 0.0 (inclusive) and 1.0 (exclusive), determined by state
    • randomizeDouble

      public static double randomizeDouble(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. randomizeDouble(++state), where the increment for state can be any number but should usually be odd (even-number increments reduce the period). 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.
      You have a choice between determine() and randomize() in this class. determine() will behave well when the inputs are sequential, while randomize() is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with the same testing requirements Evensen used for rrxmrrxmsx_0; it will have excellent quality regardless of patterns in input but will be about 30% slower than determine(). Each method will produce all long outputs if given all possible longs as input.
      Parameters:
      state - a variable that should be different every time you want a different random result; using randomizeDouble(++state) is recommended to go forwards or randomizeDouble(--state) to generate numbers in reverse order
      Returns:
      a pseudo-random double between 0.0 (inclusive) and 1.0 (exclusive), determined by state