Index: base/rand_util.cc |
diff --git a/base/rand_util.cc b/base/rand_util.cc |
index e556c07e8e7d90aedeb1555c39244d0aeffcff5e..97f63ae74069a97ab0a582ea2e6ca23b8c9e514f 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); |
- |
- // We must discard random results above this number, as they would |
+uint64 RandGenerator(uint64 n) { |
+ DCHECK(n); |
Mark Mentovai
2011/08/23 17:01:33
The DCHECK_GT was a bit more expressive. Why did y
Denis Lagno
2011/08/24 12:58:28
Done.
|
+ // We must discard random results above or equal this number, as they would |
Mark Mentovai
2011/08/23 17:01:33
Use correct English.
Denis Lagno
2011/08/24 12:58:28
Done.
|
// 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 min_unacceptable_value = (std::numeric_limits<uint64>::max() / n) * n; |
uint64 value; |
do { |
value = base::RandUint64(); |
- } while (value >= max_acceptable_value); |
+ } while (value >= min_unacceptable_value); |
- return value % max; |
+ return value % n; |
} |
void RandBytes(void* output, size_t output_length) { |