Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(563)

Unified Diff: base/rand_util.cc

Issue 7685053: Fix variable names and comments in RandGenerator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: reflected comments Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/rand_util.h ('k') | base/rand_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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) {
« no previous file with comments | « base/rand_util.h ('k') | base/rand_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698