1 // random number generation -*- C++ -*- 2 3 // Copyright (C) 2009-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, _M_param); } 1723 1724 template<typename _UniformRandomNumberGenerator> 1725 result_type 1726 operator()(_UniformRandomNumberGenerator& __urng, 1727 const param_type& __p); 1728 1729 /** 1730 * @brief Return true if two uniform integer distributions have 1731 * the same parameters. 1732 */ 1733 friend bool 1734 operator==(const uniform_int_distribution& __d1, 1735 const uniform_int_distribution& __d2) 1736 { return __d1._M_param == __d2._M_param; } 1737 1738 private: 1739 param_type _M_param; 1740 }; 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, _M_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 /** 1910 * @brief Return true if two uniform real distributions have 1911 * the same parameters. 1912 */ 1913 friend bool 1914 operator==(const uniform_real_distribution& __d1, 1915 const uniform_real_distribution& __d2) 1916 { return __d1._M_param == __d2._M_param; } 1917 1918 private: 1919 param_type _M_param; 1920 }; 1921 1922 /** 1923 * @brief Return true if two uniform real distributions have 1924 * different parameters. 1925 */ 1926 template<typename _IntType> 1927 inline bool 1928 operator!=(const std::uniform_real_distribution<_IntType>& __d1, 1929 const std::uniform_real_distribution<_IntType>& __d2) 1930 { return !(__d1 == __d2); } 1931 1932 /** 1933 * @brief Inserts a %uniform_real_distribution random number 1934 * distribution @p __x into the output stream @p __os. 1935 * 1936 * @param __os An output stream. 1937 * @param __x A %uniform_real_distribution random number distribution. 1938 * 1939 * @returns The output stream with the state of @p __x inserted or in 1940 * an error state. 1941 */ 1942 template<typename _RealType, typename _CharT, typename _Traits> 1943 std::basic_ostream<_CharT, _Traits>& 1944 operator<<(std::basic_ostream<_CharT, _Traits>&, 1945 const std::uniform_real_distribution<_RealType>&); 1946 1947 /** 1948 * @brief Extracts a %uniform_real_distribution random number distribution 1949 * @p __x from the input stream @p __is. 1950 * 1951 * @param __is An input stream. 1952 * @param __x A %uniform_real_distribution random number generator engine. 1953 * 1954 * @returns The input stream with @p __x extracted or in an error state. 1955 */ 1956 template<typename _RealType, typename _CharT, typename _Traits> 1957 std::basic_istream<_CharT, _Traits>& 1958 operator>>(std::basic_istream<_CharT, _Traits>&, 1959 std::uniform_real_distribution<_RealType>&); 1960 1961 /* @} */ // group random_distributions_uniform 1962 1963 /** 1964 * @addtogroup random_distributions_normal Normal Distributions 1965 * @ingroup random_distributions 1966 * @{ 1967 */ 1968 1969 /** 1970 * @brief A normal continuous distribution for random numbers. 1971 * 1972 * The formula for the normal probability density function is 1973 * @f[ 1974 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}} 1975 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } 1976 * @f] 1977 */ 1978 template<typename _RealType = double> 1979 class normal_distribution 1980 { 1981 static_assert(std::is_floating_point<_RealType>::value, 1982 "template argument not a floating point type"); 1983 1984 public: 1985 /** The type of the range of the distribution. */ 1986 typedef _RealType result_type; 1987 /** Parameter type. */ 1988 struct param_type 1989 { 1990 typedef normal_distribution<_RealType> distribution_type; 1991 1992 explicit 1993 param_type(_RealType __mean = _RealType(0), 1994 _RealType __stddev = _RealType(1)) 1995 : _M_mean(__mean), _M_stddev(__stddev) 1996 { 1997 _GLIBCXX_DEBUG_ASSERT(_M_stddev > _RealType(0)); 1998 } 1999 2000 _RealType 2001 mean() const 2002 { return _M_mean; } 2003 2004 _RealType 2005 stddev() const 2006 { return _M_stddev; } 2007 2008 friend bool 2009 operator==(const param_type& __p1, const param_type& __p2) 2010 { return (__p1._M_mean == __p2._M_mean 2011 && __p1._M_stddev == __p2._M_stddev); } 2012 2013 private: 2014 _RealType _M_mean; 2015 _RealType _M_stddev; 2016 }; 2017 2018 public: 2019 /** 2020 * Constructs a normal distribution with parameters @f$mean@f$ and 2021 * standard deviation. 2022 */ 2023 explicit 2024 normal_distribution(result_type __mean = result_type(0), 2025 result_type __stddev = result_type(1)) 2026 : _M_param(__mean, __stddev), _M_saved_available(false) 2027 { } 2028 2029 explicit 2030 normal_distribution(const param_type& __p) 2031 : _M_param(__p), _M_saved_available(false) 2032 { } 2033 2034 /** 2035 * @brief Resets the distribution state. 2036 */ 2037 void 2038 reset() 2039 { _M_saved_available = false; } 2040 2041 /** 2042 * @brief Returns the mean of the distribution. 2043 */ 2044 _RealType 2045 mean() const 2046 { return _M_param.mean(); } 2047 2048 /** 2049 * @brief Returns the standard deviation of the distribution. 2050 */ 2051 _RealType 2052 stddev() const 2053 { return _M_param.stddev(); } 2054 2055 /** 2056 * @brief Returns the parameter set of the distribution. 2057 */ 2058 param_type 2059 param() const 2060 { return _M_param; } 2061 2062 /** 2063 * @brief Sets the parameter set of the distribution. 2064 * @param __param The new parameter set of the distribution. 2065 */ 2066 void 2067 param(const param_type& __param) 2068 { _M_param = __param; } 2069 2070 /** 2071 * @brief Returns the greatest lower bound value of the distribution. 2072 */ 2073 result_type 2074 min() const 2075 { return std::numeric_limits<result_type>::min(); } 2076 2077 /** 2078 * @brief Returns the least upper bound value of the distribution. 2079 */ 2080 result_type 2081 max() const 2082 { return std::numeric_limits<result_type>::max(); } 2083 2084 /** 2085 * @brief Generating functions. 2086 */ 2087 template<typename _UniformRandomNumberGenerator> 2088 result_type 2089 operator()(_UniformRandomNumberGenerator& __urng) 2090 { return this->operator()(__urng, _M_param); } 2091 2092 template<typename _UniformRandomNumberGenerator> 2093 result_type 2094 operator()(_UniformRandomNumberGenerator& __urng, 2095 const param_type& __p); 2096 2097 /** 2098 * @brief Return true if two normal distributions have 2099 * the same parameters and the sequences that would 2100 * be generated are equal. 2101 */ 2102 template<typename _RealType1> 2103 friend bool 2104 operator==(const std::normal_distribution<_RealType1>& __d1, 2105 const std::normal_distribution<_RealType1>& __d2); 2106 2107 /** 2108 * @brief Inserts a %normal_distribution random number distribution 2109 * @p __x into the output stream @p __os. 2110 * 2111 * @param __os An output stream. 2112 * @param __x A %normal_distribution random number distribution. 2113 * 2114 * @returns The output stream with the state of @p __x inserted or in 2115 * an error state. 2116 */ 2117 template<typename _RealType1, typename _CharT, typename _Traits> 2118 friend std::basic_ostream<_CharT, _Traits>& 2119 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 2120 const std::normal_distribution<_RealType1>& __x); 2121 2122 /** 2123 * @brief Extracts a %normal_distribution random number distribution 2124 * @p __x from the input stream @p __is. 2125 * 2126 * @param __is An input stream. 2127 * @param __x A %normal_distribution random number generator engine. 2128 * 2129 * @returns The input stream with @p __x extracted or in an error 2130 * state. 2131 */ 2132 template<typename _RealType1, typename _CharT, typename _Traits> 2133 friend std::basic_istream<_CharT, _Traits>& 2134 operator>>(std::basic_istream<_CharT, _Traits>& __is, 2135 std::normal_distribution<_RealType1>& __x); 2136 2137 private: 2138 param_type _M_param; 2139 result_type _M_saved; 2140 bool _M_saved_available; 2141 }; 2142 2143 /** 2144 * @brief Return true if two normal distributions are different. 2145 */ 2146 template<typename _RealType> 2147 inline bool 2148 operator!=(const std::normal_distribution<_RealType>& __d1, 2149 const std::normal_distribution<_RealType>& __d2) 2150 { return !(__d1 == __d2); } 2151 2152 2153 /** 2154 * @brief A lognormal_distribution random number distribution. 2155 * 2156 * The formula for the normal probability mass function is 2157 * @f[ 2158 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}} 2159 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}} 2160 * @f] 2161 */ 2162 template<typename _RealType = double> 2163 class lognormal_distribution 2164 { 2165 static_assert(std::is_floating_point<_RealType>::value, 2166 "template argument not a floating point type"); 2167 2168 public: 2169 /** The type of the range of the distribution. */ 2170 typedef _RealType result_type; 2171 /** Parameter type. */ 2172 struct param_type 2173 { 2174 typedef lognormal_distribution<_RealType> distribution_type; 2175 2176 explicit 2177 param_type(_RealType __m = _RealType(0), 2178 _RealType __s = _RealType(1)) 2179 : _M_m(__m), _M_s(__s) 2180 { } 2181 2182 _RealType 2183 m() const 2184 { return _M_m; } 2185 2186 _RealType 2187 s() const 2188 { return _M_s; } 2189 2190 friend bool 2191 operator==(const param_type& __p1, const param_type& __p2) 2192 { return __p1._M_m == __p2._M_m && __p1._M_s == __p2._M_s; } 2193 2194 private: 2195 _RealType _M_m; 2196 _RealType _M_s; 2197 }; 2198 2199 explicit 2200 lognormal_distribution(_RealType __m = _RealType(0), 2201 _RealType __s = _RealType(1)) 2202 : _M_param(__m, __s), _M_nd() 2203 { } 2204 2205 explicit 2206 lognormal_distribution(const param_type& __p) 2207 : _M_param(__p), _M_nd() 2208 { } 2209 2210 /** 2211 * Resets the distribution state. 2212 */ 2213 void 2214 reset() 2215 { _M_nd.reset(); } 2216 2217 /** 2218 * 2219 */ 2220 _RealType 2221 m() const 2222 { return _M_param.m(); } 2223 2224 _RealType 2225 s() const 2226 { return _M_param.s(); } 2227 2228 /** 2229 * @brief Returns the parameter set of the distribution. 2230 */ 2231 param_type 2232 param() const 2233 { return _M_param; } 2234 2235 /** 2236 * @brief Sets the parameter set of the distribution. 2237 * @param __param The new parameter set of the distribution. 2238 */ 2239 void 2240 param(const param_type& __param) 2241 { _M_param = __param; } 2242 2243 /** 2244 * @brief Returns the greatest lower bound value of the distribution. 2245 */ 2246 result_type 2247 min() const 2248 { return result_type(0); } 2249 2250 /** 2251 * @brief Returns the least upper bound value of the distribution. 2252 */ 2253 result_type 2254 max() const 2255 { return std::numeric_limits<result_type>::max(); } 2256 2257 /** 2258 * @brief Generating functions. 2259 */ 2260 template<typename _UniformRandomNumberGenerator> 2261 result_type 2262 operator()(_UniformRandomNumberGenerator& __urng) 2263 { return this->operator()(__urng, _M_param); } 2264 2265 template<typename _UniformRandomNumberGenerator> 2266 result_type 2267 operator()(_UniformRandomNumberGenerator& __urng, 2268 const param_type& __p) 2269 { return std::exp(__p.s() * _M_nd(__urng) + __p.m()); } 2270 2271 /** 2272 * @brief Return true if two lognormal distributions have 2273 * the same parameters and the sequences that would 2274 * be generated are equal. 2275 */ 2276 friend bool 2277 operator==(const lognormal_distribution& __d1, 2278 const lognormal_distribution& __d2) 2279 { return (__d1._M_param == __d2._M_param 2280 && __d1._M_nd == __d2._M_nd); } 2281 2282 /** 2283 * @brief Inserts a %lognormal_distribution random number distribution 2284 * @p __x into the output stream @p __os. 2285 * 2286 * @param __os An output stream. 2287 * @param __x A %lognormal_distribution random number distribution. 2288 * 2289 * @returns The output stream with the state of @p __x inserted or in 2290 * an error state. 2291 */ 2292 template<typename _RealType1, typename _CharT, typename _Traits> 2293 friend std::basic_ostream<_CharT, _Traits>& 2294 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 2295 const std::lognormal_distribution<_RealType1>& __x); 2296 2297 /** 2298 * @brief Extracts a %lognormal_distribution random number distribution 2299 * @p __x from the input stream @p __is. 2300 * 2301 * @param __is An input stream. 2302 * @param __x A %lognormal_distribution random number 2303 * generator engine. 2304 * 2305 * @returns The input stream with @p __x extracted or in an error state. 2306 */ 2307 template<typename _RealType1, typename _CharT, typename _Traits> 2308 friend std::basic_istream<_CharT, _Traits>& 2309 operator>>(std::basic_istream<_CharT, _Traits>& __is, 2310 std::lognormal_distribution<_RealType1>& __x); 2311 2312 private: 2313 param_type _M_param; 2314 2315 std::normal_distribution<result_type> _M_nd; 2316 }; 2317 2318 /** 2319 * @brief Return true if two lognormal distributions are different. 2320 */ 2321 template<typename _RealType> 2322 inline bool 2323 operator!=(const std::lognormal_distribution<_RealType>& __d1, 2324 const std::lognormal_distribution<_RealType>& __d2) 2325 { return !(__d1 == __d2); } 2326 2327 2328 /** 2329 * @brief A gamma continuous distribution for random numbers. 2330 * 2331 * The formula for the gamma probability density function is: 2332 * @f[ 2333 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)} 2334 * (x/\beta)^{\alpha - 1} e^{-x/\beta} 2335 * @f] 2336 */ 2337 template<typename _RealType = double> 2338 class gamma_distribution 2339 { 2340 static_assert(std::is_floating_point<_RealType>::value, 2341 "template argument not a floating point type"); 2342 2343 public: 2344 /** The type of the range of the distribution. */ 2345 typedef _RealType result_type; 2346 /** Parameter type. */ 2347 struct param_type 2348 { 2349 typedef gamma_distribution<_RealType> distribution_type; 2350 friend class gamma_distribution<_RealType>; 2351 2352 explicit 2353 param_type(_RealType __alpha_val = _RealType(1), 2354 _RealType __beta_val = _RealType(1)) 2355 : _M_alpha(__alpha_val), _M_beta(__beta_val) 2356 { 2357 _GLIBCXX_DEBUG_ASSERT(_M_alpha > _RealType(0)); 2358 _M_initialize(); 2359 } 2360 2361 _RealType 2362 alpha() const 2363 { return _M_alpha; } 2364 2365 _RealType 2366 beta() const 2367 { return _M_beta; } 2368 2369 friend bool 2370 operator==(const param_type& __p1, const param_type& __p2) 2371 { return (__p1._M_alpha == __p2._M_alpha 2372 && __p1._M_beta == __p2._M_beta); } 2373 2374 private: 2375 void 2376 _M_initialize(); 2377 2378 _RealType _M_alpha; 2379 _RealType _M_beta; 2380 2381 _RealType _M_malpha, _M_a2; 2382 }; 2383 2384 public: 2385 /** 2386 * @brief Constructs a gamma distribution with parameters 2387 * @f$\alpha@f$ and @f$\beta@f$. 2388 */ 2389 explicit 2390 gamma_distribution(_RealType __alpha_val = _RealType(1), 2391 _RealType __beta_val = _RealType(1)) 2392 : _M_param(__alpha_val, __beta_val), _M_nd() 2393 { } 2394 2395 explicit 2396 gamma_distribution(const param_type& __p) 2397 : _M_param(__p), _M_nd() 2398 { } 2399 2400 /** 2401 * @brief Resets the distribution state. 2402 */ 2403 void 2404 reset() 2405 { _M_nd.reset(); } 2406 2407 /** 2408 * @brief Returns the @f$\alpha@f$ of the distribution. 2409 */ 2410 _RealType 2411 alpha() const 2412 { return _M_param.alpha(); } 2413 2414 /** 2415 * @brief Returns the @f$\beta@f$ of the distribution. 2416 */ 2417 _RealType 2418 beta() const 2419 { return _M_param.beta(); } 2420 2421 /** 2422 * @brief Returns the parameter set of the distribution. 2423 */ 2424 param_type 2425 param() const 2426 { return _M_param; } 2427 2428 /** 2429 * @brief Sets the parameter set of the distribution. 2430 * @param __param The new parameter set of the distribution. 2431 */ 2432 void 2433 param(const param_type& __param) 2434 { _M_param = __param; } 2435 2436 /** 2437 * @brief Returns the greatest lower bound value of the distribution. 2438 */ 2439 result_type 2440 min() const 2441 { return result_type(0); } 2442 2443 /** 2444 * @brief Returns the least upper bound value of the distribution. 2445 */ 2446 result_type 2447 max() const 2448 { return std::numeric_limits<result_type>::max(); } 2449 2450 /** 2451 * @brief Generating functions. 2452 */ 2453 template<typename _UniformRandomNumberGenerator> 2454 result_type 2455 operator()(_UniformRandomNumberGenerator& __urng) 2456 { return this->operator()(__urng, _M_param); } 2457 2458 template<typename _UniformRandomNumberGenerator> 2459 result_type 2460 operator()(_UniformRandomNumberGenerator& __urng, 2461 const param_type& __p); 2462 2463 /** 2464 * @brief Return true if two gamma distributions have the same 2465 * parameters and the sequences that would be generated 2466 * are equal. 2467 */ 2468 friend bool 2469 operator==(const gamma_distribution& __d1, 2470 const gamma_distribution& __d2) 2471 { return (__d1._M_param == __d2._M_param 2472 && __d1._M_nd == __d2._M_nd); } 2473 2474 /** 2475 * @brief Inserts a %gamma_distribution random number distribution 2476 * @p __x into the output stream @p __os. 2477 * 2478 * @param __os An output stream. 2479 * @param __x A %gamma_distribution random number distribution. 2480 * 2481 * @returns The output stream with the state of @p __x inserted or in 2482 * an error state. 2483 */ 2484 template<typename _RealType1, typename _CharT, typename _Traits> 2485 friend std::basic_ostream<_CharT, _Traits>& 2486 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 2487 const std::gamma_distribution<_RealType1>& __x); 2488 2489 /** 2490 * @brief Extracts a %gamma_distribution random number distribution 2491 * @p __x from the input stream @p __is. 2492 * 2493 * @param __is An input stream. 2494 * @param __x A %gamma_distribution random number generator engine. 2495 * 2496 * @returns The input stream with @p __x extracted or in an error state. 2497 */ 2498 template<typename _RealType1, typename _CharT, typename _Traits> 2499 friend std::basic_istream<_CharT, _Traits>& 2500 operator>>(std::basic_istream<_CharT, _Traits>& __is, 2501 std::gamma_distribution<_RealType1>& __x); 2502 2503 private: 2504 param_type _M_param; 2505 2506 std::normal_distribution<result_type> _M_nd; 2507 }; 2508 2509 /** 2510 * @brief Return true if two gamma distributions are different. 2511 */ 2512 template<typename _RealType> 2513 inline bool 2514 operator!=(const std::gamma_distribution<_RealType>& __d1, 2515 const std::gamma_distribution<_RealType>& __d2) 2516 { return !(__d1 == __d2); } 2517 2518 2519 /** 2520 * @brief A chi_squared_distribution random number distribution. 2521 * 2522 * The formula for the normal probability mass function is 2523 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$ 2524 */ 2525 template<typename _RealType = double> 2526 class chi_squared_distribution 2527 { 2528 static_assert(std::is_floating_point<_RealType>::value, 2529 "template argument not a floating point type"); 2530 2531 public: 2532 /** The type of the range of the distribution. */ 2533 typedef _RealType result_type; 2534 /** Parameter type. */ 2535 struct param_type 2536 { 2537 typedef chi_squared_distribution<_RealType> distribution_type; 2538 2539 explicit 2540 param_type(_RealType __n = _RealType(1)) 2541 : _M_n(__n) 2542 { } 2543 2544 _RealType 2545 n() const 2546 { return _M_n; } 2547 2548 friend bool 2549 operator==(const param_type& __p1, const param_type& __p2) 2550 { return __p1._M_n == __p2._M_n; } 2551 2552 private: 2553 _RealType _M_n; 2554 }; 2555 2556 explicit 2557 chi_squared_distribution(_RealType __n = _RealType(1)) 2558 : _M_param(__n), _M_gd(__n / 2) 2559 { } 2560 2561 explicit 2562 chi_squared_distribution(const param_type& __p) 2563 : _M_param(__p), _M_gd(__p.n() / 2) 2564 { } 2565 2566 /** 2567 * @brief Resets the distribution state. 2568 */ 2569 void 2570 reset() 2571 { _M_gd.reset(); } 2572 2573 /** 2574 * 2575 */ 2576 _RealType 2577 n() const 2578 { return _M_param.n(); } 2579 2580 /** 2581 * @brief Returns the parameter set of the distribution. 2582 */ 2583 param_type 2584 param() const 2585 { return _M_param; } 2586 2587 /** 2588 * @brief Sets the parameter set of the distribution. 2589 * @param __param The new parameter set of the distribution. 2590 */ 2591 void 2592 param(const param_type& __param) 2593 { _M_param = __param; } 2594 2595 /** 2596 * @brief Returns the greatest lower bound value of the distribution. 2597 */ 2598 result_type 2599 min() const 2600 { return result_type(0); } 2601 2602 /** 2603 * @brief Returns the least upper bound value of the distribution. 2604 */ 2605 result_type 2606 max() const 2607 { return std::numeric_limits<result_type>::max(); } 2608 2609 /** 2610 * @brief Generating functions. 2611 */ 2612 template<typename _UniformRandomNumberGenerator> 2613 result_type 2614 operator()(_UniformRandomNumberGenerator& __urng) 2615 { return 2 * _M_gd(__urng); } 2616 2617 template<typename _UniformRandomNumberGenerator> 2618 result_type 2619 operator()(_UniformRandomNumberGenerator& __urng, 2620 const param_type& __p) 2621 { 2622 typedef typename std::gamma_distribution<result_type>::param_type 2623 param_type; 2624 return 2 * _M_gd(__urng, param_type(__p.n() / 2)); 2625 } 2626 2627 /** 2628 * @brief Return true if two Chi-squared distributions have 2629 * the same parameters and the sequences that would be 2630 * generated are equal. 2631 */ 2632 friend bool 2633 operator==(const chi_squared_distribution& __d1, 2634 const chi_squared_distribution& __d2) 2635 { return __d1._M_param == __d2._M_param && __d1._M_gd == __d2._M_gd; } 2636 2637 /** 2638 * @brief Inserts a %chi_squared_distribution random number distribution 2639 * @p __x into the output stream @p __os. 2640 * 2641 * @param __os An output stream. 2642 * @param __x A %chi_squared_distribution random number distribution. 2643 * 2644 * @returns The output stream with the state of @p __x inserted or in 2645 * an error state. 2646 */ 2647 template<typename _RealType1, typename _CharT, typename _Traits> 2648 friend std::basic_ostream<_CharT, _Traits>& 2649 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 2650 const std::chi_squared_distribution<_RealType1>& __x); 2651 2652 /** 2653 * @brief Extracts a %chi_squared_distribution random number distribution 2654 * @p __x from the input stream @p __is. 2655 * 2656 * @param __is An input stream. 2657 * @param __x A %chi_squared_distribution random number 2658 * generator engine. 2659 * 2660 * @returns The input stream with @p __x extracted or in an error state. 2661 */ 2662 template<typename _RealType1, typename _CharT, typename _Traits> 2663 friend std::basic_istream<_CharT, _Traits>& 2664 operator>>(std::basic_istream<_CharT, _Traits>& __is, 2665 std::chi_squared_distribution<_RealType1>& __x); 2666 2667 private: 2668 param_type _M_param; 2669 2670 std::gamma_distribution<result_type> _M_gd; 2671 }; 2672 2673 /** 2674 * @brief Return true if two Chi-squared distributions are different. 2675 */ 2676 template<typename _RealType> 2677 inline bool 2678 operator!=(const std::chi_squared_distribution<_RealType>& __d1, 2679 const std::chi_squared_distribution<_RealType>& __d2) 2680 { return !(__d1 == __d2); } 2681 2682 2683 /** 2684 * @brief A cauchy_distribution random number distribution. 2685 * 2686 * The formula for the normal probability mass function is 2687 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$ 2688 */ 2689 template<typename _RealType = double> 2690 class cauchy_distribution 2691 { 2692 static_assert(std::is_floating_point<_RealType>::value, 2693 "template argument not a floating point type"); 2694 2695 public: 2696 /** The type of the range of the distribution. */ 2697 typedef _RealType result_type; 2698 /** Parameter type. */ 2699 struct param_type 2700 { 2701 typedef cauchy_distribution<_RealType> distribution_type; 2702 2703 explicit 2704 param_type(_RealType __a = _RealType(0), 2705 _RealType __b = _RealType(1)) 2706 : _M_a(__a), _M_b(__b) 2707 { } 2708 2709 _RealType 2710 a() const 2711 { return _M_a; } 2712 2713 _RealType 2714 b() const 2715 { return _M_b; } 2716 2717 friend bool 2718 operator==(const param_type& __p1, const param_type& __p2) 2719 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } 2720 2721 private: 2722 _RealType _M_a; 2723 _RealType _M_b; 2724 }; 2725 2726 explicit 2727 cauchy_distribution(_RealType __a = _RealType(0), 2728 _RealType __b = _RealType(1)) 2729 : _M_param(__a, __b) 2730 { } 2731 2732 explicit 2733 cauchy_distribution(const param_type& __p) 2734 : _M_param(__p) 2735 { } 2736 2737 /** 2738 * @brief Resets the distribution state. 2739 */ 2740 void 2741 reset() 2742 { } 2743 2744 /** 2745 * 2746 */ 2747 _RealType 2748 a() const 2749 { return _M_param.a(); } 2750 2751 _RealType 2752 b() const 2753 { return _M_param.b(); } 2754 2755 /** 2756 * @brief Returns the parameter set of the distribution. 2757 */ 2758 param_type 2759 param() const 2760 { return _M_param; } 2761 2762 /** 2763 * @brief Sets the parameter set of the distribution. 2764 * @param __param The new parameter set of the distribution. 2765 */ 2766 void 2767 param(const param_type& __param) 2768 { _M_param = __param; } 2769 2770 /** 2771 * @brief Returns the greatest lower bound value of the distribution. 2772 */ 2773 result_type 2774 min() const 2775 { return std::numeric_limits<result_type>::min(); } 2776 2777 /** 2778 * @brief Returns the least upper bound value of the distribution. 2779 */ 2780 result_type 2781 max() const 2782 { return std::numeric_limits<result_type>::max(); } 2783 2784 /** 2785 * @brief Generating functions. 2786 */ 2787 template<typename _UniformRandomNumberGenerator> 2788 result_type 2789 operator()(_UniformRandomNumberGenerator& __urng) 2790 { return this->operator()(__urng, _M_param); } 2791 2792 template<typename _UniformRandomNumberGenerator> 2793 result_type 2794 operator()(_UniformRandomNumberGenerator& __urng, 2795 const param_type& __p); 2796 2797 /** 2798 * @brief Return true if two Cauchy distributions have 2799 * the same parameters. 2800 */ 2801 friend bool 2802 operator==(const cauchy_distribution& __d1, 2803 const cauchy_distribution& __d2) 2804 { return __d1._M_param == __d2._M_param; } 2805 2806 private: 2807 param_type _M_param; 2808 }; 2809 2810 /** 2811 * @brief Return true if two Cauchy distributions have 2812 * different parameters. 2813 */ 2814 template<typename _RealType> 2815 inline bool 2816 operator!=(const std::cauchy_distribution<_RealType>& __d1, 2817 const std::cauchy_distribution<_RealType>& __d2) 2818 { return !(__d1 == __d2); } 2819 2820 /** 2821 * @brief Inserts a %cauchy_distribution random number distribution 2822 * @p __x into the output stream @p __os. 2823 * 2824 * @param __os An output stream. 2825 * @param __x A %cauchy_distribution random number distribution. 2826 * 2827 * @returns The output stream with the state of @p __x inserted or in 2828 * an error state. 2829 */ 2830 template<typename _RealType, typename _CharT, typename _Traits> 2831 std::basic_ostream<_CharT, _Traits>& 2832 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 2833 const std::cauchy_distribution<_RealType>& __x); 2834 2835 /** 2836 * @brief Extracts a %cauchy_distribution random number distribution 2837 * @p __x from the input stream @p __is. 2838 * 2839 * @param __is An input stream. 2840 * @param __x A %cauchy_distribution random number 2841 * generator engine. 2842 * 2843 * @returns The input stream with @p __x extracted or in an error state. 2844 */ 2845 template<typename _RealType, typename _CharT, typename _Traits> 2846 std::basic_istream<_CharT, _Traits>& 2847 operator>>(std::basic_istream<_CharT, _Traits>& __is, 2848 std::cauchy_distribution<_RealType>& __x); 2849 2850 2851 /** 2852 * @brief A fisher_f_distribution random number distribution. 2853 * 2854 * The formula for the normal probability mass function is 2855 * @f[ 2856 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)} 2857 * (\frac{m}{n})^{m/2} x^{(m/2)-1} 2858 * (1 + \frac{mx}{n})^{-(m+n)/2} 2859 * @f] 2860 */ 2861 template<typename _RealType = double> 2862 class fisher_f_distribution 2863 { 2864 static_assert(std::is_floating_point<_RealType>::value, 2865 "template argument not a floating point type"); 2866 2867 public: 2868 /** The type of the range of the distribution. */ 2869 typedef _RealType result_type; 2870 /** Parameter type. */ 2871 struct param_type 2872 { 2873 typedef fisher_f_distribution<_RealType> distribution_type; 2874 2875 explicit 2876 param_type(_RealType __m = _RealType(1), 2877 _RealType __n = _RealType(1)) 2878 : _M_m(__m), _M_n(__n) 2879 { } 2880 2881 _RealType 2882 m() const 2883 { return _M_m; } 2884 2885 _RealType 2886 n() const 2887 { return _M_n; } 2888 2889 friend bool 2890 operator==(const param_type& __p1, const param_type& __p2) 2891 { return __p1._M_m == __p2._M_m && __p1._M_n == __p2._M_n; } 2892 2893 private: 2894 _RealType _M_m; 2895 _RealType _M_n; 2896 }; 2897 2898 explicit 2899 fisher_f_distribution(_RealType __m = _RealType(1), 2900 _RealType __n = _RealType(1)) 2901 : _M_param(__m, __n), _M_gd_x(__m / 2), _M_gd_y(__n / 2) 2902 { } 2903 2904 explicit 2905 fisher_f_distribution(const param_type& __p) 2906 : _M_param(__p), _M_gd_x(__p.m() / 2), _M_gd_y(__p.n() / 2) 2907 { } 2908 2909 /** 2910 * @brief Resets the distribution state. 2911 */ 2912 void 2913 reset() 2914 { 2915 _M_gd_x.reset(); 2916 _M_gd_y.reset(); 2917 } 2918 2919 /** 2920 * 2921 */ 2922 _RealType 2923 m() const 2924 { return _M_param.m(); } 2925 2926 _RealType 2927 n() const 2928 { return _M_param.n(); } 2929 2930 /** 2931 * @brief Returns the parameter set of the distribution. 2932 */ 2933 param_type 2934 param() const 2935 { return _M_param; } 2936 2937 /** 2938 * @brief Sets the parameter set of the distribution. 2939 * @param __param The new parameter set of the distribution. 2940 */ 2941 void 2942 param(const param_type& __param) 2943 { _M_param = __param; } 2944 2945 /** 2946 * @brief Returns the greatest lower bound value of the distribution. 2947 */ 2948 result_type 2949 min() const 2950 { return result_type(0); } 2951 2952 /** 2953 * @brief Returns the least upper bound value of the distribution. 2954 */ 2955 result_type 2956 max() const 2957 { return std::numeric_limits<result_type>::max(); } 2958 2959 /** 2960 * @brief Generating functions. 2961 */ 2962 template<typename _UniformRandomNumberGenerator> 2963 result_type 2964 operator()(_UniformRandomNumberGenerator& __urng) 2965 { return (_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()); } 2966 2967 template<typename _UniformRandomNumberGenerator> 2968 result_type 2969 operator()(_UniformRandomNumberGenerator& __urng, 2970 const param_type& __p) 2971 { 2972 typedef typename std::gamma_distribution<result_type>::param_type 2973 param_type; 2974 return ((_M_gd_x(__urng, param_type(__p.m() / 2)) * n()) 2975 / (_M_gd_y(__urng, param_type(__p.n() / 2)) * m())); 2976 } 2977 2978 /** 2979 * @brief Return true if two Fisher f distributions have 2980 * the same parameters and the sequences that would 2981 * be generated are equal. 2982 */ 2983 friend bool 2984 operator==(const fisher_f_distribution& __d1, 2985 const fisher_f_distribution& __d2) 2986 { return (__d1._M_param == __d2._M_param 2987 && __d1._M_gd_x == __d2._M_gd_x 2988 && __d1._M_gd_y == __d2._M_gd_y); } 2989 2990 /** 2991 * @brief Inserts a %fisher_f_distribution random number distribution 2992 * @p __x into the output stream @p __os. 2993 * 2994 * @param __os An output stream. 2995 * @param __x A %fisher_f_distribution random number distribution. 2996 * 2997 * @returns The output stream with the state of @p __x inserted or in 2998 * an error state. 2999 */ 3000 template<typename _RealType1, typename _CharT, typename _Traits> 3001 friend std::basic_ostream<_CharT, _Traits>& 3002 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 3003 const std::fisher_f_distribution<_RealType1>& __x); 3004 3005 /** 3006 * @brief Extracts a %fisher_f_distribution random number distribution 3007 * @p __x from the input stream @p __is. 3008 * 3009 * @param __is An input stream. 3010 * @param __x A %fisher_f_distribution random number 3011 * generator engine. 3012 * 3013 * @returns The input stream with @p __x extracted or in an error state. 3014 */ 3015 template<typename _RealType1, typename _CharT, typename _Traits> 3016 friend std::basic_istream<_CharT, _Traits>& 3017 operator>>(std::basic_istream<_CharT, _Traits>& __is, 3018 std::fisher_f_distribution<_RealType1>& __x); 3019 3020 private: 3021 param_type _M_param; 3022 3023 std::gamma_distribution<result_type> _M_gd_x, _M_gd_y; 3024 }; 3025 3026 /** 3027 * @brief Return true if two Fisher f distributions are diferent. 3028 */ 3029 template<typename _RealType> 3030 inline bool 3031 operator!=(const std::fisher_f_distribution<_RealType>& __d1, 3032 const std::fisher_f_distribution<_RealType>& __d2) 3033 { return !(__d1 == __d2); } 3034 3035 /** 3036 * @brief A student_t_distribution random number distribution. 3037 * 3038 * The formula for the normal probability mass function is: 3039 * @f[ 3040 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)} 3041 * (1 + \frac{x^2}{n}) ^{-(n+1)/2} 3042 * @f] 3043 */ 3044 template<typename _RealType = double> 3045 class student_t_distribution 3046 { 3047 static_assert(std::is_floating_point<_RealType>::value, 3048 "template argument not a floating point type"); 3049 3050 public: 3051 /** The type of the range of the distribution. */ 3052 typedef _RealType result_type; 3053 /** Parameter type. */ 3054 struct param_type 3055 { 3056 typedef student_t_distribution<_RealType> distribution_type; 3057 3058 explicit 3059 param_type(_RealType __n = _RealType(1)) 3060 : _M_n(__n) 3061 { } 3062 3063 _RealType 3064 n() const 3065 { return _M_n; } 3066 3067 friend bool 3068 operator==(const param_type& __p1, const param_type& __p2) 3069 { return __p1._M_n == __p2._M_n; } 3070 3071 private: 3072 _RealType _M_n; 3073 }; 3074 3075 explicit 3076 student_t_distribution(_RealType __n = _RealType(1)) 3077 : _M_param(__n), _M_nd(), _M_gd(__n / 2, 2) 3078 { } 3079 3080 explicit 3081 student_t_distribution(const param_type& __p) 3082 : _M_param(__p), _M_nd(), _M_gd(__p.n() / 2, 2) 3083 { } 3084 3085 /** 3086 * @brief Resets the distribution state. 3087 */ 3088 void 3089 reset() 3090 { 3091 _M_nd.reset(); 3092 _M_gd.reset(); 3093 } 3094 3095 /** 3096 * 3097 */ 3098 _RealType 3099 n() const 3100 { return _M_param.n(); } 3101 3102 /** 3103 * @brief Returns the parameter set of the distribution. 3104 */ 3105 param_type 3106 param() const 3107 { return _M_param; } 3108 3109 /** 3110 * @brief Sets the parameter set of the distribution. 3111 * @param __param The new parameter set of the distribution. 3112 */ 3113 void 3114 param(const param_type& __param) 3115 { _M_param = __param; } 3116 3117 /** 3118 * @brief Returns the greatest lower bound value of the distribution. 3119 */ 3120 result_type 3121 min() const 3122 { return std::numeric_limits<result_type>::min(); } 3123 3124 /** 3125 * @brief Returns the least upper bound value of the distribution. 3126 */ 3127 result_type 3128 max() const 3129 { return std::numeric_limits<result_type>::max(); } 3130 3131 /** 3132 * @brief Generating functions. 3133 */ 3134 template<typename _UniformRandomNumberGenerator> 3135 result_type 3136 operator()(_UniformRandomNumberGenerator& __urng) 3137 { return _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng)); } 3138 3139 template<typename _UniformRandomNumberGenerator> 3140 result_type 3141 operator()(_UniformRandomNumberGenerator& __urng, 3142 const param_type& __p) 3143 { 3144 typedef typename std::gamma_distribution<result_type>::param_type 3145 param_type; 3146 3147 const result_type __g = _M_gd(__urng, param_type(__p.n() / 2, 2)); 3148 return _M_nd(__urng) * std::sqrt(__p.n() / __g); 3149 } 3150 3151 /** 3152 * @brief Return true if two Student t distributions have 3153 * the same parameters and the sequences that would 3154 * be generated are equal. 3155 */ 3156 friend bool 3157 operator==(const student_t_distribution& __d1, 3158 const student_t_distribution& __d2) 3159 { return (__d1._M_param == __d2._M_param 3160 && __d1._M_nd == __d2._M_nd && __d1._M_gd == __d2._M_gd); } 3161 3162 /** 3163 * @brief Inserts a %student_t_distribution random number distribution 3164 * @p __x into the output stream @p __os. 3165 * 3166 * @param __os An output stream. 3167 * @param __x A %student_t_distribution random number distribution. 3168 * 3169 * @returns The output stream with the state of @p __x inserted or in 3170 * an error state. 3171 */ 3172 template<typename _RealType1, typename _CharT, typename _Traits> 3173 friend std::basic_ostream<_CharT, _Traits>& 3174 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 3175 const std::student_t_distribution<_RealType1>& __x); 3176 3177 /** 3178 * @brief Extracts a %student_t_distribution random number distribution 3179 * @p __x from the input stream @p __is. 3180 * 3181 * @param __is An input stream. 3182 * @param __x A %student_t_distribution random number 3183 * generator engine. 3184 * 3185 * @returns The input stream with @p __x extracted or in an error state. 3186 */ 3187 template<typename _RealType1, typename _CharT, typename _Traits> 3188 friend std::basic_istream<_CharT, _Traits>& 3189 operator>>(std::basic_istream<_CharT, _Traits>& __is, 3190 std::student_t_distribution<_RealType1>& __x); 3191 3192 private: 3193 param_type _M_param; 3194 3195 std::normal_distribution<result_type> _M_nd; 3196 std::gamma_distribution<result_type> _M_gd; 3197 }; 3198 3199 /** 3200 * @brief Return true if two Student t distributions are different. 3201 */ 3202 template<typename _RealType> 3203 inline bool 3204 operator!=(const std::student_t_distribution<_RealType>& __d1, 3205 const std::student_t_distribution<_RealType>& __d2) 3206 { return !(__d1 == __d2); } 3207 3208 3209 /* @} */ // group random_distributions_normal 3210 3211 /** 3212 * @addtogroup random_distributions_bernoulli Bernoulli Distributions 3213 * @ingroup random_distributions 3214 * @{ 3215 */ 3216 3217 /** 3218 * @brief A Bernoulli random number distribution. 3219 * 3220 * Generates a sequence of true and false values with likelihood @f$p@f$ 3221 * that true will come up and @f$(1 - p)@f$ that false will appear. 3222 */ 3223 class bernoulli_distribution 3224 { 3225 public: 3226 /** The type of the range of the distribution. */ 3227 typedef bool result_type; 3228 /** Parameter type. */ 3229 struct param_type 3230 { 3231 typedef bernoulli_distribution distribution_type; 3232 3233 explicit 3234 param_type(double __p = 0.5) 3235 : _M_p(__p) 3236 { 3237 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0)); 3238 } 3239 3240 double 3241 p() const 3242 { return _M_p; } 3243 3244 friend bool 3245 operator==(const param_type& __p1, const param_type& __p2) 3246 { return __p1._M_p == __p2._M_p; } 3247 3248 private: 3249 double _M_p; 3250 }; 3251 3252 public: 3253 /** 3254 * @brief Constructs a Bernoulli distribution with likelihood @p p. 3255 * 3256 * @param __p [IN] The likelihood of a true result being returned. 3257 * Must be in the interval @f$[0, 1]@f$. 3258 */ 3259 explicit 3260 bernoulli_distribution(double __p = 0.5) 3261 : _M_param(__p) 3262 { } 3263 3264 explicit 3265 bernoulli_distribution(const param_type& __p) 3266 : _M_param(__p) 3267 { } 3268 3269 /** 3270 * @brief Resets the distribution state. 3271 * 3272 * Does nothing for a Bernoulli distribution. 3273 */ 3274 void 3275 reset() { } 3276 3277 /** 3278 * @brief Returns the @p p parameter of the distribution. 3279 */ 3280 double 3281 p() const 3282 { return _M_param.p(); } 3283 3284 /** 3285 * @brief Returns the parameter set of the distribution. 3286 */ 3287 param_type 3288 param() const 3289 { return _M_param; } 3290 3291 /** 3292 * @brief Sets the parameter set of the distribution. 3293 * @param __param The new parameter set of the distribution. 3294 */ 3295 void 3296 param(const param_type& __param) 3297 { _M_param = __param; } 3298 3299 /** 3300 * @brief Returns the greatest lower bound value of the distribution. 3301 */ 3302 result_type 3303 min() const 3304 { return std::numeric_limits<result_type>::min(); } 3305 3306 /** 3307 * @brief Returns the least upper bound value of the distribution. 3308 */ 3309 result_type 3310 max() const 3311 { return std::numeric_limits<result_type>::max(); } 3312 3313 /** 3314 * @brief Generating functions. 3315 */ 3316 template<typename _UniformRandomNumberGenerator> 3317 result_type 3318 operator()(_UniformRandomNumberGenerator& __urng) 3319 { return this->operator()(__urng, _M_param); } 3320 3321 template<typename _UniformRandomNumberGenerator> 3322 result_type 3323 operator()(_UniformRandomNumberGenerator& __urng, 3324 const param_type& __p) 3325 { 3326 __detail::_Adaptor<_UniformRandomNumberGenerator, double> 3327 __aurng(__urng); 3328 if ((__aurng() - __aurng.min()) 3329 < __p.p() * (__aurng.max() - __aurng.min())) 3330 return true; 3331 return false; 3332 } 3333 3334 /** 3335 * @brief Return true if two Bernoulli distributions have 3336 * the same parameters. 3337 */ 3338 friend bool 3339 operator==(const bernoulli_distribution& __d1, 3340 const bernoulli_distribution& __d2) 3341 { return __d1._M_param == __d2._M_param; } 3342 3343 private: 3344 param_type _M_param; 3345 }; 3346 3347 /** 3348 * @brief Return true if two Bernoulli distributions have 3349 * different parameters. 3350 */ 3351 inline bool 3352 operator!=(const std::bernoulli_distribution& __d1, 3353 const std::bernoulli_distribution& __d2) 3354 { return !(__d1 == __d2); } 3355 3356 /** 3357 * @brief Inserts a %bernoulli_distribution random number distribution 3358 * @p __x into the output stream @p __os. 3359 * 3360 * @param __os An output stream. 3361 * @param __x A %bernoulli_distribution random number distribution. 3362 * 3363 * @returns The output stream with the state of @p __x inserted or in 3364 * an error state. 3365 */ 3366 template<typename _CharT, typename _Traits> 3367 std::basic_ostream<_CharT, _Traits>& 3368 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 3369 const std::bernoulli_distribution& __x); 3370 3371 /** 3372 * @brief Extracts a %bernoulli_distribution random number distribution 3373 * @p __x from the input stream @p __is. 3374 * 3375 * @param __is An input stream. 3376 * @param __x A %bernoulli_distribution random number generator engine. 3377 * 3378 * @returns The input stream with @p __x extracted or in an error state. 3379 */ 3380 template<typename _CharT, typename _Traits> 3381 std::basic_istream<_CharT, _Traits>& 3382 operator>>(std::basic_istream<_CharT, _Traits>& __is, 3383 std::bernoulli_distribution& __x) 3384 { 3385 double __p; 3386 __is >> __p; 3387 __x.param(bernoulli_distribution::param_type(__p)); 3388 return __is; 3389 } 3390 3391 3392 /** 3393 * @brief A discrete binomial random number distribution. 3394 * 3395 * The formula for the binomial probability density function is 3396 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$ 3397 * and @f$p@f$ are the parameters of the distribution. 3398 */ 3399 template<typename _IntType = int> 3400 class binomial_distribution 3401 { 3402 static_assert(std::is_integral<_IntType>::value, 3403 "template argument not an integral type"); 3404 3405 public: 3406 /** The type of the range of the distribution. */ 3407 typedef _IntType result_type; 3408 /** Parameter type. */ 3409 struct param_type 3410 { 3411 typedef binomial_distribution<_IntType> distribution_type; 3412 friend class binomial_distribution<_IntType>; 3413 3414 explicit 3415 param_type(_IntType __t = _IntType(1), double __p = 0.5) 3416 : _M_t(__t), _M_p(__p) 3417 { 3418 _GLIBCXX_DEBUG_ASSERT((_M_t >= _IntType(0)) 3419 && (_M_p >= 0.0) 3420 && (_M_p <= 1.0)); 3421 _M_initialize(); 3422 } 3423 3424 _IntType 3425 t() const 3426 { return _M_t; } 3427 3428 double 3429 p() const 3430 { return _M_p; } 3431 3432 friend bool 3433 operator==(const param_type& __p1, const param_type& __p2) 3434 { return __p1._M_t == __p2._M_t && __p1._M_p == __p2._M_p; } 3435 3436 private: 3437 void 3438 _M_initialize(); 3439 3440 _IntType _M_t; 3441 double _M_p; 3442 3443 double _M_q; 3444 #if _GLIBCXX_USE_C99_MATH_TR1 3445 double _M_d1, _M_d2, _M_s1, _M_s2, _M_c, 3446 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p; 3447 #endif 3448 bool _M_easy; 3449 }; 3450 3451 // constructors and member function 3452 explicit 3453 binomial_distribution(_IntType __t = _IntType(1), 3454 double __p = 0.5) 3455 : _M_param(__t, __p), _M_nd() 3456 { } 3457 3458 explicit 3459 binomial_distribution(const param_type& __p) 3460 : _M_param(__p), _M_nd() 3461 { } 3462 3463 /** 3464 * @brief Resets the distribution state. 3465 */ 3466 void 3467 reset() 3468 { _M_nd.reset(); } 3469 3470 /** 3471 * @brief Returns the distribution @p t parameter. 3472 */ 3473 _IntType 3474 t() const 3475 { return _M_param.t(); } 3476 3477 /** 3478 * @brief Returns the distribution @p p parameter. 3479 */ 3480 double 3481 p() const 3482 { return _M_param.p(); } 3483 3484 /** 3485 * @brief Returns the parameter set of the distribution. 3486 */ 3487 param_type 3488 param() const 3489 { return _M_param; } 3490 3491 /** 3492 * @brief Sets the parameter set of the distribution. 3493 * @param __param The new parameter set of the distribution. 3494 */ 3495 void 3496 param(const param_type& __param) 3497 { _M_param = __param; } 3498 3499 /** 3500 * @brief Returns the greatest lower bound value of the distribution. 3501 */ 3502 result_type 3503 min() const 3504 { return 0; } 3505 3506 /** 3507 * @brief Returns the least upper bound value of the distribution. 3508 */ 3509 result_type 3510 max() const 3511 { return _M_param.t(); } 3512 3513 /** 3514 * @brief Generating functions. 3515 */ 3516 template<typename _UniformRandomNumberGenerator> 3517 result_type 3518 operator()(_UniformRandomNumberGenerator& __urng) 3519 { return this->operator()(__urng, _M_param); } 3520 3521 template<typename _UniformRandomNumberGenerator> 3522 result_type 3523 operator()(_UniformRandomNumberGenerator& __urng, 3524 const param_type& __p); 3525 3526 /** 3527 * @brief Return true if two binomial distributions have 3528 * the same parameters and the sequences that would 3529 * be generated are equal. 3530 */ 3531 friend bool 3532 operator==(const binomial_distribution& __d1, 3533 const binomial_distribution& __d2) 3534 #ifdef _GLIBCXX_USE_C99_MATH_TR1 3535 { return __d1._M_param == __d2._M_param && __d1._M_nd == __d2._M_nd; } 3536 #else 3537 { return __d1._M_param == __d2._M_param; } 3538 #endif 3539 3540 /** 3541 * @brief Inserts a %binomial_distribution random number distribution 3542 * @p __x into the output stream @p __os. 3543 * 3544 * @param __os An output stream. 3545 * @param __x A %binomial_distribution random number distribution. 3546 * 3547 * @returns The output stream with the state of @p __x inserted or in 3548 * an error state. 3549 */ 3550 template<typename _IntType1, 3551 typename _CharT, typename _Traits> 3552 friend std::basic_ostream<_CharT, _Traits>& 3553 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 3554 const std::binomial_distribution<_IntType1>& __x); 3555 3556 /** 3557 * @brief Extracts a %binomial_distribution random number distribution 3558 * @p __x from the input stream @p __is. 3559 * 3560 * @param __is An input stream. 3561 * @param __x A %binomial_distribution random number generator engine. 3562 * 3563 * @returns The input stream with @p __x extracted or in an error 3564 * state. 3565 */ 3566 template<typename _IntType1, 3567 typename _CharT, typename _Traits> 3568 friend std::basic_istream<_CharT, _Traits>& 3569 operator>>(std::basic_istream<_CharT, _Traits>& __is, 3570 std::binomial_distribution<_IntType1>& __x); 3571 3572 private: 3573 template<typename _UniformRandomNumberGenerator> 3574 result_type 3575 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t); 3576 3577 param_type _M_param; 3578 3579 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined. 3580 std::normal_distribution<double> _M_nd; 3581 }; 3582 3583 /** 3584 * @brief Return true if two binomial distributions are different. 3585 */ 3586 template<typename _IntType> 3587 inline bool 3588 operator!=(const std::binomial_distribution<_IntType>& __d1, 3589 const std::binomial_distribution<_IntType>& __d2) 3590 { return !(__d1 == __d2); } 3591 3592 3593 /** 3594 * @brief A discrete geometric random number distribution. 3595 * 3596 * The formula for the geometric probability density function is 3597 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the 3598 * distribution. 3599 */ 3600 template<typename _IntType = int> 3601 class geometric_distribution 3602 { 3603 static_assert(std::is_integral<_IntType>::value, 3604 "template argument not an integral type"); 3605 3606 public: 3607 /** The type of the range of the distribution. */ 3608 typedef _IntType result_type; 3609 /** Parameter type. */ 3610 struct param_type 3611 { 3612 typedef geometric_distribution<_IntType> distribution_type; 3613 friend class geometric_distribution<_IntType>; 3614 3615 explicit 3616 param_type(double __p = 0.5) 3617 : _M_p(__p) 3618 { 3619 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0)); 3620 _M_initialize(); 3621 } 3622 3623 double 3624 p() const 3625 { return _M_p; } 3626 3627 friend bool 3628 operator==(const param_type& __p1, const param_type& __p2) 3629 { return __p1._M_p == __p2._M_p; } 3630 3631 private: 3632 void 3633 _M_initialize() 3634 { _M_log_1_p = std::log(1.0 - _M_p); } 3635 3636 double _M_p; 3637 3638 double _M_log_1_p; 3639 }; 3640 3641 // constructors and member function 3642 explicit 3643 geometric_distribution(double __p = 0.5) 3644 : _M_param(__p) 3645 { } 3646 3647 explicit 3648 geometric_distribution(const param_type& __p) 3649 : _M_param(__p) 3650 { } 3651 3652 /** 3653 * @brief Resets the distribution state. 3654 * 3655 * Does nothing for the geometric distribution. 3656 */ 3657 void 3658 reset() { } 3659 3660 /** 3661 * @brief Returns the distribution parameter @p p. 3662 */ 3663 double 3664 p() const 3665 { return _M_param.p(); } 3666 3667 /** 3668 * @brief Returns the parameter set of the distribution. 3669 */ 3670 param_type 3671 param() const 3672 { return _M_param; } 3673 3674 /** 3675 * @brief Sets the parameter set of the distribution. 3676 * @param __param The new parameter set of the distribution. 3677 */ 3678 void 3679 param(const param_type& __param) 3680 { _M_param = __param; } 3681 3682 /** 3683 * @brief Returns the greatest lower bound value of the distribution. 3684 */ 3685 result_type 3686 min() const 3687 { return 0; } 3688 3689 /** 3690 * @brief Returns the least upper bound value of the distribution. 3691 */ 3692 result_type 3693 max() const 3694 { return std::numeric_limits<result_type>::max(); } 3695 3696 /** 3697 * @brief Generating functions. 3698 */ 3699 template<typename _UniformRandomNumberGenerator> 3700 result_type 3701 operator()(_UniformRandomNumberGenerator& __urng) 3702 { return this->operator()(__urng, _M_param); } 3703 3704 template<typename _UniformRandomNumberGenerator> 3705 result_type 3706 operator()(_UniformRandomNumberGenerator& __urng, 3707 const param_type& __p); 3708 3709 /** 3710 * @brief Return true if two geometric distributions have 3711 * the same parameters. 3712 */ 3713 friend bool 3714 operator==(const geometric_distribution& __d1, 3715 const geometric_distribution& __d2) 3716 { return __d1._M_param == __d2._M_param; } 3717 3718 private: 3719 param_type _M_param; 3720 }; 3721 3722 /** 3723 * @brief Return true if two geometric distributions have 3724 * different parameters. 3725 */ 3726 template<typename _IntType> 3727 inline bool 3728 operator!=(const std::geometric_distribution<_IntType>& __d1, 3729 const std::geometric_distribution<_IntType>& __d2) 3730 { return !(__d1 == __d2); } 3731 3732 /** 3733 * @brief Inserts a %geometric_distribution random number distribution 3734 * @p __x into the output stream @p __os. 3735 * 3736 * @param __os An output stream. 3737 * @param __x A %geometric_distribution random number distribution. 3738 * 3739 * @returns The output stream with the state of @p __x inserted or in 3740 * an error state. 3741 */ 3742 template<typename _IntType, 3743 typename _CharT, typename _Traits> 3744 std::basic_ostream<_CharT, _Traits>& 3745 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 3746 const std::geometric_distribution<_IntType>& __x); 3747 3748 /** 3749 * @brief Extracts a %geometric_distribution random number distribution 3750 * @p __x from the input stream @p __is. 3751 * 3752 * @param __is An input stream. 3753 * @param __x A %geometric_distribution random number generator engine. 3754 * 3755 * @returns The input stream with @p __x extracted or in an error state. 3756 */ 3757 template<typename _IntType, 3758 typename _CharT, typename _Traits> 3759 std::basic_istream<_CharT, _Traits>& 3760 operator>>(std::basic_istream<_CharT, _Traits>& __is, 3761 std::geometric_distribution<_IntType>& __x); 3762 3763 3764 /** 3765 * @brief A negative_binomial_distribution random number distribution. 3766 * 3767 * The formula for the negative binomial probability mass function is 3768 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$ 3769 * and @f$p@f$ are the parameters of the distribution. 3770 */ 3771 template<typename _IntType = int> 3772 class negative_binomial_distribution 3773 { 3774 static_assert(std::is_integral<_IntType>::value, 3775 "template argument not an integral type"); 3776 3777 public: 3778 /** The type of the range of the distribution. */ 3779 typedef _IntType result_type; 3780 /** Parameter type. */ 3781 struct param_type 3782 { 3783 typedef negative_binomial_distribution<_IntType> distribution_type; 3784 3785 explicit 3786 param_type(_IntType __k = 1, double __p = 0.5) 3787 : _M_k(__k), _M_p(__p) 3788 { 3789 _GLIBCXX_DEBUG_ASSERT((_M_k > 0) && (_M_p > 0.0) && (_M_p <= 1.0)); 3790 } 3791 3792 _IntType 3793 k() const 3794 { return _M_k; } 3795 3796 double 3797 p() const 3798 { return _M_p; } 3799 3800 friend bool 3801 operator==(const param_type& __p1, const param_type& __p2) 3802 { return __p1._M_k == __p2._M_k && __p1._M_p == __p2._M_p; } 3803 3804 private: 3805 _IntType _M_k; 3806 double _M_p; 3807 }; 3808 3809 explicit 3810 negative_binomial_distribution(_IntType __k = 1, double __p = 0.5) 3811 : _M_param(__k, __p), _M_gd(__k, (1.0 - __p) / __p) 3812 { } 3813 3814 explicit 3815 negative_binomial_distribution(const param_type& __p) 3816 : _M_param(__p), _M_gd(__p.k(), (1.0 - __p.p()) / __p.p()) 3817 { } 3818 3819 /** 3820 * @brief Resets the distribution state. 3821 */ 3822 void 3823 reset() 3824 { _M_gd.reset(); } 3825 3826 /** 3827 * @brief Return the @f$k@f$ parameter of the distribution. 3828 */ 3829 _IntType 3830 k() const 3831 { return _M_param.k(); } 3832 3833 /** 3834 * @brief Return the @f$p@f$ parameter of the distribution. 3835 */ 3836 double 3837 p() const 3838 { return _M_param.p(); } 3839 3840 /** 3841 * @brief Returns the parameter set of the distribution. 3842 */ 3843 param_type 3844 param() const 3845 { return _M_param; } 3846 3847 /** 3848 * @brief Sets the parameter set of the distribution. 3849 * @param __param The new parameter set of the distribution. 3850 */ 3851 void 3852 param(const param_type& __param) 3853 { _M_param = __param; } 3854 3855 /** 3856 * @brief Returns the greatest lower bound value of the distribution. 3857 */ 3858 result_type 3859 min() const 3860 { return result_type(0); } 3861 3862 /** 3863 * @brief Returns the least upper bound value of the distribution. 3864 */ 3865 result_type 3866 max() const 3867 { return std::numeric_limits<result_type>::max(); } 3868 3869 /** 3870 * @brief Generating functions. 3871 */ 3872 template<typename _UniformRandomNumberGenerator> 3873 result_type 3874 operator()(_UniformRandomNumberGenerator& __urng); 3875 3876 template<typename _UniformRandomNumberGenerator> 3877 result_type 3878 operator()(_UniformRandomNumberGenerator& __urng, 3879 const param_type& __p); 3880 3881 /** 3882 * @brief Return true if two negative binomial distributions have 3883 * the same parameters and the sequences that would be 3884 * generated are equal. 3885 */ 3886 friend bool 3887 operator==(const negative_binomial_distribution& __d1, 3888 const negative_binomial_distribution& __d2) 3889 { return __d1._M_param == __d2._M_param && __d1._M_gd == __d2._M_gd; } 3890 3891 /** 3892 * @brief Inserts a %negative_binomial_distribution random 3893 * number distribution @p __x into the output stream @p __os. 3894 * 3895 * @param __os An output stream. 3896 * @param __x A %negative_binomial_distribution random number 3897 * distribution. 3898 * 3899 * @returns The output stream with the state of @p __x inserted or in 3900 * an error state. 3901 */ 3902 template<typename _IntType1, typename _CharT, typename _Traits> 3903 friend std::basic_ostream<_CharT, _Traits>& 3904 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 3905 const std::negative_binomial_distribution<_IntType1>& __x); 3906 3907 /** 3908 * @brief Extracts a %negative_binomial_distribution random number 3909 * distribution @p __x from the input stream @p __is. 3910 * 3911 * @param __is An input stream. 3912 * @param __x A %negative_binomial_distribution random number 3913 * generator engine. 3914 * 3915 * @returns The input stream with @p __x extracted or in an error state. 3916 */ 3917 template<typename _IntType1, typename _CharT, typename _Traits> 3918 friend std::basic_istream<_CharT, _Traits>& 3919 operator>>(std::basic_istream<_CharT, _Traits>& __is, 3920 std::negative_binomial_distribution<_IntType1>& __x); 3921 3922 private: 3923 param_type _M_param; 3924 3925 std::gamma_distribution<double> _M_gd; 3926 }; 3927 3928 /** 3929 * @brief Return true if two negative binomial distributions are different. 3930 */ 3931 template<typename _IntType> 3932 inline bool 3933 operator!=(const std::negative_binomial_distribution<_IntType>& __d1, 3934 const std::negative_binomial_distribution<_IntType>& __d2) 3935 { return !(__d1 == __d2); } 3936 3937 3938 /* @} */ // group random_distributions_bernoulli 3939 3940 /** 3941 * @addtogroup random_distributions_poisson Poisson Distributions 3942 * @ingroup random_distributions 3943 * @{ 3944 */ 3945 3946 /** 3947 * @brief A discrete Poisson random number distribution. 3948 * 3949 * The formula for the Poisson probability density function is 3950 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the 3951 * parameter of the distribution. 3952 */ 3953 template<typename _IntType = int> 3954 class poisson_distribution 3955 { 3956 static_assert(std::is_integral<_IntType>::value, 3957 "template argument not an integral type"); 3958 3959 public: 3960 /** The type of the range of the distribution. */ 3961 typedef _IntType result_type; 3962 /** Parameter type. */ 3963 struct param_type 3964 { 3965 typedef poisson_distribution<_IntType> distribution_type; 3966 friend class poisson_distribution<_IntType>; 3967 3968 explicit 3969 param_type(double __mean = 1.0) 3970 : _M_mean(__mean) 3971 { 3972 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0); 3973 _M_initialize(); 3974 } 3975 3976 double 3977 mean() const 3978 { return _M_mean; } 3979 3980 friend bool 3981 operator==(const param_type& __p1, const param_type& __p2) 3982 { return __p1._M_mean == __p2._M_mean; } 3983 3984 private: 3985 // Hosts either log(mean) or the threshold of the simple method. 3986 void 3987 _M_initialize(); 3988 3989 double _M_mean; 3990 3991 double _M_lm_thr; 3992 #if _GLIBCXX_USE_C99_MATH_TR1 3993 double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb; 3994 #endif 3995 }; 3996 3997 // constructors and member function 3998 explicit 3999 poisson_distribution(double __mean = 1.0) 4000 : _M_param(__mean), _M_nd() 4001 { } 4002 4003 explicit 4004 poisson_distribution(const param_type& __p) 4005 : _M_param(__p), _M_nd() 4006 { } 4007 4008 /** 4009 * @brief Resets the distribution state. 4010 */ 4011 void 4012 reset() 4013 { _M_nd.reset(); } 4014 4015 /** 4016 * @brief Returns the distribution parameter @p mean. 4017 */ 4018 double 4019 mean() const 4020 { return _M_param.mean(); } 4021 4022 /** 4023 * @brief Returns the parameter set of the distribution. 4024 */ 4025 param_type 4026 param() const 4027 { return _M_param; } 4028 4029 /** 4030 * @brief Sets the parameter set of the distribution. 4031 * @param __param The new parameter set of the distribution. 4032 */ 4033 void 4034 param(const param_type& __param) 4035 { _M_param = __param; } 4036 4037 /** 4038 * @brief Returns the greatest lower bound value of the distribution. 4039 */ 4040 result_type 4041 min() const 4042 { return 0; } 4043 4044 /** 4045 * @brief Returns the least upper bound value of the distribution. 4046 */ 4047 result_type 4048 max() const 4049 { return std::numeric_limits<result_type>::max(); } 4050 4051 /** 4052 * @brief Generating functions. 4053 */ 4054 template<typename _UniformRandomNumberGenerator> 4055 result_type 4056 operator()(_UniformRandomNumberGenerator& __urng) 4057 { return this->operator()(__urng, _M_param); } 4058 4059 template<typename _UniformRandomNumberGenerator> 4060 result_type 4061 operator()(_UniformRandomNumberGenerator& __urng, 4062 const param_type& __p); 4063 4064 /** 4065 * @brief Return true if two Poisson distributions have the same 4066 * parameters and the sequences that would be generated 4067 * are equal. 4068 */ 4069 friend bool 4070 operator==(const poisson_distribution& __d1, 4071 const poisson_distribution& __d2) 4072 #ifdef _GLIBCXX_USE_C99_MATH_TR1 4073 { return __d1._M_param == __d2._M_param && __d1._M_nd == __d2._M_nd; } 4074 #else 4075 { return __d1._M_param == __d2._M_param; } 4076 #endif 4077 4078 /** 4079 * @brief Inserts a %poisson_distribution random number distribution 4080 * @p __x into the output stream @p __os. 4081 * 4082 * @param __os An output stream. 4083 * @param __x A %poisson_distribution random number distribution. 4084 * 4085 * @returns The output stream with the state of @p __x inserted or in 4086 * an error state. 4087 */ 4088 template<typename _IntType1, typename _CharT, typename _Traits> 4089 friend std::basic_ostream<_CharT, _Traits>& 4090 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 4091 const std::poisson_distribution<_IntType1>& __x); 4092 4093 /** 4094 * @brief Extracts a %poisson_distribution random number distribution 4095 * @p __x from the input stream @p __is. 4096 * 4097 * @param __is An input stream. 4098 * @param __x A %poisson_distribution random number generator engine. 4099 * 4100 * @returns The input stream with @p __x extracted or in an error 4101 * state. 4102 */ 4103 template<typename _IntType1, typename _CharT, typename _Traits> 4104 friend std::basic_istream<_CharT, _Traits>& 4105 operator>>(std::basic_istream<_CharT, _Traits>& __is, 4106 std::poisson_distribution<_IntType1>& __x); 4107 4108 private: 4109 param_type _M_param; 4110 4111 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined. 4112 std::normal_distribution<double> _M_nd; 4113 }; 4114 4115 /** 4116 * @brief Return true if two Poisson distributions are different. 4117 */ 4118 template<typename _IntType> 4119 inline bool 4120 operator!=(const std::poisson_distribution<_IntType>& __d1, 4121 const std::poisson_distribution<_IntType>& __d2) 4122 { return !(__d1 == __d2); } 4123 4124 4125 /** 4126 * @brief An exponential continuous distribution for random numbers. 4127 * 4128 * The formula for the exponential probability density function is 4129 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$. 4130 * 4131 * <table border=1 cellpadding=10 cellspacing=0> 4132 * <caption align=top>Distribution Statistics</caption> 4133 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr> 4134 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr> 4135 * <tr><td>Mode</td><td>@f$zero@f$</td></tr> 4136 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr> 4137 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr> 4138 * </table> 4139 */ 4140 template<typename _RealType = double> 4141 class exponential_distribution 4142 { 4143 static_assert(std::is_floating_point<_RealType>::value, 4144 "template argument not a floating point type"); 4145 4146 public: 4147 /** The type of the range of the distribution. */ 4148 typedef _RealType result_type; 4149 /** Parameter type. */ 4150 struct param_type 4151 { 4152 typedef exponential_distribution<_RealType> distribution_type; 4153 4154 explicit 4155 param_type(_RealType __lambda = _RealType(1)) 4156 : _M_lambda(__lambda) 4157 { 4158 _GLIBCXX_DEBUG_ASSERT(_M_lambda > _RealType(0)); 4159 } 4160 4161 _RealType 4162 lambda() const 4163 { return _M_lambda; } 4164 4165 friend bool 4166 operator==(const param_type& __p1, const param_type& __p2) 4167 { return __p1._M_lambda == __p2._M_lambda; } 4168 4169 private: 4170 _RealType _M_lambda; 4171 }; 4172 4173 public: 4174 /** 4175 * @brief Constructs an exponential distribution with inverse scale 4176 * parameter @f$\lambda@f$. 4177 */ 4178 explicit 4179 exponential_distribution(const result_type& __lambda = result_type(1)) 4180 : _M_param(__lambda) 4181 { } 4182 4183 explicit 4184 exponential_distribution(const param_type& __p) 4185 : _M_param(__p) 4186 { } 4187 4188 /** 4189 * @brief Resets the distribution state. 4190 * 4191 * Has no effect on exponential distributions. 4192 */ 4193 void 4194 reset() { } 4195 4196 /** 4197 * @brief Returns the inverse scale parameter of the distribution. 4198 */ 4199 _RealType 4200 lambda() const 4201 { return _M_param.lambda(); } 4202 4203 /** 4204 * @brief Returns the parameter set of the distribution. 4205 */ 4206 param_type 4207 param() const 4208 { return _M_param; } 4209 4210 /** 4211 * @brief Sets the parameter set of the distribution. 4212 * @param __param The new parameter set of the distribution. 4213 */ 4214 void 4215 param(const param_type& __param) 4216 { _M_param = __param; } 4217 4218 /** 4219 * @brief Returns the greatest lower bound value of the distribution. 4220 */ 4221 result_type 4222 min() const 4223 { return result_type(0); } 4224 4225 /** 4226 * @brief Returns the least upper bound value of the distribution. 4227 */ 4228 result_type 4229 max() const 4230 { return std::numeric_limits<result_type>::max(); } 4231 4232 /** 4233 * @brief Generating functions. 4234 */ 4235 template<typename _UniformRandomNumberGenerator> 4236 result_type 4237 operator()(_UniformRandomNumberGenerator& __urng) 4238 { return this->operator()(__urng, _M_param); } 4239 4240 template<typename _UniformRandomNumberGenerator> 4241 result_type 4242 operator()(_UniformRandomNumberGenerator& __urng, 4243 const param_type& __p) 4244 { 4245 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> 4246 __aurng(__urng); 4247 return -std::log(result_type(1) - __aurng()) / __p.lambda(); 4248 } 4249 4250 /** 4251 * @brief Return true if two exponential distributions have the same 4252 * parameters. 4253 */ 4254 friend bool 4255 operator==(const exponential_distribution& __d1, 4256 const exponential_distribution& __d2) 4257 { return __d1._M_param == __d2._M_param; } 4258 4259 private: 4260 param_type _M_param; 4261 }; 4262 4263 /** 4264 * @brief Return true if two exponential distributions have different 4265 * parameters. 4266 */ 4267 template<typename _RealType> 4268 inline bool 4269 operator!=(const std::exponential_distribution<_RealType>& __d1, 4270 const std::exponential_distribution<_RealType>& __d2) 4271 { return !(__d1 == __d2); } 4272 4273 /** 4274 * @brief Inserts a %exponential_distribution random number distribution 4275 * @p __x into the output stream @p __os. 4276 * 4277 * @param __os An output stream. 4278 * @param __x A %exponential_distribution random number distribution. 4279 * 4280 * @returns The output stream with the state of @p __x inserted or in 4281 * an error state. 4282 */ 4283 template<typename _RealType, typename _CharT, typename _Traits> 4284 std::basic_ostream<_CharT, _Traits>& 4285 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 4286 const std::exponential_distribution<_RealType>& __x); 4287 4288 /** 4289 * @brief Extracts a %exponential_distribution random number distribution 4290 * @p __x from the input stream @p __is. 4291 * 4292 * @param __is An input stream. 4293 * @param __x A %exponential_distribution random number 4294 * generator engine. 4295 * 4296 * @returns The input stream with @p __x extracted or in an error state. 4297 */ 4298 template<typename _RealType, typename _CharT, typename _Traits> 4299 std::basic_istream<_CharT, _Traits>& 4300 operator>>(std::basic_istream<_CharT, _Traits>& __is, 4301 std::exponential_distribution<_RealType>& __x); 4302 4303 4304 /** 4305 * @brief A weibull_distribution random number distribution. 4306 * 4307 * The formula for the normal probability density function is: 4308 * @f[ 4309 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1} 4310 * \exp{(-(\frac{x}{\beta})^\alpha)} 4311 * @f] 4312 */ 4313 template<typename _RealType = double> 4314 class weibull_distribution 4315 { 4316 static_assert(std::is_floating_point<_RealType>::value, 4317 "template argument not a floating point type"); 4318 4319 public: 4320 /** The type of the range of the distribution. */ 4321 typedef _RealType result_type; 4322 /** Parameter type. */ 4323 struct param_type 4324 { 4325 typedef weibull_distribution<_RealType> distribution_type; 4326 4327 explicit 4328 param_type(_RealType __a = _RealType(1), 4329 _RealType __b = _RealType(1)) 4330 : _M_a(__a), _M_b(__b) 4331 { } 4332 4333 _RealType 4334 a() const 4335 { return _M_a; } 4336 4337 _RealType 4338 b() const 4339 { return _M_b; } 4340 4341 friend bool 4342 operator==(const param_type& __p1, const param_type& __p2) 4343 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } 4344 4345 private: 4346 _RealType _M_a; 4347 _RealType _M_b; 4348 }; 4349 4350 explicit 4351 weibull_distribution(_RealType __a = _RealType(1), 4352 _RealType __b = _RealType(1)) 4353 : _M_param(__a, __b) 4354 { } 4355 4356 explicit 4357 weibull_distribution(const param_type& __p) 4358 : _M_param(__p) 4359 { } 4360 4361 /** 4362 * @brief Resets the distribution state. 4363 */ 4364 void 4365 reset() 4366 { } 4367 4368 /** 4369 * @brief Return the @f$a@f$ parameter of the distribution. 4370 */ 4371 _RealType 4372 a() const 4373 { return _M_param.a(); } 4374 4375 /** 4376 * @brief Return the @f$b@f$ parameter of the distribution. 4377 */ 4378 _RealType 4379 b() const 4380 { return _M_param.b(); } 4381 4382 /** 4383 * @brief Returns the parameter set of the distribution. 4384 */ 4385 param_type 4386 param() const 4387 { return _M_param; } 4388 4389 /** 4390 * @brief Sets the parameter set of the distribution. 4391 * @param __param The new parameter set of the distribution. 4392 */ 4393 void 4394 param(const param_type& __param) 4395 { _M_param = __param; } 4396 4397 /** 4398 * @brief Returns the greatest lower bound value of the distribution. 4399 */ 4400 result_type 4401 min() const 4402 { return result_type(0); } 4403 4404 /** 4405 * @brief Returns the least upper bound value of the distribution. 4406 */ 4407 result_type 4408 max() const 4409 { return std::numeric_limits<result_type>::max(); } 4410 4411 /** 4412 * @brief Generating functions. 4413 */ 4414 template<typename _UniformRandomNumberGenerator> 4415 result_type 4416 operator()(_UniformRandomNumberGenerator& __urng) 4417 { return this->operator()(__urng, _M_param); } 4418 4419 template<typename _UniformRandomNumberGenerator> 4420 result_type 4421 operator()(_UniformRandomNumberGenerator& __urng, 4422 const param_type& __p); 4423 4424 /** 4425 * @brief Return true if two Weibull distributions have the same 4426 * parameters. 4427 */ 4428 friend bool 4429 operator==(const weibull_distribution& __d1, 4430 const weibull_distribution& __d2) 4431 { return __d1._M_param == __d2._M_param; } 4432 4433 private: 4434 param_type _M_param; 4435 }; 4436 4437 /** 4438 * @brief Return true if two Weibull distributions have different 4439 * parameters. 4440 */ 4441 template<typename _RealType> 4442 inline bool 4443 operator!=(const std::weibull_distribution<_RealType>& __d1, 4444 const std::weibull_distribution<_RealType>& __d2) 4445 { return !(__d1 == __d2); } 4446 4447 /** 4448 * @brief Inserts a %weibull_distribution random number distribution 4449 * @p __x into the output stream @p __os. 4450 * 4451 * @param __os An output stream. 4452 * @param __x A %weibull_distribution random number distribution. 4453 * 4454 * @returns The output stream with the state of @p __x inserted or in 4455 * an error state. 4456 */ 4457 template<typename _RealType, typename _CharT, typename _Traits> 4458 std::basic_ostream<_CharT, _Traits>& 4459 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 4460 const std::weibull_distribution<_RealType>& __x); 4461 4462 /** 4463 * @brief Extracts a %weibull_distribution random number distribution 4464 * @p __x from the input stream @p __is. 4465 * 4466 * @param __is An input stream. 4467 * @param __x A %weibull_distribution random number 4468 * generator engine. 4469 * 4470 * @returns The input stream with @p __x extracted or in an error state. 4471 */ 4472 template<typename _RealType, typename _CharT, typename _Traits> 4473 std::basic_istream<_CharT, _Traits>& 4474 operator>>(std::basic_istream<_CharT, _Traits>& __is, 4475 std::weibull_distribution<_RealType>& __x); 4476 4477 4478 /** 4479 * @brief A extreme_value_distribution random number distribution. 4480 * 4481 * The formula for the normal probability mass function is 4482 * @f[ 4483 * p(x|a,b) = \frac{1}{b} 4484 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) 4485 * @f] 4486 */ 4487 template<typename _RealType = double> 4488 class extreme_value_distribution 4489 { 4490 static_assert(std::is_floating_point<_RealType>::value, 4491 "template argument not a floating point type"); 4492 4493 public: 4494 /** The type of the range of the distribution. */ 4495 typedef _RealType result_type; 4496 /** Parameter type. */ 4497 struct param_type 4498 { 4499 typedef extreme_value_distribution<_RealType> distribution_type; 4500 4501 explicit 4502 param_type(_RealType __a = _RealType(0), 4503 _RealType __b = _RealType(1)) 4504 : _M_a(__a), _M_b(__b) 4505 { } 4506 4507 _RealType 4508 a() const 4509 { return _M_a; } 4510 4511 _RealType 4512 b() const 4513 { return _M_b; } 4514 4515 friend bool 4516 operator==(const param_type& __p1, const param_type& __p2) 4517 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } 4518 4519 private: 4520 _RealType _M_a; 4521 _RealType _M_b; 4522 }; 4523 4524 explicit 4525 extreme_value_distribution(_RealType __a = _RealType(0), 4526 _RealType __b = _RealType(1)) 4527 : _M_param(__a, __b) 4528 { } 4529 4530 explicit 4531 extreme_value_distribution(const param_type& __p) 4532 : _M_param(__p) 4533 { } 4534 4535 /** 4536 * @brief Resets the distribution state. 4537 */ 4538 void 4539 reset() 4540 { } 4541 4542 /** 4543 * @brief Return the @f$a@f$ parameter of the distribution. 4544 */ 4545 _RealType 4546 a() const 4547 { return _M_param.a(); } 4548 4549 /** 4550 * @brief Return the @f$b@f$ parameter of the distribution. 4551 */ 4552 _RealType 4553 b() const 4554 { return _M_param.b(); } 4555 4556 /** 4557 * @brief Returns the parameter set of the distribution. 4558 */ 4559 param_type 4560 param() const 4561 { return _M_param; } 4562 4563 /** 4564 * @brief Sets the parameter set of the distribution. 4565 * @param __param The new parameter set of the distribution. 4566 */ 4567 void 4568 param(const param_type& __param) 4569 { _M_param = __param; } 4570 4571 /** 4572 * @brief Returns the greatest lower bound value of the distribution. 4573 */ 4574 result_type 4575 min() const 4576 { return std::numeric_limits<result_type>::min(); } 4577 4578 /** 4579 * @brief Returns the least upper bound value of the distribution. 4580 */ 4581 result_type 4582 max() const 4583 { return std::numeric_limits<result_type>::max(); } 4584 4585 /** 4586 * @brief Generating functions. 4587 */ 4588 template<typename _UniformRandomNumberGenerator> 4589 result_type 4590 operator()(_UniformRandomNumberGenerator& __urng) 4591 { return this->operator()(__urng, _M_param); } 4592 4593 template<typename _UniformRandomNumberGenerator> 4594 result_type 4595 operator()(_UniformRandomNumberGenerator& __urng, 4596 const param_type& __p); 4597 4598 /** 4599 * @brief Return true if two extreme value distributions have the same 4600 * parameters. 4601 */ 4602 friend bool 4603 operator==(const extreme_value_distribution& __d1, 4604 const extreme_value_distribution& __d2) 4605 { return __d1._M_param == __d2._M_param; } 4606 4607 private: 4608 param_type _M_param; 4609 }; 4610 4611 /** 4612 * @brief Return true if two extreme value distributions have different 4613 * parameters. 4614 */ 4615 template<typename _RealType> 4616 inline bool 4617 operator!=(const std::extreme_value_distribution<_RealType>& __d1, 4618 const std::extreme_value_distribution<_RealType>& __d2) 4619 { return !(__d1 == __d2); } 4620 4621 /** 4622 * @brief Inserts a %extreme_value_distribution random number distribution 4623 * @p __x into the output stream @p __os. 4624 * 4625 * @param __os An output stream. 4626 * @param __x A %extreme_value_distribution random number distribution. 4627 * 4628 * @returns The output stream with the state of @p __x inserted or in 4629 * an error state. 4630 */ 4631 template<typename _RealType, typename _CharT, typename _Traits> 4632 std::basic_ostream<_CharT, _Traits>& 4633 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 4634 const std::extreme_value_distribution<_RealType>& __x); 4635 4636 /** 4637 * @brief Extracts a %extreme_value_distribution random number 4638 * distribution @p __x from the input stream @p __is. 4639 * 4640 * @param __is An input stream. 4641 * @param __x A %extreme_value_distribution random number 4642 * generator engine. 4643 * 4644 * @returns The input stream with @p __x extracted or in an error state. 4645 */ 4646 template<typename _RealType, typename _CharT, typename _Traits> 4647 std::basic_istream<_CharT, _Traits>& 4648 operator>>(std::basic_istream<_CharT, _Traits>& __is, 4649 std::extreme_value_distribution<_RealType>& __x); 4650 4651 4652 /** 4653 * @brief A discrete_distribution random number distribution. 4654 * 4655 * The formula for the discrete probability mass function is 4656 * 4657 */ 4658 template<typename _IntType = int> 4659 class discrete_distribution 4660 { 4661 static_assert(std::is_integral<_IntType>::value, 4662 "template argument not an integral type"); 4663 4664 public: 4665 /** The type of the range of the distribution. */ 4666 typedef _IntType result_type; 4667 /** Parameter type. */ 4668 struct param_type 4669 { 4670 typedef discrete_distribution<_IntType> distribution_type; 4671 friend class discrete_distribution<_IntType>; 4672 4673 param_type() 4674 : _M_prob(), _M_cp() 4675 { } 4676 4677 template<typename _InputIterator> 4678 param_type(_InputIterator __wbegin, 4679 _InputIterator __wend) 4680 : _M_prob(__wbegin, __wend), _M_cp() 4681 { _M_initialize(); } 4682 4683 param_type(initializer_list<double> __wil) 4684 : _M_prob(__wil.begin(), __wil.end()), _M_cp() 4685 { _M_initialize(); } 4686 4687 template<typename _Func> 4688 param_type(size_t __nw, double __xmin, double __xmax, 4689 _Func __fw); 4690 4691 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/ 4692 param_type(const param_type&) = default; 4693 param_type& operator=(const param_type&) = default; 4694 4695 std::vector<double> 4696 probabilities() const 4697 { return _M_prob.empty() ? std::vector<double>(1, 1.0) : _M_prob; } 4698 4699 friend bool 4700 operator==(const param_type& __p1, const param_type& __p2) 4701 { return __p1._M_prob == __p2._M_prob; } 4702 4703 private: 4704 void 4705 _M_initialize(); 4706 4707 std::vector<double> _M_prob; 4708 std::vector<double> _M_cp; 4709 }; 4710 4711 discrete_distribution() 4712 : _M_param() 4713 { } 4714 4715 template<typename _InputIterator> 4716 discrete_distribution(_InputIterator __wbegin, 4717 _InputIterator __wend) 4718 : _M_param(__wbegin, __wend) 4719 { } 4720 4721 discrete_distribution(initializer_list<double> __wl) 4722 : _M_param(__wl) 4723 { } 4724 4725 template<typename _Func> 4726 discrete_distribution(size_t __nw, double __xmin, double __xmax, 4727 _Func __fw) 4728 : _M_param(__nw, __xmin, __xmax, __fw) 4729 { } 4730 4731 explicit 4732 discrete_distribution(const param_type& __p) 4733 : _M_param(__p) 4734 { } 4735 4736 /** 4737 * @brief Resets the distribution state. 4738 */ 4739 void 4740 reset() 4741 { } 4742 4743 /** 4744 * @brief Returns the probabilities of the distribution. 4745 */ 4746 std::vector<double> 4747 probabilities() const 4748 { 4749 return _M_param._M_prob.empty() 4750 ? std::vector<double>(1, 1.0) : _M_param._M_prob; 4751 } 4752 4753 /** 4754 * @brief Returns the parameter set of the distribution. 4755 */ 4756 param_type 4757 param() const 4758 { return _M_param; } 4759 4760 /** 4761 * @brief Sets the parameter set of the distribution. 4762 * @param __param The new parameter set of the distribution. 4763 */ 4764 void 4765 param(const param_type& __param) 4766 { _M_param = __param; } 4767 4768 /** 4769 * @brief Returns the greatest lower bound value of the distribution. 4770 */ 4771 result_type 4772 min() const 4773 { return result_type(0); } 4774 4775 /** 4776 * @brief Returns the least upper bound value of the distribution. 4777 */ 4778 result_type 4779 max() const 4780 { 4781 return _M_param._M_prob.empty() 4782 ? result_type(0) : result_type(_M_param._M_prob.size() - 1); 4783 } 4784 4785 /** 4786 * @brief Generating functions. 4787 */ 4788 template<typename _UniformRandomNumberGenerator> 4789 result_type 4790 operator()(_UniformRandomNumberGenerator& __urng) 4791 { return this->operator()(__urng, _M_param); } 4792 4793 template<typename _UniformRandomNumberGenerator> 4794 result_type 4795 operator()(_UniformRandomNumberGenerator& __urng, 4796 const param_type& __p); 4797 4798 /** 4799 * @brief Return true if two discrete distributions have the same 4800 * parameters. 4801 */ 4802 friend bool 4803 operator==(const discrete_distribution& __d1, 4804 const discrete_distribution& __d2) 4805 { return __d1._M_param == __d2._M_param; } 4806 4807 /** 4808 * @brief Inserts a %discrete_distribution random number distribution 4809 * @p __x into the output stream @p __os. 4810 * 4811 * @param __os An output stream. 4812 * @param __x A %discrete_distribution random number distribution. 4813 * 4814 * @returns The output stream with the state of @p __x inserted or in 4815 * an error state. 4816 */ 4817 template<typename _IntType1, typename _CharT, typename _Traits> 4818 friend std::basic_ostream<_CharT, _Traits>& 4819 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 4820 const std::discrete_distribution<_IntType1>& __x); 4821 4822 /** 4823 * @brief Extracts a %discrete_distribution random number distribution 4824 * @p __x from the input stream @p __is. 4825 * 4826 * @param __is An input stream. 4827 * @param __x A %discrete_distribution random number 4828 * generator engine. 4829 * 4830 * @returns The input stream with @p __x extracted or in an error 4831 * state. 4832 */ 4833 template<typename _IntType1, typename _CharT, typename _Traits> 4834 friend std::basic_istream<_CharT, _Traits>& 4835 operator>>(std::basic_istream<_CharT, _Traits>& __is, 4836 std::discrete_distribution<_IntType1>& __x); 4837 4838 private: 4839 param_type _M_param; 4840 }; 4841 4842 /** 4843 * @brief Return true if two discrete distributions have different 4844 * parameters. 4845 */ 4846 template<typename _IntType> 4847 inline bool 4848 operator!=(const std::discrete_distribution<_IntType>& __d1, 4849 const std::discrete_distribution<_IntType>& __d2) 4850 { return !(__d1 == __d2); } 4851 4852 4853 /** 4854 * @brief A piecewise_constant_distribution random number distribution. 4855 * 4856 * The formula for the piecewise constant probability mass function is 4857 * 4858 */ 4859 template<typename _RealType = double> 4860 class piecewise_constant_distribution 4861 { 4862 static_assert(std::is_floating_point<_RealType>::value, 4863 "template argument not a floating point type"); 4864 4865 public: 4866 /** The type of the range of the distribution. */ 4867 typedef _RealType result_type; 4868 /** Parameter type. */ 4869 struct param_type 4870 { 4871 typedef piecewise_constant_distribution<_RealType> distribution_type; 4872 friend class piecewise_constant_distribution<_RealType>; 4873 4874 param_type() 4875 : _M_int(), _M_den(), _M_cp() 4876 { } 4877 4878 template<typename _InputIteratorB, typename _InputIteratorW> 4879 param_type(_InputIteratorB __bfirst, 4880 _InputIteratorB __bend, 4881 _InputIteratorW __wbegin); 4882 4883 template<typename _Func> 4884 param_type(initializer_list<_RealType> __bi, _Func __fw); 4885 4886 template<typename _Func> 4887 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, 4888 _Func __fw); 4889 4890 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/ 4891 param_type(const param_type&) = default; 4892 param_type& operator=(const param_type&) = default; 4893 4894 std::vector<_RealType> 4895 intervals() const 4896 { 4897 if (_M_int.empty()) 4898 { 4899 std::vector<_RealType> __tmp(2); 4900 __tmp[1] = _RealType(1); 4901 return __tmp; 4902 } 4903 else 4904 return _M_int; 4905 } 4906 4907 std::vector<double> 4908 densities() const 4909 { return _M_den.empty() ? std::vector<double>(1, 1.0) : _M_den; } 4910 4911 friend bool 4912 operator==(const param_type& __p1, const param_type& __p2) 4913 { return __p1._M_int == __p2._M_int && __p1._M_den == __p2._M_den; } 4914 4915 private: 4916 void 4917 _M_initialize(); 4918 4919 std::vector<_RealType> _M_int; 4920 std::vector<double> _M_den; 4921 std::vector<double> _M_cp; 4922 }; 4923 4924 explicit 4925 piecewise_constant_distribution() 4926 : _M_param() 4927 { } 4928 4929 template<typename _InputIteratorB, typename _InputIteratorW> 4930 piecewise_constant_distribution(_InputIteratorB __bfirst, 4931 _InputIteratorB __bend, 4932 _InputIteratorW __wbegin) 4933 : _M_param(__bfirst, __bend, __wbegin) 4934 { } 4935 4936 template<typename _Func> 4937 piecewise_constant_distribution(initializer_list<_RealType> __bl, 4938 _Func __fw) 4939 : _M_param(__bl, __fw) 4940 { } 4941 4942 template<typename _Func> 4943 piecewise_constant_distribution(size_t __nw, 4944 _RealType __xmin, _RealType __xmax, 4945 _Func __fw) 4946 : _M_param(__nw, __xmin, __xmax, __fw) 4947 { } 4948 4949 explicit 4950 piecewise_constant_distribution(const param_type& __p) 4951 : _M_param(__p) 4952 { } 4953 4954 /** 4955 * @brief Resets the distribution state. 4956 */ 4957 void 4958 reset() 4959 { } 4960 4961 /** 4962 * @brief Returns a vector of the intervals. 4963 */ 4964 std::vector<_RealType> 4965 intervals() const 4966 { 4967 if (_M_param._M_int.empty()) 4968 { 4969 std::vector<_RealType> __tmp(2); 4970 __tmp[1] = _RealType(1); 4971 return __tmp; 4972 } 4973 else 4974 return _M_param._M_int; 4975 } 4976 4977 /** 4978 * @brief Returns a vector of the probability densities. 4979 */ 4980 std::vector<double> 4981 densities() const 4982 { 4983 return _M_param._M_den.empty() 4984 ? std::vector<double>(1, 1.0) : _M_param._M_den; 4985 } 4986 4987 /** 4988 * @brief Returns the parameter set of the distribution. 4989 */ 4990 param_type 4991 param() const 4992 { return _M_param; } 4993 4994 /** 4995 * @brief Sets the parameter set of the distribution. 4996 * @param __param The new parameter set of the distribution. 4997 */ 4998 void 4999 param(const param_type& __param) 5000 { _M_param = __param; } 5001 5002 /** 5003 * @brief Returns the greatest lower bound value of the distribution. 5004 */ 5005 result_type 5006 min() const 5007 { 5008 return _M_param._M_int.empty() 5009 ? result_type(0) : _M_param._M_int.front(); 5010 } 5011 5012 /** 5013 * @brief Returns the least upper bound value of the distribution. 5014 */ 5015 result_type 5016 max() const 5017 { 5018 return _M_param._M_int.empty() 5019 ? result_type(1) : _M_param._M_int.back(); 5020 } 5021 5022 /** 5023 * @brief Generating functions. 5024 */ 5025 template<typename _UniformRandomNumberGenerator> 5026 result_type 5027 operator()(_UniformRandomNumberGenerator& __urng) 5028 { return this->operator()(__urng, _M_param); } 5029 5030 template<typename _UniformRandomNumberGenerator> 5031 result_type 5032 operator()(_UniformRandomNumberGenerator& __urng, 5033 const param_type& __p); 5034 5035 /** 5036 * @brief Return true if two piecewise constant distributions have the 5037 * same parameters. 5038 */ 5039 friend bool 5040 operator==(const piecewise_constant_distribution& __d1, 5041 const piecewise_constant_distribution& __d2) 5042 { return __d1._M_param == __d2._M_param; } 5043 5044 /** 5045 * @brief Inserts a %piecewise_constan_distribution random 5046 * number distribution @p __x into the output stream @p __os. 5047 * 5048 * @param __os An output stream. 5049 * @param __x A %piecewise_constan_distribution random number 5050 * distribution. 5051 * 5052 * @returns The output stream with the state of @p __x inserted or in 5053 * an error state. 5054 */ 5055 template<typename _RealType1, typename _CharT, typename _Traits> 5056 friend std::basic_ostream<_CharT, _Traits>& 5057 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 5058 const std::piecewise_constant_distribution<_RealType1>& __x); 5059 5060 /** 5061 * @brief Extracts a %piecewise_constan_distribution random 5062 * number distribution @p __x from the input stream @p __is. 5063 * 5064 * @param __is An input stream. 5065 * @param __x A %piecewise_constan_distribution random number 5066 * generator engine. 5067 * 5068 * @returns The input stream with @p __x extracted or in an error 5069 * state. 5070 */ 5071 template<typename _RealType1, typename _CharT, typename _Traits> 5072 friend std::basic_istream<_CharT, _Traits>& 5073 operator>>(std::basic_istream<_CharT, _Traits>& __is, 5074 std::piecewise_constant_distribution<_RealType1>& __x); 5075 5076 private: 5077 param_type _M_param; 5078 }; 5079 5080 /** 5081 * @brief Return true if two piecewise constant distributions have 5082 * different parameters. 5083 */ 5084 template<typename _RealType> 5085 inline bool 5086 operator!=(const std::piecewise_constant_distribution<_RealType>& __d1, 5087 const std::piecewise_constant_distribution<_RealType>& __d2) 5088 { return !(__d1 == __d2); } 5089 5090 5091 /** 5092 * @brief A piecewise_linear_distribution random number distribution. 5093 * 5094 * The formula for the piecewise linear probability mass function is 5095 * 5096 */ 5097 template<typename _RealType = double> 5098 class piecewise_linear_distribution 5099 { 5100 static_assert(std::is_floating_point<_RealType>::value, 5101 "template argument not a floating point type"); 5102 5103 public: 5104 /** The type of the range of the distribution. */ 5105 typedef _RealType result_type; 5106 /** Parameter type. */ 5107 struct param_type 5108 { 5109 typedef piecewise_linear_distribution<_RealType> distribution_type; 5110 friend class piecewise_linear_distribution<_RealType>; 5111 5112 param_type() 5113 : _M_int(), _M_den(), _M_cp(), _M_m() 5114 { } 5115 5116 template<typename _InputIteratorB, typename _InputIteratorW> 5117 param_type(_InputIteratorB __bfirst, 5118 _InputIteratorB __bend, 5119 _InputIteratorW __wbegin); 5120 5121 template<typename _Func> 5122 param_type(initializer_list<_RealType> __bl, _Func __fw); 5123 5124 template<typename _Func> 5125 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, 5126 _Func __fw); 5127 5128 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/ 5129 param_type(const param_type&) = default; 5130 param_type& operator=(const param_type&) = default; 5131 5132 std::vector<_RealType> 5133 intervals() const 5134 { 5135 if (_M_int.empty()) 5136 { 5137 std::vector<_RealType> __tmp(2); 5138 __tmp[1] = _RealType(1); 5139 return __tmp; 5140 } 5141 else 5142 return _M_int; 5143 } 5144 5145 std::vector<double> 5146 densities() const 5147 { return _M_den.empty() ? std::vector<double>(2, 1.0) : _M_den; } 5148 5149 friend bool 5150 operator==(const param_type& __p1, const param_type& __p2) 5151 { return (__p1._M_int == __p2._M_int 5152 && __p1._M_den == __p2._M_den); } 5153 5154 private: 5155 void 5156 _M_initialize(); 5157 5158 std::vector<_RealType> _M_int; 5159 std::vector<double> _M_den; 5160 std::vector<double> _M_cp; 5161 std::vector<double> _M_m; 5162 }; 5163 5164 explicit 5165 piecewise_linear_distribution() 5166 : _M_param() 5167 { } 5168 5169 template<typename _InputIteratorB, typename _InputIteratorW> 5170 piecewise_linear_distribution(_InputIteratorB __bfirst, 5171 _InputIteratorB __bend, 5172 _InputIteratorW __wbegin) 5173 : _M_param(__bfirst, __bend, __wbegin) 5174 { } 5175 5176 template<typename _Func> 5177 piecewise_linear_distribution(initializer_list<_RealType> __bl, 5178 _Func __fw) 5179 : _M_param(__bl, __fw) 5180 { } 5181 5182 template<typename _Func> 5183 piecewise_linear_distribution(size_t __nw, 5184 _RealType __xmin, _RealType __xmax, 5185 _Func __fw) 5186 : _M_param(__nw, __xmin, __xmax, __fw) 5187 { } 5188 5189 explicit 5190 piecewise_linear_distribution(const param_type& __p) 5191 : _M_param(__p) 5192 { } 5193 5194 /** 5195 * Resets the distribution state. 5196 */ 5197 void 5198 reset() 5199 { } 5200 5201 /** 5202 * @brief Return the intervals of the distribution. 5203 */ 5204 std::vector<_RealType> 5205 intervals() const 5206 { 5207 if (_M_param._M_int.empty()) 5208 { 5209 std::vector<_RealType> __tmp(2); 5210 __tmp[1] = _RealType(1); 5211 return __tmp; 5212 } 5213 else 5214 return _M_param._M_int; 5215 } 5216 5217 /** 5218 * @brief Return a vector of the probability densities of the 5219 * distribution. 5220 */ 5221 std::vector<double> 5222 densities() const 5223 { 5224 return _M_param._M_den.empty() 5225 ? std::vector<double>(2, 1.0) : _M_param._M_den; 5226 } 5227 5228 /** 5229 * @brief Returns the parameter set of the distribution. 5230 */ 5231 param_type 5232 param() const 5233 { return _M_param; } 5234 5235 /** 5236 * @brief Sets the parameter set of the distribution. 5237 * @param __param The new parameter set of the distribution. 5238 */ 5239 void 5240 param(const param_type& __param) 5241 { _M_param = __param; } 5242 5243 /** 5244 * @brief Returns the greatest lower bound value of the distribution. 5245 */ 5246 result_type 5247 min() const 5248 { 5249 return _M_param._M_int.empty() 5250 ? result_type(0) : _M_param._M_int.front(); 5251 } 5252 5253 /** 5254 * @brief Returns the least upper bound value of the distribution. 5255 */ 5256 result_type 5257 max() const 5258 { 5259 return _M_param._M_int.empty() 5260 ? result_type(1) : _M_param._M_int.back(); 5261 } 5262 5263 /** 5264 * @brief Generating functions. 5265 */ 5266 template<typename _UniformRandomNumberGenerator> 5267 result_type 5268 operator()(_UniformRandomNumberGenerator& __urng) 5269 { return this->operator()(__urng, _M_param); } 5270 5271 template<typename _UniformRandomNumberGenerator> 5272 result_type 5273 operator()(_UniformRandomNumberGenerator& __urng, 5274 const param_type& __p); 5275 5276 /** 5277 * @brief Return true if two piecewise linear distributions have the 5278 * same parameters. 5279 */ 5280 friend bool 5281 operator==(const piecewise_linear_distribution& __d1, 5282 const piecewise_linear_distribution& __d2) 5283 { return __d1._M_param == __d2._M_param; } 5284 5285 /** 5286 * @brief Inserts a %piecewise_linear_distribution random number 5287 * distribution @p __x into the output stream @p __os. 5288 * 5289 * @param __os An output stream. 5290 * @param __x A %piecewise_linear_distribution random number 5291 * distribution. 5292 * 5293 * @returns The output stream with the state of @p __x inserted or in 5294 * an error state. 5295 */ 5296 template<typename _RealType1, typename _CharT, typename _Traits> 5297 friend std::basic_ostream<_CharT, _Traits>& 5298 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 5299 const std::piecewise_linear_distribution<_RealType1>& __x); 5300 5301 /** 5302 * @brief Extracts a %piecewise_linear_distribution random number 5303 * distribution @p __x from the input stream @p __is. 5304 * 5305 * @param __is An input stream. 5306 * @param __x A %piecewise_linear_distribution random number 5307 * generator engine. 5308 * 5309 * @returns The input stream with @p __x extracted or in an error 5310 * state. 5311 */ 5312 template<typename _RealType1, typename _CharT, typename _Traits> 5313 friend std::basic_istream<_CharT, _Traits>& 5314 operator>>(std::basic_istream<_CharT, _Traits>& __is, 5315 std::piecewise_linear_distribution<_RealType1>& __x); 5316 5317 private: 5318 param_type _M_param; 5319 }; 5320 5321 /** 5322 * @brief Return true if two piecewise linear distributions have 5323 * different parameters. 5324 */ 5325 template<typename _RealType> 5326 inline bool 5327 operator!=(const std::piecewise_linear_distribution<_RealType>& __d1, 5328 const std::piecewise_linear_distribution<_RealType>& __d2) 5329 { return !(__d1 == __d2); } 5330 5331 5332 /* @} */ // group random_distributions_poisson 5333 5334 /* @} */ // group random_distributions 5335 5336 /** 5337 * @addtogroup random_utilities Random Number Utilities 5338 * @ingroup random 5339 * @{ 5340 */ 5341 5342 /** 5343 * @brief The seed_seq class generates sequences of seeds for random 5344 * number generators. 5345 */ 5346 class seed_seq 5347 { 5348 5349 public: 5350 /** The type of the seed vales. */ 5351 typedef uint_least32_t result_type; 5352 5353 /** Default constructor. */ 5354 seed_seq() 5355 : _M_v() 5356 { } 5357 5358 template<typename _IntType> 5359 seed_seq(std::initializer_list<_IntType> il); 5360 5361 template<typename _InputIterator> 5362 seed_seq(_InputIterator __begin, _InputIterator __end); 5363 5364 // generating functions 5365 template<typename _RandomAccessIterator> 5366 void 5367 generate(_RandomAccessIterator __begin, _RandomAccessIterator __end); 5368 5369 // property functions 5370 size_t size() const 5371 { return _M_v.size(); } 5372 5373 template<typename OutputIterator> 5374 void 5375 param(OutputIterator __dest) const 5376 { std::copy(_M_v.begin(), _M_v.end(), __dest); } 5377 5378 private: 5379 /// 5380 std::vector<result_type> _M_v; 5381 }; 5382 5383 /* @} */ // group random_utilities 5384 5385 /* @} */ // group random 5386 5387 _GLIBCXX_END_NAMESPACE_VERSION 5388 } // namespace std 5389 5390 #endif 5391