Exemplo n.º 1
0
  /**
   * {@inheritDoc}
   *
   * <p><strong>Algorithm Description</strong>: if the lower bound is excluded, 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).
   *
   * @throws NumberIsTooLargeException if {@code lower >= upper}
   * @throws NotFiniteNumberException if one of the bounds is infinite
   * @throws NotANumberException if one of the bounds is NaN
   */
  public double nextUniform(double lower, double upper, boolean lowerInclusive)
      throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException {

    if (lower >= upper) {
      throw new NumberIsTooLargeException(
          LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false);
    }

    if (Double.isInfinite(lower)) {
      throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, lower);
    }
    if (Double.isInfinite(upper)) {
      throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, upper);
    }

    if (Double.isNaN(lower) || Double.isNaN(upper)) {
      throw new NotANumberException();
    }

    final RandomGenerator generator = getRandomGenerator();

    // ensure nextDouble() isn't 0.0
    double u = generator.nextDouble();
    while (!lowerInclusive && u <= 0.0) {
      u = generator.nextDouble();
    }

    return u * upper + (1.0 - u) * lower;
  }