Package stdlib
Class StdRandom
java.lang.Object
stdlib.StdRandom
Standard random. This class provides methods for generating
  random number from various distributions.
  
For additional documentation, see Section 2.2 of Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
- 
Field SummaryFields
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic booleanReturn a boolean, which is true with probability .5, and false otherwise.static booleanbernoulli(double p) Return a boolean, which is true with probability p, and false otherwise.static doublecauchy()Return a real number with a Cauchy distribution.static intdiscrete(double[] a) Return a number from a discrete distribution: i with probability a[i].static doubleexp(double lambda) Return a real number from an exponential distribution with rate lambda.static doublegaussian()Return a real number with a standard Gaussian distribution.static doublegaussian(double mean, double stddev) Return a real number from a gaussian distribution with given mean and stddevstatic intgeometric(double p) Return an integer with a geometric distribution with mean 1/p.static longgetSeed()Get the seed of the psedurandom number generator.static voidUnit test.static doublepareto(double alpha) Return a real number with a Pareto distribution with parameter alpha.static intpoisson(double lambda) Return an integer with a Poisson distribution with mean lambda.static doublerandom()Return real number uniformly in [0, 1).static voidsetSeed(long s) Set the seed of the psedurandom number generator.static voidshuffle(double[] a) Rearrange the elements of a double array in random order.static voidshuffle(double[] a, int lo, int hi) Rearrange the elements of the subarray a[lo..hi] in random order.static voidshuffle(int[] a) Rearrange the elements of an int array in random order.static voidshuffle(int[] a, int lo, int hi) Rearrange the elements of the subarray a[lo..hi] in random order.static voidRearrange the elements of an array in random order.static voidRearrange the elements of the subarray a[lo..hi] in random order.static doubleuniform()Return real number uniformly in [0, 1).static doubleuniform(double a, double b) Return real number uniformly in [a, b).static intuniform(int N) Return an integer uniformly between 0 (inclusive) and N (exclusive).static intuniform(int a, int b) Return int uniformly in [a, b).
- 
Field Details- 
random
- 
seed
 
- 
- 
Constructor Details- 
StdRandomprivate StdRandom()
 
- 
- 
Method Details- 
setSeedSet the seed of the psedurandom number generator.
- 
getSeedGet the seed of the psedurandom number generator.
- 
uniformReturn real number uniformly in [0, 1).
- 
uniformReturn an integer uniformly between 0 (inclusive) and N (exclusive).
- 
randomReturn real number uniformly in [0, 1).
- 
uniformReturn int uniformly in [a, b).
- 
uniformReturn real number uniformly in [a, b).
- 
bernoulliReturn a boolean, which is true with probability p, and false otherwise.
- 
bernoulliReturn a boolean, which is true with probability .5, and false otherwise.
- 
gaussianReturn a real number with a standard Gaussian distribution.
- 
gaussianReturn a real number from a gaussian distribution with given mean and stddev
- 
geometricReturn an integer with a geometric distribution with mean 1/p.
- 
poissonReturn an integer with a Poisson distribution with mean lambda.
- 
paretoReturn a real number with a Pareto distribution with parameter alpha.
- 
cauchyReturn a real number with a Cauchy distribution.
- 
discreteReturn a number from a discrete distribution: i with probability a[i]. Precondition: array entries are nonnegative and their sum (very nearly) equals 1.0.
- 
expReturn a real number from an exponential distribution with rate lambda.
- 
shuffleRearrange the elements of an array in random order.
- 
shuffleRearrange the elements of a double array in random order.
- 
shuffleRearrange the elements of an int array in random order.
- 
shuffleRearrange the elements of the subarray a[lo..hi] in random order.
- 
shuffleRearrange the elements of the subarray a[lo..hi] in random order.
- 
shuffleRearrange the elements of the subarray a[lo..hi] in random order.
- 
mainUnit test.
 
-