Class RandomUtils.DataGenerator

java.lang.Object
org.apache.commons.math4.random.RandomUtils.DataGenerator
Enclosing class:
RandomUtils

public static class RandomUtils.DataGenerator
extends java.lang.Object
Various random data generation routines.
  • Method Summary

    Modifier and Type Method Description
    java.lang.String nextHexString​(int len, boolean useSha1)
    Generates a random string of hex characters of length len.
    long nextLong​(long lower, long upper)
    Generates a uniformly distributed random long integer between lower and upper (endpoints included).
    double nextUniform​(double lower, double upper)
    Generates a uniformly distributed random value from the open interval (lower, upper) (i.e., endpoints excluded).
    double nextUniform​(double lower, double upper, boolean lowerInclusive)
    Generates a uniformly distributed random value from the interval (lower, upper) or the interval [lower, upper).

    Methods inherited from class java.lang.Object

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

    • nextHexString

      public java.lang.String nextHexString​(int len, boolean useSha1)
      Generates a random string of hex characters of length len. Algorithm Description: how hexadecimal strings are generated depends on the value of the useSha1 argument.
      • If useSha1 == false, a 2-step process is used:
        1. len / 2 + 1 binary bytes are generated using the underlying generator.
        2. Each binary byte is translated into 2 hex digits.
      • If useSha1 == true, hex strings are generated in 40-byte segments using a 3-step process:
        1. 20 random bytes are generated using the underlying generator.
        2. SHA-1 hash is applied to yield a 20-byte binary digest.
        3. Each byte of the binary digest is converted to 2 hex digits.
      Parameters:
      len - Length of the generated string.
      useSha1 - Whether to use a digest. If true (resp. false), the 3-step (resp. 2-step) process will be used.
      Returns:
      the random string.
      Throws:
      NotStrictlyPositiveException - if len <= 0.
    • nextLong

      public long nextLong​(long lower, long upper)
      Generates a uniformly distributed random long integer between lower and upper (endpoints included).
      Parameters:
      lower - Lower bound for generated long integer.
      upper - Upper bound for generated long integer.
      Returns:
      a random long integer greater than or equal to lower and less than or equal to upper
      Throws:
      NumberIsTooLargeException - if lower >= upper
    • nextUniform

      public double nextUniform​(double lower, double upper)
      Generates a uniformly distributed random value from the open interval (lower, upper) (i.e., endpoints excluded).

      Definition: Uniform Distribution lower and upper - lower are the location and scale parameters, respectively.

      Algorithm Description: scales the output of Random.nextDouble(), but rejects 0 values (i.e., will generate another random double if Random.nextDouble() returns 0). This is necessary to provide a symmetric output interval (both endpoints excluded).

      Parameters:
      lower - Lower bound of the support (excluded).
      upper - Upper bound of the support (excluded).
      Returns:
      a uniformly distributed random value between lower and upper (both excluded).
      Throws:
      NumberIsTooLargeException - if lower >= upper.
      NotFiniteNumberException - if one of the bounds is infinite.
      NotANumberException - if one of the bounds is NaN.
    • nextUniform

      public double nextUniform​(double lower, double upper, boolean lowerInclusive)
      Generates a uniformly distributed random value from the interval (lower, upper) or the interval [lower, upper). The lower bound is thus optionally included, while the upper bound is always excluded.

      Definition: Uniform Distribution lower and upper - lower are the location and scale parameters, respectively.

      Algorithm Description: if the lower bound is excluded, scales the output of "nextDouble()", but rejects 0 values (i.e. it will generate another random double if "nextDouble()" returns 0). This is necessary to provide a symmetric output interval (both endpoints excluded).

      Parameters:
      lower - Lower bound of the support.
      upper - Exclusive upper bound of the support.
      lowerInclusive - true if the lower bound is inclusive.
      Returns:
      a uniformly distributed random value in the (lower, upper) interval, if lowerInclusive is false, or in the [lower, upper) interval, if lowerInclusive is true.
      Throws:
      NumberIsTooLargeException - if lower >= upper.
      NotFiniteNumberException - if one of the bounds is infinite.
      NotANumberException - if one of the bounds is NaN.