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
ASplittableRandomAdapter
that blocks waiting to be reseeded if its entropy drops too low. Reseeding is done on aSimpleRandomSeeder
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(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 BaseRandom
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 java8.util.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, 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
- theSimpleRandomSeeder
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, SimpleRandomSeeder randomSeeder, long minimumEntropy)
Creates an instance.- Parameters:
seedGenerator
- the seed generator that will generate an initial PRNG seed for each threadrandomSeeder
- theSimpleRandomSeeder
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 BaseRandom 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.- Specified by:
setSeed
in interfaceJava8CompatRandom
- Overrides:
setSeed
in classBaseRandom
-
getSplittableRandom
protected java8.util.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.
-
-