Skip to content

Commit

Permalink
8280950: RandomGenerator:NextDouble() default behavior non conformant…
Browse files Browse the repository at this point in the history
… after JDK-8280550 fix

Reviewed-by: phh
Backport-of: 0e70d4504c267174738485c7da82a2ac0ef09770
  • Loading branch information
GoeLin committed Nov 3, 2022
1 parent 91f1538 commit 182173a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/util/Random.java
Expand Up @@ -303,7 +303,7 @@ final double internalNextDouble(double origin, double bound) {
if (origin < bound) {
r = r * (bound - origin) + origin;
if (r >= bound) // correct for rounding
r = Math.nextAfter(r, origin);
r = Math.nextAfter(bound, origin);
}
return r;
}
Expand Down
Expand Up @@ -350,7 +350,7 @@ final double internalNextDouble(double origin, double bound) {
if (origin < bound) {
r = r * (bound - origin) + origin;
if (r >= bound) // correct for rounding
r = Math.nextAfter(r, origin);
r = Math.nextAfter(bound, origin);
}
return r;
}
Expand Down
Expand Up @@ -282,7 +282,7 @@ final double internalNextDouble(double origin, double bound) {
if (origin < bound) {
r = r * (bound - origin) + origin;
if (r >= bound) // correct for rounding
Math.nextAfter(r, origin);
Math.nextAfter(bound, origin);
}
return r;
}
Expand Down
30 changes: 29 additions & 1 deletion test/jdk/java/util/Random/RandomNextDoubleBoundary.java
Expand Up @@ -24,13 +24,18 @@
/*
* @test
* @summary Verify nextDouble stays within range
* @bug 8280550
* @bug 8280550 8280950
*/

import java.util.SplittableRandom;

public class RandomNextDoubleBoundary {
public static void main(String... args) {
negativeBounds();
positiveBounds();
}

private static void negativeBounds() {
// Both bounds are negative
double lowerBound = -1.0000000000000002;
double upperBound = -1.0;
Expand All @@ -49,4 +54,27 @@ public static void main(String... args) {
throw new RuntimeException("Less than lower bound");
}
}

private static void positiveBounds() {
double[][] originAndBounds = {{10, 100},
{12345, 123456},
{5432167.234, 54321678.1238}};
for (double[] originAndBound : originAndBounds) {
nextDoublesWithRange(originAndBound[0], originAndBound[1]);
}
}

public static void nextDoublesWithRange(double origin, double bound) {
SplittableRandom rg = new SplittableRandom(42L);
double value = rg.nextDouble(origin, bound);

assertTrue(value >= origin);
assertTrue(value < bound);
}

public static void assertTrue(boolean condition) {
if (!condition) {
throw new AssertionError();
}
}
}

1 comment on commit 182173a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.