random.h 3.1 KB

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