Class XorShiftRandom
- java.lang.Object
-
- java.util.Random
-
- io.github.pr0methean.betterrandom.prng.BaseRandom
-
- io.github.pr0methean.betterrandom.prng.XorShiftRandom
-
- All Implemented Interfaces:
ByteArrayReseedableRandom
,EntropyCountingRandom
,RepeatableRandom
,Dumpable
,Serializable
public class XorShiftRandom extends BaseRandom
Very fast pseudo random number generator. See this page ( archive) for a description. This RNG has a period of about 2160, which is not as long as the
MersenneTwisterRandom
but it is faster.NOTE: Because instances of this class require 160-bit seeds, it is not possible to seed this RNG using the
setSeed(long)
method inherited fromRandom
. Calls to this method will have no effect. Instead the seed must be set by a constructor.- Since:
- 1.2
- Author:
- Daniel Dyer
- See Also:
- Serialized Form
-
-
Field Summary
-
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 XorShiftRandom()
Creates a new RNG and seeds it using theDefaultSeedGenerator
.XorShiftRandom(byte[] seed)
Creates an RNG and seeds it with the specified seed data.XorShiftRandom(SeedGenerator seedGenerator)
Seed the RNG using the provided seed generation strategy.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected MoreObjects.ToStringHelper
addSubclassFields(MoreObjects.ToStringHelper original)
Adds the fields that were not inherited from BaseRandom to the givenMoreObjects.ToStringHelper
for dumping.int
getNewSeedLength()
Returns the preferred length of a new byte-array seed.byte[]
getSeed()
Returns the seed.protected int
next(int bits)
Generates the next pseudorandom number.void
setSeed(long seed)
Reseeds this PRNG using theDefaultSeedGenerator
, since it needs a longer seed.protected void
setSeedInternal(byte[] seed)
Sets the seed, and should be overridden to set other state that derives from the seed.-
Methods inherited from class io.github.pr0methean.betterrandom.prng.BaseRandom
checkLength, checkValidRange, creditEntropyForNewSeed, debitEntropy, doubles, doubles, doubles, doubles, dump, entropyOfInt, entropyOfLong, fallbackSetSeedIfInitialized, gaussians, gaussians, getEntropyBits, getRandomSeeder, initTransientFields, internalNextGaussian, ints, ints, ints, ints, lockForNextGaussian, longs, longs, longs, longs, nextBoolean, nextBytes, nextDouble, nextDouble, nextDouble, nextDoubleNoEntropyDebit, nextElement, nextElement, nextEnum, nextFloat, nextGaussian, nextInt, nextInt, nextInt, nextLong, nextLong, nextLong, nextLongNoEntropyDebit, preferSeedWithLong, setRandomSeeder, setSeed, supportsMultipleSeedLengths, unlockForNextGaussian, usesParallelStreams, withProbability, withProbabilityInternal
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.github.pr0methean.betterrandom.EntropyCountingRandom
needsReseedingEarly
-
-
-
-
Constructor Detail
-
XorShiftRandom
public XorShiftRandom(byte[] seed)
Creates an RNG and seeds it with the specified seed data.- Parameters:
seed
- 20 bytes of seed data used to initialize the RNG.
-
XorShiftRandom
public XorShiftRandom() throws SeedException
Creates a new RNG and seeds it using theDefaultSeedGenerator
.- Throws:
SeedException
- if theDefaultSeedGenerator
fails to generate a seed.
-
XorShiftRandom
public XorShiftRandom(SeedGenerator seedGenerator) throws SeedException
Seed the RNG using the provided seed generation strategy.- Parameters:
seedGenerator
- The seed generation strategy that will provide the seed value for this RNG.- Throws:
SeedException
- if there is a problem generating a seed.
-
-
Method Detail
-
addSubclassFields
protected MoreObjects.ToStringHelper addSubclassFields(MoreObjects.ToStringHelper original)
Description copied from class:BaseRandom
Adds the fields that were not inherited from BaseRandom to the givenMoreObjects.ToStringHelper
for dumping.- Specified by:
addSubclassFields
in classBaseRandom
- Parameters:
original
- aMoreObjects.ToStringHelper
object.- Returns:
original
with the fields not inherited from BaseRandom written to it.
-
setSeed
public void setSeed(long seed)
Reseeds this PRNG using theDefaultSeedGenerator
, since it needs a longer seed.- Overrides:
setSeed
in classBaseRandom
- Parameters:
seed
- ignored- See Also:
ByteArrayReseedableRandom.preferSeedWithLong()
-
getSeed
public byte[] getSeed()
Description copied from interface:RepeatableRandom
Returns the seed.- Specified by:
getSeed
in interfaceRepeatableRandom
- Overrides:
getSeed
in classBaseRandom
- Returns:
- The seed data used to initialize this pseudo-random number generator.
-
setSeedInternal
protected void setSeedInternal(byte[] seed)
Description copied from class:BaseRandom
Sets 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.lock
is always held.- Overrides:
setSeedInternal
in classBaseRandom
- Parameters:
seed
- The new seed.
-
next
protected int next(int bits)
Description copied from class:BaseRandom
Generates the next pseudorandom number. Called by all other random-number-generating methods. Should not debit the entropy count, since that's done by the calling methods according to the amount they actually output (see for exampleBaseRandom.withProbability(double)
, which uses 53 random bits but outputs only one, and thus debits only 1 bit of entropy).- Specified by:
next
in classBaseRandom
-
getNewSeedLength
public int getNewSeedLength()
Description copied from interface:ByteArrayReseedableRandom
Returns the preferred length of a new byte-array seed. "Preferred" is implementation-defined when multiple seed lengths are supported, but should probably usually mean the longest one, since the longer the seed, the more random the output.- Specified by:
getNewSeedLength
in interfaceByteArrayReseedableRandom
- Specified by:
getNewSeedLength
in classBaseRandom
- Returns:
- The desired length of a new byte-array seed.
-
-