1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009-2015 Free Software Foundation, Inc.
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)
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.
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.
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/>.
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{random}
36 namespace std
_GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 // [26.4] Random number generation
43 * @defgroup random Random Number Generation
46 * A facility for generating random numbers on selected distributions.
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
55 template<typename _RealType
, size_t __bits
,
56 typename _UniformRandomNumberGenerator
>
58 generate_canonical(_UniformRandomNumberGenerator
& __g
);
60 _GLIBCXX_END_NAMESPACE_VERSION
63 * Implementation-space details.
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
69 template<typename _UIntType
, size_t __w
,
70 bool = __w
< static_cast<size_t>
71 (std::numeric_limits
<_UIntType
>::digits
)>
73 { static const _UIntType __value
= 0; };
75 template<typename _UIntType
, size_t __w
>
76 struct _Shift
<_UIntType
, __w
, true>
77 { static const _UIntType __value
= _UIntType(1) << __w
; };
80 int __which
= ((__s
<= __CHAR_BIT__
* sizeof (int))
81 + (__s
<= __CHAR_BIT__
* sizeof (long))
82 + (__s
<= __CHAR_BIT__
* sizeof (long long))
83 /* assume long long no bigger than __int128 */
85 struct _Select_uint_least_t
87 static_assert(__which
< 0, /* needs to be dependent */
88 "sorry, would be too much trouble for a slow result");
92 struct _Select_uint_least_t
<__s
, 4>
93 { typedef unsigned int type
; };
96 struct _Select_uint_least_t
<__s
, 3>
97 { typedef unsigned long type
; };
100 struct _Select_uint_least_t
<__s
, 2>
101 { typedef unsigned long long type
; };
103 #ifdef _GLIBCXX_USE_INT128
105 struct _Select_uint_least_t
<__s
, 1>
106 { typedef unsigned __int128 type
; };
109 // Assume a != 0, a < m, c < m, x < m.
110 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
,
111 bool __big_enough
= (!(__m
& (__m
- 1))
112 || (_Tp(-1) - __c
) / __a
>= __m
- 1),
113 bool __schrage_ok
= __m
% __a
< __m
/ __a
>
116 typedef typename _Select_uint_least_t
<std::__lg(__a
)
117 + std::__lg(__m
) + 2>::type _Tp2
;
120 { return static_cast<_Tp
>((_Tp2(__a
) * __x
+ __c
) % __m
); }
124 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
>
125 struct _Mod
<_Tp
, __m
, __a
, __c
, false, true>
132 // - for m == 2^n or m == 0, unsigned integer overflow is safe.
133 // - a * (m - 1) + c fits in _Tp, there is no overflow.
134 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool __s
>
135 struct _Mod
<_Tp
, __m
, __a
, __c
, true, __s
>
140 _Tp __res
= __a
* __x
+ __c
;
147 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
150 { return _Mod
<_Tp
, __m
, __a
, __c
>::__calc(__x
); }
152 /* Determine whether number is a power of 2. */
153 template<typename _Tp
>
157 return ((__x
- 1) & __x
) == 0;
161 * An adaptor class for converting the output of any Generator into
162 * the input for a specific Distribution.
164 template<typename _Engine
, typename _DInputType
>
167 static_assert(std::is_floating_point
<_DInputType
>::value
,
168 "template argument not a floating point type");
171 _Adaptor(_Engine
& __g
)
176 { return _DInputType(0); }
180 { return _DInputType(1); }
183 * Converts a value generated by the adapted random number generator
184 * into a value in the input domain for the dependent random number
190 return std::generate_canonical
<_DInputType
,
191 std::numeric_limits
<_DInputType
>::digits
,
199 _GLIBCXX_END_NAMESPACE_VERSION
200 } // namespace __detail
202 _GLIBCXX_BEGIN_NAMESPACE_VERSION
205 * @addtogroup random_generators Random Number Generators
208 * These classes define objects which provide random or pseudorandom
209 * numbers, either from a discrete or a continuous interval. The
210 * random number generator supplied as a part of this library are
211 * all uniform random number generators which provide a sequence of
212 * random number uniformly distributed over their range.
214 * A number generator is a function object with an operator() that
215 * takes zero arguments and returns a number.
217 * A compliant random number generator must satisfy the following
218 * requirements. <table border=1 cellpadding=10 cellspacing=0>
219 * <caption align=top>Random Number Generator Requirements</caption>
220 * <tr><td>To be documented.</td></tr> </table>
226 * @brief A model of a linear congruential random number generator.
228 * A random number generator that produces pseudorandom numbers via
231 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
234 * The template parameter @p _UIntType must be an unsigned integral type
235 * large enough to store values up to (__m-1). If the template parameter
236 * @p __m is 0, the modulus @p __m used is
237 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
238 * parameters @p __a and @p __c must be less than @p __m.
240 * The size of the state is @f$1@f$.
242 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
243 class linear_congruential_engine
245 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
246 "substituting _UIntType not an unsigned integral type");
247 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
248 "template argument substituting __m out of bounds");
251 /** The type of the generated random value. */
252 typedef _UIntType result_type
;
254 /** The multiplier. */
255 static constexpr result_type multiplier
= __a
;
257 static constexpr result_type increment
= __c
;
259 static constexpr result_type modulus
= __m
;
260 static constexpr result_type default_seed
= 1u;
263 * @brief Constructs a %linear_congruential_engine random number
264 * generator engine with seed @p __s. The default seed value
267 * @param __s The initial seed value.
270 linear_congruential_engine(result_type __s
= default_seed
)
274 * @brief Constructs a %linear_congruential_engine random number
275 * generator engine seeded from the seed sequence @p __q.
277 * @param __q the seed sequence.
279 template<typename _Sseq
, typename
= typename
280 std::enable_if
<!std::is_same
<_Sseq
, linear_congruential_engine
>::value
>
283 linear_congruential_engine(_Sseq
& __q
)
287 * @brief Reseeds the %linear_congruential_engine random number generator
288 * engine sequence to the seed @p __s.
290 * @param __s The new seed.
293 seed(result_type __s
= default_seed
);
296 * @brief Reseeds the %linear_congruential_engine random number generator
298 * sequence using values from the seed sequence @p __q.
300 * @param __q the seed sequence.
302 template<typename _Sseq
>
303 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
307 * @brief Gets the smallest possible value in the output range.
309 * The minimum depends on the @p __c parameter: if it is zero, the
310 * minimum generated must be > 0, otherwise 0 is allowed.
312 static constexpr result_type
314 { return __c
== 0u ? 1u : 0u; }
317 * @brief Gets the largest possible value in the output range.
319 static constexpr result_type
324 * @brief Discard a sequence of random numbers.
327 discard(unsigned long long __z
)
329 for (; __z
!= 0ULL; --__z
)
334 * @brief Gets the next random number in the sequence.
339 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
344 * @brief Compares two linear congruential random number generator
345 * objects of the same type for equality.
347 * @param __lhs A linear congruential random number generator object.
348 * @param __rhs Another linear congruential random number generator
351 * @returns true if the infinite sequences of generated values
352 * would be equal, false otherwise.
355 operator==(const linear_congruential_engine
& __lhs
,
356 const linear_congruential_engine
& __rhs
)
357 { return __lhs
._M_x
== __rhs
._M_x
; }
360 * @brief Writes the textual representation of the state x(i) of x to
363 * @param __os The output stream.
364 * @param __lcr A % linear_congruential_engine random number generator.
367 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
368 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
369 friend std::basic_ostream
<_CharT
, _Traits
>&
370 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
371 const std::linear_congruential_engine
<_UIntType1
,
372 __a1
, __c1
, __m1
>& __lcr
);
375 * @brief Sets the state of the engine by reading its textual
376 * representation from @p __is.
378 * The textual representation must have been previously written using
379 * an output stream whose imbued locale and whose type's template
380 * specialization arguments _CharT and _Traits were the same as those
383 * @param __is The input stream.
384 * @param __lcr A % linear_congruential_engine random number generator.
387 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
388 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
389 friend std::basic_istream
<_CharT
, _Traits
>&
390 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
391 std::linear_congruential_engine
<_UIntType1
, __a1
,
399 * @brief Compares two linear congruential random number generator
400 * objects of the same type for inequality.
402 * @param __lhs A linear congruential random number generator object.
403 * @param __rhs Another linear congruential random number generator
406 * @returns true if the infinite sequences of generated values
407 * would be different, false otherwise.
409 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
411 operator!=(const std::linear_congruential_engine
<_UIntType
, __a
,
413 const std::linear_congruential_engine
<_UIntType
, __a
,
415 { return !(__lhs
== __rhs
); }
419 * A generalized feedback shift register discrete random number generator.
421 * This algorithm avoids multiplication and division and is designed to be
422 * friendly to a pipelined architecture. If the parameters are chosen
423 * correctly, this generator will produce numbers with a very long period and
424 * fairly good apparent entropy, although still not cryptographically strong.
426 * The best way to use this generator is with the predefined mt19937 class.
428 * This algorithm was originally invented by Makoto Matsumoto and
431 * @tparam __w Word size, the number of bits in each element of
433 * @tparam __n The degree of recursion.
434 * @tparam __m The period parameter.
435 * @tparam __r The separation point bit index.
436 * @tparam __a The last row of the twist matrix.
437 * @tparam __u The first right-shift tempering matrix parameter.
438 * @tparam __d The first right-shift tempering matrix mask.
439 * @tparam __s The first left-shift tempering matrix parameter.
440 * @tparam __b The first left-shift tempering matrix mask.
441 * @tparam __t The second left-shift tempering matrix parameter.
442 * @tparam __c The second left-shift tempering matrix mask.
443 * @tparam __l The second right-shift tempering matrix parameter.
444 * @tparam __f Initialization multiplier.
446 template<typename _UIntType
, size_t __w
,
447 size_t __n
, size_t __m
, size_t __r
,
448 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
449 _UIntType __b
, size_t __t
,
450 _UIntType __c
, size_t __l
, _UIntType __f
>
451 class mersenne_twister_engine
453 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
454 "substituting _UIntType not an unsigned integral type");
455 static_assert(1u <= __m
&& __m
<= __n
,
456 "template argument substituting __m out of bounds");
457 static_assert(__r
<= __w
, "template argument substituting "
459 static_assert(__u
<= __w
, "template argument substituting "
461 static_assert(__s
<= __w
, "template argument substituting "
463 static_assert(__t
<= __w
, "template argument substituting "
465 static_assert(__l
<= __w
, "template argument substituting "
467 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
468 "template argument substituting __w out of bound");
469 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
470 "template argument substituting __a out of bound");
471 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
472 "template argument substituting __b out of bound");
473 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
474 "template argument substituting __c out of bound");
475 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
476 "template argument substituting __d out of bound");
477 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
478 "template argument substituting __f out of bound");
481 /** The type of the generated random value. */
482 typedef _UIntType result_type
;
485 static constexpr size_t word_size
= __w
;
486 static constexpr size_t state_size
= __n
;
487 static constexpr size_t shift_size
= __m
;
488 static constexpr size_t mask_bits
= __r
;
489 static constexpr result_type xor_mask
= __a
;
490 static constexpr size_t tempering_u
= __u
;
491 static constexpr result_type tempering_d
= __d
;
492 static constexpr size_t tempering_s
= __s
;
493 static constexpr result_type tempering_b
= __b
;
494 static constexpr size_t tempering_t
= __t
;
495 static constexpr result_type tempering_c
= __c
;
496 static constexpr size_t tempering_l
= __l
;
497 static constexpr result_type initialization_multiplier
= __f
;
498 static constexpr result_type default_seed
= 5489u;
500 // constructors and member function
502 mersenne_twister_engine(result_type __sd
= default_seed
)
506 * @brief Constructs a %mersenne_twister_engine random number generator
507 * engine seeded from the seed sequence @p __q.
509 * @param __q the seed sequence.
511 template<typename _Sseq
, typename
= typename
512 std::enable_if
<!std::is_same
<_Sseq
, mersenne_twister_engine
>::value
>
515 mersenne_twister_engine(_Sseq
& __q
)
519 seed(result_type __sd
= default_seed
);
521 template<typename _Sseq
>
522 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
526 * @brief Gets the smallest possible value in the output range.
528 static constexpr result_type
533 * @brief Gets the largest possible value in the output range.
535 static constexpr result_type
537 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
540 * @brief Discard a sequence of random numbers.
543 discard(unsigned long long __z
);
549 * @brief Compares two % mersenne_twister_engine random number generator
550 * objects of the same type for equality.
552 * @param __lhs A % mersenne_twister_engine random number generator
554 * @param __rhs Another % mersenne_twister_engine random number
557 * @returns true if the infinite sequences of generated values
558 * would be equal, false otherwise.
561 operator==(const mersenne_twister_engine
& __lhs
,
562 const mersenne_twister_engine
& __rhs
)
563 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
)
564 && __lhs
._M_p
== __rhs
._M_p
); }
567 * @brief Inserts the current state of a % mersenne_twister_engine
568 * random number generator engine @p __x into the output stream
571 * @param __os An output stream.
572 * @param __x A % mersenne_twister_engine random number generator
575 * @returns The output stream with the state of @p __x inserted or in
578 template<typename _UIntType1
,
579 size_t __w1
, size_t __n1
,
580 size_t __m1
, size_t __r1
,
581 _UIntType1 __a1
, size_t __u1
,
582 _UIntType1 __d1
, size_t __s1
,
583 _UIntType1 __b1
, size_t __t1
,
584 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
585 typename _CharT
, typename _Traits
>
586 friend std::basic_ostream
<_CharT
, _Traits
>&
587 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
588 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
589 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
593 * @brief Extracts the current state of a % mersenne_twister_engine
594 * random number generator engine @p __x from the input stream
597 * @param __is An input stream.
598 * @param __x A % mersenne_twister_engine random number generator
601 * @returns The input stream with the state of @p __x extracted or in
604 template<typename _UIntType1
,
605 size_t __w1
, size_t __n1
,
606 size_t __m1
, size_t __r1
,
607 _UIntType1 __a1
, size_t __u1
,
608 _UIntType1 __d1
, size_t __s1
,
609 _UIntType1 __b1
, size_t __t1
,
610 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
611 typename _CharT
, typename _Traits
>
612 friend std::basic_istream
<_CharT
, _Traits
>&
613 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
614 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
615 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
621 _UIntType _M_x
[state_size
];
626 * @brief Compares two % mersenne_twister_engine random number generator
627 * objects of the same type for inequality.
629 * @param __lhs A % mersenne_twister_engine random number generator
631 * @param __rhs Another % mersenne_twister_engine random number
634 * @returns true if the infinite sequences of generated values
635 * would be different, false otherwise.
637 template<typename _UIntType
, size_t __w
,
638 size_t __n
, size_t __m
, size_t __r
,
639 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
640 _UIntType __b
, size_t __t
,
641 _UIntType __c
, size_t __l
, _UIntType __f
>
643 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
644 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
645 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
646 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
647 { return !(__lhs
== __rhs
); }
651 * @brief The Marsaglia-Zaman generator.
653 * This is a model of a Generalized Fibonacci discrete random number
654 * generator, sometimes referred to as the SWC generator.
656 * A discrete random number generator that produces pseudorandom
659 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
662 * The size of the state is @f$r@f$
663 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
665 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
666 class subtract_with_carry_engine
668 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
669 "substituting _UIntType not an unsigned integral type");
670 static_assert(0u < __s
&& __s
< __r
,
671 "template argument substituting __s out of bounds");
672 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
673 "template argument substituting __w out of bounds");
676 /** The type of the generated random value. */
677 typedef _UIntType result_type
;
680 static constexpr size_t word_size
= __w
;
681 static constexpr size_t short_lag
= __s
;
682 static constexpr size_t long_lag
= __r
;
683 static constexpr result_type default_seed
= 19780503u;
686 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
687 * random number generator.
690 subtract_with_carry_engine(result_type __sd
= default_seed
)
694 * @brief Constructs a %subtract_with_carry_engine random number engine
695 * seeded from the seed sequence @p __q.
697 * @param __q the seed sequence.
699 template<typename _Sseq
, typename
= typename
700 std::enable_if
<!std::is_same
<_Sseq
, subtract_with_carry_engine
>::value
>
703 subtract_with_carry_engine(_Sseq
& __q
)
707 * @brief Seeds the initial state @f$x_0@f$ of the random number
710 * N1688[4.19] modifies this as follows. If @p __value == 0,
711 * sets value to 19780503. In any case, with a linear
712 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
713 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
714 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
715 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
716 * set carry to 1, otherwise sets carry to 0.
719 seed(result_type __sd
= default_seed
);
722 * @brief Seeds the initial state @f$x_0@f$ of the
723 * % subtract_with_carry_engine random number generator.
725 template<typename _Sseq
>
726 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
730 * @brief Gets the inclusive minimum value of the range of random
731 * integers returned by this generator.
733 static constexpr result_type
738 * @brief Gets the inclusive maximum value of the range of random
739 * integers returned by this generator.
741 static constexpr result_type
743 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
746 * @brief Discard a sequence of random numbers.
749 discard(unsigned long long __z
)
751 for (; __z
!= 0ULL; --__z
)
756 * @brief Gets the next random number in the sequence.
762 * @brief Compares two % subtract_with_carry_engine random number
763 * generator objects of the same type for equality.
765 * @param __lhs A % subtract_with_carry_engine random number generator
767 * @param __rhs Another % subtract_with_carry_engine random number
770 * @returns true if the infinite sequences of generated values
771 * would be equal, false otherwise.
774 operator==(const subtract_with_carry_engine
& __lhs
,
775 const subtract_with_carry_engine
& __rhs
)
776 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
)
777 && __lhs
._M_carry
== __rhs
._M_carry
778 && __lhs
._M_p
== __rhs
._M_p
); }
781 * @brief Inserts the current state of a % subtract_with_carry_engine
782 * random number generator engine @p __x into the output stream
785 * @param __os An output stream.
786 * @param __x A % subtract_with_carry_engine random number generator
789 * @returns The output stream with the state of @p __x inserted or in
792 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
793 typename _CharT
, typename _Traits
>
794 friend std::basic_ostream
<_CharT
, _Traits
>&
795 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
796 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
800 * @brief Extracts the current state of a % subtract_with_carry_engine
801 * random number generator engine @p __x from the input stream
804 * @param __is An input stream.
805 * @param __x A % subtract_with_carry_engine random number generator
808 * @returns The input stream with the state of @p __x extracted or in
811 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
812 typename _CharT
, typename _Traits
>
813 friend std::basic_istream
<_CharT
, _Traits
>&
814 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
815 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
819 /// The state of the generator. This is a ring buffer.
820 _UIntType _M_x
[long_lag
];
821 _UIntType _M_carry
; ///< The carry
822 size_t _M_p
; ///< Current index of x(i - r).
826 * @brief Compares two % subtract_with_carry_engine random number
827 * generator objects of the same type for inequality.
829 * @param __lhs A % subtract_with_carry_engine random number generator
831 * @param __rhs Another % subtract_with_carry_engine random number
834 * @returns true if the infinite sequences of generated values
835 * would be different, false otherwise.
837 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
839 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
841 const std::subtract_with_carry_engine
<_UIntType
, __w
,
843 { return !(__lhs
== __rhs
); }
847 * Produces random numbers from some base engine by discarding blocks of
850 * 0 <= @p __r <= @p __p
852 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
853 class discard_block_engine
855 static_assert(1 <= __r
&& __r
<= __p
,
856 "template argument substituting __r out of bounds");
859 /** The type of the generated random value. */
860 typedef typename
_RandomNumberEngine::result_type result_type
;
863 static constexpr size_t block_size
= __p
;
864 static constexpr size_t used_block
= __r
;
867 * @brief Constructs a default %discard_block_engine engine.
869 * The underlying engine is default constructed as well.
871 discard_block_engine()
872 : _M_b(), _M_n(0) { }
875 * @brief Copy constructs a %discard_block_engine engine.
877 * Copies an existing base class random number generator.
878 * @param __rng An existing (base class) engine object.
881 discard_block_engine(const _RandomNumberEngine
& __rng
)
882 : _M_b(__rng
), _M_n(0) { }
885 * @brief Move constructs a %discard_block_engine engine.
887 * Copies an existing base class random number generator.
888 * @param __rng An existing (base class) engine object.
891 discard_block_engine(_RandomNumberEngine
&& __rng
)
892 : _M_b(std::move(__rng
)), _M_n(0) { }
895 * @brief Seed constructs a %discard_block_engine engine.
897 * Constructs the underlying generator engine seeded with @p __s.
898 * @param __s A seed value for the base class engine.
901 discard_block_engine(result_type __s
)
902 : _M_b(__s
), _M_n(0) { }
905 * @brief Generator construct a %discard_block_engine engine.
907 * @param __q A seed sequence.
909 template<typename _Sseq
, typename
= typename
910 std::enable_if
<!std::is_same
<_Sseq
, discard_block_engine
>::value
911 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
914 discard_block_engine(_Sseq
& __q
)
919 * @brief Reseeds the %discard_block_engine object with the default
920 * seed for the underlying base class generator engine.
930 * @brief Reseeds the %discard_block_engine object with the default
931 * seed for the underlying base class generator engine.
934 seed(result_type __s
)
941 * @brief Reseeds the %discard_block_engine object with the given seed
943 * @param __q A seed generator function.
945 template<typename _Sseq
>
954 * @brief Gets a const reference to the underlying generator engine
957 const _RandomNumberEngine
&
958 base() const noexcept
962 * @brief Gets the minimum value in the generated random number range.
964 static constexpr result_type
966 { return _RandomNumberEngine::min(); }
969 * @brief Gets the maximum value in the generated random number range.
971 static constexpr result_type
973 { return _RandomNumberEngine::max(); }
976 * @brief Discard a sequence of random numbers.
979 discard(unsigned long long __z
)
981 for (; __z
!= 0ULL; --__z
)
986 * @brief Gets the next value in the generated random number sequence.
992 * @brief Compares two %discard_block_engine random number generator
993 * objects of the same type for equality.
995 * @param __lhs A %discard_block_engine random number generator object.
996 * @param __rhs Another %discard_block_engine random number generator
999 * @returns true if the infinite sequences of generated values
1000 * would be equal, false otherwise.
1003 operator==(const discard_block_engine
& __lhs
,
1004 const discard_block_engine
& __rhs
)
1005 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
1008 * @brief Inserts the current state of a %discard_block_engine random
1009 * number generator engine @p __x into the output stream
1012 * @param __os An output stream.
1013 * @param __x A %discard_block_engine random number generator engine.
1015 * @returns The output stream with the state of @p __x inserted or in
1018 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1019 typename _CharT
, typename _Traits
>
1020 friend std::basic_ostream
<_CharT
, _Traits
>&
1021 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1022 const std::discard_block_engine
<_RandomNumberEngine1
,
1026 * @brief Extracts the current state of a % subtract_with_carry_engine
1027 * random number generator engine @p __x from the input stream
1030 * @param __is An input stream.
1031 * @param __x A %discard_block_engine random number generator engine.
1033 * @returns The input stream with the state of @p __x extracted or in
1036 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1037 typename _CharT
, typename _Traits
>
1038 friend std::basic_istream
<_CharT
, _Traits
>&
1039 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1040 std::discard_block_engine
<_RandomNumberEngine1
,
1044 _RandomNumberEngine _M_b
;
1049 * @brief Compares two %discard_block_engine random number generator
1050 * objects of the same type for inequality.
1052 * @param __lhs A %discard_block_engine random number generator object.
1053 * @param __rhs Another %discard_block_engine random number generator
1056 * @returns true if the infinite sequences of generated values
1057 * would be different, false otherwise.
1059 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
1061 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1063 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1065 { return !(__lhs
== __rhs
); }
1069 * Produces random numbers by combining random numbers from some base
1070 * engine to produce random numbers with a specifies number of bits @p __w.
1072 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1073 class independent_bits_engine
1075 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
1076 "substituting _UIntType not an unsigned integral type");
1077 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
1078 "template argument substituting __w out of bounds");
1081 /** The type of the generated random value. */
1082 typedef _UIntType result_type
;
1085 * @brief Constructs a default %independent_bits_engine engine.
1087 * The underlying engine is default constructed as well.
1089 independent_bits_engine()
1093 * @brief Copy constructs a %independent_bits_engine engine.
1095 * Copies an existing base class random number generator.
1096 * @param __rng An existing (base class) engine object.
1099 independent_bits_engine(const _RandomNumberEngine
& __rng
)
1103 * @brief Move constructs a %independent_bits_engine engine.
1105 * Copies an existing base class random number generator.
1106 * @param __rng An existing (base class) engine object.
1109 independent_bits_engine(_RandomNumberEngine
&& __rng
)
1110 : _M_b(std::move(__rng
)) { }
1113 * @brief Seed constructs a %independent_bits_engine engine.
1115 * Constructs the underlying generator engine seeded with @p __s.
1116 * @param __s A seed value for the base class engine.
1119 independent_bits_engine(result_type __s
)
1123 * @brief Generator construct a %independent_bits_engine engine.
1125 * @param __q A seed sequence.
1127 template<typename _Sseq
, typename
= typename
1128 std::enable_if
<!std::is_same
<_Sseq
, independent_bits_engine
>::value
1129 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1132 independent_bits_engine(_Sseq
& __q
)
1137 * @brief Reseeds the %independent_bits_engine object with the default
1138 * seed for the underlying base class generator engine.
1145 * @brief Reseeds the %independent_bits_engine object with the default
1146 * seed for the underlying base class generator engine.
1149 seed(result_type __s
)
1153 * @brief Reseeds the %independent_bits_engine object with the given
1155 * @param __q A seed generator function.
1157 template<typename _Sseq
>
1163 * @brief Gets a const reference to the underlying generator engine
1166 const _RandomNumberEngine
&
1167 base() const noexcept
1171 * @brief Gets the minimum value in the generated random number range.
1173 static constexpr result_type
1178 * @brief Gets the maximum value in the generated random number range.
1180 static constexpr result_type
1182 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1185 * @brief Discard a sequence of random numbers.
1188 discard(unsigned long long __z
)
1190 for (; __z
!= 0ULL; --__z
)
1195 * @brief Gets the next value in the generated random number sequence.
1201 * @brief Compares two %independent_bits_engine random number generator
1202 * objects of the same type for equality.
1204 * @param __lhs A %independent_bits_engine random number generator
1206 * @param __rhs Another %independent_bits_engine random number generator
1209 * @returns true if the infinite sequences of generated values
1210 * would be equal, false otherwise.
1213 operator==(const independent_bits_engine
& __lhs
,
1214 const independent_bits_engine
& __rhs
)
1215 { return __lhs
._M_b
== __rhs
._M_b
; }
1218 * @brief Extracts the current state of a % subtract_with_carry_engine
1219 * random number generator engine @p __x from the input stream
1222 * @param __is An input stream.
1223 * @param __x A %independent_bits_engine random number generator
1226 * @returns The input stream with the state of @p __x extracted or in
1229 template<typename _CharT
, typename _Traits
>
1230 friend std::basic_istream
<_CharT
, _Traits
>&
1231 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1232 std::independent_bits_engine
<_RandomNumberEngine
,
1233 __w
, _UIntType
>& __x
)
1240 _RandomNumberEngine _M_b
;
1244 * @brief Compares two %independent_bits_engine random number generator
1245 * objects of the same type for inequality.
1247 * @param __lhs A %independent_bits_engine random number generator
1249 * @param __rhs Another %independent_bits_engine random number generator
1252 * @returns true if the infinite sequences of generated values
1253 * would be different, false otherwise.
1255 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1257 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1259 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1261 { return !(__lhs
== __rhs
); }
1264 * @brief Inserts the current state of a %independent_bits_engine random
1265 * number generator engine @p __x into the output stream @p __os.
1267 * @param __os An output stream.
1268 * @param __x A %independent_bits_engine random number generator engine.
1270 * @returns The output stream with the state of @p __x inserted or in
1273 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1274 typename _CharT
, typename _Traits
>
1275 std::basic_ostream
<_CharT
, _Traits
>&
1276 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1277 const std::independent_bits_engine
<_RandomNumberEngine
,
1278 __w
, _UIntType
>& __x
)
1286 * @brief Produces random numbers by combining random numbers from some
1287 * base engine to produce random numbers with a specifies number of bits
1290 template<typename _RandomNumberEngine
, size_t __k
>
1291 class shuffle_order_engine
1293 static_assert(1u <= __k
, "template argument substituting "
1294 "__k out of bound");
1297 /** The type of the generated random value. */
1298 typedef typename
_RandomNumberEngine::result_type result_type
;
1300 static constexpr size_t table_size
= __k
;
1303 * @brief Constructs a default %shuffle_order_engine engine.
1305 * The underlying engine is default constructed as well.
1307 shuffle_order_engine()
1309 { _M_initialize(); }
1312 * @brief Copy constructs a %shuffle_order_engine engine.
1314 * Copies an existing base class random number generator.
1315 * @param __rng An existing (base class) engine object.
1318 shuffle_order_engine(const _RandomNumberEngine
& __rng
)
1320 { _M_initialize(); }
1323 * @brief Move constructs a %shuffle_order_engine engine.
1325 * Copies an existing base class random number generator.
1326 * @param __rng An existing (base class) engine object.
1329 shuffle_order_engine(_RandomNumberEngine
&& __rng
)
1330 : _M_b(std::move(__rng
))
1331 { _M_initialize(); }
1334 * @brief Seed constructs a %shuffle_order_engine engine.
1336 * Constructs the underlying generator engine seeded with @p __s.
1337 * @param __s A seed value for the base class engine.
1340 shuffle_order_engine(result_type __s
)
1342 { _M_initialize(); }
1345 * @brief Generator construct a %shuffle_order_engine engine.
1347 * @param __q A seed sequence.
1349 template<typename _Sseq
, typename
= typename
1350 std::enable_if
<!std::is_same
<_Sseq
, shuffle_order_engine
>::value
1351 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1354 shuffle_order_engine(_Sseq
& __q
)
1356 { _M_initialize(); }
1359 * @brief Reseeds the %shuffle_order_engine object with the default seed
1360 for the underlying base class generator engine.
1370 * @brief Reseeds the %shuffle_order_engine object with the default seed
1371 * for the underlying base class generator engine.
1374 seed(result_type __s
)
1381 * @brief Reseeds the %shuffle_order_engine object with the given seed
1383 * @param __q A seed generator function.
1385 template<typename _Sseq
>
1394 * Gets a const reference to the underlying generator engine object.
1396 const _RandomNumberEngine
&
1397 base() const noexcept
1401 * Gets the minimum value in the generated random number range.
1403 static constexpr result_type
1405 { return _RandomNumberEngine::min(); }
1408 * Gets the maximum value in the generated random number range.
1410 static constexpr result_type
1412 { return _RandomNumberEngine::max(); }
1415 * Discard a sequence of random numbers.
1418 discard(unsigned long long __z
)
1420 for (; __z
!= 0ULL; --__z
)
1425 * Gets the next value in the generated random number sequence.
1431 * Compares two %shuffle_order_engine random number generator objects
1432 * of the same type for equality.
1434 * @param __lhs A %shuffle_order_engine random number generator object.
1435 * @param __rhs Another %shuffle_order_engine random number generator
1438 * @returns true if the infinite sequences of generated values
1439 * would be equal, false otherwise.
1442 operator==(const shuffle_order_engine
& __lhs
,
1443 const shuffle_order_engine
& __rhs
)
1444 { return (__lhs
._M_b
== __rhs
._M_b
1445 && std::equal(__lhs
._M_v
, __lhs
._M_v
+ __k
, __rhs
._M_v
)
1446 && __lhs
._M_y
== __rhs
._M_y
); }
1449 * @brief Inserts the current state of a %shuffle_order_engine random
1450 * number generator engine @p __x into the output stream
1453 * @param __os An output stream.
1454 * @param __x A %shuffle_order_engine random number generator engine.
1456 * @returns The output stream with the state of @p __x inserted or in
1459 template<typename _RandomNumberEngine1
, size_t __k1
,
1460 typename _CharT
, typename _Traits
>
1461 friend std::basic_ostream
<_CharT
, _Traits
>&
1462 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1463 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1467 * @brief Extracts the current state of a % subtract_with_carry_engine
1468 * random number generator engine @p __x from the input stream
1471 * @param __is An input stream.
1472 * @param __x A %shuffle_order_engine random number generator engine.
1474 * @returns The input stream with the state of @p __x extracted or in
1477 template<typename _RandomNumberEngine1
, size_t __k1
,
1478 typename _CharT
, typename _Traits
>
1479 friend std::basic_istream
<_CharT
, _Traits
>&
1480 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1481 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>& __x
);
1484 void _M_initialize()
1486 for (size_t __i
= 0; __i
< __k
; ++__i
)
1491 _RandomNumberEngine _M_b
;
1492 result_type _M_v
[__k
];
1497 * Compares two %shuffle_order_engine random number generator objects
1498 * of the same type for inequality.
1500 * @param __lhs A %shuffle_order_engine random number generator object.
1501 * @param __rhs Another %shuffle_order_engine random number generator
1504 * @returns true if the infinite sequences of generated values
1505 * would be different, false otherwise.
1507 template<typename _RandomNumberEngine
, size_t __k
>
1509 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1511 const std::shuffle_order_engine
<_RandomNumberEngine
,
1513 { return !(__lhs
== __rhs
); }
1517 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1519 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1523 * An alternative LCR (Lehmer Generator function).
1525 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1529 * The classic Mersenne Twister.
1532 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1533 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1534 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1536 typedef mersenne_twister_engine
<
1542 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1545 * An alternative Mersenne Twister.
1547 typedef mersenne_twister_engine
<
1550 0xb5026f5aa96619e9ULL
, 29,
1551 0x5555555555555555ULL
, 17,
1552 0x71d67fffeda60000ULL
, 37,
1553 0xfff7eee000000000ULL
, 43,
1554 6364136223846793005ULL> mt19937_64
;
1556 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1559 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1562 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1564 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1566 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1568 typedef minstd_rand0 default_random_engine
;
1571 * A standard interface to a platform-specific non-deterministic
1572 * random number generator (if any are available).
1577 /** The type of the generated random value. */
1578 typedef unsigned int result_type
;
1580 // constructors, destructors and member functions
1582 #ifdef _GLIBCXX_USE_RANDOM_TR1
1585 random_device(const std::string
& __token
= "default")
1596 random_device(const std::string
& __token
= "mt19937")
1597 { _M_init_pretr1(__token
); }
1603 static constexpr result_type
1605 { return std::numeric_limits
<result_type
>::min(); }
1607 static constexpr result_type
1609 { return std::numeric_limits
<result_type
>::max(); }
1612 entropy() const noexcept
1618 #ifdef _GLIBCXX_USE_RANDOM_TR1
1619 return this->_M_getval();
1621 return this->_M_getval_pretr1();
1625 // No copy functions.
1626 random_device(const random_device
&) = delete;
1627 void operator=(const random_device
&) = delete;
1631 void _M_init(const std::string
& __token
);
1632 void _M_init_pretr1(const std::string
& __token
);
1635 result_type
_M_getval();
1636 result_type
_M_getval_pretr1();
1645 /* @} */ // group random_generators
1648 * @addtogroup random_distributions Random Number Distributions
1654 * @addtogroup random_distributions_uniform Uniform Distributions
1655 * @ingroup random_distributions
1660 * @brief Uniform discrete distribution for random numbers.
1661 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1662 * probability throughout the range.
1664 template<typename _IntType
= int>
1665 class uniform_int_distribution
1667 static_assert(std::is_integral
<_IntType
>::value
,
1668 "template argument not an integral type");
1671 /** The type of the range of the distribution. */
1672 typedef _IntType result_type
;
1673 /** Parameter type. */
1676 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1679 param_type(_IntType __a
= 0,
1680 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1681 : _M_a(__a
), _M_b(__b
)
1683 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1695 operator==(const param_type
& __p1
, const param_type
& __p2
)
1696 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1705 * @brief Constructs a uniform distribution object.
1708 uniform_int_distribution(_IntType __a
= 0,
1709 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1710 : _M_param(__a
, __b
)
1714 uniform_int_distribution(const param_type
& __p
)
1719 * @brief Resets the distribution state.
1721 * Does nothing for the uniform integer distribution.
1728 { return _M_param
.a(); }
1732 { return _M_param
.b(); }
1735 * @brief Returns the parameter set of the distribution.
1739 { return _M_param
; }
1742 * @brief Sets the parameter set of the distribution.
1743 * @param __param The new parameter set of the distribution.
1746 param(const param_type
& __param
)
1747 { _M_param
= __param
; }
1750 * @brief Returns the inclusive lower bound of the distribution range.
1754 { return this->a(); }
1757 * @brief Returns the inclusive upper bound of the distribution range.
1761 { return this->b(); }
1764 * @brief Generating functions.
1766 template<typename _UniformRandomNumberGenerator
>
1768 operator()(_UniformRandomNumberGenerator
& __urng
)
1769 { return this->operator()(__urng
, _M_param
); }
1771 template<typename _UniformRandomNumberGenerator
>
1773 operator()(_UniformRandomNumberGenerator
& __urng
,
1774 const param_type
& __p
);
1776 template<typename _ForwardIterator
,
1777 typename _UniformRandomNumberGenerator
>
1779 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1780 _UniformRandomNumberGenerator
& __urng
)
1781 { this->__generate(__f
, __t
, __urng
, _M_param
); }
1783 template<typename _ForwardIterator
,
1784 typename _UniformRandomNumberGenerator
>
1786 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1787 _UniformRandomNumberGenerator
& __urng
,
1788 const param_type
& __p
)
1789 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1791 template<typename _UniformRandomNumberGenerator
>
1793 __generate(result_type
* __f
, result_type
* __t
,
1794 _UniformRandomNumberGenerator
& __urng
,
1795 const param_type
& __p
)
1796 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1799 * @brief Return true if two uniform integer distributions have
1800 * the same parameters.
1803 operator==(const uniform_int_distribution
& __d1
,
1804 const uniform_int_distribution
& __d2
)
1805 { return __d1
._M_param
== __d2
._M_param
; }
1808 template<typename _ForwardIterator
,
1809 typename _UniformRandomNumberGenerator
>
1811 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
1812 _UniformRandomNumberGenerator
& __urng
,
1813 const param_type
& __p
);
1815 param_type _M_param
;
1819 * @brief Return true if two uniform integer distributions have
1820 * different parameters.
1822 template<typename _IntType
>
1824 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1825 const std::uniform_int_distribution
<_IntType
>& __d2
)
1826 { return !(__d1
== __d2
); }
1829 * @brief Inserts a %uniform_int_distribution random number
1830 * distribution @p __x into the output stream @p os.
1832 * @param __os An output stream.
1833 * @param __x A %uniform_int_distribution random number distribution.
1835 * @returns The output stream with the state of @p __x inserted or in
1838 template<typename _IntType
, typename _CharT
, typename _Traits
>
1839 std::basic_ostream
<_CharT
, _Traits
>&
1840 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1841 const std::uniform_int_distribution
<_IntType
>&);
1844 * @brief Extracts a %uniform_int_distribution random number distribution
1845 * @p __x from the input stream @p __is.
1847 * @param __is An input stream.
1848 * @param __x A %uniform_int_distribution random number generator engine.
1850 * @returns The input stream with @p __x extracted or in an error state.
1852 template<typename _IntType
, typename _CharT
, typename _Traits
>
1853 std::basic_istream
<_CharT
, _Traits
>&
1854 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1855 std::uniform_int_distribution
<_IntType
>&);
1859 * @brief Uniform continuous distribution for random numbers.
1861 * A continuous random distribution on the range [min, max) with equal
1862 * probability throughout the range. The URNG should be real-valued and
1863 * deliver number in the range [0, 1).
1865 template<typename _RealType
= double>
1866 class uniform_real_distribution
1868 static_assert(std::is_floating_point
<_RealType
>::value
,
1869 "template argument not a floating point type");
1872 /** The type of the range of the distribution. */
1873 typedef _RealType result_type
;
1874 /** Parameter type. */
1877 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1880 param_type(_RealType __a
= _RealType(0),
1881 _RealType __b
= _RealType(1))
1882 : _M_a(__a
), _M_b(__b
)
1884 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1896 operator==(const param_type
& __p1
, const param_type
& __p2
)
1897 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1906 * @brief Constructs a uniform_real_distribution object.
1908 * @param __a [IN] The lower bound of the distribution.
1909 * @param __b [IN] The upper bound of the distribution.
1912 uniform_real_distribution(_RealType __a
= _RealType(0),
1913 _RealType __b
= _RealType(1))
1914 : _M_param(__a
, __b
)
1918 uniform_real_distribution(const param_type
& __p
)
1923 * @brief Resets the distribution state.
1925 * Does nothing for the uniform real distribution.
1932 { return _M_param
.a(); }
1936 { return _M_param
.b(); }
1939 * @brief Returns the parameter set of the distribution.
1943 { return _M_param
; }
1946 * @brief Sets the parameter set of the distribution.
1947 * @param __param The new parameter set of the distribution.
1950 param(const param_type
& __param
)
1951 { _M_param
= __param
; }
1954 * @brief Returns the inclusive lower bound of the distribution range.
1958 { return this->a(); }
1961 * @brief Returns the inclusive upper bound of the distribution range.
1965 { return this->b(); }
1968 * @brief Generating functions.
1970 template<typename _UniformRandomNumberGenerator
>
1972 operator()(_UniformRandomNumberGenerator
& __urng
)
1973 { return this->operator()(__urng
, _M_param
); }
1975 template<typename _UniformRandomNumberGenerator
>
1977 operator()(_UniformRandomNumberGenerator
& __urng
,
1978 const param_type
& __p
)
1980 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1982 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1985 template<typename _ForwardIterator
,
1986 typename _UniformRandomNumberGenerator
>
1988 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1989 _UniformRandomNumberGenerator
& __urng
)
1990 { this->__generate(__f
, __t
, __urng
, _M_param
); }
1992 template<typename _ForwardIterator
,
1993 typename _UniformRandomNumberGenerator
>
1995 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1996 _UniformRandomNumberGenerator
& __urng
,
1997 const param_type
& __p
)
1998 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2000 template<typename _UniformRandomNumberGenerator
>
2002 __generate(result_type
* __f
, result_type
* __t
,
2003 _UniformRandomNumberGenerator
& __urng
,
2004 const param_type
& __p
)
2005 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2008 * @brief Return true if two uniform real distributions have
2009 * the same parameters.
2012 operator==(const uniform_real_distribution
& __d1
,
2013 const uniform_real_distribution
& __d2
)
2014 { return __d1
._M_param
== __d2
._M_param
; }
2017 template<typename _ForwardIterator
,
2018 typename _UniformRandomNumberGenerator
>
2020 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2021 _UniformRandomNumberGenerator
& __urng
,
2022 const param_type
& __p
);
2024 param_type _M_param
;
2028 * @brief Return true if two uniform real distributions have
2029 * different parameters.
2031 template<typename _IntType
>
2033 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
2034 const std::uniform_real_distribution
<_IntType
>& __d2
)
2035 { return !(__d1
== __d2
); }
2038 * @brief Inserts a %uniform_real_distribution random number
2039 * distribution @p __x into the output stream @p __os.
2041 * @param __os An output stream.
2042 * @param __x A %uniform_real_distribution random number distribution.
2044 * @returns The output stream with the state of @p __x inserted or in
2047 template<typename _RealType
, typename _CharT
, typename _Traits
>
2048 std::basic_ostream
<_CharT
, _Traits
>&
2049 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2050 const std::uniform_real_distribution
<_RealType
>&);
2053 * @brief Extracts a %uniform_real_distribution random number distribution
2054 * @p __x from the input stream @p __is.
2056 * @param __is An input stream.
2057 * @param __x A %uniform_real_distribution random number generator engine.
2059 * @returns The input stream with @p __x extracted or in an error state.
2061 template<typename _RealType
, typename _CharT
, typename _Traits
>
2062 std::basic_istream
<_CharT
, _Traits
>&
2063 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2064 std::uniform_real_distribution
<_RealType
>&);
2066 /* @} */ // group random_distributions_uniform
2069 * @addtogroup random_distributions_normal Normal Distributions
2070 * @ingroup random_distributions
2075 * @brief A normal continuous distribution for random numbers.
2077 * The formula for the normal probability density function is
2079 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
2080 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
2083 template<typename _RealType
= double>
2084 class normal_distribution
2086 static_assert(std::is_floating_point
<_RealType
>::value
,
2087 "template argument not a floating point type");
2090 /** The type of the range of the distribution. */
2091 typedef _RealType result_type
;
2092 /** Parameter type. */
2095 typedef normal_distribution
<_RealType
> distribution_type
;
2098 param_type(_RealType __mean
= _RealType(0),
2099 _RealType __stddev
= _RealType(1))
2100 : _M_mean(__mean
), _M_stddev(__stddev
)
2102 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
2111 { return _M_stddev
; }
2114 operator==(const param_type
& __p1
, const param_type
& __p2
)
2115 { return (__p1
._M_mean
== __p2
._M_mean
2116 && __p1
._M_stddev
== __p2
._M_stddev
); }
2120 _RealType _M_stddev
;
2125 * Constructs a normal distribution with parameters @f$mean@f$ and
2126 * standard deviation.
2129 normal_distribution(result_type __mean
= result_type(0),
2130 result_type __stddev
= result_type(1))
2131 : _M_param(__mean
, __stddev
), _M_saved_available(false)
2135 normal_distribution(const param_type
& __p
)
2136 : _M_param(__p
), _M_saved_available(false)
2140 * @brief Resets the distribution state.
2144 { _M_saved_available
= false; }
2147 * @brief Returns the mean of the distribution.
2151 { return _M_param
.mean(); }
2154 * @brief Returns the standard deviation of the distribution.
2158 { return _M_param
.stddev(); }
2161 * @brief Returns the parameter set of the distribution.
2165 { return _M_param
; }
2168 * @brief Sets the parameter set of the distribution.
2169 * @param __param The new parameter set of the distribution.
2172 param(const param_type
& __param
)
2173 { _M_param
= __param
; }
2176 * @brief Returns the greatest lower bound value of the distribution.
2180 { return std::numeric_limits
<result_type
>::lowest(); }
2183 * @brief Returns the least upper bound value of the distribution.
2187 { return std::numeric_limits
<result_type
>::max(); }
2190 * @brief Generating functions.
2192 template<typename _UniformRandomNumberGenerator
>
2194 operator()(_UniformRandomNumberGenerator
& __urng
)
2195 { return this->operator()(__urng
, _M_param
); }
2197 template<typename _UniformRandomNumberGenerator
>
2199 operator()(_UniformRandomNumberGenerator
& __urng
,
2200 const param_type
& __p
);
2202 template<typename _ForwardIterator
,
2203 typename _UniformRandomNumberGenerator
>
2205 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2206 _UniformRandomNumberGenerator
& __urng
)
2207 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2209 template<typename _ForwardIterator
,
2210 typename _UniformRandomNumberGenerator
>
2212 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2213 _UniformRandomNumberGenerator
& __urng
,
2214 const param_type
& __p
)
2215 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2217 template<typename _UniformRandomNumberGenerator
>
2219 __generate(result_type
* __f
, result_type
* __t
,
2220 _UniformRandomNumberGenerator
& __urng
,
2221 const param_type
& __p
)
2222 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2225 * @brief Return true if two normal distributions have
2226 * the same parameters and the sequences that would
2227 * be generated are equal.
2229 template<typename _RealType1
>
2231 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2232 const std::normal_distribution
<_RealType1
>& __d2
);
2235 * @brief Inserts a %normal_distribution random number distribution
2236 * @p __x into the output stream @p __os.
2238 * @param __os An output stream.
2239 * @param __x A %normal_distribution random number distribution.
2241 * @returns The output stream with the state of @p __x inserted or in
2244 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2245 friend std::basic_ostream
<_CharT
, _Traits
>&
2246 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2247 const std::normal_distribution
<_RealType1
>& __x
);
2250 * @brief Extracts a %normal_distribution random number distribution
2251 * @p __x from the input stream @p __is.
2253 * @param __is An input stream.
2254 * @param __x A %normal_distribution random number generator engine.
2256 * @returns The input stream with @p __x extracted or in an error
2259 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2260 friend std::basic_istream
<_CharT
, _Traits
>&
2261 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2262 std::normal_distribution
<_RealType1
>& __x
);
2265 template<typename _ForwardIterator
,
2266 typename _UniformRandomNumberGenerator
>
2268 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2269 _UniformRandomNumberGenerator
& __urng
,
2270 const param_type
& __p
);
2272 param_type _M_param
;
2273 result_type _M_saved
;
2274 bool _M_saved_available
;
2278 * @brief Return true if two normal distributions are different.
2280 template<typename _RealType
>
2282 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2283 const std::normal_distribution
<_RealType
>& __d2
)
2284 { return !(__d1
== __d2
); }
2288 * @brief A lognormal_distribution random number distribution.
2290 * The formula for the normal probability mass function is
2292 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2293 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2296 template<typename _RealType
= double>
2297 class lognormal_distribution
2299 static_assert(std::is_floating_point
<_RealType
>::value
,
2300 "template argument not a floating point type");
2303 /** The type of the range of the distribution. */
2304 typedef _RealType result_type
;
2305 /** Parameter type. */
2308 typedef lognormal_distribution
<_RealType
> distribution_type
;
2311 param_type(_RealType __m
= _RealType(0),
2312 _RealType __s
= _RealType(1))
2313 : _M_m(__m
), _M_s(__s
)
2325 operator==(const param_type
& __p1
, const param_type
& __p2
)
2326 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2334 lognormal_distribution(_RealType __m
= _RealType(0),
2335 _RealType __s
= _RealType(1))
2336 : _M_param(__m
, __s
), _M_nd()
2340 lognormal_distribution(const param_type
& __p
)
2341 : _M_param(__p
), _M_nd()
2345 * Resets the distribution state.
2356 { return _M_param
.m(); }
2360 { return _M_param
.s(); }
2363 * @brief Returns the parameter set of the distribution.
2367 { return _M_param
; }
2370 * @brief Sets the parameter set of the distribution.
2371 * @param __param The new parameter set of the distribution.
2374 param(const param_type
& __param
)
2375 { _M_param
= __param
; }
2378 * @brief Returns the greatest lower bound value of the distribution.
2382 { return result_type(0); }
2385 * @brief Returns the least upper bound value of the distribution.
2389 { return std::numeric_limits
<result_type
>::max(); }
2392 * @brief Generating functions.
2394 template<typename _UniformRandomNumberGenerator
>
2396 operator()(_UniformRandomNumberGenerator
& __urng
)
2397 { return this->operator()(__urng
, _M_param
); }
2399 template<typename _UniformRandomNumberGenerator
>
2401 operator()(_UniformRandomNumberGenerator
& __urng
,
2402 const param_type
& __p
)
2403 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2405 template<typename _ForwardIterator
,
2406 typename _UniformRandomNumberGenerator
>
2408 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2409 _UniformRandomNumberGenerator
& __urng
)
2410 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2412 template<typename _ForwardIterator
,
2413 typename _UniformRandomNumberGenerator
>
2415 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2416 _UniformRandomNumberGenerator
& __urng
,
2417 const param_type
& __p
)
2418 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2420 template<typename _UniformRandomNumberGenerator
>
2422 __generate(result_type
* __f
, result_type
* __t
,
2423 _UniformRandomNumberGenerator
& __urng
,
2424 const param_type
& __p
)
2425 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2428 * @brief Return true if two lognormal distributions have
2429 * the same parameters and the sequences that would
2430 * be generated are equal.
2433 operator==(const lognormal_distribution
& __d1
,
2434 const lognormal_distribution
& __d2
)
2435 { return (__d1
._M_param
== __d2
._M_param
2436 && __d1
._M_nd
== __d2
._M_nd
); }
2439 * @brief Inserts a %lognormal_distribution random number distribution
2440 * @p __x into the output stream @p __os.
2442 * @param __os An output stream.
2443 * @param __x A %lognormal_distribution random number distribution.
2445 * @returns The output stream with the state of @p __x inserted or in
2448 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2449 friend std::basic_ostream
<_CharT
, _Traits
>&
2450 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2451 const std::lognormal_distribution
<_RealType1
>& __x
);
2454 * @brief Extracts a %lognormal_distribution random number distribution
2455 * @p __x from the input stream @p __is.
2457 * @param __is An input stream.
2458 * @param __x A %lognormal_distribution random number
2461 * @returns The input stream with @p __x extracted or in an error state.
2463 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2464 friend std::basic_istream
<_CharT
, _Traits
>&
2465 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2466 std::lognormal_distribution
<_RealType1
>& __x
);
2469 template<typename _ForwardIterator
,
2470 typename _UniformRandomNumberGenerator
>
2472 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2473 _UniformRandomNumberGenerator
& __urng
,
2474 const param_type
& __p
);
2476 param_type _M_param
;
2478 std::normal_distribution
<result_type
> _M_nd
;
2482 * @brief Return true if two lognormal distributions are different.
2484 template<typename _RealType
>
2486 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2487 const std::lognormal_distribution
<_RealType
>& __d2
)
2488 { return !(__d1
== __d2
); }
2492 * @brief A gamma continuous distribution for random numbers.
2494 * The formula for the gamma probability density function is:
2496 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2497 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2500 template<typename _RealType
= double>
2501 class gamma_distribution
2503 static_assert(std::is_floating_point
<_RealType
>::value
,
2504 "template argument not a floating point type");
2507 /** The type of the range of the distribution. */
2508 typedef _RealType result_type
;
2509 /** Parameter type. */
2512 typedef gamma_distribution
<_RealType
> distribution_type
;
2513 friend class gamma_distribution
<_RealType
>;
2516 param_type(_RealType __alpha_val
= _RealType(1),
2517 _RealType __beta_val
= _RealType(1))
2518 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2520 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2526 { return _M_alpha
; }
2533 operator==(const param_type
& __p1
, const param_type
& __p2
)
2534 { return (__p1
._M_alpha
== __p2
._M_alpha
2535 && __p1
._M_beta
== __p2
._M_beta
); }
2544 _RealType _M_malpha
, _M_a2
;
2549 * @brief Constructs a gamma distribution with parameters
2550 * @f$\alpha@f$ and @f$\beta@f$.
2553 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2554 _RealType __beta_val
= _RealType(1))
2555 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2559 gamma_distribution(const param_type
& __p
)
2560 : _M_param(__p
), _M_nd()
2564 * @brief Resets the distribution state.
2571 * @brief Returns the @f$\alpha@f$ of the distribution.
2575 { return _M_param
.alpha(); }
2578 * @brief Returns the @f$\beta@f$ of the distribution.
2582 { return _M_param
.beta(); }
2585 * @brief Returns the parameter set of the distribution.
2589 { return _M_param
; }
2592 * @brief Sets the parameter set of the distribution.
2593 * @param __param The new parameter set of the distribution.
2596 param(const param_type
& __param
)
2597 { _M_param
= __param
; }
2600 * @brief Returns the greatest lower bound value of the distribution.
2604 { return result_type(0); }
2607 * @brief Returns the least upper bound value of the distribution.
2611 { return std::numeric_limits
<result_type
>::max(); }
2614 * @brief Generating functions.
2616 template<typename _UniformRandomNumberGenerator
>
2618 operator()(_UniformRandomNumberGenerator
& __urng
)
2619 { return this->operator()(__urng
, _M_param
); }
2621 template<typename _UniformRandomNumberGenerator
>
2623 operator()(_UniformRandomNumberGenerator
& __urng
,
2624 const param_type
& __p
);
2626 template<typename _ForwardIterator
,
2627 typename _UniformRandomNumberGenerator
>
2629 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2630 _UniformRandomNumberGenerator
& __urng
)
2631 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2633 template<typename _ForwardIterator
,
2634 typename _UniformRandomNumberGenerator
>
2636 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2637 _UniformRandomNumberGenerator
& __urng
,
2638 const param_type
& __p
)
2639 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2641 template<typename _UniformRandomNumberGenerator
>
2643 __generate(result_type
* __f
, result_type
* __t
,
2644 _UniformRandomNumberGenerator
& __urng
,
2645 const param_type
& __p
)
2646 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2649 * @brief Return true if two gamma distributions have the same
2650 * parameters and the sequences that would be generated
2654 operator==(const gamma_distribution
& __d1
,
2655 const gamma_distribution
& __d2
)
2656 { return (__d1
._M_param
== __d2
._M_param
2657 && __d1
._M_nd
== __d2
._M_nd
); }
2660 * @brief Inserts a %gamma_distribution random number distribution
2661 * @p __x into the output stream @p __os.
2663 * @param __os An output stream.
2664 * @param __x A %gamma_distribution random number distribution.
2666 * @returns The output stream with the state of @p __x inserted or in
2669 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2670 friend std::basic_ostream
<_CharT
, _Traits
>&
2671 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2672 const std::gamma_distribution
<_RealType1
>& __x
);
2675 * @brief Extracts a %gamma_distribution random number distribution
2676 * @p __x from the input stream @p __is.
2678 * @param __is An input stream.
2679 * @param __x A %gamma_distribution random number generator engine.
2681 * @returns The input stream with @p __x extracted or in an error state.
2683 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2684 friend std::basic_istream
<_CharT
, _Traits
>&
2685 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2686 std::gamma_distribution
<_RealType1
>& __x
);
2689 template<typename _ForwardIterator
,
2690 typename _UniformRandomNumberGenerator
>
2692 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2693 _UniformRandomNumberGenerator
& __urng
,
2694 const param_type
& __p
);
2696 param_type _M_param
;
2698 std::normal_distribution
<result_type
> _M_nd
;
2702 * @brief Return true if two gamma distributions are different.
2704 template<typename _RealType
>
2706 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2707 const std::gamma_distribution
<_RealType
>& __d2
)
2708 { return !(__d1
== __d2
); }
2712 * @brief A chi_squared_distribution random number distribution.
2714 * The formula for the normal probability mass function is
2715 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2717 template<typename _RealType
= double>
2718 class chi_squared_distribution
2720 static_assert(std::is_floating_point
<_RealType
>::value
,
2721 "template argument not a floating point type");
2724 /** The type of the range of the distribution. */
2725 typedef _RealType result_type
;
2726 /** Parameter type. */
2729 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2732 param_type(_RealType __n
= _RealType(1))
2741 operator==(const param_type
& __p1
, const param_type
& __p2
)
2742 { return __p1
._M_n
== __p2
._M_n
; }
2749 chi_squared_distribution(_RealType __n
= _RealType(1))
2750 : _M_param(__n
), _M_gd(__n
/ 2)
2754 chi_squared_distribution(const param_type
& __p
)
2755 : _M_param(__p
), _M_gd(__p
.n() / 2)
2759 * @brief Resets the distribution state.
2770 { return _M_param
.n(); }
2773 * @brief Returns the parameter set of the distribution.
2777 { return _M_param
; }
2780 * @brief Sets the parameter set of the distribution.
2781 * @param __param The new parameter set of the distribution.
2784 param(const param_type
& __param
)
2785 { _M_param
= __param
; }
2788 * @brief Returns the greatest lower bound value of the distribution.
2792 { return result_type(0); }
2795 * @brief Returns the least upper bound value of the distribution.
2799 { return std::numeric_limits
<result_type
>::max(); }
2802 * @brief Generating functions.
2804 template<typename _UniformRandomNumberGenerator
>
2806 operator()(_UniformRandomNumberGenerator
& __urng
)
2807 { return 2 * _M_gd(__urng
); }
2809 template<typename _UniformRandomNumberGenerator
>
2811 operator()(_UniformRandomNumberGenerator
& __urng
,
2812 const param_type
& __p
)
2814 typedef typename
std::gamma_distribution
<result_type
>::param_type
2816 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2819 template<typename _ForwardIterator
,
2820 typename _UniformRandomNumberGenerator
>
2822 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2823 _UniformRandomNumberGenerator
& __urng
)
2824 { this->__generate_impl(__f
, __t
, __urng
); }
2826 template<typename _ForwardIterator
,
2827 typename _UniformRandomNumberGenerator
>
2829 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2830 _UniformRandomNumberGenerator
& __urng
,
2831 const param_type
& __p
)
2832 { typename
std::gamma_distribution
<result_type
>::param_type
2834 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2836 template<typename _UniformRandomNumberGenerator
>
2838 __generate(result_type
* __f
, result_type
* __t
,
2839 _UniformRandomNumberGenerator
& __urng
)
2840 { this->__generate_impl(__f
, __t
, __urng
); }
2842 template<typename _UniformRandomNumberGenerator
>
2844 __generate(result_type
* __f
, result_type
* __t
,
2845 _UniformRandomNumberGenerator
& __urng
,
2846 const param_type
& __p
)
2847 { typename
std::gamma_distribution
<result_type
>::param_type
2849 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2852 * @brief Return true if two Chi-squared distributions have
2853 * the same parameters and the sequences that would be
2854 * generated are equal.
2857 operator==(const chi_squared_distribution
& __d1
,
2858 const chi_squared_distribution
& __d2
)
2859 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
2862 * @brief Inserts a %chi_squared_distribution random number distribution
2863 * @p __x into the output stream @p __os.
2865 * @param __os An output stream.
2866 * @param __x A %chi_squared_distribution random number distribution.
2868 * @returns The output stream with the state of @p __x inserted or in
2871 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2872 friend std::basic_ostream
<_CharT
, _Traits
>&
2873 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2874 const std::chi_squared_distribution
<_RealType1
>& __x
);
2877 * @brief Extracts a %chi_squared_distribution random number distribution
2878 * @p __x from the input stream @p __is.
2880 * @param __is An input stream.
2881 * @param __x A %chi_squared_distribution random number
2884 * @returns The input stream with @p __x extracted or in an error state.
2886 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2887 friend std::basic_istream
<_CharT
, _Traits
>&
2888 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2889 std::chi_squared_distribution
<_RealType1
>& __x
);
2892 template<typename _ForwardIterator
,
2893 typename _UniformRandomNumberGenerator
>
2895 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2896 _UniformRandomNumberGenerator
& __urng
);
2898 template<typename _ForwardIterator
,
2899 typename _UniformRandomNumberGenerator
>
2901 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2902 _UniformRandomNumberGenerator
& __urng
,
2904 std::gamma_distribution
<result_type
>::param_type
& __p
);
2906 param_type _M_param
;
2908 std::gamma_distribution
<result_type
> _M_gd
;
2912 * @brief Return true if two Chi-squared distributions are different.
2914 template<typename _RealType
>
2916 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2917 const std::chi_squared_distribution
<_RealType
>& __d2
)
2918 { return !(__d1
== __d2
); }
2922 * @brief A cauchy_distribution random number distribution.
2924 * The formula for the normal probability mass function is
2925 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2927 template<typename _RealType
= double>
2928 class cauchy_distribution
2930 static_assert(std::is_floating_point
<_RealType
>::value
,
2931 "template argument not a floating point type");
2934 /** The type of the range of the distribution. */
2935 typedef _RealType result_type
;
2936 /** Parameter type. */
2939 typedef cauchy_distribution
<_RealType
> distribution_type
;
2942 param_type(_RealType __a
= _RealType(0),
2943 _RealType __b
= _RealType(1))
2944 : _M_a(__a
), _M_b(__b
)
2956 operator==(const param_type
& __p1
, const param_type
& __p2
)
2957 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2965 cauchy_distribution(_RealType __a
= _RealType(0),
2966 _RealType __b
= _RealType(1))
2967 : _M_param(__a
, __b
)
2971 cauchy_distribution(const param_type
& __p
)
2976 * @brief Resets the distribution state.
2987 { return _M_param
.a(); }
2991 { return _M_param
.b(); }
2994 * @brief Returns the parameter set of the distribution.
2998 { return _M_param
; }
3001 * @brief Sets the parameter set of the distribution.
3002 * @param __param The new parameter set of the distribution.
3005 param(const param_type
& __param
)
3006 { _M_param
= __param
; }
3009 * @brief Returns the greatest lower bound value of the distribution.
3013 { return std::numeric_limits
<result_type
>::lowest(); }
3016 * @brief Returns the least upper bound value of the distribution.
3020 { return std::numeric_limits
<result_type
>::max(); }
3023 * @brief Generating functions.
3025 template<typename _UniformRandomNumberGenerator
>
3027 operator()(_UniformRandomNumberGenerator
& __urng
)
3028 { return this->operator()(__urng
, _M_param
); }
3030 template<typename _UniformRandomNumberGenerator
>
3032 operator()(_UniformRandomNumberGenerator
& __urng
,
3033 const param_type
& __p
);
3035 template<typename _ForwardIterator
,
3036 typename _UniformRandomNumberGenerator
>
3038 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3039 _UniformRandomNumberGenerator
& __urng
)
3040 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3042 template<typename _ForwardIterator
,
3043 typename _UniformRandomNumberGenerator
>
3045 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3046 _UniformRandomNumberGenerator
& __urng
,
3047 const param_type
& __p
)
3048 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3050 template<typename _UniformRandomNumberGenerator
>
3052 __generate(result_type
* __f
, result_type
* __t
,
3053 _UniformRandomNumberGenerator
& __urng
,
3054 const param_type
& __p
)
3055 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3058 * @brief Return true if two Cauchy distributions have
3059 * the same parameters.
3062 operator==(const cauchy_distribution
& __d1
,
3063 const cauchy_distribution
& __d2
)
3064 { return __d1
._M_param
== __d2
._M_param
; }
3067 template<typename _ForwardIterator
,
3068 typename _UniformRandomNumberGenerator
>
3070 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3071 _UniformRandomNumberGenerator
& __urng
,
3072 const param_type
& __p
);
3074 param_type _M_param
;
3078 * @brief Return true if two Cauchy distributions have
3079 * different parameters.
3081 template<typename _RealType
>
3083 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
3084 const std::cauchy_distribution
<_RealType
>& __d2
)
3085 { return !(__d1
== __d2
); }
3088 * @brief Inserts a %cauchy_distribution random number distribution
3089 * @p __x into the output stream @p __os.
3091 * @param __os An output stream.
3092 * @param __x A %cauchy_distribution random number distribution.
3094 * @returns The output stream with the state of @p __x inserted or in
3097 template<typename _RealType
, typename _CharT
, typename _Traits
>
3098 std::basic_ostream
<_CharT
, _Traits
>&
3099 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3100 const std::cauchy_distribution
<_RealType
>& __x
);
3103 * @brief Extracts a %cauchy_distribution random number distribution
3104 * @p __x from the input stream @p __is.
3106 * @param __is An input stream.
3107 * @param __x A %cauchy_distribution random number
3110 * @returns The input stream with @p __x extracted or in an error state.
3112 template<typename _RealType
, typename _CharT
, typename _Traits
>
3113 std::basic_istream
<_CharT
, _Traits
>&
3114 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3115 std::cauchy_distribution
<_RealType
>& __x
);
3119 * @brief A fisher_f_distribution random number distribution.
3121 * The formula for the normal probability mass function is
3123 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
3124 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
3125 * (1 + \frac{mx}{n})^{-(m+n)/2}
3128 template<typename _RealType
= double>
3129 class fisher_f_distribution
3131 static_assert(std::is_floating_point
<_RealType
>::value
,
3132 "template argument not a floating point type");
3135 /** The type of the range of the distribution. */
3136 typedef _RealType result_type
;
3137 /** Parameter type. */
3140 typedef fisher_f_distribution
<_RealType
> distribution_type
;
3143 param_type(_RealType __m
= _RealType(1),
3144 _RealType __n
= _RealType(1))
3145 : _M_m(__m
), _M_n(__n
)
3157 operator==(const param_type
& __p1
, const param_type
& __p2
)
3158 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
3166 fisher_f_distribution(_RealType __m
= _RealType(1),
3167 _RealType __n
= _RealType(1))
3168 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
3172 fisher_f_distribution(const param_type
& __p
)
3173 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
3177 * @brief Resets the distribution state.
3191 { return _M_param
.m(); }
3195 { return _M_param
.n(); }
3198 * @brief Returns the parameter set of the distribution.
3202 { return _M_param
; }
3205 * @brief Sets the parameter set of the distribution.
3206 * @param __param The new parameter set of the distribution.
3209 param(const param_type
& __param
)
3210 { _M_param
= __param
; }
3213 * @brief Returns the greatest lower bound value of the distribution.
3217 { return result_type(0); }
3220 * @brief Returns the least upper bound value of the distribution.
3224 { return std::numeric_limits
<result_type
>::max(); }
3227 * @brief Generating functions.
3229 template<typename _UniformRandomNumberGenerator
>
3231 operator()(_UniformRandomNumberGenerator
& __urng
)
3232 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
3234 template<typename _UniformRandomNumberGenerator
>
3236 operator()(_UniformRandomNumberGenerator
& __urng
,
3237 const param_type
& __p
)
3239 typedef typename
std::gamma_distribution
<result_type
>::param_type
3241 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
3242 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
3245 template<typename _ForwardIterator
,
3246 typename _UniformRandomNumberGenerator
>
3248 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3249 _UniformRandomNumberGenerator
& __urng
)
3250 { this->__generate_impl(__f
, __t
, __urng
); }
3252 template<typename _ForwardIterator
,
3253 typename _UniformRandomNumberGenerator
>
3255 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3256 _UniformRandomNumberGenerator
& __urng
,
3257 const param_type
& __p
)
3258 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3260 template<typename _UniformRandomNumberGenerator
>
3262 __generate(result_type
* __f
, result_type
* __t
,
3263 _UniformRandomNumberGenerator
& __urng
)
3264 { this->__generate_impl(__f
, __t
, __urng
); }
3266 template<typename _UniformRandomNumberGenerator
>
3268 __generate(result_type
* __f
, result_type
* __t
,
3269 _UniformRandomNumberGenerator
& __urng
,
3270 const param_type
& __p
)
3271 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3274 * @brief Return true if two Fisher f distributions have
3275 * the same parameters and the sequences that would
3276 * be generated are equal.
3279 operator==(const fisher_f_distribution
& __d1
,
3280 const fisher_f_distribution
& __d2
)
3281 { return (__d1
._M_param
== __d2
._M_param
3282 && __d1
._M_gd_x
== __d2
._M_gd_x
3283 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
3286 * @brief Inserts a %fisher_f_distribution random number distribution
3287 * @p __x into the output stream @p __os.
3289 * @param __os An output stream.
3290 * @param __x A %fisher_f_distribution random number distribution.
3292 * @returns The output stream with the state of @p __x inserted or in
3295 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3296 friend std::basic_ostream
<_CharT
, _Traits
>&
3297 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3298 const std::fisher_f_distribution
<_RealType1
>& __x
);
3301 * @brief Extracts a %fisher_f_distribution random number distribution
3302 * @p __x from the input stream @p __is.
3304 * @param __is An input stream.
3305 * @param __x A %fisher_f_distribution random number
3308 * @returns The input stream with @p __x extracted or in an error state.
3310 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3311 friend std::basic_istream
<_CharT
, _Traits
>&
3312 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3313 std::fisher_f_distribution
<_RealType1
>& __x
);
3316 template<typename _ForwardIterator
,
3317 typename _UniformRandomNumberGenerator
>
3319 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3320 _UniformRandomNumberGenerator
& __urng
);
3322 template<typename _ForwardIterator
,
3323 typename _UniformRandomNumberGenerator
>
3325 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3326 _UniformRandomNumberGenerator
& __urng
,
3327 const param_type
& __p
);
3329 param_type _M_param
;
3331 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3335 * @brief Return true if two Fisher f distributions are different.
3337 template<typename _RealType
>
3339 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3340 const std::fisher_f_distribution
<_RealType
>& __d2
)
3341 { return !(__d1
== __d2
); }
3344 * @brief A student_t_distribution random number distribution.
3346 * The formula for the normal probability mass function is:
3348 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3349 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3352 template<typename _RealType
= double>
3353 class student_t_distribution
3355 static_assert(std::is_floating_point
<_RealType
>::value
,
3356 "template argument not a floating point type");
3359 /** The type of the range of the distribution. */
3360 typedef _RealType result_type
;
3361 /** Parameter type. */
3364 typedef student_t_distribution
<_RealType
> distribution_type
;
3367 param_type(_RealType __n
= _RealType(1))
3376 operator==(const param_type
& __p1
, const param_type
& __p2
)
3377 { return __p1
._M_n
== __p2
._M_n
; }
3384 student_t_distribution(_RealType __n
= _RealType(1))
3385 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3389 student_t_distribution(const param_type
& __p
)
3390 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3394 * @brief Resets the distribution state.
3408 { return _M_param
.n(); }
3411 * @brief Returns the parameter set of the distribution.
3415 { return _M_param
; }
3418 * @brief Sets the parameter set of the distribution.
3419 * @param __param The new parameter set of the distribution.
3422 param(const param_type
& __param
)
3423 { _M_param
= __param
; }
3426 * @brief Returns the greatest lower bound value of the distribution.
3430 { return std::numeric_limits
<result_type
>::lowest(); }
3433 * @brief Returns the least upper bound value of the distribution.
3437 { return std::numeric_limits
<result_type
>::max(); }
3440 * @brief Generating functions.
3442 template<typename _UniformRandomNumberGenerator
>
3444 operator()(_UniformRandomNumberGenerator
& __urng
)
3445 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3447 template<typename _UniformRandomNumberGenerator
>
3449 operator()(_UniformRandomNumberGenerator
& __urng
,
3450 const param_type
& __p
)
3452 typedef typename
std::gamma_distribution
<result_type
>::param_type
3455 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3456 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3459 template<typename _ForwardIterator
,
3460 typename _UniformRandomNumberGenerator
>
3462 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3463 _UniformRandomNumberGenerator
& __urng
)
3464 { this->__generate_impl(__f
, __t
, __urng
); }
3466 template<typename _ForwardIterator
,
3467 typename _UniformRandomNumberGenerator
>
3469 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3470 _UniformRandomNumberGenerator
& __urng
,
3471 const param_type
& __p
)
3472 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3474 template<typename _UniformRandomNumberGenerator
>
3476 __generate(result_type
* __f
, result_type
* __t
,
3477 _UniformRandomNumberGenerator
& __urng
)
3478 { this->__generate_impl(__f
, __t
, __urng
); }
3480 template<typename _UniformRandomNumberGenerator
>
3482 __generate(result_type
* __f
, result_type
* __t
,
3483 _UniformRandomNumberGenerator
& __urng
,
3484 const param_type
& __p
)
3485 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3488 * @brief Return true if two Student t distributions have
3489 * the same parameters and the sequences that would
3490 * be generated are equal.
3493 operator==(const student_t_distribution
& __d1
,
3494 const student_t_distribution
& __d2
)
3495 { return (__d1
._M_param
== __d2
._M_param
3496 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3499 * @brief Inserts a %student_t_distribution random number distribution
3500 * @p __x into the output stream @p __os.
3502 * @param __os An output stream.
3503 * @param __x A %student_t_distribution random number distribution.
3505 * @returns The output stream with the state of @p __x inserted or in
3508 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3509 friend std::basic_ostream
<_CharT
, _Traits
>&
3510 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3511 const std::student_t_distribution
<_RealType1
>& __x
);
3514 * @brief Extracts a %student_t_distribution random number distribution
3515 * @p __x from the input stream @p __is.
3517 * @param __is An input stream.
3518 * @param __x A %student_t_distribution random number
3521 * @returns The input stream with @p __x extracted or in an error state.
3523 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3524 friend std::basic_istream
<_CharT
, _Traits
>&
3525 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3526 std::student_t_distribution
<_RealType1
>& __x
);
3529 template<typename _ForwardIterator
,
3530 typename _UniformRandomNumberGenerator
>
3532 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3533 _UniformRandomNumberGenerator
& __urng
);
3534 template<typename _ForwardIterator
,
3535 typename _UniformRandomNumberGenerator
>
3537 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3538 _UniformRandomNumberGenerator
& __urng
,
3539 const param_type
& __p
);
3541 param_type _M_param
;
3543 std::normal_distribution
<result_type
> _M_nd
;
3544 std::gamma_distribution
<result_type
> _M_gd
;
3548 * @brief Return true if two Student t distributions are different.
3550 template<typename _RealType
>
3552 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3553 const std::student_t_distribution
<_RealType
>& __d2
)
3554 { return !(__d1
== __d2
); }
3557 /* @} */ // group random_distributions_normal
3560 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3561 * @ingroup random_distributions
3566 * @brief A Bernoulli random number distribution.
3568 * Generates a sequence of true and false values with likelihood @f$p@f$
3569 * that true will come up and @f$(1 - p)@f$ that false will appear.
3571 class bernoulli_distribution
3574 /** The type of the range of the distribution. */
3575 typedef bool result_type
;
3576 /** Parameter type. */
3579 typedef bernoulli_distribution distribution_type
;
3582 param_type(double __p
= 0.5)
3585 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
3593 operator==(const param_type
& __p1
, const param_type
& __p2
)
3594 { return __p1
._M_p
== __p2
._M_p
; }
3602 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3604 * @param __p [IN] The likelihood of a true result being returned.
3605 * Must be in the interval @f$[0, 1]@f$.
3608 bernoulli_distribution(double __p
= 0.5)
3613 bernoulli_distribution(const param_type
& __p
)
3618 * @brief Resets the distribution state.
3620 * Does nothing for a Bernoulli distribution.
3626 * @brief Returns the @p p parameter of the distribution.
3630 { return _M_param
.p(); }
3633 * @brief Returns the parameter set of the distribution.
3637 { return _M_param
; }
3640 * @brief Sets the parameter set of the distribution.
3641 * @param __param The new parameter set of the distribution.
3644 param(const param_type
& __param
)
3645 { _M_param
= __param
; }
3648 * @brief Returns the greatest lower bound value of the distribution.
3652 { return std::numeric_limits
<result_type
>::min(); }
3655 * @brief Returns the least upper bound value of the distribution.
3659 { return std::numeric_limits
<result_type
>::max(); }
3662 * @brief Generating functions.
3664 template<typename _UniformRandomNumberGenerator
>
3666 operator()(_UniformRandomNumberGenerator
& __urng
)
3667 { return this->operator()(__urng
, _M_param
); }
3669 template<typename _UniformRandomNumberGenerator
>
3671 operator()(_UniformRandomNumberGenerator
& __urng
,
3672 const param_type
& __p
)
3674 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3676 if ((__aurng() - __aurng
.min())
3677 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3682 template<typename _ForwardIterator
,
3683 typename _UniformRandomNumberGenerator
>
3685 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3686 _UniformRandomNumberGenerator
& __urng
)
3687 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3689 template<typename _ForwardIterator
,
3690 typename _UniformRandomNumberGenerator
>
3692 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3693 _UniformRandomNumberGenerator
& __urng
, const param_type
& __p
)
3694 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3696 template<typename _UniformRandomNumberGenerator
>
3698 __generate(result_type
* __f
, result_type
* __t
,
3699 _UniformRandomNumberGenerator
& __urng
,
3700 const param_type
& __p
)
3701 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3704 * @brief Return true if two Bernoulli distributions have
3705 * the same parameters.
3708 operator==(const bernoulli_distribution
& __d1
,
3709 const bernoulli_distribution
& __d2
)
3710 { return __d1
._M_param
== __d2
._M_param
; }
3713 template<typename _ForwardIterator
,
3714 typename _UniformRandomNumberGenerator
>
3716 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3717 _UniformRandomNumberGenerator
& __urng
,
3718 const param_type
& __p
);
3720 param_type _M_param
;
3724 * @brief Return true if two Bernoulli distributions have
3725 * different parameters.
3728 operator!=(const std::bernoulli_distribution
& __d1
,
3729 const std::bernoulli_distribution
& __d2
)
3730 { return !(__d1
== __d2
); }
3733 * @brief Inserts a %bernoulli_distribution random number distribution
3734 * @p __x into the output stream @p __os.
3736 * @param __os An output stream.
3737 * @param __x A %bernoulli_distribution random number distribution.
3739 * @returns The output stream with the state of @p __x inserted or in
3742 template<typename _CharT
, typename _Traits
>
3743 std::basic_ostream
<_CharT
, _Traits
>&
3744 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3745 const std::bernoulli_distribution
& __x
);
3748 * @brief Extracts a %bernoulli_distribution random number distribution
3749 * @p __x from the input stream @p __is.
3751 * @param __is An input stream.
3752 * @param __x A %bernoulli_distribution random number generator engine.
3754 * @returns The input stream with @p __x extracted or in an error state.
3756 template<typename _CharT
, typename _Traits
>
3757 std::basic_istream
<_CharT
, _Traits
>&
3758 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3759 std::bernoulli_distribution
& __x
)
3763 __x
.param(bernoulli_distribution::param_type(__p
));
3769 * @brief A discrete binomial random number distribution.
3771 * The formula for the binomial probability density function is
3772 * @f$p(i|t,p) = \binom{t}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3773 * and @f$p@f$ are the parameters of the distribution.
3775 template<typename _IntType
= int>
3776 class binomial_distribution
3778 static_assert(std::is_integral
<_IntType
>::value
,
3779 "template argument not an integral type");
3782 /** The type of the range of the distribution. */
3783 typedef _IntType result_type
;
3784 /** Parameter type. */
3787 typedef binomial_distribution
<_IntType
> distribution_type
;
3788 friend class binomial_distribution
<_IntType
>;
3791 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3792 : _M_t(__t
), _M_p(__p
)
3794 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3809 operator==(const param_type
& __p1
, const param_type
& __p2
)
3810 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3820 #if _GLIBCXX_USE_C99_MATH_TR1
3821 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3822 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3827 // constructors and member function
3829 binomial_distribution(_IntType __t
= _IntType(1),
3831 : _M_param(__t
, __p
), _M_nd()
3835 binomial_distribution(const param_type
& __p
)
3836 : _M_param(__p
), _M_nd()
3840 * @brief Resets the distribution state.
3847 * @brief Returns the distribution @p t parameter.
3851 { return _M_param
.t(); }
3854 * @brief Returns the distribution @p p parameter.
3858 { return _M_param
.p(); }
3861 * @brief Returns the parameter set of the distribution.
3865 { return _M_param
; }
3868 * @brief Sets the parameter set of the distribution.
3869 * @param __param The new parameter set of the distribution.
3872 param(const param_type
& __param
)
3873 { _M_param
= __param
; }
3876 * @brief Returns the greatest lower bound value of the distribution.
3883 * @brief Returns the least upper bound value of the distribution.
3887 { return _M_param
.t(); }
3890 * @brief Generating functions.
3892 template<typename _UniformRandomNumberGenerator
>
3894 operator()(_UniformRandomNumberGenerator
& __urng
)
3895 { return this->operator()(__urng
, _M_param
); }
3897 template<typename _UniformRandomNumberGenerator
>
3899 operator()(_UniformRandomNumberGenerator
& __urng
,
3900 const param_type
& __p
);
3902 template<typename _ForwardIterator
,
3903 typename _UniformRandomNumberGenerator
>
3905 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3906 _UniformRandomNumberGenerator
& __urng
)
3907 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3909 template<typename _ForwardIterator
,
3910 typename _UniformRandomNumberGenerator
>
3912 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3913 _UniformRandomNumberGenerator
& __urng
,
3914 const param_type
& __p
)
3915 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3917 template<typename _UniformRandomNumberGenerator
>
3919 __generate(result_type
* __f
, result_type
* __t
,
3920 _UniformRandomNumberGenerator
& __urng
,
3921 const param_type
& __p
)
3922 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3925 * @brief Return true if two binomial distributions have
3926 * the same parameters and the sequences that would
3927 * be generated are equal.
3930 operator==(const binomial_distribution
& __d1
,
3931 const binomial_distribution
& __d2
)
3932 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3933 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
3935 { return __d1
._M_param
== __d2
._M_param
; }
3939 * @brief Inserts a %binomial_distribution random number distribution
3940 * @p __x into the output stream @p __os.
3942 * @param __os An output stream.
3943 * @param __x A %binomial_distribution random number distribution.
3945 * @returns The output stream with the state of @p __x inserted or in
3948 template<typename _IntType1
,
3949 typename _CharT
, typename _Traits
>
3950 friend std::basic_ostream
<_CharT
, _Traits
>&
3951 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3952 const std::binomial_distribution
<_IntType1
>& __x
);
3955 * @brief Extracts a %binomial_distribution random number distribution
3956 * @p __x from the input stream @p __is.
3958 * @param __is An input stream.
3959 * @param __x A %binomial_distribution random number generator engine.
3961 * @returns The input stream with @p __x extracted or in an error
3964 template<typename _IntType1
,
3965 typename _CharT
, typename _Traits
>
3966 friend std::basic_istream
<_CharT
, _Traits
>&
3967 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3968 std::binomial_distribution
<_IntType1
>& __x
);
3971 template<typename _ForwardIterator
,
3972 typename _UniformRandomNumberGenerator
>
3974 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3975 _UniformRandomNumberGenerator
& __urng
,
3976 const param_type
& __p
);
3978 template<typename _UniformRandomNumberGenerator
>
3980 _M_waiting(_UniformRandomNumberGenerator
& __urng
,
3981 _IntType __t
, double __q
);
3983 param_type _M_param
;
3985 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3986 std::normal_distribution
<double> _M_nd
;
3990 * @brief Return true if two binomial distributions are different.
3992 template<typename _IntType
>
3994 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
3995 const std::binomial_distribution
<_IntType
>& __d2
)
3996 { return !(__d1
== __d2
); }
4000 * @brief A discrete geometric random number distribution.
4002 * The formula for the geometric probability density function is
4003 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
4006 template<typename _IntType
= int>
4007 class geometric_distribution
4009 static_assert(std::is_integral
<_IntType
>::value
,
4010 "template argument not an integral type");
4013 /** The type of the range of the distribution. */
4014 typedef _IntType result_type
;
4015 /** Parameter type. */
4018 typedef geometric_distribution
<_IntType
> distribution_type
;
4019 friend class geometric_distribution
<_IntType
>;
4022 param_type(double __p
= 0.5)
4025 _GLIBCXX_DEBUG_ASSERT((_M_p
> 0.0) && (_M_p
< 1.0));
4034 operator==(const param_type
& __p1
, const param_type
& __p2
)
4035 { return __p1
._M_p
== __p2
._M_p
; }
4040 { _M_log_1_p
= std::log(1.0 - _M_p
); }
4047 // constructors and member function
4049 geometric_distribution(double __p
= 0.5)
4054 geometric_distribution(const param_type
& __p
)
4059 * @brief Resets the distribution state.
4061 * Does nothing for the geometric distribution.
4067 * @brief Returns the distribution parameter @p p.
4071 { return _M_param
.p(); }
4074 * @brief Returns the parameter set of the distribution.
4078 { return _M_param
; }
4081 * @brief Sets the parameter set of the distribution.
4082 * @param __param The new parameter set of the distribution.
4085 param(const param_type
& __param
)
4086 { _M_param
= __param
; }
4089 * @brief Returns the greatest lower bound value of the distribution.
4096 * @brief Returns the least upper bound value of the distribution.
4100 { return std::numeric_limits
<result_type
>::max(); }
4103 * @brief Generating functions.
4105 template<typename _UniformRandomNumberGenerator
>
4107 operator()(_UniformRandomNumberGenerator
& __urng
)
4108 { return this->operator()(__urng
, _M_param
); }
4110 template<typename _UniformRandomNumberGenerator
>
4112 operator()(_UniformRandomNumberGenerator
& __urng
,
4113 const param_type
& __p
);
4115 template<typename _ForwardIterator
,
4116 typename _UniformRandomNumberGenerator
>
4118 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4119 _UniformRandomNumberGenerator
& __urng
)
4120 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4122 template<typename _ForwardIterator
,
4123 typename _UniformRandomNumberGenerator
>
4125 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4126 _UniformRandomNumberGenerator
& __urng
,
4127 const param_type
& __p
)
4128 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4130 template<typename _UniformRandomNumberGenerator
>
4132 __generate(result_type
* __f
, result_type
* __t
,
4133 _UniformRandomNumberGenerator
& __urng
,
4134 const param_type
& __p
)
4135 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4138 * @brief Return true if two geometric distributions have
4139 * the same parameters.
4142 operator==(const geometric_distribution
& __d1
,
4143 const geometric_distribution
& __d2
)
4144 { return __d1
._M_param
== __d2
._M_param
; }
4147 template<typename _ForwardIterator
,
4148 typename _UniformRandomNumberGenerator
>
4150 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4151 _UniformRandomNumberGenerator
& __urng
,
4152 const param_type
& __p
);
4154 param_type _M_param
;
4158 * @brief Return true if two geometric distributions have
4159 * different parameters.
4161 template<typename _IntType
>
4163 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
4164 const std::geometric_distribution
<_IntType
>& __d2
)
4165 { return !(__d1
== __d2
); }
4168 * @brief Inserts a %geometric_distribution random number distribution
4169 * @p __x into the output stream @p __os.
4171 * @param __os An output stream.
4172 * @param __x A %geometric_distribution random number distribution.
4174 * @returns The output stream with the state of @p __x inserted or in
4177 template<typename _IntType
,
4178 typename _CharT
, typename _Traits
>
4179 std::basic_ostream
<_CharT
, _Traits
>&
4180 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4181 const std::geometric_distribution
<_IntType
>& __x
);
4184 * @brief Extracts a %geometric_distribution random number distribution
4185 * @p __x from the input stream @p __is.
4187 * @param __is An input stream.
4188 * @param __x A %geometric_distribution random number generator engine.
4190 * @returns The input stream with @p __x extracted or in an error state.
4192 template<typename _IntType
,
4193 typename _CharT
, typename _Traits
>
4194 std::basic_istream
<_CharT
, _Traits
>&
4195 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4196 std::geometric_distribution
<_IntType
>& __x
);
4200 * @brief A negative_binomial_distribution random number distribution.
4202 * The formula for the negative binomial probability mass function is
4203 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
4204 * and @f$p@f$ are the parameters of the distribution.
4206 template<typename _IntType
= int>
4207 class negative_binomial_distribution
4209 static_assert(std::is_integral
<_IntType
>::value
,
4210 "template argument not an integral type");
4213 /** The type of the range of the distribution. */
4214 typedef _IntType result_type
;
4215 /** Parameter type. */
4218 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
4221 param_type(_IntType __k
= 1, double __p
= 0.5)
4222 : _M_k(__k
), _M_p(__p
)
4224 _GLIBCXX_DEBUG_ASSERT((_M_k
> 0) && (_M_p
> 0.0) && (_M_p
<= 1.0));
4236 operator==(const param_type
& __p1
, const param_type
& __p2
)
4237 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
4245 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
4246 : _M_param(__k
, __p
), _M_gd(__k
, (1.0 - __p
) / __p
)
4250 negative_binomial_distribution(const param_type
& __p
)
4251 : _M_param(__p
), _M_gd(__p
.k(), (1.0 - __p
.p()) / __p
.p())
4255 * @brief Resets the distribution state.
4262 * @brief Return the @f$k@f$ parameter of the distribution.
4266 { return _M_param
.k(); }
4269 * @brief Return the @f$p@f$ parameter of the distribution.
4273 { return _M_param
.p(); }
4276 * @brief Returns the parameter set of the distribution.
4280 { return _M_param
; }
4283 * @brief Sets the parameter set of the distribution.
4284 * @param __param The new parameter set of the distribution.
4287 param(const param_type
& __param
)
4288 { _M_param
= __param
; }
4291 * @brief Returns the greatest lower bound value of the distribution.
4295 { return result_type(0); }
4298 * @brief Returns the least upper bound value of the distribution.
4302 { return std::numeric_limits
<result_type
>::max(); }
4305 * @brief Generating functions.
4307 template<typename _UniformRandomNumberGenerator
>
4309 operator()(_UniformRandomNumberGenerator
& __urng
);
4311 template<typename _UniformRandomNumberGenerator
>
4313 operator()(_UniformRandomNumberGenerator
& __urng
,
4314 const param_type
& __p
);
4316 template<typename _ForwardIterator
,
4317 typename _UniformRandomNumberGenerator
>
4319 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4320 _UniformRandomNumberGenerator
& __urng
)
4321 { this->__generate_impl(__f
, __t
, __urng
); }
4323 template<typename _ForwardIterator
,
4324 typename _UniformRandomNumberGenerator
>
4326 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4327 _UniformRandomNumberGenerator
& __urng
,
4328 const param_type
& __p
)
4329 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4331 template<typename _UniformRandomNumberGenerator
>
4333 __generate(result_type
* __f
, result_type
* __t
,
4334 _UniformRandomNumberGenerator
& __urng
)
4335 { this->__generate_impl(__f
, __t
, __urng
); }
4337 template<typename _UniformRandomNumberGenerator
>
4339 __generate(result_type
* __f
, result_type
* __t
,
4340 _UniformRandomNumberGenerator
& __urng
,
4341 const param_type
& __p
)
4342 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4345 * @brief Return true if two negative binomial distributions have
4346 * the same parameters and the sequences that would be
4347 * generated are equal.
4350 operator==(const negative_binomial_distribution
& __d1
,
4351 const negative_binomial_distribution
& __d2
)
4352 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
4355 * @brief Inserts a %negative_binomial_distribution random
4356 * number distribution @p __x into the output stream @p __os.
4358 * @param __os An output stream.
4359 * @param __x A %negative_binomial_distribution random number
4362 * @returns The output stream with the state of @p __x inserted or in
4365 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4366 friend std::basic_ostream
<_CharT
, _Traits
>&
4367 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4368 const std::negative_binomial_distribution
<_IntType1
>& __x
);
4371 * @brief Extracts a %negative_binomial_distribution random number
4372 * distribution @p __x from the input stream @p __is.
4374 * @param __is An input stream.
4375 * @param __x A %negative_binomial_distribution random number
4378 * @returns The input stream with @p __x extracted or in an error state.
4380 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4381 friend std::basic_istream
<_CharT
, _Traits
>&
4382 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4383 std::negative_binomial_distribution
<_IntType1
>& __x
);
4386 template<typename _ForwardIterator
,
4387 typename _UniformRandomNumberGenerator
>
4389 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4390 _UniformRandomNumberGenerator
& __urng
);
4391 template<typename _ForwardIterator
,
4392 typename _UniformRandomNumberGenerator
>
4394 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4395 _UniformRandomNumberGenerator
& __urng
,
4396 const param_type
& __p
);
4398 param_type _M_param
;
4400 std::gamma_distribution
<double> _M_gd
;
4404 * @brief Return true if two negative binomial distributions are different.
4406 template<typename _IntType
>
4408 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
4409 const std::negative_binomial_distribution
<_IntType
>& __d2
)
4410 { return !(__d1
== __d2
); }
4413 /* @} */ // group random_distributions_bernoulli
4416 * @addtogroup random_distributions_poisson Poisson Distributions
4417 * @ingroup random_distributions
4422 * @brief A discrete Poisson random number distribution.
4424 * The formula for the Poisson probability density function is
4425 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
4426 * parameter of the distribution.
4428 template<typename _IntType
= int>
4429 class poisson_distribution
4431 static_assert(std::is_integral
<_IntType
>::value
,
4432 "template argument not an integral type");
4435 /** The type of the range of the distribution. */
4436 typedef _IntType result_type
;
4437 /** Parameter type. */
4440 typedef poisson_distribution
<_IntType
> distribution_type
;
4441 friend class poisson_distribution
<_IntType
>;
4444 param_type(double __mean
= 1.0)
4447 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
4456 operator==(const param_type
& __p1
, const param_type
& __p2
)
4457 { return __p1
._M_mean
== __p2
._M_mean
; }
4460 // Hosts either log(mean) or the threshold of the simple method.
4467 #if _GLIBCXX_USE_C99_MATH_TR1
4468 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
4472 // constructors and member function
4474 poisson_distribution(double __mean
= 1.0)
4475 : _M_param(__mean
), _M_nd()
4479 poisson_distribution(const param_type
& __p
)
4480 : _M_param(__p
), _M_nd()
4484 * @brief Resets the distribution state.
4491 * @brief Returns the distribution parameter @p mean.
4495 { return _M_param
.mean(); }
4498 * @brief Returns the parameter set of the distribution.
4502 { return _M_param
; }
4505 * @brief Sets the parameter set of the distribution.
4506 * @param __param The new parameter set of the distribution.
4509 param(const param_type
& __param
)
4510 { _M_param
= __param
; }
4513 * @brief Returns the greatest lower bound value of the distribution.
4520 * @brief Returns the least upper bound value of the distribution.
4524 { return std::numeric_limits
<result_type
>::max(); }
4527 * @brief Generating functions.
4529 template<typename _UniformRandomNumberGenerator
>
4531 operator()(_UniformRandomNumberGenerator
& __urng
)
4532 { return this->operator()(__urng
, _M_param
); }
4534 template<typename _UniformRandomNumberGenerator
>
4536 operator()(_UniformRandomNumberGenerator
& __urng
,
4537 const param_type
& __p
);
4539 template<typename _ForwardIterator
,
4540 typename _UniformRandomNumberGenerator
>
4542 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4543 _UniformRandomNumberGenerator
& __urng
)
4544 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4546 template<typename _ForwardIterator
,
4547 typename _UniformRandomNumberGenerator
>
4549 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4550 _UniformRandomNumberGenerator
& __urng
,
4551 const param_type
& __p
)
4552 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4554 template<typename _UniformRandomNumberGenerator
>
4556 __generate(result_type
* __f
, result_type
* __t
,
4557 _UniformRandomNumberGenerator
& __urng
,
4558 const param_type
& __p
)
4559 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4562 * @brief Return true if two Poisson distributions have the same
4563 * parameters and the sequences that would be generated
4567 operator==(const poisson_distribution
& __d1
,
4568 const poisson_distribution
& __d2
)
4569 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4570 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
4572 { return __d1
._M_param
== __d2
._M_param
; }
4576 * @brief Inserts a %poisson_distribution random number distribution
4577 * @p __x into the output stream @p __os.
4579 * @param __os An output stream.
4580 * @param __x A %poisson_distribution random number distribution.
4582 * @returns The output stream with the state of @p __x inserted or in
4585 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4586 friend std::basic_ostream
<_CharT
, _Traits
>&
4587 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4588 const std::poisson_distribution
<_IntType1
>& __x
);
4591 * @brief Extracts a %poisson_distribution random number distribution
4592 * @p __x from the input stream @p __is.
4594 * @param __is An input stream.
4595 * @param __x A %poisson_distribution random number generator engine.
4597 * @returns The input stream with @p __x extracted or in an error
4600 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4601 friend std::basic_istream
<_CharT
, _Traits
>&
4602 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4603 std::poisson_distribution
<_IntType1
>& __x
);
4606 template<typename _ForwardIterator
,
4607 typename _UniformRandomNumberGenerator
>
4609 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4610 _UniformRandomNumberGenerator
& __urng
,
4611 const param_type
& __p
);
4613 param_type _M_param
;
4615 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4616 std::normal_distribution
<double> _M_nd
;
4620 * @brief Return true if two Poisson distributions are different.
4622 template<typename _IntType
>
4624 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4625 const std::poisson_distribution
<_IntType
>& __d2
)
4626 { return !(__d1
== __d2
); }
4630 * @brief An exponential continuous distribution for random numbers.
4632 * The formula for the exponential probability density function is
4633 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4635 * <table border=1 cellpadding=10 cellspacing=0>
4636 * <caption align=top>Distribution Statistics</caption>
4637 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4638 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4639 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4640 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4641 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4644 template<typename _RealType
= double>
4645 class exponential_distribution
4647 static_assert(std::is_floating_point
<_RealType
>::value
,
4648 "template argument not a floating point type");
4651 /** The type of the range of the distribution. */
4652 typedef _RealType result_type
;
4653 /** Parameter type. */
4656 typedef exponential_distribution
<_RealType
> distribution_type
;
4659 param_type(_RealType __lambda
= _RealType(1))
4660 : _M_lambda(__lambda
)
4662 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
4667 { return _M_lambda
; }
4670 operator==(const param_type
& __p1
, const param_type
& __p2
)
4671 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4674 _RealType _M_lambda
;
4679 * @brief Constructs an exponential distribution with inverse scale
4680 * parameter @f$\lambda@f$.
4683 exponential_distribution(const result_type
& __lambda
= result_type(1))
4684 : _M_param(__lambda
)
4688 exponential_distribution(const param_type
& __p
)
4693 * @brief Resets the distribution state.
4695 * Has no effect on exponential distributions.
4701 * @brief Returns the inverse scale parameter of the distribution.
4705 { return _M_param
.lambda(); }
4708 * @brief Returns the parameter set of the distribution.
4712 { return _M_param
; }
4715 * @brief Sets the parameter set of the distribution.
4716 * @param __param The new parameter set of the distribution.
4719 param(const param_type
& __param
)
4720 { _M_param
= __param
; }
4723 * @brief Returns the greatest lower bound value of the distribution.
4727 { return result_type(0); }
4730 * @brief Returns the least upper bound value of the distribution.
4734 { return std::numeric_limits
<result_type
>::max(); }
4737 * @brief Generating functions.
4739 template<typename _UniformRandomNumberGenerator
>
4741 operator()(_UniformRandomNumberGenerator
& __urng
)
4742 { return this->operator()(__urng
, _M_param
); }
4744 template<typename _UniformRandomNumberGenerator
>
4746 operator()(_UniformRandomNumberGenerator
& __urng
,
4747 const param_type
& __p
)
4749 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4751 return -std::log(result_type(1) - __aurng()) / __p
.lambda();
4754 template<typename _ForwardIterator
,
4755 typename _UniformRandomNumberGenerator
>
4757 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4758 _UniformRandomNumberGenerator
& __urng
)
4759 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4761 template<typename _ForwardIterator
,
4762 typename _UniformRandomNumberGenerator
>
4764 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4765 _UniformRandomNumberGenerator
& __urng
,
4766 const param_type
& __p
)
4767 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4769 template<typename _UniformRandomNumberGenerator
>
4771 __generate(result_type
* __f
, result_type
* __t
,
4772 _UniformRandomNumberGenerator
& __urng
,
4773 const param_type
& __p
)
4774 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4777 * @brief Return true if two exponential distributions have the same
4781 operator==(const exponential_distribution
& __d1
,
4782 const exponential_distribution
& __d2
)
4783 { return __d1
._M_param
== __d2
._M_param
; }
4786 template<typename _ForwardIterator
,
4787 typename _UniformRandomNumberGenerator
>
4789 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4790 _UniformRandomNumberGenerator
& __urng
,
4791 const param_type
& __p
);
4793 param_type _M_param
;
4797 * @brief Return true if two exponential distributions have different
4800 template<typename _RealType
>
4802 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4803 const std::exponential_distribution
<_RealType
>& __d2
)
4804 { return !(__d1
== __d2
); }
4807 * @brief Inserts a %exponential_distribution random number distribution
4808 * @p __x into the output stream @p __os.
4810 * @param __os An output stream.
4811 * @param __x A %exponential_distribution random number distribution.
4813 * @returns The output stream with the state of @p __x inserted or in
4816 template<typename _RealType
, typename _CharT
, typename _Traits
>
4817 std::basic_ostream
<_CharT
, _Traits
>&
4818 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4819 const std::exponential_distribution
<_RealType
>& __x
);
4822 * @brief Extracts a %exponential_distribution random number distribution
4823 * @p __x from the input stream @p __is.
4825 * @param __is An input stream.
4826 * @param __x A %exponential_distribution random number
4829 * @returns The input stream with @p __x extracted or in an error state.
4831 template<typename _RealType
, typename _CharT
, typename _Traits
>
4832 std::basic_istream
<_CharT
, _Traits
>&
4833 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4834 std::exponential_distribution
<_RealType
>& __x
);
4838 * @brief A weibull_distribution random number distribution.
4840 * The formula for the normal probability density function is:
4842 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4843 * \exp{(-(\frac{x}{\beta})^\alpha)}
4846 template<typename _RealType
= double>
4847 class weibull_distribution
4849 static_assert(std::is_floating_point
<_RealType
>::value
,
4850 "template argument not a floating point type");
4853 /** The type of the range of the distribution. */
4854 typedef _RealType result_type
;
4855 /** Parameter type. */
4858 typedef weibull_distribution
<_RealType
> distribution_type
;
4861 param_type(_RealType __a
= _RealType(1),
4862 _RealType __b
= _RealType(1))
4863 : _M_a(__a
), _M_b(__b
)
4875 operator==(const param_type
& __p1
, const param_type
& __p2
)
4876 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4884 weibull_distribution(_RealType __a
= _RealType(1),
4885 _RealType __b
= _RealType(1))
4886 : _M_param(__a
, __b
)
4890 weibull_distribution(const param_type
& __p
)
4895 * @brief Resets the distribution state.
4902 * @brief Return the @f$a@f$ parameter of the distribution.
4906 { return _M_param
.a(); }
4909 * @brief Return the @f$b@f$ parameter of the distribution.
4913 { return _M_param
.b(); }
4916 * @brief Returns the parameter set of the distribution.
4920 { return _M_param
; }
4923 * @brief Sets the parameter set of the distribution.
4924 * @param __param The new parameter set of the distribution.
4927 param(const param_type
& __param
)
4928 { _M_param
= __param
; }
4931 * @brief Returns the greatest lower bound value of the distribution.
4935 { return result_type(0); }
4938 * @brief Returns the least upper bound value of the distribution.
4942 { return std::numeric_limits
<result_type
>::max(); }
4945 * @brief Generating functions.
4947 template<typename _UniformRandomNumberGenerator
>
4949 operator()(_UniformRandomNumberGenerator
& __urng
)
4950 { return this->operator()(__urng
, _M_param
); }
4952 template<typename _UniformRandomNumberGenerator
>
4954 operator()(_UniformRandomNumberGenerator
& __urng
,
4955 const param_type
& __p
);
4957 template<typename _ForwardIterator
,
4958 typename _UniformRandomNumberGenerator
>
4960 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4961 _UniformRandomNumberGenerator
& __urng
)
4962 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4964 template<typename _ForwardIterator
,
4965 typename _UniformRandomNumberGenerator
>
4967 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4968 _UniformRandomNumberGenerator
& __urng
,
4969 const param_type
& __p
)
4970 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4972 template<typename _UniformRandomNumberGenerator
>
4974 __generate(result_type
* __f
, result_type
* __t
,
4975 _UniformRandomNumberGenerator
& __urng
,
4976 const param_type
& __p
)
4977 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4980 * @brief Return true if two Weibull distributions have the same
4984 operator==(const weibull_distribution
& __d1
,
4985 const weibull_distribution
& __d2
)
4986 { return __d1
._M_param
== __d2
._M_param
; }
4989 template<typename _ForwardIterator
,
4990 typename _UniformRandomNumberGenerator
>
4992 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4993 _UniformRandomNumberGenerator
& __urng
,
4994 const param_type
& __p
);
4996 param_type _M_param
;
5000 * @brief Return true if two Weibull distributions have different
5003 template<typename _RealType
>
5005 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
5006 const std::weibull_distribution
<_RealType
>& __d2
)
5007 { return !(__d1
== __d2
); }
5010 * @brief Inserts a %weibull_distribution random number distribution
5011 * @p __x into the output stream @p __os.
5013 * @param __os An output stream.
5014 * @param __x A %weibull_distribution random number distribution.
5016 * @returns The output stream with the state of @p __x inserted or in
5019 template<typename _RealType
, typename _CharT
, typename _Traits
>
5020 std::basic_ostream
<_CharT
, _Traits
>&
5021 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5022 const std::weibull_distribution
<_RealType
>& __x
);
5025 * @brief Extracts a %weibull_distribution random number distribution
5026 * @p __x from the input stream @p __is.
5028 * @param __is An input stream.
5029 * @param __x A %weibull_distribution random number
5032 * @returns The input stream with @p __x extracted or in an error state.
5034 template<typename _RealType
, typename _CharT
, typename _Traits
>
5035 std::basic_istream
<_CharT
, _Traits
>&
5036 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5037 std::weibull_distribution
<_RealType
>& __x
);
5041 * @brief A extreme_value_distribution random number distribution.
5043 * The formula for the normal probability mass function is
5045 * p(x|a,b) = \frac{1}{b}
5046 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
5049 template<typename _RealType
= double>
5050 class extreme_value_distribution
5052 static_assert(std::is_floating_point
<_RealType
>::value
,
5053 "template argument not a floating point type");
5056 /** The type of the range of the distribution. */
5057 typedef _RealType result_type
;
5058 /** Parameter type. */
5061 typedef extreme_value_distribution
<_RealType
> distribution_type
;
5064 param_type(_RealType __a
= _RealType(0),
5065 _RealType __b
= _RealType(1))
5066 : _M_a(__a
), _M_b(__b
)
5078 operator==(const param_type
& __p1
, const param_type
& __p2
)
5079 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
5087 extreme_value_distribution(_RealType __a
= _RealType(0),
5088 _RealType __b
= _RealType(1))
5089 : _M_param(__a
, __b
)
5093 extreme_value_distribution(const param_type
& __p
)
5098 * @brief Resets the distribution state.
5105 * @brief Return the @f$a@f$ parameter of the distribution.
5109 { return _M_param
.a(); }
5112 * @brief Return the @f$b@f$ parameter of the distribution.
5116 { return _M_param
.b(); }
5119 * @brief Returns the parameter set of the distribution.
5123 { return _M_param
; }
5126 * @brief Sets the parameter set of the distribution.
5127 * @param __param The new parameter set of the distribution.
5130 param(const param_type
& __param
)
5131 { _M_param
= __param
; }
5134 * @brief Returns the greatest lower bound value of the distribution.
5138 { return std::numeric_limits
<result_type
>::lowest(); }
5141 * @brief Returns the least upper bound value of the distribution.
5145 { return std::numeric_limits
<result_type
>::max(); }
5148 * @brief Generating functions.
5150 template<typename _UniformRandomNumberGenerator
>
5152 operator()(_UniformRandomNumberGenerator
& __urng
)
5153 { return this->operator()(__urng
, _M_param
); }
5155 template<typename _UniformRandomNumberGenerator
>
5157 operator()(_UniformRandomNumberGenerator
& __urng
,
5158 const param_type
& __p
);
5160 template<typename _ForwardIterator
,
5161 typename _UniformRandomNumberGenerator
>
5163 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5164 _UniformRandomNumberGenerator
& __urng
)
5165 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5167 template<typename _ForwardIterator
,
5168 typename _UniformRandomNumberGenerator
>
5170 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5171 _UniformRandomNumberGenerator
& __urng
,
5172 const param_type
& __p
)
5173 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5175 template<typename _UniformRandomNumberGenerator
>
5177 __generate(result_type
* __f
, result_type
* __t
,
5178 _UniformRandomNumberGenerator
& __urng
,
5179 const param_type
& __p
)
5180 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5183 * @brief Return true if two extreme value distributions have the same
5187 operator==(const extreme_value_distribution
& __d1
,
5188 const extreme_value_distribution
& __d2
)
5189 { return __d1
._M_param
== __d2
._M_param
; }
5192 template<typename _ForwardIterator
,
5193 typename _UniformRandomNumberGenerator
>
5195 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5196 _UniformRandomNumberGenerator
& __urng
,
5197 const param_type
& __p
);
5199 param_type _M_param
;
5203 * @brief Return true if two extreme value distributions have different
5206 template<typename _RealType
>
5208 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
5209 const std::extreme_value_distribution
<_RealType
>& __d2
)
5210 { return !(__d1
== __d2
); }
5213 * @brief Inserts a %extreme_value_distribution random number distribution
5214 * @p __x into the output stream @p __os.
5216 * @param __os An output stream.
5217 * @param __x A %extreme_value_distribution random number distribution.
5219 * @returns The output stream with the state of @p __x inserted or in
5222 template<typename _RealType
, typename _CharT
, typename _Traits
>
5223 std::basic_ostream
<_CharT
, _Traits
>&
5224 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5225 const std::extreme_value_distribution
<_RealType
>& __x
);
5228 * @brief Extracts a %extreme_value_distribution random number
5229 * distribution @p __x from the input stream @p __is.
5231 * @param __is An input stream.
5232 * @param __x A %extreme_value_distribution random number
5235 * @returns The input stream with @p __x extracted or in an error state.
5237 template<typename _RealType
, typename _CharT
, typename _Traits
>
5238 std::basic_istream
<_CharT
, _Traits
>&
5239 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5240 std::extreme_value_distribution
<_RealType
>& __x
);
5244 * @brief A discrete_distribution random number distribution.
5246 * The formula for the discrete probability mass function is
5249 template<typename _IntType
= int>
5250 class discrete_distribution
5252 static_assert(std::is_integral
<_IntType
>::value
,
5253 "template argument not an integral type");
5256 /** The type of the range of the distribution. */
5257 typedef _IntType result_type
;
5258 /** Parameter type. */
5261 typedef discrete_distribution
<_IntType
> distribution_type
;
5262 friend class discrete_distribution
<_IntType
>;
5265 : _M_prob(), _M_cp()
5268 template<typename _InputIterator
>
5269 param_type(_InputIterator __wbegin
,
5270 _InputIterator __wend
)
5271 : _M_prob(__wbegin
, __wend
), _M_cp()
5272 { _M_initialize(); }
5274 param_type(initializer_list
<double> __wil
)
5275 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
5276 { _M_initialize(); }
5278 template<typename _Func
>
5279 param_type(size_t __nw
, double __xmin
, double __xmax
,
5282 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5283 param_type(const param_type
&) = default;
5284 param_type
& operator=(const param_type
&) = default;
5287 probabilities() const
5288 { return _M_prob
.empty() ? std::vector
<double>(1, 1.0) : _M_prob
; }
5291 operator==(const param_type
& __p1
, const param_type
& __p2
)
5292 { return __p1
._M_prob
== __p2
._M_prob
; }
5298 std::vector
<double> _M_prob
;
5299 std::vector
<double> _M_cp
;
5302 discrete_distribution()
5306 template<typename _InputIterator
>
5307 discrete_distribution(_InputIterator __wbegin
,
5308 _InputIterator __wend
)
5309 : _M_param(__wbegin
, __wend
)
5312 discrete_distribution(initializer_list
<double> __wl
)
5316 template<typename _Func
>
5317 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
5319 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5323 discrete_distribution(const param_type
& __p
)
5328 * @brief Resets the distribution state.
5335 * @brief Returns the probabilities of the distribution.
5338 probabilities() const
5340 return _M_param
._M_prob
.empty()
5341 ? std::vector
<double>(1, 1.0) : _M_param
._M_prob
;
5345 * @brief Returns the parameter set of the distribution.
5349 { return _M_param
; }
5352 * @brief Sets the parameter set of the distribution.
5353 * @param __param The new parameter set of the distribution.
5356 param(const param_type
& __param
)
5357 { _M_param
= __param
; }
5360 * @brief Returns the greatest lower bound value of the distribution.
5364 { return result_type(0); }
5367 * @brief Returns the least upper bound value of the distribution.
5372 return _M_param
._M_prob
.empty()
5373 ? result_type(0) : result_type(_M_param
._M_prob
.size() - 1);
5377 * @brief Generating functions.
5379 template<typename _UniformRandomNumberGenerator
>
5381 operator()(_UniformRandomNumberGenerator
& __urng
)
5382 { return this->operator()(__urng
, _M_param
); }
5384 template<typename _UniformRandomNumberGenerator
>
5386 operator()(_UniformRandomNumberGenerator
& __urng
,
5387 const param_type
& __p
);
5389 template<typename _ForwardIterator
,
5390 typename _UniformRandomNumberGenerator
>
5392 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5393 _UniformRandomNumberGenerator
& __urng
)
5394 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5396 template<typename _ForwardIterator
,
5397 typename _UniformRandomNumberGenerator
>
5399 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5400 _UniformRandomNumberGenerator
& __urng
,
5401 const param_type
& __p
)
5402 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5404 template<typename _UniformRandomNumberGenerator
>
5406 __generate(result_type
* __f
, result_type
* __t
,
5407 _UniformRandomNumberGenerator
& __urng
,
5408 const param_type
& __p
)
5409 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5412 * @brief Return true if two discrete distributions have the same
5416 operator==(const discrete_distribution
& __d1
,
5417 const discrete_distribution
& __d2
)
5418 { return __d1
._M_param
== __d2
._M_param
; }
5421 * @brief Inserts a %discrete_distribution random number distribution
5422 * @p __x into the output stream @p __os.
5424 * @param __os An output stream.
5425 * @param __x A %discrete_distribution random number distribution.
5427 * @returns The output stream with the state of @p __x inserted or in
5430 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5431 friend std::basic_ostream
<_CharT
, _Traits
>&
5432 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5433 const std::discrete_distribution
<_IntType1
>& __x
);
5436 * @brief Extracts a %discrete_distribution random number distribution
5437 * @p __x from the input stream @p __is.
5439 * @param __is An input stream.
5440 * @param __x A %discrete_distribution random number
5443 * @returns The input stream with @p __x extracted or in an error
5446 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5447 friend std::basic_istream
<_CharT
, _Traits
>&
5448 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5449 std::discrete_distribution
<_IntType1
>& __x
);
5452 template<typename _ForwardIterator
,
5453 typename _UniformRandomNumberGenerator
>
5455 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5456 _UniformRandomNumberGenerator
& __urng
,
5457 const param_type
& __p
);
5459 param_type _M_param
;
5463 * @brief Return true if two discrete distributions have different
5466 template<typename _IntType
>
5468 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
5469 const std::discrete_distribution
<_IntType
>& __d2
)
5470 { return !(__d1
== __d2
); }
5474 * @brief A piecewise_constant_distribution random number distribution.
5476 * The formula for the piecewise constant probability mass function is
5479 template<typename _RealType
= double>
5480 class piecewise_constant_distribution
5482 static_assert(std::is_floating_point
<_RealType
>::value
,
5483 "template argument not a floating point type");
5486 /** The type of the range of the distribution. */
5487 typedef _RealType result_type
;
5488 /** Parameter type. */
5491 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
5492 friend class piecewise_constant_distribution
<_RealType
>;
5495 : _M_int(), _M_den(), _M_cp()
5498 template<typename _InputIteratorB
, typename _InputIteratorW
>
5499 param_type(_InputIteratorB __bfirst
,
5500 _InputIteratorB __bend
,
5501 _InputIteratorW __wbegin
);
5503 template<typename _Func
>
5504 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
5506 template<typename _Func
>
5507 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5510 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5511 param_type(const param_type
&) = default;
5512 param_type
& operator=(const param_type
&) = default;
5514 std::vector
<_RealType
>
5519 std::vector
<_RealType
> __tmp(2);
5520 __tmp
[1] = _RealType(1);
5529 { return _M_den
.empty() ? std::vector
<double>(1, 1.0) : _M_den
; }
5532 operator==(const param_type
& __p1
, const param_type
& __p2
)
5533 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
5539 std::vector
<_RealType
> _M_int
;
5540 std::vector
<double> _M_den
;
5541 std::vector
<double> _M_cp
;
5545 piecewise_constant_distribution()
5549 template<typename _InputIteratorB
, typename _InputIteratorW
>
5550 piecewise_constant_distribution(_InputIteratorB __bfirst
,
5551 _InputIteratorB __bend
,
5552 _InputIteratorW __wbegin
)
5553 : _M_param(__bfirst
, __bend
, __wbegin
)
5556 template<typename _Func
>
5557 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
5559 : _M_param(__bl
, __fw
)
5562 template<typename _Func
>
5563 piecewise_constant_distribution(size_t __nw
,
5564 _RealType __xmin
, _RealType __xmax
,
5566 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5570 piecewise_constant_distribution(const param_type
& __p
)
5575 * @brief Resets the distribution state.
5582 * @brief Returns a vector of the intervals.
5584 std::vector
<_RealType
>
5587 if (_M_param
._M_int
.empty())
5589 std::vector
<_RealType
> __tmp(2);
5590 __tmp
[1] = _RealType(1);
5594 return _M_param
._M_int
;
5598 * @brief Returns a vector of the probability densities.
5603 return _M_param
._M_den
.empty()
5604 ? std::vector
<double>(1, 1.0) : _M_param
._M_den
;
5608 * @brief Returns the parameter set of the distribution.
5612 { return _M_param
; }
5615 * @brief Sets the parameter set of the distribution.
5616 * @param __param The new parameter set of the distribution.
5619 param(const param_type
& __param
)
5620 { _M_param
= __param
; }
5623 * @brief Returns the greatest lower bound value of the distribution.
5628 return _M_param
._M_int
.empty()
5629 ? result_type(0) : _M_param
._M_int
.front();
5633 * @brief Returns the least upper bound value of the distribution.
5638 return _M_param
._M_int
.empty()
5639 ? result_type(1) : _M_param
._M_int
.back();
5643 * @brief Generating functions.
5645 template<typename _UniformRandomNumberGenerator
>
5647 operator()(_UniformRandomNumberGenerator
& __urng
)
5648 { return this->operator()(__urng
, _M_param
); }
5650 template<typename _UniformRandomNumberGenerator
>
5652 operator()(_UniformRandomNumberGenerator
& __urng
,
5653 const param_type
& __p
);
5655 template<typename _ForwardIterator
,
5656 typename _UniformRandomNumberGenerator
>
5658 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5659 _UniformRandomNumberGenerator
& __urng
)
5660 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5662 template<typename _ForwardIterator
,
5663 typename _UniformRandomNumberGenerator
>
5665 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5666 _UniformRandomNumberGenerator
& __urng
,
5667 const param_type
& __p
)
5668 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5670 template<typename _UniformRandomNumberGenerator
>
5672 __generate(result_type
* __f
, result_type
* __t
,
5673 _UniformRandomNumberGenerator
& __urng
,
5674 const param_type
& __p
)
5675 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5678 * @brief Return true if two piecewise constant distributions have the
5682 operator==(const piecewise_constant_distribution
& __d1
,
5683 const piecewise_constant_distribution
& __d2
)
5684 { return __d1
._M_param
== __d2
._M_param
; }
5687 * @brief Inserts a %piecewise_constant_distribution random
5688 * number distribution @p __x into the output stream @p __os.
5690 * @param __os An output stream.
5691 * @param __x A %piecewise_constant_distribution random number
5694 * @returns The output stream with the state of @p __x inserted or in
5697 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5698 friend std::basic_ostream
<_CharT
, _Traits
>&
5699 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5700 const std::piecewise_constant_distribution
<_RealType1
>& __x
);
5703 * @brief Extracts a %piecewise_constant_distribution random
5704 * number distribution @p __x from the input stream @p __is.
5706 * @param __is An input stream.
5707 * @param __x A %piecewise_constant_distribution random number
5710 * @returns The input stream with @p __x extracted or in an error
5713 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5714 friend std::basic_istream
<_CharT
, _Traits
>&
5715 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5716 std::piecewise_constant_distribution
<_RealType1
>& __x
);
5719 template<typename _ForwardIterator
,
5720 typename _UniformRandomNumberGenerator
>
5722 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5723 _UniformRandomNumberGenerator
& __urng
,
5724 const param_type
& __p
);
5726 param_type _M_param
;
5730 * @brief Return true if two piecewise constant distributions have
5731 * different parameters.
5733 template<typename _RealType
>
5735 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5736 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5737 { return !(__d1
== __d2
); }
5741 * @brief A piecewise_linear_distribution random number distribution.
5743 * The formula for the piecewise linear probability mass function is
5746 template<typename _RealType
= double>
5747 class piecewise_linear_distribution
5749 static_assert(std::is_floating_point
<_RealType
>::value
,
5750 "template argument not a floating point type");
5753 /** The type of the range of the distribution. */
5754 typedef _RealType result_type
;
5755 /** Parameter type. */
5758 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5759 friend class piecewise_linear_distribution
<_RealType
>;
5762 : _M_int(), _M_den(), _M_cp(), _M_m()
5765 template<typename _InputIteratorB
, typename _InputIteratorW
>
5766 param_type(_InputIteratorB __bfirst
,
5767 _InputIteratorB __bend
,
5768 _InputIteratorW __wbegin
);
5770 template<typename _Func
>
5771 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5773 template<typename _Func
>
5774 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5777 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5778 param_type(const param_type
&) = default;
5779 param_type
& operator=(const param_type
&) = default;
5781 std::vector
<_RealType
>
5786 std::vector
<_RealType
> __tmp(2);
5787 __tmp
[1] = _RealType(1);
5796 { return _M_den
.empty() ? std::vector
<double>(2, 1.0) : _M_den
; }
5799 operator==(const param_type
& __p1
, const param_type
& __p2
)
5800 { return (__p1
._M_int
== __p2
._M_int
5801 && __p1
._M_den
== __p2
._M_den
); }
5807 std::vector
<_RealType
> _M_int
;
5808 std::vector
<double> _M_den
;
5809 std::vector
<double> _M_cp
;
5810 std::vector
<double> _M_m
;
5814 piecewise_linear_distribution()
5818 template<typename _InputIteratorB
, typename _InputIteratorW
>
5819 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5820 _InputIteratorB __bend
,
5821 _InputIteratorW __wbegin
)
5822 : _M_param(__bfirst
, __bend
, __wbegin
)
5825 template<typename _Func
>
5826 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5828 : _M_param(__bl
, __fw
)
5831 template<typename _Func
>
5832 piecewise_linear_distribution(size_t __nw
,
5833 _RealType __xmin
, _RealType __xmax
,
5835 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5839 piecewise_linear_distribution(const param_type
& __p
)
5844 * Resets the distribution state.
5851 * @brief Return the intervals of the distribution.
5853 std::vector
<_RealType
>
5856 if (_M_param
._M_int
.empty())
5858 std::vector
<_RealType
> __tmp(2);
5859 __tmp
[1] = _RealType(1);
5863 return _M_param
._M_int
;
5867 * @brief Return a vector of the probability densities of the
5873 return _M_param
._M_den
.empty()
5874 ? std::vector
<double>(2, 1.0) : _M_param
._M_den
;
5878 * @brief Returns the parameter set of the distribution.
5882 { return _M_param
; }
5885 * @brief Sets the parameter set of the distribution.
5886 * @param __param The new parameter set of the distribution.
5889 param(const param_type
& __param
)
5890 { _M_param
= __param
; }
5893 * @brief Returns the greatest lower bound value of the distribution.
5898 return _M_param
._M_int
.empty()
5899 ? result_type(0) : _M_param
._M_int
.front();
5903 * @brief Returns the least upper bound value of the distribution.
5908 return _M_param
._M_int
.empty()
5909 ? result_type(1) : _M_param
._M_int
.back();
5913 * @brief Generating functions.
5915 template<typename _UniformRandomNumberGenerator
>
5917 operator()(_UniformRandomNumberGenerator
& __urng
)
5918 { return this->operator()(__urng
, _M_param
); }
5920 template<typename _UniformRandomNumberGenerator
>
5922 operator()(_UniformRandomNumberGenerator
& __urng
,
5923 const param_type
& __p
);
5925 template<typename _ForwardIterator
,
5926 typename _UniformRandomNumberGenerator
>
5928 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5929 _UniformRandomNumberGenerator
& __urng
)
5930 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5932 template<typename _ForwardIterator
,
5933 typename _UniformRandomNumberGenerator
>
5935 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5936 _UniformRandomNumberGenerator
& __urng
,
5937 const param_type
& __p
)
5938 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5940 template<typename _UniformRandomNumberGenerator
>
5942 __generate(result_type
* __f
, result_type
* __t
,
5943 _UniformRandomNumberGenerator
& __urng
,
5944 const param_type
& __p
)
5945 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5948 * @brief Return true if two piecewise linear distributions have the
5952 operator==(const piecewise_linear_distribution
& __d1
,
5953 const piecewise_linear_distribution
& __d2
)
5954 { return __d1
._M_param
== __d2
._M_param
; }
5957 * @brief Inserts a %piecewise_linear_distribution random number
5958 * distribution @p __x into the output stream @p __os.
5960 * @param __os An output stream.
5961 * @param __x A %piecewise_linear_distribution random number
5964 * @returns The output stream with the state of @p __x inserted or in
5967 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5968 friend std::basic_ostream
<_CharT
, _Traits
>&
5969 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5970 const std::piecewise_linear_distribution
<_RealType1
>& __x
);
5973 * @brief Extracts a %piecewise_linear_distribution random number
5974 * distribution @p __x from the input stream @p __is.
5976 * @param __is An input stream.
5977 * @param __x A %piecewise_linear_distribution random number
5980 * @returns The input stream with @p __x extracted or in an error
5983 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5984 friend std::basic_istream
<_CharT
, _Traits
>&
5985 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5986 std::piecewise_linear_distribution
<_RealType1
>& __x
);
5989 template<typename _ForwardIterator
,
5990 typename _UniformRandomNumberGenerator
>
5992 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5993 _UniformRandomNumberGenerator
& __urng
,
5994 const param_type
& __p
);
5996 param_type _M_param
;
6000 * @brief Return true if two piecewise linear distributions have
6001 * different parameters.
6003 template<typename _RealType
>
6005 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
6006 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
6007 { return !(__d1
== __d2
); }
6010 /* @} */ // group random_distributions_poisson
6012 /* @} */ // group random_distributions
6015 * @addtogroup random_utilities Random Number Utilities
6021 * @brief The seed_seq class generates sequences of seeds for random
6022 * number generators.
6027 /** The type of the seed vales. */
6028 typedef uint_least32_t result_type
;
6030 /** Default constructor. */
6035 template<typename _IntType
>
6036 seed_seq(std::initializer_list
<_IntType
> il
);
6038 template<typename _InputIterator
>
6039 seed_seq(_InputIterator __begin
, _InputIterator __end
);
6041 // generating functions
6042 template<typename _RandomAccessIterator
>
6044 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
6046 // property functions
6047 size_t size() const noexcept
6048 { return _M_v
.size(); }
6050 template<typename OutputIterator
>
6052 param(OutputIterator __dest
) const
6053 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
6055 // no copy functions
6056 seed_seq(const seed_seq
&) = delete;
6057 seed_seq
& operator=(const seed_seq
&) = delete;
6060 std::vector
<result_type
> _M_v
;
6063 /* @} */ // group random_utilities
6065 /* @} */ // group random
6067 _GLIBCXX_END_NAMESPACE_VERSION