public final class StdRandom
extends java.lang.Object
StdRandom
class provides static methods for generating
random number from various discrete and continuous distributions,
including Bernoulli, uniform, Gaussian, exponential, pareto,
Poisson, and Cauchy. It also provides method for shuffling an
array or subarray.
For additional documentation, see Section 2.2 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
Modifier and Type | Method and Description |
---|---|
static boolean |
bernoulli()
Returns a random boolean from a Bernoulli distribution with success
probability 1/2.
|
static boolean |
bernoulli(double p)
Returns a random boolean from a Bernoulli distribution with success
probability p.
|
static double |
cauchy()
Returns a random real number from the Cauchy distribution.
|
static int |
discrete(double[] probabilities)
Returns a random integer from the specified discrete distribution.
|
static int |
discrete(int[] frequencies)
Returns a random integer from the specified discrete distribution.
|
static double |
exp(double lambda)
Returns a random real number from an exponential distribution
with rate λ.
|
static double |
gaussian()
Returns a random real number from a standard Gaussian distribution.
|
static double |
gaussian(double mu,
double sigma)
Returns a random real number from a Gaussian distribution with mean μ
and standard deviation σ.
|
static int |
geometric(double p)
Returns a random integer from a geometric distribution with success
probability p.
|
static long |
getSeed()
Returns the seed of the pseudorandom number generator.
|
static double |
pareto()
Returns a random real number from the standard Pareto distribution.
|
static double |
pareto(double alpha)
Returns a random real number from a Pareto distribution with
shape parameter α.
|
static int |
poisson(double lambda)
Returns a random integer from a Poisson distribution with mean λ.
|
static double |
random()
Deprecated.
Replaced by
uniform() . |
static void |
setSeed(long s)
Sets the seed of the pseudorandom number generator.
|
static void |
shuffle(double[] a)
Rearranges the elements of the specified array in uniformly random order.
|
static void |
shuffle(double[] a,
int lo,
int hi)
Rearranges the elements of the specified subarray in uniformly random order.
|
static void |
shuffle(int[] a)
Rearranges the elements of the specified array in uniformly random order.
|
static void |
shuffle(int[] a,
int lo,
int hi)
Rearranges the elements of the specified subarray in uniformly random order.
|
static void |
shuffle(java.lang.Object[] a)
Rearranges the elements of the specified array in uniformly random order.
|
static void |
shuffle(java.lang.Object[] a,
int lo,
int hi)
Rearranges the elements of the specified subarray in uniformly random order.
|
static double |
uniform()
Returns a random real number uniformly in [0, 1).
|
static double |
uniform(double a,
double b)
Returns a random real number uniformly in [a, b).
|
static int |
uniform(int n)
Returns a random integer uniformly in [0, n).
|
static int |
uniform(int a,
int b)
Returns a random integer uniformly in [a, b).
|
public static void setSeed(long s)
s
- the seedpublic static long getSeed()
public static double uniform()
public static int uniform(int n)
n
- number of possible integersn
(exclusive)java.lang.IllegalArgumentException
- if n <= 0
@Deprecated public static double random()
uniform()
.public static int uniform(int a, int b)
a
- the left endpointb
- the right endpointjava.lang.IllegalArgumentException
- if b <= a
java.lang.IllegalArgumentException
- if b - a >= Integer.MAX_VALUE
public static double uniform(double a, double b)
a
- the left endpointb
- the right endpointjava.lang.IllegalArgumentException
- unless a < b
public static boolean bernoulli(double p)
p
- the probability of returning true
true
with probability p
and
false
with probability p
java.lang.IllegalArgumentException
- unless p >= 0.0
and p <= 1.0
public static boolean bernoulli()
true
with probability 1/2 and
false
with probability 1/2public static double gaussian()
public static double gaussian(double mu, double sigma)
mu
- the meansigma
- the standard deviationmu
and standard deviation sigma
public static int geometric(double p)
p
- the parameter of the geometric distributionp
; or Integer.MAX_VALUE
if
p
is (nearly) equal to 1.0
.java.lang.IllegalArgumentException
- unless p >= 0.0
and p <= 1.0
public static int poisson(double lambda)
lambda
- the mean of the Poisson distributionlambda
java.lang.IllegalArgumentException
- unless lambda > 0.0
and not infinitepublic static double pareto()
public static double pareto(double alpha)
alpha
- shape parameteralpha
java.lang.IllegalArgumentException
- unless alpha > 0.0
public static double cauchy()
public static int discrete(double[] probabilities)
probabilities
- the probability of occurrence of each integeri
with probability probabilities[i]
java.lang.IllegalArgumentException
- if probabilities
is null
java.lang.IllegalArgumentException
- if sum of array entries is not (very nearly) equal to 1.0
java.lang.IllegalArgumentException
- unless probabilities[i] >= 0.0
for each index i
public static int discrete(int[] frequencies)
frequencies
- the frequency of occurrence of each integeri
with probability proportional to frequencies[i]
java.lang.IllegalArgumentException
- if frequencies
is null
java.lang.IllegalArgumentException
- if all array entries are 0
java.lang.IllegalArgumentException
- if frequencies[i]
is negative for any index i
java.lang.IllegalArgumentException
- if sum of frequencies exceeds Integer.MAX_VALUE
(231 - 1)public static double exp(double lambda)
lambda
- the rate of the exponential distributionlambda
java.lang.IllegalArgumentException
- unless lambda > 0.0
public static void shuffle(java.lang.Object[] a)
a
- the array to shufflejava.lang.IllegalArgumentException
- if a
is null
public static void shuffle(double[] a)
a
- the array to shufflejava.lang.IllegalArgumentException
- if a
is null
public static void shuffle(int[] a)
a
- the array to shufflejava.lang.IllegalArgumentException
- if a
is null
public static void shuffle(java.lang.Object[] a, int lo, int hi)
a
- the array to shufflelo
- the left endpoint (inclusive)hi
- the right endpoint (inclusive)java.lang.IllegalArgumentException
- if a
is null
java.lang.IndexOutOfBoundsException
- unless (0 <= lo) && (lo <= hi) && (hi < a.length)
public static void shuffle(double[] a, int lo, int hi)
a
- the array to shufflelo
- the left endpoint (inclusive)hi
- the right endpoint (inclusive)java.lang.IllegalArgumentException
- if a
is null
java.lang.IndexOutOfBoundsException
- unless (0 <= lo) && (lo <= hi) && (hi < a.length)
public static void shuffle(int[] a, int lo, int hi)
a
- the array to shufflelo
- the left endpoint (inclusive)hi
- the right endpoint (inclusive)java.lang.IllegalArgumentException
- if a
is null
java.lang.IndexOutOfBoundsException
- unless (0 <= lo) && (lo <= hi) && (hi < a.length)