Class Random

java.lang.Object
smile.math.Random

public class Random extends Object
A high-quality random number generator that combines two complementary generators:
  • A UniversalGenerator (Marsaglia–Zaman–Tsang) for uniform floating-point values. It has a period of 2144 and passes all standard statistical tests.
  • A MersenneTwister (MT19937) for integer values. It has a period of 219937−1 and excellent equidistribution properties.
Both generators are seeded together by setSeed(long) so that the combined stream is reproducible from a single seed.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initialize with default random number generator engine.
    Random(long seed)
    Initialize with given seed for default random number generator engine.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns a random boolean value.
    double
    Generator a random number uniformly distributed in [0, 1).
    double
    nextDouble(double lo, double hi)
    Generate a uniform random number in the range [lo, hi).
    void
    nextDoubles(double[] d)
    Generate n uniform random numbers in the range [0, 1)
    void
    nextDoubles(double[] d, double lo, double hi)
    Generate n uniform random numbers in the range [lo, hi).
    float
    Returns a random float uniformly distributed in [0, 1).
    double
    Returns a random number from the standard normal distribution N(0, 1).
    double
    nextGaussian(double mu, double sigma)
    Returns a random number from the normal distribution N(mu, sigma).
    int
    Returns a random integer.
    int
    nextInt(int n)
    Returns a random integer in [0, n).
    long
    Returns a random long integer.
    void
    permutate(double[] x)
    Permutates an array in-place using the Fisher-Yates shuffle.
    void
    permutate(float[] x)
    Permutates an array in-place using the Fisher-Yates shuffle.
    int[]
    permutate(int n)
    Returns a permutation of (0, 1, 2, ..., n-1).
    void
    permutate(int[] x)
    Permutates an array in-place using the Fisher-Yates shuffle.
    void
    Permutates an array in-place using the Fisher-Yates shuffle.
    int[]
    sample(int n, int k)
    Randomly samples k distinct indices from the range [0, n) without replacement, using a partial Fisher-Yates shuffle.
    void
    setSeed(long seed)
    Initialize the random generator with a seed.

    Methods inherited from class Object

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

    • Random

      public Random()
      Initialize with default random number generator engine.
    • Random

      public Random(long seed)
      Initialize with given seed for default random number generator engine.
      Parameters:
      seed - the RNG seed.
  • Method Details

    • setSeed

      public void setSeed(long seed)
      Initialize the random generator with a seed.
      Parameters:
      seed - the RNG seed.
    • nextDouble

      public double nextDouble()
      Generator a random number uniformly distributed in [0, 1).
      Returns:
      a pseudo random number
    • nextDoubles

      public void nextDoubles(double[] d)
      Generate n uniform random numbers in the range [0, 1)
      Parameters:
      d - array of random numbers to be generated
    • nextDouble

      public double nextDouble(double lo, double hi)
      Generate a uniform random number in the range [lo, hi).
      Parameters:
      lo - lower limit of range (inclusive).
      hi - upper limit of range (exclusive).
      Returns:
      a uniform random real in the range [lo, hi).
      Throws:
      IllegalArgumentException - if lo >= hi.
    • nextDoubles

      public void nextDoubles(double[] d, double lo, double hi)
      Generate n uniform random numbers in the range [lo, hi).
      Parameters:
      d - array to fill with random numbers.
      lo - lower limit of range (inclusive).
      hi - upper limit of range (exclusive).
      Throws:
      IllegalArgumentException - if lo >= hi.
    • nextInt

      public int nextInt()
      Returns a random integer.
      Returns:
      a random integer.
    • nextInt

      public int nextInt(int n)
      Returns a random integer in [0, n).
      Parameters:
      n - the upper bound of random number (exclusive); must be positive.
      Returns:
      a random integer in [0, n).
    • nextLong

      public long nextLong()
      Returns a random long integer.
      Returns:
      a random long integer.
    • nextBoolean

      public boolean nextBoolean()
      Returns a random boolean value.
      Returns:
      true or false with equal probability.
    • nextFloat

      public float nextFloat()
      Returns a random float uniformly distributed in [0, 1).
      Returns:
      a random float.
    • permutate

      public int[] permutate(int n)
      Returns a permutation of (0, 1, 2, ..., n-1).
      Parameters:
      n - the upper bound.
      Returns:
      the permutation of (0, 1, 2, ..., n-1).
    • permutate

      public void permutate(int[] x)
      Permutates an array in-place using the Fisher-Yates shuffle.
      Parameters:
      x - the array.
    • permutate

      public void permutate(float[] x)
      Permutates an array in-place using the Fisher-Yates shuffle.
      Parameters:
      x - the array.
    • permutate

      public void permutate(double[] x)
      Permutates an array in-place using the Fisher-Yates shuffle.
      Parameters:
      x - the array.
    • permutate

      public void permutate(Object[] x)
      Permutates an array in-place using the Fisher-Yates shuffle.
      Parameters:
      x - the array.
    • nextGaussian

      public double nextGaussian()
      Returns a random number from the standard normal distribution N(0, 1). Uses the polar form of the Box-Muller transform; pairs of values are generated and the spare is cached for the next call.
      Returns:
      a standard normal random number.
    • nextGaussian

      public double nextGaussian(double mu, double sigma)
      Returns a random number from the normal distribution N(mu, sigma).
      Parameters:
      mu - the mean.
      sigma - the standard deviation.
      Returns:
      a normal random number.
    • sample

      public int[] sample(int n, int k)
      Randomly samples k distinct indices from the range [0, n) without replacement, using a partial Fisher-Yates shuffle. The result is returned in an unspecified order.
      Parameters:
      n - the population size; must be positive.
      k - the sample size; must satisfy 0 <= k <= n.
      Returns:
      an array of k distinct indices.
      Throws:
      IllegalArgumentException - if k < 0 or k > n.