Index: webrtc/test/random.h |
diff --git a/webrtc/test/random.h b/webrtc/test/random.h |
index 5cc54f212970c8269cf2093c199ef6be55fb4a45..cd3b799ba4995c5c1f643917eca269bd22732670 100644 |
--- a/webrtc/test/random.h |
+++ b/webrtc/test/random.h |
@@ -11,6 +11,8 @@ |
#ifndef WEBRTC_TEST_RANDOM_H_ |
#define WEBRTC_TEST_RANDOM_H_ |
+#include <limits> |
+ |
#include "webrtc/typedefs.h" |
#include "webrtc/base/constructormagic.h" |
@@ -25,8 +27,26 @@ class Random { |
// Return pseudo-random number in the interval [0.0, 1.0). |
float Rand(); |
- // Return pseudo-random number mapped to the interval [low, high]. |
- int Rand(int low, int high); |
+ template <typename T> |
+ T Rand() { |
the sun
2015/10/22 12:30:38
This is very pretty!
Why are some functions calle
terelius
2015/10/26 18:18:41
I considered both ideas, but couldn't make up my m
the sun
2015/10/27 09:03:12
Ok, I understand your argument. I think we should
terelius
2015/10/29 12:54:04
But isn't it a bit weird (and error prone) to over
|
+ static_assert(std::numeric_limits<T>::is_integer && |
+ std::numeric_limits<T>::radix == 2 && |
+ 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>(Uniform(std::numeric_limits<uint32_t>::max())); |
+ } |
+ |
+ template <bool> |
+ bool Rand() { |
+ return Uniform(0, 1) == 1; |
+ } |
+ |
+ // Uniformly distributed pseudo-random number in the interval [0, t]. |
+ uint32_t Uniform(uint32_t t); |
+ |
+ // Uniformly distributed pseudo-random number in the interval [low, high]. |
+ uint32_t Uniform(uint32_t low, uint32_t high); |
// Normal Distribution. |
int Gaussian(int mean, int standard_deviation); |