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