public class DharmaRNG extends RNG implements java.io.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.
RNG.CustomRandom
Constructor and Description |
---|
DharmaRNG()
Constructs a DharmaRNG with a pseudo-random seed from Math.random().
|
DharmaRNG(java.lang.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(long seed)
Construct a new DharmaRNG with the given seed.
|
DharmaRNG(long seed,
double fairness)
Construct a new DharmaRNG with the given seed.
|
DharmaRNG(RandomnessSource rs)
Construct a new DharmaRNG with the given seed.
|
DharmaRNG(RandomnessSource rs,
double fairness)
Construct a new DharmaRNG with the given seed.
|
DharmaRNG(java.lang.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.
|
Modifier and Type | Method and Description |
---|---|
long |
between(long inner,
long outer)
Returns a value between min (inclusive) and max (exclusive).
|
DharmaRNG |
copy()
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.
|
boolean |
equals(java.lang.Object o) |
double |
getFairness()
Gets the measure that this class uses for RNG fairness, defaulting to 0.54 (always between 0.0 and 1.0).
|
double |
getFortune()
Gets the status of the fortune used when calculating fairness adjustments.
|
RandomnessSource |
getRandomness() |
int |
hashCode() |
int |
next(int bits)
Get up to 32 bits (inclusive) of random output; the int this produces
will not require more than
bits bits to represent. |
boolean |
nextBoolean()
Get a random bit of state, interpreted as true or false with approximately equal likelihood.
|
double |
nextDouble()
Generate a random double, altering the result if recently generated results have been leaning
away from this class' fairness value.
|
float |
nextFloat()
Gets a random float between 0.0f inclusive and 1.0f exclusive.
|
int |
nextInt()
Returns a random integer, which may be any positive or negative value.
|
int |
nextInt(int bound)
Returns a random integer below the given bound, or 0 if the bound is 0 or
negative.
|
int |
nextIntHasty(int bound)
Returns a random non-negative integer below the given bound, or 0 if the bound is 0.
|
long |
nextLong()
Returns a random long, which may be any positive or negative value.
|
long |
nextLong(long bound)
Returns a random long below the given bound, or 0 if the bound is 0 or
negative.
|
void |
resetFortune()
Resets the stored history this RNG uses to try to ensure fairness.
|
void |
setFairness(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.
|
void |
setRandomness(RandomnessSource random) |
java.io.Serializable |
toSerializable()
Returns this DharmaRNG in a way that can be deserialized even if only
IRNG 's methods can be called. |
java.lang.String |
toString() |
approximateBits, asRandom, betweenWeighted, getRandomCellsIterable, getRandomElement, getRandomStartIterable, getRandomUniqueCells, getRandomUniqueCells, getRandomUniqueCells, maxDoubleOf, maxFloatOf, maxIntOf, maxLongOf, minDoubleOf, minFloatOf, minIntOf, minLongOf, nextBytes, nextCoord, nextCurvedFloat, nextDoubleInclusive, nextDoubleInclusive, nextFloatInclusive, nextFloatInclusive, randomInterleave, randomOrdering, randomOrdering, randomPortion, randomPortion, randomRange, randomRotation, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace
between, between, getRandomElement, getRandomElement, nextDouble, nextFloat, nextSignedInt, nextSignedLong, swap
public DharmaRNG()
public DharmaRNG(long seed)
seed
- used to seed the default RandomnessSource.public DharmaRNG(long seed, double fairness)
seed
- used to seed the default RandomnessSource.fairness
- the desired fairness metric, which must be between 0.0 and 1.0public DharmaRNG(java.lang.CharSequence seedString)
seedString
- a String as a seedpublic DharmaRNG(java.lang.String seedString, double fairness)
seedString
- a String as a seedpublic DharmaRNG(RandomnessSource rs)
rs
- the implementation used to generate random bits.public DharmaRNG(RandomnessSource rs, double fairness)
rs
- the implementation used to generate random bits.fairness
- the desired fairness metric, which must be between 0.0 and 1.0public double nextDouble()
nextDouble
in interface IRNG
nextDouble
in class RNG
public int nextInt(int bound)
nextInt
in interface IRNG
nextInt
in class AbstractRNG
bound
- the upper bound (exclusive)public int nextInt()
public long nextLong()
public long nextLong(long bound)
nextLong
in interface IRNG
nextLong
in class AbstractRNG
bound
- the upper bound (exclusive)public double getFairness()
public void setFairness(double fairness)
fairness
- the desired fairness metric, which must be 0.0 <= fairness < 1.0public double getFortune()
public void resetFortune()
public int next(int bits)
RNG
bits
bits to represent.public long between(long inner, long outer)
The inclusive and exclusive behavior is to match the behavior of the similar method that deals with floating point values.
between
in interface IRNG
between
in class AbstractRNG
inner
- the minimum bound on the return value (inclusive)outer
- the maximum bound on the return value (exclusive)public int nextIntHasty(int bound)
nextIntHasty
in class RNG
bound
- the upper bound (exclusive); behavior is undefined if bound is negativepublic float nextFloat()
RNG
public boolean nextBoolean()
RNG
rng.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 with rng.next(1)
and very good
output with rng.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.nextBoolean
in interface IRNG
nextBoolean
in class RNG
public RandomnessSource getRandomness()
getRandomness
in class RNG
public void setRandomness(RandomnessSource random)
setRandomness
in class RNG
public DharmaRNG copy()
copy
in interface RandomnessSource
copy
in class RNG
public java.io.Serializable toSerializable()
IRNG
's methods can be called.toSerializable
in interface IRNG
toSerializable
in class RNG
Serializable
view of this DharmaRNG; always this
Copyright © Eben Howard 2012–2022. All rights reserved.