public class DeckRNG extends StatefulRNG
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
DOUBLE_UNIT, FLOAT_UNIT, haveNextNextGaussian, nextNextGaussian, ran, random
Constructor and 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(RandomnessSource random)
Seeds this DeckRNG using the RandomnessSource it is given.
|
DeckRNG(String 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.
|
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).
|
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.
|
RNG |
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.
|
<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.
|
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 |
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 |
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> 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 Fisher-Yates algorithm.
|
String |
toString() |
public DeckRNG()
public DeckRNG(long seed)
seed
- used to seed the default RandomnessSource.public DeckRNG(String 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 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 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 long between(long min, long max)
public <T> T[] shuffle(T[] elements, T[] dest)
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. It MUST have the same length as elements
dest
IllegalStateException
- If dest.length != elements.length
public <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 StatefulRNG
random
- will be used to generate a new seed, but will not be assigned as this object's RandomnessSourcepublic RNG copy()
copy
in class StatefulRNG
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 countpublic long getState()
getState
in class StatefulRNG
public void setState(long state)
setState
in class StatefulRNG
state
- any long (this can tolerate states of 0)public String toString()
toString
in class StatefulRNG
public int getStep()
public void setStep(int step)
Copyright © 2012–2016. All rights reserved.