Skip to content

Commit 35d8cbb

Browse files
committed
Fix scala-js#2144: Fix ThreadLocalRandom.nextDouble(MinValue, MaxValue).
1 parent 65035de commit 35d8cbb

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
@@ -518,8 +518,7 @@ class ThreadLocalRandomTest {
518518
@Test def should_return_nextDouble_that_fits_bounds(): Unit = {
519519
implicit val tlr = ThreadLocalRandom.current()
520520

521-
// Issue #2144
522-
// checkDoubleBounds(Double.MinValue, Double.MaxValue)
521+
checkDoubleBounds(Double.MinValue, Double.MaxValue)
523522
checkDoubleBounds(Double.MinValue, 0L)
524523
checkDoubleBounds(Double.MaxValue, 0L)
525524
checkDoubleBounds(0.14303466203185822, 0.7471945354839639)

0 commit comments

Comments
 (0)