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
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface RandomGenerator
RandomGenerator.ArbitrarilyJumpableGenerator, RandomGenerator.JumpableGenerator, RandomGenerator.LeapableGenerator, RandomGenerator.SplittableGenerator, RandomGenerator.StreamableGenerator -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncopy()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 longdetermine(long state) Fast static randomizing method that takes its state as a parameter; state is expected to change between calls to this.static intdetermineBounded(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 doubledetermineDouble(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 floatdetermineFloat(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.booleanlonggetSelectedState(int selection) Gets the current state of this generator.longgetState()intGets the number of possible state variables that can be selected withgetSelectedState(int)orsetSelectedState(int, long).getTag()inthashCode()intnext(int bits) booleanGets a random value, true or false.voidnextBytes(byte[] bytes) Given a byte array as a parameter, this will fill the array with random bytes (modifying it in-place).doubleGets a uniform random double in the range [0.0,1.0)doublenextDouble(double outer) Gets a uniform random double in the range [0.0,outer) given a positive parameter outer.floatGets a uniform random float in the range [0.0,1.0)intnextInt()Can return any int, positive or negative, of any size permissible in a 32-bit signed integer.intnextInt(int bound) Exclusive on the outer bound.intnextInt(int inner, int outer) Inclusive inner, exclusive outer.longnextLong()Can return any long, positive or negative, of any size permissible in a 64-bit signed integer.longnextLong(long bound) Exclusive on bound (which may be positive or negative), with an inner bound of 0.longnextLong(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 longrandomize(long state) High-quality static randomizing method that takes its state as a parameter; state is expected to change between calls to this.static intrandomizeBounded(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 doublerandomizeDouble(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 floatrandomizeFloat(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.voidsetSeed(long seed) Sets the seed of this random number generator using a singlelongseed.voidsetSelectedState(int selection, long seed) Sets the seed (also the current state) of this generator.voidsetState(long state) toString()Methods inherited from class LegacyRandom
nextSignedInt, nextSignedLongMethods 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, writeExternalMethods inherited from class Random
doubles, doubles, doubles, doubles, from, ints, ints, ints, ints, longs, longs, longs, longsMethods 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
-
-
Method Details
-
getTag
- Specified by:
getTagin classcom.github.tommyettinger.random.EnhancedRandom
-
getState
public long getState()- Specified by:
getStatein interfaceStatefulRandomness
-
setState
public void setState(long state) - Specified by:
setStatein interfaceStatefulRandomness- Overrides:
setStatein classcom.github.tommyettinger.random.EnhancedRandom
-
next
public int next(int bits) - Overrides:
nextin classcom.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:
nextLongin interfaceRandomGenerator- Specified by:
nextLongin classcom.github.tommyettinger.random.EnhancedRandom- Returns:
- any long, all 64 bits are random
-
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:
copyin classcom.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:
nextIntin interfaceRandomGenerator- Overrides:
nextIntin classcom.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:
nextIntin interfaceRandomGenerator- Overrides:
nextIntin classLegacyRandom- 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:
nextIntin interfaceRandomGenerator- Overrides:
nextIntin classcom.github.tommyettinger.random.EnhancedRandom- Parameters:
inner- the inner bound, inclusive, can be positive or negativeouter- 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 asnextLong().- Specified by:
nextLongin interfaceRandomGenerator- Overrides:
nextLongin classcom.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:
nextLongin interfaceRandomGenerator- Overrides:
nextLongin classcom.github.tommyettinger.random.EnhancedRandom- Parameters:
lower- the lower bound, inclusive, can be positive or negativeupper- 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:
nextDoublein interfaceRandomGenerator- Overrides:
nextDoublein classLegacyRandom- 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:
nextDoublein interfaceRandomGenerator- Overrides:
nextDoublein classcom.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:
nextFloatin interfaceRandomGenerator- Overrides:
nextFloatin classLegacyRandom- 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:
nextBooleanin interfaceRandomGenerator- Overrides:
nextBooleanin classcom.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:
nextBytesin interfaceRandomGenerator- Overrides:
nextBytesin classcom.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:
setSelectedStatein classcom.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:
getSelectedStatein classcom.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 singlelongseed. The general contract ofsetSeedis 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 argumentseedas a seed. This does not necessarily assign the state variable(s) of the implementation with the exact contents of seed, sogetSelectedState(int)] should not be expected to returnseedafter this, though it may.- Specified by:
setSeedin classcom.github.tommyettinger.random.EnhancedRandom- Parameters:
seed- the initial seed
-
getStateCount
public int getStateCount()Gets the number of possible state variables that can be selected withgetSelectedState(int)orsetSelectedState(int, long). This returns 1.- Overrides:
getStateCountin classcom.github.tommyettinger.random.EnhancedRandom- Returns:
- the non-negative number of selections possible for state variables (always 1 here)
-
toString
-
equals
-
hashCode
-
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 useDiverRNG.determine(++state)orDiverRNG.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, whilerandomize()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 thandetermine(). 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 useDiverRNG.randomize(++state)orDiverRNG.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, whilerandomize()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 thandetermine(). 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 useDiverRNG.determineBounded(++state, bound)orDiverRNG.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, whilerandomize()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 thandetermine(). 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++statebound- 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 useDiverRNG.randomizeBounded(++state)orDiverRNG.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, whilerandomize()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 thandetermine(). 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++statebound- 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, whilerandomize()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 thandetermine(). 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; usingdetermineFloat(++state)is recommended to go forwards ordetermineFloat(--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, whilerandomize()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 thandetermine(). 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; usingrandomizeFloat(++state)is recommended to go forwards orrandomizeFloat(--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, whilerandomize()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 thandetermine(). 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; usingdetermineDouble(++state)is recommended to go forwards ordetermineDouble(--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, whilerandomize()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 thandetermine(). 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; usingrandomizeDouble(++state)is recommended to go forwards orrandomizeDouble(--state)to generate numbers in reverse order- Returns:
- a pseudo-random double between 0.0 (inclusive) and 1.0 (exclusive), determined by
state
-