Skip to content

Commit 5d1dbca

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

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ class ThreadLocalRandom extends Random {
105105
def nextDouble(least: Double, bound: Double): Double = {
106106
if (least >= bound)
107107
throw new IllegalArgumentException()
108-
109-
nextDouble() * (bound - least) + least
108+
// Ensure that rangeSize != Double.PositiveInfinity - Issue #2144
109+
val rangeSize = Math.min(bound - least, Double.MaxValue)
110+
nextDouble() * rangeSize + least
110111
}
111112
}
112113

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
@@ -506,8 +506,7 @@ class ThreadLocalRandomTest {
506506
@Test def should_return_nextDouble_that_fits_bounds(): Unit = {
507507
implicit val tlr = ThreadLocalRandom.current()
508508

509-
// Issue #2144
510-
// checkDoubleBounds(Double.MinValue, Double.MaxValue)
509+
checkDoubleBounds(Double.MinValue, Double.MaxValue)
511510
checkDoubleBounds(Double.MinValue, 0L)
512511
checkDoubleBounds(Double.MaxValue, 0L)
513512
checkDoubleBounds(0.14303466203185822, 0.7471945354839639)

0 commit comments

Comments
 (0)