APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.math

public Class RandomXS128

extends Random
Class Inheritance
Imports
java.util.Random

This 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 than Random'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.

Authors
Inferno, davebaol

Field Summary

Modifier and TypeField and Description
private static final double
NORM_DOUBLE

Normalization constant for double.

private static final double
NORM_FLOAT

Normalization constant for float.

private long
seed0

The first half of the internal state of this pseudo-random number generator.

private long
seed1

The second half of the internal state of this pseudo-random number generator.

Constructor Summary

AccessConstructor and Description
public
RandomXS128()

Creates a new random number generator.

public
RandomXS128(long
the initial seed
seed
)

Creates a new random number generator using a single long seed.

public
RandomXS128(long
the first part of the initial seed
seed0
,
long
the second part of the initial seed
seed1
)

Creates a new random number generator using two long seeds.

Method Summary

Modifier and TypeMethod and Description
public long

Returns:

the internal seed that can be used in setState
getState
(int
must be 0 or 1, designating which of the 2 long seeds to return
seed
)

Returns the internal seeds to allow state saving.

private static final long
murmurHash3(long x)

protected final int
next(int bits)

Overrides java.util.Random.next.

This protected method is final because, contrary to the superclass, it's not used anymore by the other methods.

public boolean
nextBoolean()

Overrides java.util.Random.nextBoolean.

Overrides default java.util.random.RandomGenerator.nextBoolean.

Returns a pseudo-random, uniformly distributed boolean value from this random number generator's sequence.

public void
nextBytes(final byte[] bytes)

Overrides java.util.Random.nextBytes.

Overrides default java.util.random.RandomGenerator.nextBytes.

Generates random bytes and places them into a user-supplied byte array.

public double
nextDouble()

Overrides java.util.Random.nextDouble.

Overrides default java.util.random.RandomGenerator.nextDouble.

Returns a pseudo-random, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

public float
nextFloat()

Overrides java.util.Random.nextFloat.

Overrides default java.util.random.RandomGenerator.nextFloat.

Returns a pseudo-random, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

public int
nextInt()

Overrides java.util.Random.nextInt.

Overrides default java.util.random.RandomGenerator.nextInt.

Returns the next pseudo-random, uniformly distributed int value from this random number generator's sequence.

public int

Returns:

the next pseudo-random int value between 0 (inclusive) and n (exclusive).
nextInt
(final int
the positive bound on the random number to be returned.
n
)

Overrides java.util.Random.nextInt.

Overrides default java.util.random.RandomGenerator.nextInt.

Returns a pseudo-random, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

public long
nextLong()

Overrides java.util.Random.nextLong.

Implements java.util.random.RandomGenerator.nextLong.

Returns the next pseudo-random, uniformly distributed long value from this random number generator's sequence.

public long

Returns:

the next pseudo-random long value between 0 (inclusive) and n (exclusive).
nextLong
(final long
the positive bound on the random number to be returned.
n
)

Overrides default java.util.random.RandomGenerator.nextLong.

Returns a pseudo-random, uniformly distributed long value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

public void
setSeed(final long
a nonzero seed for this generator (if zero, the generator will be seeded with Long#MIN_VALUE).
seed
)

Overrides java.util.Random.setSeed.

Sets the internal seed of this generator based on the given long value.

public void
setState(final long
the first part of the internal state
seed0
,
final long
the second part of the internal state
seed1
)

Sets the internal state of this generator.

Inherited from java.util.Random:
doublesdoublesdoublesdoublesfromintsintsintsintslongslongslongslongsnextGaussian

Field Detail

NORM_DOUBLEback to summary
private static final double NORM_DOUBLE

Normalization constant for double.

NORM_FLOATback to summary
private static final double NORM_FLOAT

Normalization constant for float.

seed0back to summary
private long seed0

The first half of the internal state of this pseudo-random number generator.

seed1back to summary
private long seed1

The second half of the internal state of this pseudo-random number generator.

Constructor Detail

RandomXS128back to summary
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 Random instance to generate the initial seed.

RandomXS128back to summary
public RandomXS128(long seed)

Creates a new random number generator using a single long seed.

Parameters
seed:long

the initial seed

RandomXS128back to summary
public RandomXS128(long seed0, long seed1)

Creates a new random number generator using two long seeds.

Parameters
seed0:long

the first part of the initial seed

seed1:long

the second part of the initial seed

Method Detail

getStateback to summary
public long getState(int seed)

Returns the internal seeds to allow state saving.

Parameters
seed:int

must be 0 or 1, designating which of the 2 long seeds to return

Returns:long

the internal seed that can be used in setState

murmurHash3back to summary
private static final long murmurHash3(long x)
nextback to summary
protected final int next(int bits)

Overrides java.util.Random.next.

This protected method is final because, contrary to the superclass, it's not used anymore by the other methods.

Annotations
@Override
nextBooleanback to summary
public boolean nextBoolean()

Overrides java.util.Random.nextBoolean.

Overrides default java.util.random.RandomGenerator.nextBoolean.

Returns a pseudo-random, uniformly distributed boolean value from this random number generator's sequence.

This implementation uses nextLong() internally.

Annotations
@Override
nextBytesback to summary
public void nextBytes(final byte[] bytes)

Overrides java.util.Random.nextBytes.

Overrides default java.util.random.RandomGenerator.nextBytes.

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.

Annotations
@Override
nextDoubleback to summary
public double nextDouble()

Overrides java.util.Random.nextDouble.

Overrides default java.util.random.RandomGenerator.nextDouble.

Returns a pseudo-random, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

This implementation uses nextLong() internally.

Annotations
@Override
nextFloatback to summary
public float nextFloat()

Overrides java.util.Random.nextFloat.

Overrides default java.util.random.RandomGenerator.nextFloat.

Returns a pseudo-random, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

This implementation uses nextLong() internally.

Annotations
@Override
nextIntback to summary
public int nextInt()

Overrides java.util.Random.nextInt.

Overrides default java.util.random.RandomGenerator.nextInt.

Returns the next pseudo-random, uniformly distributed int value from this random number generator's sequence.

This implementation uses nextLong() internally.

Annotations
@Override
nextIntback to summary
public int nextInt(final int n)

Overrides java.util.Random.nextInt.

Overrides default java.util.random.RandomGenerator.nextInt.

Returns a pseudo-random, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

This implementation uses nextLong() internally.

Parameters
n:int

the positive bound on the random number to be returned.

Returns:int

the next pseudo-random int value between 0 (inclusive) and n (exclusive).

Annotations
@Override
nextLongback to summary
public long nextLong()

Overrides java.util.Random.nextLong.

Implements java.util.random.RandomGenerator.nextLong.

Returns the next pseudo-random, uniformly distributed long value from this random number generator's sequence.

Subclasses should override this, as this is used by all other methods.

Annotations
@Override
nextLongback to summary
public long nextLong(final long n)

Overrides default java.util.random.RandomGenerator.nextLong.

Returns a pseudo-random, uniformly distributed long value 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:long

the positive bound on the random number to be returned.

Returns:long

the next pseudo-random long value between 0 (inclusive) and n (exclusive).

setSeedback to summary
public void setSeed(final long seed)

Overrides java.util.Random.setSeed.

Sets the internal seed of this generator based on the given long value.

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.

Parameters
seed:long

a nonzero seed for this generator (if zero, the generator will be seeded with Long#MIN_VALUE).

Annotations
@Override
setStateback to summary
public void setState(final long seed0, final long seed1)

Sets the internal state of this generator.

Parameters
seed0:long

the first part of the internal state

seed1:long

the second part of the internal state