public final class StdRandom extends Object
StdRandom class provides static methods for generating
random number from various discrete and continuous distributions,
including uniform, Bernoulli, geometric, Gaussian, exponential, Pareto,
Poisson, and Cauchy. It also provides method for shuffling an
array or subarray and generating random permutations.
By convention, all intervals are half open. For example,
uniform(-1.0, 1.0) returns a random number between
-1.0 (inclusive) and 1.0 (exclusive).
Similarly, shuffle(a, lo, hi) shuffles the hi - lo
elements in the array a[], starting at index lo
(inclusive) and ending at index hi (exclusive).
The methods all take constant expected time, except those that involve arrays. The shuffle method takes time linear in the subarray to be shuffled; the discrete methods take time linear in the length of the argument array.
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 pseudo-random number generator.
|
static void |
main(String[] args)
Unit tests the methods in this class.
|
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[] |
permutation(int n)
Returns a uniformly random permutation of n elements.
|
static int[] |
permutation(int n,
int k)
Returns a uniformly random permutation of k of n elements.
|
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 pseudo-random number generator.
|
static void |
shuffle(char[] a)
Rearranges the elements of the specified array in uniformly random order.
|
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(Object[] a)
Rearranges the elements of the specified array in uniformly random order.
|
static void |
shuffle(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).
|
static long |
uniform(long n)
Returns a random long integer uniformly in [0, n).
|
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)IllegalArgumentException - if n <= 0public static long uniform(long n)
n - number of possible long integersn (exclusive)IllegalArgumentException - if n <= 0@Deprecated public static double random()
uniform().public static int uniform(int a,
int b)
a - the left endpointb - the right endpointIllegalArgumentException - if b <= aIllegalArgumentException - if b - a >= Integer.MAX_VALUEpublic static double uniform(double a,
double b)
a - the left endpointb - the right endpointIllegalArgumentException - unless a < bpublic static boolean bernoulli(double p)
p - the probability of returning truetrue with probability p and
false with probability 1 - pIllegalArgumentException - unless 0 ≤ p ≤ 1.0public 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 sigmapublic static int geometric(double p)
p - the parameter of the geometric distributionp; or Integer.MAX_VALUE if
p is (nearly) equal to 1.0.IllegalArgumentException - unless p >= 0.0 and p <= 1.0public static int poisson(double lambda)
lambda - the mean of the Poisson distributionlambdaIllegalArgumentException - unless lambda > 0.0 and not infinitepublic static double pareto()
public static double pareto(double alpha)
alpha - shape parameteralphaIllegalArgumentException - unless alpha > 0.0public static double cauchy()
public static int discrete(double[] probabilities)
probabilities - the probability of occurrence of each integeri with probability probabilities[i]IllegalArgumentException - if probabilities is nullIllegalArgumentException - if sum of array entries is not (very nearly) equal to 1.0IllegalArgumentException - unless probabilities[i] >= 0.0 for each index ipublic static int discrete(int[] frequencies)
frequencies - the frequency of occurrence of each integeri with probability proportional to frequencies[i]IllegalArgumentException - if frequencies is nullIllegalArgumentException - if all array entries are 0IllegalArgumentException - if frequencies[i] is negative for any index iIllegalArgumentException - if sum of frequencies exceeds Integer.MAX_VALUE (231 - 1)public static double exp(double lambda)
lambda - the rate of the exponential distributionlambdaIllegalArgumentException - unless lambda > 0.0public static void shuffle(Object[] a)
a - the array to shuffleIllegalArgumentException - if a is nullpublic static void shuffle(double[] a)
a - the array to shuffleIllegalArgumentException - if a is nullpublic static void shuffle(int[] a)
a - the array to shuffleIllegalArgumentException - if a is nullpublic static void shuffle(char[] a)
a - the array to shuffleIllegalArgumentException - if a is nullpublic static void shuffle(Object[] a, int lo, int hi)
a - the array to shufflelo - the left endpoint (inclusive)hi - the right endpoint (exclusive)IllegalArgumentException - if a is nullIllegalArgumentException - 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 (exclusive)IllegalArgumentException - if a is nullIllegalArgumentException - 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 (exclusive)IllegalArgumentException - if a is nullIllegalArgumentException - unless (0 <= lo) && (lo < hi) && (hi <= a.length)public static int[] permutation(int n)
n - number of elementsn that is a uniformly random permutation
of 0, 1, ..., n-1IllegalArgumentException - if n is negativepublic static int[] permutation(int n,
int k)
n - number of elementsk - number of elements to selectk that is a uniformly random permutation
of k of the elements from 0, 1, ..., n-1IllegalArgumentException - if n is negativeIllegalArgumentException - unless 0 <= k <= npublic static void main(String[] args)
args - the command-line arguments