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 #ifndef WEBRTC_TEST_RANDOM_H_ | 11 #ifndef WEBRTC_BASE_RANDOM_H_ |
12 #define WEBRTC_TEST_RANDOM_H_ | 12 #define WEBRTC_BASE_RANDOM_H_ |
13 | 13 |
14 #include <limits> | 14 #include <limits> |
15 | 15 |
16 #include "webrtc/typedefs.h" | 16 #include "webrtc/typedefs.h" |
17 #include "webrtc/base/constructormagic.h" | 17 #include "webrtc/base/constructormagic.h" |
18 #include "webrtc/base/checks.h" | |
18 | 19 |
19 namespace webrtc { | 20 namespace webrtc { |
20 | 21 |
21 namespace test { | |
22 | |
23 class Random { | 22 class Random { |
24 public: | 23 public: |
25 explicit Random(uint32_t seed); | 24 explicit Random(uint64_t seed); |
26 | 25 |
27 // Return pseudo-random integer of the specified type. | 26 // Return pseudo-random integer of the specified type. |
27 // We need to limit the size to 32 bits to keep the output close to uniform. | |
28 template <typename T> | 28 template <typename T> |
29 T Rand() { | 29 T Rand() { |
pbos-webrtc
2015/11/20 12:51:57
Do we need this generic template version or is sup
the sun
2015/11/25 10:12:37
It is quite useful; often you need to get a random
terelius
2015/11/27 11:31:23
I think pbos is suggesting not having the generic
| |
30 static_assert(std::numeric_limits<T>::is_integer && | 30 static_assert(std::numeric_limits<T>::is_integer && |
31 std::numeric_limits<T>::radix == 2 && | 31 std::numeric_limits<T>::radix == 2 && |
32 std::numeric_limits<T>::digits <= 32, | 32 std::numeric_limits<T>::digits <= 32, |
33 "Rand is only supported for built-in integer types that are " | 33 "Rand is only supported for built-in integer types that are " |
34 "32 bits or smaller."); | 34 "32 bits or smaller."); |
35 return static_cast<T>(Rand(std::numeric_limits<uint32_t>::max())); | 35 return static_cast<T>(NextOutput()); |
pbos-webrtc
2015/11/20 12:51:57
I believe this is undefined value/implementation m
pbos-webrtc
2015/11/25 21:16:16
We might accept this so long as ubsan doesn't star
terelius
2015/11/27 11:31:23
The best solution I've been able to come up with,
| |
36 } | 36 } |
37 | 37 |
38 // Uniformly distributed pseudo-random number in the interval [0, t]. | 38 // Uniformly distributed pseudo-random number in the interval [0, t]. |
39 uint32_t Rand(uint32_t t); | 39 uint32_t Rand(uint32_t t); |
40 | 40 |
41 // Uniformly distributed pseudo-random number in the interval [low, high]. | 41 // Uniformly distributed pseudo-random number in the interval [low, high]. |
42 uint32_t Rand(uint32_t low, uint32_t high); | 42 uint32_t Rand(uint32_t low, uint32_t high); |
43 | 43 |
44 // Uniformly distributed pseudo-random number in the interval [low, high]. | |
45 int32_t Rand(int32_t low, int32_t high); | |
46 | |
44 // Normal Distribution. | 47 // Normal Distribution. |
45 int Gaussian(int mean, int standard_deviation); | 48 double Gaussian(double mean, double standard_deviation); |
46 | 49 |
47 // Exponential Distribution. | 50 // Exponential Distribution. |
48 int Exponential(float lambda); | 51 double Exponential(double lambda); |
49 | |
50 // TODO(solenberg): Random from histogram. | |
51 // template<typename T> int Distribution(const std::vector<T> histogram) { | |
52 | 52 |
53 private: | 53 private: |
54 uint32_t a_; | 54 // Outputs a nonzero 64-bit random number. |
55 uint32_t b_; | 55 uint64_t NextOutput() { |
pbos-webrtc
2015/11/20 12:51:57
Move implementation to .cc
terelius
2015/11/27 11:31:23
I actually prefer to have this inlined since the b
| |
56 state_ ^= state_ >> 12; | |
57 state_ ^= state_ << 25; | |
58 state_ ^= state_ >> 27; | |
59 RTC_DCHECK(state_ != 0x0ULL); | |
60 return state_ * 2685821657736338717ull; | |
61 } | |
62 | |
63 uint64_t state_; | |
56 | 64 |
57 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Random); | 65 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Random); |
58 }; | 66 }; |
59 | 67 |
60 // Return pseudo-random number in the interval [0.0, 1.0). | 68 // Return pseudo-random number in the interval [0.0, 1.0). |
61 template <> | 69 template <> |
62 float Random::Rand<float>(); | 70 float Random::Rand<float>(); |
63 | 71 |
72 // Return pseudo-random number in the interval [0.0, 1.0). | |
73 template <> | |
74 double Random::Rand<double>(); | |
75 | |
64 // Return pseudo-random boolean value. | 76 // Return pseudo-random boolean value. |
65 template <> | 77 template <> |
66 bool Random::Rand<bool>(); | 78 bool Random::Rand<bool>(); |
67 | 79 |
68 } // namespace test | |
69 } // namespace webrtc | 80 } // namespace webrtc |
70 | 81 |
71 #endif // WEBRTC_TEST_RANDOM_H_ | 82 #endif // WEBRTC_BASE_RANDOM_H_ |
OLD | NEW |