Class EntropyBlockingSplittableRandomAdapter
- java.lang.Object
-
- java.util.Random
-
- io.github.pr0methean.betterrandom.prng.BaseRandom
-
- io.github.pr0methean.betterrandom.prng.adapter.BaseSplittableRandomAdapter
-
- io.github.pr0methean.betterrandom.prng.adapter.SplittableRandomAdapter
-
- io.github.pr0methean.betterrandom.prng.adapter.EntropyBlockingSplittableRandomAdapter
-
- All Implemented Interfaces:
ByteArrayReseedableRandom
,EntropyCountingRandom
,RepeatableRandom
,Dumpable
,Serializable
public class EntropyBlockingSplittableRandomAdapter extends SplittableRandomAdapter
ASplittableRandomAdapter
that blocks waiting to be reseeded if its entropy drops too low. Reseeding is done on aRandomSeeder
if provided, and on the calling thread otherwise. Entropy count is thread-local, so consuming entropy on one thread won't directly cause blocking on another thread.- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class io.github.pr0methean.betterrandom.prng.adapter.SplittableRandomAdapter
threadLocal
-
Fields inherited from class io.github.pr0methean.betterrandom.prng.BaseRandom
ENTROPY_OF_DOUBLE, ENTROPY_OF_FLOAT, entropyBits, lock, randomSeeder, seed, superConstructorFinished
-
-
Constructor Summary
Constructors Constructor Description EntropyBlockingSplittableRandomAdapter(RandomSeeder randomSeeder, long minimumEntropy)
Creates an instance.EntropyBlockingSplittableRandomAdapter(SeedGenerator seedGenerator, RandomSeeder randomSeeder, long minimumEntropy)
Creates an instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected EntropyBlockingRandomWrapper<SingleThreadSplittableRandomAdapter>
createDelegate()
Creates the delegate for the calling thread.protected void
debitEntropy(long bits)
Record that entropy has been spent, and schedule a reseeding if this PRNG has now spent as much as it's been seeded with.byte[]
getSeed()
Returns the calling thread's seed, not the master seed.protected SplittableRandom
getSplittableRandom()
Returns theSplittableRandom
that is to be used to generate random numbers for the current thread.void
setSeed(byte[] seed)
Reseed this PRNG.void
setSeed(long seed)
Sets the seed of this random number generator using a single long seed, if this implementation supports that.-
Methods inherited from class io.github.pr0methean.betterrandom.prng.adapter.SplittableRandomAdapter
addSubclassFields, equals, getEntropyBits, hashCode, initTransientFields, nextGaussian, setRandomSeeder, setSeedInternal, toString, usesParallelStreams
-
Methods inherited from class io.github.pr0methean.betterrandom.prng.adapter.BaseSplittableRandomAdapter
getNewSeedLength, lockForNextGaussian, next, nextBoolean, nextBytes, nextDouble, nextDouble, nextDoubleNoEntropyDebit, nextFloat, nextInt, nextInt, nextInt, nextLong, nextLong, nextLongNoEntropyDebit, preferSeedWithLong, unlockForNextGaussian, withProbabilityInternal
-
Methods inherited from class io.github.pr0methean.betterrandom.prng.BaseRandom
checkLength, checkValidRange, creditEntropyForNewSeed, doubles, doubles, doubles, doubles, dump, entropyOfInt, entropyOfLong, fallbackSetSeedIfInitialized, gaussians, gaussians, getRandomSeeder, internalNextGaussian, ints, ints, ints, ints, longs, longs, longs, longs, nextDouble, nextElement, nextElement, nextEnum, nextLong, supportsMultipleSeedLengths, withProbability
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.github.pr0methean.betterrandom.EntropyCountingRandom
needsReseedingEarly
-
-
-
-
Constructor Detail
-
EntropyBlockingSplittableRandomAdapter
public EntropyBlockingSplittableRandomAdapter(RandomSeeder randomSeeder, long minimumEntropy)
Creates an instance.- Parameters:
randomSeeder
- theRandomSeeder
that will reseed this. Its seed generator is also used on the calling thread to generate an initial seed when thisEntropyBlockingSplittableRandomAdapter
is used the first time on each thread.minimumEntropy
- the minimum entropy; operations that would drop the entropy below this amount will instead block until the calling thread's PRNG is reseeded. Should generally be zero or negative.
-
EntropyBlockingSplittableRandomAdapter
public EntropyBlockingSplittableRandomAdapter(@Nullable SeedGenerator seedGenerator, RandomSeeder randomSeeder, long minimumEntropy)
Creates an instance.- Parameters:
seedGenerator
- the seed generator that will generate an initial PRNG seed for each threadrandomSeeder
- theRandomSeeder
that will reseed thisminimumEntropy
- the minimum entropy; operations that would drop the entropy below this amount will instead block until the calling thread's PRNG is reseeded. Should generally be zero or negative.
-
-
Method Detail
-
createDelegate
protected EntropyBlockingRandomWrapper<SingleThreadSplittableRandomAdapter> createDelegate()
Description copied from class:SplittableRandomAdapter
Creates the delegate for the calling thread.- Overrides:
createDelegate
in classSplittableRandomAdapter
- Returns:
- the thread-local delegate
-
setSeed
public void setSeed(byte[] seed)
Reseed this PRNG.Most subclasses should override
Applies only to the calling thread.BaseRandom.setSeedInternal(byte[])
instead of this method, so that they will deserialize properly.- Specified by:
setSeed
in interfaceByteArrayReseedableRandom
- Overrides:
setSeed
in classBaseRandom
- Parameters:
seed
- The PRNG's new seed.
-
setSeed
public void setSeed(long seed)
Sets the seed of this random number generator using a single long seed, if this implementation supports that. If it is capable of using 64 bits or less of seed data (i.e. if{@link #getNewSeedLength()} <= {@link Long#BYTES}
), then this method shall replace the entire seed asRandom.setSeed(long)
does; otherwise, it shall either combine the input with the existing seed asSecureRandom.setSeed(long)
does, or it shall generate a new seed using theDefaultSeedGenerator
. The latter is a backward-compatibility measure and can be very slow. Applies only to the calling thread.- Overrides:
setSeed
in classBaseRandom
- See Also:
ByteArrayReseedableRandom.preferSeedWithLong()
-
getSplittableRandom
protected SplittableRandom getSplittableRandom()
Description copied from class:BaseSplittableRandomAdapter
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.- Overrides:
getSplittableRandom
in classSplittableRandomAdapter
- Returns:
- the
SplittableRandom
to use with the current thread.
-
getSeed
public byte[] getSeed()
Returns the calling thread's seed, not the master seed.- Specified by:
getSeed
in interfaceRepeatableRandom
- Overrides:
getSeed
in classSplittableRandomAdapter
- Returns:
- the seed for the calling thread
-
debitEntropy
protected void debitEntropy(long bits)
Description copied from class:BaseRandom
Record that entropy has been spent, and schedule a reseeding if this PRNG has now spent as much as it's been seeded with.- Overrides:
debitEntropy
in classSplittableRandomAdapter
- Parameters:
bits
- The number of bits of entropy spent.
-
-