1 // random number generation -*- C++ -*- 2 3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /** 26 * @file bits/random.h 27 * This is an internal header file, included by other library headers. 28 * You should not attempt to use it directly. 29 */ 30 31 #include <vector> 32 33 namespace std 34 { 35 // [26.4] Random number generation 36 37 /** 38 * @defgroup random Random Number Generation 39 * @ingroup numerics 40 * 41 * A facility for generating random numbers on selected distributions. 42 * @{ 43 */ 44 45 /** 46 * @brief A function template for converting the output of a (integral) 47 * uniform random number generator to a floatng point result in the range 48 * [0-1). 49 */ 50 template<typename _RealType, size_t __bits, 51 typename _UniformRandomNumberGenerator> 52 _RealType 53 generate_canonical(_UniformRandomNumberGenerator& __g); 54 55 /* 56 * Implementation-space details. 57 */ 58 namespace __detail 59 { 60 template<typename _UIntType, size_t __w, 61 bool = __w < static_cast<size_t> 62 (std::numeric_limits<_UIntType>::digits)> 63 struct _Shift 64 { static const _UIntType __value = 0; }; 65 66 template<typename _UIntType, size_t __w> 67 struct _Shift<_UIntType, __w, true> 68 { static const _UIntType __value = _UIntType(1) << __w; }; 69 70 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool> 71 struct _Mod; 72 73 // Dispatch based on modulus value to prevent divide-by-zero compile-time 74 // errors when m == 0. 75 template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0> 76 inline _Tp 77 __mod(_Tp __x) 78 { return _Mod<_Tp, __m, __a, __c, __m == 0>::__calc(__x); } 79 80 /* 81 * An adaptor class for converting the output of any Generator into 82 * the input for a specific Distribution. 83 */ 84 template<typename _Engine, typename _DInputType> 85 struct _Adaptor 86 { 87 88 public: 89 _Adaptor(_Engine& __g) 90 : _M_g(__g) { } 91 92 _DInputType 93 min() const 94 { return _DInputType(0); } 95 96 _DInputType 97 max() const 98 { return _DInputType(1); } 99 100 /* 101 * Converts a value generated by the adapted random number generator 102 * into a value in the input domain for the dependent random number 103 * distribution. 104 */ 105 _DInputType 106 operator()() 107 { 108 return std::generate_canonical<_DInputType, 109 std::numeric_limits<_DInputType>::digits, 110 _Engine>(_M_g); 111 } 112 113 private: 114 _Engine& _M_g; 115 }; 116 } // namespace __detail 117 118 /** 119 * @addtogroup random_generators Random Number Generators 120 * @ingroup random 121 * 122 * These classes define objects which provide random or pseudorandom 123 * numbers, either from a discrete or a continuous interval. The 124 * random number generator supplied as a part of this library are 125 * all uniform random number generators which provide a sequence of 126 * random number uniformly distributed over their range. 127 * 128 * A number generator is a function object with an operator() that 129 * takes zero arguments and returns a number. 130 * 131 * A compliant random number generator must satisfy the following 132 * requirements. <table border=1 cellpadding=10 cellspacing=0> 133 * <caption align=top>Random Number Generator Requirements</caption> 134 * <tr><td>To be documented.</td></tr> </table> 135 * 136 * @{ 137 */ 138 139 /** 140 * @brief A model of a linear congruential random number generator. 141 * 142 * A random number generator that produces pseudorandom numbers via 143 * linear function: 144 * @f[ 145 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m 146 * @f] 147 * 148 * The template parameter @p _UIntType must be an unsigned integral type 149 * large enough to store values up to (__m-1). If the template parameter 150 * @p __m is 0, the modulus @p __m used is 151 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template 152 * parameters @p __a and @p __c must be less than @p __m. 153 * 154 * The size of the state is @f$1@f$. 155 */ 156 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> 157 class linear_congruential_engine 158 { 159 static_assert(std::is_unsigned<_UIntType>::value, "template argument " 160 "substituting _UIntType not an unsigned integral type"); 161 static_assert(__m == 0u || (__a < __m && __c < __m), 162 "template argument substituting __m out of bounds"); 163 164 public: 165 /** The type of the generated random value. */ 166 typedef _UIntType result_type; 167 168 /** The multiplier. */ 169 static const result_type multiplier = __a; 170 /** An increment. */ 171 static const result_type increment = __c; 172 /** The modulus. */ 173 static const result_type modulus = __m; 174 static const result_type default_seed = 1u; 175 176 /** 177 * @brief Constructs a %linear_congruential_engine random number 178 * generator engine with seed @p __s. The default seed value 179 * is 1. 180 * 181 * @param __s The initial seed value. 182 */ 183 explicit 184 linear_congruential_engine(result_type __s = default_seed) 185 { seed(__s); } 186 187 /** 188 * @brief Constructs a %linear_congruential_engine random number 189 * generator engine seeded from the seed sequence @p __q. 190 * 191 * @param __q the seed sequence. 192 */ 193 template<typename _Sseq, typename = typename 194 std::enable_if<!std::is_same<_Sseq, linear_congruential_engine>::value> 195 ::type> 196 explicit 197 linear_congruential_engine(_Sseq& __q) 198 { seed(__q); } 199 200 /** 201 * @brief Reseeds the %linear_congruential_engine random number generator 202 * engine sequence to the seed @p __s. 203 * 204 * @param __s The new seed. 205 */ 206 void 207 seed(result_type __s = default_seed); 208 209 /** 210 * @brief Reseeds the %linear_congruential_engine random number generator 211 * engine 212 * sequence using values from the seed sequence @p __q. 213 * 214 * @param __q the seed sequence. 215 */ 216 template<typename _Sseq> 217 typename std::enable_if<std::is_class<_Sseq>::value>::type 218 seed(_Sseq& __q); 219 220 /** 221 * @brief Gets the smallest possible value in the output range. 222 * 223 * The minimum depends on the @p __c parameter: if it is zero, the 224 * minimum generated must be > 0, otherwise 0 is allowed. 225 * 226 * @todo This should be constexpr. 227 */ 228 result_type 229 min() const 230 { return __c == 0u ? 1u : 0u; } 231 232 /** 233 * @brief Gets the largest possible value in the output range. 234 * 235 * @todo This should be constexpr. 236 */ 237 result_type 238 max() const 239 { return __m - 1u; } 240 241 /** 242 * @brief Discard a sequence of random numbers. 243 * 244 * @todo Look for a faster way to do discard. 245 */ 246 void 247 discard(unsigned long long __z) 248 { 249 for (; __z != 0ULL; --__z) 250 (*this)(); 251 } 252 253 /** 254 * @brief Gets the next random number in the sequence. 255 */ 256 result_type 257 operator()() 258 { 259 _M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x); 260 return _M_x; 261 } 262 263 /** 264 * @brief Compares two linear congruential random number generator 265 * objects of the same type for equality. 266 * 267 * @param __lhs A linear congruential random number generator object. 268 * @param __rhs Another linear congruential random number generator 269 * object. 270 * 271 * @returns true if the infinite sequences of generated values 272 * would be equal, false otherwise. 273 */ 274 friend bool 275 operator==(const linear_congruential_engine& __lhs, 276 const linear_congruential_engine& __rhs) 277 { return __lhs._M_x == __rhs._M_x; } 278 279 /** 280 * @brief Writes the textual representation of the state x(i) of x to 281 * @p __os. 282 * 283 * @param __os The output stream. 284 * @param __lcr A % linear_congruential_engine random number generator. 285 * @returns __os. 286 */ 287 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1, 288 _UIntType1 __m1, typename _CharT, typename _Traits> 289 friend std::basic_ostream<_CharT, _Traits>& 290 operator<<(std::basic_ostream<_CharT, _Traits>&, 291 const std::linear_congruential_engine<_UIntType1, 292 __a1, __c1, __m1>&); 293 294 /** 295 * @brief Sets the state of the engine by reading its textual 296 * representation from @p __is. 297 * 298 * The textual representation must have been previously written using 299 * an output stream whose imbued locale and whose type's template 300 * specialization arguments _CharT and _Traits were the same as those 301 * of @p __is. 302 * 303 * @param __is The input stream. 304 * @param __lcr A % linear_congruential_engine random number generator. 305 * @returns __is. 306 */ 307 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1, 308 _UIntType1 __m1, typename _CharT, typename _Traits> 309 friend std::basic_istream<_CharT, _Traits>& 310 operator>>(std::basic_istream<_CharT, _Traits>&, 311 std::linear_congruential_engine<_UIntType1, __a1, 312 __c1, __m1>&); 313 314 private: 315 _UIntType _M_x; 316 }; 317 318 /** 319 * @brief Compares two linear congruential random number generator 320 * objects of the same type for inequality. 321 * 322 * @param __lhs A linear congruential random number generator object. 323 * @param __rhs Another linear congruential random number generator 324 * object. 325 * 326 * @returns true if the infinite sequences of generated values 327 * would be different, false otherwise. 328 */ 329 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> 330 inline bool 331 operator!=(const std::linear_congruential_engine<_UIntType, __a, 332 __c, __m>& __lhs, 333 const std::linear_congruential_engine<_UIntType, __a, 334 __c, __m>& __rhs) 335 { return !(__lhs == __rhs); } 336 337 338 /** 339 * A generalized feedback shift register discrete random number generator. 340 * 341 * This algorithm avoids multiplication and division and is designed to be 342 * friendly to a pipelined architecture. If the parameters are chosen 343 * correctly, this generator will produce numbers with a very long period and 344 * fairly good apparent entropy, although still not cryptographically strong. 345 * 346 * The best way to use this generator is with the predefined mt19937 class. 347 * 348 * This algorithm was originally invented by Makoto Matsumoto and 349 * Takuji Nishimura. 350 * 351 * @var word_size The number of bits in each element of the state vector. 352 * @var state_size The degree of recursion. 353 * @var shift_size The period parameter. 354 * @var mask_bits The separation point bit index. 355 * @var parameter_a The last row of the twist matrix. 356 * @var output_u The first right-shift tempering matrix parameter. 357 * @var output_s The first left-shift tempering matrix parameter. 358 * @var output_b The first left-shift tempering matrix mask. 359 * @var output_t The second left-shift tempering matrix parameter. 360 * @var output_c The second left-shift tempering matrix mask. 361 * @var output_l The second right-shift tempering matrix parameter. 362 */ 363 template<typename _UIntType, size_t __w, 364 size_t __n, size_t __m, size_t __r, 365 _UIntType __a, size_t __u, _UIntType __d, size_t __s, 366 _UIntType __b, size_t __t, 367 _UIntType __c, size_t __l, _UIntType __f> 368 class mersenne_twister_engine 369 { 370 static_assert(std::is_unsigned<_UIntType>::value, "template argument " 371 "substituting _UIntType not an unsigned integral type"); 372 static_assert(1u <= __m && __m <= __n, 373 "template argument substituting __m out of bounds"); 374 static_assert(__r <= __w, "template argument substituting " 375 "__r out of bound"); 376 static_assert(__u <= __w, "template argument substituting " 377 "__u out of bound"); 378 static_assert(__s <= __w, "template argument substituting " 379 "__s out of bound"); 380 static_assert(__t <= __w, "template argument substituting " 381 "__t out of bound"); 382 static_assert(__l <= __w, "template argument substituting " 383 "__l out of bound"); 384 static_assert(__w <= std::numeric_limits<_UIntType>::digits, 385 "template argument substituting __w out of bound"); 386 static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1), 387 "template argument substituting __a out of bound"); 388 static_assert(__b <= (__detail::_Shift<_UIntType, __w>::__value - 1), 389 "template argument substituting __b out of bound"); 390 static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1), 391 "template argument substituting __c out of bound"); 392 static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1), 393 "template argument substituting __d out of bound"); 394 static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1), 395 "template argument substituting __f out of bound"); 396 397 public: 398 /** The type of the generated random value. */ 399 typedef _UIntType result_type; 400 401 // parameter values 402 static const size_t word_size = __w; 403 static const size_t state_size = __n; 404 static const size_t shift_size = __m; 405 static const size_t mask_bits = __r; 406 static const result_type xor_mask = __a; 407 static const size_t tempering_u = __u; 408 static const result_type tempering_d = __d; 409 static const size_t tempering_s = __s; 410 static const result_type tempering_b = __b; 411 static const size_t tempering_t = __t; 412 static const result_type tempering_c = __c; 413 static const size_t tempering_l = __l; 414 static const result_type initialization_multiplier = __f; 415 static const result_type default_seed = 5489u; 416 417 // constructors and member function 418 explicit 419 mersenne_twister_engine(result_type __sd = default_seed) 420 { seed(__sd); } 421 422 /** 423 * @brief Constructs a %mersenne_twister_engine random number generator 424 * engine seeded from the seed sequence @p __q. 425 * 426 * @param __q the seed sequence. 427 */ 428 template<typename _Sseq, typename = typename 429 std::enable_if<!std::is_same<_Sseq, mersenne_twister_engine>::value> 430 ::type> 431 explicit 432 mersenne_twister_engine(_Sseq& __q) 433 { seed(__q); } 434 435 void 436 seed(result_type __sd = default_seed); 437 438 template<typename _Sseq> 439 typename std::enable_if<std::is_class<_Sseq>::value>::type 440 seed(_Sseq& __q); 441 442 /** 443 * @brief Gets the smallest possible value in the output range. 444 * 445 * @todo This should be constexpr. 446 */ 447 result_type 448 min() const 449 { return 0; }; 450 451 /** 452 * @brief Gets the largest possible value in the output range. 453 * 454 * @todo This should be constexpr. 455 */ 456 result_type 457 max() const 458 { return __detail::_Shift<_UIntType, __w>::__value - 1; } 459 460 /** 461 * @brief Discard a sequence of random numbers. 462 * 463 * @todo Look for a faster way to do discard. 464 */ 465 void 466 discard(unsigned long long __z) 467 { 468 for (; __z != 0ULL; --__z) 469 (*this)(); 470 } 471 472 result_type 473 operator()(); 474 475 /** 476 * @brief Compares two % mersenne_twister_engine random number generator 477 * objects of the same type for equality. 478 * 479 * @param __lhs A % mersenne_twister_engine random number generator 480 * object. 481 * @param __rhs Another % mersenne_twister_engine random number 482 * generator object. 483 * 484 * @returns true if the infinite sequences of generated values 485 * would be equal, false otherwise. 486 */ 487 friend bool 488 operator==(const mersenne_twister_engine& __lhs, 489 const mersenne_twister_engine& __rhs) 490 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); } 491 492 /** 493 * @brief Inserts the current state of a % mersenne_twister_engine 494 * random number generator engine @p __x into the output stream 495 * @p __os. 496 * 497 * @param __os An output stream. 498 * @param __x A % mersenne_twister_engine random number generator 499 * engine. 500 * 501 * @returns The output stream with the state of @p __x inserted or in 502 * an error state. 503 */ 504 template<typename _UIntType1, 505 size_t __w1, size_t __n1, 506 size_t __m1, size_t __r1, 507 _UIntType1 __a1, size_t __u1, 508 _UIntType1 __d1, size_t __s1, 509 _UIntType1 __b1, size_t __t1, 510 _UIntType1 __c1, size_t __l1, _UIntType1 __f1, 511 typename _CharT, typename _Traits> 512 friend std::basic_ostream<_CharT, _Traits>& 513 operator<<(std::basic_ostream<_CharT, _Traits>&, 514 const std::mersenne_twister_engine<_UIntType1, __w1, __n1, 515 __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1, 516 __l1, __f1>&); 517 518 /** 519 * @brief Extracts the current state of a % mersenne_twister_engine 520 * random number generator engine @p __x from the input stream 521 * @p __is. 522 * 523 * @param __is An input stream. 524 * @param __x A % mersenne_twister_engine random number generator 525 * engine. 526 * 527 * @returns The input stream with the state of @p __x extracted or in 528 * an error state. 529 */ 530 template<typename _UIntType1, 531 size_t __w1, size_t __n1, 532 size_t __m1, size_t __r1, 533 _UIntType1 __a1, size_t __u1, 534 _UIntType1 __d1, size_t __s1, 535 _UIntType1 __b1, size_t __t1, 536 _UIntType1 __c1, size_t __l1, _UIntType1 __f1, 537 typename _CharT, typename _Traits> 538 friend std::basic_istream<_CharT, _Traits>& 539 operator>>(std::basic_istream<_CharT, _Traits>&, 540 std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1, 541 __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1, 542 __l1, __f1>&); 543 544 private: 545 _UIntType _M_x[state_size]; 546 size_t _M_p; 547 }; 548 549 /** 550 * @brief Compares two % mersenne_twister_engine random number generator 551 * objects of the same type for inequality. 552 * 553 * @param __lhs A % mersenne_twister_engine random number generator 554 * object. 555 * @param __rhs Another % mersenne_twister_engine random number 556 * generator object. 557 * 558 * @returns true if the infinite sequences of generated values 559 * would be different, false otherwise. 560 */ 561 template<typename _UIntType, size_t __w, 562 size_t __n, size_t __m, size_t __r, 563 _UIntType __a, size_t __u, _UIntType __d, size_t __s, 564 _UIntType __b, size_t __t, 565 _UIntType __c, size_t __l, _UIntType __f> 566 inline bool 567 operator!=(const std::mersenne_twister_engine<_UIntType, __w, __n, __m, 568 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __lhs, 569 const std::mersenne_twister_engine<_UIntType, __w, __n, __m, 570 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __rhs) 571 { return !(__lhs == __rhs); } 572 573 574 /** 575 * @brief The Marsaglia-Zaman generator. 576 * 577 * This is a model of a Generalized Fibonacci discrete random number 578 * generator, sometimes referred to as the SWC generator. 579 * 580 * A discrete random number generator that produces pseudorandom 581 * numbers using: 582 * @f[ 583 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m 584 * @f] 585 * 586 * The size of the state is @f$r@f$ 587 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$. 588 * 589 * @var _M_x The state of the generator. This is a ring buffer. 590 * @var _M_carry The carry. 591 * @var _M_p Current index of x(i - r). 592 */ 593 template<typename _UIntType, size_t __w, size_t __s, size_t __r> 594 class subtract_with_carry_engine 595 { 596 static_assert(std::is_unsigned<_UIntType>::value, "template argument " 597 "substituting _UIntType not an unsigned integral type"); 598 static_assert(0u < __s && __s < __r, 599 "template argument substituting __s out of bounds"); 600 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits, 601 "template argument substituting __w out of bounds"); 602 603 public: 604 /** The type of the generated random value. */ 605 typedef _UIntType result_type; 606 607 // parameter values 608 static const size_t word_size = __w; 609 static const size_t short_lag = __s; 610 static const size_t long_lag = __r; 611 static const result_type default_seed = 19780503u; 612 613 /** 614 * @brief Constructs an explicitly seeded % subtract_with_carry_engine 615 * random number generator. 616 */ 617 explicit 618 subtract_with_carry_engine(result_type __sd = default_seed) 619 { seed(__sd); } 620 621 /** 622 * @brief Constructs a %subtract_with_carry_engine random number engine 623 * seeded from the seed sequence @p __q. 624 * 625 * @param __q the seed sequence. 626 */ 627 template<typename _Sseq, typename = typename 628 std::enable_if<!std::is_same<_Sseq, subtract_with_carry_engine>::value> 629 ::type> 630 explicit 631 subtract_with_carry_engine(_Sseq& __q) 632 { seed(__q); } 633 634 /** 635 * @brief Seeds the initial state @f$x_0@f$ of the random number 636 * generator. 637 * 638 * N1688[4.19] modifies this as follows. If @p __value == 0, 639 * sets value to 19780503. In any case, with a linear 640 * congruential generator lcg(i) having parameters @f$ m_{lcg} = 641 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value 642 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m 643 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$ 644 * set carry to 1, otherwise sets carry to 0. 645 */ 646 void 647 seed(result_type __sd = default_seed); 648 649 /** 650 * @brief Seeds the initial state @f$x_0@f$ of the 651 * % subtract_with_carry_engine random number generator. 652 */ 653 template<typename _Sseq> 654 typename std::enable_if<std::is_class<_Sseq>::value>::type 655 seed(_Sseq& __q); 656 657 /** 658 * @brief Gets the inclusive minimum value of the range of random 659 * integers returned by this generator. 660 * 661 * @todo This should be constexpr. 662 */ 663 result_type 664 min() const 665 { return 0; } 666 667 /** 668 * @brief Gets the inclusive maximum value of the range of random 669 * integers returned by this generator. 670 * 671 * @todo This should be constexpr. 672 */ 673 result_type 674 max() const 675 { return __detail::_Shift<_UIntType, __w>::__value - 1; } 676 677 /** 678 * @brief Discard a sequence of random numbers. 679 * 680 * @todo Look for a faster way to do discard. 681 */ 682 void 683 discard(unsigned long long __z) 684 { 685 for (; __z != 0ULL; --__z) 686 (*this)(); 687 } 688 689 /** 690 * @brief Gets the next random number in the sequence. 691 */ 692 result_type 693 operator()(); 694 695 /** 696 * @brief Compares two % subtract_with_carry_engine random number 697 * generator objects of the same type for equality. 698 * 699 * @param __lhs A % subtract_with_carry_engine random number generator 700 * object. 701 * @param __rhs Another % subtract_with_carry_engine random number 702 * generator object. 703 * 704 * @returns true if the infinite sequences of generated values 705 * would be equal, false otherwise. 706 */ 707 friend bool 708 operator==(const subtract_with_carry_engine& __lhs, 709 const subtract_with_carry_engine& __rhs) 710 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); } 711 712 /** 713 * @brief Inserts the current state of a % subtract_with_carry_engine 714 * random number generator engine @p __x into the output stream 715 * @p __os. 716 * 717 * @param __os An output stream. 718 * @param __x A % subtract_with_carry_engine random number generator 719 * engine. 720 * 721 * @returns The output stream with the state of @p __x inserted or in 722 * an error state. 723 */ 724 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1, 725 typename _CharT, typename _Traits> 726 friend std::basic_ostream<_CharT, _Traits>& 727 operator<<(std::basic_ostream<_CharT, _Traits>&, 728 const std::subtract_with_carry_engine<_UIntType1, __w1, 729 __s1, __r1>&); 730 731 /** 732 * @brief Extracts the current state of a % subtract_with_carry_engine 733 * random number generator engine @p __x from the input stream 734 * @p __is. 735 * 736 * @param __is An input stream. 737 * @param __x A % subtract_with_carry_engine random number generator 738 * engine. 739 * 740 * @returns The input stream with the state of @p __x extracted or in 741 * an error state. 742 */ 743 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1, 744 typename _CharT, typename _Traits> 745 friend std::basic_istream<_CharT, _Traits>& 746 operator>>(std::basic_istream<_CharT, _Traits>&, 747 std::subtract_with_carry_engine<_UIntType1, __w1, 748 __s1, __r1>&); 749 750 private: 751 _UIntType _M_x[long_lag]; 752 _UIntType _M_carry; 753 size_t _M_p; 754 }; 755 756 /** 757 * @brief Compares two % subtract_with_carry_engine random number 758 * generator objects of the same type for inequality. 759 * 760 * @param __lhs A % subtract_with_carry_engine random number generator 761 * object. 762 * @param __rhs Another % subtract_with_carry_engine random number 763 * generator object. 764 * 765 * @returns true if the infinite sequences of generated values 766 * would be different, false otherwise. 767 */ 768 template<typename _UIntType, size_t __w, size_t __s, size_t __r> 769 inline bool 770 operator!=(const std::subtract_with_carry_engine<_UIntType, __w, 771 __s, __r>& __lhs, 772 const std::subtract_with_carry_engine<_UIntType, __w, 773 __s, __r>& __rhs) 774 { return !(__lhs == __rhs); } 775 776 777 /** 778 * Produces random numbers from some base engine by discarding blocks of 779 * data. 780 * 781 * 0 <= @p __r <= @p __p 782 */ 783 template<typename _RandomNumberEngine, size_t __p, size_t __r> 784 class discard_block_engine 785 { 786 static_assert(1 <= __r && __r <= __p, 787 "template argument substituting __r out of bounds"); 788 789 public: 790 /** The type of the generated random value. */ 791 typedef typename _RandomNumberEngine::result_type result_type; 792 793 // parameter values 794 static const size_t block_size = __p; 795 static const size_t used_block = __r; 796 797 /** 798 * @brief Constructs a default %discard_block_engine engine. 799 * 800 * The underlying engine is default constructed as well. 801 */ 802 discard_block_engine() 803 : _M_b(), _M_n(0) { } 804 805 /** 806 * @brief Copy constructs a %discard_block_engine engine. 807 * 808 * Copies an existing base class random number generator. 809 * @param rng An existing (base class) engine object. 810 */ 811 explicit 812 discard_block_engine(const _RandomNumberEngine& __rne) 813 : _M_b(__rne), _M_n(0) { } 814 815 /** 816 * @brief Move constructs a %discard_block_engine engine. 817 * 818 * Copies an existing base class random number generator. 819 * @param rng An existing (base class) engine object. 820 */ 821 explicit 822 discard_block_engine(_RandomNumberEngine&& __rne) 823 : _M_b(std::move(__rne)), _M_n(0) { } 824 825 /** 826 * @brief Seed constructs a %discard_block_engine engine. 827 * 828 * Constructs the underlying generator engine seeded with @p __s. 829 * @param __s A seed value for the base class engine. 830 */ 831 explicit 832 discard_block_engine(result_type __s) 833 : _M_b(__s), _M_n(0) { } 834 835 /** 836 * @brief Generator construct a %discard_block_engine engine. 837 * 838 * @param __q A seed sequence. 839 */ 840 template<typename _Sseq, typename = typename 841 std::enable_if<!std::is_same<_Sseq, discard_block_engine>::value 842 && !std::is_same<_Sseq, _RandomNumberEngine>::value> 843 ::type> 844 explicit 845 discard_block_engine(_Sseq& __q) 846 : _M_b(__q), _M_n(0) 847 { } 848 849 /** 850 * @brief Reseeds the %discard_block_engine object with the default 851 * seed for the underlying base class generator engine. 852 */ 853 void 854 seed() 855 { 856 _M_b.seed(); 857 _M_n = 0; 858 } 859 860 /** 861 * @brief Reseeds the %discard_block_engine object with the default 862 * seed for the underlying base class generator engine. 863 */ 864 void 865 seed(result_type __s) 866 { 867 _M_b.seed(__s); 868 _M_n = 0; 869 } 870 871 /** 872 * @brief Reseeds the %discard_block_engine object with the given seed 873 * sequence. 874 * @param __q A seed generator function. 875 */ 876 template<typename _Sseq> 877 void 878 seed(_Sseq& __q) 879 { 880 _M_b.seed(__q); 881 _M_n = 0; 882 } 883 884 /** 885 * @brief Gets a const reference to the underlying generator engine 886 * object. 887 */ 888 const _RandomNumberEngine& 889 base() const 890 { return _M_b; } 891 892 /** 893 * @brief Gets the minimum value in the generated random number range. 894 * 895 * @todo This should be constexpr. 896 */ 897 result_type 898 min() const 899 { return _M_b.min(); } 900 901 /** 902 * @brief Gets the maximum value in the generated random number range. 903 * 904 * @todo This should be constexpr. 905 */ 906 result_type 907 max() const 908 { return _M_b.max(); } 909 910 /** 911 * @brief Discard a sequence of random numbers. 912 * 913 * @todo Look for a faster way to do discard. 914 */ 915 void 916 discard(unsigned long long __z) 917 { 918 for (; __z != 0ULL; --__z) 919 (*this)(); 920 } 921 922 /** 923 * @brief Gets the next value in the generated random number sequence. 924 */ 925 result_type 926 operator()(); 927 928 /** 929 * @brief Compares two %discard_block_engine random number generator 930 * objects of the same type for equality. 931 * 932 * @param __lhs A %discard_block_engine random number generator object. 933 * @param __rhs Another %discard_block_engine random number generator 934 * object. 935 * 936 * @returns true if the infinite sequences of generated values 937 * would be equal, false otherwise. 938 */ 939 friend bool 940 operator==(const discard_block_engine& __lhs, 941 const discard_block_engine& __rhs) 942 { return __lhs._M_b == __rhs._M_b && __lhs._M_n == __rhs._M_n; } 943 944 /** 945 * @brief Inserts the current state of a %discard_block_engine random 946 * number generator engine @p __x into the output stream 947 * @p __os. 948 * 949 * @param __os An output stream. 950 * @param __x A %discard_block_engine random number generator engine. 951 * 952 * @returns The output stream with the state of @p __x inserted or in 953 * an error state. 954 */ 955 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1, 956 typename _CharT, typename _Traits> 957 friend std::basic_ostream<_CharT, _Traits>& 958 operator<<(std::basic_ostream<_CharT, _Traits>&, 959 const std::discard_block_engine<_RandomNumberEngine1, 960 __p1, __r1>&); 961 962 /** 963 * @brief Extracts the current state of a % subtract_with_carry_engine 964 * random number generator engine @p __x from the input stream 965 * @p __is. 966 * 967 * @param __is An input stream. 968 * @param __x A %discard_block_engine random number generator engine. 969 * 970 * @returns The input stream with the state of @p __x extracted or in 971 * an error state. 972 */ 973 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1, 974 typename _CharT, typename _Traits> 975 friend std::basic_istream<_CharT, _Traits>& 976 operator>>(std::basic_istream<_CharT, _Traits>&, 977 std::discard_block_engine<_RandomNumberEngine1, 978 __p1, __r1>&); 979 980 private: 981 _RandomNumberEngine _M_b; 982 size_t _M_n; 983 }; 984 985 /** 986 * @brief Compares two %discard_block_engine random number generator 987 * objects of the same type for inequality. 988 * 989 * @param __lhs A %discard_block_engine random number generator object. 990 * @param __rhs Another %discard_block_engine random number generator 991 * object. 992 * 993 * @returns true if the infinite sequences of generated values 994 * would be different, false otherwise. 995 */ 996 template<typename _RandomNumberEngine, size_t __p, size_t __r> 997 inline bool 998 operator!=(const std::discard_block_engine<_RandomNumberEngine, __p, 999 __r>& __lhs, 1000 const std::discard_block_engine<_RandomNumberEngine, __p, 1001 __r>& __rhs) 1002 { return !(__lhs == __rhs); } 1003 1004 1005 /** 1006 * Produces random numbers by combining random numbers from some base 1007 * engine to produce random numbers with a specifies number of bits @p __w. 1008 */ 1009 template<typename _RandomNumberEngine, size_t __w, typename _UIntType> 1010 class independent_bits_engine 1011 { 1012 static_assert(std::is_unsigned<_UIntType>::value, "template argument " 1013 "substituting _UIntType not an unsigned integral type"); 1014 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits, 1015 "template argument substituting __w out of bounds"); 1016 1017 public: 1018 /** The type of the generated random value. */ 1019 typedef _UIntType result_type; 1020 1021 /** 1022 * @brief Constructs a default %independent_bits_engine engine. 1023 * 1024 * The underlying engine is default constructed as well. 1025 */ 1026 independent_bits_engine() 1027 : _M_b() { } 1028 1029 /** 1030 * @brief Copy constructs a %independent_bits_engine engine. 1031 * 1032 * Copies an existing base class random number generator. 1033 * @param rng An existing (base class) engine object. 1034 */ 1035 explicit 1036 independent_bits_engine(const _RandomNumberEngine& __rne) 1037 : _M_b(__rne) { } 1038 1039 /** 1040 * @brief Move constructs a %independent_bits_engine engine. 1041 * 1042 * Copies an existing base class random number generator. 1043 * @param rng An existing (base class) engine object. 1044 */ 1045 explicit 1046 independent_bits_engine(_RandomNumberEngine&& __rne) 1047 : _M_b(std::move(__rne)) { } 1048 1049 /** 1050 * @brief Seed constructs a %independent_bits_engine engine. 1051 * 1052 * Constructs the underlying generator engine seeded with @p __s. 1053 * @param __s A seed value for the base class engine. 1054 */ 1055 explicit 1056 independent_bits_engine(result_type __s) 1057 : _M_b(__s) { } 1058 1059 /** 1060 * @brief Generator construct a %independent_bits_engine engine. 1061 * 1062 * @param __q A seed sequence. 1063 */ 1064 template<typename _Sseq, typename = typename 1065 std::enable_if<!std::is_same<_Sseq, independent_bits_engine>::value 1066 && !std::is_same<_Sseq, _RandomNumberEngine>::value> 1067 ::type> 1068 explicit 1069 independent_bits_engine(_Sseq& __q) 1070 : _M_b(__q) 1071 { } 1072 1073 /** 1074 * @brief Reseeds the %independent_bits_engine object with the default 1075 * seed for the underlying base class generator engine. 1076 */ 1077 void 1078 seed() 1079 { _M_b.seed(); } 1080 1081 /** 1082 * @brief Reseeds the %independent_bits_engine object with the default 1083 * seed for the underlying base class generator engine. 1084 */ 1085 void 1086 seed(result_type __s) 1087 { _M_b.seed(__s); } 1088 1089 /** 1090 * @brief Reseeds the %independent_bits_engine object with the given 1091 * seed sequence. 1092 * @param __q A seed generator function. 1093 */ 1094 template<typename _Sseq> 1095 void 1096 seed(_Sseq& __q) 1097 { _M_b.seed(__q); } 1098 1099 /** 1100 * @brief Gets a const reference to the underlying generator engine 1101 * object. 1102 */ 1103 const _RandomNumberEngine& 1104 base() const 1105 { return _M_b; } 1106 1107 /** 1108 * @brief Gets the minimum value in the generated random number range. 1109 * 1110 * @todo This should be constexpr. 1111 */ 1112 result_type 1113 min() const 1114 { return 0U; } 1115 1116 /** 1117 * @brief Gets the maximum value in the generated random number range. 1118 * 1119 * @todo This should be constexpr. 1120 */ 1121 result_type 1122 max() const 1123 { return __detail::_Shift<_UIntType, __w>::__value - 1; } 1124 1125 /** 1126 * @brief Discard a sequence of random numbers. 1127 * 1128 * @todo Look for a faster way to do discard. 1129 */ 1130 void 1131 discard(unsigned long long __z) 1132 { 1133 for (; __z != 0ULL; --__z) 1134 (*this)(); 1135 } 1136 1137 /** 1138 * @brief Gets the next value in the generated random number sequence. 1139 */ 1140 result_type 1141 operator()(); 1142 1143 /** 1144 * @brief Compares two %independent_bits_engine random number generator 1145 * objects of the same type for equality. 1146 * 1147 * @param __lhs A %independent_bits_engine random number generator 1148 * object. 1149 * @param __rhs Another %independent_bits_engine random number generator 1150 * object. 1151 * 1152 * @returns true if the infinite sequences of generated values 1153 * would be equal, false otherwise. 1154 */ 1155 friend bool 1156 operator==(const independent_bits_engine& __lhs, 1157 const independent_bits_engine& __rhs) 1158 { return __lhs._M_b == __rhs._M_b; } 1159 1160 /** 1161 * @brief Extracts the current state of a % subtract_with_carry_engine 1162 * random number generator engine @p __x from the input stream 1163 * @p __is. 1164 * 1165 * @param __is An input stream. 1166 * @param __x A %independent_bits_engine random number generator 1167 * engine. 1168 * 1169 * @returns The input stream with the state of @p __x extracted or in 1170 * an error state. 1171 */ 1172 template<typename _CharT, typename _Traits> 1173 friend std::basic_istream<_CharT, _Traits>& 1174 operator>>(std::basic_istream<_CharT, _Traits>& __is, 1175 std::independent_bits_engine<_RandomNumberEngine, 1176 __w, _UIntType>& __x) 1177 { 1178 __is >> __x._M_b; 1179 return __is; 1180 } 1181 1182 private: 1183 _RandomNumberEngine _M_b; 1184 }; 1185 1186 /** 1187 * @brief Compares two %independent_bits_engine random number generator 1188 * objects of the same type for inequality. 1189 * 1190 * @param __lhs A %independent_bits_engine random number generator 1191 * object. 1192 * @param __rhs Another %independent_bits_engine random number generator 1193 * object. 1194 * 1195 * @returns true if the infinite sequences of generated values 1196 * would be different, false otherwise. 1197 */ 1198 template<typename _RandomNumberEngine, size_t __w, typename _UIntType> 1199 inline bool 1200 operator!=(const std::independent_bits_engine<_RandomNumberEngine, __w, 1201 _UIntType>& __lhs, 1202 const std::independent_bits_engine<_RandomNumberEngine, __w, 1203 _UIntType>& __rhs) 1204 { return !(__lhs == __rhs); } 1205 1206 /** 1207 * @brief Inserts the current state of a %independent_bits_engine random 1208 * number generator engine @p __x into the output stream @p __os. 1209 * 1210 * @param __os An output stream. 1211 * @param __x A %independent_bits_engine random number generator engine. 1212 * 1213 * @returns The output stream with the state of @p __x inserted or in 1214 * an error state. 1215 */ 1216 template<typename _RandomNumberEngine, size_t __w, typename _UIntType, 1217 typename _CharT, typename _Traits> 1218 std::basic_ostream<_CharT, _Traits>& 1219 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 1220 const std::independent_bits_engine<_RandomNumberEngine, 1221 __w, _UIntType>& __x) 1222 { 1223 __os << __x.base(); 1224 return __os; 1225 } 1226 1227 1228 /** 1229 * @brief Produces random numbers by combining random numbers from some 1230 * base engine to produce random numbers with a specifies number of bits 1231 * @p __w. 1232 */ 1233 template<typename _RandomNumberEngine, size_t __k> 1234 class shuffle_order_engine 1235 { 1236 static_assert(1u <= __k, "template argument substituting " 1237 "__k out of bound"); 1238 1239 public: 1240 /** The type of the generated random value. */ 1241 typedef typename _RandomNumberEngine::result_type result_type; 1242 1243 static const size_t table_size = __k; 1244 1245 /** 1246 * @brief Constructs a default %shuffle_order_engine engine. 1247 * 1248 * The underlying engine is default constructed as well. 1249 */ 1250 shuffle_order_engine() 1251 : _M_b() 1252 { _M_initialize(); } 1253 1254 /** 1255 * @brief Copy constructs a %shuffle_order_engine engine. 1256 * 1257 * Copies an existing base class random number generator. 1258 * @param rng An existing (base class) engine object. 1259 */ 1260 explicit 1261 shuffle_order_engine(const _RandomNumberEngine& __rne) 1262 : _M_b(__rne) 1263 { _M_initialize(); } 1264 1265 /** 1266 * @brief Move constructs a %shuffle_order_engine engine. 1267 * 1268 * Copies an existing base class random number generator. 1269 * @param rng An existing (base class) engine object. 1270 */ 1271 explicit 1272 shuffle_order_engine(_RandomNumberEngine&& __rne) 1273 : _M_b(std::move(__rne)) 1274 { _M_initialize(); } 1275 1276 /** 1277 * @brief Seed constructs a %shuffle_order_engine engine. 1278 * 1279 * Constructs the underlying generator engine seeded with @p __s. 1280 * @param __s A seed value for the base class engine. 1281 */ 1282 explicit 1283 shuffle_order_engine(result_type __s) 1284 : _M_b(__s) 1285 { _M_initialize(); } 1286 1287 /** 1288 * @brief Generator construct a %shuffle_order_engine engine. 1289 * 1290 * @param __q A seed sequence. 1291 */ 1292 template<typename _Sseq, typename = typename 1293 std::enable_if<!std::is_same<_Sseq, shuffle_order_engine>::value 1294 && !std::is_same<_Sseq, _RandomNumberEngine>::value> 1295 ::type> 1296 explicit 1297 shuffle_order_engine(_Sseq& __q) 1298 : _M_b(__q) 1299 { _M_initialize(); } 1300 1301 /** 1302 * @brief Reseeds the %shuffle_order_engine object with the default seed 1303 for the underlying base class generator engine. 1304 */ 1305 void 1306 seed() 1307 { 1308 _M_b.seed(); 1309 _M_initialize(); 1310 } 1311 1312 /** 1313 * @brief Reseeds the %shuffle_order_engine object with the default seed 1314 * for the underlying base class generator engine. 1315 */ 1316 void 1317 seed(result_type __s) 1318 { 1319 _M_b.seed(__s); 1320 _M_initialize(); 1321 } 1322 1323 /** 1324 * @brief Reseeds the %shuffle_order_engine object with the given seed 1325 * sequence. 1326 * @param __q A seed generator function. 1327 */ 1328 template<typename _Sseq> 1329 void 1330 seed(_Sseq& __q) 1331 { 1332 _M_b.seed(__q); 1333 _M_initialize(); 1334 } 1335 1336 /** 1337 * Gets a const reference to the underlying generator engine object. 1338 */ 1339 const _RandomNumberEngine& 1340 base() const 1341 { return _M_b; } 1342 1343 /** 1344 * Gets the minimum value in the generated random number range. 1345 * 1346 * @todo This should be constexpr. 1347 */ 1348 result_type 1349 min() const 1350 { return _M_b.min(); } 1351 1352 /** 1353 * Gets the maximum value in the generated random number range. 1354 * 1355 * @todo This should be constexpr. 1356 */ 1357 result_type 1358 max() const 1359 { return _M_b.max(); } 1360 1361 /** 1362 * Discard a sequence of random numbers. 1363 * 1364 * @todo Look for a faster way to do discard. 1365 */ 1366 void 1367 discard(unsigned long long __z) 1368 { 1369 for (; __z != 0ULL; --__z) 1370 (*this)(); 1371 } 1372 1373 /** 1374 * Gets the next value in the generated random number sequence. 1375 */ 1376 result_type 1377 operator()(); 1378 1379 /** 1380 * Compares two %shuffle_order_engine random number generator objects 1381 * of the same type for equality. 1382 * 1383 * @param __lhs A %shuffle_order_engine random number generator object. 1384 * @param __rhs Another %shuffle_order_engine random number generator 1385 * object. 1386 * 1387 * @returns true if the infinite sequences of generated values 1388 * would be equal, false otherwise. 1389 */ 1390 friend bool 1391 operator==(const shuffle_order_engine& __lhs, 1392 const shuffle_order_engine& __rhs) 1393 { return __lhs._M_b == __rhs._M_b; } 1394 1395 /** 1396 * @brief Inserts the current state of a %shuffle_order_engine random 1397 * number generator engine @p __x into the output stream 1398 @p __os. 1399 * 1400 * @param __os An output stream. 1401 * @param __x A %shuffle_order_engine random number generator engine. 1402 * 1403 * @returns The output stream with the state of @p __x inserted or in 1404 * an error state. 1405 */ 1406 template<typename _RandomNumberEngine1, size_t __k1, 1407 typename _CharT, typename _Traits> 1408 friend std::basic_ostream<_CharT, _Traits>& 1409 operator<<(std::basic_ostream<_CharT, _Traits>&, 1410 const std::shuffle_order_engine<_RandomNumberEngine1, 1411 __k1>&); 1412 1413 /** 1414 * @brief Extracts the current state of a % subtract_with_carry_engine 1415 * random number generator engine @p __x from the input stream 1416 * @p __is. 1417 * 1418 * @param __is An input stream. 1419 * @param __x A %shuffle_order_engine random number generator engine. 1420 * 1421 * @returns The input stream with the state of @p __x extracted or in 1422 * an error state. 1423 */ 1424 template<typename _RandomNumberEngine1, size_t __k1, 1425 typename _CharT, typename _Traits> 1426 friend std::basic_istream<_CharT, _Traits>& 1427 operator>>(std::basic_istream<_CharT, _Traits>&, 1428 std::shuffle_order_engine<_RandomNumberEngine1, __k1>&); 1429 1430 private: 1431 void _M_initialize() 1432 { 1433 for (size_t __i = 0; __i < __k; ++__i) 1434 _M_v[__i] = _M_b(); 1435 _M_y = _M_b(); 1436 } 1437 1438 _RandomNumberEngine _M_b; 1439 result_type _M_v[__k]; 1440 result_type _M_y; 1441 }; 1442 1443 /** 1444 * Compares two %shuffle_order_engine random number generator objects 1445 * of the same type for inequality. 1446 * 1447 * @param __lhs A %shuffle_order_engine random number generator object. 1448 * @param __rhs Another %shuffle_order_engine random number generator 1449 * object. 1450 * 1451 * @returns true if the infinite sequences of generated values 1452 * would be different, false otherwise. 1453 */ 1454 template<typename _RandomNumberEngine, size_t __k> 1455 inline bool 1456 operator!=(const std::shuffle_order_engine<_RandomNumberEngine, 1457 __k>& __lhs, 1458 const std::shuffle_order_engine<_RandomNumberEngine, 1459 __k>& __rhs) 1460 { return !(__lhs == __rhs); } 1461 1462 1463 /** 1464 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller. 1465 */ 1466 typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL> 1467 minstd_rand0; 1468 1469 /** 1470 * An alternative LCR (Lehmer Generator function). 1471 */ 1472 typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL> 1473 minstd_rand; 1474 1475 /** 1476 * The classic Mersenne Twister. 1477 * 1478 * Reference: 1479 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally 1480 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions 1481 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30. 1482 */ 1483 typedef mersenne_twister_engine< 1484 uint_fast32_t, 1485 32, 624, 397, 31, 1486 0x9908b0dfUL, 11, 1487 0xffffffffUL, 7, 1488 0x9d2c5680UL, 15, 1489 0xefc60000UL, 18, 1812433253UL> mt19937; 1490 1491 /** 1492 * An alternative Mersenne Twister. 1493 */ 1494 typedef mersenne_twister_engine< 1495 uint_fast64_t, 1496 64, 312, 156, 31, 1497 0xb5026f5aa96619e9ULL, 29, 1498 0x5555555555555555ULL, 17, 1499 0x71d67fffeda60000ULL, 37, 1500 0xfff7eee000000000ULL, 43, 1501 6364136223846793005ULL> mt19937_64; 1502 1503 typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> 1504 ranlux24_base; 1505 1506 typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> 1507 ranlux48_base; 1508 1509 typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; 1510 1511 typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; 1512 1513 typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; 1514 1515 typedef minstd_rand0 default_random_engine; 1516 1517 /** 1518 * A standard interface to a platform-specific non-deterministic 1519 * random number generator (if any are available). 1520 */ 1521 class random_device 1522 { 1523 public: 1524 /** The type of the generated random value. */ 1525 typedef unsigned int result_type; 1526 1527 // constructors, destructors and member functions 1528 1529 #ifdef _GLIBCXX_USE_RANDOM_TR1 1530 1531 explicit 1532 random_device(const std::string& __token = "/dev/urandom") 1533 { 1534 if ((__token != "/dev/urandom" && __token != "/dev/random") 1535 || !(_M_file = std::fopen(__token.c_str(), "rb"))) 1536 std::__throw_runtime_error(__N("random_device::" 1537 "random_device(const std::string&)")); 1538 } 1539 1540 ~random_device() 1541 { std::fclose(_M_file); } 1542 1543 #else 1544 1545 explicit 1546 random_device(const std::string& __token = "mt19937") 1547 : _M_mt(_M_strtoul(__token)) { } 1548 1549 private: 1550 static unsigned long 1551 _M_strtoul(const std::string& __str) 1552 { 1553 unsigned long __ret = 5489UL; 1554 if (__str != "mt19937") 1555 { 1556 const char* __nptr = __str.c_str(); 1557 char* __endptr; 1558 __ret = std::strtoul(__nptr, &__endptr, 0); 1559 if (*__nptr == '\0' || *__endptr != '\0') 1560 std::__throw_runtime_error(__N("random_device::_M_strtoul" 1561 "(const std::string&)")); 1562 } 1563 return __ret; 1564 } 1565 1566 public: 1567 1568 #endif 1569 1570 result_type 1571 min() const 1572 { return std::numeric_limits<result_type>::min(); } 1573 1574 result_type 1575 max() const 1576 { return std::numeric_limits<result_type>::max(); } 1577 1578 double 1579 entropy() const 1580 { return 0.0; } 1581 1582 result_type 1583 operator()() 1584 { 1585 #ifdef _GLIBCXX_USE_RANDOM_TR1 1586 result_type __ret; 1587 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type), 1588 1, _M_file); 1589 return __ret; 1590 #else 1591 return _M_mt(); 1592 #endif 1593 } 1594 1595 // No copy functions. 1596 random_device(const random_device&) = delete; 1597 void operator=(const random_device&) = delete; 1598 1599 private: 1600 1601 #ifdef _GLIBCXX_USE_RANDOM_TR1 1602 FILE* _M_file; 1603 #else 1604 mt19937 _M_mt; 1605 #endif 1606 }; 1607 1608 /* @} */ // group random_generators 1609 1610 /** 1611 * @addtogroup random_distributions Random Number Distributions 1612 * @ingroup random 1613 * @{ 1614 */ 1615 1616 /** 1617 * @addtogroup random_distributions_uniform Uniform 1618 * @ingroup random_distributions 1619 * @{ 1620 */ 1621 1622 /** 1623 * @brief Uniform discrete distribution for random numbers. 1624 * A discrete random distribution on the range @f$[min, max]@f$ with equal 1625 * probability throughout the range. 1626 */ 1627 template<typename _IntType = int> 1628 class uniform_int_distribution 1629 { 1630 static_assert(std::is_integral<_IntType>::value, 1631 "template argument not an integral type"); 1632 1633 public: 1634 /** The type of the range of the distribution. */ 1635 typedef _IntType result_type; 1636 /** Parameter type. */ 1637 struct param_type 1638 { 1639 typedef uniform_int_distribution<_IntType> distribution_type; 1640 1641 explicit 1642 param_type(_IntType __a = 0, 1643 _IntType __b = std::numeric_limits<_IntType>::max()) 1644 : _M_a(__a), _M_b(__b) 1645 { 1646 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b); 1647 } 1648 1649 result_type 1650 a() const 1651 { return _M_a; } 1652 1653 result_type 1654 b() const 1655 { return _M_b; } 1656 1657 friend bool 1658 operator==(const param_type& __p1, const param_type& __p2) 1659 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } 1660 1661 private: 1662 _IntType _M_a; 1663 _IntType _M_b; 1664 }; 1665 1666 public: 1667 /** 1668 * @brief Constructs a uniform distribution object. 1669 */ 1670 explicit 1671 uniform_int_distribution(_IntType __a = 0, 1672 _IntType __b = std::numeric_limits<_IntType>::max()) 1673 : _M_param(__a, __b) 1674 { } 1675 1676 explicit 1677 uniform_int_distribution(const param_type& __p) 1678 : _M_param(__p) 1679 { } 1680 1681 /** 1682 * @brief Resets the distribution state. 1683 * 1684 * Does nothing for the uniform integer distribution. 1685 */ 1686 void 1687 reset() { } 1688 1689 result_type 1690 a() const 1691 { return _M_param.a(); } 1692 1693 result_type 1694 b() const 1695 { return _M_param.b(); } 1696 1697 /** 1698 * @brief Returns the parameter set of the distribution. 1699 */ 1700 param_type 1701 param() const 1702 { return _M_param; } 1703 1704 /** 1705 * @brief Sets the parameter set of the distribution. 1706 * @param __param The new parameter set of the distribution. 1707 */ 1708 void 1709 param(const param_type& __param) 1710 { _M_param = __param; } 1711 1712 /** 1713 * @brief Returns the inclusive lower bound of the distribution range. 1714 */ 1715 result_type 1716 min() const 1717 { return this->a(); } 1718 1719 /** 1720 * @brief Returns the inclusive upper bound of the distribution range. 1721 */ 1722 result_type 1723 max() const 1724 { return this->b(); } 1725 1726 /** 1727 * @brief Generating functions. 1728 */ 1729 template<typename _UniformRandomNumberGenerator> 1730 result_type 1731 operator()(_UniformRandomNumberGenerator& __urng) 1732 { return this->operator()(__urng, this->param()); } 1733 1734 template<typename _UniformRandomNumberGenerator> 1735 result_type 1736 operator()(_UniformRandomNumberGenerator& __urng, 1737 const param_type& __p); 1738 1739 param_type _M_param; 1740 }; 1741 1742 /** 1743 * @brief Return true if two uniform integer distributions have 1744 * the same parameters. 1745 */ 1746 template<typename _IntType> 1747 inline bool 1748 operator==(const std::uniform_int_distribution<_IntType>& __d1, 1749 const std::uniform_int_distribution<_IntType>& __d2) 1750 { return __d1.param() == __d2.param(); } 1751 1752 /** 1753 * @brief Return true if two uniform integer distributions have 1754 * different parameters. 1755 */ 1756 template<typename _IntType> 1757 inline bool 1758 operator!=(const std::uniform_int_distribution<_IntType>& __d1, 1759 const std::uniform_int_distribution<_IntType>& __d2) 1760 { return !(__d1 == __d2); } 1761 1762 /** 1763 * @brief Inserts a %uniform_int_distribution random number 1764 * distribution @p __x into the output stream @p os. 1765 * 1766 * @param __os An output stream. 1767 * @param __x A %uniform_int_distribution random number distribution. 1768 * 1769 * @returns The output stream with the state of @p __x inserted or in 1770 * an error state. 1771 */ 1772 template<typename _IntType, typename _CharT, typename _Traits> 1773 std::basic_ostream<_CharT, _Traits>& 1774 operator<<(std::basic_ostream<_CharT, _Traits>&, 1775 const std::uniform_int_distribution<_IntType>&); 1776 1777 /** 1778 * @brief Extracts a %uniform_int_distribution random number distribution 1779 * @p __x from the input stream @p __is. 1780 * 1781 * @param __is An input stream. 1782 * @param __x A %uniform_int_distribution random number generator engine. 1783 * 1784 * @returns The input stream with @p __x extracted or in an error state. 1785 */ 1786 template<typename _IntType, typename _CharT, typename _Traits> 1787 std::basic_istream<_CharT, _Traits>& 1788 operator>>(std::basic_istream<_CharT, _Traits>&, 1789 std::uniform_int_distribution<_IntType>&); 1790 1791 1792 /** 1793 * @brief Uniform continuous distribution for random numbers. 1794 * 1795 * A continuous random distribution on the range [min, max) with equal 1796 * probability throughout the range. The URNG should be real-valued and 1797 * deliver number in the range [0, 1). 1798 */ 1799 template<typename _RealType = double> 1800 class uniform_real_distribution 1801 { 1802 static_assert(std::is_floating_point<_RealType>::value, 1803 "template argument not a floating point type"); 1804 1805 public: 1806 /** The type of the range of the distribution. */ 1807 typedef _RealType result_type; 1808 /** Parameter type. */ 1809 struct param_type 1810 { 1811 typedef uniform_real_distribution<_RealType> distribution_type; 1812 1813 explicit 1814 param_type(_RealType __a = _RealType(0), 1815 _RealType __b = _RealType(1)) 1816 : _M_a(__a), _M_b(__b) 1817 { 1818 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b); 1819 } 1820 1821 result_type 1822 a() const 1823 { return _M_a; } 1824 1825 result_type 1826 b() const 1827 { return _M_b; } 1828 1829 friend bool 1830 operator==(const param_type& __p1, const param_type& __p2) 1831 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } 1832 1833 private: 1834 _RealType _M_a; 1835 _RealType _M_b; 1836 }; 1837 1838 public: 1839 /** 1840 * @brief Constructs a uniform_real_distribution object. 1841 * 1842 * @param __min [IN] The lower bound of the distribution. 1843 * @param __max [IN] The upper bound of the distribution. 1844 */ 1845 explicit 1846 uniform_real_distribution(_RealType __a = _RealType(0), 1847 _RealType __b = _RealType(1)) 1848 : _M_param(__a, __b) 1849 { } 1850 1851 explicit 1852 uniform_real_distribution(const param_type& __p) 1853 : _M_param(__p) 1854 { } 1855 1856 /** 1857 * @brief Resets the distribution state. 1858 * 1859 * Does nothing for the uniform real distribution. 1860 */ 1861 void 1862 reset() { } 1863 1864 result_type 1865 a() const 1866 { return _M_param.a(); } 1867 1868 result_type 1869 b() const 1870 { return _M_param.b(); } 1871 1872 /** 1873 * @brief Returns the parameter set of the distribution. 1874 */ 1875 param_type 1876 param() const 1877 { return _M_param; } 1878 1879 /** 1880 * @brief Sets the parameter set of the distribution. 1881 * @param __param The new parameter set of the distribution. 1882 */ 1883 void 1884 param(const param_type& __param) 1885 { _M_param = __param; } 1886 1887 /** 1888 * @brief Returns the inclusive lower bound of the distribution range. 1889 */ 1890 result_type 1891 min() const 1892 { return this->a(); } 1893 1894 /** 1895 * @brief Returns the inclusive upper bound of the distribution range. 1896 */ 1897 result_type 1898 max() const 1899 { return this->b(); } 1900 1901 /** 1902 * @brief Generating functions. 1903 */ 1904 template<typename _UniformRandomNumberGenerator> 1905 result_type 1906 operator()(_UniformRandomNumberGenerator& __urng) 1907 { return this->operator()(__urng, this->param()); } 1908 1909 template<typename _UniformRandomNumberGenerator> 1910 result_type 1911 operator()(_UniformRandomNumberGenerator& __urng, 1912 const param_type& __p) 1913 { 1914 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> 1915 __aurng(__urng); 1916 return (__aurng() * (__p.b() - __p.a())) + __p.a(); 1917 } 1918 1919 private: 1920 param_type _M_param; 1921 }; 1922 1923 /** 1924 * @brief Return true if two uniform real distributions have 1925 * the same parameters. 1926 */ 1927 template<typename _IntType> 1928 inline bool 1929 operator==(const std::uniform_real_distribution<_IntType>& __d1, 1930 const std::uniform_real_distribution<_IntType>& __d2) 1931 { return __d1.param() == __d2.param(); } 1932 1933 /** 1934 * @brief Return true if two uniform real distributions have 1935 * different parameters. 1936 */ 1937 template<typename _IntType> 1938 inline bool 1939 operator!=(const std::uniform_real_distribution<_IntType>& __d1, 1940 const std::uniform_real_distribution<_IntType>& __d2) 1941 { return !(__d1 == __d2); } 1942 1943 /** 1944 * @brief Inserts a %uniform_real_distribution random number 1945 * distribution @p __x into the output stream @p __os. 1946 * 1947 * @param __os An output stream. 1948 * @param __x A %uniform_real_distribution random number distribution. 1949 * 1950 * @returns The output stream with the state of @p __x inserted or in 1951 * an error state. 1952 */ 1953 template<typename _RealType, typename _CharT, typename _Traits> 1954 std::basic_ostream<_CharT, _Traits>& 1955 operator<<(std::basic_ostream<_CharT, _Traits>&, 1956 const std::uniform_real_distribution<_RealType>&); 1957 1958 /** 1959 * @brief Extracts a %uniform_real_distribution random number distribution 1960 * @p __x from the input stream @p __is. 1961 * 1962 * @param __is An input stream. 1963 * @param __x A %uniform_real_distribution random number generator engine. 1964 * 1965 * @returns The input stream with @p __x extracted or in an error state. 1966 */ 1967 template<typename _RealType, typename _CharT, typename _Traits> 1968 std::basic_istream<_CharT, _Traits>& 1969 operator>>(std::basic_istream<_CharT, _Traits>&, 1970 std::uniform_real_distribution<_RealType>&); 1971 1972 /* @} */ // group random_distributions_uniform 1973 1974 /** 1975 * @addtogroup random_distributions_normal Normal 1976 * @ingroup random_distributions 1977 * @{ 1978 */ 1979 1980 /** 1981 * @brief A normal continuous distribution for random numbers. 1982 * 1983 * The formula for the normal probability density function is 1984 * @f[ 1985 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}} 1986 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } 1987 * @f] 1988 */ 1989 template<typename _RealType = double> 1990 class normal_distribution 1991 { 1992 static_assert(std::is_floating_point<_RealType>::value, 1993 "template argument not a floating point type"); 1994 1995 public: 1996 /** The type of the range of the distribution. */ 1997 typedef _RealType result_type; 1998 /** Parameter type. */ 1999 struct param_type 2000 { 2001 typedef normal_distribution<_RealType> distribution_type; 2002 2003 explicit 2004 param_type(_RealType __mean = _RealType(0), 2005 _RealType __stddev = _RealType(1)) 2006 : _M_mean(__mean), _M_stddev(__stddev) 2007 { 2008 _GLIBCXX_DEBUG_ASSERT(_M_stddev > _RealType(0)); 2009 } 2010 2011 _RealType 2012 mean() const 2013 { return _M_mean; } 2014 2015 _RealType 2016 stddev() const 2017 { return _M_stddev; } 2018 2019 friend bool 2020 operator==(const param_type& __p1, const param_type& __p2) 2021 { return (__p1._M_mean == __p2._M_mean 2022 && __p1._M_stddev == __p2._M_stddev); } 2023 2024 private: 2025 _RealType _M_mean; 2026 _RealType _M_stddev; 2027 }; 2028 2029 public: 2030 /** 2031 * Constructs a normal distribution with parameters @f$mean@f$ and 2032 * standard deviation. 2033 */ 2034 explicit 2035 normal_distribution(result_type __mean = result_type(0), 2036 result_type __stddev = result_type(1)) 2037 : _M_param(__mean, __stddev), _M_saved_available(false) 2038 { } 2039 2040 explicit 2041 normal_distribution(const param_type& __p) 2042 : _M_param(__p), _M_saved_available(false) 2043 { } 2044 2045 /** 2046 * @brief Resets the distribution state. 2047 */ 2048 void 2049 reset() 2050 { _M_saved_available = false; } 2051 2052 /** 2053 * @brief Returns the mean of the distribution. 2054 */ 2055 _RealType 2056 mean() const 2057 { return _M_param.mean(); } 2058 2059 /** 2060 * @brief Returns the standard deviation of the distribution. 2061 */ 2062 _RealType 2063 stddev() const 2064 { return _M_param.stddev(); } 2065 2066 /** 2067 * @brief Returns the parameter set of the distribution. 2068 */ 2069 param_type 2070 param() const 2071 { return _M_param; } 2072 2073 /** 2074 * @brief Sets the parameter set of the distribution. 2075 * @param __param The new parameter set of the distribution. 2076 */ 2077 void 2078 param(const param_type& __param) 2079 { _M_param = __param; } 2080 2081 /** 2082 * @brief Returns the greatest lower bound value of the distribution. 2083 */ 2084 result_type 2085 min() const 2086 { return std::numeric_limits<result_type>::min(); } 2087 2088 /** 2089 * @brief Returns the least upper bound value of the distribution. 2090 */ 2091 result_type 2092 max() const 2093 { return std::numeric_limits<result_type>::max(); } 2094 2095 /** 2096 * @brief Generating functions. 2097 */ 2098 template<typename _UniformRandomNumberGenerator> 2099 result_type 2100 operator()(_UniformRandomNumberGenerator& __urng) 2101 { return this->operator()(__urng, this->param()); } 2102 2103 template<typename _UniformRandomNumberGenerator> 2104 result_type 2105 operator()(_UniformRandomNumberGenerator& __urng, 2106 const param_type& __p); 2107 2108 /** 2109 * @brief Return true if two normal distributions have 2110 * the same parameters and the sequences that would 2111 * be generated are equal. 2112 */ 2113 template<typename _RealType1> 2114 friend bool 2115 operator==(const std::normal_distribution<_RealType1>& __d1, 2116 const std::normal_distribution<_RealType1>& __d2); 2117 2118 /** 2119 * @brief Inserts a %normal_distribution random number distribution 2120 * @p __x into the output stream @p __os. 2121 * 2122 * @param __os An output stream. 2123 * @param __x A %normal_distribution random number distribution. 2124 * 2125 * @returns The output stream with the state of @p __x inserted or in 2126 * an error state. 2127 */ 2128 template<typename _RealType1, typename _CharT, typename _Traits> 2129 friend std::basic_ostream<_CharT, _Traits>& 2130 operator<<(std::basic_ostream<_CharT, _Traits>&, 2131 const std::normal_distribution<_RealType1>&); 2132 2133 /** 2134 * @brief Extracts a %normal_distribution random number distribution 2135 * @p __x from the input stream @p __is. 2136 * 2137 * @param __is An input stream. 2138 * @param __x A %normal_distribution random number generator engine. 2139 * 2140 * @returns The input stream with @p __x extracted or in an error 2141 * state. 2142 */ 2143 template<typename _RealType1, typename _CharT, typename _Traits> 2144 friend std::basic_istream<_CharT, _Traits>& 2145 operator>>(std::basic_istream<_CharT, _Traits>&, 2146 std::normal_distribution<_RealType1>&); 2147 2148 private: 2149 param_type _M_param; 2150 result_type _M_saved; 2151 bool _M_saved_available; 2152 }; 2153 2154 /** 2155 * @brief Return true if two normal distributions are different. 2156 */ 2157 template<typename _RealType> 2158 inline bool 2159 operator!=(const std::normal_distribution<_RealType>& __d1, 2160 const std::normal_distribution<_RealType>& __d2) 2161 { return !(__d1 == __d2); } 2162 2163 2164 /** 2165 * @brief A lognormal_distribution random number distribution. 2166 * 2167 * The formula for the normal probability mass function is 2168 * @f[ 2169 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}} 2170 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}} 2171 * @f] 2172 */ 2173 template<typename _RealType = double> 2174 class lognormal_distribution 2175 { 2176 static_assert(std::is_floating_point<_RealType>::value, 2177 "template argument not a floating point type"); 2178 2179 public: 2180 /** The type of the range of the distribution. */ 2181 typedef _RealType result_type; 2182 /** Parameter type. */ 2183 struct param_type 2184 { 2185 typedef lognormal_distribution<_RealType> distribution_type; 2186 2187 explicit 2188 param_type(_RealType __m = _RealType(0), 2189 _RealType __s = _RealType(1)) 2190 : _M_m(__m), _M_s(__s) 2191 { } 2192 2193 _RealType 2194 m() const 2195 { return _M_m; } 2196 2197 _RealType 2198 s() const 2199 { return _M_s; } 2200 2201 friend bool 2202 operator==(const param_type& __p1, const param_type& __p2) 2203 { return __p1._M_m == __p2._M_m && __p1._M_s == __p2._M_s; } 2204 2205 private: 2206 _RealType _M_m; 2207 _RealType _M_s; 2208 }; 2209 2210 explicit 2211 lognormal_distribution(_RealType __m = _RealType(0), 2212 _RealType __s = _RealType(1)) 2213 : _M_param(__m, __s), _M_nd() 2214 { } 2215 2216 explicit 2217 lognormal_distribution(const param_type& __p) 2218 : _M_param(__p), _M_nd() 2219 { } 2220 2221 /** 2222 * Resets the distribution state. 2223 */ 2224 void 2225 reset() 2226 { _M_nd.reset(); } 2227 2228 /** 2229 * 2230 */ 2231 _RealType 2232 m() const 2233 { return _M_param.m(); } 2234 2235 _RealType 2236 s() const 2237 { return _M_param.s(); } 2238 2239 /** 2240 * @brief Returns the parameter set of the distribution. 2241 */ 2242 param_type 2243 param() const 2244 { return _M_param; } 2245 2246 /** 2247 * @brief Sets the parameter set of the distribution. 2248 * @param __param The new parameter set of the distribution. 2249 */ 2250 void 2251 param(const param_type& __param) 2252 { _M_param = __param; } 2253 2254 /** 2255 * @brief Returns the greatest lower bound value of the distribution. 2256 */ 2257 result_type 2258 min() const 2259 { return result_type(0); } 2260 2261 /** 2262 * @brief Returns the least upper bound value of the distribution. 2263 */ 2264 result_type 2265 max() const 2266 { return std::numeric_limits<result_type>::max(); } 2267 2268 /** 2269 * @brief Generating functions. 2270 */ 2271 template<typename _UniformRandomNumberGenerator> 2272 result_type 2273 operator()(_UniformRandomNumberGenerator& __urng) 2274 { return this->operator()(__urng, this->param()); } 2275 2276 template<typename _UniformRandomNumberGenerator> 2277 result_type 2278 operator()(_UniformRandomNumberGenerator& __urng, 2279 const param_type& __p) 2280 { return std::exp(__p.s() * _M_nd(__urng) + __p.m()); } 2281 2282 /** 2283 * @brief Return true if two lognormal distributions have 2284 * the same parameters and the sequences that would 2285 * be generated are equal. 2286 */ 2287 template<typename _RealType1> 2288 friend bool 2289 operator==(const std::lognormal_distribution<_RealType1>& __d1, 2290 const std::lognormal_distribution<_RealType1>& __d2) 2291 { return (__d1.param() == __d2.param() 2292 && __d1._M_nd == __d2._M_nd); } 2293 2294 /** 2295 * @brief Inserts a %lognormal_distribution random number distribution 2296 * @p __x into the output stream @p __os. 2297 * 2298 * @param __os An output stream. 2299 * @param __x A %lognormal_distribution random number distribution. 2300 * 2301 * @returns The output stream with the state of @p __x inserted or in 2302 * an error state. 2303 */ 2304 template<typename _RealType1, typename _CharT, typename _Traits> 2305 friend std::basic_ostream<_CharT, _Traits>& 2306 operator<<(std::basic_ostream<_CharT, _Traits>&, 2307 const std::lognormal_distribution<_RealType1>&); 2308 2309 /** 2310 * @brief Extracts a %lognormal_distribution random number distribution 2311 * @p __x from the input stream @p __is. 2312 * 2313 * @param __is An input stream. 2314 * @param __x A %lognormal_distribution random number 2315 * generator engine. 2316 * 2317 * @returns The input stream with @p __x extracted or in an error state. 2318 */ 2319 template<typename _RealType1, typename _CharT, typename _Traits> 2320 friend std::basic_istream<_CharT, _Traits>& 2321 operator>>(std::basic_istream<_CharT, _Traits>&, 2322 std::lognormal_distribution<_RealType1>&); 2323 2324 private: 2325 param_type _M_param; 2326 2327 std::normal_distribution<result_type> _M_nd; 2328 }; 2329 2330 /** 2331 * @brief Return true if two lognormal distributions are different. 2332 */ 2333 template<typename _RealType> 2334 inline bool 2335 operator!=(const std::lognormal_distribution<_RealType>& __d1, 2336 const std::lognormal_distribution<_RealType>& __d2) 2337 { return !(__d1 == __d2); } 2338 2339 2340 /** 2341 * @brief A gamma continuous distribution for random numbers. 2342 * 2343 * The formula for the gamma probability density function is: 2344 * @f[ 2345 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)} 2346 * (x/\beta)^{\alpha - 1} e^{-x/\beta} 2347 * @f] 2348 */ 2349 template<typename _RealType = double> 2350 class gamma_distribution 2351 { 2352 static_assert(std::is_floating_point<_RealType>::value, 2353 "template argument not a floating point type"); 2354 2355 public: 2356 /** The type of the range of the distribution. */ 2357 typedef _RealType result_type; 2358 /** Parameter type. */ 2359 struct param_type 2360 { 2361 typedef gamma_distribution<_RealType> distribution_type; 2362 friend class gamma_distribution<_RealType>; 2363 2364 explicit 2365 param_type(_RealType __alpha_val = _RealType(1), 2366 _RealType __beta_val = _RealType(1)) 2367 : _M_alpha(__alpha_val), _M_beta(__beta_val) 2368 { 2369 _GLIBCXX_DEBUG_ASSERT(_M_alpha > _RealType(0)); 2370 _M_initialize(); 2371 } 2372 2373 _RealType 2374 alpha() const 2375 { return _M_alpha; } 2376 2377 _RealType 2378 beta() const 2379 { return _M_beta; } 2380 2381 friend bool 2382 operator==(const param_type& __p1, const param_type& __p2) 2383 { return (__p1._M_alpha == __p2._M_alpha 2384 && __p1._M_beta == __p2._M_beta); } 2385 2386 private: 2387 void 2388 _M_initialize(); 2389 2390 _RealType _M_alpha; 2391 _RealType _M_beta; 2392 2393 _RealType _M_malpha, _M_a2; 2394 }; 2395 2396 public: 2397 /** 2398 * @brief Constructs a gamma distribution with parameters 2399 * @f$\alpha@f$ and @f$\beta@f$. 2400 */ 2401 explicit 2402 gamma_distribution(_RealType __alpha_val = _RealType(1), 2403 _RealType __beta_val = _RealType(1)) 2404 : _M_param(__alpha_val, __beta_val), _M_nd() 2405 { } 2406 2407 explicit 2408 gamma_distribution(const param_type& __p) 2409 : _M_param(__p), _M_nd() 2410 { } 2411 2412 /** 2413 * @brief Resets the distribution state. 2414 */ 2415 void 2416 reset() 2417 { _M_nd.reset(); } 2418 2419 /** 2420 * @brief Returns the @f$\alpha@f$ of the distribution. 2421 */ 2422 _RealType 2423 alpha() const 2424 { return _M_param.alpha(); } 2425 2426 /** 2427 * @brief Returns the @f$\beta@f$ of the distribution. 2428 */ 2429 _RealType 2430 beta() const 2431 { return _M_param.beta(); } 2432 2433 /** 2434 * @brief Returns the parameter set of the distribution. 2435 */ 2436 param_type 2437 param() const 2438 { return _M_param; } 2439 2440 /** 2441 * @brief Sets the parameter set of the distribution. 2442 * @param __param The new parameter set of the distribution. 2443 */ 2444 void 2445 param(const param_type& __param) 2446 { _M_param = __param; } 2447 2448 /** 2449 * @brief Returns the greatest lower bound value of the distribution. 2450 */ 2451 result_type 2452 min() const 2453 { return result_type(0); } 2454 2455 /** 2456 * @brief Returns the least upper bound value of the distribution. 2457 */ 2458 result_type 2459 max() const 2460 { return std::numeric_limits<result_type>::max(); } 2461 2462 /** 2463 * @brief Generating functions. 2464 */ 2465 template<typename _UniformRandomNumberGenerator> 2466 result_type 2467 operator()(_UniformRandomNumberGenerator& __urng) 2468 { return this->operator()(__urng, this->param()); } 2469 2470 template<typename _UniformRandomNumberGenerator> 2471 result_type 2472 operator()(_UniformRandomNumberGenerator& __urng, 2473 const param_type& __p); 2474 2475 /** 2476 * @brief Return true if two gamma distributions have the same 2477 * parameters and the sequences that would be generated 2478 * are equal. 2479 */ 2480 template<typename _RealType1> 2481 friend bool 2482 operator==(const std::gamma_distribution<_RealType1>& __d1, 2483 const std::gamma_distribution<_RealType1>& __d2) 2484 { return (__d1.param() == __d2.param() 2485 && __d1._M_nd == __d2._M_nd); } 2486 2487 /** 2488 * @brief Inserts a %gamma_distribution random number distribution 2489 * @p __x into the output stream @p __os. 2490 * 2491 * @param __os An output stream. 2492 * @param __x A %gamma_distribution random number distribution. 2493 * 2494 * @returns The output stream with the state of @p __x inserted or in 2495 * an error state. 2496 */ 2497 template<typename _RealType1, typename _CharT, typename _Traits> 2498 friend std::basic_ostream<_CharT, _Traits>& 2499 operator<<(std::basic_ostream<_CharT, _Traits>&, 2500 const std::gamma_distribution<_RealType1>&); 2501 2502 /** 2503 * @brief Extracts a %gamma_distribution random number distribution 2504 * @p __x from the input stream @p __is. 2505 * 2506 * @param __is An input stream. 2507 * @param __x A %gamma_distribution random number generator engine. 2508 * 2509 * @returns The input stream with @p __x extracted or in an error state. 2510 */ 2511 template<typename _RealType1, typename _CharT, typename _Traits> 2512 friend std::basic_istream<_CharT, _Traits>& 2513 operator>>(std::basic_istream<_CharT, _Traits>&, 2514 std::gamma_distribution<_RealType1>&); 2515 2516 private: 2517 param_type _M_param; 2518 2519 std::normal_distribution<result_type> _M_nd; 2520 }; 2521 2522 /** 2523 * @brief Return true if two gamma distributions are different. 2524 */ 2525 template<typename _RealType> 2526 inline bool 2527 operator!=(const std::gamma_distribution<_RealType>& __d1, 2528 const std::gamma_distribution<_RealType>& __d2) 2529 { return !(__d1 == __d2); } 2530 2531 2532 /** 2533 * @brief A chi_squared_distribution random number distribution. 2534 * 2535 * The formula for the normal probability mass function is 2536 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$ 2537 */ 2538 template<typename _RealType = double> 2539 class chi_squared_distribution 2540 { 2541 static_assert(std::is_floating_point<_RealType>::value, 2542 "template argument not a floating point type"); 2543 2544 public: 2545 /** The type of the range of the distribution. */ 2546 typedef _RealType result_type; 2547 /** Parameter type. */ 2548 struct param_type 2549 { 2550 typedef chi_squared_distribution<_RealType> distribution_type; 2551 2552 explicit 2553 param_type(_RealType __n = _RealType(1)) 2554 : _M_n(__n) 2555 { } 2556 2557 _RealType 2558 n() const 2559 { return _M_n; } 2560 2561 friend bool 2562 operator==(const param_type& __p1, const param_type& __p2) 2563 { return __p1._M_n == __p2._M_n; } 2564 2565 private: 2566 _RealType _M_n; 2567 }; 2568 2569 explicit 2570 chi_squared_distribution(_RealType __n = _RealType(1)) 2571 : _M_param(__n), _M_gd(__n / 2) 2572 { } 2573 2574 explicit 2575 chi_squared_distribution(const param_type& __p) 2576 : _M_param(__p), _M_gd(__p.n() / 2) 2577 { } 2578 2579 /** 2580 * @brief Resets the distribution state. 2581 */ 2582 void 2583 reset() 2584 { _M_gd.reset(); } 2585 2586 /** 2587 * 2588 */ 2589 _RealType 2590 n() const 2591 { return _M_param.n(); } 2592 2593 /** 2594 * @brief Returns the parameter set of the distribution. 2595 */ 2596 param_type 2597 param() const 2598 { return _M_param; } 2599 2600 /** 2601 * @brief Sets the parameter set of the distribution. 2602 * @param __param The new parameter set of the distribution. 2603 */ 2604 void 2605 param(const param_type& __param) 2606 { _M_param = __param; } 2607 2608 /** 2609 * @brief Returns the greatest lower bound value of the distribution. 2610 */ 2611 result_type 2612 min() const 2613 { return result_type(0); } 2614 2615 /** 2616 * @brief Returns the least upper bound value of the distribution. 2617 */ 2618 result_type 2619 max() const 2620 { return std::numeric_limits<result_type>::max(); } 2621 2622 /** 2623 * @brief Generating functions. 2624 */ 2625 template<typename _UniformRandomNumberGenerator> 2626 result_type 2627 operator()(_UniformRandomNumberGenerator& __urng) 2628 { return 2 * _M_gd(__urng); } 2629 2630 template<typename _UniformRandomNumberGenerator> 2631 result_type 2632 operator()(_UniformRandomNumberGenerator& __urng, 2633 const param_type& __p) 2634 { 2635 typedef typename std::gamma_distribution<result_type>::param_type 2636 param_type; 2637 return 2 * _M_gd(__urng, param_type(__p.n() / 2)); 2638 } 2639 2640 /** 2641 * @brief Return true if two Chi-squared distributions have 2642 * the same parameters and the sequences that would be 2643 * generated are equal. 2644 */ 2645 template<typename _RealType1> 2646 friend bool 2647 operator==(const std::chi_squared_distribution<_RealType1>& __d1, 2648 const std::chi_squared_distribution<_RealType1>& __d2) 2649 { return __d1.param() == __d2.param() && __d1._M_gd == __d2._M_gd; } 2650 2651 /** 2652 * @brief Inserts a %chi_squared_distribution random number distribution 2653 * @p __x into the output stream @p __os. 2654 * 2655 * @param __os An output stream. 2656 * @param __x A %chi_squared_distribution random number distribution. 2657 * 2658 * @returns The output stream with the state of @p __x inserted or in 2659 * an error state. 2660 */ 2661 template<typename _RealType1, typename _CharT, typename _Traits> 2662 friend std::basic_ostream<_CharT, _Traits>& 2663 operator<<(std::basic_ostream<_CharT, _Traits>&, 2664 const std::chi_squared_distribution<_RealType1>&); 2665 2666 /** 2667 * @brief Extracts a %chi_squared_distribution random number distribution 2668 * @p __x from the input stream @p __is. 2669 * 2670 * @param __is An input stream. 2671 * @param __x A %chi_squared_distribution random number 2672 * generator engine. 2673 * 2674 * @returns The input stream with @p __x extracted or in an error state. 2675 */ 2676 template<typename _RealType1, typename _CharT, typename _Traits> 2677 friend std::basic_istream<_CharT, _Traits>& 2678 operator>>(std::basic_istream<_CharT, _Traits>&, 2679 std::chi_squared_distribution<_RealType1>&); 2680 2681 private: 2682 param_type _M_param; 2683 2684 std::gamma_distribution<result_type> _M_gd; 2685 }; 2686 2687 /** 2688 * @brief Return true if two Chi-squared distributions are different. 2689 */ 2690 template<typename _RealType> 2691 inline bool 2692 operator!=(const std::chi_squared_distribution<_RealType>& __d1, 2693 const std::chi_squared_distribution<_RealType>& __d2) 2694 { return !(__d1 == __d2); } 2695 2696 2697 /** 2698 * @brief A cauchy_distribution random number distribution. 2699 * 2700 * The formula for the normal probability mass function is 2701 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$ 2702 */ 2703 template<typename _RealType = double> 2704 class cauchy_distribution 2705 { 2706 static_assert(std::is_floating_point<_RealType>::value, 2707 "template argument not a floating point type"); 2708 2709 public: 2710 /** The type of the range of the distribution. */ 2711 typedef _RealType result_type; 2712 /** Parameter type. */ 2713 struct param_type 2714 { 2715 typedef cauchy_distribution<_RealType> distribution_type; 2716 2717 explicit 2718 param_type(_RealType __a = _RealType(0), 2719 _RealType __b = _RealType(1)) 2720 : _M_a(__a), _M_b(__b) 2721 { } 2722 2723 _RealType 2724 a() const 2725 { return _M_a; } 2726 2727 _RealType 2728 b() const 2729 { return _M_b; } 2730 2731 friend bool 2732 operator==(const param_type& __p1, const param_type& __p2) 2733 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } 2734 2735 private: 2736 _RealType _M_a; 2737 _RealType _M_b; 2738 }; 2739 2740 explicit 2741 cauchy_distribution(_RealType __a = _RealType(0), 2742 _RealType __b = _RealType(1)) 2743 : _M_param(__a, __b) 2744 { } 2745 2746 explicit 2747 cauchy_distribution(const param_type& __p) 2748 : _M_param(__p) 2749 { } 2750 2751 /** 2752 * @brief Resets the distribution state. 2753 */ 2754 void 2755 reset() 2756 { } 2757 2758 /** 2759 * 2760 */ 2761 _RealType 2762 a() const 2763 { return _M_param.a(); } 2764 2765 _RealType 2766 b() const 2767 { return _M_param.b(); } 2768 2769 /** 2770 * @brief Returns the parameter set of the distribution. 2771 */ 2772 param_type 2773 param() const 2774 { return _M_param; } 2775 2776 /** 2777 * @brief Sets the parameter set of the distribution. 2778 * @param __param The new parameter set of the distribution. 2779 */ 2780 void 2781 param(const param_type& __param) 2782 { _M_param = __param; } 2783 2784 /** 2785 * @brief Returns the greatest lower bound value of the distribution. 2786 */ 2787 result_type 2788 min() const 2789 { return std::numeric_limits<result_type>::min(); } 2790 2791 /** 2792 * @brief Returns the least upper bound value of the distribution. 2793 */ 2794 result_type 2795 max() const 2796 { return std::numeric_limits<result_type>::max(); } 2797 2798 /** 2799 * @brief Generating functions. 2800 */ 2801 template<typename _UniformRandomNumberGenerator> 2802 result_type 2803 operator()(_UniformRandomNumberGenerator& __urng) 2804 { return this->operator()(__urng, this->param()); } 2805 2806 template<typename _UniformRandomNumberGenerator> 2807 result_type 2808 operator()(_UniformRandomNumberGenerator& __urng, 2809 const param_type& __p); 2810 2811 private: 2812 param_type _M_param; 2813 }; 2814 2815 /** 2816 * @brief Return true if two Cauchy distributions have 2817 * the same parameters. 2818 */ 2819 template<typename _RealType> 2820 inline bool 2821 operator==(const std::cauchy_distribution<_RealType>& __d1, 2822 const std::cauchy_distribution<_RealType>& __d2) 2823 { return __d1.param() == __d2.param(); } 2824 2825 /** 2826 * @brief Return true if two Cauchy distributions have 2827 * different parameters. 2828 */ 2829 template<typename _RealType> 2830 inline bool 2831 operator!=(const std::cauchy_distribution<_RealType>& __d1, 2832 const std::cauchy_distribution<_RealType>& __d2) 2833 { return !(__d1 == __d2); } 2834 2835 /** 2836 * @brief Inserts a %cauchy_distribution random number distribution 2837 * @p __x into the output stream @p __os. 2838 * 2839 * @param __os An output stream. 2840 * @param __x A %cauchy_distribution random number distribution. 2841 * 2842 * @returns The output stream with the state of @p __x inserted or in 2843 * an error state. 2844 */ 2845 template<typename _RealType, typename _CharT, typename _Traits> 2846 std::basic_ostream<_CharT, _Traits>& 2847 operator<<(std::basic_ostream<_CharT, _Traits>&, 2848 const std::cauchy_distribution<_RealType>&); 2849 2850 /** 2851 * @brief Extracts a %cauchy_distribution random number distribution 2852 * @p __x from the input stream @p __is. 2853 * 2854 * @param __is An input stream. 2855 * @param __x A %cauchy_distribution random number 2856 * generator engine. 2857 * 2858 * @returns The input stream with @p __x extracted or in an error state. 2859 */ 2860 template<typename _RealType, typename _CharT, typename _Traits> 2861 std::basic_istream<_CharT, _Traits>& 2862 operator>>(std::basic_istream<_CharT, _Traits>&, 2863 std::cauchy_distribution<_RealType>&); 2864 2865 2866 /** 2867 * @brief A fisher_f_distribution random number distribution. 2868 * 2869 * The formula for the normal probability mass function is 2870 * @f[ 2871 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)} 2872 * (\frac{m}{n})^{m/2} x^{(m/2)-1} 2873 * (1 + \frac{mx}{n})^{-(m+n)/2} 2874 * @f] 2875 */ 2876 template<typename _RealType = double> 2877 class fisher_f_distribution 2878 { 2879 static_assert(std::is_floating_point<_RealType>::value, 2880 "template argument not a floating point type"); 2881 2882 public: 2883 /** The type of the range of the distribution. */ 2884 typedef _RealType result_type; 2885 /** Parameter type. */ 2886 struct param_type 2887 { 2888 typedef fisher_f_distribution<_RealType> distribution_type; 2889 2890 explicit 2891 param_type(_RealType __m = _RealType(1), 2892 _RealType __n = _RealType(1)) 2893 : _M_m(__m), _M_n(__n) 2894 { } 2895 2896 _RealType 2897 m() const 2898 { return _M_m; } 2899 2900 _RealType 2901 n() const 2902 { return _M_n; } 2903 2904 friend bool 2905 operator==(const param_type& __p1, const param_type& __p2) 2906 { return __p1._M_m == __p2._M_m && __p1._M_n == __p2._M_n; } 2907 2908 private: 2909 _RealType _M_m; 2910 _RealType _M_n; 2911 }; 2912 2913 explicit 2914 fisher_f_distribution(_RealType __m = _RealType(1), 2915 _RealType __n = _RealType(1)) 2916 : _M_param(__m, __n), _M_gd_x(__m / 2), _M_gd_y(__n / 2) 2917 { } 2918 2919 explicit 2920 fisher_f_distribution(const param_type& __p) 2921 : _M_param(__p), _M_gd_x(__p.m() / 2), _M_gd_y(__p.n() / 2) 2922 { } 2923 2924 /** 2925 * @brief Resets the distribution state. 2926 */ 2927 void 2928 reset() 2929 { 2930 _M_gd_x.reset(); 2931 _M_gd_y.reset(); 2932 } 2933 2934 /** 2935 * 2936 */ 2937 _RealType 2938 m() const 2939 { return _M_param.m(); } 2940 2941 _RealType 2942 n() const 2943 { return _M_param.n(); } 2944 2945 /** 2946 * @brief Returns the parameter set of the distribution. 2947 */ 2948 param_type 2949 param() const 2950 { return _M_param; } 2951 2952 /** 2953 * @brief Sets the parameter set of the distribution. 2954 * @param __param The new parameter set of the distribution. 2955 */ 2956 void 2957 param(const param_type& __param) 2958 { _M_param = __param; } 2959 2960 /** 2961 * @brief Returns the greatest lower bound value of the distribution. 2962 */ 2963 result_type 2964 min() const 2965 { return result_type(0); } 2966 2967 /** 2968 * @brief Returns the least upper bound value of the distribution. 2969 */ 2970 result_type 2971 max() const 2972 { return std::numeric_limits<result_type>::max(); } 2973 2974 /** 2975 * @brief Generating functions. 2976 */ 2977 template<typename _UniformRandomNumberGenerator> 2978 result_type 2979 operator()(_UniformRandomNumberGenerator& __urng) 2980 { return (_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()); } 2981 2982 template<typename _UniformRandomNumberGenerator> 2983 result_type 2984 operator()(_UniformRandomNumberGenerator& __urng, 2985 const param_type& __p) 2986 { 2987 typedef typename std::gamma_distribution<result_type>::param_type 2988 param_type; 2989 return ((_M_gd_x(__urng, param_type(__p.m() / 2)) * n()) 2990 / (_M_gd_y(__urng, param_type(__p.n() / 2)) * m())); 2991 } 2992 2993 /** 2994 * @brief Return true if two Fisher f distributions have 2995 * the same parameters and the sequences that would 2996 * be generated are equal. 2997 */ 2998 template<typename _RealType1> 2999 friend bool 3000 operator==(const std::fisher_f_distribution<_RealType1>& __d1, 3001 const std::fisher_f_distribution<_RealType1>& __d2) 3002 { return (__d1.param() == __d2.param() 3003 && __d1._M_gd_x == __d2._M_gd_x 3004 && __d1._M_gd_y == __d2._M_gd_y); } 3005 3006 /** 3007 * @brief Inserts a %fisher_f_distribution random number distribution 3008 * @p __x into the output stream @p __os. 3009 * 3010 * @param __os An output stream. 3011 * @param __x A %fisher_f_distribution random number distribution. 3012 * 3013 * @returns The output stream with the state of @p __x inserted or in 3014 * an error state. 3015 */ 3016 template<typename _RealType1, typename _CharT, typename _Traits> 3017 friend std::basic_ostream<_CharT, _Traits>& 3018 operator<<(std::basic_ostream<_CharT, _Traits>&, 3019 const std::fisher_f_distribution<_RealType1>&); 3020 3021 /** 3022 * @brief Extracts a %fisher_f_distribution random number distribution 3023 * @p __x from the input stream @p __is. 3024 * 3025 * @param __is An input stream. 3026 * @param __x A %fisher_f_distribution random number 3027 * generator engine. 3028 * 3029 * @returns The input stream with @p __x extracted or in an error state. 3030 */ 3031 template<typename _RealType1, typename _CharT, typename _Traits> 3032 friend std::basic_istream<_CharT, _Traits>& 3033 operator>>(std::basic_istream<_CharT, _Traits>&, 3034 std::fisher_f_distribution<_RealType1>&); 3035 3036 private: 3037 param_type _M_param; 3038 3039 std::gamma_distribution<result_type> _M_gd_x, _M_gd_y; 3040 }; 3041 3042 /** 3043 * @brief Return true if two Fisher f distributions are diferent. 3044 */ 3045 template<typename _RealType> 3046 inline bool 3047 operator!=(const std::fisher_f_distribution<_RealType>& __d1, 3048 const std::fisher_f_distribution<_RealType>& __d2) 3049 { return !(__d1 == __d2); } 3050 3051 /** 3052 * @brief A student_t_distribution random number distribution. 3053 * 3054 * The formula for the normal probability mass function is: 3055 * @f[ 3056 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)} 3057 * (1 + \frac{x^2}{n}) ^{-(n+1)/2} 3058 * @f] 3059 */ 3060 template<typename _RealType = double> 3061 class student_t_distribution 3062 { 3063 static_assert(std::is_floating_point<_RealType>::value, 3064 "template argument not a floating point type"); 3065 3066 public: 3067 /** The type of the range of the distribution. */ 3068 typedef _RealType result_type; 3069 /** Parameter type. */ 3070 struct param_type 3071 { 3072 typedef student_t_distribution<_RealType> distribution_type; 3073 3074 explicit 3075 param_type(_RealType __n = _RealType(1)) 3076 : _M_n(__n) 3077 { } 3078 3079 _RealType 3080 n() const 3081 { return _M_n; } 3082 3083 friend bool 3084 operator==(const param_type& __p1, const param_type& __p2) 3085 { return __p1._M_n == __p2._M_n; } 3086 3087 private: 3088 _RealType _M_n; 3089 }; 3090 3091 explicit 3092 student_t_distribution(_RealType __n = _RealType(1)) 3093 : _M_param(__n), _M_nd(), _M_gd(__n / 2, 2) 3094 { } 3095 3096 explicit 3097 student_t_distribution(const param_type& __p) 3098 : _M_param(__p), _M_nd(), _M_gd(__p.n() / 2, 2) 3099 { } 3100 3101 /** 3102 * @brief Resets the distribution state. 3103 */ 3104 void 3105 reset() 3106 { 3107 _M_nd.reset(); 3108 _M_gd.reset(); 3109 } 3110 3111 /** 3112 * 3113 */ 3114 _RealType 3115 n() const 3116 { return _M_param.n(); } 3117 3118 /** 3119 * @brief Returns the parameter set of the distribution. 3120 */ 3121 param_type 3122 param() const 3123 { return _M_param; } 3124 3125 /** 3126 * @brief Sets the parameter set of the distribution. 3127 * @param __param The new parameter set of the distribution. 3128 */ 3129 void 3130 param(const param_type& __param) 3131 { _M_param = __param; } 3132 3133 /** 3134 * @brief Returns the greatest lower bound value of the distribution. 3135 */ 3136 result_type 3137 min() const 3138 { return std::numeric_limits<result_type>::min(); } 3139 3140 /** 3141 * @brief Returns the least upper bound value of the distribution. 3142 */ 3143 result_type 3144 max() const 3145 { return std::numeric_limits<result_type>::max(); } 3146 3147 /** 3148 * @brief Generating functions. 3149 */ 3150 template<typename _UniformRandomNumberGenerator> 3151 result_type 3152 operator()(_UniformRandomNumberGenerator& __urng) 3153 { return _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng)); } 3154 3155 template<typename _UniformRandomNumberGenerator> 3156 result_type 3157 operator()(_UniformRandomNumberGenerator& __urng, 3158 const param_type& __p) 3159 { 3160 typedef typename std::gamma_distribution<result_type>::param_type 3161 param_type; 3162 3163 const result_type __g = _M_gd(__urng, param_type(__p.n() / 2, 2)); 3164 return _M_nd(__urng) * std::sqrt(__p.n() / __g); 3165 } 3166 3167 /** 3168 * @brief Return true if two Student t distributions have 3169 * the same parameters and the sequences that would 3170 * be generated are equal. 3171 */ 3172 template<typename _RealType1> 3173 friend bool 3174 operator==(const std::student_t_distribution<_RealType1>& __d1, 3175 const std::student_t_distribution<_RealType1>& __d2) 3176 { return (__d1.param() == __d2.param() 3177 && __d1._M_nd == __d2._M_nd && __d1._M_gd == __d2._M_gd); } 3178 3179 /** 3180 * @brief Inserts a %student_t_distribution random number distribution 3181 * @p __x into the output stream @p __os. 3182 * 3183 * @param __os An output stream. 3184 * @param __x A %student_t_distribution random number distribution. 3185 * 3186 * @returns The output stream with the state of @p __x inserted or in 3187 * an error state. 3188 */ 3189 template<typename _RealType1, typename _CharT, typename _Traits> 3190 friend std::basic_ostream<_CharT, _Traits>& 3191 operator<<(std::basic_ostream<_CharT, _Traits>&, 3192 const std::student_t_distribution<_RealType1>&); 3193 3194 /** 3195 * @brief Extracts a %student_t_distribution random number distribution 3196 * @p __x from the input stream @p __is. 3197 * 3198 * @param __is An input stream. 3199 * @param __x A %student_t_distribution random number 3200 * generator engine. 3201 * 3202 * @returns The input stream with @p __x extracted or in an error state. 3203 */ 3204 template<typename _RealType1, typename _CharT, typename _Traits> 3205 friend std::basic_istream<_CharT, _Traits>& 3206 operator>>(std::basic_istream<_CharT, _Traits>&, 3207 std::student_t_distribution<_RealType1>&); 3208 3209 private: 3210 param_type _M_param; 3211 3212 std::normal_distribution<result_type> _M_nd; 3213 std::gamma_distribution<result_type> _M_gd; 3214 }; 3215 3216 /** 3217 * @brief Return true if two Student t distributions are different. 3218 */ 3219 template<typename _RealType> 3220 inline bool 3221 operator!=(const std::student_t_distribution<_RealType>& __d1, 3222 const std::student_t_distribution<_RealType>& __d2) 3223 { return !(__d1 == __d2); } 3224 3225 3226 /* @} */ // group random_distributions_normal 3227 3228 /** 3229 * @addtogroup random_distributions_bernoulli Bernoulli 3230 * @ingroup random_distributions 3231 * @{ 3232 */ 3233 3234 /** 3235 * @brief A Bernoulli random number distribution. 3236 * 3237 * Generates a sequence of true and false values with likelihood @f$p@f$ 3238 * that true will come up and @f$(1 - p)@f$ that false will appear. 3239 */ 3240 class bernoulli_distribution 3241 { 3242 public: 3243 /** The type of the range of the distribution. */ 3244 typedef bool result_type; 3245 /** Parameter type. */ 3246 struct param_type 3247 { 3248 typedef bernoulli_distribution distribution_type; 3249 3250 explicit 3251 param_type(double __p = 0.5) 3252 : _M_p(__p) 3253 { 3254 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0)); 3255 } 3256 3257 double 3258 p() const 3259 { return _M_p; } 3260 3261 friend bool 3262 operator==(const param_type& __p1, const param_type& __p2) 3263 { return __p1._M_p == __p2._M_p; } 3264 3265 private: 3266 double _M_p; 3267 }; 3268 3269 public: 3270 /** 3271 * @brief Constructs a Bernoulli distribution with likelihood @p p. 3272 * 3273 * @param __p [IN] The likelihood of a true result being returned. 3274 * Must be in the interval @f$[0, 1]@f$. 3275 */ 3276 explicit 3277 bernoulli_distribution(double __p = 0.5) 3278 : _M_param(__p) 3279 { } 3280 3281 explicit 3282 bernoulli_distribution(const param_type& __p) 3283 : _M_param(__p) 3284 { } 3285 3286 /** 3287 * @brief Resets the distribution state. 3288 * 3289 * Does nothing for a Bernoulli distribution. 3290 */ 3291 void 3292 reset() { } 3293 3294 /** 3295 * @brief Returns the @p p parameter of the distribution. 3296 */ 3297 double 3298 p() const 3299 { return _M_param.p(); } 3300 3301 /** 3302 * @brief Returns the parameter set of the distribution. 3303 */ 3304 param_type 3305 param() const 3306 { return _M_param; } 3307 3308 /** 3309 * @brief Sets the parameter set of the distribution. 3310 * @param __param The new parameter set of the distribution. 3311 */ 3312 void 3313 param(const param_type& __param) 3314 { _M_param = __param; } 3315 3316 /** 3317 * @brief Returns the greatest lower bound value of the distribution. 3318 */ 3319 result_type 3320 min() const 3321 { return std::numeric_limits<result_type>::min(); } 3322 3323 /** 3324 * @brief Returns the least upper bound value of the distribution. 3325 */ 3326 result_type 3327 max() const 3328 { return std::numeric_limits<result_type>::max(); } 3329 3330 /** 3331 * @brief Generating functions. 3332 */ 3333 template<typename _UniformRandomNumberGenerator> 3334 result_type 3335 operator()(_UniformRandomNumberGenerator& __urng) 3336 { return this->operator()(__urng, this->param()); } 3337 3338 template<typename _UniformRandomNumberGenerator> 3339 result_type 3340 operator()(_UniformRandomNumberGenerator& __urng, 3341 const param_type& __p) 3342 { 3343 __detail::_Adaptor<_UniformRandomNumberGenerator, double> 3344 __aurng(__urng); 3345 if ((__aurng() - __aurng.min()) 3346 < __p.p() * (__aurng.max() - __aurng.min())) 3347 return true; 3348 return false; 3349 } 3350 3351 private: 3352 param_type _M_param; 3353 }; 3354 3355 /** 3356 * @brief Return true if two Bernoulli distributions have 3357 * the same parameters. 3358 */ 3359 inline bool 3360 operator==(const std::bernoulli_distribution& __d1, 3361 const std::bernoulli_distribution& __d2) 3362 { return __d1.param() == __d2.param(); } 3363 3364 /** 3365 * @brief Return true if two Bernoulli distributions have 3366 * different parameters. 3367 */ 3368 inline bool 3369 operator!=(const std::bernoulli_distribution& __d1, 3370 const std::bernoulli_distribution& __d2) 3371 { return !(__d1 == __d2); } 3372 3373 /** 3374 * @brief Inserts a %bernoulli_distribution random number distribution 3375 * @p __x into the output stream @p __os. 3376 * 3377 * @param __os An output stream. 3378 * @param __x A %bernoulli_distribution random number distribution. 3379 * 3380 * @returns The output stream with the state of @p __x inserted or in 3381 * an error state. 3382 */ 3383 template<typename _CharT, typename _Traits> 3384 std::basic_ostream<_CharT, _Traits>& 3385 operator<<(std::basic_ostream<_CharT, _Traits>&, 3386 const std::bernoulli_distribution&); 3387 3388 /** 3389 * @brief Extracts a %bernoulli_distribution random number distribution 3390 * @p __x from the input stream @p __is. 3391 * 3392 * @param __is An input stream. 3393 * @param __x A %bernoulli_distribution random number generator engine. 3394 * 3395 * @returns The input stream with @p __x extracted or in an error state. 3396 */ 3397 template<typename _CharT, typename _Traits> 3398 std::basic_istream<_CharT, _Traits>& 3399 operator>>(std::basic_istream<_CharT, _Traits>& __is, 3400 std::bernoulli_distribution& __x) 3401 { 3402 double __p; 3403 __is >> __p; 3404 __x.param(bernoulli_distribution::param_type(__p)); 3405 return __is; 3406 } 3407 3408 3409 /** 3410 * @brief A discrete binomial random number distribution. 3411 * 3412 * The formula for the binomial probability density function is 3413 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$ 3414 * and @f$p@f$ are the parameters of the distribution. 3415 */ 3416 template<typename _IntType = int> 3417 class binomial_distribution 3418 { 3419 static_assert(std::is_integral<_IntType>::value, 3420 "template argument not an integral type"); 3421 3422 public: 3423 /** The type of the range of the distribution. */ 3424 typedef _IntType result_type; 3425 /** Parameter type. */ 3426 struct param_type 3427 { 3428 typedef binomial_distribution<_IntType> distribution_type; 3429 friend class binomial_distribution<_IntType>; 3430 3431 explicit 3432 param_type(_IntType __t = _IntType(1), double __p = 0.5) 3433 : _M_t(__t), _M_p(__p) 3434 { 3435 _GLIBCXX_DEBUG_ASSERT((_M_t >= _IntType(0)) 3436 && (_M_p >= 0.0) 3437 && (_M_p <= 1.0)); 3438 _M_initialize(); 3439 } 3440 3441 _IntType 3442 t() const 3443 { return _M_t; } 3444 3445 double 3446 p() const 3447 { return _M_p; } 3448 3449 friend bool 3450 operator==(const param_type& __p1, const param_type& __p2) 3451 { return __p1._M_t == __p2._M_t && __p1._M_p == __p2._M_p; } 3452 3453 private: 3454 void 3455 _M_initialize(); 3456 3457 _IntType _M_t; 3458 double _M_p; 3459 3460 double _M_q; 3461 #if _GLIBCXX_USE_C99_MATH_TR1 3462 double _M_d1, _M_d2, _M_s1, _M_s2, _M_c, 3463 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p; 3464 #endif 3465 bool _M_easy; 3466 }; 3467 3468 // constructors and member function 3469 explicit 3470 binomial_distribution(_IntType __t = _IntType(1), 3471 double __p = 0.5) 3472 : _M_param(__t, __p), _M_nd() 3473 { } 3474 3475 explicit 3476 binomial_distribution(const param_type& __p) 3477 : _M_param(__p), _M_nd() 3478 { } 3479 3480 /** 3481 * @brief Resets the distribution state. 3482 */ 3483 void 3484 reset() 3485 { _M_nd.reset(); } 3486 3487 /** 3488 * @brief Returns the distribution @p t parameter. 3489 */ 3490 _IntType 3491 t() const 3492 { return _M_param.t(); } 3493 3494 /** 3495 * @brief Returns the distribution @p p parameter. 3496 */ 3497 double 3498 p() const 3499 { return _M_param.p(); } 3500 3501 /** 3502 * @brief Returns the parameter set of the distribution. 3503 */ 3504 param_type 3505 param() const 3506 { return _M_param; } 3507 3508 /** 3509 * @brief Sets the parameter set of the distribution. 3510 * @param __param The new parameter set of the distribution. 3511 */ 3512 void 3513 param(const param_type& __param) 3514 { _M_param = __param; } 3515 3516 /** 3517 * @brief Returns the greatest lower bound value of the distribution. 3518 */ 3519 result_type 3520 min() const 3521 { return 0; } 3522 3523 /** 3524 * @brief Returns the least upper bound value of the distribution. 3525 */ 3526 result_type 3527 max() const 3528 { return _M_param.t(); } 3529 3530 /** 3531 * @brief Generating functions. 3532 */ 3533 template<typename _UniformRandomNumberGenerator> 3534 result_type 3535 operator()(_UniformRandomNumberGenerator& __urng) 3536 { return this->operator()(__urng, this->param()); } 3537 3538 template<typename _UniformRandomNumberGenerator> 3539 result_type 3540 operator()(_UniformRandomNumberGenerator& __urng, 3541 const param_type& __p); 3542 3543 /** 3544 * @brief Return true if two binomial distributions have 3545 * the same parameters and the sequences that would 3546 * be generated are equal. 3547 */ 3548 template<typename _IntType1> 3549 friend bool 3550 operator==(const std::binomial_distribution<_IntType1>& __d1, 3551 const std::binomial_distribution<_IntType1>& __d2) 3552 #ifdef _GLIBCXX_USE_C99_MATH_TR1 3553 { return __d1.param() == __d2.param() && __d1._M_nd == __d2._M_nd; } 3554 #else 3555 { return __d1.param() == __d2.param(); } 3556 #endif 3557 3558 /** 3559 * @brief Inserts a %binomial_distribution random number distribution 3560 * @p __x into the output stream @p __os. 3561 * 3562 * @param __os An output stream. 3563 * @param __x A %binomial_distribution random number distribution. 3564 * 3565 * @returns The output stream with the state of @p __x inserted or in 3566 * an error state. 3567 */ 3568 template<typename _IntType1, 3569 typename _CharT, typename _Traits> 3570 friend std::basic_ostream<_CharT, _Traits>& 3571 operator<<(std::basic_ostream<_CharT, _Traits>&, 3572 const std::binomial_distribution<_IntType1>&); 3573 3574 /** 3575 * @brief Extracts a %binomial_distribution random number distribution 3576 * @p __x from the input stream @p __is. 3577 * 3578 * @param __is An input stream. 3579 * @param __x A %binomial_distribution random number generator engine. 3580 * 3581 * @returns The input stream with @p __x extracted or in an error 3582 * state. 3583 */ 3584 template<typename _IntType1, 3585 typename _CharT, typename _Traits> 3586 friend std::basic_istream<_CharT, _Traits>& 3587 operator>>(std::basic_istream<_CharT, _Traits>&, 3588 std::binomial_distribution<_IntType1>&); 3589 3590 private: 3591 template<typename _UniformRandomNumberGenerator> 3592 result_type 3593 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t); 3594 3595 param_type _M_param; 3596 3597 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined. 3598 std::normal_distribution<double> _M_nd; 3599 }; 3600 3601 /** 3602 * @brief Return true if two binomial distributions are different. 3603 */ 3604 template<typename _IntType> 3605 inline bool 3606 operator!=(const std::binomial_distribution<_IntType>& __d1, 3607 const std::binomial_distribution<_IntType>& __d2) 3608 { return !(__d1 == __d2); } 3609 3610 3611 /** 3612 * @brief A discrete geometric random number distribution. 3613 * 3614 * The formula for the geometric probability density function is 3615 * @f$p(i|p) = (1 - p)p^{i-1}@f$ where @f$p@f$ is the parameter of the 3616 * distribution. 3617 */ 3618 template<typename _IntType = int> 3619 class geometric_distribution 3620 { 3621 static_assert(std::is_integral<_IntType>::value, 3622 "template argument not an integral type"); 3623 3624 public: 3625 /** The type of the range of the distribution. */ 3626 typedef _IntType result_type; 3627 /** Parameter type. */ 3628 struct param_type 3629 { 3630 typedef geometric_distribution<_IntType> distribution_type; 3631 friend class geometric_distribution<_IntType>; 3632 3633 explicit 3634 param_type(double __p = 0.5) 3635 : _M_p(__p) 3636 { 3637 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) 3638 && (_M_p <= 1.0)); 3639 _M_initialize(); 3640 } 3641 3642 double 3643 p() const 3644 { return _M_p; } 3645 3646 friend bool 3647 operator==(const param_type& __p1, const param_type& __p2) 3648 { return __p1._M_p == __p2._M_p; } 3649 3650 private: 3651 void 3652 _M_initialize() 3653 { _M_log_p = std::log(_M_p); } 3654 3655 double _M_p; 3656 3657 double _M_log_p; 3658 }; 3659 3660 // constructors and member function 3661 explicit 3662 geometric_distribution(double __p = 0.5) 3663 : _M_param(__p) 3664 { } 3665 3666 explicit 3667 geometric_distribution(const param_type& __p) 3668 : _M_param(__p) 3669 { } 3670 3671 /** 3672 * @brief Resets the distribution state. 3673 * 3674 * Does nothing for the geometric distribution. 3675 */ 3676 void 3677 reset() { } 3678 3679 /** 3680 * @brief Returns the distribution parameter @p p. 3681 */ 3682 double 3683 p() const 3684 { return _M_param.p(); } 3685 3686 /** 3687 * @brief Returns the parameter set of the distribution. 3688 */ 3689 param_type 3690 param() const 3691 { return _M_param; } 3692 3693 /** 3694 * @brief Sets the parameter set of the distribution. 3695 * @param __param The new parameter set of the distribution. 3696 */ 3697 void 3698 param(const param_type& __param) 3699 { _M_param = __param; } 3700 3701 /** 3702 * @brief Returns the greatest lower bound value of the distribution. 3703 */ 3704 result_type 3705 min() const 3706 { return 0; } 3707 3708 /** 3709 * @brief Returns the least upper bound value of the distribution. 3710 */ 3711 result_type 3712 max() const 3713 { return std::numeric_limits<result_type>::max(); } 3714 3715 /** 3716 * @brief Generating functions. 3717 */ 3718 template<typename _UniformRandomNumberGenerator> 3719 result_type 3720 operator()(_UniformRandomNumberGenerator& __urng) 3721 { return this->operator()(__urng, this->param()); } 3722 3723 template<typename _UniformRandomNumberGenerator> 3724 result_type 3725 operator()(_UniformRandomNumberGenerator& __urng, 3726 const param_type& __p); 3727 3728 private: 3729 param_type _M_param; 3730 }; 3731 3732 /** 3733 * @brief Return true if two geometric distributions have 3734 * the same parameters. 3735 */ 3736 template<typename _IntType> 3737 inline bool 3738 operator==(const std::geometric_distribution<_IntType>& __d1, 3739 const std::geometric_distribution<_IntType>& __d2) 3740 { return __d1.param() == __d2.param(); } 3741 3742 /** 3743 * @brief Return true if two geometric distributions have 3744 * different parameters. 3745 */ 3746 template<typename _IntType> 3747 inline bool 3748 operator!=(const std::geometric_distribution<_IntType>& __d1, 3749 const std::geometric_distribution<_IntType>& __d2) 3750 { return !(__d1 == __d2); } 3751 3752 /** 3753 * @brief Inserts a %geometric_distribution random number distribution 3754 * @p __x into the output stream @p __os. 3755 * 3756 * @param __os An output stream. 3757 * @param __x A %geometric_distribution random number distribution. 3758 * 3759 * @returns The output stream with the state of @p __x inserted or in 3760 * an error state. 3761 */ 3762 template<typename _IntType, 3763 typename _CharT, typename _Traits> 3764 std::basic_ostream<_CharT, _Traits>& 3765 operator<<(std::basic_ostream<_CharT, _Traits>&, 3766 const std::geometric_distribution<_IntType>&); 3767 3768 /** 3769 * @brief Extracts a %geometric_distribution random number distribution 3770 * @p __x from the input stream @p __is. 3771 * 3772 * @param __is An input stream. 3773 * @param __x A %geometric_distribution random number generator engine. 3774 * 3775 * @returns The input stream with @p __x extracted or in an error state. 3776 */ 3777 template<typename _IntType, 3778 typename _CharT, typename _Traits> 3779 std::basic_istream<_CharT, _Traits>& 3780 operator>>(std::basic_istream<_CharT, _Traits>&, 3781 std::geometric_distribution<_IntType>&); 3782 3783 3784 /** 3785 * @brief A negative_binomial_distribution random number distribution. 3786 * 3787 * The formula for the negative binomial probability mass function is 3788 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$ 3789 * and @f$p@f$ are the parameters of the distribution. 3790 */ 3791 template<typename _IntType = int> 3792 class negative_binomial_distribution 3793 { 3794 static_assert(std::is_integral<_IntType>::value, 3795 "template argument not an integral type"); 3796 3797 public: 3798 /** The type of the range of the distribution. */ 3799 typedef _IntType result_type; 3800 /** Parameter type. */ 3801 struct param_type 3802 { 3803 typedef negative_binomial_distribution<_IntType> distribution_type; 3804 3805 explicit 3806 param_type(_IntType __k = 1, double __p = 0.5) 3807 : _M_k(__k), _M_p(__p) 3808 { } 3809 3810 _IntType 3811 k() const 3812 { return _M_k; } 3813 3814 double 3815 p() const 3816 { return _M_p; } 3817 3818 friend bool 3819 operator==(const param_type& __p1, const param_type& __p2) 3820 { return __p1._M_k == __p2._M_k && __p1._M_p == __p2._M_p; } 3821 3822 private: 3823 _IntType _M_k; 3824 double _M_p; 3825 }; 3826 3827 explicit 3828 negative_binomial_distribution(_IntType __k = 1, double __p = 0.5) 3829 : _M_param(__k, __p), _M_gd(__k, __p / (1.0 - __p)) 3830 { } 3831 3832 explicit 3833 negative_binomial_distribution(const param_type& __p) 3834 : _M_param(__p), _M_gd(__p.k(), __p.p() / (1.0 - __p.p())) 3835 { } 3836 3837 /** 3838 * @brief Resets the distribution state. 3839 */ 3840 void 3841 reset() 3842 { _M_gd.reset(); } 3843 3844 /** 3845 * @brief Return the @f$k@f$ parameter of the distribution. 3846 */ 3847 _IntType 3848 k() const 3849 { return _M_param.k(); } 3850 3851 /** 3852 * @brief Return the @f$p@f$ parameter of the distribution. 3853 */ 3854 double 3855 p() const 3856 { return _M_param.p(); } 3857 3858 /** 3859 * @brief Returns the parameter set of the distribution. 3860 */ 3861 param_type 3862 param() const 3863 { return _M_param; } 3864 3865 /** 3866 * @brief Sets the parameter set of the distribution. 3867 * @param __param The new parameter set of the distribution. 3868 */ 3869 void 3870 param(const param_type& __param) 3871 { _M_param = __param; } 3872 3873 /** 3874 * @brief Returns the greatest lower bound value of the distribution. 3875 */ 3876 result_type 3877 min() const 3878 { return result_type(0); } 3879 3880 /** 3881 * @brief Returns the least upper bound value of the distribution. 3882 */ 3883 result_type 3884 max() const 3885 { return std::numeric_limits<result_type>::max(); } 3886 3887 /** 3888 * @brief Generating functions. 3889 */ 3890 template<typename _UniformRandomNumberGenerator> 3891 result_type 3892 operator()(_UniformRandomNumberGenerator& __urng); 3893 3894 template<typename _UniformRandomNumberGenerator> 3895 result_type 3896 operator()(_UniformRandomNumberGenerator& __urng, 3897 const param_type& __p); 3898 3899 /** 3900 * @brief Return true if two negative binomial distributions have 3901 * the same parameters and the sequences that would be 3902 * generated are equal. 3903 */ 3904 template<typename _IntType1> 3905 friend bool 3906 operator==(const std::negative_binomial_distribution<_IntType1>& __d1, 3907 const std::negative_binomial_distribution<_IntType1>& __d2) 3908 { return __d1.param() == __d2.param() && __d1._M_gd == __d2._M_gd; } 3909 3910 /** 3911 * @brief Inserts a %negative_binomial_distribution random 3912 * number distribution @p __x into the output stream @p __os. 3913 * 3914 * @param __os An output stream. 3915 * @param __x A %negative_binomial_distribution random number 3916 * distribution. 3917 * 3918 * @returns The output stream with the state of @p __x inserted or in 3919 * an error state. 3920 */ 3921 template<typename _IntType1, typename _CharT, typename _Traits> 3922 friend std::basic_ostream<_CharT, _Traits>& 3923 operator<<(std::basic_ostream<_CharT, _Traits>&, 3924 const std::negative_binomial_distribution<_IntType1>&); 3925 3926 /** 3927 * @brief Extracts a %negative_binomial_distribution random number 3928 * distribution @p __x from the input stream @p __is. 3929 * 3930 * @param __is An input stream. 3931 * @param __x A %negative_binomial_distribution random number 3932 * generator engine. 3933 * 3934 * @returns The input stream with @p __x extracted or in an error state. 3935 */ 3936 template<typename _IntType1, typename _CharT, typename _Traits> 3937 friend std::basic_istream<_CharT, _Traits>& 3938 operator>>(std::basic_istream<_CharT, _Traits>&, 3939 std::negative_binomial_distribution<_IntType1>&); 3940 3941 private: 3942 param_type _M_param; 3943 3944 std::gamma_distribution<double> _M_gd; 3945 }; 3946 3947 /** 3948 * @brief Return true if two negative binomial distributions are different. 3949 */ 3950 template<typename _IntType> 3951 inline bool 3952 operator!=(const std::negative_binomial_distribution<_IntType>& __d1, 3953 const std::negative_binomial_distribution<_IntType>& __d2) 3954 { return !(__d1 == __d2); } 3955 3956 3957 /* @} */ // group random_distributions_bernoulli 3958 3959 /** 3960 * @addtogroup random_distributions_poisson Poisson 3961 * @ingroup random_distributions 3962 * @{ 3963 */ 3964 3965 /** 3966 * @brief A discrete Poisson random number distribution. 3967 * 3968 * The formula for the Poisson probability density function is 3969 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the 3970 * parameter of the distribution. 3971 */ 3972 template<typename _IntType = int> 3973 class poisson_distribution 3974 { 3975 static_assert(std::is_integral<_IntType>::value, 3976 "template argument not an integral type"); 3977 3978 public: 3979 /** The type of the range of the distribution. */ 3980 typedef _IntType result_type; 3981 /** Parameter type. */ 3982 struct param_type 3983 { 3984 typedef poisson_distribution<_IntType> distribution_type; 3985 friend class poisson_distribution<_IntType>; 3986 3987 explicit 3988 param_type(double __mean = 1.0) 3989 : _M_mean(__mean) 3990 { 3991 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0); 3992 _M_initialize(); 3993 } 3994 3995 double 3996 mean() const 3997 { return _M_mean; } 3998 3999 friend bool 4000 operator==(const param_type& __p1, const param_type& __p2) 4001 { return __p1._M_mean == __p2._M_mean; } 4002 4003 private: 4004 // Hosts either log(mean) or the threshold of the simple method. 4005 void 4006 _M_initialize(); 4007 4008 double _M_mean; 4009 4010 double _M_lm_thr; 4011 #if _GLIBCXX_USE_C99_MATH_TR1 4012 double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb; 4013 #endif 4014 }; 4015 4016 // constructors and member function 4017 explicit 4018 poisson_distribution(double __mean = 1.0) 4019 : _M_param(__mean), _M_nd() 4020 { } 4021 4022 explicit 4023 poisson_distribution(const param_type& __p) 4024 : _M_param(__p), _M_nd() 4025 { } 4026 4027 /** 4028 * @brief Resets the distribution state. 4029 */ 4030 void 4031 reset() 4032 { _M_nd.reset(); } 4033 4034 /** 4035 * @brief Returns the distribution parameter @p mean. 4036 */ 4037 double 4038 mean() const 4039 { return _M_param.mean(); } 4040 4041 /** 4042 * @brief Returns the parameter set of the distribution. 4043 */ 4044 param_type 4045 param() const 4046 { return _M_param; } 4047 4048 /** 4049 * @brief Sets the parameter set of the distribution. 4050 * @param __param The new parameter set of the distribution. 4051 */ 4052 void 4053 param(const param_type& __param) 4054 { _M_param = __param; } 4055 4056 /** 4057 * @brief Returns the greatest lower bound value of the distribution. 4058 */ 4059 result_type 4060 min() const 4061 { return 0; } 4062 4063 /** 4064 * @brief Returns the least upper bound value of the distribution. 4065 */ 4066 result_type 4067 max() const 4068 { return std::numeric_limits<result_type>::max(); } 4069 4070 /** 4071 * @brief Generating functions. 4072 */ 4073 template<typename _UniformRandomNumberGenerator> 4074 result_type 4075 operator()(_UniformRandomNumberGenerator& __urng) 4076 { return this->operator()(__urng, this->param()); } 4077 4078 template<typename _UniformRandomNumberGenerator> 4079 result_type 4080 operator()(_UniformRandomNumberGenerator& __urng, 4081 const param_type& __p); 4082 4083 /** 4084 * @brief Return true if two Poisson distributions have the same 4085 * parameters and the sequences that would be generated 4086 * are equal. 4087 */ 4088 template<typename _IntType1> 4089 friend bool 4090 operator==(const std::poisson_distribution<_IntType1>& __d1, 4091 const std::poisson_distribution<_IntType1>& __d2) 4092 #ifdef _GLIBCXX_USE_C99_MATH_TR1 4093 { return __d1.param() == __d2.param() && __d1._M_nd == __d2._M_nd; } 4094 #else 4095 { return __d1.param() == __d2.param(); } 4096 #endif 4097 4098 /** 4099 * @brief Inserts a %poisson_distribution random number distribution 4100 * @p __x into the output stream @p __os. 4101 * 4102 * @param __os An output stream. 4103 * @param __x A %poisson_distribution random number distribution. 4104 * 4105 * @returns The output stream with the state of @p __x inserted or in 4106 * an error state. 4107 */ 4108 template<typename _IntType1, typename _CharT, typename _Traits> 4109 friend std::basic_ostream<_CharT, _Traits>& 4110 operator<<(std::basic_ostream<_CharT, _Traits>&, 4111 const std::poisson_distribution<_IntType1>&); 4112 4113 /** 4114 * @brief Extracts a %poisson_distribution random number distribution 4115 * @p __x from the input stream @p __is. 4116 * 4117 * @param __is An input stream. 4118 * @param __x A %poisson_distribution random number generator engine. 4119 * 4120 * @returns The input stream with @p __x extracted or in an error 4121 * state. 4122 */ 4123 template<typename _IntType1, typename _CharT, typename _Traits> 4124 friend std::basic_istream<_CharT, _Traits>& 4125 operator>>(std::basic_istream<_CharT, _Traits>&, 4126 std::poisson_distribution<_IntType1>&); 4127 4128 private: 4129 param_type _M_param; 4130 4131 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined. 4132 std::normal_distribution<double> _M_nd; 4133 }; 4134 4135 /** 4136 * @brief Return true if two Poisson distributions are different. 4137 */ 4138 template<typename _IntType> 4139 inline bool 4140 operator!=(const std::poisson_distribution<_IntType>& __d1, 4141 const std::poisson_distribution<_IntType>& __d2) 4142 { return !(__d1 == __d2); } 4143 4144 4145 /** 4146 * @brief An exponential continuous distribution for random numbers. 4147 * 4148 * The formula for the exponential probability density function is 4149 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$. 4150 * 4151 * <table border=1 cellpadding=10 cellspacing=0> 4152 * <caption align=top>Distribution Statistics</caption> 4153 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr> 4154 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr> 4155 * <tr><td>Mode</td><td>@f$zero@f$</td></tr> 4156 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr> 4157 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr> 4158 * </table> 4159 */ 4160 template<typename _RealType = double> 4161 class exponential_distribution 4162 { 4163 static_assert(std::is_floating_point<_RealType>::value, 4164 "template argument not a floating point type"); 4165 4166 public: 4167 /** The type of the range of the distribution. */ 4168 typedef _RealType result_type; 4169 /** Parameter type. */ 4170 struct param_type 4171 { 4172 typedef exponential_distribution<_RealType> distribution_type; 4173 4174 explicit 4175 param_type(_RealType __lambda = _RealType(1)) 4176 : _M_lambda(__lambda) 4177 { 4178 _GLIBCXX_DEBUG_ASSERT(_M_lambda > _RealType(0)); 4179 } 4180 4181 _RealType 4182 lambda() const 4183 { return _M_lambda; } 4184 4185 friend bool 4186 operator==(const param_type& __p1, const param_type& __p2) 4187 { return __p1._M_lambda == __p2._M_lambda; } 4188 4189 private: 4190 _RealType _M_lambda; 4191 }; 4192 4193 public: 4194 /** 4195 * @brief Constructs an exponential distribution with inverse scale 4196 * parameter @f$\lambda@f$. 4197 */ 4198 explicit 4199 exponential_distribution(const result_type& __lambda = result_type(1)) 4200 : _M_param(__lambda) 4201 { } 4202 4203 explicit 4204 exponential_distribution(const param_type& __p) 4205 : _M_param(__p) 4206 { } 4207 4208 /** 4209 * @brief Resets the distribution state. 4210 * 4211 * Has no effect on exponential distributions. 4212 */ 4213 void 4214 reset() { } 4215 4216 /** 4217 * @brief Returns the inverse scale parameter of the distribution. 4218 */ 4219 _RealType 4220 lambda() const 4221 { return _M_param.lambda(); } 4222 4223 /** 4224 * @brief Returns the parameter set of the distribution. 4225 */ 4226 param_type 4227 param() const 4228 { return _M_param; } 4229 4230 /** 4231 * @brief Sets the parameter set of the distribution. 4232 * @param __param The new parameter set of the distribution. 4233 */ 4234 void 4235 param(const param_type& __param) 4236 { _M_param = __param; } 4237 4238 /** 4239 * @brief Returns the greatest lower bound value of the distribution. 4240 */ 4241 result_type 4242 min() const 4243 { return result_type(0); } 4244 4245 /** 4246 * @brief Returns the least upper bound value of the distribution. 4247 */ 4248 result_type 4249 max() const 4250 { return std::numeric_limits<result_type>::max(); } 4251 4252 /** 4253 * @brief Generating functions. 4254 */ 4255 template<typename _UniformRandomNumberGenerator> 4256 result_type 4257 operator()(_UniformRandomNumberGenerator& __urng) 4258 { return this->operator()(__urng, this->param()); } 4259 4260 template<typename _UniformRandomNumberGenerator> 4261 result_type 4262 operator()(_UniformRandomNumberGenerator& __urng, 4263 const param_type& __p) 4264 { 4265 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> 4266 __aurng(__urng); 4267 return -std::log(__aurng()) / __p.lambda(); 4268 } 4269 4270 private: 4271 param_type _M_param; 4272 }; 4273 4274 /** 4275 * @brief Return true if two exponential distributions have the same 4276 * parameters. 4277 */ 4278 template<typename _RealType> 4279 inline bool 4280 operator==(const std::exponential_distribution<_RealType>& __d1, 4281 const std::exponential_distribution<_RealType>& __d2) 4282 { return __d1.param() == __d2.param(); } 4283 4284 /** 4285 * @brief Return true if two exponential distributions have different 4286 * parameters. 4287 */ 4288 template<typename _RealType> 4289 inline bool 4290 operator!=(const std::exponential_distribution<_RealType>& __d1, 4291 const std::exponential_distribution<_RealType>& __d2) 4292 { return !(__d1 == __d2); } 4293 4294 /** 4295 * @brief Inserts a %exponential_distribution random number distribution 4296 * @p __x into the output stream @p __os. 4297 * 4298 * @param __os An output stream. 4299 * @param __x A %exponential_distribution random number distribution. 4300 * 4301 * @returns The output stream with the state of @p __x inserted or in 4302 * an error state. 4303 */ 4304 template<typename _RealType, typename _CharT, typename _Traits> 4305 std::basic_ostream<_CharT, _Traits>& 4306 operator<<(std::basic_ostream<_CharT, _Traits>&, 4307 const std::exponential_distribution<_RealType>&); 4308 4309 /** 4310 * @brief Extracts a %exponential_distribution random number distribution 4311 * @p __x from the input stream @p __is. 4312 * 4313 * @param __is An input stream. 4314 * @param __x A %exponential_distribution random number 4315 * generator engine. 4316 * 4317 * @returns The input stream with @p __x extracted or in an error state. 4318 */ 4319 template<typename _RealType, typename _CharT, typename _Traits> 4320 std::basic_istream<_CharT, _Traits>& 4321 operator>>(std::basic_istream<_CharT, _Traits>&, 4322 std::exponential_distribution<_RealType>&); 4323 4324 4325 /** 4326 * @brief A weibull_distribution random number distribution. 4327 * 4328 * The formula for the normal probability density function is: 4329 * @f[ 4330 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1} 4331 * \exp{(-(\frac{x}{\beta})^\alpha)} 4332 * @f] 4333 */ 4334 template<typename _RealType = double> 4335 class weibull_distribution 4336 { 4337 static_assert(std::is_floating_point<_RealType>::value, 4338 "template argument not a floating point type"); 4339 4340 public: 4341 /** The type of the range of the distribution. */ 4342 typedef _RealType result_type; 4343 /** Parameter type. */ 4344 struct param_type 4345 { 4346 typedef weibull_distribution<_RealType> distribution_type; 4347 4348 explicit 4349 param_type(_RealType __a = _RealType(1), 4350 _RealType __b = _RealType(1)) 4351 : _M_a(__a), _M_b(__b) 4352 { } 4353 4354 _RealType 4355 a() const 4356 { return _M_a; } 4357 4358 _RealType 4359 b() const 4360 { return _M_b; } 4361 4362 friend bool 4363 operator==(const param_type& __p1, const param_type& __p2) 4364 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } 4365 4366 private: 4367 _RealType _M_a; 4368 _RealType _M_b; 4369 }; 4370 4371 explicit 4372 weibull_distribution(_RealType __a = _RealType(1), 4373 _RealType __b = _RealType(1)) 4374 : _M_param(__a, __b) 4375 { } 4376 4377 explicit 4378 weibull_distribution(const param_type& __p) 4379 : _M_param(__p) 4380 { } 4381 4382 /** 4383 * @brief Resets the distribution state. 4384 */ 4385 void 4386 reset() 4387 { } 4388 4389 /** 4390 * @brief Return the @f$a@f$ parameter of the distribution. 4391 */ 4392 _RealType 4393 a() const 4394 { return _M_param.a(); } 4395 4396 /** 4397 * @brief Return the @f$b@f$ parameter of the distribution. 4398 */ 4399 _RealType 4400 b() const 4401 { return _M_param.b(); } 4402 4403 /** 4404 * @brief Returns the parameter set of the distribution. 4405 */ 4406 param_type 4407 param() const 4408 { return _M_param; } 4409 4410 /** 4411 * @brief Sets the parameter set of the distribution. 4412 * @param __param The new parameter set of the distribution. 4413 */ 4414 void 4415 param(const param_type& __param) 4416 { _M_param = __param; } 4417 4418 /** 4419 * @brief Returns the greatest lower bound value of the distribution. 4420 */ 4421 result_type 4422 min() const 4423 { return result_type(0); } 4424 4425 /** 4426 * @brief Returns the least upper bound value of the distribution. 4427 */ 4428 result_type 4429 max() const 4430 { return std::numeric_limits<result_type>::max(); } 4431 4432 /** 4433 * @brief Generating functions. 4434 */ 4435 template<typename _UniformRandomNumberGenerator> 4436 result_type 4437 operator()(_UniformRandomNumberGenerator& __urng) 4438 { return this->operator()(__urng, this->param()); } 4439 4440 template<typename _UniformRandomNumberGenerator> 4441 result_type 4442 operator()(_UniformRandomNumberGenerator& __urng, 4443 const param_type& __p); 4444 4445 private: 4446 param_type _M_param; 4447 }; 4448 4449 /** 4450 * @brief Return true if two Weibull distributions have the same 4451 * parameters. 4452 */ 4453 template<typename _RealType> 4454 inline bool 4455 operator==(const std::weibull_distribution<_RealType>& __d1, 4456 const std::weibull_distribution<_RealType>& __d2) 4457 { return __d1.param() == __d2.param(); } 4458 4459 /** 4460 * @brief Return true if two Weibull distributions have different 4461 * parameters. 4462 */ 4463 template<typename _RealType> 4464 inline bool 4465 operator!=(const std::weibull_distribution<_RealType>& __d1, 4466 const std::weibull_distribution<_RealType>& __d2) 4467 { return !(__d1 == __d2); } 4468 4469 /** 4470 * @brief Inserts a %weibull_distribution random number distribution 4471 * @p __x into the output stream @p __os. 4472 * 4473 * @param __os An output stream. 4474 * @param __x A %weibull_distribution random number distribution. 4475 * 4476 * @returns The output stream with the state of @p __x inserted or in 4477 * an error state. 4478 */ 4479 template<typename _RealType, typename _CharT, typename _Traits> 4480 std::basic_ostream<_CharT, _Traits>& 4481 operator<<(std::basic_ostream<_CharT, _Traits>&, 4482 const std::weibull_distribution<_RealType>&); 4483 4484 /** 4485 * @brief Extracts a %weibull_distribution random number distribution 4486 * @p __x from the input stream @p __is. 4487 * 4488 * @param __is An input stream. 4489 * @param __x A %weibull_distribution random number 4490 * generator engine. 4491 * 4492 * @returns The input stream with @p __x extracted or in an error state. 4493 */ 4494 template<typename _RealType, typename _CharT, typename _Traits> 4495 std::basic_istream<_CharT, _Traits>& 4496 operator>>(std::basic_istream<_CharT, _Traits>&, 4497 std::weibull_distribution<_RealType>&); 4498 4499 4500 /** 4501 * @brief A extreme_value_distribution random number distribution. 4502 * 4503 * The formula for the normal probability mass function is 4504 * @f[ 4505 * p(x|a,b) = \frac{1}{b} 4506 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) 4507 * @f] 4508 */ 4509 template<typename _RealType = double> 4510 class extreme_value_distribution 4511 { 4512 static_assert(std::is_floating_point<_RealType>::value, 4513 "template argument not a floating point type"); 4514 4515 public: 4516 /** The type of the range of the distribution. */ 4517 typedef _RealType result_type; 4518 /** Parameter type. */ 4519 struct param_type 4520 { 4521 typedef extreme_value_distribution<_RealType> distribution_type; 4522 4523 explicit 4524 param_type(_RealType __a = _RealType(0), 4525 _RealType __b = _RealType(1)) 4526 : _M_a(__a), _M_b(__b) 4527 { } 4528 4529 _RealType 4530 a() const 4531 { return _M_a; } 4532 4533 _RealType 4534 b() const 4535 { return _M_b; } 4536 4537 friend bool 4538 operator==(const param_type& __p1, const param_type& __p2) 4539 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } 4540 4541 private: 4542 _RealType _M_a; 4543 _RealType _M_b; 4544 }; 4545 4546 explicit 4547 extreme_value_distribution(_RealType __a = _RealType(0), 4548 _RealType __b = _RealType(1)) 4549 : _M_param(__a, __b) 4550 { } 4551 4552 explicit 4553 extreme_value_distribution(const param_type& __p) 4554 : _M_param(__p) 4555 { } 4556 4557 /** 4558 * @brief Resets the distribution state. 4559 */ 4560 void 4561 reset() 4562 { } 4563 4564 /** 4565 * @brief Return the @f$a@f$ parameter of the distribution. 4566 */ 4567 _RealType 4568 a() const 4569 { return _M_param.a(); } 4570 4571 /** 4572 * @brief Return the @f$b@f$ parameter of the distribution. 4573 */ 4574 _RealType 4575 b() const 4576 { return _M_param.b(); } 4577 4578 /** 4579 * @brief Returns the parameter set of the distribution. 4580 */ 4581 param_type 4582 param() const 4583 { return _M_param; } 4584 4585 /** 4586 * @brief Sets the parameter set of the distribution. 4587 * @param __param The new parameter set of the distribution. 4588 */ 4589 void 4590 param(const param_type& __param) 4591 { _M_param = __param; } 4592 4593 /** 4594 * @brief Returns the greatest lower bound value of the distribution. 4595 */ 4596 result_type 4597 min() const 4598 { return std::numeric_limits<result_type>::min(); } 4599 4600 /** 4601 * @brief Returns the least upper bound value of the distribution. 4602 */ 4603 result_type 4604 max() const 4605 { return std::numeric_limits<result_type>::max(); } 4606 4607 /** 4608 * @brief Generating functions. 4609 */ 4610 template<typename _UniformRandomNumberGenerator> 4611 result_type 4612 operator()(_UniformRandomNumberGenerator& __urng) 4613 { return this->operator()(__urng, this->param()); } 4614 4615 template<typename _UniformRandomNumberGenerator> 4616 result_type 4617 operator()(_UniformRandomNumberGenerator& __urng, 4618 const param_type& __p); 4619 4620 private: 4621 param_type _M_param; 4622 }; 4623 4624 /** 4625 * @brief Return true if two extreme value distributions have the same 4626 * parameters. 4627 */ 4628 template<typename _RealType> 4629 inline bool 4630 operator==(const std::extreme_value_distribution<_RealType>& __d1, 4631 const std::extreme_value_distribution<_RealType>& __d2) 4632 { return __d1.param() == __d2.param(); } 4633 4634 /** 4635 * @brief Return true if two extreme value distributions have different 4636 * parameters. 4637 */ 4638 template<typename _RealType> 4639 inline bool 4640 operator!=(const std::extreme_value_distribution<_RealType>& __d1, 4641 const std::extreme_value_distribution<_RealType>& __d2) 4642 { return !(__d1 == __d2); } 4643 4644 /** 4645 * @brief Inserts a %extreme_value_distribution random number distribution 4646 * @p __x into the output stream @p __os. 4647 * 4648 * @param __os An output stream. 4649 * @param __x A %extreme_value_distribution random number distribution. 4650 * 4651 * @returns The output stream with the state of @p __x inserted or in 4652 * an error state. 4653 */ 4654 template<typename _RealType, typename _CharT, typename _Traits> 4655 std::basic_ostream<_CharT, _Traits>& 4656 operator<<(std::basic_ostream<_CharT, _Traits>&, 4657 const std::extreme_value_distribution<_RealType>&); 4658 4659 /** 4660 * @brief Extracts a %extreme_value_distribution random number 4661 * distribution @p __x from the input stream @p __is. 4662 * 4663 * @param __is An input stream. 4664 * @param __x A %extreme_value_distribution random number 4665 * generator engine. 4666 * 4667 * @returns The input stream with @p __x extracted or in an error state. 4668 */ 4669 template<typename _RealType, typename _CharT, typename _Traits> 4670 std::basic_istream<_CharT, _Traits>& 4671 operator>>(std::basic_istream<_CharT, _Traits>&, 4672 std::extreme_value_distribution<_RealType>&); 4673 4674 4675 /** 4676 * @brief A discrete_distribution random number distribution. 4677 * 4678 * The formula for the discrete probability mass function is 4679 * 4680 */ 4681 template<typename _IntType = int> 4682 class discrete_distribution 4683 { 4684 static_assert(std::is_integral<_IntType>::value, 4685 "template argument not an integral type"); 4686 4687 public: 4688 /** The type of the range of the distribution. */ 4689 typedef _IntType result_type; 4690 /** Parameter type. */ 4691 struct param_type 4692 { 4693 typedef discrete_distribution<_IntType> distribution_type; 4694 friend class discrete_distribution<_IntType>; 4695 4696 param_type() 4697 : _M_prob(), _M_cp() 4698 { _M_initialize(); } 4699 4700 template<typename _InputIterator> 4701 param_type(_InputIterator __wbegin, 4702 _InputIterator __wend) 4703 : _M_prob(__wbegin, __wend), _M_cp() 4704 { _M_initialize(); } 4705 4706 param_type(initializer_list<double> __wil) 4707 : _M_prob(__wil.begin(), __wil.end()), _M_cp() 4708 { _M_initialize(); } 4709 4710 template<typename _Func> 4711 param_type(size_t __nw, double __xmin, double __xmax, 4712 _Func __fw); 4713 4714 std::vector<double> 4715 probabilities() const 4716 { return _M_prob; } 4717 4718 friend bool 4719 operator==(const param_type& __p1, const param_type& __p2) 4720 { return __p1._M_prob == __p2._M_prob; } 4721 4722 private: 4723 void 4724 _M_initialize(); 4725 4726 std::vector<double> _M_prob; 4727 std::vector<double> _M_cp; 4728 }; 4729 4730 discrete_distribution() 4731 : _M_param() 4732 { } 4733 4734 template<typename _InputIterator> 4735 discrete_distribution(_InputIterator __wbegin, 4736 _InputIterator __wend) 4737 : _M_param(__wbegin, __wend) 4738 { } 4739 4740 discrete_distribution(initializer_list<double> __wl) 4741 : _M_param(__wl) 4742 { } 4743 4744 template<typename _Func> 4745 discrete_distribution(size_t __nw, double __xmin, double __xmax, 4746 _Func __fw) 4747 : _M_param(__nw, __xmin, __xmax, __fw) 4748 { } 4749 4750 explicit 4751 discrete_distribution(const param_type& __p) 4752 : _M_param(__p) 4753 { } 4754 4755 /** 4756 * @brief Resets the distribution state. 4757 */ 4758 void 4759 reset() 4760 { } 4761 4762 /** 4763 * @brief Returns the probabilities of the distribution. 4764 */ 4765 std::vector<double> 4766 probabilities() const 4767 { return _M_param.probabilities(); } 4768 4769 /** 4770 * @brief Returns the parameter set of the distribution. 4771 */ 4772 param_type 4773 param() const 4774 { return _M_param; } 4775 4776 /** 4777 * @brief Sets the parameter set of the distribution. 4778 * @param __param The new parameter set of the distribution. 4779 */ 4780 void 4781 param(const param_type& __param) 4782 { _M_param = __param; } 4783 4784 /** 4785 * @brief Returns the greatest lower bound value of the distribution. 4786 */ 4787 result_type 4788 min() const 4789 { return result_type(0); } 4790 4791 /** 4792 * @brief Returns the least upper bound value of the distribution. 4793 */ 4794 result_type 4795 max() const 4796 { return this->_M_param._M_prob.size() - 1; } 4797 4798 /** 4799 * @brief Generating functions. 4800 */ 4801 template<typename _UniformRandomNumberGenerator> 4802 result_type 4803 operator()(_UniformRandomNumberGenerator& __urng) 4804 { return this->operator()(__urng, this->param()); } 4805 4806 template<typename _UniformRandomNumberGenerator> 4807 result_type 4808 operator()(_UniformRandomNumberGenerator& __urng, 4809 const param_type& __p); 4810 4811 /** 4812 * @brief Inserts a %discrete_distribution random number distribution 4813 * @p __x into the output stream @p __os. 4814 * 4815 * @param __os An output stream. 4816 * @param __x A %discrete_distribution random number distribution. 4817 * 4818 * @returns The output stream with the state of @p __x inserted or in 4819 * an error state. 4820 */ 4821 template<typename _IntType1, typename _CharT, typename _Traits> 4822 friend std::basic_ostream<_CharT, _Traits>& 4823 operator<<(std::basic_ostream<_CharT, _Traits>&, 4824 const std::discrete_distribution<_IntType1>&); 4825 4826 /** 4827 * @brief Extracts a %discrete_distribution random number distribution 4828 * @p __x from the input stream @p __is. 4829 * 4830 * @param __is An input stream. 4831 * @param __x A %discrete_distribution random number 4832 * generator engine. 4833 * 4834 * @returns The input stream with @p __x extracted or in an error 4835 * state. 4836 */ 4837 template<typename _IntType1, typename _CharT, typename _Traits> 4838 friend std::basic_istream<_CharT, _Traits>& 4839 operator>>(std::basic_istream<_CharT, _Traits>&, 4840 std::discrete_distribution<_IntType1>&); 4841 4842 private: 4843 param_type _M_param; 4844 }; 4845 4846 /** 4847 * @brief Return true if two discrete distributions have the same 4848 * parameters. 4849 */ 4850 template<typename _IntType> 4851 inline bool 4852 operator==(const std::discrete_distribution<_IntType>& __d1, 4853 const std::discrete_distribution<_IntType>& __d2) 4854 { return __d1.param() == __d2.param(); } 4855 4856 /** 4857 * @brief Return true if two discrete distributions have different 4858 * parameters. 4859 */ 4860 template<typename _IntType> 4861 inline bool 4862 operator!=(const std::discrete_distribution<_IntType>& __d1, 4863 const std::discrete_distribution<_IntType>& __d2) 4864 { return !(__d1 == __d2); } 4865 4866 4867 /** 4868 * @brief A piecewise_constant_distribution random number distribution. 4869 * 4870 * The formula for the piecewise constant probability mass function is 4871 * 4872 */ 4873 template<typename _RealType = double> 4874 class piecewise_constant_distribution 4875 { 4876 static_assert(std::is_floating_point<_RealType>::value, 4877 "template argument not a floating point type"); 4878 4879 public: 4880 /** The type of the range of the distribution. */ 4881 typedef _RealType result_type; 4882 /** Parameter type. */ 4883 struct param_type 4884 { 4885 typedef piecewise_constant_distribution<_RealType> distribution_type; 4886 friend class piecewise_constant_distribution<_RealType>; 4887 4888 param_type() 4889 : _M_int(), _M_den(), _M_cp() 4890 { _M_initialize(); } 4891 4892 template<typename _InputIteratorB, typename _InputIteratorW> 4893 param_type(_InputIteratorB __bfirst, 4894 _InputIteratorB __bend, 4895 _InputIteratorW __wbegin); 4896 4897 template<typename _Func> 4898 param_type(initializer_list<_RealType> __bi, _Func __fw); 4899 4900 template<typename _Func> 4901 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, 4902 _Func __fw); 4903 4904 std::vector<_RealType> 4905 intervals() const 4906 { return _M_int; } 4907 4908 std::vector<double> 4909 densities() const 4910 { return _M_den; } 4911 4912 friend bool 4913 operator==(const param_type& __p1, const param_type& __p2) 4914 { return __p1._M_int == __p2._M_int && __p1._M_den == __p2._M_den; } 4915 4916 private: 4917 void 4918 _M_initialize(); 4919 4920 std::vector<_RealType> _M_int; 4921 std::vector<double> _M_den; 4922 std::vector<double> _M_cp; 4923 }; 4924 4925 explicit 4926 piecewise_constant_distribution() 4927 : _M_param() 4928 { } 4929 4930 template<typename _InputIteratorB, typename _InputIteratorW> 4931 piecewise_constant_distribution(_InputIteratorB __bfirst, 4932 _InputIteratorB __bend, 4933 _InputIteratorW __wbegin) 4934 : _M_param(__bfirst, __bend, __wbegin) 4935 { } 4936 4937 template<typename _Func> 4938 piecewise_constant_distribution(initializer_list<_RealType> __bl, 4939 _Func __fw) 4940 : _M_param(__bl, __fw) 4941 { } 4942 4943 template<typename _Func> 4944 piecewise_constant_distribution(size_t __nw, 4945 _RealType __xmin, _RealType __xmax, 4946 _Func __fw) 4947 : _M_param(__nw, __xmin, __xmax, __fw) 4948 { } 4949 4950 explicit 4951 piecewise_constant_distribution(const param_type& __p) 4952 : _M_param(__p) 4953 { } 4954 4955 /** 4956 * @brief Resets the distribution state. 4957 */ 4958 void 4959 reset() 4960 { } 4961 4962 /** 4963 * @brief Returns a vector of the intervals. 4964 */ 4965 std::vector<_RealType> 4966 intervals() const 4967 { return _M_param.intervals(); } 4968 4969 /** 4970 * @brief Returns a vector of the probability densities. 4971 */ 4972 std::vector<double> 4973 densities() const 4974 { return _M_param.densities(); } 4975 4976 /** 4977 * @brief Returns the parameter set of the distribution. 4978 */ 4979 param_type 4980 param() const 4981 { return _M_param; } 4982 4983 /** 4984 * @brief Sets the parameter set of the distribution. 4985 * @param __param The new parameter set of the distribution. 4986 */ 4987 void 4988 param(const param_type& __param) 4989 { _M_param = __param; } 4990 4991 /** 4992 * @brief Returns the greatest lower bound value of the distribution. 4993 */ 4994 result_type 4995 min() const 4996 { return this->_M_param._M_int.front(); } 4997 4998 /** 4999 * @brief Returns the least upper bound value of the distribution. 5000 */ 5001 result_type 5002 max() const 5003 { return this->_M_param._M_int.back(); } 5004 5005 /** 5006 * @brief Generating functions. 5007 */ 5008 template<typename _UniformRandomNumberGenerator> 5009 result_type 5010 operator()(_UniformRandomNumberGenerator& __urng) 5011 { return this->operator()(__urng, this->param()); } 5012 5013 template<typename _UniformRandomNumberGenerator> 5014 result_type 5015 operator()(_UniformRandomNumberGenerator& __urng, 5016 const param_type& __p); 5017 5018 /** 5019 * @brief Inserts a %piecewise_constan_distribution random 5020 * number distribution @p __x into the output stream @p __os. 5021 * 5022 * @param __os An output stream. 5023 * @param __x A %piecewise_constan_distribution random number 5024 * distribution. 5025 * 5026 * @returns The output stream with the state of @p __x inserted or in 5027 * an error state. 5028 */ 5029 template<typename _RealType1, typename _CharT, typename _Traits> 5030 friend std::basic_ostream<_CharT, _Traits>& 5031 operator<<(std::basic_ostream<_CharT, _Traits>&, 5032 const std::piecewise_constant_distribution<_RealType1>&); 5033 5034 /** 5035 * @brief Extracts a %piecewise_constan_distribution random 5036 * number distribution @p __x from the input stream @p __is. 5037 * 5038 * @param __is An input stream. 5039 * @param __x A %piecewise_constan_distribution random number 5040 * generator engine. 5041 * 5042 * @returns The input stream with @p __x extracted or in an error 5043 * state. 5044 */ 5045 template<typename _RealType1, typename _CharT, typename _Traits> 5046 friend std::basic_istream<_CharT, _Traits>& 5047 operator>>(std::basic_istream<_CharT, _Traits>&, 5048 std::piecewise_constant_distribution<_RealType1>&); 5049 5050 private: 5051 param_type _M_param; 5052 }; 5053 5054 /** 5055 * @brief Return true if two piecewise constant distributions have the 5056 * same parameters. 5057 */ 5058 template<typename _RealType> 5059 inline bool 5060 operator==(const std::piecewise_constant_distribution<_RealType>& __d1, 5061 const std::piecewise_constant_distribution<_RealType>& __d2) 5062 { return __d1.param() == __d2.param(); } 5063 5064 /** 5065 * @brief Return true if two piecewise constant distributions have 5066 * different parameters. 5067 */ 5068 template<typename _RealType> 5069 inline bool 5070 operator!=(const std::piecewise_constant_distribution<_RealType>& __d1, 5071 const std::piecewise_constant_distribution<_RealType>& __d2) 5072 { return !(__d1 == __d2); } 5073 5074 5075 /** 5076 * @brief A piecewise_linear_distribution random number distribution. 5077 * 5078 * The formula for the piecewise linear probability mass function is 5079 * 5080 */ 5081 template<typename _RealType = double> 5082 class piecewise_linear_distribution 5083 { 5084 static_assert(std::is_floating_point<_RealType>::value, 5085 "template argument not a floating point type"); 5086 5087 public: 5088 /** The type of the range of the distribution. */ 5089 typedef _RealType result_type; 5090 /** Parameter type. */ 5091 struct param_type 5092 { 5093 typedef piecewise_linear_distribution<_RealType> distribution_type; 5094 friend class piecewise_linear_distribution<_RealType>; 5095 5096 param_type() 5097 : _M_int(), _M_den(), _M_cp(), _M_m() 5098 { _M_initialize(); } 5099 5100 template<typename _InputIteratorB, typename _InputIteratorW> 5101 param_type(_InputIteratorB __bfirst, 5102 _InputIteratorB __bend, 5103 _InputIteratorW __wbegin); 5104 5105 template<typename _Func> 5106 param_type(initializer_list<_RealType> __bl, _Func __fw); 5107 5108 template<typename _Func> 5109 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, 5110 _Func __fw); 5111 5112 std::vector<_RealType> 5113 intervals() const 5114 { return _M_int; } 5115 5116 std::vector<double> 5117 densities() const 5118 { return _M_den; } 5119 5120 friend bool 5121 operator==(const param_type& __p1, const param_type& __p2) 5122 { return (__p1._M_int == __p2._M_int 5123 && __p1._M_den == __p2._M_den); } 5124 5125 private: 5126 void 5127 _M_initialize(); 5128 5129 std::vector<_RealType> _M_int; 5130 std::vector<double> _M_den; 5131 std::vector<double> _M_cp; 5132 std::vector<double> _M_m; 5133 }; 5134 5135 explicit 5136 piecewise_linear_distribution() 5137 : _M_param() 5138 { } 5139 5140 template<typename _InputIteratorB, typename _InputIteratorW> 5141 piecewise_linear_distribution(_InputIteratorB __bfirst, 5142 _InputIteratorB __bend, 5143 _InputIteratorW __wbegin) 5144 : _M_param(__bfirst, __bend, __wbegin) 5145 { } 5146 5147 template<typename _Func> 5148 piecewise_linear_distribution(initializer_list<_RealType> __bl, 5149 _Func __fw) 5150 : _M_param(__bl, __fw) 5151 { } 5152 5153 template<typename _Func> 5154 piecewise_linear_distribution(size_t __nw, 5155 _RealType __xmin, _RealType __xmax, 5156 _Func __fw) 5157 : _M_param(__nw, __xmin, __xmax, __fw) 5158 { } 5159 5160 explicit 5161 piecewise_linear_distribution(const param_type& __p) 5162 : _M_param(__p) 5163 { } 5164 5165 /** 5166 * Resets the distribution state. 5167 */ 5168 void 5169 reset() 5170 { } 5171 5172 /** 5173 * @brief Return the intervals of the distribution. 5174 */ 5175 std::vector<_RealType> 5176 intervals() const 5177 { return _M_param.intervals(); } 5178 5179 /** 5180 * @brief Return a vector of the probability densities of the 5181 * distribution. 5182 */ 5183 std::vector<double> 5184 densities() const 5185 { return _M_param.densities(); } 5186 5187 /** 5188 * @brief Returns the parameter set of the distribution. 5189 */ 5190 param_type 5191 param() const 5192 { return _M_param; } 5193 5194 /** 5195 * @brief Sets the parameter set of the distribution. 5196 * @param __param The new parameter set of the distribution. 5197 */ 5198 void 5199 param(const param_type& __param) 5200 { _M_param = __param; } 5201 5202 /** 5203 * @brief Returns the greatest lower bound value of the distribution. 5204 */ 5205 result_type 5206 min() const 5207 { return this->_M_param._M_int.front(); } 5208 5209 /** 5210 * @brief Returns the least upper bound value of the distribution. 5211 */ 5212 result_type 5213 max() const 5214 { return this->_M_param._M_int.back(); } 5215 5216 /** 5217 * @brief Generating functions. 5218 */ 5219 template<typename _UniformRandomNumberGenerator> 5220 result_type 5221 operator()(_UniformRandomNumberGenerator& __urng) 5222 { return this->operator()(__urng, this->param()); } 5223 5224 template<typename _UniformRandomNumberGenerator> 5225 result_type 5226 operator()(_UniformRandomNumberGenerator& __urng, 5227 const param_type& __p); 5228 5229 /** 5230 * @brief Inserts a %piecewise_linear_distribution random number 5231 * distribution @p __x into the output stream @p __os. 5232 * 5233 * @param __os An output stream. 5234 * @param __x A %piecewise_linear_distribution random number 5235 * distribution. 5236 * 5237 * @returns The output stream with the state of @p __x inserted or in 5238 * an error state. 5239 */ 5240 template<typename _RealType1, typename _CharT, typename _Traits> 5241 friend std::basic_ostream<_CharT, _Traits>& 5242 operator<<(std::basic_ostream<_CharT, _Traits>&, 5243 const std::piecewise_linear_distribution<_RealType1>&); 5244 5245 /** 5246 * @brief Extracts a %piecewise_linear_distribution random number 5247 * distribution @p __x from the input stream @p __is. 5248 * 5249 * @param __is An input stream. 5250 * @param __x A %piecewise_linear_distribution random number 5251 * generator engine. 5252 * 5253 * @returns The input stream with @p __x extracted or in an error 5254 * state. 5255 */ 5256 template<typename _RealType1, typename _CharT, typename _Traits> 5257 friend std::basic_istream<_CharT, _Traits>& 5258 operator>>(std::basic_istream<_CharT, _Traits>&, 5259 std::piecewise_linear_distribution<_RealType1>&); 5260 5261 private: 5262 param_type _M_param; 5263 }; 5264 5265 /** 5266 * @brief Return true if two piecewise linear distributions have the 5267 * same parameters. 5268 */ 5269 template<typename _RealType> 5270 inline bool 5271 operator==(const std::piecewise_linear_distribution<_RealType>& __d1, 5272 const std::piecewise_linear_distribution<_RealType>& __d2) 5273 { return __d1.param() == __d2.param(); } 5274 5275 /** 5276 * @brief Return true if two piecewise linear distributions have 5277 * different parameters. 5278 */ 5279 template<typename _RealType> 5280 inline bool 5281 operator!=(const std::piecewise_linear_distribution<_RealType>& __d1, 5282 const std::piecewise_linear_distribution<_RealType>& __d2) 5283 { return !(__d1 == __d2); } 5284 5285 5286 /* @} */ // group random_distributions_poisson 5287 5288 /* @} */ // group random_distributions 5289 5290 /** 5291 * @addtogroup random_utilities Random Number Utilities 5292 * @ingroup random 5293 * @{ 5294 */ 5295 5296 /** 5297 * @brief The seed_seq class generates sequences of seeds for random 5298 * number generators. 5299 */ 5300 class seed_seq 5301 { 5302 5303 public: 5304 /** The type of the seed vales. */ 5305 typedef uint_least32_t result_type; 5306 5307 /** Default constructor. */ 5308 seed_seq() 5309 : _M_v() 5310 { } 5311 5312 template<typename _IntType> 5313 seed_seq(std::initializer_list<_IntType> il); 5314 5315 template<typename _InputIterator> 5316 seed_seq(_InputIterator __begin, _InputIterator __end); 5317 5318 // generating functions 5319 template<typename _RandomAccessIterator> 5320 void 5321 generate(_RandomAccessIterator __begin, _RandomAccessIterator __end); 5322 5323 // property functions 5324 size_t size() const 5325 { return _M_v.size(); } 5326 5327 template<typename OutputIterator> 5328 void 5329 param(OutputIterator __dest) const 5330 { std::copy(_M_v.begin(), _M_v.end(), __dest); } 5331 5332 private: 5333 /// 5334 std::vector<result_type> _M_v; 5335 }; 5336 5337 /* @} */ // group random_utilities 5338 5339 /* @} */ // group random 5340 5341 } 5342 5343