[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/rand_util.h" |
| 6 | |
| 7 | #include <math.h> |
| 8 | |
[email protected] | c56bef4d | 2013-10-15 20:29:03 | [diff] [blame] | 9 | #include <algorithm> |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 10 | #include <limits> |
| 11 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 12 | #include "base/basictypes.h" |
| 13 | #include "base/logging.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 15 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 16 | namespace base { |
| 17 | |
| 18 | int RandInt(int min, int max) { |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 19 | DCHECK_LE(min, max); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 20 | |
[email protected] | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 21 | uint64 range = static_cast<uint64>(max) - min + 1; |
| 22 | int result = min + static_cast<int>(base::RandGenerator(range)); |
[email protected] | e1be56d | 2011-05-04 01:29:38 | [diff] [blame] | 23 | DCHECK_GE(result, min); |
| 24 | DCHECK_LE(result, max); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 25 | return result; |
| 26 | } |
| 27 | |
| 28 | double RandDouble() { |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 29 | return BitsToOpenEndedUnitInterval(base::RandUint64()); |
| 30 | } |
| 31 | |
| 32 | double BitsToOpenEndedUnitInterval(uint64 bits) { |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 33 | // 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 | // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa |
| 36 | // is expected to accommodate 53 bits. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 37 | |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 38 | COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); |
| 39 | static const int kBits = std::numeric_limits<double>::digits; |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 40 | uint64 random_bits = bits & ((GG_UINT64_C(1) << kBits) - 1); |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 41 | double result = ldexp(static_cast<double>(random_bits), -1 * kBits); |
[email protected] | e1be56d | 2011-05-04 01:29:38 | [diff] [blame] | 42 | DCHECK_GE(result, 0.0); |
| 43 | DCHECK_LT(result, 1.0); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 44 | return result; |
| 45 | } |
| 46 | |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 47 | uint64 RandGenerator(uint64 range) { |
| 48 | DCHECK_GT(range, 0u); |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 49 | // We must discard random results above this number, as they would |
| 50 | // make the random generator non-uniform (consider e.g. if |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 51 | // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice |
| 52 | // as likely as a result of 3 or 4). |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 53 | uint64 max_acceptable_value = |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 54 | (std::numeric_limits<uint64>::max() / range) * range - 1; |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 55 | |
| 56 | uint64 value; |
| 57 | do { |
| 58 | value = base::RandUint64(); |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 59 | } while (value > max_acceptable_value); |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 60 | |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 61 | return value % range; |
[email protected] | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 64 | void RandBytes(void* output, size_t output_length) { |
| 65 | uint64 random_int; |
| 66 | size_t random_int_size = sizeof(random_int); |
| 67 | for (size_t i = 0; i < output_length; i += random_int_size) { |
| 68 | random_int = base::RandUint64(); |
| 69 | size_t copy_count = std::min(output_length - i, random_int_size); |
| 70 | memcpy(((uint8*)output) + i, &random_int, copy_count); |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 71 | } |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 72 | } |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 73 | |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 74 | std::string RandBytesAsString(size_t length) { |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 75 | DCHECK_GT(length, 0u); |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 76 | std::string result; |
| 77 | RandBytes(WriteInto(&result, length + 1), length); |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 78 | return result; |
| 79 | } |
| 80 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 81 | } // namespace base |