blob: b3b9fd1608c1d918e6a113fc109e5a2eebc75899 [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
Yves Gerey988cc082018-10-23 10:03:0114#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Henrik Kjellanderec78f1c2017-06-29 05:52:5016#include <limits>
terelius56b11282015-11-06 13:13:5517
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/checks.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5019
20namespace webrtc {
21
22class Random {
23 public:
24 // TODO(tommi): Change this so that the seed can be initialized internally,
25 // e.g. by offering two ways of constructing or offer a static method that
26 // returns a seed that's suitable for initialization.
27 // The problem now is that callers are calling clock_->TimeInMicroseconds()
28 // which calls TickTime::Now().Ticks(), which can return a very low value on
29 // Mac and can result in a seed of 0 after conversion to microseconds.
30 // Besides the quality of the random seed being poor, this also requires
31 // the client to take on extra dependencies to generate a seed.
32 // If we go for a static seed generator in Random, we can use something from
kjellandere96c45b2017-06-30 17:45:2133 // webrtc/rtc_base and make sure that it works the same way across platforms.
Henrik Kjellanderec78f1c2017-06-29 05:52:5034 // See also discussion here: https://codereview.webrtc.org/1623543002/
35 explicit Random(uint64_t seed);
36
Niels Möllerde953292020-09-29 07:46:2137 Random() = delete;
38 Random(const Random&) = delete;
39 Random& operator=(const Random&) = delete;
40
Henrik Kjellanderec78f1c2017-06-29 05:52:5041 // Return pseudo-random integer of the specified type.
42 // We need to limit the size to 32 bits to keep the output close to uniform.
43 template <typename T>
44 T Rand() {
45 static_assert(std::numeric_limits<T>::is_integer &&
46 std::numeric_limits<T>::radix == 2 &&
47 std::numeric_limits<T>::digits <= 32,
48 "Rand is only supported for built-in integer types that are "
49 "32 bits or smaller.");
50 return static_cast<T>(NextOutput());
51 }
52
53 // Uniformly distributed pseudo-random number in the interval [0, t].
54 uint32_t Rand(uint32_t t);
55
56 // Uniformly distributed pseudo-random number in the interval [low, high].
57 uint32_t Rand(uint32_t low, uint32_t high);
58
59 // Uniformly distributed pseudo-random number in the interval [low, high].
60 int32_t Rand(int32_t low, int32_t high);
61
62 // Normal Distribution.
63 double Gaussian(double mean, double standard_deviation);
64
65 // Exponential Distribution.
66 double Exponential(double lambda);
67
68 private:
Artem Titovec9b2812021-01-07 14:49:3169 // Outputs a nonzero 64-bit random number using Xorshift algorithm.
70 // https://en.wikipedia.org/wiki/Xorshift
Henrik Kjellanderec78f1c2017-06-29 05:52:5071 uint64_t NextOutput() {
72 state_ ^= state_ >> 12;
73 state_ ^= state_ << 25;
74 state_ ^= state_ >> 27;
75 RTC_DCHECK(state_ != 0x0ULL);
76 return state_ * 2685821657736338717ull;
77 }
78
79 uint64_t state_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5080};
81
82// Return pseudo-random number in the interval [0.0, 1.0).
83template <>
84float Random::Rand<float>();
85
86// Return pseudo-random number in the interval [0.0, 1.0).
87template <>
88double Random::Rand<double>();
89
90// Return pseudo-random boolean value.
91template <>
92bool Random::Rand<bool>();
93
94} // namespace webrtc
stefanc62642c2015-07-07 11:20:3495
Mirko Bonadei92ea95e2017-09-15 04:47:3196#endif // RTC_BASE_RANDOM_H_