public class DeckRNG extends StatefulRNG implements java.io.Serializable
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.
RNG.CustomRandom
Constructor and Description |
---|
DeckRNG()
Constructs a DeckRNG with a pseudo-random seed from Math.random().
|
DeckRNG(java.lang.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(long seed)
Construct a new DeckRNG with the given seed.
|
DeckRNG(RandomnessSource random)
Seeds this DeckRNG using the RandomnessSource it is given.
|
Modifier and Type | Method and Description |
---|---|
java.util.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 inner,
long outer)
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(java.lang.Object o) |
<T> T |
getRandomElement(java.util.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(java.util.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 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.
|
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.
|
java.io.Serializable |
toSerializable()
Returns this DeckRNG in a way that can be deserialized even if only
IRNG 's methods can be called. |
java.lang.String |
toString() |
approximateBits, getRandomCellsIterable, getRandomStartIterable, getRandomUniqueCells, getRandomUniqueCells, getRandomUniqueCells, maxDoubleOf, maxFloatOf, maxIntOf, maxLongOf, minDoubleOf, minFloatOf, minIntOf, minLongOf, nextBytes, nextCoord, nextCurvedFloat, nextDoubleInclusive, nextDoubleInclusive, nextFloatInclusive, nextFloatInclusive, randomInterleave, randomOrdering, randomPortion, randomPortion, randomRange, randomRotation, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace
nextFloat, nextSignedInt, nextSignedLong, swap
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
nextFloat, nextSignedInt, nextSignedLong, randomOrdering, randomPortion, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace
public DeckRNG()
public DeckRNG(long seed)
seed
- used to seed the default RandomnessSource.public DeckRNG(java.lang.CharSequence seedString)
seedString
- a String to use as a seed; will be hashed in a uniform way across platforms.public DeckRNG(RandomnessSource random)
random
- will be used to generate a new seed, but will not be assigned as this object's RandomnessSourcepublic double nextDouble()
nextDouble
in interface IRNG
nextDouble
in class RNG
public double nextDouble(double max)
nextDouble
in interface IRNG
nextDouble
in class AbstractRNG
max
- the outer exclusive bound as a double; can be negative or positivepublic double between(double min, double max)
between
in interface IRNG
between
in class AbstractRNG
min
- the minimum bound on the return value (inclusive)max
- the maximum bound on the return value (exclusive)public int between(int min, int max)
between
in interface IRNG
between
in class AbstractRNG
min
- the minimum bound on the return value (inclusive)max
- the maximum bound on the return value (exclusive)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 interface IRNG
getRandomElement
in class RNG
T
- the type of the returned objectarray
- the array to get an element frompublic <T> T getRandomElement(java.util.List<T> list)
getRandomElement
in interface IRNG
getRandomElement
in class AbstractRNG
T
- the type of the returned objectlist
- the list to get an element frompublic <T> T getRandomElement(java.util.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 interface IRNG
getRandomElement
in class AbstractRNG
T
- the type of the returned objectcoll
- the Collection to get an element from; remember, Map does not implement Collectionpublic int nextInt(int bound)
nextInt
in interface IRNG
nextInt
in class AbstractRNG
bound
- the upper bound (exclusive)public <T> T[] shuffle(T[] elements)
public int[] randomOrdering(int length)
randomOrdering
in interface IRNG
randomOrdering
in class RNG
length
- the size of the ordering to producepublic int nextIntHasty(int bound)
nextIntHasty
in class RNG
bound
- the upper bound (exclusive); behavior is undefined if bound is negativepublic int nextInt()
public long nextLong()
public long nextLong(long bound)
nextLong
in interface IRNG
nextLong
in class AbstractRNG
bound
- the upper bound (exclusive)public int next(int bits)
RNG
bits
bits to represent.public java.util.Random asRandom()
public long between(long inner, long outer)
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 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 StatefulRNG
random
- will be used to generate a new seed, but will not be assigned as this object's RandomnessSourcepublic DeckRNG copy()
copy
in interface RandomnessSource
copy
in interface StatefulRandomness
copy
in class StatefulRNG
public long getState()
getState
in interface StatefulRandomness
getState
in class StatefulRNG
public void setState(long state)
setState
in interface StatefulRandomness
setState
in class StatefulRNG
state
- any long (this can tolerate states of 0)public java.lang.String toString()
toString
in class StatefulRNG
public int getStep()
public void setStep(int step)
public java.io.Serializable toSerializable()
IRNG
's methods can be called.toSerializable
in interface IRNG
toSerializable
in class StatefulRNG
Serializable
view of this DeckRNG; always this
Copyright © Eben Howard 2012–2022. All rights reserved.