Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8283084

RandomGenerator nextDouble(double, double) is documented incorrectly

    XMLWordPrintable

Details

    • b20
    • generic
    • generic

    Description

      A DESCRIPTION OF THE PROBLEM :
      RandomGenerator:
      default double nextDouble(double origin, double bound);
      https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/random/RandomGenerator.html#nextDouble(double,double)

      default float nextFloat(float origin, float bound);
      https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/random/RandomGenerator.html#nextFloat(float,float)

      These methods are documented incorrectly. The default method checks (origin < bound) and (bound - origin) < +infinity.

      The exception conditions are incorrect:
      "if {@code origin} is not finite,
       or {@code bound} is not finite, or {@code origin}
       is greater than or equal to {@code bound}"

      This is not true. Calling with -Double.MAX_VALUE and Double.MAX_VALUE satisfies the documented requirements but actually throws an exception.

      The implementation spec is incorrect:

      The default implementation checks that {@code origin} and
      {@code bound} are positive finite doubles.

      This is not true. Origin and Bound can be negative.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      import java.util.random.*;

      public class NextDoubleBug {
        public static void main(String[] args) {
          RandomGenerator g = RandomGeneratorFactory.of("L64X128MixRandom").create(42);
          System.out.printf("Negative origin and bound are OK: %s%n", g.nextDouble(-10, -5));
          try {
            g.nextDouble(-Double.MAX_VALUE, Double.MAX_VALUE);
            throw new IllegalStateException("Infinite range should throw");
          } catch (IllegalArgumentException expected) {
            System.out.println("Infinite range throws");
          }
        }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      > java NextDoubleBug
      Negative origin and bound are OK: -6.5179306489351125
      Infinite range throws
      ACTUAL -
      The result is as expected. This does not match the javadoc behaviour.


      ---------- BEGIN SOURCE ----------
      Provided
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Issue Links

          Activity

            People

              jlaskey Jim Laskey
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: