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 _URNG> result_type operator()(_URNG& g);
16
17 #include <random>
18 #include <vector>
19 #include <cassert>
20
main()21 int main()
22 {
23 {
24 typedef std::discrete_distribution<> D;
25 typedef std::minstd_rand G;
26 G g;
27 D d;
28 const int N = 100;
29 std::vector<D::result_type> u(d.max()+1);
30 for (int i = 0; i < N; ++i)
31 {
32 D::result_type v = d(g);
33 assert(d.min() <= v && v <= d.max());
34 u[v]++;
35 }
36 std::vector<double> prob = d.probabilities();
37 for (int i = 0; i <= d.max(); ++i)
38 assert((double)u[i]/N == prob[i]);
39 }
40 {
41 typedef std::discrete_distribution<> D;
42 typedef std::minstd_rand G;
43 G g;
44 double p0[] = {.3};
45 D d(p0, p0+1);
46 const int N = 100;
47 std::vector<D::result_type> u(d.max()+1);
48 for (int i = 0; i < N; ++i)
49 {
50 D::result_type v = d(g);
51 assert(d.min() <= v && v <= d.max());
52 u[v]++;
53 }
54 std::vector<double> prob = d.probabilities();
55 for (int i = 0; i <= d.max(); ++i)
56 assert((double)u[i]/N == prob[i]);
57 }
58 {
59 typedef std::discrete_distribution<> D;
60 typedef std::minstd_rand G;
61 G g;
62 double p0[] = {.75, .25};
63 D d(p0, p0+2);
64 const int N = 1000000;
65 std::vector<D::result_type> u(d.max()+1);
66 for (int i = 0; i < N; ++i)
67 {
68 D::result_type v = d(g);
69 assert(d.min() <= v && v <= d.max());
70 u[v]++;
71 }
72 std::vector<double> prob = d.probabilities();
73 for (int i = 0; i <= d.max(); ++i)
74 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
75 }
76 {
77 typedef std::discrete_distribution<> D;
78 typedef std::minstd_rand G;
79 G g;
80 double p0[] = {0, 1};
81 D d(p0, p0+2);
82 const int N = 1000000;
83 std::vector<D::result_type> u(d.max()+1);
84 for (int i = 0; i < N; ++i)
85 {
86 D::result_type v = d(g);
87 assert(d.min() <= v && v <= d.max());
88 u[v]++;
89 }
90 std::vector<double> prob = d.probabilities();
91 assert((double)u[0]/N == prob[0]);
92 assert((double)u[1]/N == prob[1]);
93 }
94 {
95 typedef std::discrete_distribution<> D;
96 typedef std::minstd_rand G;
97 G g;
98 double p0[] = {1, 0};
99 D d(p0, p0+2);
100 const int N = 1000000;
101 std::vector<D::result_type> u(d.max()+1);
102 for (int i = 0; i < N; ++i)
103 {
104 D::result_type v = d(g);
105 assert(d.min() <= v && v <= d.max());
106 u[v]++;
107 }
108 std::vector<double> prob = d.probabilities();
109 assert((double)u[0]/N == prob[0]);
110 assert((double)u[1]/N == prob[1]);
111 }
112 {
113 typedef std::discrete_distribution<> D;
114 typedef std::minstd_rand G;
115 G g;
116 double p0[] = {.3, .1, .6};
117 D d(p0, p0+3);
118 const int N = 10000000;
119 std::vector<D::result_type> u(d.max()+1);
120 for (int i = 0; i < N; ++i)
121 {
122 D::result_type v = d(g);
123 assert(d.min() <= v && v <= d.max());
124 u[v]++;
125 }
126 std::vector<double> prob = d.probabilities();
127 for (int i = 0; i <= d.max(); ++i)
128 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
129 }
130 {
131 typedef std::discrete_distribution<> D;
132 typedef std::minstd_rand G;
133 G g;
134 double p0[] = {0, 25, 75};
135 D d(p0, p0+3);
136 const int N = 1000000;
137 std::vector<D::result_type> u(d.max()+1);
138 for (int i = 0; i < N; ++i)
139 {
140 D::result_type v = d(g);
141 assert(d.min() <= v && v <= d.max());
142 u[v]++;
143 }
144 std::vector<double> prob = d.probabilities();
145 for (int i = 0; i <= d.max(); ++i)
146 if (prob[i] != 0)
147 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
148 else
149 assert(u[i] == 0);
150 }
151 {
152 typedef std::discrete_distribution<> D;
153 typedef std::minstd_rand G;
154 G g;
155 double p0[] = {25, 0, 75};
156 D d(p0, p0+3);
157 const int N = 1000000;
158 std::vector<D::result_type> u(d.max()+1);
159 for (int i = 0; i < N; ++i)
160 {
161 D::result_type v = d(g);
162 assert(d.min() <= v && v <= d.max());
163 u[v]++;
164 }
165 std::vector<double> prob = d.probabilities();
166 for (int i = 0; i <= d.max(); ++i)
167 if (prob[i] != 0)
168 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
169 else
170 assert(u[i] == 0);
171 }
172 {
173 typedef std::discrete_distribution<> D;
174 typedef std::minstd_rand G;
175 G g;
176 double p0[] = {25, 75, 0};
177 D d(p0, p0+3);
178 const int N = 1000000;
179 std::vector<D::result_type> u(d.max()+1);
180 for (int i = 0; i < N; ++i)
181 {
182 D::result_type v = d(g);
183 assert(d.min() <= v && v <= d.max());
184 u[v]++;
185 }
186 std::vector<double> prob = d.probabilities();
187 for (int i = 0; i <= d.max(); ++i)
188 if (prob[i] != 0)
189 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
190 else
191 assert(u[i] == 0);
192 }
193 {
194 typedef std::discrete_distribution<> D;
195 typedef std::minstd_rand G;
196 G g;
197 double p0[] = {0, 0, 1};
198 D d(p0, p0+3);
199 const int N = 100;
200 std::vector<D::result_type> u(d.max()+1);
201 for (int i = 0; i < N; ++i)
202 {
203 D::result_type v = d(g);
204 assert(d.min() <= v && v <= d.max());
205 u[v]++;
206 }
207 std::vector<double> prob = d.probabilities();
208 for (int i = 0; i <= d.max(); ++i)
209 if (prob[i] != 0)
210 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
211 else
212 assert(u[i] == 0);
213 }
214 {
215 typedef std::discrete_distribution<> D;
216 typedef std::minstd_rand G;
217 G g;
218 double p0[] = {0, 1, 0};
219 D d(p0, p0+3);
220 const int N = 100;
221 std::vector<D::result_type> u(d.max()+1);
222 for (int i = 0; i < N; ++i)
223 {
224 D::result_type v = d(g);
225 assert(d.min() <= v && v <= d.max());
226 u[v]++;
227 }
228 std::vector<double> prob = d.probabilities();
229 for (int i = 0; i <= d.max(); ++i)
230 if (prob[i] != 0)
231 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
232 else
233 assert(u[i] == 0);
234 }
235 {
236 typedef std::discrete_distribution<> D;
237 typedef std::minstd_rand G;
238 G g;
239 double p0[] = {1, 0, 0};
240 D d(p0, p0+3);
241 const int N = 100;
242 std::vector<D::result_type> u(d.max()+1);
243 for (int i = 0; i < N; ++i)
244 {
245 D::result_type v = d(g);
246 assert(d.min() <= v && v <= d.max());
247 u[v]++;
248 }
249 std::vector<double> prob = d.probabilities();
250 for (int i = 0; i <= d.max(); ++i)
251 if (prob[i] != 0)
252 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
253 else
254 assert(u[i] == 0);
255 }
256 {
257 typedef std::discrete_distribution<> D;
258 typedef std::minstd_rand G;
259 G g;
260 double p0[] = {33, 0, 0, 67};
261 D d(p0, p0+3);
262 const int N = 1000000;
263 std::vector<D::result_type> u(d.max()+1);
264 for (int i = 0; i < N; ++i)
265 {
266 D::result_type v = d(g);
267 assert(d.min() <= v && v <= d.max());
268 u[v]++;
269 }
270 std::vector<double> prob = d.probabilities();
271 for (int i = 0; i <= d.max(); ++i)
272 if (prob[i] != 0)
273 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
274 else
275 assert(u[i] == 0);
276 }
277 }
278