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.SimpleDistribution
distribution
MoonwalkRNG
rng
-
Constructor Summary
Constructors Constructor Description DistributedRNG()
DistributedRNG(long state)
DistributedRNG(long state, IDistribution.SimpleDistribution distribution)
-
Method Summary
Modifier and Type Method Description DistributedRNG
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.long
getState()
Get the current internal state of the StatefulRandomness as a long.int
next(int bits)
Get up to 32 bits (inclusive) of random output; the int this produces will not require more thanbits
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.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.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.int
nextSignedInt(int bound)
Returns a random non-negative integer between 0 (inclusive) and the given bound (exclusive), or 0 if the bound is 0.long
nextSignedLong(long bound)
Exclusive on bound (which may be positive or negative), with an inner bound of 0.void
setState(long state)
Set the current internal state of this StatefulRandomness with a long; this accepts a state of 0 with no issues.Serializable
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.Methods inherited from class squidpony.squidmath.AbstractRNG
between, between, between, getRandomElement, getRandomElement, getRandomElement, nextDouble, nextFloat, randomOrdering, randomOrdering, randomPortion, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace, swap
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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 thanbits
bits to represent.- Specified by:
next
in interfaceIRNG
- Specified by:
next
in interfaceRandomnessSource
- Specified by:
next
in 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:
nextInt
in interfaceIRNG
- Specified by:
nextInt
in 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:
nextLong
in interfaceIRNG
- Specified by:
nextLong
in interfaceRandomnessSource
- Specified by:
nextLong
in 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:
nextBoolean
in interfaceIRNG
- Specified by:
nextBoolean
in 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:
nextDouble
in interfaceIRNG
- Specified by:
nextDouble
in 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:
nextFloat
in interfaceIRNG
- Specified by:
nextFloat
in 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:
nextInt
in interfaceIRNG
- Overrides:
nextInt
in 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:
nextLong
in interfaceIRNG
- Overrides:
nextLong
in 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:
nextSignedLong
in interfaceIRNG
- Overrides:
nextSignedLong
in 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:
nextSignedInt
in interfaceIRNG
- Overrides:
nextSignedInt
in 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:
copy
in interfaceIRNG
- Specified by:
copy
in interfaceRandomnessSource
- Specified by:
copy
in interfaceStatefulRandomness
- Specified by:
copy
in 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:
toSerializable
in interfaceIRNG
- Specified by:
toSerializable
in classAbstractRNG
- Returns:
- a
Serializable
view of this IRNG or a similar one; may bethis
-
getState
Get the current internal state of the StatefulRandomness as a long.- Specified by:
getState
in 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:
setState
in interfaceStatefulRandomness
- Parameters:
state
- a 64-bit long.
-