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
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface RandomGenerator
RandomGenerator.ArbitrarilyJumpableGenerator, RandomGenerator.JumpableGenerator, RandomGenerator.LeapableGenerator, RandomGenerator.SplittableGenerator, RandomGenerator.StreamableGenerator -
Constructor Summary
ConstructorsConstructorDescriptionConstructor 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.LinnormRNG(CharSequence seed) Constructor that hashes seed withCrossHash.hash64(CharSequence)and uses the result as the state. -
Method Summary
Modifier and TypeMethodDescriptioncopy()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 longdetermine(long state) 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) 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) longgetState()Gets the current state of this generator.intgetTag()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.voidsetSeed(long seed) voidsetSelectedState(int selection, long value) voidsetState(long seed) Sets the seed (also the current state) of this generator.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
-
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
Constructor that hashes seed withCrossHash.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
- Specified by:
getTagin 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 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:
copyin classcom.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:
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. 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 tonextLong(), 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 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.
-
setState
public void setState(long seed) Sets the seed (also the current state) of this generator.- Specified by:
setStatein interfaceStatefulRandomness- Overrides:
setStatein classcom.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:
getStatein interfaceStatefulRandomness- Returns:
- the current seed of this LinnormRNG, changed once per call to nextLong()
-
setSeed
public void setSeed(long seed) - Specified by:
setSeedin classcom.github.tommyettinger.random.EnhancedRandom
-
getStateCount
public int getStateCount()- Overrides:
getStateCountin classcom.github.tommyettinger.random.EnhancedRandom
-
getSelectedState
public long getSelectedState(int selection) - Overrides:
getSelectedStatein classcom.github.tommyettinger.random.EnhancedRandom
-
setSelectedState
public void setSelectedState(int selection, long value) - Overrides:
setSelectedStatein classcom.github.tommyettinger.random.EnhancedRandom
-
toString
-
equals
-
hashCode
-
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 useLinnormRNG.determine(++state)orLinnormRNG.determine(--state)to produce a sequence of different numbers, but you can also useLinnormRNG.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 useLinnormRNG.determineBounded(++state, bound)orLinnormRNG.determineBounded(--state, bound)to produce a sequence of different numbers, but you can also useLinnormRNG.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++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.determine(++state), where the increment for state should be odd but otherwise doesn't really matter. This multiplies state by0x632BE59BD9B4E019Lwithin 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; usingdetermine(++state)is recommended to go forwards ordetermine(--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 by0x632BE59BD9B4E019Lwithin 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; usingdetermine(++state)is recommended to go forwards ordetermine(--state)to generate numbers in reverse order- Returns:
- a pseudo-random double between 0.0 (inclusive) and 1.0 (exclusive), determined by
state
-