Package stdlib

Class StdRandom

java.lang.Object
stdlib.StdRandom

public final class StdRandom extends Object
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.

  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Return a boolean, which is true with probability .5, and false otherwise.
    static boolean
    bernoulli(double p)
    Return a boolean, which is true with probability p, and false otherwise.
    static double
    Return a real number with a Cauchy distribution.
    static int
    discrete(double[] a)
    Return a number from a discrete distribution: i with probability a[i].
    static double
    exp(double lambda)
    Return a real number from an exponential distribution with rate lambda.
    static double
    Return a real number with a standard Gaussian distribution.
    static double
    gaussian(double mean, double stddev)
    Return a real number from a gaussian distribution with given mean and stddev
    static int
    geometric(double p)
    Return an integer with a geometric distribution with mean 1/p.
    static long
    Get the seed of the psedurandom number generator.
    static void
    main(String[] args)
    Unit test.
    static double
    pareto(double alpha)
    Return a real number with a Pareto distribution with parameter alpha.
    static int
    poisson(double lambda)
    Return an integer with a Poisson distribution with mean lambda.
    static double
    Return real number uniformly in [0, 1).
    static void
    setSeed(long s)
    Set the seed of the psedurandom number generator.
    static void
    shuffle(double[] a)
    Rearrange the elements of a double array in random order.
    static void
    shuffle(double[] a, int lo, int hi)
    Rearrange the elements of the subarray a[lo..hi] in random order.
    static void
    shuffle(int[] a)
    Rearrange the elements of an int array in random order.
    static void
    shuffle(int[] a, int lo, int hi)
    Rearrange the elements of the subarray a[lo..hi] in random order.
    static void
    Rearrange the elements of an array in random order.
    static void
    shuffle(Object[] a, int lo, int hi)
    Rearrange the elements of the subarray a[lo..hi] in random order.
    static double
    Return real number uniformly in [0, 1).
    static double
    uniform(double a, double b)
    Return real number uniformly in [a, b).
    static int
    uniform(int N)
    Return an integer uniformly between 0 (inclusive) and N (exclusive).
    static int
    uniform(int a, int b)
    Return int uniformly in [a, b).

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • setSeed

      public static void setSeed(long s)
      Set the seed of the psedurandom number generator.
    • getSeed

      public static long getSeed()
      Get the seed of the psedurandom number generator.
    • uniform

      public static double uniform()
      Return real number uniformly in [0, 1).
    • uniform

      public static int uniform(int N)
      Return an integer uniformly between 0 (inclusive) and N (exclusive).
    • random

      public static double random()
      Return real number uniformly in [0, 1).
    • uniform

      public static int uniform(int a, int b)
      Return int uniformly in [a, b).
    • uniform

      public static double uniform(double a, double b)
      Return real number uniformly in [a, b).
    • bernoulli

      public static boolean bernoulli(double p)
      Return a boolean, which is true with probability p, and false otherwise.
    • bernoulli

      public static boolean bernoulli()
      Return a boolean, which is true with probability .5, and false otherwise.
    • gaussian

      public static double gaussian()
      Return a real number with a standard Gaussian distribution.
    • gaussian

      public static double gaussian(double mean, double stddev)
      Return a real number from a gaussian distribution with given mean and stddev
    • geometric

      public static int geometric(double p)
      Return an integer with a geometric distribution with mean 1/p.
    • poisson

      public static int poisson(double lambda)
      Return an integer with a Poisson distribution with mean lambda.
    • pareto

      public static double pareto(double alpha)
      Return a real number with a Pareto distribution with parameter alpha.
    • cauchy

      public static double cauchy()
      Return a real number with a Cauchy distribution.
    • discrete

      public static int discrete(double[] a)
      Return a number from a discrete distribution: i with probability a[i]. Precondition: array entries are nonnegative and their sum (very nearly) equals 1.0.
    • exp

      public static double exp(double lambda)
      Return a real number from an exponential distribution with rate lambda.
    • shuffle

      public static void shuffle(Object[] a)
      Rearrange the elements of an array in random order.
    • shuffle

      public static void shuffle(double[] a)
      Rearrange the elements of a double array in random order.
    • shuffle

      public static void shuffle(int[] a)
      Rearrange the elements of an int array in random order.
    • shuffle

      public static void shuffle(Object[] a, int lo, int hi)
      Rearrange the elements of the subarray a[lo..hi] in random order.
    • shuffle

      public static void shuffle(double[] a, int lo, int hi)
      Rearrange the elements of the subarray a[lo..hi] in random order.
    • shuffle

      public static void shuffle(int[] a, int lo, int hi)
      Rearrange the elements of the subarray a[lo..hi] in random order.
    • main

      public static void main(String[] args)
      Unit test.