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 // REQUIRES: long_tests
11
12 // <random>
13
14 // template<class _IntType = int>
15 // class uniform_int_distribution
16
17 // template<class _URNG> result_type operator()(_URNG& g);
18
19 #include <random>
20 #include <cassert>
21 #include <vector>
22 #include <numeric>
23
24 template <class T>
25 inline
26 T
sqr(T x)27 sqr(T x)
28 {
29 return x * x;
30 }
31
main()32 int main()
33 {
34 {
35 typedef std::uniform_int_distribution<> D;
36 typedef std::minstd_rand0 G;
37 G g;
38 D d;
39 const int N = 100000;
40 std::vector<D::result_type> u;
41 for (int i = 0; i < N; ++i)
42 {
43 D::result_type v = d(g);
44 assert(d.a() <= v && v <= d.b());
45 u.push_back(v);
46 }
47 double mean = std::accumulate(u.begin(), u.end(),
48 double(0)) / u.size();
49 double var = 0;
50 double skew = 0;
51 double kurtosis = 0;
52 for (int i = 0; i < u.size(); ++i)
53 {
54 double d = (u[i] - mean);
55 double d2 = sqr(d);
56 var += d2;
57 skew += d * d2;
58 kurtosis += d2 * d2;
59 }
60 var /= u.size();
61 double dev = std::sqrt(var);
62 skew /= u.size() * dev * var;
63 kurtosis /= u.size() * var * var;
64 kurtosis -= 3;
65 double x_mean = ((double)d.a() + d.b()) / 2;
66 double x_var = (sqr((double)d.b() - d.a() + 1) - 1) / 12;
67 double x_skew = 0;
68 double x_kurtosis = -6. * (sqr((double)d.b() - d.a() + 1) + 1) /
69 (5. * (sqr((double)d.b() - d.a() + 1) - 1));
70 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
71 assert(std::abs((var - x_var) / x_var) < 0.01);
72 assert(std::abs(skew - x_skew) < 0.01);
73 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
74 }
75 {
76 typedef std::uniform_int_distribution<> D;
77 typedef std::minstd_rand G;
78 G g;
79 D d;
80 const int N = 100000;
81 std::vector<D::result_type> u;
82 for (int i = 0; i < N; ++i)
83 {
84 D::result_type v = d(g);
85 assert(d.a() <= v && v <= d.b());
86 u.push_back(v);
87 }
88 double mean = std::accumulate(u.begin(), u.end(),
89 double(0)) / u.size();
90 double var = 0;
91 double skew = 0;
92 double kurtosis = 0;
93 for (int i = 0; i < u.size(); ++i)
94 {
95 double d = (u[i] - mean);
96 double d2 = sqr(d);
97 var += d2;
98 skew += d * d2;
99 kurtosis += d2 * d2;
100 }
101 var /= u.size();
102 double dev = std::sqrt(var);
103 skew /= u.size() * dev * var;
104 kurtosis /= u.size() * var * var;
105 kurtosis -= 3;
106 double x_mean = ((double)d.a() + d.b()) / 2;
107 double x_var = (sqr((double)d.b() - d.a() + 1) - 1) / 12;
108 double x_skew = 0;
109 double x_kurtosis = -6. * (sqr((double)d.b() - d.a() + 1) + 1) /
110 (5. * (sqr((double)d.b() - d.a() + 1) - 1));
111 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
112 assert(std::abs((var - x_var) / x_var) < 0.01);
113 assert(std::abs(skew - x_skew) < 0.01);
114 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
115 }
116 {
117 typedef std::uniform_int_distribution<> D;
118 typedef std::mt19937 G;
119 G g;
120 D d;
121 const int N = 100000;
122 std::vector<D::result_type> u;
123 for (int i = 0; i < N; ++i)
124 {
125 D::result_type v = d(g);
126 assert(d.a() <= v && v <= d.b());
127 u.push_back(v);
128 }
129 double mean = std::accumulate(u.begin(), u.end(),
130 double(0)) / u.size();
131 double var = 0;
132 double skew = 0;
133 double kurtosis = 0;
134 for (int i = 0; i < u.size(); ++i)
135 {
136 double d = (u[i] - mean);
137 double d2 = sqr(d);
138 var += d2;
139 skew += d * d2;
140 kurtosis += d2 * d2;
141 }
142 var /= u.size();
143 double dev = std::sqrt(var);
144 skew /= u.size() * dev * var;
145 kurtosis /= u.size() * var * var;
146 kurtosis -= 3;
147 double x_mean = ((double)d.a() + d.b()) / 2;
148 double x_var = (sqr((double)d.b() - d.a() + 1) - 1) / 12;
149 double x_skew = 0;
150 double x_kurtosis = -6. * (sqr((double)d.b() - d.a() + 1) + 1) /
151 (5. * (sqr((double)d.b() - d.a() + 1) - 1));
152 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
153 assert(std::abs((var - x_var) / x_var) < 0.01);
154 assert(std::abs(skew - x_skew) < 0.01);
155 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
156 }
157 {
158 typedef std::uniform_int_distribution<> D;
159 typedef std::mt19937_64 G;
160 G g;
161 D d;
162 const int N = 100000;
163 std::vector<D::result_type> u;
164 for (int i = 0; i < N; ++i)
165 {
166 D::result_type v = d(g);
167 assert(d.a() <= v && v <= d.b());
168 u.push_back(v);
169 }
170 double mean = std::accumulate(u.begin(), u.end(),
171 double(0)) / u.size();
172 double var = 0;
173 double skew = 0;
174 double kurtosis = 0;
175 for (int i = 0; i < u.size(); ++i)
176 {
177 double d = (u[i] - mean);
178 double d2 = sqr(d);
179 var += d2;
180 skew += d * d2;
181 kurtosis += d2 * d2;
182 }
183 var /= u.size();
184 double dev = std::sqrt(var);
185 skew /= u.size() * dev * var;
186 kurtosis /= u.size() * var * var;
187 kurtosis -= 3;
188 double x_mean = ((double)d.a() + d.b()) / 2;
189 double x_var = (sqr((double)d.b() - d.a() + 1) - 1) / 12;
190 double x_skew = 0;
191 double x_kurtosis = -6. * (sqr((double)d.b() - d.a() + 1) + 1) /
192 (5. * (sqr((double)d.b() - d.a() + 1) - 1));
193 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
194 assert(std::abs((var - x_var) / x_var) < 0.01);
195 assert(std::abs(skew - x_skew) < 0.01);
196 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
197 }
198 {
199 typedef std::uniform_int_distribution<> D;
200 typedef std::ranlux24_base G;
201 G g;
202 D d;
203 const int N = 100000;
204 std::vector<D::result_type> u;
205 for (int i = 0; i < N; ++i)
206 {
207 D::result_type v = d(g);
208 assert(d.a() <= v && v <= d.b());
209 u.push_back(v);
210 }
211 double mean = std::accumulate(u.begin(), u.end(),
212 double(0)) / u.size();
213 double var = 0;
214 double skew = 0;
215 double kurtosis = 0;
216 for (int i = 0; i < u.size(); ++i)
217 {
218 double d = (u[i] - mean);
219 double d2 = sqr(d);
220 var += d2;
221 skew += d * d2;
222 kurtosis += d2 * d2;
223 }
224 var /= u.size();
225 double dev = std::sqrt(var);
226 skew /= u.size() * dev * var;
227 kurtosis /= u.size() * var * var;
228 kurtosis -= 3;
229 double x_mean = ((double)d.a() + d.b()) / 2;
230 double x_var = (sqr((double)d.b() - d.a() + 1) - 1) / 12;
231 double x_skew = 0;
232 double x_kurtosis = -6. * (sqr((double)d.b() - d.a() + 1) + 1) /
233 (5. * (sqr((double)d.b() - d.a() + 1) - 1));
234 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
235 assert(std::abs((var - x_var) / x_var) < 0.01);
236 assert(std::abs(skew - x_skew) < 0.01);
237 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
238 }
239 {
240 typedef std::uniform_int_distribution<> D;
241 typedef std::ranlux48_base G;
242 G g;
243 D d;
244 const int N = 100000;
245 std::vector<D::result_type> u;
246 for (int i = 0; i < N; ++i)
247 {
248 D::result_type v = d(g);
249 assert(d.a() <= v && v <= d.b());
250 u.push_back(v);
251 }
252 double mean = std::accumulate(u.begin(), u.end(),
253 double(0)) / u.size();
254 double var = 0;
255 double skew = 0;
256 double kurtosis = 0;
257 for (int i = 0; i < u.size(); ++i)
258 {
259 double d = (u[i] - mean);
260 double d2 = sqr(d);
261 var += d2;
262 skew += d * d2;
263 kurtosis += d2 * d2;
264 }
265 var /= u.size();
266 double dev = std::sqrt(var);
267 skew /= u.size() * dev * var;
268 kurtosis /= u.size() * var * var;
269 kurtosis -= 3;
270 double x_mean = ((double)d.a() + d.b()) / 2;
271 double x_var = (sqr((double)d.b() - d.a() + 1) - 1) / 12;
272 double x_skew = 0;
273 double x_kurtosis = -6. * (sqr((double)d.b() - d.a() + 1) + 1) /
274 (5. * (sqr((double)d.b() - d.a() + 1) - 1));
275 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
276 assert(std::abs((var - x_var) / x_var) < 0.01);
277 assert(std::abs(skew - x_skew) < 0.01);
278 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
279 }
280 {
281 typedef std::uniform_int_distribution<> D;
282 typedef std::ranlux24 G;
283 G g;
284 D d;
285 const int N = 100000;
286 std::vector<D::result_type> u;
287 for (int i = 0; i < N; ++i)
288 {
289 D::result_type v = d(g);
290 assert(d.a() <= v && v <= d.b());
291 u.push_back(v);
292 }
293 double mean = std::accumulate(u.begin(), u.end(),
294 double(0)) / u.size();
295 double var = 0;
296 double skew = 0;
297 double kurtosis = 0;
298 for (int i = 0; i < u.size(); ++i)
299 {
300 double d = (u[i] - mean);
301 double d2 = sqr(d);
302 var += d2;
303 skew += d * d2;
304 kurtosis += d2 * d2;
305 }
306 var /= u.size();
307 double dev = std::sqrt(var);
308 skew /= u.size() * dev * var;
309 kurtosis /= u.size() * var * var;
310 kurtosis -= 3;
311 double x_mean = ((double)d.a() + d.b()) / 2;
312 double x_var = (sqr((double)d.b() - d.a() + 1) - 1) / 12;
313 double x_skew = 0;
314 double x_kurtosis = -6. * (sqr((double)d.b() - d.a() + 1) + 1) /
315 (5. * (sqr((double)d.b() - d.a() + 1) - 1));
316 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
317 assert(std::abs((var - x_var) / x_var) < 0.01);
318 assert(std::abs(skew - x_skew) < 0.01);
319 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
320 }
321 {
322 typedef std::uniform_int_distribution<> D;
323 typedef std::ranlux48 G;
324 G g;
325 D d;
326 const int N = 100000;
327 std::vector<D::result_type> u;
328 for (int i = 0; i < N; ++i)
329 {
330 D::result_type v = d(g);
331 assert(d.a() <= v && v <= d.b());
332 u.push_back(v);
333 }
334 double mean = std::accumulate(u.begin(), u.end(),
335 double(0)) / u.size();
336 double var = 0;
337 double skew = 0;
338 double kurtosis = 0;
339 for (int i = 0; i < u.size(); ++i)
340 {
341 double d = (u[i] - mean);
342 double d2 = sqr(d);
343 var += d2;
344 skew += d * d2;
345 kurtosis += d2 * d2;
346 }
347 var /= u.size();
348 double dev = std::sqrt(var);
349 skew /= u.size() * dev * var;
350 kurtosis /= u.size() * var * var;
351 kurtosis -= 3;
352 double x_mean = ((double)d.a() + d.b()) / 2;
353 double x_var = (sqr((double)d.b() - d.a() + 1) - 1) / 12;
354 double x_skew = 0;
355 double x_kurtosis = -6. * (sqr((double)d.b() - d.a() + 1) + 1) /
356 (5. * (sqr((double)d.b() - d.a() + 1) - 1));
357 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
358 assert(std::abs((var - x_var) / x_var) < 0.01);
359 assert(std::abs(skew - x_skew) < 0.01);
360 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
361 }
362 {
363 typedef std::uniform_int_distribution<> D;
364 typedef std::knuth_b G;
365 G g;
366 D d;
367 const int N = 100000;
368 std::vector<D::result_type> u;
369 for (int i = 0; i < N; ++i)
370 {
371 D::result_type v = d(g);
372 assert(d.a() <= v && v <= d.b());
373 u.push_back(v);
374 }
375 double mean = std::accumulate(u.begin(), u.end(),
376 double(0)) / u.size();
377 double var = 0;
378 double skew = 0;
379 double kurtosis = 0;
380 for (int i = 0; i < u.size(); ++i)
381 {
382 double d = (u[i] - mean);
383 double d2 = sqr(d);
384 var += d2;
385 skew += d * d2;
386 kurtosis += d2 * d2;
387 }
388 var /= u.size();
389 double dev = std::sqrt(var);
390 skew /= u.size() * dev * var;
391 kurtosis /= u.size() * var * var;
392 kurtosis -= 3;
393 double x_mean = ((double)d.a() + d.b()) / 2;
394 double x_var = (sqr((double)d.b() - d.a() + 1) - 1) / 12;
395 double x_skew = 0;
396 double x_kurtosis = -6. * (sqr((double)d.b() - d.a() + 1) + 1) /
397 (5. * (sqr((double)d.b() - d.a() + 1) - 1));
398 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
399 assert(std::abs((var - x_var) / x_var) < 0.01);
400 assert(std::abs(skew - x_skew) < 0.01);
401 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
402 }
403 {
404 typedef std::uniform_int_distribution<> D;
405 typedef std::minstd_rand0 G;
406 G g;
407 D d(-6, 106);
408 for (int i = 0; i < 10000; ++i)
409 {
410 int u = d(g);
411 assert(-6 <= u && u <= 106);
412 }
413 }
414 {
415 typedef std::uniform_int_distribution<> D;
416 typedef std::minstd_rand G;
417 G g;
418 D d(5, 100);
419 const int N = 100000;
420 std::vector<D::result_type> u;
421 for (int i = 0; i < N; ++i)
422 {
423 D::result_type v = d(g);
424 assert(d.a() <= v && v <= d.b());
425 u.push_back(v);
426 }
427 double mean = std::accumulate(u.begin(), u.end(),
428 double(0)) / u.size();
429 double var = 0;
430 double skew = 0;
431 double kurtosis = 0;
432 for (int i = 0; i < u.size(); ++i)
433 {
434 double d = (u[i] - mean);
435 double d2 = sqr(d);
436 var += d2;
437 skew += d * d2;
438 kurtosis += d2 * d2;
439 }
440 var /= u.size();
441 double dev = std::sqrt(var);
442 skew /= u.size() * dev * var;
443 kurtosis /= u.size() * var * var;
444 kurtosis -= 3;
445 double x_mean = ((double)d.a() + d.b()) / 2;
446 double x_var = (sqr((double)d.b() - d.a() + 1) - 1) / 12;
447 double x_skew = 0;
448 double x_kurtosis = -6. * (sqr((double)d.b() - d.a() + 1) + 1) /
449 (5. * (sqr((double)d.b() - d.a() + 1) - 1));
450 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
451 assert(std::abs((var - x_var) / x_var) < 0.01);
452 assert(std::abs(skew - x_skew) < 0.01);
453 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
454 }
455 }
456