Chromium Code Reviews| Index: webrtc/base/random.h |
| diff --git a/webrtc/test/random.h b/webrtc/base/random.h |
| similarity index 65% |
| rename from webrtc/test/random.h |
| rename to webrtc/base/random.h |
| index c7254a9b4946e41f6e62d333af79bfd45dad5e96..7bf6af278be7baa3177ac2e7d4e7033a9b428868 100644 |
| --- a/webrtc/test/random.h |
| +++ b/webrtc/base/random.h |
| @@ -8,23 +8,23 @@ |
| * be found in the AUTHORS file in the root of the source tree. |
| */ |
| -#ifndef WEBRTC_TEST_RANDOM_H_ |
| -#define WEBRTC_TEST_RANDOM_H_ |
| +#ifndef WEBRTC_BASE_RANDOM_H_ |
| +#define WEBRTC_BASE_RANDOM_H_ |
| #include <limits> |
| #include "webrtc/typedefs.h" |
| #include "webrtc/base/constructormagic.h" |
| +#include "webrtc/base/checks.h" |
| namespace webrtc { |
| -namespace test { |
| - |
| class Random { |
| public: |
| - explicit Random(uint32_t seed); |
| + explicit Random(uint64_t seed); |
| // Return pseudo-random integer of the specified type. |
| + // We need to limit the size to 32 bits to keep the output close to uniform. |
| template <typename T> |
| T Rand() { |
| static_assert(std::numeric_limits<T>::is_integer && |
| @@ -32,7 +32,7 @@ class Random { |
| std::numeric_limits<T>::digits <= 32, |
| "Rand is only supported for built-in integer types that are " |
| "32 bits or smaller."); |
| - return static_cast<T>(Rand(std::numeric_limits<uint32_t>::max())); |
| + return static_cast<T>(NextOutput()); |
| } |
| // Uniformly distributed pseudo-random number in the interval [0, t]. |
| @@ -41,18 +41,29 @@ class Random { |
| // Uniformly distributed pseudo-random number in the interval [low, high]. |
| uint32_t Rand(uint32_t low, uint32_t high); |
| + // Uniformly distributed pseudo-random number in the interval [low, high]. |
| + int32_t Rand(int32_t low, int32_t high); |
| + |
| // Normal Distribution. |
| - int Gaussian(int mean, int standard_deviation); |
| + double Gaussian(double mean, double standard_deviation); |
| // Exponential Distribution. |
| - int Exponential(float lambda); |
| + double Exponential(double lambda); |
| // TODO(solenberg): Random from histogram. |
|
the sun
2015/11/20 09:38:50
You can remove this TODO, or take over it. :)
terelius
2015/11/20 10:32:29
Ok, removed it.
|
| // template<typename T> int Distribution(const std::vector<T> histogram) { |
| private: |
| - uint32_t a_; |
| - uint32_t b_; |
| + // Outputs a nonzero 64-bit random number. |
| + uint64_t NextOutput() { |
| + state_ ^= state_ >> 12; |
| + state_ ^= state_ << 25; |
| + state_ ^= state_ >> 27; |
| + RTC_DCHECK(state_ != 0x0ULL); |
|
the sun
2015/11/20 09:38:50
If the output *by definition* can never be zero, t
terelius
2015/11/20 10:32:29
The 64-bit state can never be 0 but the integer fu
The Sun (google.com)
2015/11/20 11:37:18
Ah, I should have read the implementation; thanks
|
| + return state_ * 2685821657736338717ull; |
| + } |
| + |
| + uint64_t state_; |
| RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Random); |
| }; |
| @@ -61,11 +72,14 @@ class Random { |
| template <> |
| float Random::Rand<float>(); |
| +// Return pseudo-random number in the interval [0.0, 1.0). |
| +template <> |
| +double Random::Rand<double>(); |
| + |
| // Return pseudo-random boolean value. |
| template <> |
| bool Random::Rand<bool>(); |
| -} // namespace test |
| } // namespace webrtc |
| -#endif // WEBRTC_TEST_RANDOM_H_ |
| +#endif // WEBRTC_BASE_RANDOM_H_ |