1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <random>
11
12 // template<class IntType = int>
13 // class discrete_distribution
14
15 // template<class UnaryOperation>
16 // discrete_distribution(size_t nw, double xmin, double xmax,
17 // UnaryOperation fw);
18
19 #include <random>
20 #include <cassert>
21
fw(double x)22 double fw(double x)
23 {
24 return x+1;
25 }
26
main()27 int main()
28 {
29 {
30 typedef std::discrete_distribution<> D;
31 D d(0, 0, 1, fw);
32 std::vector<double> p = d.probabilities();
33 assert(p.size() == 1);
34 assert(p[0] == 1);
35 }
36 {
37 typedef std::discrete_distribution<> D;
38 D d(1, 0, 1, fw);
39 std::vector<double> p = d.probabilities();
40 assert(p.size() == 1);
41 assert(p[0] == 1);
42 }
43 {
44 typedef std::discrete_distribution<> D;
45 D d(2, 0.5, 1.5, fw);
46 std::vector<double> p = d.probabilities();
47 assert(p.size() == 2);
48 assert(p[0] == .4375);
49 assert(p[1] == .5625);
50 }
51 {
52 typedef std::discrete_distribution<> D;
53 D d(4, 0, 2, fw);
54 std::vector<double> p = d.probabilities();
55 assert(p.size() == 4);
56 assert(p[0] == .15625);
57 assert(p[1] == .21875);
58 assert(p[2] == .28125);
59 }
60 }
61