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

java.util.Random#ints rejects over-represented candidates after JDK-8248862

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P4
    • tbd
    • 17, 18, 19, 20, 21
    • core-libs
    • None

    Description

      The following contrived example shows a change in the behaviour of Random#ints after JDK-8248862.

      The documentation for Random#ints and #longs specifies implementations for the methods:

      https://github.com/openjdk/jdk/blob/09bfbf80639f059563fcd4432995b8c380cea298/src/java.base/share/classes/java/util/Random.java#L927-L942

      https://github.com/openjdk/jdk/blob/09bfbf80639f059563fcd4432995b8c380cea298/src/java.base/share/classes/java/util/Random.java#L1043-L1063

      The implementation of long is documented as having additional logic to 'reject over-represented candidates'. After the implementation of JDK-8248862, it looks like both methods are using logic in RandomSupport (in boundedNextInt and boundedNextLong) that are identical aside from the use of 'int' and 'long', and both of those implementations include the 'over-represented candidates' logic.

      Is this change deliberate? If so, would it make sense to update the javadoc for Random#ints to clarify that, like #longs, it includes the additional logic?

      ```
      import java.util.Random;

      public class R {
        public static void main(String[] args) {
          Random r = new Random() {
            @Override public int nextInt() { return 9; }
            @Override public int nextInt(int i) { return nextInt(); }
            @Override public long nextLong() { return nextInt(); }
          };

          r.longs(400, 1000).limit(1).forEachOrdered(System.err::println);
          r.ints(400, 1000).limit(1).forEachOrdered(System.err::println);
        }
      }
      ```

      ./jdk-17-ea+17/bin/java R
      404
      404

      ./jdk-17-ea+16/bin/java R
      404
      409 // this matches 'nextInt(n) + origin' in the Random#ints docs

      Attachments

        Activity

          People

            rgiulietti Raffaello Giulietti
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: