| OLD | NEW |
| 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 #include <stdint.h> |
| 8 | 9 |
| 9 #include <algorithm> | 10 #include <algorithm> |
| 10 #include <limits> | 11 #include <limits> |
| 11 | 12 |
| 12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 | 18 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 30 } | 31 } |
| 31 | 32 |
| 32 double BitsToOpenEndedUnitInterval(uint64 bits) { | 33 double BitsToOpenEndedUnitInterval(uint64 bits) { |
| 33 // We try to get maximum precision by masking out as many bits as will fit | 34 // We try to get maximum precision by masking out as many bits as will fit |
| 34 // in the target type's mantissa, and raising it to an appropriate power to | 35 // in the target type's mantissa, and raising it to an appropriate power to |
| 35 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa | 36 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa |
| 36 // is expected to accommodate 53 bits. | 37 // is expected to accommodate 53 bits. |
| 37 | 38 |
| 38 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); | 39 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); |
| 39 static const int kBits = std::numeric_limits<double>::digits; | 40 static const int kBits = std::numeric_limits<double>::digits; |
| 40 uint64 random_bits = bits & ((GG_UINT64_C(1) << kBits) - 1); | 41 uint64 random_bits = bits & ((UINT64_C(1) << kBits) - 1); |
| 41 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); | 42 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); |
| 42 DCHECK_GE(result, 0.0); | 43 DCHECK_GE(result, 0.0); |
| 43 DCHECK_LT(result, 1.0); | 44 DCHECK_LT(result, 1.0); |
| 44 return result; | 45 return result; |
| 45 } | 46 } |
| 46 | 47 |
| 47 uint64 RandGenerator(uint64 range) { | 48 uint64 RandGenerator(uint64 range) { |
| 48 DCHECK_GT(range, 0u); | 49 DCHECK_GT(range, 0u); |
| 49 // We must discard random results above this number, as they would | 50 // We must discard random results above this number, as they would |
| 50 // make the random generator non-uniform (consider e.g. if | 51 // make the random generator non-uniform (consider e.g. if |
| (...skipping 11 matching lines...) Expand all Loading... |
| 62 } | 63 } |
| 63 | 64 |
| 64 std::string RandBytesAsString(size_t length) { | 65 std::string RandBytesAsString(size_t length) { |
| 65 DCHECK_GT(length, 0u); | 66 DCHECK_GT(length, 0u); |
| 66 std::string result; | 67 std::string result; |
| 67 RandBytes(WriteInto(&result, length + 1), length); | 68 RandBytes(WriteInto(&result, length + 1), length); |
| 68 return result; | 69 return result; |
| 69 } | 70 } |
| 70 | 71 |
| 71 } // namespace base | 72 } // namespace base |
| OLD | NEW |