-
Notifications
You must be signed in to change notification settings - Fork 448
Fix RandomSample crash when 0 was out from random. #70
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
Conversation
Thanks for your contribution, @sidepelican! This looks great.
Your seeded number generator correctly produces |
I can make the
The test stops at this line when using such a generator. The fundamental loop is here. Based on the above, I changed the test to use internal function directly. 5acda56 Or, should make a generator that return |
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 can make the
RandomNumberGenerator
type its anytime returns0
, but it doesn't work correctly becaseInt.random(in: 0..<N, using: &g)
(N > 2) falls into infinit loop.
Indeed, what I had in mind was either a generator that starts by generating at least two 0
s like you mentioned, or one that generates a 0
with a certain (high) probability.
Based on the above, I changed the test to use internal function directly.
This I like even more! Now we're not making any assumptions about the exact order in which these functions are called.
I think this means that we can't test in release any longer, right? @natecook1000, how do you feel about that? Do we already use a testable import elsewhere? (n.b. this comment is not a "we shouldn't do this," it's a "we should be aware that we are making a choice here.") |
We chatted, and we're fine with this. Approved. |
Upon more detailed review, this fixes the most obvious problem, but is not actually a complete solution. The zero division itself is actually fine. Division by zero in floating-point is not an error; it produces infinity or NaN, which is fine. It's the conversion to
We should also have a clamping initializer for Int from BinaryFloatingPoint, so that people don't have to do this themselves. That belongs in the standard library, but I'll land it in Numerics first and we can switch over to using it in algorithms. |
Nice catch, and TIL about
We can trivially construct a range larger than that, so we have real reasons to fix this. |
TIL about I tried #70 (comment) , its looks fine but, I found crash when returning
I fixed this by just adding |
Can't we eliminate the +1 there and use ( |
Fix #69 .
I changed the random ranges from
[0, 1)
to(0, 1]
.w
must not be0
so fixednextW
too. (inlog(1 - w)
,log(1)
is0
so zero division happens.)Checklist