Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Unified Diff: webrtc/base/random.h

Issue 1457023002: Rewrote the PRNG using an xorshift* algorithm and moved the files from test/ to base/. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebase Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/base/base_tests.gyp ('k') | webrtc/base/random.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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() {
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,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() {
+ 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_
« no previous file with comments | « webrtc/base/base_tests.gyp ('k') | webrtc/base/random.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698