1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// REQUIRES: long_tests
10
11// <random>
12
13// template<class RealType = double>
14// class uniform_real_distribution
15
16// template<class _URNG> result_type operator()(_URNG& g);
17
18#include <random>
19#include <cassert>
20#include <vector>
21#include <numeric>
22#include <cstddef>
23
24#include "test_macros.h"
25
26template <class T>
27inline
28T
29sqr(T x)
30{
31 return x * x;
32}
33
34int main(int, char**)
35{
36 {
37 typedef std::uniform_real_distribution<> D;
38 typedef std::minstd_rand0 G;
39 G g;
40 D d;
41 const int N = 100000;
42 std::vector<D::result_type> u;
43 for (int i = 0; i < N; ++i)
44 {
45 D::result_type v = d(g);
46 assert(d.a() <= v && v < d.b());
47 u.push_back(x: v);
48 }
49 D::result_type mean = std::accumulate(u.begin(), u.end(),
50 D::result_type(0)) / u.size();
51 D::result_type var = 0;
52 D::result_type skew = 0;
53 D::result_type kurtosis = 0;
54 for (std::size_t i = 0; i < u.size(); ++i)
55 {
56 D::result_type dbl = (u[i] - mean);
57 D::result_type d2 = sqr(dbl);
58 var += d2;
59 skew += dbl * d2;
60 kurtosis += d2 * d2;
61 }
62 var /= u.size();
63 D::result_type dev = std::sqrt(x: var);
64 skew /= u.size() * dev * var;
65 kurtosis /= u.size() * var * var;
66 kurtosis -= 3;
67 D::result_type x_mean = (d.a() + d.b()) / 2;
68 D::result_type x_var = sqr(d.b() - d.a()) / 12;
69 D::result_type x_skew = 0;
70 D::result_type x_kurtosis = -6./5;
71 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
72 assert(std::abs((var - x_var) / x_var) < 0.01);
73 assert(std::abs(skew - x_skew) < 0.01);
74 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
75 }
76 {
77 typedef std::uniform_real_distribution<> D;
78 typedef std::minstd_rand G;
79 G g;
80 D d;
81 const int N = 100000;
82 std::vector<D::result_type> u;
83 for (int i = 0; i < N; ++i)
84 {
85 D::result_type v = d(g);
86 assert(d.a() <= v && v < d.b());
87 u.push_back(v);
88 }
89 D::result_type mean = std::accumulate(u.begin(), u.end(),
90 D::result_type(0)) / u.size();
91 D::result_type var = 0;
92 D::result_type skew = 0;
93 D::result_type kurtosis = 0;
94 for (std::size_t i = 0; i < u.size(); ++i)
95 {
96 D::result_type dbl = (u[i] - mean);
97 D::result_type d2 = sqr(dbl);
98 var += d2;
99 skew += dbl * d2;
100 kurtosis += d2 * d2;
101 }
102 var /= u.size();
103 D::result_type dev = std::sqrt(var);
104 skew /= u.size() * dev * var;
105 kurtosis /= u.size() * var * var;
106 kurtosis -= 3;
107 D::result_type x_mean = (d.a() + d.b()) / 2;
108 D::result_type x_var = sqr(d.b() - d.a()) / 12;
109 D::result_type x_skew = 0;
110 D::result_type x_kurtosis = -6./5;
111 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
112 assert(std::abs((var - x_var) / x_var) < 0.01);
113 assert(std::abs(skew - x_skew) < 0.01);
114 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
115 }
116 {
117 typedef std::uniform_real_distribution<> D;
118 typedef std::mt19937 G;
119 G g;
120 D d;
121 const int N = 100000;
122 std::vector<D::result_type> u;
123 for (int i = 0; i < N; ++i)
124 {
125 D::result_type v = d(g);
126 assert(d.a() <= v && v < d.b());
127 u.push_back(v);
128 }
129 D::result_type mean = std::accumulate(u.begin(), u.end(),
130 D::result_type(0)) / u.size();
131 D::result_type var = 0;
132 D::result_type skew = 0;
133 D::result_type kurtosis = 0;
134 for (std::size_t i = 0; i < u.size(); ++i)
135 {
136 D::result_type dbl = (u[i] - mean);
137 D::result_type d2 = sqr(dbl);
138 var += d2;
139 skew += dbl * d2;
140 kurtosis += d2 * d2;
141 }
142 var /= u.size();
143 D::result_type dev = std::sqrt(var);
144 skew /= u.size() * dev * var;
145 kurtosis /= u.size() * var * var;
146 kurtosis -= 3;
147 D::result_type x_mean = (d.a() + d.b()) / 2;
148 D::result_type x_var = sqr(d.b() - d.a()) / 12;
149 D::result_type x_skew = 0;
150 D::result_type x_kurtosis = -6./5;
151 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
152 assert(std::abs((var - x_var) / x_var) < 0.01);
153 assert(std::abs(skew - x_skew) < 0.01);
154 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
155 }
156 {
157 typedef std::uniform_real_distribution<> D;
158 typedef std::mt19937_64 G;
159 G g;
160 D d;
161 const int N = 100000;
162 std::vector<D::result_type> u;
163 for (int i = 0; i < N; ++i)
164 {
165 D::result_type v = d(g);
166 assert(d.a() <= v && v < d.b());
167 u.push_back(v);
168 }
169 D::result_type mean = std::accumulate(u.begin(), u.end(),
170 D::result_type(0)) / u.size();
171 D::result_type var = 0;
172 D::result_type skew = 0;
173 D::result_type kurtosis = 0;
174 for (std::size_t i = 0; i < u.size(); ++i)
175 {
176 D::result_type dbl = (u[i] - mean);
177 D::result_type d2 = sqr(dbl);
178 var += d2;
179 skew += dbl * d2;
180 kurtosis += d2 * d2;
181 }
182 var /= u.size();
183 D::result_type dev = std::sqrt(var);
184 skew /= u.size() * dev * var;
185 kurtosis /= u.size() * var * var;
186 kurtosis -= 3;
187 D::result_type x_mean = (d.a() + d.b()) / 2;
188 D::result_type x_var = sqr(d.b() - d.a()) / 12;
189 D::result_type x_skew = 0;
190 D::result_type x_kurtosis = -6./5;
191 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
192 assert(std::abs((var - x_var) / x_var) < 0.01);
193 assert(std::abs(skew - x_skew) < 0.01);
194 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
195 }
196 {
197 typedef std::uniform_real_distribution<> D;
198 typedef std::ranlux24_base G;
199 G g;
200 D d;
201 const int N = 100000;
202 std::vector<D::result_type> u;
203 for (int i = 0; i < N; ++i)
204 {
205 D::result_type v = d(g);
206 assert(d.a() <= v && v < d.b());
207 u.push_back(v);
208 }
209 D::result_type mean = std::accumulate(u.begin(), u.end(),
210 D::result_type(0)) / u.size();
211 D::result_type var = 0;
212 D::result_type skew = 0;
213 D::result_type kurtosis = 0;
214 for (std::size_t i = 0; i < u.size(); ++i)
215 {
216 D::result_type dbl = (u[i] - mean);
217 D::result_type d2 = sqr(dbl);
218 var += d2;
219 skew += dbl * d2;
220 kurtosis += d2 * d2;
221 }
222 var /= u.size();
223 D::result_type dev = std::sqrt(var);
224 skew /= u.size() * dev * var;
225 kurtosis /= u.size() * var * var;
226 kurtosis -= 3;
227 D::result_type x_mean = (d.a() + d.b()) / 2;
228 D::result_type x_var = sqr(d.b() - d.a()) / 12;
229 D::result_type x_skew = 0;
230 D::result_type x_kurtosis = -6./5;
231 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
232 assert(std::abs((var - x_var) / x_var) < 0.01);
233 assert(std::abs(skew - x_skew) < 0.02);
234 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
235 }
236 {
237 typedef std::uniform_real_distribution<> D;
238 typedef std::ranlux48_base G;
239 G g;
240 D d;
241 const int N = 100000;
242 std::vector<D::result_type> u;
243 for (int i = 0; i < N; ++i)
244 {
245 D::result_type v = d(g);
246 assert(d.a() <= v && v < d.b());
247 u.push_back(v);
248 }
249 D::result_type mean = std::accumulate(u.begin(), u.end(),
250 D::result_type(0)) / u.size();
251 D::result_type var = 0;
252 D::result_type skew = 0;
253 D::result_type kurtosis = 0;
254 for (std::size_t i = 0; i < u.size(); ++i)
255 {
256 D::result_type dbl = (u[i] - mean);
257 D::result_type d2 = sqr(dbl);
258 var += d2;
259 skew += dbl * d2;
260 kurtosis += d2 * d2;
261 }
262 var /= u.size();
263 D::result_type dev = std::sqrt(var);
264 skew /= u.size() * dev * var;
265 kurtosis /= u.size() * var * var;
266 kurtosis -= 3;
267 D::result_type x_mean = (d.a() + d.b()) / 2;
268 D::result_type x_var = sqr(d.b() - d.a()) / 12;
269 D::result_type x_skew = 0;
270 D::result_type x_kurtosis = -6./5;
271 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
272 assert(std::abs((var - x_var) / x_var) < 0.01);
273 assert(std::abs(skew - x_skew) < 0.01);
274 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
275 }
276 {
277 typedef std::uniform_real_distribution<> D;
278 typedef std::ranlux24 G;
279 G g;
280 D d;
281 const int N = 100000;
282 std::vector<D::result_type> u;
283 for (int i = 0; i < N; ++i)
284 {
285 D::result_type v = d(g);
286 assert(d.a() <= v && v < d.b());
287 u.push_back(v);
288 }
289 D::result_type mean = std::accumulate(u.begin(), u.end(),
290 D::result_type(0)) / u.size();
291 D::result_type var = 0;
292 D::result_type skew = 0;
293 D::result_type kurtosis = 0;
294 for (std::size_t i = 0; i < u.size(); ++i)
295 {
296 D::result_type dbl = (u[i] - mean);
297 D::result_type d2 = sqr(dbl);
298 var += d2;
299 skew += dbl * d2;
300 kurtosis += d2 * d2;
301 }
302 var /= u.size();
303 D::result_type dev = std::sqrt(var);
304 skew /= u.size() * dev * var;
305 kurtosis /= u.size() * var * var;
306 kurtosis -= 3;
307 D::result_type x_mean = (d.a() + d.b()) / 2;
308 D::result_type x_var = sqr(d.b() - d.a()) / 12;
309 D::result_type x_skew = 0;
310 D::result_type x_kurtosis = -6./5;
311 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
312 assert(std::abs((var - x_var) / x_var) < 0.01);
313 assert(std::abs(skew - x_skew) < 0.01);
314 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
315 }
316 {
317 typedef std::uniform_real_distribution<> D;
318 typedef std::ranlux48 G;
319 G g;
320 D d;
321 const int N = 100000;
322 std::vector<D::result_type> u;
323 for (int i = 0; i < N; ++i)
324 {
325 D::result_type v = d(g);
326 assert(d.a() <= v && v < d.b());
327 u.push_back(v);
328 }
329 D::result_type mean = std::accumulate(u.begin(), u.end(),
330 D::result_type(0)) / u.size();
331 D::result_type var = 0;
332 D::result_type skew = 0;
333 D::result_type kurtosis = 0;
334 for (std::size_t i = 0; i < u.size(); ++i)
335 {
336 D::result_type dbl = (u[i] - mean);
337 D::result_type d2 = sqr(dbl);
338 var += d2;
339 skew += dbl * d2;
340 kurtosis += d2 * d2;
341 }
342 var /= u.size();
343 D::result_type dev = std::sqrt(var);
344 skew /= u.size() * dev * var;
345 kurtosis /= u.size() * var * var;
346 kurtosis -= 3;
347 D::result_type x_mean = (d.a() + d.b()) / 2;
348 D::result_type x_var = sqr(d.b() - d.a()) / 12;
349 D::result_type x_skew = 0;
350 D::result_type x_kurtosis = -6./5;
351 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
352 assert(std::abs((var - x_var) / x_var) < 0.01);
353 assert(std::abs(skew - x_skew) < 0.01);
354 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
355 }
356 {
357 typedef std::uniform_real_distribution<> D;
358 typedef std::knuth_b G;
359 G g;
360 D d;
361 const int N = 100000;
362 std::vector<D::result_type> u;
363 for (int i = 0; i < N; ++i)
364 {
365 D::result_type v = d(g);
366 assert(d.a() <= v && v < d.b());
367 u.push_back(v);
368 }
369 D::result_type mean = std::accumulate(u.begin(), u.end(),
370 D::result_type(0)) / u.size();
371 D::result_type var = 0;
372 D::result_type skew = 0;
373 D::result_type kurtosis = 0;
374 for (std::size_t i = 0; i < u.size(); ++i)
375 {
376 D::result_type dbl = (u[i] - mean);
377 D::result_type d2 = sqr(dbl);
378 var += d2;
379 skew += dbl * d2;
380 kurtosis += d2 * d2;
381 }
382 var /= u.size();
383 D::result_type dev = std::sqrt(var);
384 skew /= u.size() * dev * var;
385 kurtosis /= u.size() * var * var;
386 kurtosis -= 3;
387 D::result_type x_mean = (d.a() + d.b()) / 2;
388 D::result_type x_var = sqr(d.b() - d.a()) / 12;
389 D::result_type x_skew = 0;
390 D::result_type x_kurtosis = -6./5;
391 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
392 assert(std::abs((var - x_var) / x_var) < 0.01);
393 assert(std::abs(skew - x_skew) < 0.01);
394 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
395 }
396 {
397 typedef std::uniform_real_distribution<> D;
398 typedef std::minstd_rand G;
399 G g;
400 D d(-1, 1);
401 const int N = 100000;
402 std::vector<D::result_type> u;
403 for (int i = 0; i < N; ++i)
404 {
405 D::result_type v = d(g);
406 assert(d.a() <= v && v < d.b());
407 u.push_back(v);
408 }
409 D::result_type mean = std::accumulate(u.begin(), u.end(),
410 D::result_type(0)) / u.size();
411 D::result_type var = 0;
412 D::result_type skew = 0;
413 D::result_type kurtosis = 0;
414 for (std::size_t i = 0; i < u.size(); ++i)
415 {
416 D::result_type dbl = (u[i] - mean);
417 D::result_type d2 = sqr(dbl);
418 var += d2;
419 skew += dbl * d2;
420 kurtosis += d2 * d2;
421 }
422 var /= u.size();
423 D::result_type dev = std::sqrt(var);
424 skew /= u.size() * dev * var;
425 kurtosis /= u.size() * var * var;
426 kurtosis -= 3;
427 D::result_type x_mean = (d.a() + d.b()) / 2;
428 D::result_type x_var = sqr(d.b() - d.a()) / 12;
429 D::result_type x_skew = 0;
430 D::result_type x_kurtosis = -6./5;
431 assert(std::abs(mean - x_mean) < 0.01);
432 assert(std::abs((var - x_var) / x_var) < 0.01);
433 assert(std::abs(skew - x_skew) < 0.01);
434 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
435 }
436 {
437 typedef std::uniform_real_distribution<> D;
438 typedef std::minstd_rand G;
439 G g;
440 D d(5.5, 25);
441 const int N = 100000;
442 std::vector<D::result_type> u;
443 for (int i = 0; i < N; ++i)
444 {
445 D::result_type v = d(g);
446 assert(d.a() <= v && v < d.b());
447 u.push_back(v);
448 }
449 D::result_type mean = std::accumulate(u.begin(), u.end(),
450 D::result_type(0)) / u.size();
451 D::result_type var = 0;
452 D::result_type skew = 0;
453 D::result_type kurtosis = 0;
454 for (std::size_t i = 0; i < u.size(); ++i)
455 {
456 D::result_type dbl = (u[i] - mean);
457 D::result_type d2 = sqr(dbl);
458 var += d2;
459 skew += dbl * d2;
460 kurtosis += d2 * d2;
461 }
462 var /= u.size();
463 D::result_type dev = std::sqrt(var);
464 skew /= u.size() * dev * var;
465 kurtosis /= u.size() * var * var;
466 kurtosis -= 3;
467 D::result_type x_mean = (d.a() + d.b()) / 2;
468 D::result_type x_var = sqr(d.b() - d.a()) / 12;
469 D::result_type x_skew = 0;
470 D::result_type x_kurtosis = -6./5;
471 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
472 assert(std::abs((var - x_var) / x_var) < 0.01);
473 assert(std::abs(skew - x_skew) < 0.01);
474 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
475 }
476
477 return 0;
478}
479

source code of libcxx/test/std/numerics/rand/rand.dist/rand.dist.uni/rand.dist.uni.real/eval.pass.cpp