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