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

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: Unit test cancel support. Created 9 years, 8 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
OLDNEW
1 // Copyright (c) 2010 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 13
14 namespace base { 14 namespace base {
15 15
16 int RandInt(int min, int max) { 16 int RandInt(int min, int max) {
17 DCHECK(min <= max); 17 DCHECK(min <= max);
18 18
19 uint64 range = static_cast<uint64>(max) - min + 1; 19 uint64 range = static_cast<uint64>(max) - min + 1;
20 int result = min + static_cast<int>(base::RandGenerator(range)); 20 int result = min + static_cast<int>(base::RandGenerator(range));
21 DCHECK(result >= min && result <= max); 21 DCHECK(result >= min && result <= max);
22 return result; 22 return result;
23 } 23 }
24 24
25 double RandDouble() { 25 double RandDouble() {
26 return BitsToRandLikeDouble(base::RandUint64());
27 }
28
29 double BitsToRandLikeDouble(uint64 bits) {
jar (doing other things) 2011/04/21 22:10:02 Why not just BitsToDouble()? I really don't like
Jói 2011/04/28 01:03:50 This is technically one of the half-open unit inte
26 // We try to get maximum precision by masking out as many bits as will fit 30 // We try to get maximum precision by masking out as many bits as will fit
27 // in the target type's mantissa, and raising it to an appropriate power to 31 // in the target type's mantissa, and raising it to an appropriate power to
28 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa 32 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa
29 // is expected to accommodate 53 bits. 33 // is expected to accommodate 53 bits.
30 34
31 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); 35 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn);
32 static const int kBits = std::numeric_limits<double>::digits; 36 static const int kBits = std::numeric_limits<double>::digits;
33 uint64 random_bits = base::RandUint64() & ((GG_UINT64_C(1) << kBits) - 1); 37 uint64 random_bits = bits & ((GG_UINT64_C(1) << kBits) - 1);
34 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); 38 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
35 DCHECK(result >= 0.0 && result < 1.0); 39 DCHECK(result >= 0.0 && result < 1.0);
36 return result; 40 return result;
37 } 41 }
38 42
39 uint64 RandGenerator(uint64 max) { 43 uint64 RandGenerator(uint64 max) {
40 DCHECK_GT(max, 0ULL); 44 DCHECK_GT(max, 0ULL);
41 return base::RandUint64() % max; 45 return base::RandUint64() % max;
42 } 46 }
43 47
44 } // namespace base 48 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698