类 StdRandom


  • public final class StdRandom
    extends java.lang.Object
    The 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).

    For additional documentation, see Section 2.2 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.

    • 方法概要

      修饰符和类型 方法 说明
      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​(java.lang.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()
      已过时。
      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​(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).
      static long uniform​(long n)
      Returns a random long integer uniformly in [0, n).
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 方法详细资料

      • 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.
        参数:
        s - the seed
      • getSeed

        public static long getSeed()
        Returns the seed of the pseudo-random number generator.
        返回:
        the seed
      • uniform

        public static double uniform()
        Returns a random real number uniformly in [0, 1).
        返回:
        a random real number uniformly in [0, 1)
      • uniform

        public static int uniform​(int n)
        Returns a random integer uniformly in [0, n).
        参数:
        n - number of possible integers
        返回:
        a random integer uniformly between 0 (inclusive) and n (exclusive)
        抛出:
        java.lang.IllegalArgumentException - if n <= 0
      • uniform

        public static long uniform​(long n)
        Returns a random long integer uniformly in [0, n).
        参数:
        n - number of possible long integers
        返回:
        a random long integer uniformly between 0 (inclusive) and n (exclusive)
        抛出:
        java.lang.IllegalArgumentException - if n <= 0
      • random

        @Deprecated
        public static double random()
        已过时。
        Replaced by uniform().
        Returns a random real number uniformly in [0, 1).
        返回:
        a random real number uniformly in [0, 1)
      • uniform

        public static int uniform​(int a,
                                  int b)
        Returns a random integer uniformly in [a, b).
        参数:
        a - the left endpoint
        b - the right endpoint
        返回:
        a random integer uniformly in [a, b)
        抛出:
        java.lang.IllegalArgumentException - if b <= a
        java.lang.IllegalArgumentException - if b - a >= Integer.MAX_VALUE
      • uniform

        public static double uniform​(double a,
                                     double b)
        Returns a random real number uniformly in [a, b).
        参数:
        a - the left endpoint
        b - the right endpoint
        返回:
        a random real number uniformly in [a, b)
        抛出:
        java.lang.IllegalArgumentException - unless a < b
      • bernoulli

        public static boolean bernoulli​(double p)
        Returns a random boolean from a Bernoulli distribution with success probability p.
        参数:
        p - the probability of returning true
        返回:
        true with probability p and false with probability 1 - p
        抛出:
        java.lang.IllegalArgumentException - unless 0p1.0
      • bernoulli

        public static boolean bernoulli()
        Returns a random boolean from a Bernoulli distribution with success probability 1/2.
        返回:
        true with probability 1/2 and false with probability 1/2
      • gaussian

        public static double gaussian()
        Returns a random real number from a standard Gaussian distribution.
        返回:
        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 σ.
        参数:
        mu - the mean
        sigma - the standard deviation
        返回:
        a real number distributed according to the Gaussian distribution with mean mu and standard deviation sigma
      • 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.
        参数:
        p - the parameter of the geometric distribution
        返回:
        a random integer from a geometric distribution with success probability p; or Integer.MAX_VALUE if p is (nearly) equal to 1.0.
        抛出:
        java.lang.IllegalArgumentException - unless p >= 0.0 and p <= 1.0
      • poisson

        public static int poisson​(double lambda)
        Returns a random integer from a Poisson distribution with mean λ.
        参数:
        lambda - the mean of the Poisson distribution
        返回:
        a random integer from a Poisson distribution with mean lambda
        抛出:
        java.lang.IllegalArgumentException - unless lambda > 0.0 and not infinite
      • pareto

        public static double pareto()
        Returns a random real number from the standard Pareto distribution.
        返回:
        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 α.
        参数:
        alpha - shape parameter
        返回:
        a random real number from a Pareto distribution with shape parameter alpha
        抛出:
        java.lang.IllegalArgumentException - unless alpha > 0.0
      • cauchy

        public static double cauchy()
        Returns a random real number from the Cauchy distribution.
        返回:
        a random real number from the Cauchy distribution.
      • discrete

        public static int discrete​(double[] probabilities)
        Returns a random integer from the specified discrete distribution.
        参数:
        probabilities - the probability of occurrence of each integer
        返回:
        a random integer from a discrete distribution: i 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
      • discrete

        public static int discrete​(int[] frequencies)
        Returns a random integer from the specified discrete distribution.
        参数:
        frequencies - the frequency of occurrence of each integer
        返回:
        a random integer from a discrete distribution: i 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)
      • exp

        public static double exp​(double lambda)
        Returns a random real number from an exponential distribution with rate λ.
        参数:
        lambda - the rate of the exponential distribution
        返回:
        a random real number from an exponential distribution with rate lambda
        抛出:
        java.lang.IllegalArgumentException - unless lambda > 0.0
      • shuffle

        public static void shuffle​(java.lang.Object[] a)
        Rearranges the elements of the specified array in uniformly random order.
        参数:
        a - the array to shuffle
        抛出:
        java.lang.IllegalArgumentException - if a is null
      • shuffle

        public static void shuffle​(double[] a)
        Rearranges the elements of the specified array in uniformly random order.
        参数:
        a - the array to shuffle
        抛出:
        java.lang.IllegalArgumentException - if a is null
      • shuffle

        public static void shuffle​(int[] a)
        Rearranges the elements of the specified array in uniformly random order.
        参数:
        a - the array to shuffle
        抛出:
        java.lang.IllegalArgumentException - if a is null
      • shuffle

        public static void shuffle​(char[] a)
        Rearranges the elements of the specified array in uniformly random order.
        参数:
        a - the array to shuffle
        抛出:
        java.lang.IllegalArgumentException - if a is null
      • shuffle

        public static void shuffle​(java.lang.Object[] a,
                                   int lo,
                                   int hi)
        Rearranges the elements of the specified subarray in uniformly random order.
        参数:
        a - the array to shuffle
        lo - the left endpoint (inclusive)
        hi - the right endpoint (exclusive)
        抛出:
        java.lang.IllegalArgumentException - if a is null
        java.lang.IllegalArgumentException - 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.
        参数:
        a - the array to shuffle
        lo - the left endpoint (inclusive)
        hi - the right endpoint (exclusive)
        抛出:
        java.lang.IllegalArgumentException - if a is null
        java.lang.IllegalArgumentException - 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.
        参数:
        a - the array to shuffle
        lo - the left endpoint (inclusive)
        hi - the right endpoint (exclusive)
        抛出:
        java.lang.IllegalArgumentException - if a is null
        java.lang.IllegalArgumentException - unless (0 <= lo) && (lo < hi) && (hi <= a.length)
      • permutation

        public static int[] permutation​(int n)
        Returns a uniformly random permutation of n elements.
        参数:
        n - number of elements
        返回:
        an array of length n that is a uniformly random permutation of 0, 1, ..., n-1
        抛出:
        java.lang.IllegalArgumentException - if n is negative
      • permutation

        public static int[] permutation​(int n,
                                        int k)
        Returns a uniformly random permutation of k of n elements.
        参数:
        n - number of elements
        k - number of elements to select
        返回:
        an array of length k that is a uniformly random permutation of k of the elements from 0, 1, ..., n-1
        抛出:
        java.lang.IllegalArgumentException - if n is negative
        java.lang.IllegalArgumentException - unless 0 <= k <= n
      • main

        public static void main​(java.lang.String[] args)
        Unit tests the methods in this class.
        参数:
        args - the command-line arguments