Package com.badlogic.gdx.math
Class RandomXS128
- java.lang.Object
-
- java.util.Random
-
- com.badlogic.gdx.math.RandomXS128
-
- All Implemented Interfaces:
java.io.Serializable
public class RandomXS128 extends java.util.RandomThis class implements the xorshift128+ algorithm that is a very fast, top-quality 64-bit pseudo-random number generator. The quality of this PRNG is much higher thanRandom's, and its cycle length is 2128 − 1, which is more than enough for any single-thread application. More details and algorithms can be found here.Instances of RandomXS128 are not thread-safe.
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description RandomXS128()Creates a new random number generator.RandomXS128(long seed)Creates a new random number generator using a singlelongseed.RandomXS128(long seed0, long seed1)Creates a new random number generator using twolongseeds.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longgetState(int seed)Returns the internal seeds to allow state saving.protected intnext(int bits)This protected method is final because, contrary to the superclass, it's not used anymore by the other methods.booleannextBoolean()Returns a pseudo-random, uniformly distributedbooleanvalue from this random number generator's sequence.voidnextBytes(byte[] bytes)Generates random bytes and places them into a user-supplied byte array.doublenextDouble()Returns a pseudo-random, uniformly distributeddoublevalue between 0.0 and 1.0 from this random number generator's sequence.floatnextFloat()Returns a pseudo-random, uniformly distributedfloatvalue between 0.0 and 1.0 from this random number generator's sequence.intnextInt()Returns the next pseudo-random, uniformly distributedintvalue from this random number generator's sequence.intnextInt(int n)Returns a pseudo-random, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.longnextLong()Returns the next pseudo-random, uniformly distributedlongvalue from this random number generator's sequence.longnextLong(long n)Returns a pseudo-random, uniformly distributedlongvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.voidsetSeed(long seed)Sets the internal seed of this generator based on the givenlongvalue.voidsetState(long seed0, long seed1)Sets the internal state of this generator.
-
-
-
Constructor Detail
-
RandomXS128
public RandomXS128()
Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.This implementation creates a
Randominstance to generate the initial seed.
-
RandomXS128
public RandomXS128(long seed)
Creates a new random number generator using a singlelongseed.- Parameters:
seed- the initial seed
-
RandomXS128
public RandomXS128(long seed0, long seed1)Creates a new random number generator using twolongseeds.- Parameters:
seed0- the first part of the initial seedseed1- the second part of the initial seed
-
-
Method Detail
-
nextLong
public long nextLong()
Returns the next pseudo-random, uniformly distributedlongvalue from this random number generator's sequence.Subclasses should override this, as this is used by all other methods.
- Overrides:
nextLongin classjava.util.Random
-
next
protected final int next(int bits)
This protected method is final because, contrary to the superclass, it's not used anymore by the other methods.- Overrides:
nextin classjava.util.Random
-
nextInt
public int nextInt()
Returns the next pseudo-random, uniformly distributedintvalue from this random number generator's sequence.This implementation uses
nextLong()internally.- Overrides:
nextIntin classjava.util.Random
-
nextInt
public int nextInt(int n)
Returns a pseudo-random, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.This implementation uses
nextLong()internally.- Overrides:
nextIntin classjava.util.Random- Parameters:
n- the positive bound on the random number to be returned.- Returns:
- the next pseudo-random
intvalue between0(inclusive) andn(exclusive).
-
nextLong
public long nextLong(long n)
Returns a pseudo-random, uniformly distributedlongvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The algorithm used to generate the value guarantees that the result is uniform, provided that the sequence of 64-bit values produced by this generator is.This implementation uses
nextLong()internally.- Parameters:
n- the positive bound on the random number to be returned.- Returns:
- the next pseudo-random
longvalue between0(inclusive) andn(exclusive).
-
nextDouble
public double nextDouble()
Returns a pseudo-random, uniformly distributeddoublevalue between 0.0 and 1.0 from this random number generator's sequence.This implementation uses
nextLong()internally.- Overrides:
nextDoublein classjava.util.Random
-
nextFloat
public float nextFloat()
Returns a pseudo-random, uniformly distributedfloatvalue between 0.0 and 1.0 from this random number generator's sequence.This implementation uses
nextLong()internally.- Overrides:
nextFloatin classjava.util.Random
-
nextBoolean
public boolean nextBoolean()
Returns a pseudo-random, uniformly distributedbooleanvalue from this random number generator's sequence.This implementation uses
nextLong()internally.- Overrides:
nextBooleanin classjava.util.Random
-
nextBytes
public void nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.This implementation uses
nextLong()internally.- Overrides:
nextBytesin classjava.util.Random
-
setSeed
public void setSeed(long seed)
Sets the internal seed of this generator based on the givenlongvalue.The given seed is passed twice through a hash function. This way, if the user passes a small value we avoid the short irregular transient associated with states having a very small number of bits set.
- Overrides:
setSeedin classjava.util.Random- Parameters:
seed- a nonzero seed for this generator (if zero, the generator will be seeded withLong.MIN_VALUE).
-
setState
public void setState(long seed0, long seed1)Sets the internal state of this generator.- Parameters:
seed0- the first part of the internal stateseed1- the second part of the internal state
-
getState
public long getState(int seed)
Returns the internal seeds to allow state saving.- Parameters:
seed- must be 0 or 1, designating which of the 2 long seeds to return- Returns:
- the internal seed that can be used in setState
-
-