Class SharedRandom
java.lang.Object
io.github.cjstehno.testthings.rando.SharedRandom
- All Implemented Interfaces:
RandomGenerator
A thread-safe "random" number generated similar to the
ThreadLocalRandom
, but with the
ability to have its seed changed after construction.
The ability to know and change the seed at runtime is very useful in allowing for repeatable testing of "random"-based
utilities. If you use the same seed, the same values will be generated in the same order.
The seed may be injected programmatically, or a system property may be set ("test-tings.rando.seed") which will
specify the seed for the whole JVM.
NOTE: Setting the seed to a known value should ONLY be used for development and testing purposes.-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.random.RandomGenerator
RandomGenerator.ArbitrarilyJumpableGenerator, RandomGenerator.JumpableGenerator, RandomGenerator.LeapableGenerator, RandomGenerator.SplittableGenerator, RandomGenerator.StreamableGenerator
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The System property which may be used to specify a custom seed value (e.g "test-things.rando.seed"). -
Method Summary
Modifier and TypeMethodDescriptionstatic RandomGenerator
current()
Retrieves the singleton instance of the RandomGenerator for the current thread.static SharedRandom
Builds a SharedRandom with the default seed.static SharedRandom
Builds a SharedRandom with the specified seed.long
nextLong()
void
reseed
(long newSeed) Updates the seed value and rebuilds the internal random generator.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.random.RandomGenerator
doubles, doubles, doubles, doubles, ints, ints, ints, ints, isDeprecated, longs, longs, longs, longs, nextBoolean, nextBytes, nextDouble, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextFloat, nextGaussian, nextGaussian, nextInt, nextInt, nextInt, nextLong, nextLong
-
Field Details
-
SEED_PROPERTY
The System property which may be used to specify a custom seed value (e.g "test-things.rando.seed").- See Also:
-
-
Method Details
-
current
Retrieves the singleton instance of the RandomGenerator for the current thread.- Returns:
- the random generator instance
-
nextLong
public long nextLong()- Specified by:
nextLong
in interfaceRandomGenerator
-
reseed
public void reseed(long newSeed) Updates the seed value and rebuilds the internal random generator.- Parameters:
newSeed
- the new seed value
-
generator
Builds a SharedRandom with the specified seed.- Parameters:
seed
- the seed- Returns:
- the SharedRandom instance
-
generator
Builds a SharedRandom with the default seed.- Returns:
- the SharedRandom instance
-