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,RepeatableRandom,Dumpable,Serializable
- Direct Known Subclasses:
SingleThreadSplittableRandomAdapter,SplittableRandomAdapter
public abstract class BaseSplittableRandomAdapter extends BaseRandom
Abstract class for implementations ofBaseRandomthat wrap one or moreSplittableRandominstances.- 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 protectedBaseSplittableRandomAdapter(byte[] seed)Constructs an instance with the given seed.protectedBaseSplittableRandomAdapter(long seed)Constructs an instance with the given seed.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description intgetNewSeedLength()Returns the only supported seed length.protected abstract SplittableRandomgetSplittableRandom()Returns theSplittableRandomthat is to be used to generate random numbers for the current thread.protected voidlockForNextGaussian()Performs whatever locking is needed byBaseRandom.nextGaussian().protected intnext(int bits)Delegates toSplittableRandom.nextInt()orSplittableRandom.nextInt(int).booleannextBoolean()Delegates toSplittableRandom.nextBoolean().voidnextBytes(byte[] bytes)Delegates toSplittableRandom.nextInt(256).doublenextDouble(double bound)Delegates toSplittableRandom.nextDouble(bound).doublenextDouble(double origin, double bound)Delegates toSplittableRandom.nextDouble (origin, bound).protected doublenextDoubleNoEntropyDebit()Delegates toSplittableRandom.nextDouble().floatnextFloat()Delegates toSplittableRandom.nextInt(int).doublenextGaussian()intnextInt()Delegates toSplittableRandom.nextInt().intnextInt(int bound)Delegates toSplittableRandom.nextInt(bound).intnextInt(int origin, int bound)Delegates toSplittableRandom.nextInt(origin, bound).longnextLong(long bound)Delegates toSplittableRandom.nextLong(bound).longnextLong(long origin, long bound)Delegates toSplittableRandom.nextLong(origin, bound).protected longnextLongNoEntropyDebit()Delegates toSplittableRandom.nextLong().booleanpreferSeedWithLong()Indicates whetherRandom.setSeed(long)is recommended overByteArrayReseedableRandom.setSeed(byte[])when the seed is already in the form of along.protected voidunlockForNextGaussian()Releases the locks acquired byBaseRandom.lockForNextGaussian().protected booleanwithProbabilityInternal(double probability)Delegates toSplittableRandom.nextDouble().-
Methods inherited from class io.github.pr0methean.betterrandom.prng.BaseRandom
addSubclassFields, checkLength, checkValidRange, 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, nextDouble, nextElement, nextElement, nextEnum, nextLong, setRandomSeeder, setSeed, setSeed, setSeedInternal, supportsMultipleSeedLengths, usesParallelStreams, withProbability
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.github.pr0methean.betterrandom.EntropyCountingRandom
needsReseedingEarly
-
-
-
-
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 SplittableRandom getSplittableRandom()
Returns theSplittableRandomthat is to be used to generate random numbers for the current thread. (SplittableRandomisn't thread-safe.) Called by all thenext*methods.- Returns:
- the
SplittableRandomto use with the current thread.
-
nextDouble
public double nextDouble(double bound)
Delegates toSplittableRandom.nextDouble(bound).- Overrides:
nextDoublein classBaseRandom- Parameters:
bound- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
doublevalue between zero (inclusive) and the bound (exclusive)
-
nextDouble
public double nextDouble(double origin, double bound)Delegates toSplittableRandom.nextDouble (origin, bound).- Overrides:
nextDoublein classBaseRandom- Parameters:
origin- the least value returnedbound- the upper bound (exclusive)- Returns:
- a pseudorandom
doublevalue between the origin (inclusive) and the bound (exclusive)
-
next
protected int next(int bits)
Delegates toSplittableRandom.nextInt()orSplittableRandom.nextInt(int).- Specified by:
nextin classBaseRandom
-
nextBytes
public void nextBytes(byte[] bytes)
Delegates toSplittableRandom.nextInt(256).- Overrides:
nextBytesin classBaseRandom
-
nextInt
public int nextInt()
Delegates toSplittableRandom.nextInt().- Overrides:
nextIntin classBaseRandom
-
nextInt
public int nextInt(int bound)
Delegates toSplittableRandom.nextInt(bound).- Overrides:
nextIntin classBaseRandom
-
nextInt
public int nextInt(int origin, int bound)Delegates toSplittableRandom.nextInt(origin, bound).- Overrides:
nextIntin classBaseRandom- Parameters:
origin- the least value returnedbound- the upper bound (exclusive)- Returns:
- a pseudorandom
intvalue 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 thatprobabilityis strictly between 0 and 1.- Overrides:
withProbabilityInternalin classBaseRandom- Parameters:
probability- The probability (between 0 and 1 exclusive) of returning true.- Returns:
- True with probability equal to the
probabilityparameter; false otherwise.
-
preferSeedWithLong
public boolean preferSeedWithLong()
Description copied from interface:ByteArrayReseedableRandomIndicates whetherRandom.setSeed(long)is recommended overByteArrayReseedableRandom.setSeed(byte[])when the seed is already in the form of along.- Specified by:
preferSeedWithLongin interfaceByteArrayReseedableRandom- Overrides:
preferSeedWithLongin 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:
nextLongNoEntropyDebitin classBaseRandom- Returns:
- a pseudorandom
longwith all possible values equally likely.
-
nextLong
public long nextLong(long bound)
Delegates toSplittableRandom.nextLong(bound).- Overrides:
nextLongin classBaseRandom- Parameters:
bound- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
longvalue between zero (inclusive) and the bound (exclusive)
-
nextLong
public long nextLong(long origin, long bound)Delegates toSplittableRandom.nextLong(origin, bound).- Overrides:
nextLongin classBaseRandom- Parameters:
origin- the least value returnedbound- the upper bound (exclusive)- Returns:
- a pseudorandom
longvalue between the origin (inclusive) and the bound (exclusive)
-
nextDoubleNoEntropyDebit
protected double nextDoubleNoEntropyDebit()
Delegates toSplittableRandom.nextDouble().- Overrides:
nextDoubleNoEntropyDebitin classBaseRandom- Returns:
- a pseudorandom
double.
-
nextGaussian
public double nextGaussian()
Delegates toSplittableRandom.nextDouble()viaBaseRandom.internalNextGaussian(java.util.function.DoubleSupplier).- Overrides:
nextGaussianin classBaseRandom
-
lockForNextGaussian
protected void lockForNextGaussian()
Description copied from class:BaseRandomPerforms whatever locking is needed byBaseRandom.nextGaussian().- Overrides:
lockForNextGaussianin classBaseRandom
-
unlockForNextGaussian
protected void unlockForNextGaussian()
Description copied from class:BaseRandomReleases the locks acquired byBaseRandom.lockForNextGaussian().- Overrides:
unlockForNextGaussianin classBaseRandom
-
nextBoolean
public boolean nextBoolean()
Delegates toSplittableRandom.nextBoolean().- Overrides:
nextBooleanin classBaseRandom
-
nextFloat
public float nextFloat()
Delegates toSplittableRandom.nextInt(int).- Overrides:
nextFloatin classBaseRandom
-
getNewSeedLength
public int getNewSeedLength()
Returns the only supported seed length.- Specified by:
getNewSeedLengthin interfaceByteArrayReseedableRandom- Specified by:
getNewSeedLengthin classBaseRandom- Returns:
- The desired length of a new byte-array seed.
-
-