bug icon indicating copy to clipboard operation
bug copied to clipboard

scala.util.Random.between(minInclusive, maxExclusive) overflow bug.

Open dragonfly-ai opened this issue 8 months ago • 5 comments

Reproduction steps

println( new scala.util.Random().between(Double.MinValue, Double.MaxValue) ) // always prints: 1.7976931348623155E308

See this Scastie.

Problem

One would expect a truly, full spectrum random number from the interval [Double.MinValue Double.MaxValue) instead this code always returns 1.7976931348623155E308.

dragonfly-ai avatar Mar 14 '25 16:03 dragonfly-ai

FTR, there is a correct implementation of nextDouble(origin, bound) in the following Scala.js PR: https://github.com/scala-js/scala-js/pull/5142 It corresponds to what su.Random calls between. A fix could be taken from there.

sjrd avatar Mar 14 '25 16:03 sjrd

Happy Pi Day!

som-snytt avatar Mar 14 '25 17:03 som-snytt

Happy Pi Day!

It sure is! Also wanted to tell you @som-snytt that we miss you in Discord land. So glad you're still going strong here, though!

dragonfly-ai avatar Mar 14 '25 17:03 dragonfly-ai

obviously always returning 1.7976931348623155E308 is undesirable and we should do better

but that said, I'm not sure how meaningful it is to ask for a random floating point number over such a wide range — IEEE 754 gets weird once huge numbers are involved, the numbers that are even representable are farther and farther apart from each other. I'm not sure what "truly, full spectrum" even means in this context. (I would imagine there's literature on this...?)

regardless, it seems that a PR porting what Scala.js did would be welcome

SethTisue avatar Mar 19 '25 16:03 SethTisue

If there's no hurry, I'm willing to take the assignment. It took over a year last time. This time, how about maybe a month?

dragonfly-ai avatar Mar 19 '25 21:03 dragonfly-ai