[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 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 7 | #include <limits.h> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 8 | #include <math.h> |
tfarina | 3208992 | 2015-05-18 22:14:09 | [diff] [blame] | 9 | #include <stdint.h> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 10 | |
[email protected] | c56bef4d | 2013-10-15 20:29:03 | [diff] [blame] | 11 | #include <algorithm> |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 12 | #include <limits> |
| 13 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 14 | #include "base/logging.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 16 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 17 | namespace base { |
| 18 | |
| 19 | int RandInt(int min, int max) { |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 20 | DCHECK_LE(min, max); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 21 | |
Nico Weber | 0a3852a7 | 2015-10-29 20:42:58 | [diff] [blame] | 22 | uint64_t range = static_cast<uint64_t>(max) - min + 1; |
| 23 | // |range| is at most UINT_MAX + 1, so the result of RandGenerator(range) |
| 24 | // is at most UINT_MAX. Hence it's safe to cast it from uint64_t to int64_t. |
| 25 | int result = |
| 26 | static_cast<int>(min + static_cast<int64_t>(base::RandGenerator(range))); |
[email protected] | e1be56d | 2011-05-04 01:29:38 | [diff] [blame] | 27 | DCHECK_GE(result, min); |
| 28 | DCHECK_LE(result, max); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 29 | return result; |
| 30 | } |
| 31 | |
| 32 | double RandDouble() { |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 33 | return BitsToOpenEndedUnitInterval(base::RandUint64()); |
| 34 | } |
| 35 | |
Nico Weber | 0a3852a7 | 2015-10-29 20:42:58 | [diff] [blame] | 36 | double BitsToOpenEndedUnitInterval(uint64_t bits) { |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 37 | // We try to get maximum precision by masking out as many bits as will fit |
| 38 | // in the target type's mantissa, and raising it to an appropriate power to |
| 39 | // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa |
| 40 | // is expected to accommodate 53 bits. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 41 | |
avi | 4ec0dff | 2015-11-24 14:26:24 | [diff] [blame] | 42 | static_assert(std::numeric_limits<double>::radix == 2, |
| 43 | "otherwise use scalbn"); |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 44 | static const int kBits = std::numeric_limits<double>::digits; |
Nico Weber | 0a3852a7 | 2015-10-29 20:42:58 | [diff] [blame] | 45 | uint64_t random_bits = bits & ((UINT64_C(1) << kBits) - 1); |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 46 | double result = ldexp(static_cast<double>(random_bits), -1 * kBits); |
[email protected] | e1be56d | 2011-05-04 01:29:38 | [diff] [blame] | 47 | DCHECK_GE(result, 0.0); |
| 48 | DCHECK_LT(result, 1.0); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 49 | return result; |
| 50 | } |
| 51 | |
Nico Weber | 0a3852a7 | 2015-10-29 20:42:58 | [diff] [blame] | 52 | uint64_t RandGenerator(uint64_t range) { |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 53 | DCHECK_GT(range, 0u); |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 54 | // We must discard random results above this number, as they would |
| 55 | // make the random generator non-uniform (consider e.g. if |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 56 | // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice |
| 57 | // as likely as a result of 3 or 4). |
Nico Weber | 0a3852a7 | 2015-10-29 20:42:58 | [diff] [blame] | 58 | uint64_t max_acceptable_value = |
| 59 | (std::numeric_limits<uint64_t>::max() / range) * range - 1; |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 60 | |
Nico Weber | 0a3852a7 | 2015-10-29 20:42:58 | [diff] [blame] | 61 | uint64_t value; |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 62 | do { |
| 63 | value = base::RandUint64(); |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 64 | } while (value > max_acceptable_value); |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 65 | |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 66 | return value % range; |
[email protected] | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 69 | std::string RandBytesAsString(size_t length) { |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 70 | DCHECK_GT(length, 0u); |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 71 | std::string result; |
| 72 | RandBytes(WriteInto(&result, length + 1), length); |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 73 | return result; |
| 74 | } |
| 75 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 76 | } // namespace base |