Skip to content

Commit e43e440

Browse files
committed
Fix scala-js#2144: Fix ThreadLocalRandom.nextDouble(MinValue, MaxValue).
1 parent 5c315e6 commit e43e440

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

javalib/src/main/scala/java/util/concurrent/ThreadLocalRandom.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ class ThreadLocalRandom extends Random {
106106
if (least >= bound)
107107
throw new IllegalArgumentException()
108108

109-
nextDouble() * (bound - least) + least
109+
/* Based on documentation for Random.doubles to avoid issue #2144 and other
110+
* possible rounding up issues:
111+
* https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#doubles-double-double-
112+
*/
113+
val next = nextDouble() * (bound - least) + least
114+
if (next < bound) next
115+
else Math.nextAfter(bound, Double.NegativeInfinity)
110116
}
111117
}
112118

test-suite/shared/src/test/require-jdk7/org/scalajs/testsuite/javalib/util/concurrent/ThreadLocalRandomTest.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,7 @@ class ThreadLocalRandomTest {
515515
@Test def should_return_nextDouble_that_fits_bounds(): Unit = {
516516
implicit val tlr = ThreadLocalRandom.current()
517517

518-
// Issue #2144
519-
// checkDoubleBounds(Double.MinValue, Double.MaxValue)
518+
checkDoubleBounds(Double.MinValue, Double.MaxValue)
520519
checkDoubleBounds(Double.MinValue, 0L)
521520
checkDoubleBounds(Double.MaxValue, 0L)
522521
checkDoubleBounds(0.14303466203185822, 0.7471945354839639)

0 commit comments

Comments
 (0)