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 #include "webrtc/rtc_base/random.h" | 10 #include "webrtc/rtc_base/random.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 return result; | 33 return result; |
| 34 } | 34 } |
| 35 | 35 |
| 36 uint32_t Random::Rand(uint32_t low, uint32_t high) { | 36 uint32_t Random::Rand(uint32_t low, uint32_t high) { |
| 37 RTC_DCHECK(low <= high); | 37 RTC_DCHECK(low <= high); |
| 38 return Rand(high - low) + low; | 38 return Rand(high - low) + low; |
| 39 } | 39 } |
| 40 | 40 |
| 41 int32_t Random::Rand(int32_t low, int32_t high) { | 41 int32_t Random::Rand(int32_t low, int32_t high) { |
| 42 RTC_DCHECK(low <= high); | 42 RTC_DCHECK(low <= high); |
| 43 // We rely on subtraction (and addition) to be the same for signed and | 43 const auto low_i64 = static_cast<int64_t>(low); |
|
kwiberg-webrtc
2017/09/04 23:09:50
This can be
const int64_t low_i64{low};
oprypin_webrtc
2017/09/05 07:27:34
I used the code you suggested in https://bugs.chro
kwiberg-webrtc
2017/09/05 08:37:30
Excellent. That matches my results from when I wro
| |
| 44 // unsigned numbers in two-complement representation. Thus, although | 44 return static_cast<int32_t>(Rand(static_cast<uint32_t>(high - low_i64)) + |
| 45 // high - low might be negative as an int, it is the correct difference | 45 low_i64); |
|
kwiberg-webrtc
2017/09/04 23:09:51
Both of these static_casts can be proven to always
oprypin_webrtc
2017/09/05 07:27:34
high - low_i64 always fits in uint32_t:
(2**31-1)
kwiberg-webrtc
2017/09/05 08:37:31
Thanks for double-checking the math!
| |
| 46 // when interpreted as an unsigned. | |
| 47 return Rand(high - low) + low; | |
| 48 } | 46 } |
| 49 | 47 |
| 50 template <> | 48 template <> |
| 51 float Random::Rand<float>() { | 49 float Random::Rand<float>() { |
| 52 double result = NextOutput() - 1; | 50 double result = NextOutput() - 1; |
| 53 result = result / 0xFFFFFFFFFFFFFFFEull; | 51 result = result / 0xFFFFFFFFFFFFFFFEull; |
| 54 return static_cast<float>(result); | 52 return static_cast<float>(result); |
| 55 } | 53 } |
| 56 | 54 |
| 57 template <> | 55 template <> |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 77 double u2 = static_cast<double>(NextOutput()) / 0xFFFFFFFFFFFFFFFFull; | 75 double u2 = static_cast<double>(NextOutput()) / 0xFFFFFFFFFFFFFFFFull; |
| 78 return mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2); | 76 return mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2); |
| 79 } | 77 } |
| 80 | 78 |
| 81 double Random::Exponential(double lambda) { | 79 double Random::Exponential(double lambda) { |
| 82 double uniform = Rand<double>(); | 80 double uniform = Rand<double>(); |
| 83 return -log(uniform) / lambda; | 81 return -log(uniform) / lambda; |
| 84 } | 82 } |
| 85 | 83 |
| 86 } // namespace webrtc | 84 } // namespace webrtc |
| OLD | NEW |