You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/random.h"
  11. #include <math.h>
  12. #include "rtc_base/checks.h"
  13. #include "rtc_base/numerics/safe_conversions.h"
  14. namespace webrtc {
  15. Random::Random(uint64_t seed) {
  16. RTC_DCHECK(seed != 0x0ull);
  17. state_ = seed;
  18. }
  19. uint32_t Random::Rand(uint32_t t) {
  20. // Casting the output to 32 bits will give an almost uniform number.
  21. // Pr[x=0] = (2^32-1) / (2^64-1)
  22. // Pr[x=k] = 2^32 / (2^64-1) for k!=0
  23. // Uniform would be Pr[x=k] = 2^32 / 2^64 for all 32-bit integers k.
  24. uint32_t x = NextOutput();
  25. // If x / 2^32 is uniform on [0,1), then x / 2^32 * (t+1) is uniform on
  26. // the interval [0,t+1), so the integer part is uniform on [0,t].
  27. uint64_t result = x * (static_cast<uint64_t>(t) + 1);
  28. result >>= 32;
  29. return result;
  30. }
  31. uint32_t Random::Rand(uint32_t low, uint32_t high) {
  32. RTC_DCHECK(low <= high);
  33. return Rand(high - low) + low;
  34. }
  35. int32_t Random::Rand(int32_t low, int32_t high) {
  36. RTC_DCHECK(low <= high);
  37. const int64_t low_i64{low};
  38. return rtc::dchecked_cast<int32_t>(
  39. Rand(rtc::dchecked_cast<uint32_t>(high - low_i64)) + low_i64);
  40. }
  41. template <>
  42. float Random::Rand<float>() {
  43. double result = NextOutput() - 1;
  44. result = result / 0xFFFFFFFFFFFFFFFEull;
  45. return static_cast<float>(result);
  46. }
  47. template <>
  48. double Random::Rand<double>() {
  49. double result = NextOutput() - 1;
  50. result = result / 0xFFFFFFFFFFFFFFFEull;
  51. return result;
  52. }
  53. template <>
  54. bool Random::Rand<bool>() {
  55. return Rand(0, 1) == 1;
  56. }
  57. double Random::Gaussian(double mean, double standard_deviation) {
  58. // Creating a Normal distribution variable from two independent uniform
  59. // variables based on the Box-Muller transform, which is defined on the
  60. // interval (0, 1]. Note that we rely on NextOutput to generate integers
  61. // in the range [1, 2^64-1]. Normally this behavior is a bit frustrating,
  62. // but here it is exactly what we need.
  63. const double kPi = 3.14159265358979323846;
  64. double u1 = static_cast<double>(NextOutput()) / 0xFFFFFFFFFFFFFFFFull;
  65. double u2 = static_cast<double>(NextOutput()) / 0xFFFFFFFFFFFFFFFFull;
  66. return mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2);
  67. }
  68. double Random::Exponential(double lambda) {
  69. double uniform = Rand<double>();
  70. return -log(uniform) / lambda;
  71. }
  72. } // namespace webrtc