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

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: numeric_limits 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..fcbccef4446009e0968ff57792f29230db312d12 100644
--- a/base/rand_util.cc
+++ b/base/rand_util.cc
@@ -43,22 +43,21 @@ 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).
+ // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice
+ // as likely as a result of 3 or 4).
uint64 max_acceptable_value =
- (std::numeric_limits<uint64>::max() / max) * max;
+ (std::numeric_limits<uint64>::max() / range) * range - 1;
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