| 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 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <limits> | 11 #include <limits> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/logging.h" | 13 #include "base/logging.h" |
| 15 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 16 | 15 |
| 17 namespace base { | 16 namespace base { |
| 18 | 17 |
| 19 int RandInt(int min, int max) { | 18 int RandInt(int min, int max) { |
| 20 DCHECK_LE(min, max); | 19 DCHECK_LE(min, max); |
| 21 | 20 |
| 22 uint64 range = static_cast<uint64>(max) - min + 1; | 21 uint64_t range = static_cast<uint64_t>(max) - min + 1; |
| 23 int result = min + static_cast<int>(base::RandGenerator(range)); | 22 // |range| is at most UINT_MAX + 1, so the result of RandGenerator(range) |
| 23 // is at most UINT_MAX. Hence it's safe to cast it from uint64_t to int64_t. |
| 24 int result = |
| 25 static_cast<int>(min + static_cast<int64_t>(base::RandGenerator(range))); |
| 24 DCHECK_GE(result, min); | 26 DCHECK_GE(result, min); |
| 25 DCHECK_LE(result, max); | 27 DCHECK_LE(result, max); |
| 26 return result; | 28 return result; |
| 27 } | 29 } |
| 28 | 30 |
| 29 double RandDouble() { | 31 double RandDouble() { |
| 30 return BitsToOpenEndedUnitInterval(base::RandUint64()); | 32 return BitsToOpenEndedUnitInterval(base::RandUint64()); |
| 31 } | 33 } |
| 32 | 34 |
| 33 double BitsToOpenEndedUnitInterval(uint64 bits) { | 35 double BitsToOpenEndedUnitInterval(uint64_t bits) { |
| 34 // We try to get maximum precision by masking out as many bits as will fit | 36 // We try to get maximum precision by masking out as many bits as will fit |
| 35 // in the target type's mantissa, and raising it to an appropriate power to | 37 // in the target type's mantissa, and raising it to an appropriate power to |
| 36 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa | 38 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa |
| 37 // is expected to accommodate 53 bits. | 39 // is expected to accommodate 53 bits. |
| 38 | 40 |
| 39 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); | 41 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); |
| 40 static const int kBits = std::numeric_limits<double>::digits; | 42 static const int kBits = std::numeric_limits<double>::digits; |
| 41 uint64 random_bits = bits & ((UINT64_C(1) << kBits) - 1); | 43 uint64_t random_bits = bits & ((UINT64_C(1) << kBits) - 1); |
| 42 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); | 44 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); |
| 43 DCHECK_GE(result, 0.0); | 45 DCHECK_GE(result, 0.0); |
| 44 DCHECK_LT(result, 1.0); | 46 DCHECK_LT(result, 1.0); |
| 45 return result; | 47 return result; |
| 46 } | 48 } |
| 47 | 49 |
| 48 uint64 RandGenerator(uint64 range) { | 50 uint64_t RandGenerator(uint64_t range) { |
| 49 DCHECK_GT(range, 0u); | 51 DCHECK_GT(range, 0u); |
| 50 // We must discard random results above this number, as they would | 52 // We must discard random results above this number, as they would |
| 51 // make the random generator non-uniform (consider e.g. if | 53 // make the random generator non-uniform (consider e.g. if |
| 52 // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice | 54 // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice |
| 53 // as likely as a result of 3 or 4). | 55 // as likely as a result of 3 or 4). |
| 54 uint64 max_acceptable_value = | 56 uint64_t max_acceptable_value = |
| 55 (std::numeric_limits<uint64>::max() / range) * range - 1; | 57 (std::numeric_limits<uint64_t>::max() / range) * range - 1; |
| 56 | 58 |
| 57 uint64 value; | 59 uint64_t value; |
| 58 do { | 60 do { |
| 59 value = base::RandUint64(); | 61 value = base::RandUint64(); |
| 60 } while (value > max_acceptable_value); | 62 } while (value > max_acceptable_value); |
| 61 | 63 |
| 62 return value % range; | 64 return value % range; |
| 63 } | 65 } |
| 64 | 66 |
| 65 std::string RandBytesAsString(size_t length) { | 67 std::string RandBytesAsString(size_t length) { |
| 66 DCHECK_GT(length, 0u); | 68 DCHECK_GT(length, 0u); |
| 67 std::string result; | 69 std::string result; |
| 68 RandBytes(WriteInto(&result, length + 1), length); | 70 RandBytes(WriteInto(&result, length + 1), length); |
| 69 return result; | 71 return result; |
| 70 } | 72 } |
| 71 | 73 |
| 72 } // namespace base | 74 } // namespace base |
| OLD | NEW |