public interface IRNG extends RandomnessSource
RandomnessSource
). It's a stripped down version of the original RNG
. It's an interface instead of a
class, to be able to implement using random number generators that don't implement RandomnessSource, like libGDX's
RandomXS128, or to hard-code the RandomnessSource to avoid overhead or use some methods differently (like preferring
32-bit math or optimizing for GWT, as GWTRNG
does). You can use any IRNG as a RandomnessSource, but that only
allows its next(int)
, nextLong()
, and RandomnessSource.copy()
methods to be called, so most usage that can
benefit from methods like nextDouble()
or between(int, int)
should prefer IRNG for parameter types.Modifier and Type | Method and Description |
---|---|
double |
between(double min,
double max)
Returns a value from a uniform distribution from min (inclusive) to max
(exclusive).
|
int |
between(int min,
int max)
Returns a value between min (inclusive) and max (exclusive) as ints.
|
long |
between(long min,
long max)
Returns a value between min (inclusive) and max (exclusive) as longs.
|
<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.
|
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()
Gets a random double between 0.0 inclusive and 1.0 exclusive.
|
double |
nextDouble(double outer)
This returns a random double between 0.0 (inclusive) and outer (exclusive).
|
float |
nextFloat()
Gets a random float between 0.0f inclusive and 1.0f exclusive.
|
float |
nextFloat(float outer)
This returns a random float between 0.0f (inclusive) and outer (exclusive).
|
int |
nextInt()
Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive).
|
int |
nextInt(int bound)
Returns a random non-negative 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.
|
int |
nextSignedInt(int outerBound)
Returns a random non-negative integer between 0 (inclusive) and the given bound (exclusive),
or 0 if the bound is 0.
|
long |
nextSignedLong(long outerBound)
Exclusive on bound (which may be positive or negative), with an inner bound of 0.
|
int[] |
randomOrdering(int length)
Generates a random permutation of the range from 0 (inclusive) to length (exclusive).
|
int[] |
randomOrdering(int length,
int[] dest)
Generates a random permutation of the range from 0 (inclusive) to length (exclusive) and stores it in
the dest parameter, avoiding allocations.
|
<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.
|
<T> java.util.ArrayList<T> |
shuffle(java.util.Collection<T> elements)
Shuffles a
Collection of T using the Fisher-Yates algorithm and returns an ArrayList of T. |
<T> java.util.ArrayList<T> |
shuffle(java.util.Collection<T> elements,
java.util.ArrayList<T> buf)
Shuffles a
Collection of T using the Fisher-Yates algorithm and puts it in a buffer. |
<T> T[] |
shuffle(T[] elements)
Shuffle an array using the Fisher-Yates algorithm and returns a shuffled copy, freshly-allocated, without
modifying elements.
|
<T> T[] |
shuffle(T[] elements,
T[] dest)
Shuffle an array using the Fisher-Yates algorithm.
|
<T> java.util.List<T> |
shuffleInPlace(java.util.List<T> elements)
Shuffles a Collection of T items in-place using the Fisher-Yates algorithm.
|
<T> T[] |
shuffleInPlace(T[] elements)
Shuffles an array in-place using the Fisher-Yates algorithm.
|
java.io.Serializable |
toSerializable()
Gets a view of this IRNG in a way that implements
Serializable , which may simply be this IRNG if it
implements Serializable as well as IRNG. |
copy
int next(int bits)
bits
bits to represent.next
in interface RandomnessSource
bits
- an int between 1 and 32, both inclusiveint nextInt()
int nextInt(int bound)
bound
- the upper bound (exclusive)long nextLong()
nextLong
in interface RandomnessSource
long nextLong(long bound)
bound
- the upper bound (exclusive)boolean nextBoolean()
double nextDouble()
double nextDouble(double outer)
outer
- the outer exclusive bound as a double; can be negative or positivefloat nextFloat()
float nextFloat(float outer)
outer
- the outer exclusive bound as a float; can be negative or positivelong nextSignedLong(long outerBound)
nextLong()
, where rejection sampling
would sometimes advance by one call, but other times by arbitrarily many more.outerBound
- the outer exclusive bound; can be positive or negativeint nextSignedInt(int outerBound)
outerBound
- the outer bound (exclusive), can be negative or positiveint between(int min, int max)
min
and max
happen to be the same, min
is returned
(breaking the exclusive behavior, but it's convenient to do so).min
- the minimum bound on the return value (inclusive)max
- the maximum bound on the return value (exclusive)long between(long min, long max)
min
and max
happen to be the same, min
is returned
(breaking the exclusive behavior, but it's convenient to do so).min
- the minimum bound on the return value (inclusive)max
- the maximum bound on the return value (exclusive)double between(double min, double max)
min
- the minimum bound on the return value (inclusive)max
- the maximum bound on the return value (exclusive)<T> T getRandomElement(T[] array)
T
- the type of the returned objectarray
- the array to get an element from<T> T getRandomElement(java.util.List<T> list)
T
- the type of the returned objectlist
- the list to get an element from<T> T getRandomElement(java.util.Collection<T> coll)
getRandomElement(Queue)
,
since Queue implements Collection and the older Queue-using implementation was probably less efficient.
getRandomElement(List)
whenever possible, or in some cases you can use
methods that get a random value on the Collection (or Map, in the case of OrderedMap) itself.T
- the type of the returned objectcoll
- the Collection to get an element from; remember, Map does not implement Collection<T> T[] shuffle(T[] elements)
T
- can be any non-primitive type.elements
- an array of T; will not be modified<T> T[] shuffleInPlace(T[] elements)
shuffle(Object[], Object[])
.
T
- can be any non-primitive type.elements
- an array of T; will be modified<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 modifications<T> java.util.ArrayList<T> shuffle(java.util.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 modified<T> java.util.ArrayList<T> shuffle(java.util.Collection<T> elements, java.util.ArrayList<T> buf)
Collection
of T using the Fisher-Yates algorithm and puts it in a buffer.
The result is allocated if buf
is null or if buf
isn't empty,
otherwise elements
is poured into buf
.
T
- can be any non-primitive type.elements
- a Collection of T; will not be modifiedbuf
- a buffer as an ArrayList that will be filled with the shuffled contents of elements;
if null or non-empty, a new ArrayList will be allocated and returnedbuf
<T> java.util.List<T> shuffleInPlace(java.util.List<T> elements)
shuffle(Collection)
, which returns a List as well.
T
- can be any non-primitive type.elements
- a Collection of T; will be modifiedint[] randomOrdering(int length)
length
- the size of the ordering to produceint[] randomOrdering(int length, int[] dest)
length
- the size of the ordering to producedest
- the destination array; will be modified<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 lengthMath.min(output.length, data.length)
unique items have been put into it from datajava.io.Serializable toSerializable()
Serializable
, which may simply be this IRNG if it
implements Serializable as well as IRNG.
RNG
initialized by calling
RNG(long)
with nextLong()
if you are unable to save the current state of this IRNG and the
caller still needs something saved. This won't preserve the current state or the choice of IRNG implementation,
however, so it is simply a last resort in case you don't want to throw an exception.Serializable
view of this IRNG or a similar one; may be this
Copyright © Eben Howard 2012–2022. All rights reserved.