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 discrete_distribution
13 
14 // template<class UnaryOperation>
15 //     param_type(size_t nw, double xmin, double xmax,
16 //                           UnaryOperation fw);
17 
18 #include <random>
19 
20 #include <cassert>
21 #include <vector>
22 
23 #include "test_macros.h"
24 
fw(double x)25 double fw(double x)
26 {
27     return x+1;
28 }
29 
main(int,char **)30 int main(int, char**)
31 {
32     {
33         typedef std::discrete_distribution<> D;
34         typedef D::param_type P;
35         P pa(0, 0, 1, fw);
36         std::vector<double> p = pa.probabilities();
37         assert(p.size() == 1);
38         assert(p[0] == 1);
39     }
40     {
41         typedef std::discrete_distribution<> D;
42         typedef D::param_type P;
43         P pa(1, 0, 1, fw);
44         std::vector<double> p = pa.probabilities();
45         assert(p.size() == 1);
46         assert(p[0] == 1);
47     }
48     {
49         typedef std::discrete_distribution<> D;
50         typedef D::param_type P;
51         P pa(2, 0.5, 1.5, fw);
52         std::vector<double> p = pa.probabilities();
53         assert(p.size() == 2);
54         assert(p[0] == .4375);
55         assert(p[1] == .5625);
56     }
57     {
58         typedef std::discrete_distribution<> D;
59         typedef D::param_type P;
60         P pa(4, 0, 2, fw);
61         std::vector<double> p = pa.probabilities();
62         assert(p.size() == 4);
63         assert(p[0] == .15625);
64         assert(p[1] == .21875);
65         assert(p[2] == .28125);
66     }
67 
68   return 0;
69 }
70