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 _IntType = int>
12// class uniform_int_distribution
13
14// template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
15
16#include <random>
17#include <cassert>
18#include <cmath>
19#include <cstddef>
20#include <numeric>
21#include <vector>
22
23#include "test_macros.h"
24
25template <class T>
26inline
27T
28sqr(T x)
29{
30 return x * x;
31}
32
33int main(int, char**)
34{
35 {
36 typedef std::uniform_int_distribution<> D;
37 typedef std::minstd_rand G;
38 typedef D::param_type P;
39 G g;
40 D d(5, 100);
41 P p(-10, 20);
42 const int N = 100000;
43 std::vector<D::result_type> u;
44 for (int i = 0; i < N; ++i)
45 {
46 D::result_type v = d(g, p);
47 assert(p.a() <= v && v <= p.b());
48 u.push_back(v);
49 }
50 double mean = std::accumulate(u.begin(), u.end(),
51 double(0)) / u.size();
52 double var = 0;
53 double skew = 0;
54 double kurtosis = 0;
55 for (std::size_t i = 0; i < u.size(); ++i)
56 {
57 double dbl = (u[i] - mean);
58 double d2 = sqr(dbl);
59 var += d2;
60 skew += dbl * d2;
61 kurtosis += d2 * d2;
62 }
63 var /= u.size();
64 double dev = std::sqrt(x: var);
65 skew /= u.size() * dev * var;
66 kurtosis /= u.size() * var * var;
67 kurtosis -= 3;
68 double x_mean = ((double)p.a() + p.b()) / 2;
69 double x_var = (sqr((double)p.b() - p.a() + 1) - 1) / 12;
70 double x_skew = 0;
71 double x_kurtosis = -6. * (sqr((double)p.b() - p.a() + 1) + 1) /
72 (5. * (sqr((double)p.b() - p.a() + 1) - 1));
73 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
74 assert(std::abs((var - x_var) / x_var) < 0.01);
75 assert(std::abs(skew - x_skew) < 0.01);
76 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
77 }
78
79 return 0;
80}
81

source code of libcxx/test/std/numerics/rand/rand.dist/rand.dist.uni/rand.dist.uni.int/eval_param.pass.cpp