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