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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/rand_util.h ('k') | base/rand_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/rand_util.h" 5 #include "base/rand_util.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include <limits> 9 #include <limits>
10 10
(...skipping 25 matching lines...) Expand all
36 36
37 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); 37 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn);
38 static const int kBits = std::numeric_limits<double>::digits; 38 static const int kBits = std::numeric_limits<double>::digits;
39 uint64 random_bits = bits & ((GG_UINT64_C(1) << kBits) - 1); 39 uint64 random_bits = bits & ((GG_UINT64_C(1) << kBits) - 1);
40 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); 40 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
41 DCHECK_GE(result, 0.0); 41 DCHECK_GE(result, 0.0);
42 DCHECK_LT(result, 1.0); 42 DCHECK_LT(result, 1.0);
43 return result; 43 return result;
44 } 44 }
45 45
46 uint64 RandGenerator(uint64 max) { 46 uint64 RandGenerator(uint64 range) {
47 DCHECK_GT(max, 0ULL); 47 DCHECK_GT(range, 0u);
48
49 // We must discard random results above this number, as they would 48 // We must discard random results above this number, as they would
50 // make the random generator non-uniform (consider e.g. if 49 // make the random generator non-uniform (consider e.g. if
51 // MAX_UINT64 was 4 and max was 3, then a result of 1 would be twice 50 // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice
52 // as likely as a result of 0 or 2). 51 // as likely as a result of 3 or 4).
53 uint64 max_acceptable_value = 52 uint64 max_acceptable_value =
54 (std::numeric_limits<uint64>::max() / max) * max; 53 (std::numeric_limits<uint64>::max() / range) * range - 1;
55 54
56 uint64 value; 55 uint64 value;
57 do { 56 do {
58 value = base::RandUint64(); 57 value = base::RandUint64();
59 } while (value >= max_acceptable_value); 58 } while (value > max_acceptable_value);
60 59
61 return value % max; 60 return value % range;
62 } 61 }
63 62
64 void RandBytes(void* output, size_t output_length) { 63 void RandBytes(void* output, size_t output_length) {
65 uint64 random_int; 64 uint64 random_int;
66 size_t random_int_size = sizeof(random_int); 65 size_t random_int_size = sizeof(random_int);
67 for (size_t i = 0; i < output_length; i += random_int_size) { 66 for (size_t i = 0; i < output_length; i += random_int_size) {
68 random_int = base::RandUint64(); 67 random_int = base::RandUint64();
69 size_t copy_count = std::min(output_length - i, random_int_size); 68 size_t copy_count = std::min(output_length - i, random_int_size);
70 memcpy(((uint8*)output) + i, &random_int, copy_count); 69 memcpy(((uint8*)output) + i, &random_int, copy_count);
71 } 70 }
72 } 71 }
73 72
74 std::string RandBytesAsString(size_t length) { 73 std::string RandBytesAsString(size_t length) {
75 std::string result; 74 std::string result;
76 RandBytes(WriteInto(&result, length + 1), length); 75 RandBytes(WriteInto(&result, length + 1), length);
77 return result; 76 return result;
78 } 77 }
79 78
80 } // namespace base 79 } // namespace base
OLDNEW
« 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