Class Pcg64Random
- java.lang.Object
-
- java.util.Random
-
- io.github.pr0methean.betterrandom.prng.BaseRandom
-
- io.github.pr0methean.betterrandom.prng.Pcg64Random
-
- All Implemented Interfaces:
ByteArrayReseedableRandom,EntropyCountingRandom,RepeatableRandom,SeekableRandom,Dumpable,Serializable
public class Pcg64Random extends BaseRandom implements SeekableRandom
From the original description, "PCG is a family of simple fast space-efficient statistically good algorithms for random number generation. Unlike many general-purpose RNGs, they are also hard to predict." This is a Java port of the "XSH RR 64/32" generator presented at http://www.pcg-random.org/. Period is 262 bits. This PRNG is seekable.
Sharing a single instance across threads that are frequently using it concurrently isn't recommended, unless memory is too constrained to use with a
ThreadLocalRandomWrapper.- Author:
- M.E. O'Neill (algorithm and C++ implementation), Chris Hennick (Java port)
- 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 Pcg64Random()Creates a new PRNG with a seed from theDefaultSeedGenerator.Pcg64Random(byte[] seed)Creates a new PRNG with the provided seed.Pcg64Random(long seed)Creates a new PRNG with the provided seed.Pcg64Random(SeedGenerator seedGenerator)Creates a new PRNG with a seed from the providedSeedGenerator.
-
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.voidadvance(long delta)Advances the generator forwarddeltasteps, but does so in logarithmic time.intgetNewSeedLength()Returns the preferred length of a new byte-array seed.byte[]getSeed()Returns the seed.protected intnext(int bits)Generates the next pseudorandom number.protected longnextLongNoEntropyDebit()Returns the next randomlong, but does not debit entropy.voidsetSeed(long seed)Sets the seed of this random number generator using a single long seed, if this implementation supports that.voidsetSeedInternal(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, 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
-
Pcg64Random
public Pcg64Random()
Creates a new PRNG with a seed from theDefaultSeedGenerator.
-
Pcg64Random
public Pcg64Random(SeedGenerator seedGenerator) throws SeedException
Creates a new PRNG with a seed from the providedSeedGenerator.- Parameters:
seedGenerator- the seed generator that will generate the initial seed- Throws:
SeedException- ifseedGeneratorfails to generate a seed
-
Pcg64Random
public Pcg64Random(byte[] seed)
Creates a new PRNG with the provided seed.- Parameters:
seed- the seed; must be exactly 8 bytes
-
Pcg64Random
public Pcg64Random(long seed)
Creates a new PRNG with the provided seed.- Parameters:
seed- the seed
-
-
Method Detail
-
nextLongNoEntropyDebit
protected long nextLongNoEntropyDebit()
Description copied from class:BaseRandomReturns the next randomlong, but does not debit entropy.- Overrides:
nextLongNoEntropyDebitin classBaseRandom- Returns:
- a pseudorandom
longwith all possible values equally likely.
-
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.
-
setSeed
public void setSeed(long seed)
Description copied from class:BaseRandomSets the seed of this random number generator using a single long seed, if this implementation supports that. If it is capable of using 64 bits or less of seed data (i.e. if{@link #getNewSeedLength()} <= {@link Long#BYTES}), then this method shall replace the entire seed asRandom.setSeed(long)does; otherwise, it shall either combine the input with the existing seed asSecureRandom.setSeed(long)does, or it shall generate a new seed using theDefaultSeedGenerator. The latter is a backward-compatibility measure and can be very slow.- Overrides:
setSeedin classBaseRandom- See Also:
ByteArrayReseedableRandom.preferSeedWithLong()
-
advance
public void advance(long delta)
Description copied from interface:SeekableRandomAdvances the generator forwarddeltasteps, but does so in logarithmic time.- Specified by:
advancein interfaceSeekableRandom- Parameters:
delta- the number of steps to advance; can be negative
-
setSeedInternal
public 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.
-
next
protected int next(int bits)
Description copied from class:BaseRandomGenerates 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:
nextin classBaseRandom
-
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.
-
getNewSeedLength
public int getNewSeedLength()
Description copied from interface:ByteArrayReseedableRandomReturns 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:
getNewSeedLengthin interfaceByteArrayReseedableRandom- Specified by:
getNewSeedLengthin classBaseRandom- Returns:
- The desired length of a new byte-array seed.
-
-