blob: 5206b817f3c06b8bf5583332795cb628d574518c [file] [log] [blame]
terelius84e78f92015-12-10 09:50:551/*
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 */
Mirko Bonadei92ea95e2017-09-15 04:47:3110#include "rtc_base/random.h"
terelius84e78f92015-12-10 09:50:5511
12#include <math.h>
13
Mirko Bonadei92ea95e2017-09-15 04:47:3114#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 09:42:2615#include "rtc_base/numerics/safe_conversions.h"
terelius84e78f92015-12-10 09:50:5516
17namespace webrtc {
18
19Random::Random(uint64_t seed) {
20 RTC_DCHECK(seed != 0x0ull);
21 state_ = seed;
22}
23
24uint32_t Random::Rand(uint32_t t) {
25 // Casting the output to 32 bits will give an almost uniform number.
26 // Pr[x=0] = (2^32-1) / (2^64-1)
27 // Pr[x=k] = 2^32 / (2^64-1) for k!=0
28 // Uniform would be Pr[x=k] = 2^32 / 2^64 for all 32-bit integers k.
29 uint32_t x = NextOutput();
30 // If x / 2^32 is uniform on [0,1), then x / 2^32 * (t+1) is uniform on
31 // the interval [0,t+1), so the integer part is uniform on [0,t].
32 uint64_t result = x * (static_cast<uint64_t>(t) + 1);
33 result >>= 32;
34 return result;
35}
36
37uint32_t Random::Rand(uint32_t low, uint32_t high) {
38 RTC_DCHECK(low <= high);
39 return Rand(high - low) + low;
40}
41
42int32_t Random::Rand(int32_t low, int32_t high) {
43 RTC_DCHECK(low <= high);
oprypin8ad0e582017-09-05 10:00:3744 const int64_t low_i64{low};
45 return rtc::dchecked_cast<int32_t>(
46 Rand(rtc::dchecked_cast<uint32_t>(high - low_i64)) + low_i64);
terelius84e78f92015-12-10 09:50:5547}
48
49template <>
50float Random::Rand<float>() {
51 double result = NextOutput() - 1;
Peter Kasting482b7c02021-06-25 19:14:3552 result = result / static_cast<double>(0xFFFFFFFFFFFFFFFFull);
terelius84e78f92015-12-10 09:50:5553 return static_cast<float>(result);
54}
55
56template <>
57double Random::Rand<double>() {
58 double result = NextOutput() - 1;
Peter Kasting482b7c02021-06-25 19:14:3559 result = result / static_cast<double>(0xFFFFFFFFFFFFFFFFull);
terelius84e78f92015-12-10 09:50:5560 return result;
61}
62
63template <>
64bool Random::Rand<bool>() {
65 return Rand(0, 1) == 1;
66}
67
68double Random::Gaussian(double mean, double standard_deviation) {
69 // Creating a Normal distribution variable from two independent uniform
70 // variables based on the Box-Muller transform, which is defined on the
71 // interval (0, 1]. Note that we rely on NextOutput to generate integers
72 // in the range [1, 2^64-1]. Normally this behavior is a bit frustrating,
73 // but here it is exactly what we need.
74 const double kPi = 3.14159265358979323846;
Peter Kasting482b7c02021-06-25 19:14:3575 double u1 = static_cast<double>(NextOutput()) /
76 static_cast<double>(0xFFFFFFFFFFFFFFFFull);
77 double u2 = static_cast<double>(NextOutput()) /
78 static_cast<double>(0xFFFFFFFFFFFFFFFFull);
terelius84e78f92015-12-10 09:50:5579 return mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2);
80}
81
82double Random::Exponential(double lambda) {
83 double uniform = Rand<double>();
84 return -log(uniform) / lambda;
85}
86
87} // namespace webrtc