Chromium Code Reviews| Index: base/rand_util.cc |
| diff --git a/base/rand_util.cc b/base/rand_util.cc |
| index e556c07e8e7d90aedeb1555c39244d0aeffcff5e..f6eb4a2ddb870d273a2d97f5c62955efbc0ba7e4 100644 |
| --- a/base/rand_util.cc |
| +++ b/base/rand_util.cc |
| @@ -43,22 +43,20 @@ double BitsToOpenEndedUnitInterval(uint64 bits) { |
| return result; |
| } |
| -uint64 RandGenerator(uint64 max) { |
| - DCHECK_GT(max, 0ULL); |
| - |
| +uint64 RandGenerator(uint64 range) { |
| + DCHECK_GT(range, 0u); |
| // We must discard random results above this number, as they would |
| // make the random generator non-uniform (consider e.g. if |
| - // MAX_UINT64 was 4 and max was 3, then a result of 1 would be twice |
| - // as likely as a result of 0 or 2). |
| - uint64 max_acceptable_value = |
| - (std::numeric_limits<uint64>::max() / max) * max; |
| + // MAX_UINT64 was 7 and |n| was 5, then a result of 1 would be twice |
| + // as likely as a result of 3 or 4). |
| + uint64 max_acceptable_value = kuint64max / range * range - 1; |
|
Mark Mentovai
2011/08/24 14:03:03
Please go back to using std::numeric_limits<uint64
Denis Lagno
2011/08/24 16:42:44
Done.
|
| uint64 value; |
| do { |
| value = base::RandUint64(); |
| - } while (value >= max_acceptable_value); |
| + } while (value > max_acceptable_value); |
| - return value % max; |
| + return value % range; |
| } |
| void RandBytes(void* output, size_t output_length) { |