Package squidpony.squidmath
Class DistributedRNG
java.lang.Object
squidpony.squidmath.AbstractRNG
squidpony.squidmath.DistributedRNG
- All Implemented Interfaces:
Serializable,IRNG,IStatefulRNG,RandomnessSource,StatefulRandomness
public class DistributedRNG extends AbstractRNG implements IStatefulRNG, StatefulRandomness, Serializable
An implementation of
This uses a
Created by Tommy Ettinger on 11/27/2019.
IRNG that allows specifying a distribution for all random numbers it produces via a
IDistribution.SimpleDistribution value. You can adapt any IDistribution to a
SimpleDistribution with the static methods in SimpleDistribution, like
IDistribution.SimpleDistribution.fractionalDistribution(IDistribution). If no
distribution is specified, this uses CurvedBoundedDistribution.instance.
This uses a
MoonwalkRNG internally to handle the number generation that the distribution requests. While you
can call methods on rng that are specific to MoonwalkRNG, many distributions will get multiple random
numbers where a normal RNG would only get one, and this makes the state-jumping features of MoonwalkRNG less useful
here. It's still a fast generator when it comes to generating doubles, which is why it's used here; GWT-oriented
generators would be slower at generating doubles on desktop and many mobile platforms.
Created by Tommy Ettinger on 11/27/2019.
- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description IDistribution.SimpleDistributiondistributionMoonwalkRNGrng -
Constructor Summary
Constructors Constructor Description DistributedRNG()DistributedRNG(long state)DistributedRNG(long state, IDistribution.SimpleDistribution distribution) -
Method Summary
Modifier and Type Method Description DistributedRNGcopy()Creates a copy of this IRNG; it will generate the same random numbers, given the same calls in order, as this IRNG at the point copy() is called.longgetState()Get the current internal state of the StatefulRandomness as a long.intnext(int bits)Get up to 32 bits (inclusive) of random output; the int this produces will not require more thanbitsbits to represent.booleannextBoolean()Get a random bit of state, interpreted as true or false with approximately equal likelihood.doublenextDouble()Gets a random double between 0.0 inclusive and 1.0 exclusive.floatnextFloat()Gets a random float between 0.0f inclusive and 1.0f exclusive.intnextInt()Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive).intnextInt(int bound)Returns a random non-negative integer below the given bound, or 0 if the bound is 0 or negative.longnextLong()Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive).longnextLong(long bound)Exclusive on bound (which must be positive), with an inner bound of 0.intnextSignedInt(int bound)Returns a random non-negative integer between 0 (inclusive) and the given bound (exclusive), or 0 if the bound is 0.longnextSignedLong(long bound)Exclusive on bound (which may be positive or negative), with an inner bound of 0.voidsetState(long state)Set the current internal state of this StatefulRandomness with a long; this accepts a state of 0 with no issues.SerializabletoSerializable()Gets a view of this IRNG in a way that implementsSerializable, which may simply be this IRNG if it implements Serializable as well as IRNG.Methods inherited from class squidpony.squidmath.AbstractRNG
between, between, between, getRandomElement, getRandomElement, getRandomElement, nextDouble, nextFloat, randomOrdering, randomOrdering, randomPortion, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace, swapMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface squidpony.squidmath.IRNG
between, between, between, getRandomElement, getRandomElement, getRandomElement, nextDouble, nextFloat, randomOrdering, randomOrdering, randomPortion, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace
-
Field Details
-
Constructor Details
-
Method Details
-
next
Get up to 32 bits (inclusive) of random output; the int this produces will not require more thanbitsbits to represent.- Specified by:
nextin interfaceIRNG- Specified by:
nextin interfaceRandomnessSource- Specified by:
nextin classAbstractRNG- Parameters:
bits- an int between 1 and 32, both inclusive- Returns:
- a random number that fits in the specified number of bits
-
nextInt
Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive).- Specified by:
nextIntin interfaceIRNG- Specified by:
nextIntin classAbstractRNG- Returns:
- a 32-bit random int.
-
nextLong
Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive). This implementation has "holes" in its range, numbers that it cannot produce regardless of distribution.- Specified by:
nextLongin interfaceIRNG- Specified by:
nextLongin interfaceRandomnessSource- Specified by:
nextLongin classAbstractRNG- Returns:
- a 64-bit random long.
-
nextBoolean
Get a random bit of state, interpreted as true or false with approximately equal likelihood.- Specified by:
nextBooleanin interfaceIRNG- Specified by:
nextBooleanin classAbstractRNG- Returns:
- a random boolean.
-
nextDouble
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:
nextDoublein interfaceIRNG- Specified by:
nextDoublein classAbstractRNG- Returns:
- a double between 0.0 (inclusive) and 0.9999999999999999 (inclusive)
-
nextFloat
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 .- Specified by:
nextFloatin interfaceIRNG- Specified by:
nextFloatin classAbstractRNG- Returns:
- a float between 0f (inclusive) and 0.99999994f (inclusive)
-
nextInt
Returns a random non-negative integer below the given bound, or 0 if the bound is 0 or negative.- Specified by:
nextIntin interfaceIRNG- Overrides:
nextIntin classAbstractRNG- Parameters:
bound- the upper bound (exclusive)- Returns:
- the found number
-
nextLong
Exclusive on bound (which must be positive), with an inner bound of 0. If bound is negative or 0 this always returns 0.- Specified by:
nextLongin interfaceIRNG- Overrides:
nextLongin classAbstractRNG- Parameters:
bound- the outer exclusive bound; should be positive, otherwise this always returns 0L- Returns:
- a random long between 0 (inclusive) and bound (exclusive)
-
nextSignedLong
Exclusive on bound (which may be positive or negative), with an inner bound of 0. If bound is negative this returns a negative long; if bound is positive this returns a positive long. The bound can even be 0, which will cause this to return 0L every time.- Specified by:
nextSignedLongin interfaceIRNG- Overrides:
nextSignedLongin classAbstractRNG- Parameters:
bound- the outer exclusive bound; can be positive or negative- Returns:
- a random long between 0 (inclusive) and bound (exclusive)
-
nextSignedInt
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.- Specified by:
nextSignedIntin interfaceIRNG- Overrides:
nextSignedIntin classAbstractRNG- Parameters:
bound- the outer bound (exclusive), can be negative or positive- Returns:
- the found number
-
copy
Creates a copy of this IRNG; it will generate the same random numbers, given the same calls in order, as this IRNG at the point copy() is called. The copy will not share references with this IRNG, except todistribution, which usually shouldn't change much.- Specified by:
copyin interfaceIRNG- Specified by:
copyin interfaceRandomnessSource- Specified by:
copyin interfaceStatefulRandomness- Specified by:
copyin classAbstractRNG- Returns:
- a copy of this IRNG
-
toSerializable
Gets a view of this IRNG in a way that implementsSerializable, which may simply be this IRNG if it implements Serializable as well as IRNG.- Specified by:
toSerializablein interfaceIRNG- Specified by:
toSerializablein classAbstractRNG- Returns:
- a
Serializableview of this IRNG or a similar one; may bethis
-
getState
Get the current internal state of the StatefulRandomness as a long.- Specified by:
getStatein interfaceStatefulRandomness- Returns:
- the current internal state of this object.
-
setState
Set the current internal state of this StatefulRandomness with a long; this accepts a state of 0 with no issues.- Specified by:
setStatein interfaceStatefulRandomness- Parameters:
state- a 64-bit long.
-