Package squidpony.squidmath
Class CriticalRNG
java.lang.Object
squidpony.squidmath.RNG
squidpony.squidmath.CriticalRNG
- All Implemented Interfaces:
Serializable
,IRNG
,RandomnessSource
public class CriticalRNG extends RNG
A type of RNG that can generate values larger or smaller than the normal maximum or minimum, based on a modifier.
You should not use this as a general-purpose substitute for
The name comes from "critical hit," the rare but potentially very significant strike in many role-playing games.
Created by Tommy Ettinger on 9/20/2017.
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.
The name comes from "critical hit," the rare but potentially very significant strike in many role-playing games.
Created by Tommy Ettinger on 9/20/2017.
- See Also:
- Serialized Form
-
Nested Class Summary
-
Field Summary
Fields Modifier and Type Field Description float
luck
Positive for higher results, negative for lower results; usually this is small, between -0.5f and 0.5f . -
Constructor Summary
Constructors Constructor Description CriticalRNG()
Makes a CriticalRNG with a luck factor of 0 and a randomly-seeded DiverRNG for its RandomnessSource.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(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 usingCrossHash.hash64(CharSequence)
and use the result to seed the DiverRNG).CriticalRNG(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 usingCrossHash.hash64(CharSequence)
and use the result to seed the DiverRNG).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. -
Method Summary
Modifier and Type Method Description <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.<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)
Gets a pseudo-random long between 0 and bound.Methods inherited from class squidpony.squidmath.RNG
approximateBits, asRandom, between, between, between, betweenWeighted, copy, equals, getRandomCellsIterable, getRandomness, getRandomStartIterable, getRandomUniqueCells, getRandomUniqueCells, getRandomUniqueCells, hashCode, maxDoubleOf, maxFloatOf, maxIntOf, maxLongOf, minDoubleOf, minFloatOf, minIntOf, minLongOf, next, nextBytes, nextCoord, nextCurvedFloat, nextDoubleInclusive, nextDoubleInclusive, nextFloat, nextFloatInclusive, nextFloatInclusive, nextSignedInt, nextSignedLong, randomInterleave, randomOrdering, randomOrdering, randomPortion, randomPortion, randomRange, randomRotation, setRandomness, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace, toSerializable, toString
-
Field Details
-
luck
Positive for higher results, negative for lower results; usually this is small, between -0.5f and 0.5f .
-
-
Constructor Details
-
CriticalRNG
public CriticalRNG()Makes a CriticalRNG with a luck factor of 0 and a randomly-seeded DiverRNG for its RandomnessSource. -
CriticalRNG
Makes a CriticalRNG with a luck factor of 0 and a DiverRNG with the given seed for its RandomnessSource.- Parameters:
seed
- any long
-
CriticalRNG
Makes a CriticalRNG with a luck factor of 0 and a DiverRNG with the given seed for its RandomnessSource (this will hash seedString usingCrossHash.hash64(CharSequence)
and use the result to seed the DiverRNG).- Parameters:
seedString
- any String
-
CriticalRNG
Makes a CriticalRNG with a luck factor of 0 and the given RandomnessSource.- Parameters:
random
- a RandomnessSource, such as aLongPeriodRNG
orLightRNG
-
CriticalRNG
Makes a CriticalRNG with the given luck factor and a randomly-seeded DiverRNG for its RandomnessSource.- Parameters:
luck
- typically a small float, often between -0.5f and 0.5f, that will affect the results this returns
-
CriticalRNG
Makes a CriticalRNG with the given luck factor and a DiverRNG with the given seed for its RandomnessSource.- Parameters:
seed
- any longluck
- typically a small float, often between -0.5f and 0.5f, that will affect the results this returns
-
CriticalRNG
Makes a CriticalRNG with a luck factor of 0 and a DiverRNG with the given seed for its RandomnessSource (this will hash seedString usingCrossHash.hash64(CharSequence)
and use the result to seed the DiverRNG).- Parameters:
seedString
- any Stringluck
- typically a small float, often between -0.5f and 0.5f, that will affect the results this returns
-
CriticalRNG
Makes a CriticalRNG with a luck factor of 0 and the given RandomnessSource.- Parameters:
random
- a RandomnessSource, such as aLongPeriodRNG
orLightRNG
luck
- typically a small float, often between -0.5f and 0.5f, that will affect the results this returns
-
-
Method Details
-
nextDouble
Description copied from class:RNG
Gets a random double between 0.0 inclusive and 1.0 exclusive. This returns a maximum of 0.9999999999999999 because that is the largest double value that is less than 1.0 .- Specified by:
nextDouble
in interfaceIRNG
- Overrides:
nextDouble
in classRNG
- Returns:
- a double between 0.0 (inclusive) and 0.9999999999999999 (inclusive)
-
nextDouble
Description copied from class:RNG
This returns a random double between 0.0 (inclusive) and outer (exclusive). The value for outer can be positive or negative. Because of how math on doubles works, there are at most 2 to the 53 values this can return for any given outer bound, and very large values for outer will not necessarily produce all numbers you might expect.- Specified by:
nextDouble
in interfaceIRNG
- Overrides:
nextDouble
in classRNG
- Parameters:
max
- the outer exclusive bound as a double; can be negative or positive- Returns:
- a double between 0.0 (inclusive) and outer (exclusive)
-
nextFloat
Description copied from class:RNG
Gets a random float between 0.0f inclusive and 1.0f exclusive. This returns a maximum of 0.99999994 because that is the largest float value that is less than 1.0f . -
nextBoolean
Description copied from class:RNG
Get a random bit of state, interpreted as true or false with approximately equal likelihood. This may have better behavior thanrng.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 withrng.next(1)
and very good output withrng.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.- Specified by:
nextBoolean
in interfaceIRNG
- Overrides:
nextBoolean
in classRNG
- Returns:
- a random boolean.
-
nextLong
Description copied from class:RNG
Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive). -
nextLong
Description copied from class:RNG
Gets a pseudo-random long between 0 and bound. Exclusive on bound (which must be positive), with an inner bound of 0 If bound is negative or 0, this always returns 0. You can useRNG.nextSignedLong(long)
to use a negative bound. The technique that
Credit for this method goes to Rafael Baptista's blog for the original idea, and the JDK10 Math class' usage of Karatsuba multiplication for the current algorithm. This method is drastically faster than the previous implementation when the bound varies often (roughly 4x faster, possibly more). It also always gets exactly one random long, so by default it advances the state as much asRNG.nextLong()
. -
nextInt
Description copied from class:RNG
Returns a random non-negative integer below the given bound, or 0 if the bound is 0 or negative. Always makes one call to theRandomnessSource.next(int)
method of the RandomnessSource that would be returned byRNG.getRandomness()
, even if bound is 0 or negative, to avoid branching and also to ensure consistent advancement rates for the RandomnessSource (this can be important if you use aSkippingRandomness
and want to go back before a result was produced).
This method changed a fair amount on April 5, 2018 to better support RandomnessSource implementations with a slower nextLong() method, such asLathe32RNG
, and to avoid branching/irregular state advancement/modulus operations. It is now almost identical toRNG.nextIntHasty(int)
, but won't return negative results if bound is negative (matching its previous behavior). This may have statistical issues (small ones) if bound is very large (the estimate is still at least a bound of a billion or more before issues are observable). ConsiderRNG.nextSignedInt(int)
if the bound should be allowed to be negative;RNG.nextIntHasty(int)
is here for compatibility with earlier versions, but the two methods are very similar.
Credit goes to Daniel Lemire, http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ -
nextIntHasty
Description copied from class:RNG
Returns a random non-negative integer between 0 (inclusive) and the given bound (exclusive), or 0 if the bound is 0. The bound can be negative, which will produce 0 or a negative result. Uses an aggressively optimized technique that has some bias, but mostly for values of bound over 1 billion. This method is no more "hasty" thanRNG.nextInt(int)
, but it is a little faster than that method because this avoids special behavior for when bound is negative.
Credit goes to Daniel Lemire, http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/- Overrides:
nextIntHasty
in classRNG
- Parameters:
bound
- the outer bound (exclusive), can be negative or positive- Returns:
- the found number
-
nextInt
Description copied from class:RNG
Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive). -
getRandomElement
Description copied from class:RNG
Returns a random element from the provided array and maintains object type.- Specified by:
getRandomElement
in interfaceIRNG
- Overrides:
getRandomElement
in classRNG
- Type Parameters:
T
- the type of the returned object- Parameters:
array
- the array to get an element from- Returns:
- the randomly selected element
-
getRandomElement
Description copied from class:RNG
Returns a random element from the provided list. If the list is empty then null is returned.- Specified by:
getRandomElement
in interfaceIRNG
- Overrides:
getRandomElement
in classRNG
- Type Parameters:
T
- the type of the returned object- Parameters:
list
- the list to get an element from- Returns:
- the randomly selected element
-
getRandomElement
Description copied from class:RNG
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). If you give this a Set, it should be a LinkedHashSet or some form of sorted Set like TreeSet if you want predictable results. Any List or Queue should be fine. Map does not implement Collection, thank you very much Java library designers, so you can't actually pass a Map to this, though you can pass the keys or values. If coll is empty, returns null.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.- Specified by:
getRandomElement
in interfaceIRNG
- Overrides:
getRandomElement
in classRNG
- Type Parameters:
T
- the type of the returned object- Parameters:
coll
- the Collection to get an element from; remember, Map does not implement Collection- Returns:
- the randomly selected element
-