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
ASplittableRandomAdapterthat blocks waiting to be reseeded if its entropy drops too low. Reseeding is done on aRandomSeederif 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 voiddebitEntropy(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 SplittableRandomgetSplittableRandom()Returns theSplittableRandomthat is to be used to generate random numbers for the current thread.voidsetSeed(byte[] seed)Reseed this PRNG.voidsetSeed(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- theRandomSeederthat will reseed this. Its seed generator is also used on the calling thread to generate an initial seed when thisEntropyBlockingSplittableRandomAdapteris 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- theRandomSeederthat 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:SplittableRandomAdapterCreates the delegate for the calling thread.- Overrides:
createDelegatein 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:
setSeedin interfaceByteArrayReseedableRandom- Overrides:
setSeedin 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:
setSeedin classBaseRandom- See Also:
ByteArrayReseedableRandom.preferSeedWithLong()
-
getSplittableRandom
protected SplittableRandom getSplittableRandom()
Description copied from class:BaseSplittableRandomAdapterReturns theSplittableRandomthat is to be used to generate random numbers for the current thread. (SplittableRandomisn't thread-safe.) Called by all thenext*methods.- Overrides:
getSplittableRandomin classSplittableRandomAdapter- Returns:
- the
SplittableRandomto use with the current thread.
-
getSeed
public byte[] getSeed()
Returns the calling thread's seed, not the master seed.- Specified by:
getSeedin interfaceRepeatableRandom- Overrides:
getSeedin classSplittableRandomAdapter- Returns:
- the seed for the calling thread
-
debitEntropy
protected void debitEntropy(long bits)
Description copied from class:BaseRandomRecord that entropy has been spent, and schedule a reseeding if this PRNG has now spent as much as it's been seeded with.- Overrides:
debitEntropyin classSplittableRandomAdapter- Parameters:
bits- The number of bits of entropy spent.
-
-