blob: 647b84c9c947b8d11f711012c9deb5e09b151d3f [file] [log] [blame]
stefanc62642c2015-07-07 04:20:34 -07001/*
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
terelius84e78f92015-12-10 01:50:55 -080011#ifndef WEBRTC_BASE_RANDOM_H_
12#define WEBRTC_BASE_RANDOM_H_
stefanc62642c2015-07-07 04:20:34 -070013
terelius56b11282015-11-06 05:13:55 -080014#include <limits>
15
stefanc62642c2015-07-07 04:20:34 -070016#include "webrtc/typedefs.h"
17#include "webrtc/base/constructormagic.h"
terelius84e78f92015-12-10 01:50:55 -080018#include "webrtc/base/checks.h"
stefanc62642c2015-07-07 04:20:34 -070019
20namespace webrtc {
21
22class Random {
23 public:
terelius84e78f92015-12-10 01:50:55 -080024 explicit Random(uint64_t seed);
stefanc62642c2015-07-07 04:20:34 -070025
terelius56b11282015-11-06 05:13:55 -080026 // Return pseudo-random integer of the specified type.
terelius84e78f92015-12-10 01:50:55 -080027 // We need to limit the size to 32 bits to keep the output close to uniform.
terelius56b11282015-11-06 05:13:55 -080028 template <typename T>
29 T Rand() {
30 static_assert(std::numeric_limits<T>::is_integer &&
31 std::numeric_limits<T>::radix == 2 &&
32 std::numeric_limits<T>::digits <= 32,
33 "Rand is only supported for built-in integer types that are "
34 "32 bits or smaller.");
terelius84e78f92015-12-10 01:50:55 -080035 return static_cast<T>(NextOutput());
terelius56b11282015-11-06 05:13:55 -080036 }
stefanc62642c2015-07-07 04:20:34 -070037
terelius56b11282015-11-06 05:13:55 -080038 // Uniformly distributed pseudo-random number in the interval [0, t].
39 uint32_t Rand(uint32_t t);
40
41 // Uniformly distributed pseudo-random number in the interval [low, high].
42 uint32_t Rand(uint32_t low, uint32_t high);
solenberg5bdddf92015-10-15 05:10:30 -070043
terelius84e78f92015-12-10 01:50:55 -080044 // Uniformly distributed pseudo-random number in the interval [low, high].
45 int32_t Rand(int32_t low, int32_t high);
46
stefanc62642c2015-07-07 04:20:34 -070047 // Normal Distribution.
terelius84e78f92015-12-10 01:50:55 -080048 double Gaussian(double mean, double standard_deviation);
stefanc62642c2015-07-07 04:20:34 -070049
solenberg5bdddf92015-10-15 05:10:30 -070050 // Exponential Distribution.
terelius84e78f92015-12-10 01:50:55 -080051 double Exponential(double lambda);
stefanc62642c2015-07-07 04:20:34 -070052
53 private:
terelius84e78f92015-12-10 01:50:55 -080054 // Outputs a nonzero 64-bit random number.
55 uint64_t NextOutput() {
56 state_ ^= state_ >> 12;
57 state_ ^= state_ << 25;
58 state_ ^= state_ >> 27;
59 RTC_DCHECK(state_ != 0x0ULL);
60 return state_ * 2685821657736338717ull;
61 }
62
63 uint64_t state_;
stefanc62642c2015-07-07 04:20:34 -070064
henrikg3c089d72015-09-16 05:37:44 -070065 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Random);
stefanc62642c2015-07-07 04:20:34 -070066};
terelius56b11282015-11-06 05:13:55 -080067
68// Return pseudo-random number in the interval [0.0, 1.0).
69template <>
70float Random::Rand<float>();
71
terelius84e78f92015-12-10 01:50:55 -080072// Return pseudo-random number in the interval [0.0, 1.0).
73template <>
74double Random::Rand<double>();
75
terelius56b11282015-11-06 05:13:55 -080076// Return pseudo-random boolean value.
77template <>
78bool Random::Rand<bool>();
79
stefanc62642c2015-07-07 04:20:34 -070080} // namespace webrtc
81
terelius84e78f92015-12-10 01:50:55 -080082#endif // WEBRTC_BASE_RANDOM_H_