[jdk17] RFR: JDK-8266313 (JEP-356) - RandomGenerator spec implementation requirements tightly coupled to JDK internal classes
Jim Laskey
jlaskey at openjdk.java.net
Mon Jun 28 16:00:09 UTC 2021
On Mon, 28 Jun 2021 15:26:45 GMT, Roger Riggs <rriggs at openjdk.org> wrote:
>> The wording of the @implSpec referred to internal methods in the description. The patch rewords the @implSpec to be more descriptive of the algorithm than the methods used.
>
> src/java.base/share/classes/java/util/random/RandomGenerator.java line 648:
>
>> 646: * power of two then limiting is a simple masking operation. Otherwise, a
>> 647: * new result is re-calculated by averaging the previous result and
>> 648: * {@code nextInt()} until the final result is greater equal zero and less
>
> I don't see how 'averaging' comes from the invocation of boundedNextInt which appears to choose the first candidate that meets the criteria.
> The comment also applies to nextLong overloads.
If the first value calculated doesn't meet the criteria, then you either mask (power of 2) or go into this loop with does the averaging. The intent is not to bias the outcome.
for (long u = r >>> 1; // ensure nonnegative
u + m - (r = u % n) < 0L; // rejection check
u = rng.nextLong() >>> 1) // retry
;
-------------
PR: https://git.openjdk.java.net/jdk17/pull/151
More information about the core-libs-dev
mailing list