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

Random.float lacks sanitization of input and emits invalid numbers when bounds are swapped #21

Open
SiriusStarr opened this issue Dec 5, 2021 · 0 comments · May be fixed by #22
Open

Comments

@SiriusStarr
Copy link

Random.int contains a sanitization step at:

random/src/Random.elm

Lines 81 to 85 in c1c9da4

( lo, hi ) =
if a < b then
( a, b )
else
( b, a )

As such, Random.int 100 -100 produces values in the range [-100, 100]

Random.float contains no such step, and Random.float 100 -100 produces values in the range [100, 300] due to this, which assumes the bounds were passed in the correct order:

random/src/Random.elm

Lines 180 to 182 in c1c9da4

-- Scale it into our range
range =
abs (b - a)
There is no information in the documentation warning of this (otherwise silent) error, and it violates the principle of least surprise.

Either the same sanitization step should be added to Random.float or at the very least a prominent warning should be added to the documentation.

@SiriusStarr SiriusStarr linked a pull request Dec 5, 2021 that will close this issue
rupertlssmith pushed a commit to elm-janitor/random that referenced this issue Feb 17, 2023
fixes elm#22

Sanitize input to `float` to avoid invalid values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant