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

Side by Side Diff: base/rand_util.cc

Issue 6883102: Add one-time randomization support for FieldTrial, and the ability to (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge to head. Created 9 years, 7 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') | chrome/browser/browser_main.h » ('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
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 14
15 namespace base { 15 namespace base {
16 16
17 int RandInt(int min, int max) { 17 int RandInt(int min, int max) {
18 DCHECK_LE(min, max); 18 DCHECK_LE(min, max);
19 19
20 uint64 range = static_cast<uint64>(max) - min + 1; 20 uint64 range = static_cast<uint64>(max) - min + 1;
21 int result = min + static_cast<int>(base::RandGenerator(range)); 21 int result = min + static_cast<int>(base::RandGenerator(range));
22 DCHECK_GE(result, min); 22 DCHECK_GE(result, min);
23 DCHECK_LE(result, max); 23 DCHECK_LE(result, max);
24 return result; 24 return result;
25 } 25 }
26 26
27 double RandDouble() { 27 double RandDouble() {
28 return BitsToOpenEndedUnitInterval(base::RandUint64());
29 }
30
31 double BitsToOpenEndedUnitInterval(uint64 bits) {
28 // We try to get maximum precision by masking out as many bits as will fit 32 // We try to get maximum precision by masking out as many bits as will fit
29 // in the target type's mantissa, and raising it to an appropriate power to 33 // in the target type's mantissa, and raising it to an appropriate power to
30 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa 34 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa
31 // is expected to accommodate 53 bits. 35 // is expected to accommodate 53 bits.
32 36
33 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); 37 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn);
34 static const int kBits = std::numeric_limits<double>::digits; 38 static const int kBits = std::numeric_limits<double>::digits;
35 uint64 random_bits = base::RandUint64() & ((GG_UINT64_C(1) << kBits) - 1); 39 uint64 random_bits = bits & ((GG_UINT64_C(1) << kBits) - 1);
36 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); 40 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
37 DCHECK_GE(result, 0.0); 41 DCHECK_GE(result, 0.0);
38 DCHECK_LT(result, 1.0); 42 DCHECK_LT(result, 1.0);
39 return result; 43 return result;
40 } 44 }
41 45
42 uint64 RandGenerator(uint64 max) { 46 uint64 RandGenerator(uint64 max) {
43 DCHECK_GT(max, 0ULL); 47 DCHECK_GT(max, 0ULL);
44 return base::RandUint64() % max; 48 return base::RandUint64() % max;
45 } 49 }
46 50
47 void RandBytes(void* output, size_t output_length) { 51 void RandBytes(void* output, size_t output_length) {
48 uint64 random_int; 52 uint64 random_int;
49 size_t random_int_size = sizeof(random_int); 53 size_t random_int_size = sizeof(random_int);
50 for (size_t i = 0; i < output_length; i += random_int_size) { 54 for (size_t i = 0; i < output_length; i += random_int_size) {
51 random_int = base::RandUint64(); 55 random_int = base::RandUint64();
52 size_t copy_count = std::min(output_length - i, random_int_size); 56 size_t copy_count = std::min(output_length - i, random_int_size);
53 memcpy(((uint8*)output) + i, &random_int, copy_count); 57 memcpy(((uint8*)output) + i, &random_int, copy_count);
54 } 58 }
55 } 59 }
56 60
57 std::string RandBytesAsString(size_t length) { 61 std::string RandBytesAsString(size_t length) {
58 std::string result; 62 std::string result;
59 RandBytes(WriteInto(&result, length + 1), length); 63 RandBytes(WriteInto(&result, length + 1), length);
60 return result; 64 return result;
61 } 65 }
62 66
63 } // namespace base 67 } // namespace base
OLDNEW
« no previous file with comments | « base/rand_util.h ('k') | chrome/browser/browser_main.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698