forked from Xrysnow/lstgx_Math
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXRandom.cpp
More file actions
217 lines (199 loc) · 4.6 KB
/
XRandom.cpp
File metadata and controls
217 lines (199 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "XRandom.h"
#include "XConstant.h"
#include <cmath>
#include <limits>
using namespace xmath;
using namespace xmath::random;
constexpr uint64_t MaxIntFromDouble = 9007199254740992;//2^53
constexpr double NormalFactor = 1.71552776992141359296;//4*e^(-0.5)/sqrt(2)
constexpr double log4 = 1.3862943611198906188;
constexpr double SG_MAGICCONST = 1.0 + 1.504077396776274073373;
constexpr double _e = 2.71828182845904523536028;
int64_t Random::range(uint64_t start)
{
return below(start);
}
int64_t Random::range(int64_t start, int64_t stop, int64_t step)
{
if (start == stop)
return start; //empty range
const auto width = stop - start;
if (step == 1 && width > 0)
return start + below(width);
if (step == 1 || step == 0)
return start; //empty range
const int64_t n = step > 0 ? double(width + step - 1) / step : double(width + step + 1) / step;
if (n <= 0)
return start; //empty range
return start + double(step) * below(n);
}
uint64_t Random::below(uint64_t n)
{
if (n > MaxIntFromDouble || n == 0)
return _rand() * n;
const auto rem = MaxIntFromDouble % n;
const auto limit = double(MaxIntFromDouble - rem) / MaxIntFromDouble;
auto r = _rand();
while (r >= limit)
r = _rand();
return uint64_t(r * MaxIntFromDouble) % n;
}
double Random::uniform(double a, double b)
{
return a + (b - a) * _rand();
}
double Random::triangular()
{
return triangular(0.0, 1.0);
}
double Random::triangular(double low, double high)
{
const auto u = _rand();
if (u > 0.5)
return high + (low - high)*std::sqrt((1 - u)*0.5);
return low + (high - low)*sqrt(u*0.5);
}
double Random::triangular(double low, double high, double mode)
{
const auto div = high - low;
if (div == 0.0)
return low;
const auto c = (mode - low) / div;
const auto u = _rand();
if (u > c)
return high + (low - high)*std::sqrt((1 - u)*(1 - c));
return low + (high - low)*sqrt(u*c);
}
double Random::normal(double mu, double sigma)
{
double z;
while (true)
{
const double u1 = _rand();
const double u2 = _rand();
z = NormalFactor * (u1 - 0.5) / u2;
if (z * z / 4 <= -std::log(u2))
break;
}
return mu + z * sigma;
}
double Random::log_normal(double mu, double sigma)
{
return std::exp(normal(mu, sigma));
}
double Random::expo(double lambda)
{
if (lambda == 0.0)return 0.0;
return -std::log(1 - _rand()) / lambda;
}
double Random::vonMises(double mu, double kappa)
{
if (kappa <= 1e-6)
return pix2 * _rand();
const auto s = 0.5 / kappa;
const auto r = s + std::sqrt(1 + s * s);
double z;
while (true)
{
const double u1 = _rand();
z = std::cos(pi * u1);
const double d = z / (r + z);
const double u2 = _rand();
if (u2 < 1 - d * d || u2 <= (1 - d)*std::exp(d))
break;
}
const auto q = 1.0 / r;
const auto f = (q + z) / (1 + q * z);
if (_rand() > 0.5)
return (mu + std::acos(f)) * pix2;
else
return (mu - std::acos(f)) * pix2;
}
double Random::gamma(double alpha, double beta)
{
if (alpha <= 0.0 || beta <= 0.0)return 0.0;
if (alpha > 1.0)
{
const auto ainv = std::sqrt(2 * alpha - 1);
const auto bbb = alpha - log4;
const auto ccc = alpha + ainv;
double u1, u2, v, x, z, r;
while (true)
{
u1 = _rand();
while (u1 < 1e-7 || u1 > 1 - 1e-7)
u1 = _rand();
u2 = 1 - _rand();
v = std::log(u1 / (1.0 - u1)) / ainv;
x = alpha * std::exp(v);
z = u1 * u1*u2;
r = bbb + ccc * v - x;
if (r + SG_MAGICCONST - 4.5 * z >= 0.0 || r >= std::log(z))
return x * beta;
}
}
else if (alpha == 1.0)
{
auto u = _rand();
while (u <= 1e-7)
u = _rand();
return -std::log(u)*beta;
}
else
{
double x;
while (true)
{
const auto u = _rand();
const auto b = (_e + alpha) / _e;
const auto p = b * u;
if (p <= 1.0)
x = std::pow(p, 1.0 / alpha);
else
x = -std::log((b - p) / alpha);
const auto u1 = _rand();
if (p > 1.0)
{
if (u1 <= std::pow(x, alpha - 1.0))
break;
}
else if (u1 <= std::exp(-x))
break;
}
return x * beta;
}
}
double Random::gauss(double mu, double sigma)
{
double z;
if (!has_gauss_next)
{
const auto x2pi = pix2 * _rand();
const auto g2rad = std::sqrt(-2.0 * std::log(1.0 - _rand()));
z = std::cos(x2pi)*g2rad;
_gauss_next = std::sin(x2pi)*g2rad;
has_gauss_next = true;
}
else
{
z = _gauss_next;
has_gauss_next = false;
}
return mu + z * sigma;
}
double Random::beta(double alpha, double beta)
{
const auto y = gamma(alpha, 1.0);
if (y == 0.0)
return 0.0;
else
return y / (y + gamma(beta, 1.0));
}
double Random::pareto(double alpha)
{
return 1.0 / std::pow(1.0 - _rand(), 1.0 / alpha);
}
double Random::weibull(double alpha, double beta)
{
return alpha * std::pow(-std::log(1.0 - _rand()), 1.0 / beta);
}