Class ReseedingThreadLocalRandomWrapper
- java.lang.Object
-
- java.util.Random
-
- io.github.pr0methean.betterrandom.prng.BaseRandom
-
- io.github.pr0methean.betterrandom.prng.adapter.RandomWrapper
-
- io.github.pr0methean.betterrandom.prng.adapter.ThreadLocalRandomWrapper
-
- io.github.pr0methean.betterrandom.prng.adapter.ReseedingThreadLocalRandomWrapper
-
- All Implemented Interfaces:
ByteArrayReseedableRandom
,EntropyCountingRandom
,Java8CompatRandom
,RepeatableRandom
,Dumpable
,Serializable
public class ReseedingThreadLocalRandomWrapper extends ThreadLocalRandomWrapper
AThreadLocalRandomWrapper
that reseeds all its instances using aSimpleRandomSeeder
.- 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, java8.util.function.Function<byte[],? extends BaseRandom> creator)
Wraps a seed generator and a function that takes a seed byte array as input.ReseedingThreadLocalRandomWrapper(int seedSize, SimpleRandomSeeder randomSeederThread, java8.util.function.Function<byte[],? extends BaseRandom> creator, SeedGenerator seedGenerator)
Wraps a seed generator and a function that takes a seed byte array as input.ReseedingThreadLocalRandomWrapper(SeedGenerator seedGenerator, java8.util.function.Supplier<? extends BaseRandom> initializer)
Wraps the givenSupplier
.ReseedingThreadLocalRandomWrapper(java8.util.function.Supplier<? extends BaseRandom> initializer, SimpleRandomSeeder randomSeederThread)
Wraps the givenSupplier
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description SimpleRandomSeeder
getRandomSeeder()
Returns the current seed generator for this PRNG.void
setRandomSeeder(SimpleRandomSeeder randomSeeder)
Not supported, because this class uses a thread-local seed.static ReseedingThreadLocalRandomWrapper
wrapLegacy(java8.util.function.LongFunction<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, nextBoolean, nextBytes, nextDouble, nextDoubleNoEntropyDebit, nextFloat, nextGaussian, nextInt, nextInt, nextInt, nextLong, nextLong, nextLong, nextLongNoEntropyDebit, preferSeedWithLong, setSeed, setSeedInternal, withProbability, withProbabilityInternal, wrapLongCreatorAsByteArrayCreator
-
Methods inherited from class io.github.pr0methean.betterrandom.prng.adapter.RandomWrapper
next, setWrapped, supportsMultipleSeedLengths, toString, usesParallelStreams
-
Methods inherited from class io.github.pr0methean.betterrandom.prng.BaseRandom
checkLength, creditEntropyForNewSeed, doubles, doubles, doubles, doubles, dump, entropyOfInt, entropyOfLong, fallbackSetSeedIfInitialized, gaussians, gaussians, initTransientFields, internalNextGaussian, ints, ints, ints, ints, lockForNextGaussian, longs, longs, longs, longs, needsReseedingEarly, nextDouble, nextDouble, nextElement, nextElement, nextEnum, setSeed, unlockForNextGaussian
-
-
-
-
Constructor Detail
-
ReseedingThreadLocalRandomWrapper
public ReseedingThreadLocalRandomWrapper(SeedGenerator seedGenerator, java8.util.function.Supplier<? extends BaseRandom> initializer)
Wraps the givenSupplier
. Uses the givenSimpleRandomSeeder
to reseed PRNGs, but not to initialize them unless theSupplier
does so. This ThreadLocalRandomWrapper will be serializable if theSupplier
is serializable.- Parameters:
initializer
- a supplier that will be called to provide the initialBaseRandom
for each thread.seedGenerator
- The seed generation strategy whoseSimpleRandomSeeder
will be used to reseed each thread's PRNG.
-
ReseedingThreadLocalRandomWrapper
public ReseedingThreadLocalRandomWrapper(java8.util.function.Supplier<? extends BaseRandom> initializer, SimpleRandomSeeder randomSeederThread)
Wraps the givenSupplier
. Uses the givenSimpleRandomSeeder
to reseed PRNGs, but not to initialize them unless theSupplier
does so. This ThreadLocalRandomWrapper will be serializable if theSupplier
is 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, java8.util.function.Function<byte[],? extends BaseRandom> creator) throws SeedException
Wraps a seed generator and a function that takes a seed byte array as input. This ReseedingThreadLocalRandomWrapper will be serializable if theFunction
is 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 correspondingSimpleRandomSeeder
.creator
- aFunction
that creates aBaseRandom
from each seed. Probably a constructor reference.- Throws:
SeedException
- ifseedGenerator
fails to generate an initial seed
-
ReseedingThreadLocalRandomWrapper
public ReseedingThreadLocalRandomWrapper(int seedSize, SimpleRandomSeeder randomSeederThread, java8.util.function.Function<byte[],? extends BaseRandom> creator, SeedGenerator seedGenerator) throws SeedException
Wraps a seed generator and a function that takes a seed byte array as input. This ReseedingThreadLocalRandomWrapper will be serializable if theFunction
is serializable.- Parameters:
seedSize
- the size of seed arrays to generate.randomSeederThread
- The random seeder to use for reseeding.creator
- aFunction
that creates aBaseRandom
from each seed.seedGenerator
- the seed generator for initialization.- Throws:
SeedException
- ifseedGenerator
fails to generate an initial seed
-
-
Method Detail
-
wrapLegacy
public static ReseedingThreadLocalRandomWrapper wrapLegacy(java8.util.function.LongFunction<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(SimpleRandomSeeder randomSeeder)
Description copied from class:ThreadLocalRandomWrapper
Not supported, because this class uses a thread-local seed.- Overrides:
setRandomSeeder
in classThreadLocalRandomWrapper
- Parameters:
randomSeeder
- ignored.
-
getRandomSeeder
public SimpleRandomSeeder getRandomSeeder()
Description copied from class:BaseRandom
Returns the current seed generator for this PRNG.- Overrides:
getRandomSeeder
in classThreadLocalRandomWrapper
- Returns:
- the current seed generator, or null if there is none
-
-