Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/remote_bitrate_estimator/test/random.h" | 11 #include "webrtc/test/random.h" |
| 12 | 12 |
| 13 #include <math.h> | 13 #include <math.h> |
| 14 | 14 |
| 15 #include "webrtc/base/checks.h" | |
| 16 | |
| 15 namespace webrtc { | 17 namespace webrtc { |
| 16 | 18 |
| 19 namespace test { | |
| 20 | |
| 17 Random::Random(uint32_t seed) : a_(0x531FDB97 ^ seed), b_(0x6420ECA8 + seed) { | 21 Random::Random(uint32_t seed) : a_(0x531FDB97 ^ seed), b_(0x6420ECA8 + seed) { |
| 18 } | 22 } |
| 19 | 23 |
| 20 float Random::Rand() { | 24 float Random::Rand() { |
| 21 const float kScale = 1.0f / 0xffffffff; | 25 const float kScale = 1.0f / 0xffffffff; |
| 22 float result = kScale * b_; | 26 float result = kScale * b_; |
| 23 a_ ^= b_; | 27 a_ ^= b_; |
| 24 b_ += a_; | 28 b_ += a_; |
| 25 return result; | 29 return result; |
| 26 } | 30 } |
| 27 | 31 |
| 32 int Random::Rand(int low, int high) { | |
| 33 RTC_DCHECK(low <= high); | |
| 34 float uniform = Rand() * (high - low + 1) + low; | |
|
pbos-webrtc
2015/10/19 13:33:01
Then this one has off-by-one errors?
for Rand() =
the sun
2015/10/19 15:10:25
Looks like that. Good catch!
https://codereview.we
| |
| 35 return static_cast<int>(uniform); | |
| 36 } | |
| 37 | |
| 28 int Random::Gaussian(int mean, int standard_deviation) { | 38 int Random::Gaussian(int mean, int standard_deviation) { |
| 29 // Creating a Normal distribution variable from two independent uniform | 39 // Creating a Normal distribution variable from two independent uniform |
| 30 // variables based on the Box-Muller transform, which is defined on the | 40 // variables based on the Box-Muller transform, which is defined on the |
| 31 // interval (0, 1], hence the mask+add below. | 41 // interval (0, 1], hence the mask+add below. |
| 32 const double kPi = 3.14159265358979323846; | 42 const double kPi = 3.14159265358979323846; |
| 33 const double kScale = 1.0 / 0x80000000ul; | 43 const double kScale = 1.0 / 0x80000000ul; |
| 34 double u1 = kScale * ((a_ & 0x7ffffffful) + 1); | 44 double u1 = kScale * ((a_ & 0x7ffffffful) + 1); |
| 35 double u2 = kScale * ((b_ & 0x7ffffffful) + 1); | 45 double u2 = kScale * ((b_ & 0x7ffffffful) + 1); |
| 36 a_ ^= b_; | 46 a_ ^= b_; |
| 37 b_ += a_; | 47 b_ += a_; |
| 38 return static_cast<int>( | 48 return static_cast<int>( |
| 39 mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2)); | 49 mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2)); |
| 40 } | 50 } |
| 51 | |
| 52 int Random::Exponential(float lambda) { | |
| 53 float uniform = Rand(); | |
| 54 return static_cast<int>(-log(uniform) / lambda); | |
| 55 } | |
| 56 } // namespace test | |
| 41 } // namespace webrtc | 57 } // namespace webrtc |
| OLD | NEW |