Package arc.math

Class Rand

All Implemented Interfaces:
Serializable

public class Rand extends 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.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    long
    The first half of the internal state of this pseudo-random number generator.
    long
    The second half of the internal state of this pseudo-random number generator.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new random number generator.
    Rand(long seed)
    Creates a new random number generator using a single long seed.
    Rand(long seed0, long seed1)
    Creates a new random number generator using two long seeds.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    chance(double chance)
     
    long
    getState(int seed)
    Returns the internal seeds to allow state saving.
    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.
    boolean
    Returns a pseudo-random, uniformly distributed boolean value from this random number generator's sequence.
    void
    nextBytes(byte[] bytes)
    Generates random bytes and places them into a user-supplied byte array.
    double
    Returns a pseudo-random, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
    float
    Returns a pseudo-random, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
    int
    Returns the next pseudo-random, uniformly distributed int value from this random number generator's sequence.
    int
    nextInt(int n)
    Returns a pseudo-random, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
    long
    Returns the next pseudo-random, uniformly distributed long value from this random number generator's sequence.
    long
    nextLong(long n)
    Returns a pseudo-random, uniformly distributed long value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
    float
    random(float max)
     
    float
    random(float min, float max)
     
    int
    random(int max)
    Inclusive.
    int
    random(int min, int max)
     
    float
    range(float amount)
     
    int
    range(int amount)
     
    void
    setSeed(long seed)
    Sets the internal seed of this generator based on the given long value.
    void
    setState(long seed0, long seed1)
    Sets the internal state of this generator.

    Methods inherited from class java.util.Random

    doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longs, nextGaussian

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • seed0

      public long seed0
      The first half of the internal state of this pseudo-random number generator.
    • seed1

      public long seed1
      The second half of the internal state of this pseudo-random number generator.
  • Constructor Details

    • Rand

      public Rand()
      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.

    • Rand

      public Rand(long seed)
      Creates a new random number generator using a single long seed.
      Parameters:
      seed - the initial seed
    • Rand

      public Rand(long seed0, long seed1)
      Creates a new random number generator using two long seeds.
      Parameters:
      seed0 - the first part of the initial seed
      seed1 - the second part of the initial seed
  • Method Details

    • nextLong

      public long 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.

      Overrides:
      nextLong in class 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:
      next in class Random
    • nextInt

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

      This implementation uses nextLong() internally.

      Overrides:
      nextInt in class Random
    • nextInt

      public int nextInt(int n)
      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.

      Overrides:
      nextInt in class Random
      Parameters:
      n - the positive bound on the random number to be returned.
      Returns:
      the next pseudo-random int value between 0 (inclusive) and n (exclusive).
    • nextLong

      public long nextLong(long n)
      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 - the positive bound on the random number to be returned.
      Returns:
      the next pseudo-random long value between 0 (inclusive) and n (exclusive).
    • nextDouble

      public double 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.

      Overrides:
      nextDouble in class Random
    • nextFloat

      public float 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.

      Overrides:
      nextFloat in class Random
    • nextBoolean

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

      This implementation uses nextLong() internally.

      Overrides:
      nextBoolean in class 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:
      nextBytes in class Random
    • setSeed

      public void setSeed(long seed)
      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.

      Overrides:
      setSeed in class Random
      Parameters:
      seed - a nonzero seed for this generator (if zero, the generator will be seeded with Long.MIN_VALUE).
    • chance

      public boolean chance(double chance)
    • range

      public float range(float amount)
    • random

      public float random(float max)
    • random

      public int random(int max)
      Inclusive.
    • random

      public float random(float min, float max)
    • range

      public int range(int amount)
    • random

      public int random(int min, int max)
    • setState

      public void setState(long seed0, long seed1)
      Sets the internal state of this generator.
      Parameters:
      seed0 - the first part of the internal state
      seed1 - 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