Class RandomVariates

java.lang.Object
org.cicirello.math.rand.RandomVariates

public final class RandomVariates extends Object
This utility class provides methods for generating random variates from different distributions.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    nextBinomial(int n, double p)
    Generates a pseudorandom integer from a binomial distribution.
    static int
    nextBinomial(int n, double p, RandomGenerator r)
    Generates a pseudorandom integer from a binomial distribution.
    static double
    nextCauchy(double scale)
    Generates a pseudorandom number from a Cauchy distribution with median 0 and chosen scale parameter.
    static double
    nextCauchy(double median, double scale)
    Generates a pseudorandom number from a Cauchy distribution.
    static double
    nextCauchy(double median, double scale, RandomGenerator r)
    Generates a pseudorandom number from a Cauchy distribution.
    static double
    nextCauchy(double scale, RandomGenerator r)
    Generates a pseudorandom number from a Cauchy distribution with median 0 and chosen scale parameter.
    static double
    Generates a random number from a Gaussian distribution with mean 0 and standard deviation 1.
    static double
    nextGaussian(double sigma)
    Generates a random number from a Gaussian distribution with mean 0 and standard deviation sigma.
    static double
    nextGaussian(double mu, double sigma)
    Generates a random number from a Gaussian distribution with mean mu and standard deviation sigma.
    static double
    nextGaussian(double mu, double sigma, RandomGenerator r)
    Generates a random number from a Gaussian distribution with mean mu and standard deviation sigma.
    static double
    nextGaussian(double sigma, RandomGenerator r)
    Generates a random number from a Gaussian distribution with mean 0 and standard deviation sigma.
    static double
    Generates a random number from a Gaussian distribution with mean 0 and standard deviation 1.

    Methods inherited from class java.lang.Object

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

    • nextBinomial

      public static int nextBinomial(int n, double p)
      Generates a pseudorandom integer from a binomial distribution. The source of randomness is via the ThreadLocalRandom class, and thus this method is both safe and efficient for use with threads.
      Parameters:
      n - Number of trials for the binomial distribution.
      p - The probability of a successful trial.
      Returns:
      A pseudorandom integer from a binomial distribution.
    • nextBinomial

      public static int nextBinomial(int n, double p, RandomGenerator r)
      Generates a pseudorandom integer from a binomial distribution.
      Parameters:
      n - Number of trials for the binomial distribution.
      p - The probability of a successful trial.
      r - The source of randomness.
      Returns:
      A pseudorandom integer from a binomial distribution.
    • nextCauchy

      public static double nextCauchy(double median, double scale)
      Generates a pseudorandom number from a Cauchy distribution.
      Parameters:
      median - The median of the Cauchy.
      scale - The scale parameter of the Cauchy.
      Returns:
      a pseudorandom number from a Cauchy distribution
    • nextCauchy

      public static double nextCauchy(double scale)
      Generates a pseudorandom number from a Cauchy distribution with median 0 and chosen scale parameter.
      Parameters:
      scale - The scale parameter of the Cauchy.
      Returns:
      a pseudorandom number from a Cauchy distribution
    • nextCauchy

      public static double nextCauchy(double median, double scale, RandomGenerator r)
      Generates a pseudorandom number from a Cauchy distribution.
      Parameters:
      median - The median of the Cauchy.
      scale - The scale parameter of the Cauchy.
      r - The source of randomness.
      Returns:
      a pseudorandom number from a Cauchy distribution
    • nextCauchy

      public static double nextCauchy(double scale, RandomGenerator r)
      Generates a pseudorandom number from a Cauchy distribution with median 0 and chosen scale parameter.
      Parameters:
      scale - The scale parameter of the Cauchy.
      r - The source of randomness.
      Returns:
      a pseudorandom number from a Cauchy distribution
    • nextGaussian

      public static double nextGaussian(double mu, double sigma)
      Generates a random number from a Gaussian distribution with mean mu and standard deviation sigma.

      ThreadLocalRandom is used as the source of randomness. However, it does not directly use ThreadLocalRandom's nextGaussian, which is the slow polar method. Instead, it uses the approach described in the following paper to use the RandomGenerator interface's default implementation of McFarland's modified ziggurat algorithm, which is much faster.

      Vincent A. Cicirello. 2024. Fast Gaussian Distributed Pseudorandom Number Generation in Java via the Ziggurat Algorithm. arXiv:2405.19493, May 2024. doi:10.48550/arXiv.2405.19493. [PDF]

      Parameters:
      mu - The mean of the Gaussian.
      sigma - The standard deviation of the Gaussian.
      Returns:
      A random number from a Gaussian distribution with mean mu and standard deviation sigma.
    • nextGaussian

      public static double nextGaussian(double mu, double sigma, RandomGenerator r)
      Generates a random number from a Gaussian distribution with mean mu and standard deviation sigma.

      If the RandomGenerator r is one of Java's legacy pseudorandom number generators Random or one of its subclasses, then this method uses our implementation of the original ziggurat algorithm, and otherwise it uses the RandomGenerator's default implementation of nextGaussian introduced in Java 17 which is McFarland's modified ziggurat. See the following report for relevant experiments:

      Vincent A. Cicirello. 2024. Fast Gaussian Distributed Pseudorandom Number Generation in Java via the Ziggurat Algorithm. arXiv:2405.19493, May 2024. doi:10.48550/arXiv.2405.19493. [PDF]

      Parameters:
      mu - The mean of the Gaussian.
      sigma - The standard deviation of the Gaussian.
      r - The pseudorandom number generator to use for the source of randomness.
      Returns:
      A random number from a Gaussian distribution with mean mu and standard deviation sigma.
    • nextGaussian

      public static double nextGaussian(double sigma)
      Generates a random number from a Gaussian distribution with mean 0 and standard deviation sigma.

      ThreadLocalRandom is used as the source of randomness. However, it does not directly use ThreadLocalRandom's nextGaussian, which is the slow polar method. Instead, it uses the approach described in the following paper to use the RandomGenerator interface's default implementation of McFarland's modified ziggurat algorithm, which is much faster.

      Vincent A. Cicirello. 2024. Fast Gaussian Distributed Pseudorandom Number Generation in Java via the Ziggurat Algorithm. arXiv:2405.19493, May 2024. doi:10.48550/arXiv.2405.19493. [PDF]

      Parameters:
      sigma - The standard deviation of the Gaussian.
      Returns:
      A random number from a Gaussian distribution with mean 0 and standard deviation sigma.
    • nextGaussian

      public static double nextGaussian(double sigma, RandomGenerator r)
      Generates a random number from a Gaussian distribution with mean 0 and standard deviation sigma.

      If the RandomGenerator r is one of Java's legacy pseudorandom number generators Random or one of its subclasses, then this method uses our implementation of the original ziggurat algorithm, and otherwise it uses the RandomGenerator's default implementation of nextGaussian introduced in Java 17 which is McFarland's modified ziggurat. See the following report for relevant experiments:

      Vincent A. Cicirello. 2024. Fast Gaussian Distributed Pseudorandom Number Generation in Java via the Ziggurat Algorithm. arXiv:2405.19493, May 2024. doi:10.48550/arXiv.2405.19493. [PDF]

      Parameters:
      sigma - The standard deviation of the Gaussian.
      r - The pseudorandom number generator to use for the source of randomness.
      Returns:
      A random number from a Gaussian distribution with mean 0 and standard deviation sigma.
    • nextGaussian

      public static double nextGaussian()
      Generates a random number from a Gaussian distribution with mean 0 and standard deviation 1.

      ThreadLocalRandom is used as the source of randomness. However, it does not directly use ThreadLocalRandom's nextGaussian, which is the slow polar method. Instead, it uses the approach described in the following paper to use the RandomGenerator interface's default implementation of McFarland's modified ziggurat algorithm, which is much faster.

      Vincent A. Cicirello. 2024. Fast Gaussian Distributed Pseudorandom Number Generation in Java via the Ziggurat Algorithm. arXiv:2405.19493, May 2024. doi:10.48550/arXiv.2405.19493. [PDF]

      Returns:
      A random number from a Gaussian distribution with mean 0 and standard deviation 1.
    • nextGaussian

      public static double nextGaussian(RandomGenerator r)
      Generates a random number from a Gaussian distribution with mean 0 and standard deviation 1.

      If the RandomGenerator r is one of Java's legacy pseudorandom number generators Random or one of its subclasses, then this method uses our implementation of the original ziggurat algorithm, and otherwise it uses the RandomGenerator's default implementation of nextGaussian introduced in Java 17 which is McFarland's modified ziggurat. See the following report for relevant experiments:

      Vincent A. Cicirello. 2024. Fast Gaussian Distributed Pseudorandom Number Generation in Java via the Ziggurat Algorithm. arXiv:2405.19493, May 2024. doi:10.48550/arXiv.2405.19493. [PDF]

      Parameters:
      r - The pseudorandom number generator to use for the source of randomness.
      Returns:
      A random number from a Gaussian distribution with mean 0 and standard deviation 1.