Skip to content

Commit dd8a057

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

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
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

0 commit comments

Comments
 (0)