random.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #ifndef RTC_BASE_RANDOM_H_
  11. #define RTC_BASE_RANDOM_H_
  12. #include <stdint.h>
  13. #include <limits>
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/constructor_magic.h"
  16. namespace webrtc {
  17. class Random {
  18. public:
  19. // TODO(tommi): Change this so that the seed can be initialized internally,
  20. // e.g. by offering two ways of constructing or offer a static method that
  21. // returns a seed that's suitable for initialization.
  22. // The problem now is that callers are calling clock_->TimeInMicroseconds()
  23. // which calls TickTime::Now().Ticks(), which can return a very low value on
  24. // Mac and can result in a seed of 0 after conversion to microseconds.
  25. // Besides the quality of the random seed being poor, this also requires
  26. // the client to take on extra dependencies to generate a seed.
  27. // If we go for a static seed generator in Random, we can use something from
  28. // webrtc/rtc_base and make sure that it works the same way across platforms.
  29. // See also discussion here: https://codereview.webrtc.org/1623543002/
  30. explicit Random(uint64_t seed);
  31. // Return pseudo-random integer of the specified type.
  32. // We need to limit the size to 32 bits to keep the output close to uniform.
  33. template <typename T>
  34. T Rand() {
  35. static_assert(std::numeric_limits<T>::is_integer &&
  36. std::numeric_limits<T>::radix == 2 &&
  37. std::numeric_limits<T>::digits <= 32,
  38. "Rand is only supported for built-in integer types that are "
  39. "32 bits or smaller.");
  40. return static_cast<T>(NextOutput());
  41. }
  42. // Uniformly distributed pseudo-random number in the interval [0, t].
  43. uint32_t Rand(uint32_t t);
  44. // Uniformly distributed pseudo-random number in the interval [low, high].
  45. uint32_t Rand(uint32_t low, uint32_t high);
  46. // Uniformly distributed pseudo-random number in the interval [low, high].
  47. int32_t Rand(int32_t low, int32_t high);
  48. // Normal Distribution.
  49. double Gaussian(double mean, double standard_deviation);
  50. // Exponential Distribution.
  51. double Exponential(double lambda);
  52. private:
  53. // Outputs a nonzero 64-bit random number.
  54. uint64_t NextOutput() {
  55. state_ ^= state_ >> 12;
  56. state_ ^= state_ << 25;
  57. state_ ^= state_ >> 27;
  58. RTC_DCHECK(state_ != 0x0ULL);
  59. return state_ * 2685821657736338717ull;
  60. }
  61. uint64_t state_;
  62. RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Random);
  63. };
  64. // Return pseudo-random number in the interval [0.0, 1.0).
  65. template <>
  66. float Random::Rand<float>();
  67. // Return pseudo-random number in the interval [0.0, 1.0).
  68. template <>
  69. double Random::Rand<double>();
  70. // Return pseudo-random boolean value.
  71. template <>
  72. bool Random::Rand<bool>();
  73. } // namespace webrtc
  74. #endif // RTC_BASE_RANDOM_H_