kushi.p@gmail.com | e486977 | 2011-04-22 22:13:07 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [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 | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 7 | #include <limits.h> |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 8 | #include <math.h> |
tfarina | 0dae636 | 2015-05-19 07:14:09 +0900 | [diff] [blame] | 9 | #include <stdint.h> |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 10 | |
scottmg@chromium.org | fd621b9 | 2013-10-16 05:29:03 +0900 | [diff] [blame] | 11 | #include <algorithm> |
mark@chromium.org | c22c62b | 2008-09-30 23:26:33 +0900 | [diff] [blame] | 12 | #include <limits> |
| 13 | |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 14 | #include "base/logging.h" |
avi@chromium.org | 68a745c | 2013-06-11 05:11:14 +0900 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 16 | |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 17 | namespace base { |
| 18 | |
| 19 | int RandInt(int min, int max) { |
kushi.p@gmail.com | e486977 | 2011-04-22 22:13:07 +0900 | [diff] [blame] | 20 | DCHECK_LE(min, max); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 21 | |
Nico Weber | 1796efe | 2015-10-30 05:42:58 +0900 | [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))); |
kushi.p@gmail.com | 381e966 | 2011-05-04 10:29:38 +0900 | [diff] [blame] | 27 | DCHECK_GE(result, min); |
| 28 | DCHECK_LE(result, max); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 29 | return result; |
| 30 | } |
| 31 | |
| 32 | double RandDouble() { |
joi@chromium.org | df3561d | 2011-05-11 02:18:53 +0900 | [diff] [blame] | 33 | return BitsToOpenEndedUnitInterval(base::RandUint64()); |
| 34 | } |
| 35 | |
Nico Weber | 1796efe | 2015-10-30 05:42:58 +0900 | [diff] [blame] | 36 | double BitsToOpenEndedUnitInterval(uint64_t bits) { |
mark@chromium.org | c22c62b | 2008-09-30 23:26:33 +0900 | [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. |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 41 | |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 42 | static_assert(std::numeric_limits<double>::radix == 2, |
| 43 | "otherwise use scalbn"); |
mark@chromium.org | c22c62b | 2008-09-30 23:26:33 +0900 | [diff] [blame] | 44 | static const int kBits = std::numeric_limits<double>::digits; |
Nico Weber | 1796efe | 2015-10-30 05:42:58 +0900 | [diff] [blame] | 45 | uint64_t random_bits = bits & ((UINT64_C(1) << kBits) - 1); |
mark@chromium.org | c22c62b | 2008-09-30 23:26:33 +0900 | [diff] [blame] | 46 | double result = ldexp(static_cast<double>(random_bits), -1 * kBits); |
kushi.p@gmail.com | 381e966 | 2011-05-04 10:29:38 +0900 | [diff] [blame] | 47 | DCHECK_GE(result, 0.0); |
| 48 | DCHECK_LT(result, 1.0); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 49 | return result; |
| 50 | } |
| 51 | |
Nico Weber | 1796efe | 2015-10-30 05:42:58 +0900 | [diff] [blame] | 52 | uint64_t RandGenerator(uint64_t range) { |
dilmah@chromium.org | bd36573 | 2011-08-25 04:58:36 +0900 | [diff] [blame] | 53 | DCHECK_GT(range, 0u); |
joi@chromium.org | edf24fc | 2011-05-31 02:39:09 +0900 | [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 |
dilmah@chromium.org | bd36573 | 2011-08-25 04:58:36 +0900 | [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 | 1796efe | 2015-10-30 05:42:58 +0900 | [diff] [blame] | 58 | uint64_t max_acceptable_value = |
| 59 | (std::numeric_limits<uint64_t>::max() / range) * range - 1; |
joi@chromium.org | edf24fc | 2011-05-31 02:39:09 +0900 | [diff] [blame] | 60 | |
Nico Weber | 1796efe | 2015-10-30 05:42:58 +0900 | [diff] [blame] | 61 | uint64_t value; |
joi@chromium.org | edf24fc | 2011-05-31 02:39:09 +0900 | [diff] [blame] | 62 | do { |
| 63 | value = base::RandUint64(); |
dilmah@chromium.org | bd36573 | 2011-08-25 04:58:36 +0900 | [diff] [blame] | 64 | } while (value > max_acceptable_value); |
joi@chromium.org | edf24fc | 2011-05-31 02:39:09 +0900 | [diff] [blame] | 65 | |
dilmah@chromium.org | bd36573 | 2011-08-25 04:58:36 +0900 | [diff] [blame] | 66 | return value % range; |
isherman@chromium.org | 01eaa0b | 2010-08-31 06:07:05 +0900 | [diff] [blame] | 67 | } |
| 68 | |
qsr@google.com | fbda4d5 | 2011-05-05 17:46:11 +0900 | [diff] [blame] | 69 | std::string RandBytesAsString(size_t length) { |
pkasting@chromium.org | d347937 | 2011-11-30 05:06:18 +0900 | [diff] [blame] | 70 | DCHECK_GT(length, 0u); |
qsr@google.com | fbda4d5 | 2011-05-05 17:46:11 +0900 | [diff] [blame] | 71 | std::string result; |
| 72 | RandBytes(WriteInto(&result, length + 1), length); |
abarth@chromium.org | c19cb31 | 2011-04-30 06:03:54 +0900 | [diff] [blame] | 73 | return result; |
| 74 | } |
| 75 | |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 76 | } // namespace base |