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 IntType = int> |
14 | // class discrete_distribution |
15 | |
16 | // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm); |
17 | |
18 | #include <cassert> |
19 | #include <cstdlib> |
20 | #include <random> |
21 | #include <vector> |
22 | |
23 | int main(int, char**) |
24 | { |
25 | { |
26 | typedef std::discrete_distribution<> D; |
27 | typedef D::param_type P; |
28 | typedef std::minstd_rand G; |
29 | G g; |
30 | D d; |
31 | double p0[] = {.3, .1, .6}; |
32 | P p(p0, p0+3); |
33 | const int N = 10000000; |
34 | std::vector<D::result_type> u(3); |
35 | for (int i = 0; i < N; ++i) |
36 | { |
37 | D::result_type v = d(g, p); |
38 | assert(0 <= v && v <= 2); |
39 | u[v]++; |
40 | } |
41 | std::vector<double> prob = p.probabilities(); |
42 | for (int i = 0; i <= 2; ++i) |
43 | assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); |
44 | } |
45 | |
46 | return 0; |
47 | } |
48 | |