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