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

Unified Diff: webrtc/test/random.cc

Issue 1413053002: Change to use local Random object instead of global rand() in the RtcEventLog unit test. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebase Created 5 years, 1 month 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/test/random.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/test/random.cc
diff --git a/webrtc/test/random.cc b/webrtc/test/random.cc
index c4c405f6b8884732de3d3bc7e95ddb80f026190a..b9b599e264f624d6077df74b1d0bf4499ed86b78 100644
--- a/webrtc/test/random.cc
+++ b/webrtc/test/random.cc
@@ -21,7 +21,22 @@ namespace test {
Random::Random(uint32_t seed) : a_(0x531FDB97 ^ seed), b_(0x6420ECA8 + seed) {
}
-float Random::Rand() {
+uint32_t Random::Rand(uint32_t t) {
+ uint64_t result = t;
+ result = b_ * (result + 1);
pbos-webrtc 2015/11/06 10:35:21 static_cast<uint64_t>(t) instead of result, that m
terelius 2015/11/06 10:59:19 Done.
+ result = result >> 32;
pbos-webrtc 2015/11/06 10:35:21 result >>= 32;
terelius 2015/11/06 10:59:19 Done.
+ a_ ^= b_;
+ b_ += a_;
+ return result;
+}
+
+uint32_t Random::Rand(uint32_t low, uint32_t high) {
+ RTC_DCHECK(low <= high);
+ return Rand(high - low) + low;
+}
+
+template <>
+float Random::Rand<float>() {
const double kScale = 1.0f / (static_cast<uint64_t>(1) << 32);
double result = kScale * b_;
a_ ^= b_;
@@ -29,10 +44,9 @@ float Random::Rand() {
return static_cast<float>(result);
}
-int Random::Rand(int low, int high) {
- RTC_DCHECK(low <= high);
- float uniform = Rand() * (high - low + 1) + low;
- return static_cast<int>(uniform);
+template <>
+bool Random::Rand<bool>() {
+ return Rand(0, 1) == 1;
}
int Random::Gaussian(int mean, int standard_deviation) {
@@ -50,7 +64,7 @@ int Random::Gaussian(int mean, int standard_deviation) {
}
int Random::Exponential(float lambda) {
- float uniform = Rand();
+ float uniform = Rand<float>();
return static_cast<int>(-log(uniform) / lambda);
}
} // namespace test
« no previous file with comments | « webrtc/test/random.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698