Class BaseSplittableRandomAdapter
- java.lang.Object
-
- java.util.Random
-
- io.github.pr0methean.betterrandom.prng.BaseRandom
-
- io.github.pr0methean.betterrandom.prng.adapter.BaseSplittableRandomAdapter
-
- All Implemented Interfaces:
ByteArrayReseedableRandom
,EntropyCountingRandom
,Java8CompatRandom
,RepeatableRandom
,Dumpable
,Serializable
- Direct Known Subclasses:
DirectSplittableRandomAdapter
,SplittableRandomAdapter
public abstract class BaseSplittableRandomAdapter extends BaseRandom
Abstract class for implementations ofBaseRandom
that wrap one or moreSplittableRandom
instances.- Author:
- Chris Hennick
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class io.github.pr0methean.betterrandom.prng.BaseRandom
ENTROPY_OF_DOUBLE, ENTROPY_OF_FLOAT, entropyBits, lock, randomSeeder, seed, superConstructorFinished
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
BaseSplittableRandomAdapter(byte[] seed)
Constructs an instance with the given seed.protected
BaseSplittableRandomAdapter(long seed)
Constructs an instance with the given seed.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description int
getNewSeedLength()
Returns the only supported seed length.protected abstract java8.util.SplittableRandom
getSplittableRandom()
Returns theSplittableRandom
that is to be used to generate random numbers for the current thread.protected void
lockForNextGaussian()
Performs whatever locking is needed byBaseRandom.nextGaussian()
.protected int
next(int bits)
Delegates toSplittableRandom.nextInt()
orSplittableRandom.nextInt(int)
.boolean
nextBoolean()
Delegates toSplittableRandom.nextBoolean()
.void
nextBytes(byte[] bytes)
Delegates toSplittableRandom.nextInt(256)
.double
nextDouble(double bound)
Delegates toSplittableRandom.nextDouble(bound)
.double
nextDouble(double origin, double bound)
Delegates toSplittableRandom.nextDouble (origin, bound)
.protected double
nextDoubleNoEntropyDebit()
Delegates toSplittableRandom.nextDouble()
.float
nextFloat()
Delegates toSplittableRandom.nextInt(int)
.double
nextGaussian()
Delegates toSplittableRandom.nextDouble()
viaBaseRandom.internalNextGaussian(DoubleSupplier)
.int
nextInt()
Delegates toSplittableRandom.nextInt()
.int
nextInt(int bound)
Delegates toSplittableRandom.nextInt(bound)
.int
nextInt(int origin, int bound)
Delegates toSplittableRandom.nextInt(origin, bound)
.long
nextLong(long bound)
Delegates toSplittableRandom.nextLong(bound)
.long
nextLong(long origin, long bound)
Delegates toSplittableRandom.nextLong(origin, bound)
.protected long
nextLongNoEntropyDebit()
Delegates toSplittableRandom.nextLong()
.boolean
preferSeedWithLong()
Indicates whetherRandom.setSeed(long)
is recommended overByteArrayReseedableRandom.setSeed(byte[])
when the seed is already in the form of along
.protected void
unlockForNextGaussian()
Releases the locks acquired byBaseRandom.lockForNextGaussian()
.protected boolean
withProbabilityInternal(double probability)
Delegates toSplittableRandom.nextDouble()
.-
Methods inherited from class io.github.pr0methean.betterrandom.prng.BaseRandom
addSubclassFields, checkLength, creditEntropyForNewSeed, debitEntropy, doubles, doubles, doubles, doubles, dump, entropyOfInt, entropyOfLong, fallbackSetSeedIfInitialized, gaussians, gaussians, getEntropyBits, getRandomSeeder, getSeed, initTransientFields, internalNextGaussian, ints, ints, ints, ints, longs, longs, longs, longs, needsReseedingEarly, nextDouble, nextElement, nextElement, nextEnum, nextLong, setRandomSeeder, setSeed, setSeed, setSeedInternal, supportsMultipleSeedLengths, usesParallelStreams, withProbability
-
-
-
-
Constructor Detail
-
BaseSplittableRandomAdapter
protected BaseSplittableRandomAdapter(byte[] seed)
Constructs an instance with the given seed.- Parameters:
seed
- The seed.
-
BaseSplittableRandomAdapter
protected BaseSplittableRandomAdapter(long seed)
Constructs an instance with the given seed.- Parameters:
seed
- The seed.
-
-
Method Detail
-
getSplittableRandom
protected abstract java8.util.SplittableRandom getSplittableRandom()
Returns theSplittableRandom
that is to be used to generate random numbers for the current thread. (SplittableRandom
isn't thread-safe.) Called by all thenext*
methods.- Returns:
- the
SplittableRandom
to use with the current thread.
-
nextDouble
public double nextDouble(double bound)
Delegates toSplittableRandom.nextDouble(bound)
.- Overrides:
nextDouble
in classBaseRandom
- Parameters:
bound
- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
double
value between zero (inclusive) and the bound (exclusive)
-
nextDouble
public double nextDouble(double origin, double bound)
Delegates toSplittableRandom.nextDouble (origin, bound)
.- Overrides:
nextDouble
in classBaseRandom
- Parameters:
origin
- the least value returnedbound
- the upper bound (exclusive)- Returns:
- a pseudorandom
double
value between the origin (inclusive) and the bound (exclusive)
-
next
protected int next(int bits)
Delegates toSplittableRandom.nextInt()
orSplittableRandom.nextInt(int)
.- Specified by:
next
in classBaseRandom
-
nextBytes
public void nextBytes(byte[] bytes)
Delegates toSplittableRandom.nextInt(256)
.- Specified by:
nextBytes
in interfaceJava8CompatRandom
- Overrides:
nextBytes
in classBaseRandom
-
nextInt
public int nextInt()
Delegates toSplittableRandom.nextInt()
.- Specified by:
nextInt
in interfaceJava8CompatRandom
- Overrides:
nextInt
in classBaseRandom
-
nextInt
public int nextInt(int bound)
Delegates toSplittableRandom.nextInt(bound)
.- Specified by:
nextInt
in interfaceJava8CompatRandom
- Overrides:
nextInt
in classBaseRandom
-
nextInt
public int nextInt(int origin, int bound)
Delegates toSplittableRandom.nextInt(origin, bound)
.- Overrides:
nextInt
in classBaseRandom
- Parameters:
origin
- the least value returnedbound
- the upper bound (exclusive)- Returns:
- a pseudorandom
int
value between the origin (inclusive) and the bound (exclusive)
-
withProbabilityInternal
protected boolean withProbabilityInternal(double probability)
Delegates to
Called bySplittableRandom.nextDouble()
.BaseRandom.withProbability(double)
to generate a boolean with a specified probability of returning true, after checking thatprobability
is strictly between 0 and 1.- Overrides:
withProbabilityInternal
in classBaseRandom
- Parameters:
probability
- The probability (between 0 and 1 exclusive) of returning true.- Returns:
- True with probability equal to the
probability
parameter; false otherwise.
-
preferSeedWithLong
public boolean preferSeedWithLong()
Description copied from interface:ByteArrayReseedableRandom
Indicates whetherRandom.setSeed(long)
is recommended overByteArrayReseedableRandom.setSeed(byte[])
when the seed is already in the form of along
.- Specified by:
preferSeedWithLong
in interfaceByteArrayReseedableRandom
- Overrides:
preferSeedWithLong
in classBaseRandom
- Returns:
- true if
Random.setSeed(long)
will tend to perform better thanByteArrayReseedableRandom.setSeed(byte[])
.
-
nextLongNoEntropyDebit
protected long nextLongNoEntropyDebit()
Delegates to
Returns the next randomSplittableRandom.nextLong()
.long
, but does not debit entropy.- Overrides:
nextLongNoEntropyDebit
in classBaseRandom
- Returns:
- a pseudorandom
long
with all possible values equally likely.
-
nextLong
public long nextLong(long bound)
Delegates toSplittableRandom.nextLong(bound)
.- Overrides:
nextLong
in classBaseRandom
- Parameters:
bound
- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
long
value between zero (inclusive) and the bound (exclusive)
-
nextLong
public long nextLong(long origin, long bound)
Delegates toSplittableRandom.nextLong(origin, bound)
.- Overrides:
nextLong
in classBaseRandom
- Parameters:
origin
- the least value returnedbound
- the upper bound (exclusive)- Returns:
- a pseudorandom
long
value between the origin (inclusive) and the bound (exclusive)
-
nextDoubleNoEntropyDebit
protected double nextDoubleNoEntropyDebit()
Delegates toSplittableRandom.nextDouble()
.- Overrides:
nextDoubleNoEntropyDebit
in classBaseRandom
- Returns:
- a pseudorandom
double
.
-
nextGaussian
public double nextGaussian()
Delegates toSplittableRandom.nextDouble()
viaBaseRandom.internalNextGaussian(DoubleSupplier)
.- Specified by:
nextGaussian
in interfaceJava8CompatRandom
- Overrides:
nextGaussian
in classBaseRandom
-
lockForNextGaussian
protected void lockForNextGaussian()
Description copied from class:BaseRandom
Performs whatever locking is needed byBaseRandom.nextGaussian()
.- Overrides:
lockForNextGaussian
in classBaseRandom
-
unlockForNextGaussian
protected void unlockForNextGaussian()
Description copied from class:BaseRandom
Releases the locks acquired byBaseRandom.lockForNextGaussian()
.- Overrides:
unlockForNextGaussian
in classBaseRandom
-
nextBoolean
public boolean nextBoolean()
Delegates toSplittableRandom.nextBoolean()
.- Specified by:
nextBoolean
in interfaceJava8CompatRandom
- Overrides:
nextBoolean
in classBaseRandom
-
nextFloat
public float nextFloat()
Delegates toSplittableRandom.nextInt(int)
.- Specified by:
nextFloat
in interfaceJava8CompatRandom
- Overrides:
nextFloat
in classBaseRandom
-
getNewSeedLength
public int getNewSeedLength()
Returns the only supported seed length.- Specified by:
getNewSeedLength
in interfaceByteArrayReseedableRandom
- Specified by:
getNewSeedLength
in classBaseRandom
- Returns:
- The desired length of a new byte-array seed.
-
-