|
|
DescriptionFix off-by-one error in PRNG.
BUG=
Committed: https://crrev.com/0d97d5380854ba571672c2b33bfc6b6c03b69853
Cr-Commit-Position: refs/heads/master@{#10328}
Patch Set 1 #
Total comments: 3
Patch Set 2 : made Rand() interval exclusive of 1.0 #
Messages
Total messages: 18 (6 generated)
solenberg@webrtc.org changed reviewers: + pbos@webrtc.org
lgtm
pls fix, then lgtm https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.cc File webrtc/test/random.cc (right): https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.cc#newcode34 webrtc/test/random.cc:34: float uniform = Rand() * (high - low) + low; Wait, this actually causes a bad distribution, if we do this (with 1.0 inclusive) then 2, 3 gives that 3 is infinitely (ok, 1 out of 2**32) unlikely. Revert this line and divide kScale by 2**32 instead so that 1.0 is inclusive, then the previous line was OK. https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.h File webrtc/test/random.h (right): https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.h#newcode27 webrtc/test/random.h:27: // Return pseudo-random number in the interval [0.0, 1.0]. Let's make this one [0.0, 1.0).
terelius@webrtc.org changed reviewers: + terelius@webrtc.org
https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.cc File webrtc/test/random.cc (right): https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.cc#newcode34 webrtc/test/random.cc:34: float uniform = Rand() * (high - low) + low; On 2015/10/19 15:22:21, pbos-webrtc wrote: > Wait, this actually causes a bad distribution, if we do this (with 1.0 > inclusive) then 2, 3 gives that 3 is infinitely (ok, 1 out of 2**32) unlikely. > > Revert this line and divide kScale by 2**32 instead so that 1.0 is inclusive, > then the previous line was OK. I was actually about to file a bug about this independently. As Peter points out, this gives the wrong distribution, but with the +1, the computation overflows. I am quite convinced that the only way to solve this completely is to use unsigned.
On 2015/10/19 15:27:24, terelius wrote: > https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.cc > File webrtc/test/random.cc (right): > > https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.cc#newcode34 > webrtc/test/random.cc:34: float uniform = Rand() * (high - low) + low; > On 2015/10/19 15:22:21, pbos-webrtc wrote: > > Wait, this actually causes a bad distribution, if we do this (with 1.0 > > inclusive) then 2, 3 gives that 3 is infinitely (ok, 1 out of 2**32) unlikely. > > > > Revert this line and divide kScale by 2**32 instead so that 1.0 is inclusive, > > then the previous line was OK. > > I was actually about to file a bug about this independently. As Peter points > out, this gives the wrong distribution, but with the +1, the computation > overflows. > > I am quite convinced that the only way to solve this completely is to use > unsigned. Excluding the 1.0 from the distribution should do it as well, right?
On 2015/10/19 15:31:55, the sun wrote: > On 2015/10/19 15:27:24, terelius wrote: > > https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.cc > > File webrtc/test/random.cc (right): > > > > > https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.cc#newcode34 > > webrtc/test/random.cc:34: float uniform = Rand() * (high - low) + low; > > On 2015/10/19 15:22:21, pbos-webrtc wrote: > > > Wait, this actually causes a bad distribution, if we do this (with 1.0 > > > inclusive) then 2, 3 gives that 3 is infinitely (ok, 1 out of 2**32) > unlikely. > > > > > > Revert this line and divide kScale by 2**32 instead so that 1.0 is > inclusive, > > > then the previous line was OK. > > > > I was actually about to file a bug about this independently. As Peter points > > out, this gives the wrong distribution, but with the +1, the computation > > overflows. > > > > I am quite convinced that the only way to solve this completely is to use > > unsigned. > > Excluding the 1.0 from the distribution should do it as well, right? No. If you ask for a uniformly distributed integer in the range [INT_MIN, INT_MAX], you'll get a difference 0xFFFFFFFF so when you add 1 you end up multiplying the floating point random number by 0. In fact there are even more problems which can give you a negative difference. Multiplying the floating point rand by this negative number and adding the (negative) low limit gives something that is outside the desired interval and possibly lower than INT_MIN. Feel free to reassign this bug to me if you want.
On 2015/10/19 15:40:19, terelius wrote: > On 2015/10/19 15:31:55, the sun wrote: > > On 2015/10/19 15:27:24, terelius wrote: > > > https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.cc > > > File webrtc/test/random.cc (right): > > > > > > > > > https://codereview.webrtc.org/1412183002/diff/1/webrtc/test/random.cc#newcode34 > > > webrtc/test/random.cc:34: float uniform = Rand() * (high - low) + low; > > > On 2015/10/19 15:22:21, pbos-webrtc wrote: > > > > Wait, this actually causes a bad distribution, if we do this (with 1.0 > > > > inclusive) then 2, 3 gives that 3 is infinitely (ok, 1 out of 2**32) > > unlikely. > > > > > > > > Revert this line and divide kScale by 2**32 instead so that 1.0 is > > inclusive, > > > > then the previous line was OK. > > > > > > I was actually about to file a bug about this independently. As Peter points > > > out, this gives the wrong distribution, but with the +1, the computation > > > overflows. > > > > > > I am quite convinced that the only way to solve this completely is to use > > > unsigned. > > > > Excluding the 1.0 from the distribution should do it as well, right? > > No. If you ask for a uniformly distributed integer in the range [INT_MIN, > INT_MAX], you'll get a difference 0xFFFFFFFF so when you add 1 you end up > multiplying the floating point random number by 0. > > In fact there are even more problems which can give you a negative difference. > Multiplying the floating point rand by this negative number and adding the > (negative) low limit gives something that is outside the desired interval and > possibly lower than INT_MIN. > > Feel free to reassign this bug to me if you want. After discussing with pbos@, it seems we are solving two different overflows. Changing the interval to [0,1) seems like a good idea and it will solve the problem pbos had. I am talking about another case where low is negative and high is large. I can solve that one after this CL lands.
The CQ bit was checked by terelius@webrtc.org
The patchset sent to the CQ was uploaded after l-g-t-m from pbos@webrtc.org Link to the patchset: https://codereview.webrtc.org/1412183002/#ps20001 (title: "made Rand() interval exclusive of 1.0")
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/patch-status/1412183002/20001 View timeline at https://chromium-cq-status.appspot.com/patch-timeline/1412183002/20001
The CQ bit was unchecked by commit-bot@chromium.org
Try jobs failed on following builders: android_arm64_rel on tryserver.webrtc (JOB_TIMED_OUT, no build URL) android_dbg on tryserver.webrtc (JOB_TIMED_OUT, no build URL) android_rel on tryserver.webrtc (JOB_TIMED_OUT, no build URL)
The CQ bit was checked by solenberg@webrtc.org
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/patch-status/1412183002/20001 View timeline at https://chromium-cq-status.appspot.com/patch-timeline/1412183002/20001
Message was sent while issue was closed.
Committed patchset #2 (id:20001)
Message was sent while issue was closed.
Patchset 2 (id:??) landed as https://crrev.com/0d97d5380854ba571672c2b33bfc6b6c03b69853 Cr-Commit-Position: refs/heads/master@{#10328} |