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,Java8CompatRandom,RepeatableRandom,Dumpable,Serializable
public class EntropyBlockingSplittableRandomAdapter extends SplittableRandomAdapter
ASplittableRandomAdapterthat blocks waiting to be reseeded if its entropy drops too low. Reseeding is done on aSimpleRandomSeederif 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(SeedGenerator seedGenerator, SimpleRandomSeeder randomSeeder, long minimumEntropy)Creates an instance.EntropyBlockingSplittableRandomAdapter(SimpleRandomSeeder randomSeeder, long minimumEntropy)Creates an instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected BaseRandomcreateDelegate()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 java8.util.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, 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, creditEntropyForNewSeed, doubles, doubles, doubles, doubles, dump, entropyOfInt, entropyOfLong, fallbackSetSeedIfInitialized, gaussians, gaussians, getRandomSeeder, initTransientFields, internalNextGaussian, ints, ints, ints, ints, longs, longs, longs, longs, needsReseedingEarly, nextDouble, nextElement, nextElement, nextEnum, nextLong, supportsMultipleSeedLengths, withProbability
-
-
-
-
Constructor Detail
-
EntropyBlockingSplittableRandomAdapter
public EntropyBlockingSplittableRandomAdapter(SimpleRandomSeeder randomSeeder, long minimumEntropy)
Creates an instance.- Parameters:
randomSeeder- theSimpleRandomSeederthat 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, SimpleRandomSeeder randomSeeder, long minimumEntropy)
Creates an instance.- Parameters:
seedGenerator- the seed generator that will generate an initial PRNG seed for each threadrandomSeeder- theSimpleRandomSeederthat 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 BaseRandom 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.- Specified by:
setSeedin interfaceJava8CompatRandom- Overrides:
setSeedin classBaseRandom
-
getSplittableRandom
protected java8.util.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.
-
-