public class CriticalRNG extends RNG
RNG
; it is meant for cases where there is no hard
maximum or minimum for a random value, so it is a poor fit for getting random items from collections or shuffling.
It also uses a curved distribution (almost Gaussian, but slightly more shallow), which makes its results to be most
often near the center of the range they can fall into. The luck
field affects the distribution simply, and
should generally be between -0.5f and 0.5f except in cases where a character or event routinely defies all odds.
There is no value for luck that will prevent this from sometimes producing values outside the requested range, though
at luck = 0 it is somewhat rare for the bounds to be significantly exceeded.
RNG.CustomRandom
Modifier and Type | Field and Description |
---|---|
float |
luck
Positive for higher results, negative for lower results; usually this is small, between -0.5f and 0.5f .
|
Constructor and Description |
---|
CriticalRNG()
Makes a CriticalRNG with a luck factor of 0 and a randomly-seeded DiverRNG for its RandomnessSource.
|
CriticalRNG(java.lang.CharSequence seedString)
Makes a CriticalRNG with a luck factor of 0 and a DiverRNG with the given seed for its RandomnessSource (this
will hash seedString using
CrossHash.hash64(CharSequence) and use the result to seed the DiverRNG). |
CriticalRNG(java.lang.CharSequence seedString,
float luck)
Makes a CriticalRNG with a luck factor of 0 and a DiverRNG with the given seed for its RandomnessSource (this
will hash seedString using
CrossHash.hash64(CharSequence) and use the result to seed the DiverRNG). |
CriticalRNG(float luck)
Makes a CriticalRNG with the given luck factor and a randomly-seeded DiverRNG for its RandomnessSource.
|
CriticalRNG(long seed)
Makes a CriticalRNG with a luck factor of 0 and a DiverRNG with the given seed for its RandomnessSource.
|
CriticalRNG(long seed,
float luck)
Makes a CriticalRNG with the given luck factor and a DiverRNG with the given seed for its RandomnessSource.
|
CriticalRNG(RandomnessSource random)
Makes a CriticalRNG with a luck factor of 0 and the given RandomnessSource.
|
CriticalRNG(RandomnessSource random,
float luck)
Makes a CriticalRNG with a luck factor of 0 and the given RandomnessSource.
|
Modifier and Type | Method and Description |
---|---|
<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.
|
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 max)
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.
|
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.
|
int |
nextIntHasty(int bound)
Returns a random non-negative integer between 0 (inclusive) and the given bound (exclusive),
or 0 if the bound is 0.
|
long |
nextLong()
Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive).
|
long |
nextLong(long bound)
Exclusive on bound (which must be positive), with an inner bound of 0.
|
approximateBits, asRandom, betweenWeighted, copy, equals, getRandomCellsIterable, getRandomness, getRandomStartIterable, getRandomUniqueCells, getRandomUniqueCells, getRandomUniqueCells, hashCode, maxDoubleOf, maxFloatOf, maxIntOf, maxLongOf, minDoubleOf, minFloatOf, minIntOf, minLongOf, next, nextBytes, nextCoord, nextCurvedFloat, nextDoubleInclusive, nextDoubleInclusive, nextFloatInclusive, nextFloatInclusive, randomInterleave, randomOrdering, randomOrdering, randomPortion, randomPortion, randomRange, randomRotation, setRandomness, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace, toSerializable, toString
between, between, between, nextFloat, nextSignedInt, nextSignedLong, swap
public float luck
public CriticalRNG()
public CriticalRNG(long seed)
seed
- any longpublic CriticalRNG(java.lang.CharSequence seedString)
CrossHash.hash64(CharSequence)
and use the result to seed the DiverRNG).seedString
- any Stringpublic CriticalRNG(RandomnessSource random)
random
- a RandomnessSource, such as a LongPeriodRNG
or LightRNG
public CriticalRNG(float luck)
luck
- typically a small float, often between -0.5f and 0.5f, that will affect the results this returnspublic CriticalRNG(long seed, float luck)
seed
- any longluck
- typically a small float, often between -0.5f and 0.5f, that will affect the results this returnspublic CriticalRNG(java.lang.CharSequence seedString, float luck)
CrossHash.hash64(CharSequence)
and use the result to seed the DiverRNG).seedString
- any Stringluck
- typically a small float, often between -0.5f and 0.5f, that will affect the results this returnspublic CriticalRNG(RandomnessSource random, float luck)
random
- a RandomnessSource, such as a LongPeriodRNG
or LightRNG
luck
- typically a small float, often between -0.5f and 0.5f, that will affect the results this returnspublic double nextDouble()
RNG
nextDouble
in interface IRNG
nextDouble
in class RNG
public double nextDouble(double max)
AbstractRNG
nextDouble
in interface IRNG
nextDouble
in class AbstractRNG
max
- the outer exclusive bound as a double; can be negative or positivepublic 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 long nextLong()
RNG
public long nextLong(long bound)
AbstractRNG
AbstractRNG.nextLong()
; subclasses can generate two ints instead of one long if they prefer.nextLong
in interface IRNG
nextLong
in class AbstractRNG
bound
- the outer exclusive bound; should be positive, otherwise this always returns 0Lpublic int nextInt(int bound)
AbstractRNG
nextInt
in interface IRNG
nextInt
in class AbstractRNG
bound
- the upper bound (exclusive)public int nextIntHasty(int bound)
RNG
AbstractRNG.nextInt(int)
, but it is a little
faster than that method because this avoids special behavior for when bound is negative.
nextIntHasty
in class RNG
bound
- the outer bound (exclusive), can be negative or positivepublic int nextInt()
RNG
public <T> T getRandomElement(T[] array)
RNG
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)
AbstractRNG
LinkedList
or similar classes
that need to iterate one-by-one in their List.get(int)
method.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)
AbstractRNG
getRandomElement(Queue)
,
since Queue implements Collection and the older Queue-using implementation was probably less efficient.
AbstractRNG.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.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 CollectionCopyright © Eben Howard 2012–2022. All rights reserved.