Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dragonfly-ai opened this issue Mar 14, 2025 · 5 comments
Open

Comments

@dragonfly-ai
Copy link

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.

@sjrd
Copy link
Member

sjrd commented Mar 14, 2025

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

@som-snytt
Copy link

Happy Pi Day!

@dragonfly-ai
Copy link
Author

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!

@SethTisue
Copy link
Member

SethTisue commented Mar 19, 2025

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

@dragonfly-ai
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants