public class DharmaRNG extends RNG
This takes a RandomnessSource, defaulting to a LightRNG, 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
DOUBLE_UNIT, FLOAT_UNIT, haveNextNextGaussian, nextNextGaussian, ran, random
Constructor and 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(RandomnessSource rs)
Construct a new DharmaRNG with the given seed.
|
DharmaRNG(RandomnessSource rs,
double fairness)
Construct a new DharmaRNG with the given seed.
|
DharmaRNG(String seedString)
String-seeded constructor; uses a platform-independent hash of the String (it does not use String.hashCode) as a
seed for LightRNG, 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 LightRNG, 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 |
---|---|
Random |
asRandom() |
double |
between(double min,
double max)
Returns a value from a even distribution from min (inclusive) to max
(exclusive).
|
int |
between(int min,
int max)
Returns a value between min (inclusive) and max (exclusive).
|
int |
betweenWeighted(int min,
int max,
int samples)
Returns the average of a number of randomly selected numbers from the
provided range, with min being inclusive and max being exclusive.
|
RNG |
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.
|
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.
|
<T> T |
getRandomElement(Collection<T> coll)
Returns a random element from the provided Collection, which should have predictable iteration order if you want
predictable behavior for identical RNG seeds, though it will get a random element just fine for any Collection
(just not predictably in all cases).
|
<T> T |
getRandomElement(List<T> list)
Returns a random element from the provided list.
|
short |
getRandomElement(ShortSet set)
Returns a random element from the provided ShortSet.
|
<T> T |
getRandomElement(T[] array)
Returns a random element from the provided array and maintains object
type.
|
RandomnessSource |
getRandomness() |
<T> Iterable<T> |
getRandomStartIterable(List<T> list)
Get an Iterable that starts at a random location in list and continues on through list in its current order.
|
int |
next(int bits)
Get up to 32 bits (inclusive) of random state from the RandomnessSource.
|
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.
|
double |
nextDouble(double max)
This returns a random double between 0.0 (inclusive) and max (exclusive).
|
float |
nextFloat()
This returns a maximum of 0.99999994 because that is the largest Float
value that is less than 1.0f
|
double |
nextGaussian() |
int |
nextInt()
Returns a random integer, which may be positive or negative.
|
int |
nextInt(int bound)
Returns a random integer below the given bound, or 0 if the bound is 0 or
negative.
|
long |
nextLong()
Returns a random long, which may be positive or negative.
|
long |
nextLong(long bound)
Returns a random long below the given bound, or 0 if the bound is 0 or
negative.
|
<T> List<T> |
randomPortion(List<T> data,
int count)
Gets a random portion of a List and returns it as a new List.
|
<T> T[] |
randomPortion(T[] data,
T[] output)
Gets a random portion of data (an array), assigns that portion to output (an array) so that it fills as much as
it can, and then returns output.
|
int[] |
randomRange(int start,
int end,
int count)
Gets a random subrange of the non-negative ints from start (inclusive) to end (exclusive), using count elements.
|
<T> List<T> |
randomRotation(List<T> l)
Given a
List l, this selects a random element of l to be the first value in the returned list l2. |
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) |
<T> ArrayList<T> |
shuffle(Collection<T> elements)
Shuffles a
Collection of T using the Fisher-Yates algorithm and returns an ArrayList of T. |
<T> T[] |
shuffle(T[] elements,
T[] dest)
Shuffle an array using the "inside-out" Fisher-Yates algorithm.
|
String |
toString() |
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(String seedString)
seedString
- a String as a seedpublic DharmaRNG(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 class RNG
public double nextDouble(double max)
nextDouble
in class RNG
public double between(double min, double max)
public int between(int min, int max)
public int betweenWeighted(int min, int max, int samples)
betweenWeighted
in class RNG
min
- the minimum bound on the return value (inclusive)max
- the maximum bound on the return value (exclusive)samples
- the number of samples to takepublic <T> T getRandomElement(T[] array)
getRandomElement
in class RNG
T
- the type of the returned objectarray
- the array to get an element frompublic <T> T getRandomElement(List<T> list)
getRandomElement
in class RNG
T
- the type of the returned objectlist
- the list to get an element frompublic short getRandomElement(ShortSet set)
Requires iterating through a random amount of the elements in set, so performance depends on the size of set but is likely to be decent. This is mostly meant for internal use, the same as ShortSet.
getRandomElement
in class RNG
set
- the ShortSet to get an element frompublic <T> T getRandomElement(Collection<T> coll)
Requires iterating through a random amount of coll's elements, so performance depends on the size of coll but is
likely to be decent, as long as iteration isn't unusually slow. This replaces getRandomElement(Queue)
,
since Queue implements Collection and the older Queue-using implementation was probably less efficient.
getRandomElement
in class RNG
T
- the type of the returned objectcoll
- the Collection to get an element from; remember, Map does not implement Collectionpublic double nextGaussian()
nextGaussian
in class RNG
public int nextInt(int bound)
public int nextInt()
public long nextLong()
public long nextLong(long bound)
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
public <T> List<T> randomRotation(List<T> l)
RNG
List
l, this selects a random element of l to be the first value in the returned list l2. It
retains the order of elements in l after that random element and makes them follow the first element in l2, and
loops around to use elements from the start of l after it has placed the last element of l into l2.
randomRotation
in class RNG
T
- No restrictions on type. Changes to elements of the returned List will be reflected in the parameter.l
- A List
that will not be modified by this method. All elements of this parameter will be
shared with the returned List.l
that has been rotated so its first element has been randomly chosen
from all possible elements but order is retained. Will "loop around" to contain element 0 of l after the last
element of l, then element 1, etc.public <T> Iterable<T> getRandomStartIterable(List<T> list)
RNG
list
while you use the returned reference. And there'll be no
ConcurrentModificationException to detect such erroneous uses.getRandomStartIterable
in class RNG
list
- A list with a constant-time List.get(int)
method (otherwise performance degrades).Iterable
that iterates over list
but start at
a random index. If the chosen index is i
, the iterator
will return:
list[i]; list[i+1]; ...; list[list.length() - 1]; list[0]; list[i-1]
public <T> T[] shuffle(T[] elements, T[] dest)
RNG
shuffle
in class RNG
T
- can be any non-primitive type.elements
- an array of T; will not be modifieddest
- Where to put the shuffle. If it does not have the same length as elements
, this will use the
randomPortion method of this class to fill the smaller dest. MUST NOT be the same array as elements!dest
after modificationspublic <T> ArrayList<T> shuffle(Collection<T> elements)
RNG
Collection
of T using the Fisher-Yates algorithm and returns an ArrayList of T.public float nextFloat()
RNG
public boolean nextBoolean()
RNG
nextBoolean
in class RNG
public RandomnessSource getRandomness()
getRandomness
in class RNG
public void setRandomness(RandomnessSource random)
setRandomness
in class RNG
public RNG copy()
public <T> T[] randomPortion(T[] data, T[] output)
randomPortion
in class RNG
T
- can be any non-primitive type.data
- an array of T; will not be modified.output
- an array of T that will be overwritten; should always be instantiated with the portion lengthpublic <T> List<T> randomPortion(List<T> data, int count)
randomPortion
in class RNG
T
- can be any non-primitive typedata
- a List of T; will not be modified.count
- the non-negative number of elements to randomly take from datapublic int[] randomRange(int start, int end, int count)
randomRange
in class RNG
start
- the start of the range of numbers to potentially use (inclusive)end
- the end of the range of numbers to potentially use (exclusive)count
- the total number of elements to use; will be less if the range is smaller than countCopyright © 2012–2016. All rights reserved.