Package edu.princeton.cs.algs4
Class StdRandom
- Object
-
- edu.princeton.cs.algs4.StdRandom
-
public final class StdRandom extends Object
TheStdRandomclass 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.Conventions. By convention, all intervals are half open. For example,
uniformDouble(-1.0, 1.0)returns a random number between-1.0(inclusive) and1.0(exclusive). Similarly,shuffle(a, lo, hi)shuffles thehi - loelements in the arraya[], starting at indexlo(inclusive) and ending at indexhi(exclusive).Performance. 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.
Additional information. For additional documentation, see Section 2.2 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanbernoulli()Returns a random boolean from a Bernoulli distribution with success probability 1/2.static booleanbernoulli(double p)Returns a random boolean from a Bernoulli distribution with success probability p.static doublecauchy()Returns a random real number from the Cauchy distribution.static intdiscrete(double[] probabilities)Returns a random integer from the specified discrete distribution.static intdiscrete(int[] frequencies)Returns a random integer from the specified discrete distribution.static doubleexponential(double lambda)Returns a random real number from an exponential distribution with rate λ.static doublegaussian()Returns a random real number from a standard Gaussian distribution.static doublegaussian(double mu, double sigma)Returns a random real number from a Gaussian distribution with mean μ and standard deviation σ.static intgeometric(double p)Returns a random integer from a geometric distribution with success probability p.static longgetSeed()Returns the seed of the pseudo-random number generator.static voidmain(String[] args)Unit tests the methods in this class.static doublepareto()Returns a random real number from the standard Pareto distribution.static doublepareto(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 intpoisson(double lambda)Returns a random integer from a Poisson distribution with mean λ.static voidsetSeed(long s)Sets the seed of the pseudo-random number generator.static voidshuffle(char[] a)Rearranges the elements of the specified array in uniformly random order.static voidshuffle(double[] a)Rearranges the elements of the specified array in uniformly random order.static voidshuffle(double[] a, int lo, int hi)Rearranges the elements of the specified subarray in uniformly random order.static voidshuffle(int[] a)Rearranges the elements of the specified array in uniformly random order.static voidshuffle(int[] a, int lo, int hi)Rearranges the elements of the specified subarray in uniformly random order.static voidshuffle(Object[] a)Rearranges the elements of the specified array in uniformly random order.static voidshuffle(Object[] a, int lo, int hi)Rearranges the elements of the specified subarray in uniformly random order.static doubleuniformDouble()Returns a random real number uniformly in [0, 1).static doubleuniformDouble(double a, double b)Returns a random real number uniformly in [a, b).static intuniformInt(int n)Returns a random integer uniformly in [0, n).static intuniformInt(int a, int b)Returns a random integer uniformly in [a, b).static longuniformLong(long n)Returns a random long integer uniformly in [0, n).
-
-
-
Method Detail
-
setSeed
public static void setSeed(long s)
Sets the seed of the pseudo-random number generator. This method enables you to produce the same sequence of "random" number for each execution of the program. Ordinarily, you should call this method at most once per program.- Parameters:
s- the seed
-
getSeed
public static long getSeed()
Returns the seed of the pseudo-random number generator.- Returns:
- the seed
-
uniformDouble
public static double uniformDouble()
Returns a random real number uniformly in [0, 1).- Returns:
- a random real number uniformly in [0, 1)
-
uniformInt
public static int uniformInt(int n)
Returns a random integer uniformly in [0, n).- Parameters:
n- number of possible integers- Returns:
- a random integer uniformly between 0 (inclusive) and
n(exclusive) - Throws:
IllegalArgumentException- ifn <= 0
-
uniformLong
public static long uniformLong(long n)
Returns a random long integer uniformly in [0, n).- Parameters:
n- number of possiblelongintegers- Returns:
- a random long integer uniformly between 0 (inclusive) and
n(exclusive) - Throws:
IllegalArgumentException- ifn <= 0
-
uniformInt
public static int uniformInt(int a, int b)Returns a random integer uniformly in [a, b).- Parameters:
a- the left endpointb- the right endpoint- Returns:
- a random integer uniformly in [a, b)
- Throws:
IllegalArgumentException- ifb <= aIllegalArgumentException- ifb - a >= Integer.MAX_VALUE
-
uniformDouble
public static double uniformDouble(double a, double b)Returns a random real number uniformly in [a, b).- Parameters:
a- the left endpointb- the right endpoint- Returns:
- a random real number uniformly in [a, b)
- Throws:
IllegalArgumentException- unlessa < b
-
bernoulli
public static boolean bernoulli(double p)
Returns a random boolean from a Bernoulli distribution with success probability p.- Parameters:
p- the probability of returningtrue- Returns:
truewith probabilitypandfalsewith probability1 - p- Throws:
IllegalArgumentException- unless0≤p≤1.0
-
bernoulli
public static boolean bernoulli()
Returns a random boolean from a Bernoulli distribution with success probability 1/2.- Returns:
truewith probability 1/2 andfalsewith probability 1/2
-
gaussian
public static double gaussian()
Returns a random real number from a standard Gaussian distribution.- Returns:
- a random real number from a standard Gaussian distribution (mean 0 and standard deviation 1).
-
gaussian
public static double gaussian(double mu, double sigma)Returns a random real number from a Gaussian distribution with mean μ and standard deviation σ.- Parameters:
mu- the meansigma- the standard deviation- Returns:
- a real number distributed according to the Gaussian distribution
with mean
muand standard deviationsigma
-
geometric
public static int geometric(double p)
Returns a random integer from a geometric distribution with success probability p. The integer represents the number of independent trials before the first success.- Parameters:
p- the parameter of the geometric distribution- Returns:
- a random integer from a geometric distribution with success
probability
p; orInteger.MAX_VALUEifpis (nearly) equal to1.0. - Throws:
IllegalArgumentException- unlessp >= 0.0andp <= 1.0
-
poisson
public static int poisson(double lambda)
Returns a random integer from a Poisson distribution with mean λ.- Parameters:
lambda- the mean of the Poisson distribution- Returns:
- a random integer from a Poisson distribution with mean
lambda - Throws:
IllegalArgumentException- unlesslambda > 0.0and not infinite
-
pareto
public static double pareto()
Returns a random real number from the standard Pareto distribution.- Returns:
- a random real number from the standard Pareto distribution
-
pareto
public static double pareto(double alpha)
Returns a random real number from a Pareto distribution with shape parameter α.- Parameters:
alpha- shape parameter- Returns:
- a random real number from a Pareto distribution with shape
parameter
alpha - Throws:
IllegalArgumentException- unlessalpha > 0.0
-
cauchy
public static double cauchy()
Returns a random real number from the Cauchy distribution.- Returns:
- a random real number from the Cauchy distribution.
-
discrete
public static int discrete(double[] probabilities)
Returns a random integer from the specified discrete distribution.- Parameters:
probabilities- the probability of occurrence of each integer- Returns:
- a random integer from a discrete distribution:
iwith probabilityprobabilities[i] - Throws:
IllegalArgumentException- ifprobabilitiesisnullIllegalArgumentException- if sum of array entries is not (very nearly) equal to1.0IllegalArgumentException- unlessprobabilities[i] >= 0.0for each indexi
-
discrete
public static int discrete(int[] frequencies)
Returns a random integer from the specified discrete distribution.- Parameters:
frequencies- the frequency of occurrence of each integer- Returns:
- a random integer from a discrete distribution:
iwith probability proportional tofrequencies[i] - Throws:
IllegalArgumentException- iffrequenciesisnullIllegalArgumentException- if all array entries are0IllegalArgumentException- iffrequencies[i]is negative for any indexiIllegalArgumentException- if sum of frequencies exceedsInteger.MAX_VALUE(231 - 1)
-
exponential
public static double exponential(double lambda)
Returns a random real number from an exponential distribution with rate λ.- Parameters:
lambda- the rate of the exponential distribution- Returns:
- a random real number from an exponential distribution with
rate
lambda - Throws:
IllegalArgumentException- unlesslambda > 0.0
-
shuffle
public static void shuffle(Object[] a)
Rearranges the elements of the specified array in uniformly random order.- Parameters:
a- the array to shuffle- Throws:
IllegalArgumentException- ifaisnull
-
shuffle
public static void shuffle(double[] a)
Rearranges the elements of the specified array in uniformly random order.- Parameters:
a- the array to shuffle- Throws:
IllegalArgumentException- ifaisnull
-
shuffle
public static void shuffle(int[] a)
Rearranges the elements of the specified array in uniformly random order.- Parameters:
a- the array to shuffle- Throws:
IllegalArgumentException- ifaisnull
-
shuffle
public static void shuffle(char[] a)
Rearranges the elements of the specified array in uniformly random order.- Parameters:
a- the array to shuffle- Throws:
IllegalArgumentException- ifaisnull
-
shuffle
public static void shuffle(Object[] a, int lo, int hi)
Rearranges the elements of the specified subarray in uniformly random order.- Parameters:
a- the array to shufflelo- the left endpoint (inclusive)hi- the right endpoint (exclusive)- Throws:
IllegalArgumentException- ifaisnullIllegalArgumentException- unless(0 <= lo) && (lo < hi) && (hi <= a.length)
-
shuffle
public static void shuffle(double[] a, int lo, int hi)Rearranges the elements of the specified subarray in uniformly random order.- Parameters:
a- the array to shufflelo- the left endpoint (inclusive)hi- the right endpoint (exclusive)- Throws:
IllegalArgumentException- ifaisnullIllegalArgumentException- unless(0 <= lo) && (lo < hi) && (hi <= a.length)
-
shuffle
public static void shuffle(int[] a, int lo, int hi)Rearranges the elements of the specified subarray in uniformly random order.- Parameters:
a- the array to shufflelo- the left endpoint (inclusive)hi- the right endpoint (exclusive)- Throws:
IllegalArgumentException- ifaisnullIllegalArgumentException- unless(0 <= lo) && (lo < hi) && (hi <= a.length)
-
permutation
public static int[] permutation(int n)
Returns a uniformly random permutation of n elements.- Parameters:
n- number of elements- Returns:
- an array of length
nthat is a uniformly random permutation of0,1, ...,n-1 - Throws:
IllegalArgumentException- ifnis negative
-
permutation
public static int[] permutation(int n, int k)Returns a uniformly random permutation of k of n elements.- Parameters:
n- number of elementsk- number of elements to select- Returns:
- an array of length
kthat is a uniformly random permutation ofkof the elements from0,1, ...,n-1 - Throws:
IllegalArgumentException- ifnis negativeIllegalArgumentException- unless0 <= k <= n
-
main
public static void main(String[] args)
Unit tests the methods in this class.- Parameters:
args- the command-line arguments
-
-