Class SplittableRandomAdapter
- 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
-
- All Implemented Interfaces:
ByteArrayReseedableRandom,EntropyCountingRandom,RepeatableRandom,Dumpable,Serializable
- Direct Known Subclasses:
EntropyBlockingSplittableRandomAdapter
public class SplittableRandomAdapter extends BaseSplittableRandomAdapter
Thread-safe PRNG that wraps aThreadLocal<SplittableRandom>. Registers each thread's instance with aRandomSeederto replace itsSplittableRandomwith a reseeded one as frequently as possible, but not more frequently than it is being used.In OpenJDK 8 and Android API 24 and later,
ThreadLocalRandomuses the same PRNG algorithm asSplittableRandom, and is faster because of internal coupling withThread. As well, the instance returned byThreadLocalRandom.current()can be safely passed to any thread that has ever calledcurrent(), and streams created by a ThreadLocalRandom are safely parallel. Thus, this class should only be used when reseeding or the ability to specify a seed is required, or for compatibility with JDK 7 or an older Android version.- Author:
- Chris Hennick
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected ThreadLocal<BaseRandom>threadLocalA thread-local delegate.-
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 SplittableRandomAdapter(SeedGenerator seedGenerator)Creates an instance that uses the sameSeedGeneratorfor reseeding and for initial seeding, and whoseRandomSeederuses aRandomSeeder.DefaultThreadFactory.SplittableRandomAdapter(SeedGenerator seedGenerator, RandomSeeder randomSeeder)Creates an instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected MoreObjects.ToStringHelperaddSubclassFields(MoreObjects.ToStringHelper original)Adds the fields that were not inherited from BaseRandom to the givenMoreObjects.ToStringHelperfor dumping.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.booleanequals(Object o)longgetEntropyBits()Returns an estimate of the current amount of entropy.byte[]getSeed()Returns the seed.protected SplittableRandomgetSplittableRandom()Returns theSplittableRandomthat is to be used to generate random numbers for the current thread.inthashCode()protected voidinitTransientFields()Called in constructor and readObject to initialize transient fields.doublenextGaussian()voidsetRandomSeeder(RandomSeeder randomSeeder)Registers this PRNG with theRandomSeederfor the correspondingSeedGenerator, to schedule reseeding when we run out of entropy.protected voidsetSeedInternal(byte[] seed)Sets the seed, and should be overridden to set other state that derives from the seed.StringtoString()booleanusesParallelStreams()-
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, setSeed, setSeed, 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
-
-
-
-
Field Detail
-
threadLocal
protected transient ThreadLocal<BaseRandom> threadLocal
A thread-local delegate.
-
-
Constructor Detail
-
SplittableRandomAdapter
public SplittableRandomAdapter(SeedGenerator seedGenerator, @Nullable RandomSeeder randomSeeder) throws SeedException
Creates an instance.- Parameters:
seedGenerator- the seed generator that will generate an initial seed for each threadrandomSeeder- theRandomSeederthat will generate a seed for a newSplittableRandominstance whenever each thread's instance needs reseeding- Throws:
SeedException- ifseedGeneratorfails to generate an initial seed
-
SplittableRandomAdapter
public SplittableRandomAdapter(SeedGenerator seedGenerator)
Creates an instance that uses the sameSeedGeneratorfor reseeding and for initial seeding, and whoseRandomSeederuses aRandomSeeder.DefaultThreadFactory.- Parameters:
seedGenerator- the seed generator that will generate an initial seed for each thread- Throws:
SeedException- ifseedGeneratorfails to generate an initial seed
-
-
Method Detail
-
initTransientFields
protected void initTransientFields()
Description copied from class:BaseRandomCalled in constructor and readObject to initialize transient fields.- Overrides:
initTransientFieldsin classBaseRandom
-
createDelegate
protected BaseRandom createDelegate()
Creates the delegate for the calling thread.- Returns:
- the thread-local delegate
-
getEntropyBits
public long getEntropyBits()
Description copied from interface:EntropyCountingRandomReturns an estimate of the current amount of entropy. Every time the PRNG is reseeded, the entropy count is set to the new seed's length; and every time it is used, it is decreased by the number of random bits in the output rounded up. The amount of entropy can go below zero, giving an indication of how far the entropy has been stretched. This estimate is a lower bound if the seed is perfectly random and is not being reused.- Specified by:
getEntropyBitsin interfaceEntropyCountingRandom- Overrides:
getEntropyBitsin classBaseRandom- Returns:
- The current estimated amount of entropy.
-
getSeed
public byte[] getSeed()
Description copied from interface:RepeatableRandomReturns the seed.- Specified by:
getSeedin interfaceRepeatableRandom- Overrides:
getSeedin classBaseRandom- Returns:
- The seed data used to initialize this pseudo-random number generator.
-
setRandomSeeder
public void setRandomSeeder(@Nullable RandomSeeder randomSeeder)
Description copied from class:BaseRandomRegisters this PRNG with theRandomSeederfor the correspondingSeedGenerator, to schedule reseeding when we run out of entropy. Unregisters this PRNG with the previousRandomSeederif it had a different one.- Overrides:
setRandomSeederin classBaseRandom- Parameters:
randomSeeder- aSeedGeneratorwhoseRandomSeederwill be used to reseed this PRNG, or null to stop using one.
-
usesParallelStreams
public boolean usesParallelStreams()
- Overrides:
usesParallelStreamsin classBaseRandom- Returns:
- true if this PRNG creates parallel streams; false otherwise.
-
addSubclassFields
protected MoreObjects.ToStringHelper addSubclassFields(MoreObjects.ToStringHelper original)
Description copied from class:BaseRandomAdds the fields that were not inherited from BaseRandom to the givenMoreObjects.ToStringHelperfor dumping.- Specified by:
addSubclassFieldsin classBaseRandom- Parameters:
original- aMoreObjects.ToStringHelperobject.- Returns:
originalwith the fields not inherited from BaseRandom written to it.
-
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.- Specified by:
getSplittableRandomin classBaseSplittableRandomAdapter- Returns:
- the
SplittableRandomto use with the current thread.
-
nextGaussian
public double nextGaussian()
Description copied from class:BaseSplittableRandomAdapterDelegates toSplittableRandom.nextDouble()viaBaseRandom.internalNextGaussian(java.util.function.DoubleSupplier).- Overrides:
nextGaussianin classBaseSplittableRandomAdapter
-
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 classBaseRandom- Parameters:
bits- The number of bits of entropy spent.
-
setSeedInternal
protected void setSeedInternal(byte[] seed)
Description copied from class:BaseRandomSets the seed, and should be overridden to set other state that derives from the seed. Called byBaseRandom.setSeed(byte[]), constructors, andreadObject(ObjectInputStream). When called after initialization, theBaseRandom.lockis always held.- Overrides:
setSeedInternalin classBaseRandom- Parameters:
seed- The new seed.
-
-