[email protected] | ead8c1fa | 2012-05-30 14:26:13 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | #ifndef BASE_RAND_UTIL_H_ |
| 6 | #define BASE_RAND_UTIL_H_ |
| 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
tzik | 5de2157f | 2018-05-08 03:42:47 | [diff] [blame] | 11 | #include <algorithm> |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 12 | #include <string> |
| 13 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 14 | #include "base/base_export.h" |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 19 | // Returns a random number in range [0, UINT64_MAX]. Thread-safe. |
| 20 | BASE_EXPORT uint64_t RandUint64(); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 21 | |
| 22 | // Returns a random number between min and max (inclusive). Thread-safe. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 23 | BASE_EXPORT int RandInt(int min, int max); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 24 | |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 25 | // Returns a random number in range [0, range). Thread-safe. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 26 | BASE_EXPORT uint64_t RandGenerator(uint64_t range); |
[email protected] | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 27 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 28 | // Returns a random double in range [0, 1). Thread-safe. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 29 | BASE_EXPORT double RandDouble(); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 30 | |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 31 | // Given input |bits|, convert with maximum precision to a double in |
| 32 | // the range [0, 1). Thread-safe. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 33 | BASE_EXPORT double BitsToOpenEndedUnitInterval(uint64_t bits); |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 34 | |
John Mellor | afab972d | 2017-09-26 16:28:19 | [diff] [blame] | 35 | // Fills |output_length| bytes of |output| with random data. Thread-safe. |
[email protected] | 9b20578 | 2012-08-02 20:22:25 | [diff] [blame] | 36 | // |
John Mellor | afab972d | 2017-09-26 16:28:19 | [diff] [blame] | 37 | // Although implementations are required to use a cryptographically secure |
| 38 | // random number source, code outside of base/ that relies on this should use |
| 39 | // crypto::RandBytes instead to ensure the requirement is easily discoverable. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 40 | BASE_EXPORT void RandBytes(void* output, size_t output_length); |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 41 | |
[email protected] | 3818dd0 | 2014-05-13 05:56:19 | [diff] [blame] | 42 | // Fills a string of length |length| with random data and returns it. |
John Mellor | afab972d | 2017-09-26 16:28:19 | [diff] [blame] | 43 | // |length| should be nonzero. Thread-safe. |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 44 | // |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 45 | // Note that this is a variation of |RandBytes| with a different return type. |
[email protected] | 3818dd0 | 2014-05-13 05:56:19 | [diff] [blame] | 46 | // The returned string is likely not ASCII/UTF-8. Use with care. |
[email protected] | 9b20578 | 2012-08-02 20:22:25 | [diff] [blame] | 47 | // |
John Mellor | afab972d | 2017-09-26 16:28:19 | [diff] [blame] | 48 | // Although implementations are required to use a cryptographically secure |
| 49 | // random number source, code outside of base/ that relies on this should use |
| 50 | // crypto::RandBytes instead to ensure the requirement is easily discoverable. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 51 | BASE_EXPORT std::string RandBytesAsString(size_t length); |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 52 | |
tzik | 5de2157f | 2018-05-08 03:42:47 | [diff] [blame] | 53 | // An STL UniformRandomBitGenerator backed by RandUint64. |
| 54 | // TODO(tzik): Consider replacing this with a faster implementation. |
| 55 | class RandomBitGenerator { |
| 56 | public: |
| 57 | using result_type = uint64_t; |
| 58 | static constexpr result_type min() { return 0; } |
| 59 | static constexpr result_type max() { return UINT64_MAX; } |
| 60 | result_type operator()() const { return RandUint64(); } |
| 61 | |
| 62 | RandomBitGenerator() = default; |
| 63 | ~RandomBitGenerator() = default; |
| 64 | }; |
| 65 | |
| 66 | // Shuffles [first, last) randomly. Thread-safe. |
| 67 | template <typename Itr> |
| 68 | void RandomShuffle(Itr first, Itr last) { |
| 69 | std::shuffle(first, last, RandomBitGenerator()); |
| 70 | } |
| 71 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 72 | #if defined(OS_POSIX) |
[email protected] | ead8c1fa | 2012-05-30 14:26:13 | [diff] [blame] | 73 | BASE_EXPORT int GetUrandomFD(); |
| 74 | #endif |
| 75 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 76 | } // namespace base |
| 77 | |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 78 | #endif // BASE_RAND_UTIL_H_ |