OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 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 |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/modules/remote_bitrate_estimator/test/random.h" |
| 12 |
| 13 #include <math.h> |
| 14 |
| 15 Random::Random(uint32_t seed) : a_(0x531FDB97 ^ seed), b_(0x6420ECA8 + seed) { |
| 16 } |
| 17 |
| 18 float Random::Rand() { |
| 19 const float kScale = 1.0f / 0xffffffff; |
| 20 float result = kScale * b_; |
| 21 a_ ^= b_; |
| 22 b_ += a_; |
| 23 return result; |
| 24 } |
| 25 |
| 26 int Random::Gaussian(int mean, int standard_deviation) { |
| 27 // Creating a Normal distribution variable from two independent uniform |
| 28 // variables based on the Box-Muller transform, which is defined on the |
| 29 // interval (0, 1], hence the mask+add below. |
| 30 const double kPi = 3.14159265358979323846; |
| 31 const double kScale = 1.0 / 0x80000000ul; |
| 32 double u1 = kScale * ((a_ & 0x7ffffffful) + 1); |
| 33 double u2 = kScale * ((b_ & 0x7ffffffful) + 1); |
| 34 a_ ^= b_; |
| 35 b_ += a_; |
| 36 return static_cast<int>( |
| 37 mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2)); |
| 38 } |
OLD | NEW |