Class DharmaRNG
- All Implemented Interfaces:
- Serializable,- IRNG,- RandomnessSource
public class DharmaRNG extends RNG implements Serializable
This takes a RandomnessSource, defaulting to a DiverRNG, and uses it to generate random values, but tracks the total and compares it to the potential total of a generator of only numbers with a desired value (default 0.54, so it compares against a sequence of all 0.54). If the current generated total is too high or low compared to the desired total, the currently used seed is possibly changed, the generated number is moved in the direction of the desired fairness, and it returns that instead of the number that would have pushed the current generated total beyond the desired threshold. The new number, if one is changed, will always be closer to the desired fairness. This is absolutely insecure for cryptographic purposes, but should seem more "fair" to a player than a random number generator that seeks to be truly random. You can create multiple DharmaRNG objects with different fairness values and seeds, and use favorable generators (with fairness greater than 0.54) for characters that need an easier time, or unfavorable generators if you want the characters that use that RNG to be impeded somewhat. The name comes from the Wheel of Dharma. This class currently will have a slight bias toward lower numbers with many RNGs unless fairness is tweaked; 0.54 can be used as a stand-in because 0.5 leans too low.
 You can get values from this generator with: nextDouble(), nextInt(),
   nextLong(), and the bounded variants on each of those.
 
 You can alter the tracking information or requested fairness with resetFortune(),
   setFairness(double), and getFairness().
 Created by Tommy Ettinger on 5/2/2015.
- See Also:
- Serialized Form
- 
Nested Class Summary
- 
Field Summary
- 
Constructor SummaryConstructors Constructor Description DharmaRNG()Constructs a DharmaRNG with a pseudo-random seed from Math.random().DharmaRNG(long seed)Construct a new DharmaRNG with the given seed.DharmaRNG(long seed, double fairness)Construct a new DharmaRNG with the given seed.DharmaRNG(CharSequence seedString)String-seeded constructor; uses a platform-independent hash of the String (it does not use String.hashCode) as a seed for DiverRNG, which is of high quality, but low period (which rarely matters for games), and has good speed, tiny state size, and excellent 64-bit number generation.DharmaRNG(String seedString, double fairness)String-seeded constructor; uses a platform-independent hash of the String (it does not use String.hashCode) as a seed for DiverRNG, which is of high quality, but low period (which rarely matters for games), and has good speed, tiny state size, and excellent 64-bit number generation.DharmaRNG(RandomnessSource rs)Construct a new DharmaRNG with the given seed.DharmaRNG(RandomnessSource rs, double fairness)Construct a new DharmaRNG with the given seed.
- 
Method SummaryModifier and Type Method Description longbetween(long min, long max)Returns a value between min (inclusive) and max (exclusive).DharmaRNGcopy()Creates a copy of this DharmaRNG; it will generate the same random numbers, given the same calls in order, as this DharmaRNG at the point copy() is called.booleanequals(Object o)doublegetFairness()Gets the measure that this class uses for RNG fairness, defaulting to 0.54 (always between 0.0 and 1.0).doublegetFortune()Gets the status of the fortune used when calculating fairness adjustments.RandomnessSourcegetRandomness()inthashCode()intnext(int bits)Get up to 32 bits (inclusive) of random output; the int this produces will not require more thanbitsbits to represent.booleannextBoolean()Get a random bit of state, interpreted as true or false with approximately equal likelihood.doublenextDouble()Generate a random double, altering the result if recently generated results have been leaning away from this class' fairness value.floatnextFloat()Gets a random float between 0.0f inclusive and 1.0f exclusive.intnextInt()Returns a random integer, which may be any positive or negative value.intnextInt(int bound)Returns a random integer below the given bound, or 0 if the bound is 0 or negative.intnextIntHasty(int bound)Returns a random non-negative integer below the given bound, or 0 if the bound is 0.longnextLong()Returns a random long, which may be any positive or negative value.longnextLong(long bound)Returns a random long below the given bound, or 0 if the bound is 0 or negative.voidresetFortune()Resets the stored history this RNG uses to try to ensure fairness.voidsetFairness(double fairness)Sets the measure that this class uses for RNG fairness, which must always be between 0.0 and 1.0, and will be set to 0.54 if an invalid value is passed.voidsetRandomness(RandomnessSource random)SerializabletoSerializable()Returns this DharmaRNG in a way that can be deserialized even if onlyIRNG's methods can be called.StringtoString()Methods inherited from class squidpony.squidmath.RNGapproximateBits, asRandom, between, between, betweenWeighted, getRandomCellsIterable, getRandomElement, getRandomElement, getRandomElement, getRandomStartIterable, getRandomUniqueCells, getRandomUniqueCells, getRandomUniqueCells, maxDoubleOf, maxFloatOf, maxIntOf, maxLongOf, minDoubleOf, minFloatOf, minIntOf, minLongOf, nextBytes, nextCoord, nextCurvedFloat, nextDouble, nextDoubleInclusive, nextDoubleInclusive, nextFloat, nextFloatInclusive, nextFloatInclusive, nextSignedInt, nextSignedLong, randomInterleave, randomOrdering, randomOrdering, randomPortion, randomPortion, randomRange, randomRotation, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace
- 
Constructor Details- 
DharmaRNGpublic DharmaRNG()Constructs a DharmaRNG with a pseudo-random seed from Math.random().
- 
DharmaRNGConstruct a new DharmaRNG with the given seed.- Parameters:
- seed- used to seed the default RandomnessSource.
 
- 
DharmaRNGConstruct a new DharmaRNG with the given seed.- Parameters:
- seed- used to seed the default RandomnessSource.
- fairness- the desired fairness metric, which must be between 0.0 and 1.0
 
- 
DharmaRNGString-seeded constructor; uses a platform-independent hash of the String (it does not use String.hashCode) as a seed for DiverRNG, which is of high quality, but low period (which rarely matters for games), and has good speed, tiny state size, and excellent 64-bit number generation.- Parameters:
- seedString- a String as a seed
 
- 
DharmaRNGString-seeded constructor; uses a platform-independent hash of the String (it does not use String.hashCode) as a seed for DiverRNG, which is of high quality, but low period (which rarely matters for games), and has good speed, tiny state size, and excellent 64-bit number generation.- Parameters:
- seedString- a String as a seed
 
- 
DharmaRNGConstruct a new DharmaRNG with the given seed.- Parameters:
- rs- the implementation used to generate random bits.
 
- 
DharmaRNGConstruct a new DharmaRNG with the given seed.- Parameters:
- rs- the implementation used to generate random bits.
- fairness- the desired fairness metric, which must be between 0.0 and 1.0
 
 
- 
- 
Method Details- 
nextDoubleGenerate a random double, altering the result if recently generated results have been leaning away from this class' fairness value.- Specified by:
- nextDoublein interface- IRNG
- Overrides:
- nextDoublein class- RNG
- Returns:
- a double between 0.0 (inclusive) and 1.0 (exclusive)
 
- 
nextIntReturns a random integer below the given bound, or 0 if the bound is 0 or negative. Affects the current fortune.
- 
nextIntReturns a random integer, which may be any positive or negative value. Affects the current fortune.
- 
nextLongReturns a random long, which may be any positive or negative value. Affects the current fortune.
- 
nextLongReturns a random long below the given bound, or 0 if the bound is 0 or negative.
- 
getFairnessGets the measure that this class uses for RNG fairness, defaulting to 0.54 (always between 0.0 and 1.0).- Returns:
- the current fairness metric.
 
- 
setFairnessSets the measure that this class uses for RNG fairness, which must always be between 0.0 and 1.0, and will be set to 0.54 if an invalid value is passed.- Parameters:
- fairness- the desired fairness metric, which must be 0.0 <= fairness < 1.0
 
- 
getFortuneGets the status of the fortune used when calculating fairness adjustments.- Returns:
- the current value used to determine whether the results should be adjusted toward fairness.
 
- 
resetFortuneResets the stored history this RNG uses to try to ensure fairness.
- 
nextDescription copied from class:RNGGet up to 32 bits (inclusive) of random output; the int this produces will not require more thanbitsbits to represent.
- 
betweenReturns a value between min (inclusive) and max (exclusive).The inclusive and exclusive behavior is to match the behavior of the similar method that deals with floating point values. 
- 
nextIntHastyReturns a random non-negative integer below the given bound, or 0 if the bound is 0. Uses a slightly optimized technique. This method is considered "hasty" since it should be faster than nextInt() doesn't check for "less-valid" bounds values. It also has undefined behavior if bound is negative, though it will probably produce a negative number (just how negative is an open question).- Overrides:
- nextIntHastyin class- RNG
- Parameters:
- bound- the upper bound (exclusive); behavior is undefined if bound is negative
- Returns:
- the found number
 
- 
nextFloatDescription copied from class:RNGGets 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 .
- 
nextBooleanDescription copied from class:RNGGet a random bit of state, interpreted as true or false with approximately equal likelihood. This may have better behavior thanrng.next(1), depending on the RandomnessSource implementation; the default DiverRNG will behave fine, as will LightRNG and ThrustAltRNG (these all use similar algorithms), but the normally-high-quality XoRoRNG will produce very predictable output withrng.next(1)and very good output withrng.nextBoolean(). This is a known and considered flaw of Xoroshiro128+, the algorithm used by XoRoRNG, and a large number of generators have lower quality on the least-significant bit than the most- significant bit, where this method only checks the most-significant bit.- Specified by:
- nextBooleanin interface- IRNG
- Overrides:
- nextBooleanin class- RNG
- Returns:
- a random boolean.
 
- 
getRandomness- Overrides:
- getRandomnessin class- RNG
 
- 
setRandomness- Overrides:
- setRandomnessin class- RNG
 
- 
copyCreates a copy of this DharmaRNG; it will generate the same random numbers, given the same calls in order, as this DharmaRNG at the point copy() is called. The copy will not share references with this DharmaRNG.
- 
toString
- 
equals
- 
hashCode
- 
toSerializableReturns this DharmaRNG in a way that can be deserialized even if onlyIRNG's methods can be called.- Specified by:
- toSerializablein interface- IRNG
- Overrides:
- toSerializablein class- RNG
- Returns:
- a Serializableview of this DharmaRNG; alwaysthis
 
 
-