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 | // <random> |
10 | |
11 | // template<class RealType = double> |
12 | // class uniform_real_distribution |
13 | |
14 | // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm); |
15 | |
16 | #include <random> |
17 | #include <cassert> |
18 | #include <vector> |
19 | #include <numeric> |
20 | #include <cstddef> |
21 | |
22 | #include "test_macros.h" |
23 | |
24 | template <class T> |
25 | inline |
26 | T |
27 | sqr(T x) |
28 | { |
29 | return x * x; |
30 | } |
31 | |
32 | int main(int, char**) |
33 | { |
34 | { |
35 | typedef std::uniform_real_distribution<> D; |
36 | typedef std::minstd_rand G; |
37 | typedef D::param_type P; |
38 | G g; |
39 | D d(5.5, 25); |
40 | P p(-10, 20); |
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, p); |
46 | assert(p.a() <= v && v < p.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 = (p.a() + p.b()) / 2; |
68 | D::result_type x_var = sqr(p.b() - p.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 | return 0; |
78 | } |
79 |