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 inaccurate #679

Closed
bugQ opened this Issue Aug 7, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@bugQ

bugQ commented Aug 7, 2016

The current implementation of Random.float generates values in a range slightly lower than advertised. The error is approximately 1.16e10, but this means that, for example, Random.float 0 1 can produce a negative value, which is probably not intended. The bug can be reproduced by copying the body of the function and replacing number with minInt or maxInt to observe the output.

There are two possible solutions, both affecting line 155:

        toFloat number / toFloat (maxInt - minInt + 1)      -- produces range [lo, hi)
        (toFloat number + 0.5) / toFloat (maxInt - minInt)  -- produces range [lo, hi]

That is, one option excludes the upper bound, and the other includes it. There are cases to be made for either option. Many algorithms can take advantage of the fact that the upper bound is excluded, including my implementation of the standard normal distribution (elm-community/random-extra#2). However, instead of submitting a pull request, I thought I'd ask for comments first.

P.S. negativeOneToOne is a misnomer, as it seems to be intended to produce numbers between -0.5 and 0.5. The name of the variable doesn't affect the result, of course.

@process-bot

This comment has been minimized.

Show comment
Hide comment
@process-bot

process-bot Aug 7, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

process-bot commented Aug 7, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@bugQ

This comment has been minimized.

Show comment
Hide comment
@bugQ

bugQ Aug 7, 2016

I was too hasty. I've been informed that Random is already due to be replaced by PCG, and by reviewing code and testing the edge cases, the implementation looks sound (and excludes the upper bound, like previous implementations have).

Feel free to close this.

bugQ commented Aug 7, 2016

I was too hasty. I've been informed that Random is already due to be replaced by PCG, and by reviewing code and testing the edge cases, the implementation looks sound (and excludes the upper bound, like previous implementations have).

Feel free to close this.

@evancz evancz closed this Aug 7, 2016

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