-
Notifications
You must be signed in to change notification settings - Fork 8
Add uniform function producing a random float in (0.0, 1.0)
#4
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
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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.
This implementation certainly works, but:
0x1p-64instead)?n = 0and branching can be a bit costly. In stead, the same behaviour could be obtained by adding a small number after the multiplication (0x1p-54or0x1p-65, depending on what you choose for the first remark). Sure, then you need to change the documentation below...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.
"take [all the bits] into account (and multiply by 0x1p-64 instead)": then you get FP rounding during the int64 -> float conversion. The rounding can produce 0.0 and 1.0, and seems to introduce some (very subtle) bias in the output, as discussed in Generating Random Floating-Point Numbers by Dividing Integers: a Case Study. With the current code, we know exactly which FP values we can get, and we're sure they all have the same probability.
"Testing n = 0 and branching can be a bit costly". Really? This is one integer comparison plus a branch that is perfectly predicted (as n is almost never 0).
"adding a small number after the multiplication": the resulting rounding makes me nervous (again).
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.
Just for fun: I tweaked the n = 0 test so that the conditional branch is statically predicted as not taken.
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.
Right. Adding
0x1p-54here would make producing 1. possible because of the round-to-even rule. And adding a slightly smaller number would introduce a-0x1p55biais, because all values > 0.5 would be rounded down. While this is a very small biais (probably impossible to notice), I then agree that what you propose is probably the best.My concern about branching was a remainder of memories of what's happening when writing the same code in C : then, the introduced branching would be a problem, since it would prevent some SIMD optimizations. But Ocamlopt does not support these optimizations anyway, and the branch will very likely be predicted.
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.
Right, I forgot about your adventures with SIMD :-)
I take it that you find the name
uniformacceptable for this function ? The only alternative I could think of isfloat1, but it's ugly.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 agree that
uniformis not such a good name, because it does not follow the convention of the rest of the file, where the name follow, in some sense, the type of the return value, whileuniformdescribes the distribution.One possibility would be to not provide publicly this function, but still document the new guarantee. The downside is the additional multiplication by the constant 1., but:
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 agree the additional multiplication by 1.0 is not costly, so it's OK if users continue to call [float g 1.0] instead of [uniform g]. On the other hand, I find it makes the documentation clearer. By "it" I mean "having the
uniformfunction exported and documented separately". So let's do that.