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

Side by Side Diff: base/rand_util.cc

Issue 6931048: Revert 84197 - Add one-time randomization support for FieldTrial, and the ability to (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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) {
32 // We try to get maximum precision by masking out as many bits as will fit 28 // We try to get maximum precision by masking out as many bits as will fit
33 // in the target type's mantissa, and raising it to an appropriate power to 29 // in the target type's mantissa, and raising it to an appropriate power to
34 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa 30 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa
35 // is expected to accommodate 53 bits. 31 // is expected to accommodate 53 bits.
36 32
37 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); 33 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn);
38 static const int kBits = std::numeric_limits<double>::digits; 34 static const int kBits = std::numeric_limits<double>::digits;
39 uint64 random_bits = bits & ((GG_UINT64_C(1) << kBits) - 1); 35 uint64 random_bits = base::RandUint64() & ((GG_UINT64_C(1) << kBits) - 1);
40 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); 36 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
41 DCHECK_GE(result, 0.0); 37 DCHECK_GE(result, 0.0);
42 DCHECK_LT(result, 1.0); 38 DCHECK_LT(result, 1.0);
43 return result; 39 return result;
44 } 40 }
45 41
46 uint64 RandGenerator(uint64 max) { 42 uint64 RandGenerator(uint64 max) {
47 DCHECK_GT(max, 0ULL); 43 DCHECK_GT(max, 0ULL);
48 return base::RandUint64() % max; 44 return base::RandUint64() % max;
49 } 45 }
50 46
51 void RandBytes(void* output, size_t output_length) { 47 void RandBytes(void* output, size_t output_length) {
52 uint64 random_int; 48 uint64 random_int;
53 size_t random_int_size = sizeof(random_int); 49 size_t random_int_size = sizeof(random_int);
54 for (size_t i = 0; i < output_length; i += random_int_size) { 50 for (size_t i = 0; i < output_length; i += random_int_size) {
55 random_int = base::RandUint64(); 51 random_int = base::RandUint64();
56 size_t copy_count = std::min(output_length - i, random_int_size); 52 size_t copy_count = std::min(output_length - i, random_int_size);
57 memcpy(((uint8*)output) + i, &random_int, copy_count); 53 memcpy(((uint8*)output) + i, &random_int, copy_count);
58 } 54 }
59 } 55 }
60 56
61 std::string RandBytesAsString(size_t length) { 57 std::string RandBytesAsString(size_t length) {
62 std::string result; 58 std::string result;
63 RandBytes(WriteInto(&result, length + 1), length); 59 RandBytes(WriteInto(&result, length + 1), length);
64 return result; 60 return result;
65 } 61 }
66 62
67 } // namespace base 63 } // 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