Class ReseedingThreadLocalRandomWrapper<T extends BaseRandom>
- java.lang.Object
-
- java.util.Random
-
- io.github.pr0methean.betterrandom.prng.BaseRandom
-
- io.github.pr0methean.betterrandom.prng.adapter.RandomWrapper<T>
-
- io.github.pr0methean.betterrandom.prng.adapter.ThreadLocalRandomWrapper<T>
-
- io.github.pr0methean.betterrandom.prng.adapter.ReseedingThreadLocalRandomWrapper<T>
-
- All Implemented Interfaces:
ByteArrayReseedableRandom
,EntropyCountingRandom
,RepeatableRandom
,Dumpable
,Serializable
public class ReseedingThreadLocalRandomWrapper<T extends BaseRandom> extends ThreadLocalRandomWrapper<T>
AThreadLocalRandomWrapper
that reseeds all its instances using aRandomSeeder
.- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class io.github.pr0methean.betterrandom.prng.adapter.ThreadLocalRandomWrapper
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 ReseedingThreadLocalRandomWrapper(int seedSize, SeedGenerator seedGenerator, SerializableFunction<byte[],? extends T> creator)
Wraps a seed generator and a function that takes a seed byte array as input.ReseedingThreadLocalRandomWrapper(SeedGenerator seedGenerator, SerializableSupplier<? extends T> initializer)
Wraps the givenSerializableSupplier
.ReseedingThreadLocalRandomWrapper(SerializableSupplier<? extends T> initializer, RandomSeeder randomSeederThread)
Wraps the givenSerializableSupplier
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RandomSeeder
getRandomSeeder()
Returns the current seed generator for this PRNG.void
setRandomSeeder(RandomSeeder randomSeeder)
Not supported, because this class uses a thread-local seed.static ReseedingThreadLocalRandomWrapper<BaseRandom>
wrapLegacy(SerializableLongFunction<Random> legacyCreator, SeedGenerator seedGenerator)
Uses this class andRandomWrapper
to decorate any implementation ofRandom
that can be constructed from along
seed into a fully-concurrent one.-
Methods inherited from class io.github.pr0methean.betterrandom.prng.adapter.ThreadLocalRandomWrapper
addSubclassFields, debitEntropy, getEntropyBits, getNewSeedLength, getSeed, getWrapped, initTransientFields, nextBoolean, nextBytes, nextDouble, nextDoubleNoEntropyDebit, nextFloat, nextGaussian, nextInt, nextInt, nextInt, nextLong, nextLong, nextLong, nextLongNoEntropyDebit, preferSeedWithLong, setSeed, setSeedInternal, withProbability, withProbabilityInternal
-
Methods inherited from class io.github.pr0methean.betterrandom.prng.adapter.RandomWrapper
next, setInitiallyKnownSeed, setWrapped, supportsMultipleSeedLengths, toString, usesParallelStreams, wrapJavaUtilRandom, wrapJavaUtilRandom, wrapJavaUtilRandom, wrapSecureRandom
-
Methods inherited from class io.github.pr0methean.betterrandom.prng.BaseRandom
checkLength, checkValidRange, creditEntropyForNewSeed, doubles, doubles, doubles, doubles, dump, entropyOfInt, entropyOfLong, fallbackSetSeedIfInitialized, gaussians, gaussians, internalNextGaussian, ints, ints, ints, ints, lockForNextGaussian, longs, longs, longs, longs, nextDouble, nextDouble, nextElement, nextElement, nextEnum, setSeed, unlockForNextGaussian
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.github.pr0methean.betterrandom.EntropyCountingRandom
needsReseedingEarly
-
-
-
-
Constructor Detail
-
ReseedingThreadLocalRandomWrapper
public ReseedingThreadLocalRandomWrapper(SeedGenerator seedGenerator, SerializableSupplier<? extends T> initializer)
Wraps the givenSerializableSupplier
. Uses the givenRandomSeeder
to reseed PRNGs, but not to initialize them unless theSerializableSupplier
does so. This PRNG will be serializable as long as theSerializableSupplier
's captures are serializable.- Parameters:
initializer
- a supplier that will be called to provide the initialBaseRandom
for each thread.seedGenerator
- The seed generation strategy whoseRandomSeeder
will be used to reseed each thread's PRNG.
-
ReseedingThreadLocalRandomWrapper
public ReseedingThreadLocalRandomWrapper(SerializableSupplier<? extends T> initializer, RandomSeeder randomSeederThread)
Wraps the givenSerializableSupplier
. Uses the givenRandomSeeder
to reseed PRNGs, but not to initialize them unless theSerializableSupplier
does so. This PRNG will be serializable if theSerializableSupplier
's captures are serializable.- Parameters:
initializer
- a supplier that will be called to provide the initialBaseRandom
for each threadrandomSeederThread
- a random seeder that will reseed the PRNG for each thread when necessary
-
ReseedingThreadLocalRandomWrapper
public ReseedingThreadLocalRandomWrapper(int seedSize, SeedGenerator seedGenerator, SerializableFunction<byte[],? extends T> creator) throws SeedException
Wraps a seed generator and a function that takes a seed byte array as input. This ReseedingThreadLocalRandomWrapper will be serializable if theSerializableFunction
's captures are serializable.- Parameters:
seedSize
- the size of seed arrays to generate.seedGenerator
- The seed generation strategy that will provide the seed value for each thread'sBaseRandom
, both at initialization and through the correspondingRandomSeeder
.creator
- aSerializableFunction
that creates aBaseRandom
from each seed. Probably a constructor reference.- Throws:
SeedException
- ifseedGenerator
fails to generate an initial seed
-
-
Method Detail
-
wrapLegacy
public static ReseedingThreadLocalRandomWrapper<BaseRandom> wrapLegacy(SerializableLongFunction<Random> legacyCreator, SeedGenerator seedGenerator)
Uses this class andRandomWrapper
to decorate any implementation ofRandom
that can be constructed from along
seed into a fully-concurrent one.- Parameters:
legacyCreator
- a function that provides theRandom
that underlies the returned wrapper on each thread, taking a seed as input.seedGenerator
- the seed generator whose output will be fed tolegacyCreator
.- Returns:
- a ThreadLocalRandomWrapper decorating instances created by
legacyCreator
.
-
setRandomSeeder
public void setRandomSeeder(RandomSeeder randomSeeder)
Description copied from class:ThreadLocalRandomWrapper
Not supported, because this class uses a thread-local seed.- Overrides:
setRandomSeeder
in classThreadLocalRandomWrapper<T extends BaseRandom>
- Parameters:
randomSeeder
- ignored.
-
getRandomSeeder
public RandomSeeder getRandomSeeder()
Description copied from class:BaseRandom
Returns the current seed generator for this PRNG.- Overrides:
getRandomSeeder
in classThreadLocalRandomWrapper<T extends BaseRandom>
- Returns:
- the current seed generator, or null if there is none
-
-