Index: webrtc/base/random.h |
diff --git a/webrtc/test/random.h b/webrtc/base/random.h |
similarity index 63% |
rename from webrtc/test/random.h |
rename to webrtc/base/random.h |
index c7254a9b4946e41f6e62d333af79bfd45dad5e96..647b84c9c947b8d11f711012c9deb5e09b151d3f 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() { |
pbos-webrtc
2015/11/20 12:51:57
Do we need this generic template version or is sup
the sun
2015/11/25 10:12:37
It is quite useful; often you need to get a random
terelius
2015/11/27 11:31:23
I think pbos is suggesting not having the generic
|
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()); |
pbos-webrtc
2015/11/20 12:51:57
I believe this is undefined value/implementation m
pbos-webrtc
2015/11/25 21:16:16
We might accept this so long as ubsan doesn't star
terelius
2015/11/27 11:31:23
The best solution I've been able to come up with,
|
} |
// Uniformly distributed pseudo-random number in the interval [0, t]. |
@@ -41,18 +41,26 @@ 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); |
- |
- // TODO(solenberg): Random from histogram. |
- // template<typename T> int Distribution(const std::vector<T> histogram) { |
+ double Exponential(double lambda); |
private: |
- uint32_t a_; |
- uint32_t b_; |
+ // Outputs a nonzero 64-bit random number. |
+ uint64_t NextOutput() { |
pbos-webrtc
2015/11/20 12:51:57
Move implementation to .cc
terelius
2015/11/27 11:31:23
I actually prefer to have this inlined since the b
|
+ state_ ^= state_ >> 12; |
+ state_ ^= state_ << 25; |
+ state_ ^= state_ >> 27; |
+ RTC_DCHECK(state_ != 0x0ULL); |
+ return state_ * 2685821657736338717ull; |
+ } |
+ |
+ uint64_t state_; |
RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Random); |
}; |
@@ -61,11 +69,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_ |