-
Notifications
You must be signed in to change notification settings - Fork 13.4k
rand: Add next_f64/f32 to Rng. #18534
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
Merged
+56
−14
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not knowledgeable about the topic but does this in any way skew random streams that are otherwise evenly distributed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does; there is precisely one value that doubles in frequency:
0.25
. Leteps
be the floating point precision, e.g. 2-53 for f64 andx = eps/4
be the value added here, then0.25 = 0.25 + x = 0.25' + x
where0.25' = 0.25 - x
is the largest float less than 0.25.However, this is doubling from 1 in 2-53 to 1 in 2-52 (or 24 → 23 for f32) which is rather small. It's exceedingly unlikely that a consumer of f64's will see 0.25 even once (but rather less unlikely for f32's).
I think I investigated this before for the original implementation to see what was done elsewhere, but I have forgot it, and did not record my findings.
(Note that this line isn't actually changing the functionality.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dSFMT operates on numbers between 1.0 and 2.0, just ors with 1 in the smallest bit and then subtracts 1.0 to lie in the open interval
(0.0, 1.0)
(e.g. 0 becomes 2-53). That behaviour halves the output space, but it does not lead to a bias.GSL does rejection sampling (calling the RNG until 0 is not encountered, which is rather likely). I guess we should consider doing that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @huonw!