blob: 10435054588a78b2e8a41271ca9624425fe52cec [file] [log] [blame]
terelius84e78f92015-12-10 01:50:55 -08001/*
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 06:47:31 +020010#include "rtc_base/random.h"
terelius84e78f92015-12-10 01:50:55 -080011
12#include <math.h>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
15#include "rtc_base/safe_conversions.h"
terelius84e78f92015-12-10 01:50:55 -080016
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 03:00:37 -070044 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 01:50:55 -080047}
48
49template <>
50float Random::Rand<float>() {
51 double result = NextOutput() - 1;
52 result = result / 0xFFFFFFFFFFFFFFFEull;
53 return static_cast<float>(result);
54}
55
56template <>
57double Random::Rand<double>() {
58 double result = NextOutput() - 1;
59 result = result / 0xFFFFFFFFFFFFFFFEull;
60 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;
75 double u1 = static_cast<double>(NextOutput()) / 0xFFFFFFFFFFFFFFFFull;
76 double u2 = static_cast<double>(NextOutput()) / 0xFFFFFFFFFFFFFFFFull;
77 return mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2);
78}
79
80double Random::Exponential(double lambda) {
81 double uniform = Rand<double>();
82 return -log(uniform) / lambda;
83}
84
85} // namespace webrtc