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/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" | 15 #include "webrtc/base/checks.h" |
16 | 16 |
17 namespace webrtc { | 17 namespace webrtc { |
18 | 18 |
19 namespace test { | 19 namespace test { |
20 | 20 |
21 Random::Random(uint32_t seed) : a_(0x531FDB97 ^ seed), b_(0x6420ECA8 + seed) { | 21 Random::Random(uint32_t seed) : a_(0x531FDB97 ^ seed), b_(0x6420ECA8 + seed) { |
22 } | 22 } |
23 | 23 |
24 float Random::Rand() { | 24 uint32_t Random::Rand(uint32_t t) { |
25 uint64_t result = t; | |
26 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.
| |
27 result = result >> 32; | |
pbos-webrtc
2015/11/06 10:35:21
result >>= 32;
terelius
2015/11/06 10:59:19
Done.
| |
28 a_ ^= b_; | |
29 b_ += a_; | |
30 return result; | |
31 } | |
32 | |
33 uint32_t Random::Rand(uint32_t low, uint32_t high) { | |
34 RTC_DCHECK(low <= high); | |
35 return Rand(high - low) + low; | |
36 } | |
37 | |
38 template <> | |
39 float Random::Rand<float>() { | |
25 const double kScale = 1.0f / (static_cast<uint64_t>(1) << 32); | 40 const double kScale = 1.0f / (static_cast<uint64_t>(1) << 32); |
26 double result = kScale * b_; | 41 double result = kScale * b_; |
27 a_ ^= b_; | 42 a_ ^= b_; |
28 b_ += a_; | 43 b_ += a_; |
29 return static_cast<float>(result); | 44 return static_cast<float>(result); |
30 } | 45 } |
31 | 46 |
32 int Random::Rand(int low, int high) { | 47 template <> |
33 RTC_DCHECK(low <= high); | 48 bool Random::Rand<bool>() { |
34 float uniform = Rand() * (high - low + 1) + low; | 49 return Rand(0, 1) == 1; |
35 return static_cast<int>(uniform); | |
36 } | 50 } |
37 | 51 |
38 int Random::Gaussian(int mean, int standard_deviation) { | 52 int Random::Gaussian(int mean, int standard_deviation) { |
39 // Creating a Normal distribution variable from two independent uniform | 53 // Creating a Normal distribution variable from two independent uniform |
40 // variables based on the Box-Muller transform, which is defined on the | 54 // variables based on the Box-Muller transform, which is defined on the |
41 // interval (0, 1], hence the mask+add below. | 55 // interval (0, 1], hence the mask+add below. |
42 const double kPi = 3.14159265358979323846; | 56 const double kPi = 3.14159265358979323846; |
43 const double kScale = 1.0 / 0x80000000ul; | 57 const double kScale = 1.0 / 0x80000000ul; |
44 double u1 = kScale * ((a_ & 0x7ffffffful) + 1); | 58 double u1 = kScale * ((a_ & 0x7ffffffful) + 1); |
45 double u2 = kScale * ((b_ & 0x7ffffffful) + 1); | 59 double u2 = kScale * ((b_ & 0x7ffffffful) + 1); |
46 a_ ^= b_; | 60 a_ ^= b_; |
47 b_ += a_; | 61 b_ += a_; |
48 return static_cast<int>( | 62 return static_cast<int>( |
49 mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2)); | 63 mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2)); |
50 } | 64 } |
51 | 65 |
52 int Random::Exponential(float lambda) { | 66 int Random::Exponential(float lambda) { |
53 float uniform = Rand(); | 67 float uniform = Rand<float>(); |
54 return static_cast<int>(-log(uniform) / lambda); | 68 return static_cast<int>(-log(uniform) / lambda); |
55 } | 69 } |
56 } // namespace test | 70 } // namespace test |
57 } // namespace webrtc | 71 } // namespace webrtc |
OLD | NEW |