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 #ifndef WEBRTC_BASE_RANDOM_H_ | 11 #ifndef WEBRTC_BASE_RANDOM_H_ |
| 12 #define WEBRTC_BASE_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 #include "webrtc/base/checks.h" |
| 19 | 19 |
| 20 namespace webrtc { | 20 namespace webrtc { |
| 21 | 21 |
| 22 class Random { | 22 class Random { |
| 23 public: | 23 public: |
| 24 // TODO(tommi): Change this so that the seed is initialized internally. | |
|
the sun
2016/02/01 10:41:28
Please remove this comment. I believe the right de
tommi
2016/02/01 17:32:03
Yeah, so, I'm on the fence about that. On one sid
pbos-webrtc
2016/02/01 17:59:25
Sounds like a case for having two constructors whe
| |
| 25 // At the moment all callers are calling clock_->TimeInMicroseconds() which | |
| 26 // goes through a global clock object (+vtable) and eventually calls | |
| 27 // TickTime::Now().Ticks(), which can return a very low value on Mac, which | |
| 28 // can result in a seed of 0 after conversion to microseconds. | |
| 29 // Instead see if we can use something from webrtc/base that works the same | |
| 30 // way across platforms (and is itself more random than the Mac implementation | |
| 31 // of TickTime::Now()). | |
| 24 explicit Random(uint64_t seed); | 32 explicit Random(uint64_t seed); |
| 25 | 33 |
| 26 // Return pseudo-random integer of the specified type. | 34 // 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. | 35 // We need to limit the size to 32 bits to keep the output close to uniform. |
| 28 template <typename T> | 36 template <typename T> |
| 29 T Rand() { | 37 T Rand() { |
| 30 static_assert(std::numeric_limits<T>::is_integer && | 38 static_assert(std::numeric_limits<T>::is_integer && |
| 31 std::numeric_limits<T>::radix == 2 && | 39 std::numeric_limits<T>::radix == 2 && |
| 32 std::numeric_limits<T>::digits <= 32, | 40 std::numeric_limits<T>::digits <= 32, |
| 33 "Rand is only supported for built-in integer types that are " | 41 "Rand is only supported for built-in integer types that are " |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 template <> | 81 template <> |
| 74 double Random::Rand<double>(); | 82 double Random::Rand<double>(); |
| 75 | 83 |
| 76 // Return pseudo-random boolean value. | 84 // Return pseudo-random boolean value. |
| 77 template <> | 85 template <> |
| 78 bool Random::Rand<bool>(); | 86 bool Random::Rand<bool>(); |
| 79 | 87 |
| 80 } // namespace webrtc | 88 } // namespace webrtc |
| 81 | 89 |
| 82 #endif // WEBRTC_BASE_RANDOM_H_ | 90 #endif // WEBRTC_BASE_RANDOM_H_ |
| OLD | NEW |