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