public class RNG extends Object implements Serializable
Modifier and Type | Class and Description |
---|---|
static class |
RNG.CustomRandom
A subclass of java.util.Random that uses a RandomnessSource supplied by the user instead of the default.
|
Modifier and Type | Field and Description |
---|---|
protected static double |
DOUBLE_UNIT |
protected static float |
FLOAT_UNIT |
protected boolean |
haveNextNextGaussian |
protected double |
nextNextGaussian |
protected Random |
ran |
protected RandomnessSource |
random |
Constructor and Description |
---|
RNG()
Default constructor; uses SplitMix64, 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.
|
RNG(long seed)
Seeded constructor; uses 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.
|
RNG(RandomnessSource random)
Uses the provided source of randomness for all calculations.
|
RNG(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.
|
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 RNG; it will generate the same random numbers, given the same calls in order, as this RNG
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.
|
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()
This returns a maximum of 0.9999999999999999 because that is the largest
Double value that is less than 1.0
|
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()
Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive).
|
int |
nextInt(int bound)
Returns a random integer below the given bound, or 0 if the bound is 0 or
negative.
|
long |
nextLong()
Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive).
|
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) |
<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)
Shuffle an array using the Fisher-Yates algorithm.
|
<T> T[] |
shuffle(T[] elements,
T[] dest)
Shuffle an array using the "inside-out" Fisher-Yates algorithm.
|
String |
toString() |
protected static final double DOUBLE_UNIT
protected static final float FLOAT_UNIT
protected RandomnessSource random
protected double nextNextGaussian
protected boolean haveNextNextGaussian
public RNG()
public RNG(long seed)
public RNG(String seedString)
public RNG(RandomnessSource random)
random
- the source of pseudo-randomness, such as a MersenneTwister or SobolQRNG objectpublic Random asRandom()
public double between(double min, double max)
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)
min
- the minimum bound on the return value (inclusive)max
- the maximum bound on the return value (exclusive)public long between(long min, long max)
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)
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)
T
- the type of the returned objectarray
- the array to get an element frompublic <T> T getRandomElement(List<T> list)
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.
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.
T
- the type of the returned objectcoll
- the Collection to get an element from; remember, Map does not implement Collectionpublic <T> List<T> randomRotation(List<T> l)
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.
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)
list
while you use the returned reference. And there'll be no
ConcurrentModificationException to detect such erroneous uses.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
- can be any non-primitive type.elements
- an array of T; will not be modifiedpublic <T> T[] shuffle(T[] elements, T[] dest)
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)
Collection
of T using the Fisher-Yates algorithm and returns an ArrayList of T.T
- can be any non-primitive type.elements
- a Collection of T; will not be modifiedpublic <T> T[] randomPortion(T[] data, T[] output)
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)
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)
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 double nextGaussian()
public double nextDouble()
public double nextDouble(double max)
public float nextFloat()
public boolean nextBoolean()
public long nextLong()
public long nextLong(long bound)
bound
- the upper bound (exclusive)public int nextInt(int bound)
bound
- the upper bound (exclusive)public int nextInt()
public int next(int bits)
bits
- 1 to 32public RandomnessSource getRandomness()
public void setRandomness(RandomnessSource random)
public RNG copy()
Copyright © 2012–2016. All rights reserved.