stefan | c62642c | 2015-07-07 04:20:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_RANDOM_H_ |
| 12 | #define RTC_BASE_RANDOM_H_ |
stefan | c62642c | 2015-07-07 04:20:34 -0700 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 15 | #include <limits> |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/constructor_magic.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | class Random { |
| 23 | public: |
| 24 | // TODO(tommi): Change this so that the seed can be initialized internally, |
| 25 | // e.g. by offering two ways of constructing or offer a static method that |
| 26 | // returns a seed that's suitable for initialization. |
| 27 | // The problem now is that callers are calling clock_->TimeInMicroseconds() |
| 28 | // which calls TickTime::Now().Ticks(), which can return a very low value on |
| 29 | // Mac and can result in a seed of 0 after conversion to microseconds. |
| 30 | // Besides the quality of the random seed being poor, this also requires |
| 31 | // the client to take on extra dependencies to generate a seed. |
| 32 | // If we go for a static seed generator in Random, we can use something from |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 33 | // webrtc/rtc_base and make sure that it works the same way across platforms. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 34 | // See also discussion here: https://codereview.webrtc.org/1623543002/ |
| 35 | explicit Random(uint64_t seed); |
| 36 | |
| 37 | // Return pseudo-random integer of the specified type. |
| 38 | // We need to limit the size to 32 bits to keep the output close to uniform. |
| 39 | template <typename T> |
| 40 | T Rand() { |
| 41 | static_assert(std::numeric_limits<T>::is_integer && |
| 42 | std::numeric_limits<T>::radix == 2 && |
| 43 | std::numeric_limits<T>::digits <= 32, |
| 44 | "Rand is only supported for built-in integer types that are " |
| 45 | "32 bits or smaller."); |
| 46 | return static_cast<T>(NextOutput()); |
| 47 | } |
| 48 | |
| 49 | // Uniformly distributed pseudo-random number in the interval [0, t]. |
| 50 | uint32_t Rand(uint32_t t); |
| 51 | |
| 52 | // Uniformly distributed pseudo-random number in the interval [low, high]. |
| 53 | uint32_t Rand(uint32_t low, uint32_t high); |
| 54 | |
| 55 | // Uniformly distributed pseudo-random number in the interval [low, high]. |
| 56 | int32_t Rand(int32_t low, int32_t high); |
| 57 | |
| 58 | // Normal Distribution. |
| 59 | double Gaussian(double mean, double standard_deviation); |
| 60 | |
| 61 | // Exponential Distribution. |
| 62 | double Exponential(double lambda); |
| 63 | |
| 64 | private: |
| 65 | // Outputs a nonzero 64-bit random number. |
| 66 | uint64_t NextOutput() { |
| 67 | state_ ^= state_ >> 12; |
| 68 | state_ ^= state_ << 25; |
| 69 | state_ ^= state_ >> 27; |
| 70 | RTC_DCHECK(state_ != 0x0ULL); |
| 71 | return state_ * 2685821657736338717ull; |
| 72 | } |
| 73 | |
| 74 | uint64_t state_; |
| 75 | |
| 76 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Random); |
| 77 | }; |
| 78 | |
| 79 | // Return pseudo-random number in the interval [0.0, 1.0). |
| 80 | template <> |
| 81 | float Random::Rand<float>(); |
| 82 | |
| 83 | // Return pseudo-random number in the interval [0.0, 1.0). |
| 84 | template <> |
| 85 | double Random::Rand<double>(); |
| 86 | |
| 87 | // Return pseudo-random boolean value. |
| 88 | template <> |
| 89 | bool Random::Rand<bool>(); |
| 90 | |
| 91 | } // namespace webrtc |
stefan | c62642c | 2015-07-07 04:20:34 -0700 | [diff] [blame] | 92 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 93 | #endif // RTC_BASE_RANDOM_H_ |