Class Random
java.lang.Object
smile.math.Random
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.
setSeed(long) so that the
combined stream is reproducible from a single seed.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns a random boolean value.doubleGenerator a random number uniformly distributed in [0, 1).doublenextDouble(double lo, double hi) Generate a uniform random number in the range [lo, hi).voidnextDoubles(double[] d) Generate n uniform random numbers in the range [0, 1)voidnextDoubles(double[] d, double lo, double hi) Generate n uniform random numbers in the range [lo, hi).floatReturns a random float uniformly distributed in [0, 1).doubleReturns a random number from the standard normal distribution N(0, 1).doublenextGaussian(double mu, double sigma) Returns a random number from the normal distribution N(mu, sigma).intnextInt()Returns a random integer.intnextInt(int n) Returns a random integer in [0, n).longnextLong()Returns a random long integer.voidpermutate(double[] x) Permutates an array in-place using the Fisher-Yates shuffle.voidpermutate(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).voidpermutate(int[] x) Permutates an array in-place using the Fisher-Yates shuffle.voidPermutates an array in-place using the Fisher-Yates shuffle.int[]sample(int n, int k) Randomly sampleskdistinct indices from the range[0, n)without replacement, using a partial Fisher-Yates shuffle.voidsetSeed(long seed) Initialize the random generator with a seed.
-
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- iflo >= 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- iflo >= 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:
trueorfalsewith 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
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 sampleskdistinct 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 satisfy0 <= k <= n.- Returns:
- an array of
kdistinct indices. - Throws:
IllegalArgumentException- ifk < 0ork > n.
-