Index: webrtc/rtc_base/random.cc |
diff --git a/webrtc/rtc_base/random.cc b/webrtc/rtc_base/random.cc |
index c0cf7fea71ac982ac5d653500935d44a9d31cb5b..3dddf53b23effd0f71ea1936ec7e6c80af1631b2 100644 |
--- a/webrtc/rtc_base/random.cc |
+++ b/webrtc/rtc_base/random.cc |
@@ -40,11 +40,9 @@ uint32_t Random::Rand(uint32_t low, uint32_t high) { |
int32_t Random::Rand(int32_t low, int32_t high) { |
RTC_DCHECK(low <= high); |
- // We rely on subtraction (and addition) to be the same for signed and |
- // unsigned numbers in two-complement representation. Thus, although |
- // high - low might be negative as an int, it is the correct difference |
- // when interpreted as an unsigned. |
- return Rand(high - low) + low; |
+ 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
|
+ return static_cast<int32_t>(Rand(static_cast<uint32_t>(high - low_i64)) + |
+ 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!
|
} |
template <> |