Package squidpony.squidmath
Class DeckRNG
java.lang.Object
squidpony.squidmath.RNG
squidpony.squidmath.StatefulRNG
squidpony.squidmath.DeckRNG
- All Implemented Interfaces:
Serializable
,IRNG
,IStatefulRNG
,RandomnessSource
,StatefulRandomness
public class DeckRNG extends StatefulRNG implements Serializable
An RNG variant that has 16 possible grades of value it can produce and shuffles them like a deck of cards.
It repeats grades of value, but not exact values, every 16 numbers requested from it. Grades go in increments of
0.0625 from 0.0 to 0.9375, and are added to a random double less than 0.0625 to get the random number for that
grade.
You can get values from this generator with: nextDouble()
, nextInt()
,
nextLong()
, and the bounded variants on each of those.
Created by Tommy Ettinger on 5/2/2015.
- See Also:
- Serialized Form
-
Nested Class Summary
-
Field Summary
-
Constructor Summary
Constructors Constructor Description DeckRNG()
Constructs a DeckRNG with a pseudo-random seed from Math.random().DeckRNG(long seed)
Construct a new DeckRNG with the given seed.DeckRNG(CharSequence seedString)
String-seeded constructor uses the hash of the String as a seed for LightRNG, which is of high quality, but low period (which rarely matters for games), and has good speed and tiny state size.DeckRNG(RandomnessSource random)
Seeds this DeckRNG using the RandomnessSource it is given. -
Method Summary
Modifier and Type Method 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).long
between(long min, long 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.DeckRNG
copy()
Creates a copy of this DeckRNG; it will generate the same random numbers, given the same calls in order, as this DeckRNG at the point copy() is called.boolean
equals(Object o)
<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.<T> T
getRandomElement(T[] array)
Returns a random element from the provided array and maintains object type.RandomnessSource
getRandomness()
long
getState()
Get a long that can be used to reproduce the sequence of random numbers this object will generate starting now.int
getStep()
int
hashCode()
int
next(int bits)
Get up to 32 bits (inclusive) of random output; the int this produces will not require more thanbits
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.double
nextDouble(double max)
This returns a random double between 0.0 (inclusive) and max (exclusive).float
nextFloat()
Gets a random float between 0.0f inclusive and 1.0f exclusive.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.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 positive or negative.long
nextLong(long bound)
Returns a random long below the given bound, or 0 if the bound is 0 or negative.int[]
randomOrdering(int length)
Generates a random permutation of the range from 0 (inclusive) to length (exclusive).void
setRandomness(RandomnessSource random)
Reseeds this DeckRNG using the RandomnessSource it is given.void
setState(long state)
Sets the state of the random number generator to a given long, which will alter future random numbers this produces based on the state.void
setStep(int step)
<T> T[]
shuffle(T[] elements)
Shuffle an array using the Fisher-Yates algorithm.Serializable
toSerializable()
Returns this DeckRNG in a way that can be deserialized even if onlyIRNG
's methods can be called.String
toString()
Methods inherited from class squidpony.squidmath.RNG
approximateBits, getRandomCellsIterable, getRandomStartIterable, getRandomUniqueCells, getRandomUniqueCells, getRandomUniqueCells, maxDoubleOf, maxFloatOf, maxIntOf, maxLongOf, minDoubleOf, minFloatOf, minIntOf, minLongOf, nextBytes, nextCoord, nextCurvedFloat, nextDoubleInclusive, nextDoubleInclusive, nextFloat, nextFloatInclusive, nextFloatInclusive, nextSignedInt, nextSignedLong, randomInterleave, randomOrdering, randomPortion, randomPortion, randomRange, randomRotation, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface squidpony.squidmath.IRNG
nextFloat, nextSignedInt, nextSignedLong, randomOrdering, randomPortion, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace
-
Constructor Details
-
DeckRNG
public DeckRNG()Constructs a DeckRNG with a pseudo-random seed from Math.random(). -
DeckRNG
Construct a new DeckRNG with the given seed.- Parameters:
seed
- used to seed the default RandomnessSource.
-
DeckRNG
String-seeded constructor uses the hash of the String as a seed for LightRNG, which is of high quality, but low period (which rarely matters for games), and has good speed and tiny state size.- Parameters:
seedString
- a String to use as a seed; will be hashed in a uniform way across platforms.
-
DeckRNG
Seeds this DeckRNG using the RandomnessSource it is given. Does not assign the RandomnessSource to any fields that would affect future pseudo-random number generation.- Parameters:
random
- will be used to generate a new seed, but will not be assigned as this object's RandomnessSource
-
-
Method Details
-
nextDouble
Generate a random double, altering the result if recently generated results have been leaning away from this class' fairness value.- Specified by:
nextDouble
in interfaceIRNG
- Overrides:
nextDouble
in classRNG
- Returns:
- a double between 0.0 (inclusive) and 1.0 (exclusive)
-
nextDouble
This returns a random double between 0.0 (inclusive) and max (exclusive).- Specified by:
nextDouble
in interfaceIRNG
- Overrides:
nextDouble
in classRNG
- Parameters:
max
- the outer exclusive bound as a double; can be negative or positive- Returns:
- a value between 0 (inclusive) and max (exclusive)
-
between
Returns a value from a even distribution from min (inclusive) to max (exclusive). -
between
Returns 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. -
betweenWeighted
Returns the average of a number of randomly selected numbers from the provided range, with min being inclusive and max being exclusive. It will sample the number of times passed in as the third parameter. The inclusive and exclusive behavior is to match the behavior of the similar method that deals with floating point values. This can be used to weight RNG calls to the average between min and max.- Overrides:
betweenWeighted
in classRNG
- Parameters:
min
- the minimum bound on the return value (inclusive)max
- the maximum bound on the return value (exclusive)samples
- the number of samples to take- Returns:
- the found value
-
getRandomElement
Returns a random element from the provided array and maintains object type.- Specified by:
getRandomElement
in interfaceIRNG
- Overrides:
getRandomElement
in classRNG
- Type Parameters:
T
- the type of the returned object- Parameters:
array
- the array to get an element from- Returns:
- the randomly selected element
-
getRandomElement
Returns a random element from the provided list. If the list is empty then null is returned.- Specified by:
getRandomElement
in interfaceIRNG
- Overrides:
getRandomElement
in classRNG
- Type Parameters:
T
- the type of the returned object- Parameters:
list
- the list to get an element from- Returns:
- the randomly selected element
-
getRandomElement
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). If you give this a Set, it should be a LinkedHashSet or some form of sorted Set like TreeSet if you want predictable results. Any List or Queue should be fine. Map does not implement Collection, thank you very much Java library designers, so you can't actually pass a Map to this, though you can pass the keys or values. If coll is empty, returns null.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.- Specified by:
getRandomElement
in interfaceIRNG
- Overrides:
getRandomElement
in classRNG
- Type Parameters:
T
- the type of the returned object- Parameters:
coll
- the Collection to get an element from; remember, Map does not implement Collection- Returns:
- the randomly selected element
-
nextInt
Returns a random integer below the given bound, or 0 if the bound is 0 or negative. Affects the current fortune. -
shuffle
Shuffle an array using the Fisher-Yates algorithm. Not GWT-compatible; use the overload that takes two arrays.
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle -
randomOrdering
Generates a random permutation of the range from 0 (inclusive) to length (exclusive). Useful for passing to OrderedMap or OrderedSet's reorder() methods.- Specified by:
randomOrdering
in interfaceIRNG
- Overrides:
randomOrdering
in classRNG
- Parameters:
length
- the size of the ordering to produce- Returns:
- a random ordering containing all ints from 0 to length (exclusive)
-
nextIntHasty
Returns 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:
nextIntHasty
in classRNG
- Parameters:
bound
- the upper bound (exclusive); behavior is undefined if bound is negative- Returns:
- the found number
-
nextInt
Returns a random integer, which may be positive or negative. -
nextLong
Returns a random long, which may be positive or negative. -
nextLong
Returns a random long below the given bound, or 0 if the bound is 0 or negative. -
next
Description copied from class:RNG
Get up to 32 bits (inclusive) of random output; the int this produces will not require more thanbits
bits to represent. -
asRandom
-
between
Returns 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. -
nextFloat
Description copied from class:RNG
Gets 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 . -
nextBoolean
Description copied from class:RNG
Get 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:
nextBoolean
in interfaceIRNG
- Overrides:
nextBoolean
in classRNG
- Returns:
- a random boolean.
-
getRandomness
- Overrides:
getRandomness
in classRNG
-
setRandomness
Reseeds this DeckRNG using the RandomnessSource it is given. Does not assign the RandomnessSource to any fields that would affect future pseudo-random number generation.- Overrides:
setRandomness
in classStatefulRNG
- Parameters:
random
- will be used to generate a new seed, but will not be assigned as this object's RandomnessSource
-
copy
Creates a copy of this DeckRNG; it will generate the same random numbers, given the same calls in order, as this DeckRNG at the point copy() is called. The copy will not share references with this DeckRNG.- Specified by:
copy
in interfaceIRNG
- Specified by:
copy
in interfaceRandomnessSource
- Specified by:
copy
in interfaceStatefulRandomness
- Overrides:
copy
in classStatefulRNG
- Returns:
- a copy of this DeckRNG
-
getState
Get a long that can be used to reproduce the sequence of random numbers this object will generate starting now.- Specified by:
getState
in interfaceStatefulRandomness
- Overrides:
getState
in classStatefulRNG
- Returns:
- a long that can be used as state.
-
setState
Sets the state of the random number generator to a given long, which will alter future random numbers this produces based on the state. Setting the state always causes the "deck" of random grades to be shuffled.- Specified by:
setState
in interfaceStatefulRandomness
- Overrides:
setState
in classStatefulRNG
- Parameters:
state
- any long (this can tolerate states of 0)
-
toString
- Overrides:
toString
in classStatefulRNG
-
equals
-
hashCode
-
getStep
-
setStep
-
toSerializable
Returns this DeckRNG in a way that can be deserialized even if onlyIRNG
's methods can be called.- Specified by:
toSerializable
in interfaceIRNG
- Overrides:
toSerializable
in classStatefulRNG
- Returns:
- a
Serializable
view of this DeckRNG; alwaysthis
-