1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009-2018 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}
35 #include <bits/uniform_int_dist.h>
37 namespace std
_GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 // [26.4] Random number generation
44 * @defgroup random Random Number Generation
47 * A facility for generating random numbers on selected distributions.
52 * @brief A function template for converting the output of a (integral)
53 * uniform random number generator to a floatng point result in the range
56 template<typename _RealType
, size_t __bits
,
57 typename _UniformRandomNumberGenerator
>
59 generate_canonical(_UniformRandomNumberGenerator
& __g
);
62 * Implementation-space details.
66 template<typename _UIntType
, size_t __w
,
67 bool = __w
< static_cast<size_t>
68 (std::numeric_limits
<_UIntType
>::digits
)>
70 { static const _UIntType __value
= 0; };
72 template<typename _UIntType
, size_t __w
>
73 struct _Shift
<_UIntType
, __w
, true>
74 { static const _UIntType __value
= _UIntType(1) << __w
; };
77 int __which
= ((__s
<= __CHAR_BIT__
* sizeof (int))
78 + (__s
<= __CHAR_BIT__
* sizeof (long))
79 + (__s
<= __CHAR_BIT__
* sizeof (long long))
80 /* assume long long no bigger than __int128 */
82 struct _Select_uint_least_t
84 static_assert(__which
< 0, /* needs to be dependent */
85 "sorry, would be too much trouble for a slow result");
89 struct _Select_uint_least_t
<__s
, 4>
90 { typedef unsigned int type
; };
93 struct _Select_uint_least_t
<__s
, 3>
94 { typedef unsigned long type
; };
97 struct _Select_uint_least_t
<__s
, 2>
98 { typedef unsigned long long type
; };
100 #ifdef _GLIBCXX_USE_INT128
102 struct _Select_uint_least_t
<__s
, 1>
103 { typedef unsigned __int128 type
; };
106 // Assume a != 0, a < m, c < m, x < m.
107 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
,
108 bool __big_enough
= (!(__m
& (__m
- 1))
109 || (_Tp(-1) - __c
) / __a
>= __m
- 1),
110 bool __schrage_ok
= __m
% __a
< __m
/ __a
>
113 typedef typename _Select_uint_least_t
<std::__lg(__a
)
114 + std::__lg(__m
) + 2>::type _Tp2
;
117 { return static_cast<_Tp
>((_Tp2(__a
) * __x
+ __c
) % __m
); }
121 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
>
122 struct _Mod
<_Tp
, __m
, __a
, __c
, false, true>
129 // - for m == 2^n or m == 0, unsigned integer overflow is safe.
130 // - a * (m - 1) + c fits in _Tp, there is no overflow.
131 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool __s
>
132 struct _Mod
<_Tp
, __m
, __a
, __c
, true, __s
>
137 _Tp __res
= __a
* __x
+ __c
;
144 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
147 { return _Mod
<_Tp
, __m
, __a
, __c
>::__calc(__x
); }
150 * An adaptor class for converting the output of any Generator into
151 * the input for a specific Distribution.
153 template<typename _Engine
, typename _DInputType
>
156 static_assert(std::is_floating_point
<_DInputType
>::value
,
157 "template argument must be a floating point type");
160 _Adaptor(_Engine
& __g
)
165 { return _DInputType(0); }
169 { return _DInputType(1); }
172 * Converts a value generated by the adapted random number generator
173 * into a value in the input domain for the dependent random number
179 return std::generate_canonical
<_DInputType
,
180 std::numeric_limits
<_DInputType
>::digits
,
188 } // namespace __detail
191 * @addtogroup random_generators Random Number Generators
194 * These classes define objects which provide random or pseudorandom
195 * numbers, either from a discrete or a continuous interval. The
196 * random number generator supplied as a part of this library are
197 * all uniform random number generators which provide a sequence of
198 * random number uniformly distributed over their range.
200 * A number generator is a function object with an operator() that
201 * takes zero arguments and returns a number.
203 * A compliant random number generator must satisfy the following
204 * requirements. <table border=1 cellpadding=10 cellspacing=0>
205 * <caption align=top>Random Number Generator Requirements</caption>
206 * <tr><td>To be documented.</td></tr> </table>
212 * @brief A model of a linear congruential random number generator.
214 * A random number generator that produces pseudorandom numbers via
217 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
220 * The template parameter @p _UIntType must be an unsigned integral type
221 * large enough to store values up to (__m-1). If the template parameter
222 * @p __m is 0, the modulus @p __m used is
223 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
224 * parameters @p __a and @p __c must be less than @p __m.
226 * The size of the state is @f$1@f$.
228 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
229 class linear_congruential_engine
231 static_assert(std::is_unsigned
<_UIntType
>::value
,
232 "result_type must be an unsigned integral type");
233 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
234 "template argument substituting __m out of bounds");
237 /** The type of the generated random value. */
238 typedef _UIntType result_type
;
240 /** The multiplier. */
241 static constexpr result_type multiplier
= __a
;
243 static constexpr result_type increment
= __c
;
245 static constexpr result_type modulus
= __m
;
246 static constexpr result_type default_seed
= 1u;
249 * @brief Constructs a %linear_congruential_engine random number
250 * generator engine with seed @p __s. The default seed value
253 * @param __s The initial seed value.
256 linear_congruential_engine(result_type __s
= default_seed
)
260 * @brief Constructs a %linear_congruential_engine random number
261 * generator engine seeded from the seed sequence @p __q.
263 * @param __q the seed sequence.
265 template<typename _Sseq
, typename
= typename
266 std::enable_if
<!std::is_same
<_Sseq
, linear_congruential_engine
>::value
>
269 linear_congruential_engine(_Sseq
& __q
)
273 * @brief Reseeds the %linear_congruential_engine random number generator
274 * engine sequence to the seed @p __s.
276 * @param __s The new seed.
279 seed(result_type __s
= default_seed
);
282 * @brief Reseeds the %linear_congruential_engine random number generator
284 * sequence using values from the seed sequence @p __q.
286 * @param __q the seed sequence.
288 template<typename _Sseq
>
289 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
293 * @brief Gets the smallest possible value in the output range.
295 * The minimum depends on the @p __c parameter: if it is zero, the
296 * minimum generated must be > 0, otherwise 0 is allowed.
298 static constexpr result_type
300 { return __c
== 0u ? 1u : 0u; }
303 * @brief Gets the largest possible value in the output range.
305 static constexpr result_type
310 * @brief Discard a sequence of random numbers.
313 discard(unsigned long long __z
)
315 for (; __z
!= 0ULL; --__z
)
320 * @brief Gets the next random number in the sequence.
325 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
330 * @brief Compares two linear congruential random number generator
331 * objects of the same type for equality.
333 * @param __lhs A linear congruential random number generator object.
334 * @param __rhs Another linear congruential random number generator
337 * @returns true if the infinite sequences of generated values
338 * would be equal, false otherwise.
341 operator==(const linear_congruential_engine
& __lhs
,
342 const linear_congruential_engine
& __rhs
)
343 { return __lhs
._M_x
== __rhs
._M_x
; }
346 * @brief Writes the textual representation of the state x(i) of x to
349 * @param __os The output stream.
350 * @param __lcr A % linear_congruential_engine random number generator.
353 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
354 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
355 friend std::basic_ostream
<_CharT
, _Traits
>&
356 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
357 const std::linear_congruential_engine
<_UIntType1
,
358 __a1
, __c1
, __m1
>& __lcr
);
361 * @brief Sets the state of the engine by reading its textual
362 * representation from @p __is.
364 * The textual representation must have been previously written using
365 * an output stream whose imbued locale and whose type's template
366 * specialization arguments _CharT and _Traits were the same as those
369 * @param __is The input stream.
370 * @param __lcr A % linear_congruential_engine random number generator.
373 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
374 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
375 friend std::basic_istream
<_CharT
, _Traits
>&
376 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
377 std::linear_congruential_engine
<_UIntType1
, __a1
,
385 * @brief Compares two linear congruential random number generator
386 * objects of the same type for inequality.
388 * @param __lhs A linear congruential random number generator object.
389 * @param __rhs Another linear congruential random number generator
392 * @returns true if the infinite sequences of generated values
393 * would be different, false otherwise.
395 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
397 operator!=(const std::linear_congruential_engine
<_UIntType
, __a
,
399 const std::linear_congruential_engine
<_UIntType
, __a
,
401 { return !(__lhs
== __rhs
); }
405 * A generalized feedback shift register discrete random number generator.
407 * This algorithm avoids multiplication and division and is designed to be
408 * friendly to a pipelined architecture. If the parameters are chosen
409 * correctly, this generator will produce numbers with a very long period and
410 * fairly good apparent entropy, although still not cryptographically strong.
412 * The best way to use this generator is with the predefined mt19937 class.
414 * This algorithm was originally invented by Makoto Matsumoto and
417 * @tparam __w Word size, the number of bits in each element of
419 * @tparam __n The degree of recursion.
420 * @tparam __m The period parameter.
421 * @tparam __r The separation point bit index.
422 * @tparam __a The last row of the twist matrix.
423 * @tparam __u The first right-shift tempering matrix parameter.
424 * @tparam __d The first right-shift tempering matrix mask.
425 * @tparam __s The first left-shift tempering matrix parameter.
426 * @tparam __b The first left-shift tempering matrix mask.
427 * @tparam __t The second left-shift tempering matrix parameter.
428 * @tparam __c The second left-shift tempering matrix mask.
429 * @tparam __l The second right-shift tempering matrix parameter.
430 * @tparam __f Initialization multiplier.
432 template<typename _UIntType
, size_t __w
,
433 size_t __n
, size_t __m
, size_t __r
,
434 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
435 _UIntType __b
, size_t __t
,
436 _UIntType __c
, size_t __l
, _UIntType __f
>
437 class mersenne_twister_engine
439 static_assert(std::is_unsigned
<_UIntType
>::value
,
440 "result_type must be an unsigned integral type");
441 static_assert(1u <= __m
&& __m
<= __n
,
442 "template argument substituting __m out of bounds");
443 static_assert(__r
<= __w
, "template argument substituting "
445 static_assert(__u
<= __w
, "template argument substituting "
447 static_assert(__s
<= __w
, "template argument substituting "
449 static_assert(__t
<= __w
, "template argument substituting "
451 static_assert(__l
<= __w
, "template argument substituting "
453 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
454 "template argument substituting __w out of bound");
455 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
456 "template argument substituting __a out of bound");
457 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
458 "template argument substituting __b out of bound");
459 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
460 "template argument substituting __c out of bound");
461 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
462 "template argument substituting __d out of bound");
463 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
464 "template argument substituting __f out of bound");
467 /** The type of the generated random value. */
468 typedef _UIntType result_type
;
471 static constexpr size_t word_size
= __w
;
472 static constexpr size_t state_size
= __n
;
473 static constexpr size_t shift_size
= __m
;
474 static constexpr size_t mask_bits
= __r
;
475 static constexpr result_type xor_mask
= __a
;
476 static constexpr size_t tempering_u
= __u
;
477 static constexpr result_type tempering_d
= __d
;
478 static constexpr size_t tempering_s
= __s
;
479 static constexpr result_type tempering_b
= __b
;
480 static constexpr size_t tempering_t
= __t
;
481 static constexpr result_type tempering_c
= __c
;
482 static constexpr size_t tempering_l
= __l
;
483 static constexpr result_type initialization_multiplier
= __f
;
484 static constexpr result_type default_seed
= 5489u;
486 // constructors and member function
488 mersenne_twister_engine(result_type __sd
= default_seed
)
492 * @brief Constructs a %mersenne_twister_engine random number generator
493 * engine seeded from the seed sequence @p __q.
495 * @param __q the seed sequence.
497 template<typename _Sseq
, typename
= typename
498 std::enable_if
<!std::is_same
<_Sseq
, mersenne_twister_engine
>::value
>
501 mersenne_twister_engine(_Sseq
& __q
)
505 seed(result_type __sd
= default_seed
);
507 template<typename _Sseq
>
508 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
512 * @brief Gets the smallest possible value in the output range.
514 static constexpr result_type
519 * @brief Gets the largest possible value in the output range.
521 static constexpr result_type
523 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
526 * @brief Discard a sequence of random numbers.
529 discard(unsigned long long __z
);
535 * @brief Compares two % mersenne_twister_engine random number generator
536 * objects of the same type for equality.
538 * @param __lhs A % mersenne_twister_engine random number generator
540 * @param __rhs Another % mersenne_twister_engine random number
543 * @returns true if the infinite sequences of generated values
544 * would be equal, false otherwise.
547 operator==(const mersenne_twister_engine
& __lhs
,
548 const mersenne_twister_engine
& __rhs
)
549 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
)
550 && __lhs
._M_p
== __rhs
._M_p
); }
553 * @brief Inserts the current state of a % mersenne_twister_engine
554 * random number generator engine @p __x into the output stream
557 * @param __os An output stream.
558 * @param __x A % mersenne_twister_engine random number generator
561 * @returns The output stream with the state of @p __x inserted or in
564 template<typename _UIntType1
,
565 size_t __w1
, size_t __n1
,
566 size_t __m1
, size_t __r1
,
567 _UIntType1 __a1
, size_t __u1
,
568 _UIntType1 __d1
, size_t __s1
,
569 _UIntType1 __b1
, size_t __t1
,
570 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
571 typename _CharT
, typename _Traits
>
572 friend std::basic_ostream
<_CharT
, _Traits
>&
573 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
574 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
575 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
579 * @brief Extracts the current state of a % mersenne_twister_engine
580 * random number generator engine @p __x from the input stream
583 * @param __is An input stream.
584 * @param __x A % mersenne_twister_engine random number generator
587 * @returns The input stream with the state of @p __x extracted or in
590 template<typename _UIntType1
,
591 size_t __w1
, size_t __n1
,
592 size_t __m1
, size_t __r1
,
593 _UIntType1 __a1
, size_t __u1
,
594 _UIntType1 __d1
, size_t __s1
,
595 _UIntType1 __b1
, size_t __t1
,
596 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
597 typename _CharT
, typename _Traits
>
598 friend std::basic_istream
<_CharT
, _Traits
>&
599 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
600 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
601 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
607 _UIntType _M_x
[state_size
];
612 * @brief Compares two % mersenne_twister_engine random number generator
613 * objects of the same type for inequality.
615 * @param __lhs A % mersenne_twister_engine random number generator
617 * @param __rhs Another % mersenne_twister_engine random number
620 * @returns true if the infinite sequences of generated values
621 * would be different, false otherwise.
623 template<typename _UIntType
, size_t __w
,
624 size_t __n
, size_t __m
, size_t __r
,
625 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
626 _UIntType __b
, size_t __t
,
627 _UIntType __c
, size_t __l
, _UIntType __f
>
629 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
630 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
631 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
632 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
633 { return !(__lhs
== __rhs
); }
637 * @brief The Marsaglia-Zaman generator.
639 * This is a model of a Generalized Fibonacci discrete random number
640 * generator, sometimes referred to as the SWC generator.
642 * A discrete random number generator that produces pseudorandom
645 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
648 * The size of the state is @f$r@f$
649 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
651 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
652 class subtract_with_carry_engine
654 static_assert(std::is_unsigned
<_UIntType
>::value
,
655 "result_type must be an unsigned integral type");
656 static_assert(0u < __s
&& __s
< __r
,
658 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
659 "template argument substituting __w out of bounds");
662 /** The type of the generated random value. */
663 typedef _UIntType result_type
;
666 static constexpr size_t word_size
= __w
;
667 static constexpr size_t short_lag
= __s
;
668 static constexpr size_t long_lag
= __r
;
669 static constexpr result_type default_seed
= 19780503u;
672 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
673 * random number generator.
676 subtract_with_carry_engine(result_type __sd
= default_seed
)
680 * @brief Constructs a %subtract_with_carry_engine random number engine
681 * seeded from the seed sequence @p __q.
683 * @param __q the seed sequence.
685 template<typename _Sseq
, typename
= typename
686 std::enable_if
<!std::is_same
<_Sseq
, subtract_with_carry_engine
>::value
>
689 subtract_with_carry_engine(_Sseq
& __q
)
693 * @brief Seeds the initial state @f$x_0@f$ of the random number
696 * N1688[4.19] modifies this as follows. If @p __value == 0,
697 * sets value to 19780503. In any case, with a linear
698 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
699 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
700 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
701 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
702 * set carry to 1, otherwise sets carry to 0.
705 seed(result_type __sd
= default_seed
);
708 * @brief Seeds the initial state @f$x_0@f$ of the
709 * % subtract_with_carry_engine random number generator.
711 template<typename _Sseq
>
712 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
716 * @brief Gets the inclusive minimum value of the range of random
717 * integers returned by this generator.
719 static constexpr result_type
724 * @brief Gets the inclusive maximum value of the range of random
725 * integers returned by this generator.
727 static constexpr result_type
729 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
732 * @brief Discard a sequence of random numbers.
735 discard(unsigned long long __z
)
737 for (; __z
!= 0ULL; --__z
)
742 * @brief Gets the next random number in the sequence.
748 * @brief Compares two % subtract_with_carry_engine random number
749 * generator objects of the same type for equality.
751 * @param __lhs A % subtract_with_carry_engine random number generator
753 * @param __rhs Another % subtract_with_carry_engine random number
756 * @returns true if the infinite sequences of generated values
757 * would be equal, false otherwise.
760 operator==(const subtract_with_carry_engine
& __lhs
,
761 const subtract_with_carry_engine
& __rhs
)
762 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
)
763 && __lhs
._M_carry
== __rhs
._M_carry
764 && __lhs
._M_p
== __rhs
._M_p
); }
767 * @brief Inserts the current state of a % subtract_with_carry_engine
768 * random number generator engine @p __x into the output stream
771 * @param __os An output stream.
772 * @param __x A % subtract_with_carry_engine random number generator
775 * @returns The output stream with the state of @p __x inserted or in
778 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
779 typename _CharT
, typename _Traits
>
780 friend std::basic_ostream
<_CharT
, _Traits
>&
781 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
782 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
786 * @brief Extracts the current state of a % subtract_with_carry_engine
787 * random number generator engine @p __x from the input stream
790 * @param __is An input stream.
791 * @param __x A % subtract_with_carry_engine random number generator
794 * @returns The input stream with the state of @p __x extracted or in
797 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
798 typename _CharT
, typename _Traits
>
799 friend std::basic_istream
<_CharT
, _Traits
>&
800 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
801 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
805 /// The state of the generator. This is a ring buffer.
806 _UIntType _M_x
[long_lag
];
807 _UIntType _M_carry
; ///< The carry
808 size_t _M_p
; ///< Current index of x(i - r).
812 * @brief Compares two % subtract_with_carry_engine random number
813 * generator objects of the same type for inequality.
815 * @param __lhs A % subtract_with_carry_engine random number generator
817 * @param __rhs Another % subtract_with_carry_engine random number
820 * @returns true if the infinite sequences of generated values
821 * would be different, false otherwise.
823 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
825 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
827 const std::subtract_with_carry_engine
<_UIntType
, __w
,
829 { return !(__lhs
== __rhs
); }
833 * Produces random numbers from some base engine by discarding blocks of
836 * 0 <= @p __r <= @p __p
838 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
839 class discard_block_engine
841 static_assert(1 <= __r
&& __r
<= __p
,
842 "template argument substituting __r out of bounds");
845 /** The type of the generated random value. */
846 typedef typename
_RandomNumberEngine::result_type result_type
;
849 static constexpr size_t block_size
= __p
;
850 static constexpr size_t used_block
= __r
;
853 * @brief Constructs a default %discard_block_engine engine.
855 * The underlying engine is default constructed as well.
857 discard_block_engine()
858 : _M_b(), _M_n(0) { }
861 * @brief Copy constructs a %discard_block_engine engine.
863 * Copies an existing base class random number generator.
864 * @param __rng An existing (base class) engine object.
867 discard_block_engine(const _RandomNumberEngine
& __rng
)
868 : _M_b(__rng
), _M_n(0) { }
871 * @brief Move constructs a %discard_block_engine engine.
873 * Copies an existing base class random number generator.
874 * @param __rng An existing (base class) engine object.
877 discard_block_engine(_RandomNumberEngine
&& __rng
)
878 : _M_b(std::move(__rng
)), _M_n(0) { }
881 * @brief Seed constructs a %discard_block_engine engine.
883 * Constructs the underlying generator engine seeded with @p __s.
884 * @param __s A seed value for the base class engine.
887 discard_block_engine(result_type __s
)
888 : _M_b(__s
), _M_n(0) { }
891 * @brief Generator construct a %discard_block_engine engine.
893 * @param __q A seed sequence.
895 template<typename _Sseq
, typename
= typename
896 std::enable_if
<!std::is_same
<_Sseq
, discard_block_engine
>::value
897 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
900 discard_block_engine(_Sseq
& __q
)
905 * @brief Reseeds the %discard_block_engine object with the default
906 * seed for the underlying base class generator engine.
916 * @brief Reseeds the %discard_block_engine object with the default
917 * seed for the underlying base class generator engine.
920 seed(result_type __s
)
927 * @brief Reseeds the %discard_block_engine object with the given seed
929 * @param __q A seed generator function.
931 template<typename _Sseq
>
940 * @brief Gets a const reference to the underlying generator engine
943 const _RandomNumberEngine
&
944 base() const noexcept
948 * @brief Gets the minimum value in the generated random number range.
950 static constexpr result_type
952 { return _RandomNumberEngine::min(); }
955 * @brief Gets the maximum value in the generated random number range.
957 static constexpr result_type
959 { return _RandomNumberEngine::max(); }
962 * @brief Discard a sequence of random numbers.
965 discard(unsigned long long __z
)
967 for (; __z
!= 0ULL; --__z
)
972 * @brief Gets the next value in the generated random number sequence.
978 * @brief Compares two %discard_block_engine random number generator
979 * objects of the same type for equality.
981 * @param __lhs A %discard_block_engine random number generator object.
982 * @param __rhs Another %discard_block_engine random number generator
985 * @returns true if the infinite sequences of generated values
986 * would be equal, false otherwise.
989 operator==(const discard_block_engine
& __lhs
,
990 const discard_block_engine
& __rhs
)
991 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
994 * @brief Inserts the current state of a %discard_block_engine random
995 * number generator engine @p __x into the output stream
998 * @param __os An output stream.
999 * @param __x A %discard_block_engine random number generator engine.
1001 * @returns The output stream with the state of @p __x inserted or in
1004 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1005 typename _CharT
, typename _Traits
>
1006 friend std::basic_ostream
<_CharT
, _Traits
>&
1007 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1008 const std::discard_block_engine
<_RandomNumberEngine1
,
1012 * @brief Extracts the current state of a % subtract_with_carry_engine
1013 * random number generator engine @p __x from the input stream
1016 * @param __is An input stream.
1017 * @param __x A %discard_block_engine random number generator engine.
1019 * @returns The input stream with the state of @p __x extracted or in
1022 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1023 typename _CharT
, typename _Traits
>
1024 friend std::basic_istream
<_CharT
, _Traits
>&
1025 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1026 std::discard_block_engine
<_RandomNumberEngine1
,
1030 _RandomNumberEngine _M_b
;
1035 * @brief Compares two %discard_block_engine random number generator
1036 * objects of the same type for inequality.
1038 * @param __lhs A %discard_block_engine random number generator object.
1039 * @param __rhs Another %discard_block_engine random number generator
1042 * @returns true if the infinite sequences of generated values
1043 * would be different, false otherwise.
1045 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
1047 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1049 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1051 { return !(__lhs
== __rhs
); }
1055 * Produces random numbers by combining random numbers from some base
1056 * engine to produce random numbers with a specifies number of bits @p __w.
1058 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1059 class independent_bits_engine
1061 static_assert(std::is_unsigned
<_UIntType
>::value
,
1062 "result_type must be an unsigned integral type");
1063 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
1064 "template argument substituting __w out of bounds");
1067 /** The type of the generated random value. */
1068 typedef _UIntType result_type
;
1071 * @brief Constructs a default %independent_bits_engine engine.
1073 * The underlying engine is default constructed as well.
1075 independent_bits_engine()
1079 * @brief Copy constructs a %independent_bits_engine engine.
1081 * Copies an existing base class random number generator.
1082 * @param __rng An existing (base class) engine object.
1085 independent_bits_engine(const _RandomNumberEngine
& __rng
)
1089 * @brief Move constructs a %independent_bits_engine engine.
1091 * Copies an existing base class random number generator.
1092 * @param __rng An existing (base class) engine object.
1095 independent_bits_engine(_RandomNumberEngine
&& __rng
)
1096 : _M_b(std::move(__rng
)) { }
1099 * @brief Seed constructs a %independent_bits_engine engine.
1101 * Constructs the underlying generator engine seeded with @p __s.
1102 * @param __s A seed value for the base class engine.
1105 independent_bits_engine(result_type __s
)
1109 * @brief Generator construct a %independent_bits_engine engine.
1111 * @param __q A seed sequence.
1113 template<typename _Sseq
, typename
= typename
1114 std::enable_if
<!std::is_same
<_Sseq
, independent_bits_engine
>::value
1115 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1118 independent_bits_engine(_Sseq
& __q
)
1123 * @brief Reseeds the %independent_bits_engine object with the default
1124 * seed for the underlying base class generator engine.
1131 * @brief Reseeds the %independent_bits_engine object with the default
1132 * seed for the underlying base class generator engine.
1135 seed(result_type __s
)
1139 * @brief Reseeds the %independent_bits_engine object with the given
1141 * @param __q A seed generator function.
1143 template<typename _Sseq
>
1149 * @brief Gets a const reference to the underlying generator engine
1152 const _RandomNumberEngine
&
1153 base() const noexcept
1157 * @brief Gets the minimum value in the generated random number range.
1159 static constexpr result_type
1164 * @brief Gets the maximum value in the generated random number range.
1166 static constexpr result_type
1168 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1171 * @brief Discard a sequence of random numbers.
1174 discard(unsigned long long __z
)
1176 for (; __z
!= 0ULL; --__z
)
1181 * @brief Gets the next value in the generated random number sequence.
1187 * @brief Compares two %independent_bits_engine random number generator
1188 * objects of the same type for equality.
1190 * @param __lhs A %independent_bits_engine random number generator
1192 * @param __rhs Another %independent_bits_engine random number generator
1195 * @returns true if the infinite sequences of generated values
1196 * would be equal, false otherwise.
1199 operator==(const independent_bits_engine
& __lhs
,
1200 const independent_bits_engine
& __rhs
)
1201 { return __lhs
._M_b
== __rhs
._M_b
; }
1204 * @brief Extracts the current state of a % subtract_with_carry_engine
1205 * random number generator engine @p __x from the input stream
1208 * @param __is An input stream.
1209 * @param __x A %independent_bits_engine random number generator
1212 * @returns The input stream with the state of @p __x extracted or in
1215 template<typename _CharT
, typename _Traits
>
1216 friend std::basic_istream
<_CharT
, _Traits
>&
1217 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1218 std::independent_bits_engine
<_RandomNumberEngine
,
1219 __w
, _UIntType
>& __x
)
1226 _RandomNumberEngine _M_b
;
1230 * @brief Compares two %independent_bits_engine random number generator
1231 * objects of the same type for inequality.
1233 * @param __lhs A %independent_bits_engine random number generator
1235 * @param __rhs Another %independent_bits_engine random number generator
1238 * @returns true if the infinite sequences of generated values
1239 * would be different, false otherwise.
1241 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1243 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1245 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1247 { return !(__lhs
== __rhs
); }
1250 * @brief Inserts the current state of a %independent_bits_engine random
1251 * number generator engine @p __x into the output stream @p __os.
1253 * @param __os An output stream.
1254 * @param __x A %independent_bits_engine random number generator engine.
1256 * @returns The output stream with the state of @p __x inserted or in
1259 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1260 typename _CharT
, typename _Traits
>
1261 std::basic_ostream
<_CharT
, _Traits
>&
1262 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1263 const std::independent_bits_engine
<_RandomNumberEngine
,
1264 __w
, _UIntType
>& __x
)
1272 * @brief Produces random numbers by combining random numbers from some
1273 * base engine to produce random numbers with a specifies number of bits
1276 template<typename _RandomNumberEngine
, size_t __k
>
1277 class shuffle_order_engine
1279 static_assert(1u <= __k
, "template argument substituting "
1280 "__k out of bound");
1283 /** The type of the generated random value. */
1284 typedef typename
_RandomNumberEngine::result_type result_type
;
1286 static constexpr size_t table_size
= __k
;
1289 * @brief Constructs a default %shuffle_order_engine engine.
1291 * The underlying engine is default constructed as well.
1293 shuffle_order_engine()
1295 { _M_initialize(); }
1298 * @brief Copy constructs a %shuffle_order_engine engine.
1300 * Copies an existing base class random number generator.
1301 * @param __rng An existing (base class) engine object.
1304 shuffle_order_engine(const _RandomNumberEngine
& __rng
)
1306 { _M_initialize(); }
1309 * @brief Move constructs a %shuffle_order_engine engine.
1311 * Copies an existing base class random number generator.
1312 * @param __rng An existing (base class) engine object.
1315 shuffle_order_engine(_RandomNumberEngine
&& __rng
)
1316 : _M_b(std::move(__rng
))
1317 { _M_initialize(); }
1320 * @brief Seed constructs a %shuffle_order_engine engine.
1322 * Constructs the underlying generator engine seeded with @p __s.
1323 * @param __s A seed value for the base class engine.
1326 shuffle_order_engine(result_type __s
)
1328 { _M_initialize(); }
1331 * @brief Generator construct a %shuffle_order_engine engine.
1333 * @param __q A seed sequence.
1335 template<typename _Sseq
, typename
= typename
1336 std::enable_if
<!std::is_same
<_Sseq
, shuffle_order_engine
>::value
1337 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1340 shuffle_order_engine(_Sseq
& __q
)
1342 { _M_initialize(); }
1345 * @brief Reseeds the %shuffle_order_engine object with the default seed
1346 for the underlying base class generator engine.
1356 * @brief Reseeds the %shuffle_order_engine object with the default seed
1357 * for the underlying base class generator engine.
1360 seed(result_type __s
)
1367 * @brief Reseeds the %shuffle_order_engine object with the given seed
1369 * @param __q A seed generator function.
1371 template<typename _Sseq
>
1380 * Gets a const reference to the underlying generator engine object.
1382 const _RandomNumberEngine
&
1383 base() const noexcept
1387 * Gets the minimum value in the generated random number range.
1389 static constexpr result_type
1391 { return _RandomNumberEngine::min(); }
1394 * Gets the maximum value in the generated random number range.
1396 static constexpr result_type
1398 { return _RandomNumberEngine::max(); }
1401 * Discard a sequence of random numbers.
1404 discard(unsigned long long __z
)
1406 for (; __z
!= 0ULL; --__z
)
1411 * Gets the next value in the generated random number sequence.
1417 * Compares two %shuffle_order_engine random number generator objects
1418 * of the same type for equality.
1420 * @param __lhs A %shuffle_order_engine random number generator object.
1421 * @param __rhs Another %shuffle_order_engine random number generator
1424 * @returns true if the infinite sequences of generated values
1425 * would be equal, false otherwise.
1428 operator==(const shuffle_order_engine
& __lhs
,
1429 const shuffle_order_engine
& __rhs
)
1430 { return (__lhs
._M_b
== __rhs
._M_b
1431 && std::equal(__lhs
._M_v
, __lhs
._M_v
+ __k
, __rhs
._M_v
)
1432 && __lhs
._M_y
== __rhs
._M_y
); }
1435 * @brief Inserts the current state of a %shuffle_order_engine random
1436 * number generator engine @p __x into the output stream
1439 * @param __os An output stream.
1440 * @param __x A %shuffle_order_engine random number generator engine.
1442 * @returns The output stream with the state of @p __x inserted or in
1445 template<typename _RandomNumberEngine1
, size_t __k1
,
1446 typename _CharT
, typename _Traits
>
1447 friend std::basic_ostream
<_CharT
, _Traits
>&
1448 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1449 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1453 * @brief Extracts the current state of a % subtract_with_carry_engine
1454 * random number generator engine @p __x from the input stream
1457 * @param __is An input stream.
1458 * @param __x A %shuffle_order_engine random number generator engine.
1460 * @returns The input stream with the state of @p __x extracted or in
1463 template<typename _RandomNumberEngine1
, size_t __k1
,
1464 typename _CharT
, typename _Traits
>
1465 friend std::basic_istream
<_CharT
, _Traits
>&
1466 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1467 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>& __x
);
1470 void _M_initialize()
1472 for (size_t __i
= 0; __i
< __k
; ++__i
)
1477 _RandomNumberEngine _M_b
;
1478 result_type _M_v
[__k
];
1483 * Compares two %shuffle_order_engine random number generator objects
1484 * of the same type for inequality.
1486 * @param __lhs A %shuffle_order_engine random number generator object.
1487 * @param __rhs Another %shuffle_order_engine random number generator
1490 * @returns true if the infinite sequences of generated values
1491 * would be different, false otherwise.
1493 template<typename _RandomNumberEngine
, size_t __k
>
1495 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1497 const std::shuffle_order_engine
<_RandomNumberEngine
,
1499 { return !(__lhs
== __rhs
); }
1503 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1505 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1509 * An alternative LCR (Lehmer Generator function).
1511 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1515 * The classic Mersenne Twister.
1518 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1519 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1520 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1522 typedef mersenne_twister_engine
<
1528 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1531 * An alternative Mersenne Twister.
1533 typedef mersenne_twister_engine
<
1536 0xb5026f5aa96619e9ULL
, 29,
1537 0x5555555555555555ULL
, 17,
1538 0x71d67fffeda60000ULL
, 37,
1539 0xfff7eee000000000ULL
, 43,
1540 6364136223846793005ULL> mt19937_64
;
1542 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1545 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1548 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1550 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1552 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1554 typedef minstd_rand0 default_random_engine
;
1557 * A standard interface to a platform-specific non-deterministic
1558 * random number generator (if any are available).
1563 /** The type of the generated random value. */
1564 typedef unsigned int result_type
;
1566 // constructors, destructors and member functions
1568 #ifdef _GLIBCXX_USE_RANDOM_TR1
1571 random_device(const std::string
& __token
= "default")
1582 random_device(const std::string
& __token
= "mt19937")
1583 { _M_init_pretr1(__token
); }
1589 static constexpr result_type
1591 { return std::numeric_limits
<result_type
>::min(); }
1593 static constexpr result_type
1595 { return std::numeric_limits
<result_type
>::max(); }
1598 entropy() const noexcept
1600 #ifdef _GLIBCXX_USE_RANDOM_TR1
1601 return this->_M_getentropy();
1610 #ifdef _GLIBCXX_USE_RANDOM_TR1
1611 return this->_M_getval();
1613 return this->_M_getval_pretr1();
1617 // No copy functions.
1618 random_device(const random_device
&) = delete;
1619 void operator=(const random_device
&) = delete;
1623 void _M_init(const std::string
& __token
);
1624 void _M_init_pretr1(const std::string
& __token
);
1627 result_type
_M_getval();
1628 result_type
_M_getval_pretr1();
1629 double _M_getentropy() const noexcept
;
1638 /* @} */ // group random_generators
1641 * @addtogroup random_distributions Random Number Distributions
1647 * @addtogroup random_distributions_uniform Uniform Distributions
1648 * @ingroup random_distributions
1652 // std::uniform_int_distribution is defined in <bits/uniform_int_dist.h>
1655 * @brief Return true if two uniform integer distributions have
1656 * different parameters.
1658 template<typename _IntType
>
1660 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1661 const std::uniform_int_distribution
<_IntType
>& __d2
)
1662 { return !(__d1
== __d2
); }
1665 * @brief Inserts a %uniform_int_distribution random number
1666 * distribution @p __x into the output stream @p os.
1668 * @param __os An output stream.
1669 * @param __x A %uniform_int_distribution random number distribution.
1671 * @returns The output stream with the state of @p __x inserted or in
1674 template<typename _IntType
, typename _CharT
, typename _Traits
>
1675 std::basic_ostream
<_CharT
, _Traits
>&
1676 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1677 const std::uniform_int_distribution
<_IntType
>&);
1680 * @brief Extracts a %uniform_int_distribution random number distribution
1681 * @p __x from the input stream @p __is.
1683 * @param __is An input stream.
1684 * @param __x A %uniform_int_distribution random number generator engine.
1686 * @returns The input stream with @p __x extracted or in an error state.
1688 template<typename _IntType
, typename _CharT
, typename _Traits
>
1689 std::basic_istream
<_CharT
, _Traits
>&
1690 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1691 std::uniform_int_distribution
<_IntType
>&);
1695 * @brief Uniform continuous distribution for random numbers.
1697 * A continuous random distribution on the range [min, max) with equal
1698 * probability throughout the range. The URNG should be real-valued and
1699 * deliver number in the range [0, 1).
1701 template<typename _RealType
= double>
1702 class uniform_real_distribution
1704 static_assert(std::is_floating_point
<_RealType
>::value
,
1705 "result_type must be a floating point type");
1708 /** The type of the range of the distribution. */
1709 typedef _RealType result_type
;
1711 /** Parameter type. */
1714 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1717 param_type(_RealType __a
= _RealType(0),
1718 _RealType __b
= _RealType(1))
1719 : _M_a(__a
), _M_b(__b
)
1721 __glibcxx_assert(_M_a
<= _M_b
);
1733 operator==(const param_type
& __p1
, const param_type
& __p2
)
1734 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1737 operator!=(const param_type
& __p1
, const param_type
& __p2
)
1738 { return !(__p1
== __p2
); }
1747 * @brief Constructs a uniform_real_distribution object.
1749 * @param __a [IN] The lower bound of the distribution.
1750 * @param __b [IN] The upper bound of the distribution.
1753 uniform_real_distribution(_RealType __a
= _RealType(0),
1754 _RealType __b
= _RealType(1))
1755 : _M_param(__a
, __b
)
1759 uniform_real_distribution(const param_type
& __p
)
1764 * @brief Resets the distribution state.
1766 * Does nothing for the uniform real distribution.
1773 { return _M_param
.a(); }
1777 { return _M_param
.b(); }
1780 * @brief Returns the parameter set of the distribution.
1784 { return _M_param
; }
1787 * @brief Sets the parameter set of the distribution.
1788 * @param __param The new parameter set of the distribution.
1791 param(const param_type
& __param
)
1792 { _M_param
= __param
; }
1795 * @brief Returns the inclusive lower bound of the distribution range.
1799 { return this->a(); }
1802 * @brief Returns the inclusive upper bound of the distribution range.
1806 { return this->b(); }
1809 * @brief Generating functions.
1811 template<typename _UniformRandomNumberGenerator
>
1813 operator()(_UniformRandomNumberGenerator
& __urng
)
1814 { return this->operator()(__urng
, _M_param
); }
1816 template<typename _UniformRandomNumberGenerator
>
1818 operator()(_UniformRandomNumberGenerator
& __urng
,
1819 const param_type
& __p
)
1821 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1823 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1826 template<typename _ForwardIterator
,
1827 typename _UniformRandomNumberGenerator
>
1829 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1830 _UniformRandomNumberGenerator
& __urng
)
1831 { this->__generate(__f
, __t
, __urng
, _M_param
); }
1833 template<typename _ForwardIterator
,
1834 typename _UniformRandomNumberGenerator
>
1836 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1837 _UniformRandomNumberGenerator
& __urng
,
1838 const param_type
& __p
)
1839 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1841 template<typename _UniformRandomNumberGenerator
>
1843 __generate(result_type
* __f
, result_type
* __t
,
1844 _UniformRandomNumberGenerator
& __urng
,
1845 const param_type
& __p
)
1846 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1849 * @brief Return true if two uniform real distributions have
1850 * the same parameters.
1853 operator==(const uniform_real_distribution
& __d1
,
1854 const uniform_real_distribution
& __d2
)
1855 { return __d1
._M_param
== __d2
._M_param
; }
1858 template<typename _ForwardIterator
,
1859 typename _UniformRandomNumberGenerator
>
1861 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
1862 _UniformRandomNumberGenerator
& __urng
,
1863 const param_type
& __p
);
1865 param_type _M_param
;
1869 * @brief Return true if two uniform real distributions have
1870 * different parameters.
1872 template<typename _IntType
>
1874 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1875 const std::uniform_real_distribution
<_IntType
>& __d2
)
1876 { return !(__d1
== __d2
); }
1879 * @brief Inserts a %uniform_real_distribution random number
1880 * distribution @p __x into the output stream @p __os.
1882 * @param __os An output stream.
1883 * @param __x A %uniform_real_distribution random number distribution.
1885 * @returns The output stream with the state of @p __x inserted or in
1888 template<typename _RealType
, typename _CharT
, typename _Traits
>
1889 std::basic_ostream
<_CharT
, _Traits
>&
1890 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1891 const std::uniform_real_distribution
<_RealType
>&);
1894 * @brief Extracts a %uniform_real_distribution random number distribution
1895 * @p __x from the input stream @p __is.
1897 * @param __is An input stream.
1898 * @param __x A %uniform_real_distribution random number generator engine.
1900 * @returns The input stream with @p __x extracted or in an error state.
1902 template<typename _RealType
, typename _CharT
, typename _Traits
>
1903 std::basic_istream
<_CharT
, _Traits
>&
1904 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1905 std::uniform_real_distribution
<_RealType
>&);
1907 /* @} */ // group random_distributions_uniform
1910 * @addtogroup random_distributions_normal Normal Distributions
1911 * @ingroup random_distributions
1916 * @brief A normal continuous distribution for random numbers.
1918 * The formula for the normal probability density function is
1920 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1921 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1924 template<typename _RealType
= double>
1925 class normal_distribution
1927 static_assert(std::is_floating_point
<_RealType
>::value
,
1928 "result_type must be a floating point type");
1931 /** The type of the range of the distribution. */
1932 typedef _RealType result_type
;
1934 /** Parameter type. */
1937 typedef normal_distribution
<_RealType
> distribution_type
;
1940 param_type(_RealType __mean
= _RealType(0),
1941 _RealType __stddev
= _RealType(1))
1942 : _M_mean(__mean
), _M_stddev(__stddev
)
1944 __glibcxx_assert(_M_stddev
> _RealType(0));
1953 { return _M_stddev
; }
1956 operator==(const param_type
& __p1
, const param_type
& __p2
)
1957 { return (__p1
._M_mean
== __p2
._M_mean
1958 && __p1
._M_stddev
== __p2
._M_stddev
); }
1961 operator!=(const param_type
& __p1
, const param_type
& __p2
)
1962 { return !(__p1
== __p2
); }
1966 _RealType _M_stddev
;
1971 * Constructs a normal distribution with parameters @f$mean@f$ and
1972 * standard deviation.
1975 normal_distribution(result_type __mean
= result_type(0),
1976 result_type __stddev
= result_type(1))
1977 : _M_param(__mean
, __stddev
), _M_saved_available(false)
1981 normal_distribution(const param_type
& __p
)
1982 : _M_param(__p
), _M_saved_available(false)
1986 * @brief Resets the distribution state.
1990 { _M_saved_available
= false; }
1993 * @brief Returns the mean of the distribution.
1997 { return _M_param
.mean(); }
2000 * @brief Returns the standard deviation of the distribution.
2004 { return _M_param
.stddev(); }
2007 * @brief Returns the parameter set of the distribution.
2011 { return _M_param
; }
2014 * @brief Sets the parameter set of the distribution.
2015 * @param __param The new parameter set of the distribution.
2018 param(const param_type
& __param
)
2019 { _M_param
= __param
; }
2022 * @brief Returns the greatest lower bound value of the distribution.
2026 { return std::numeric_limits
<result_type
>::lowest(); }
2029 * @brief Returns the least upper bound value of the distribution.
2033 { return std::numeric_limits
<result_type
>::max(); }
2036 * @brief Generating functions.
2038 template<typename _UniformRandomNumberGenerator
>
2040 operator()(_UniformRandomNumberGenerator
& __urng
)
2041 { return this->operator()(__urng
, _M_param
); }
2043 template<typename _UniformRandomNumberGenerator
>
2045 operator()(_UniformRandomNumberGenerator
& __urng
,
2046 const param_type
& __p
);
2048 template<typename _ForwardIterator
,
2049 typename _UniformRandomNumberGenerator
>
2051 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2052 _UniformRandomNumberGenerator
& __urng
)
2053 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2055 template<typename _ForwardIterator
,
2056 typename _UniformRandomNumberGenerator
>
2058 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2059 _UniformRandomNumberGenerator
& __urng
,
2060 const param_type
& __p
)
2061 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2063 template<typename _UniformRandomNumberGenerator
>
2065 __generate(result_type
* __f
, result_type
* __t
,
2066 _UniformRandomNumberGenerator
& __urng
,
2067 const param_type
& __p
)
2068 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2071 * @brief Return true if two normal distributions have
2072 * the same parameters and the sequences that would
2073 * be generated are equal.
2075 template<typename _RealType1
>
2077 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2078 const std::normal_distribution
<_RealType1
>& __d2
);
2081 * @brief Inserts a %normal_distribution random number distribution
2082 * @p __x into the output stream @p __os.
2084 * @param __os An output stream.
2085 * @param __x A %normal_distribution random number distribution.
2087 * @returns The output stream with the state of @p __x inserted or in
2090 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2091 friend std::basic_ostream
<_CharT
, _Traits
>&
2092 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2093 const std::normal_distribution
<_RealType1
>& __x
);
2096 * @brief Extracts a %normal_distribution random number distribution
2097 * @p __x from the input stream @p __is.
2099 * @param __is An input stream.
2100 * @param __x A %normal_distribution random number generator engine.
2102 * @returns The input stream with @p __x extracted or in an error
2105 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2106 friend std::basic_istream
<_CharT
, _Traits
>&
2107 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2108 std::normal_distribution
<_RealType1
>& __x
);
2111 template<typename _ForwardIterator
,
2112 typename _UniformRandomNumberGenerator
>
2114 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2115 _UniformRandomNumberGenerator
& __urng
,
2116 const param_type
& __p
);
2118 param_type _M_param
;
2119 result_type _M_saved
;
2120 bool _M_saved_available
;
2124 * @brief Return true if two normal distributions are different.
2126 template<typename _RealType
>
2128 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2129 const std::normal_distribution
<_RealType
>& __d2
)
2130 { return !(__d1
== __d2
); }
2134 * @brief A lognormal_distribution random number distribution.
2136 * The formula for the normal probability mass function is
2138 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2139 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2142 template<typename _RealType
= double>
2143 class lognormal_distribution
2145 static_assert(std::is_floating_point
<_RealType
>::value
,
2146 "result_type must be a floating point type");
2149 /** The type of the range of the distribution. */
2150 typedef _RealType result_type
;
2152 /** Parameter type. */
2155 typedef lognormal_distribution
<_RealType
> distribution_type
;
2158 param_type(_RealType __m
= _RealType(0),
2159 _RealType __s
= _RealType(1))
2160 : _M_m(__m
), _M_s(__s
)
2172 operator==(const param_type
& __p1
, const param_type
& __p2
)
2173 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2176 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2177 { return !(__p1
== __p2
); }
2185 lognormal_distribution(_RealType __m
= _RealType(0),
2186 _RealType __s
= _RealType(1))
2187 : _M_param(__m
, __s
), _M_nd()
2191 lognormal_distribution(const param_type
& __p
)
2192 : _M_param(__p
), _M_nd()
2196 * Resets the distribution state.
2207 { return _M_param
.m(); }
2211 { return _M_param
.s(); }
2214 * @brief Returns the parameter set of the distribution.
2218 { return _M_param
; }
2221 * @brief Sets the parameter set of the distribution.
2222 * @param __param The new parameter set of the distribution.
2225 param(const param_type
& __param
)
2226 { _M_param
= __param
; }
2229 * @brief Returns the greatest lower bound value of the distribution.
2233 { return result_type(0); }
2236 * @brief Returns the least upper bound value of the distribution.
2240 { return std::numeric_limits
<result_type
>::max(); }
2243 * @brief Generating functions.
2245 template<typename _UniformRandomNumberGenerator
>
2247 operator()(_UniformRandomNumberGenerator
& __urng
)
2248 { return this->operator()(__urng
, _M_param
); }
2250 template<typename _UniformRandomNumberGenerator
>
2252 operator()(_UniformRandomNumberGenerator
& __urng
,
2253 const param_type
& __p
)
2254 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2256 template<typename _ForwardIterator
,
2257 typename _UniformRandomNumberGenerator
>
2259 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2260 _UniformRandomNumberGenerator
& __urng
)
2261 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2263 template<typename _ForwardIterator
,
2264 typename _UniformRandomNumberGenerator
>
2266 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2267 _UniformRandomNumberGenerator
& __urng
,
2268 const param_type
& __p
)
2269 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2271 template<typename _UniformRandomNumberGenerator
>
2273 __generate(result_type
* __f
, result_type
* __t
,
2274 _UniformRandomNumberGenerator
& __urng
,
2275 const param_type
& __p
)
2276 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2279 * @brief Return true if two lognormal distributions have
2280 * the same parameters and the sequences that would
2281 * be generated are equal.
2284 operator==(const lognormal_distribution
& __d1
,
2285 const lognormal_distribution
& __d2
)
2286 { return (__d1
._M_param
== __d2
._M_param
2287 && __d1
._M_nd
== __d2
._M_nd
); }
2290 * @brief Inserts a %lognormal_distribution random number distribution
2291 * @p __x into the output stream @p __os.
2293 * @param __os An output stream.
2294 * @param __x A %lognormal_distribution random number distribution.
2296 * @returns The output stream with the state of @p __x inserted or in
2299 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2300 friend std::basic_ostream
<_CharT
, _Traits
>&
2301 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2302 const std::lognormal_distribution
<_RealType1
>& __x
);
2305 * @brief Extracts a %lognormal_distribution random number distribution
2306 * @p __x from the input stream @p __is.
2308 * @param __is An input stream.
2309 * @param __x A %lognormal_distribution random number
2312 * @returns The input stream with @p __x extracted or in an error state.
2314 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2315 friend std::basic_istream
<_CharT
, _Traits
>&
2316 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2317 std::lognormal_distribution
<_RealType1
>& __x
);
2320 template<typename _ForwardIterator
,
2321 typename _UniformRandomNumberGenerator
>
2323 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2324 _UniformRandomNumberGenerator
& __urng
,
2325 const param_type
& __p
);
2327 param_type _M_param
;
2329 std::normal_distribution
<result_type
> _M_nd
;
2333 * @brief Return true if two lognormal distributions are different.
2335 template<typename _RealType
>
2337 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2338 const std::lognormal_distribution
<_RealType
>& __d2
)
2339 { return !(__d1
== __d2
); }
2343 * @brief A gamma continuous distribution for random numbers.
2345 * The formula for the gamma probability density function is:
2347 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2348 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2351 template<typename _RealType
= double>
2352 class gamma_distribution
2354 static_assert(std::is_floating_point
<_RealType
>::value
,
2355 "result_type must be a floating point type");
2358 /** The type of the range of the distribution. */
2359 typedef _RealType result_type
;
2361 /** Parameter type. */
2364 typedef gamma_distribution
<_RealType
> distribution_type
;
2365 friend class gamma_distribution
<_RealType
>;
2368 param_type(_RealType __alpha_val
= _RealType(1),
2369 _RealType __beta_val
= _RealType(1))
2370 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2372 __glibcxx_assert(_M_alpha
> _RealType(0));
2378 { return _M_alpha
; }
2385 operator==(const param_type
& __p1
, const param_type
& __p2
)
2386 { return (__p1
._M_alpha
== __p2
._M_alpha
2387 && __p1
._M_beta
== __p2
._M_beta
); }
2390 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2391 { return !(__p1
== __p2
); }
2400 _RealType _M_malpha
, _M_a2
;
2405 * @brief Constructs a gamma distribution with parameters
2406 * @f$\alpha@f$ and @f$\beta@f$.
2409 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2410 _RealType __beta_val
= _RealType(1))
2411 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2415 gamma_distribution(const param_type
& __p
)
2416 : _M_param(__p
), _M_nd()
2420 * @brief Resets the distribution state.
2427 * @brief Returns the @f$\alpha@f$ of the distribution.
2431 { return _M_param
.alpha(); }
2434 * @brief Returns the @f$\beta@f$ of the distribution.
2438 { return _M_param
.beta(); }
2441 * @brief Returns the parameter set of the distribution.
2445 { return _M_param
; }
2448 * @brief Sets the parameter set of the distribution.
2449 * @param __param The new parameter set of the distribution.
2452 param(const param_type
& __param
)
2453 { _M_param
= __param
; }
2456 * @brief Returns the greatest lower bound value of the distribution.
2460 { return result_type(0); }
2463 * @brief Returns the least upper bound value of the distribution.
2467 { return std::numeric_limits
<result_type
>::max(); }
2470 * @brief Generating functions.
2472 template<typename _UniformRandomNumberGenerator
>
2474 operator()(_UniformRandomNumberGenerator
& __urng
)
2475 { return this->operator()(__urng
, _M_param
); }
2477 template<typename _UniformRandomNumberGenerator
>
2479 operator()(_UniformRandomNumberGenerator
& __urng
,
2480 const param_type
& __p
);
2482 template<typename _ForwardIterator
,
2483 typename _UniformRandomNumberGenerator
>
2485 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2486 _UniformRandomNumberGenerator
& __urng
)
2487 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2489 template<typename _ForwardIterator
,
2490 typename _UniformRandomNumberGenerator
>
2492 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2493 _UniformRandomNumberGenerator
& __urng
,
2494 const param_type
& __p
)
2495 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2497 template<typename _UniformRandomNumberGenerator
>
2499 __generate(result_type
* __f
, result_type
* __t
,
2500 _UniformRandomNumberGenerator
& __urng
,
2501 const param_type
& __p
)
2502 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2505 * @brief Return true if two gamma distributions have the same
2506 * parameters and the sequences that would be generated
2510 operator==(const gamma_distribution
& __d1
,
2511 const gamma_distribution
& __d2
)
2512 { return (__d1
._M_param
== __d2
._M_param
2513 && __d1
._M_nd
== __d2
._M_nd
); }
2516 * @brief Inserts a %gamma_distribution random number distribution
2517 * @p __x into the output stream @p __os.
2519 * @param __os An output stream.
2520 * @param __x A %gamma_distribution random number distribution.
2522 * @returns The output stream with the state of @p __x inserted or in
2525 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2526 friend std::basic_ostream
<_CharT
, _Traits
>&
2527 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2528 const std::gamma_distribution
<_RealType1
>& __x
);
2531 * @brief Extracts a %gamma_distribution random number distribution
2532 * @p __x from the input stream @p __is.
2534 * @param __is An input stream.
2535 * @param __x A %gamma_distribution random number generator engine.
2537 * @returns The input stream with @p __x extracted or in an error state.
2539 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2540 friend std::basic_istream
<_CharT
, _Traits
>&
2541 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2542 std::gamma_distribution
<_RealType1
>& __x
);
2545 template<typename _ForwardIterator
,
2546 typename _UniformRandomNumberGenerator
>
2548 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2549 _UniformRandomNumberGenerator
& __urng
,
2550 const param_type
& __p
);
2552 param_type _M_param
;
2554 std::normal_distribution
<result_type
> _M_nd
;
2558 * @brief Return true if two gamma distributions are different.
2560 template<typename _RealType
>
2562 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2563 const std::gamma_distribution
<_RealType
>& __d2
)
2564 { return !(__d1
== __d2
); }
2568 * @brief A chi_squared_distribution random number distribution.
2570 * The formula for the normal probability mass function is
2571 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2573 template<typename _RealType
= double>
2574 class chi_squared_distribution
2576 static_assert(std::is_floating_point
<_RealType
>::value
,
2577 "result_type must be a floating point type");
2580 /** The type of the range of the distribution. */
2581 typedef _RealType result_type
;
2583 /** Parameter type. */
2586 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2589 param_type(_RealType __n
= _RealType(1))
2598 operator==(const param_type
& __p1
, const param_type
& __p2
)
2599 { return __p1
._M_n
== __p2
._M_n
; }
2602 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2603 { return !(__p1
== __p2
); }
2610 chi_squared_distribution(_RealType __n
= _RealType(1))
2611 : _M_param(__n
), _M_gd(__n
/ 2)
2615 chi_squared_distribution(const param_type
& __p
)
2616 : _M_param(__p
), _M_gd(__p
.n() / 2)
2620 * @brief Resets the distribution state.
2631 { return _M_param
.n(); }
2634 * @brief Returns the parameter set of the distribution.
2638 { return _M_param
; }
2641 * @brief Sets the parameter set of the distribution.
2642 * @param __param The new parameter set of the distribution.
2645 param(const param_type
& __param
)
2648 typedef typename
std::gamma_distribution
<result_type
>::param_type
2650 _M_gd
.param(param_type
{__param
.n() / 2});
2654 * @brief Returns the greatest lower bound value of the distribution.
2658 { return result_type(0); }
2661 * @brief Returns the least upper bound value of the distribution.
2665 { return std::numeric_limits
<result_type
>::max(); }
2668 * @brief Generating functions.
2670 template<typename _UniformRandomNumberGenerator
>
2672 operator()(_UniformRandomNumberGenerator
& __urng
)
2673 { return 2 * _M_gd(__urng
); }
2675 template<typename _UniformRandomNumberGenerator
>
2677 operator()(_UniformRandomNumberGenerator
& __urng
,
2678 const param_type
& __p
)
2680 typedef typename
std::gamma_distribution
<result_type
>::param_type
2682 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2685 template<typename _ForwardIterator
,
2686 typename _UniformRandomNumberGenerator
>
2688 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2689 _UniformRandomNumberGenerator
& __urng
)
2690 { this->__generate_impl(__f
, __t
, __urng
); }
2692 template<typename _ForwardIterator
,
2693 typename _UniformRandomNumberGenerator
>
2695 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2696 _UniformRandomNumberGenerator
& __urng
,
2697 const param_type
& __p
)
2698 { typename
std::gamma_distribution
<result_type
>::param_type
2700 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2702 template<typename _UniformRandomNumberGenerator
>
2704 __generate(result_type
* __f
, result_type
* __t
,
2705 _UniformRandomNumberGenerator
& __urng
)
2706 { this->__generate_impl(__f
, __t
, __urng
); }
2708 template<typename _UniformRandomNumberGenerator
>
2710 __generate(result_type
* __f
, result_type
* __t
,
2711 _UniformRandomNumberGenerator
& __urng
,
2712 const param_type
& __p
)
2713 { typename
std::gamma_distribution
<result_type
>::param_type
2715 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2718 * @brief Return true if two Chi-squared distributions have
2719 * the same parameters and the sequences that would be
2720 * generated are equal.
2723 operator==(const chi_squared_distribution
& __d1
,
2724 const chi_squared_distribution
& __d2
)
2725 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
2728 * @brief Inserts a %chi_squared_distribution random number distribution
2729 * @p __x into the output stream @p __os.
2731 * @param __os An output stream.
2732 * @param __x A %chi_squared_distribution random number distribution.
2734 * @returns The output stream with the state of @p __x inserted or in
2737 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2738 friend std::basic_ostream
<_CharT
, _Traits
>&
2739 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2740 const std::chi_squared_distribution
<_RealType1
>& __x
);
2743 * @brief Extracts a %chi_squared_distribution random number distribution
2744 * @p __x from the input stream @p __is.
2746 * @param __is An input stream.
2747 * @param __x A %chi_squared_distribution random number
2750 * @returns The input stream with @p __x extracted or in an error state.
2752 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2753 friend std::basic_istream
<_CharT
, _Traits
>&
2754 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2755 std::chi_squared_distribution
<_RealType1
>& __x
);
2758 template<typename _ForwardIterator
,
2759 typename _UniformRandomNumberGenerator
>
2761 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2762 _UniformRandomNumberGenerator
& __urng
);
2764 template<typename _ForwardIterator
,
2765 typename _UniformRandomNumberGenerator
>
2767 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2768 _UniformRandomNumberGenerator
& __urng
,
2770 std::gamma_distribution
<result_type
>::param_type
& __p
);
2772 param_type _M_param
;
2774 std::gamma_distribution
<result_type
> _M_gd
;
2778 * @brief Return true if two Chi-squared distributions are different.
2780 template<typename _RealType
>
2782 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2783 const std::chi_squared_distribution
<_RealType
>& __d2
)
2784 { return !(__d1
== __d2
); }
2788 * @brief A cauchy_distribution random number distribution.
2790 * The formula for the normal probability mass function is
2791 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2793 template<typename _RealType
= double>
2794 class cauchy_distribution
2796 static_assert(std::is_floating_point
<_RealType
>::value
,
2797 "result_type must be a floating point type");
2800 /** The type of the range of the distribution. */
2801 typedef _RealType result_type
;
2803 /** Parameter type. */
2806 typedef cauchy_distribution
<_RealType
> distribution_type
;
2809 param_type(_RealType __a
= _RealType(0),
2810 _RealType __b
= _RealType(1))
2811 : _M_a(__a
), _M_b(__b
)
2823 operator==(const param_type
& __p1
, const param_type
& __p2
)
2824 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2827 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2828 { return !(__p1
== __p2
); }
2836 cauchy_distribution(_RealType __a
= _RealType(0),
2837 _RealType __b
= _RealType(1))
2838 : _M_param(__a
, __b
)
2842 cauchy_distribution(const param_type
& __p
)
2847 * @brief Resets the distribution state.
2858 { return _M_param
.a(); }
2862 { return _M_param
.b(); }
2865 * @brief Returns the parameter set of the distribution.
2869 { return _M_param
; }
2872 * @brief Sets the parameter set of the distribution.
2873 * @param __param The new parameter set of the distribution.
2876 param(const param_type
& __param
)
2877 { _M_param
= __param
; }
2880 * @brief Returns the greatest lower bound value of the distribution.
2884 { return std::numeric_limits
<result_type
>::lowest(); }
2887 * @brief Returns the least upper bound value of the distribution.
2891 { return std::numeric_limits
<result_type
>::max(); }
2894 * @brief Generating functions.
2896 template<typename _UniformRandomNumberGenerator
>
2898 operator()(_UniformRandomNumberGenerator
& __urng
)
2899 { return this->operator()(__urng
, _M_param
); }
2901 template<typename _UniformRandomNumberGenerator
>
2903 operator()(_UniformRandomNumberGenerator
& __urng
,
2904 const param_type
& __p
);
2906 template<typename _ForwardIterator
,
2907 typename _UniformRandomNumberGenerator
>
2909 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2910 _UniformRandomNumberGenerator
& __urng
)
2911 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2913 template<typename _ForwardIterator
,
2914 typename _UniformRandomNumberGenerator
>
2916 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2917 _UniformRandomNumberGenerator
& __urng
,
2918 const param_type
& __p
)
2919 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2921 template<typename _UniformRandomNumberGenerator
>
2923 __generate(result_type
* __f
, result_type
* __t
,
2924 _UniformRandomNumberGenerator
& __urng
,
2925 const param_type
& __p
)
2926 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2929 * @brief Return true if two Cauchy distributions have
2930 * the same parameters.
2933 operator==(const cauchy_distribution
& __d1
,
2934 const cauchy_distribution
& __d2
)
2935 { return __d1
._M_param
== __d2
._M_param
; }
2938 template<typename _ForwardIterator
,
2939 typename _UniformRandomNumberGenerator
>
2941 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2942 _UniformRandomNumberGenerator
& __urng
,
2943 const param_type
& __p
);
2945 param_type _M_param
;
2949 * @brief Return true if two Cauchy distributions have
2950 * different parameters.
2952 template<typename _RealType
>
2954 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
2955 const std::cauchy_distribution
<_RealType
>& __d2
)
2956 { return !(__d1
== __d2
); }
2959 * @brief Inserts a %cauchy_distribution random number distribution
2960 * @p __x into the output stream @p __os.
2962 * @param __os An output stream.
2963 * @param __x A %cauchy_distribution random number distribution.
2965 * @returns The output stream with the state of @p __x inserted or in
2968 template<typename _RealType
, typename _CharT
, typename _Traits
>
2969 std::basic_ostream
<_CharT
, _Traits
>&
2970 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2971 const std::cauchy_distribution
<_RealType
>& __x
);
2974 * @brief Extracts a %cauchy_distribution random number distribution
2975 * @p __x from the input stream @p __is.
2977 * @param __is An input stream.
2978 * @param __x A %cauchy_distribution random number
2981 * @returns The input stream with @p __x extracted or in an error state.
2983 template<typename _RealType
, typename _CharT
, typename _Traits
>
2984 std::basic_istream
<_CharT
, _Traits
>&
2985 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2986 std::cauchy_distribution
<_RealType
>& __x
);
2990 * @brief A fisher_f_distribution random number distribution.
2992 * The formula for the normal probability mass function is
2994 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2995 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2996 * (1 + \frac{mx}{n})^{-(m+n)/2}
2999 template<typename _RealType
= double>
3000 class fisher_f_distribution
3002 static_assert(std::is_floating_point
<_RealType
>::value
,
3003 "result_type must be a floating point type");
3006 /** The type of the range of the distribution. */
3007 typedef _RealType result_type
;
3009 /** Parameter type. */
3012 typedef fisher_f_distribution
<_RealType
> distribution_type
;
3015 param_type(_RealType __m
= _RealType(1),
3016 _RealType __n
= _RealType(1))
3017 : _M_m(__m
), _M_n(__n
)
3029 operator==(const param_type
& __p1
, const param_type
& __p2
)
3030 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
3033 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3034 { return !(__p1
== __p2
); }
3042 fisher_f_distribution(_RealType __m
= _RealType(1),
3043 _RealType __n
= _RealType(1))
3044 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
3048 fisher_f_distribution(const param_type
& __p
)
3049 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
3053 * @brief Resets the distribution state.
3067 { return _M_param
.m(); }
3071 { return _M_param
.n(); }
3074 * @brief Returns the parameter set of the distribution.
3078 { return _M_param
; }
3081 * @brief Sets the parameter set of the distribution.
3082 * @param __param The new parameter set of the distribution.
3085 param(const param_type
& __param
)
3086 { _M_param
= __param
; }
3089 * @brief Returns the greatest lower bound value of the distribution.
3093 { return result_type(0); }
3096 * @brief Returns the least upper bound value of the distribution.
3100 { return std::numeric_limits
<result_type
>::max(); }
3103 * @brief Generating functions.
3105 template<typename _UniformRandomNumberGenerator
>
3107 operator()(_UniformRandomNumberGenerator
& __urng
)
3108 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
3110 template<typename _UniformRandomNumberGenerator
>
3112 operator()(_UniformRandomNumberGenerator
& __urng
,
3113 const param_type
& __p
)
3115 typedef typename
std::gamma_distribution
<result_type
>::param_type
3117 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
3118 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
3121 template<typename _ForwardIterator
,
3122 typename _UniformRandomNumberGenerator
>
3124 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3125 _UniformRandomNumberGenerator
& __urng
)
3126 { this->__generate_impl(__f
, __t
, __urng
); }
3128 template<typename _ForwardIterator
,
3129 typename _UniformRandomNumberGenerator
>
3131 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3132 _UniformRandomNumberGenerator
& __urng
,
3133 const param_type
& __p
)
3134 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3136 template<typename _UniformRandomNumberGenerator
>
3138 __generate(result_type
* __f
, result_type
* __t
,
3139 _UniformRandomNumberGenerator
& __urng
)
3140 { this->__generate_impl(__f
, __t
, __urng
); }
3142 template<typename _UniformRandomNumberGenerator
>
3144 __generate(result_type
* __f
, result_type
* __t
,
3145 _UniformRandomNumberGenerator
& __urng
,
3146 const param_type
& __p
)
3147 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3150 * @brief Return true if two Fisher f distributions have
3151 * the same parameters and the sequences that would
3152 * be generated are equal.
3155 operator==(const fisher_f_distribution
& __d1
,
3156 const fisher_f_distribution
& __d2
)
3157 { return (__d1
._M_param
== __d2
._M_param
3158 && __d1
._M_gd_x
== __d2
._M_gd_x
3159 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
3162 * @brief Inserts a %fisher_f_distribution random number distribution
3163 * @p __x into the output stream @p __os.
3165 * @param __os An output stream.
3166 * @param __x A %fisher_f_distribution random number distribution.
3168 * @returns The output stream with the state of @p __x inserted or in
3171 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3172 friend std::basic_ostream
<_CharT
, _Traits
>&
3173 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3174 const std::fisher_f_distribution
<_RealType1
>& __x
);
3177 * @brief Extracts a %fisher_f_distribution random number distribution
3178 * @p __x from the input stream @p __is.
3180 * @param __is An input stream.
3181 * @param __x A %fisher_f_distribution random number
3184 * @returns The input stream with @p __x extracted or in an error state.
3186 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3187 friend std::basic_istream
<_CharT
, _Traits
>&
3188 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3189 std::fisher_f_distribution
<_RealType1
>& __x
);
3192 template<typename _ForwardIterator
,
3193 typename _UniformRandomNumberGenerator
>
3195 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3196 _UniformRandomNumberGenerator
& __urng
);
3198 template<typename _ForwardIterator
,
3199 typename _UniformRandomNumberGenerator
>
3201 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3202 _UniformRandomNumberGenerator
& __urng
,
3203 const param_type
& __p
);
3205 param_type _M_param
;
3207 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3211 * @brief Return true if two Fisher f distributions are different.
3213 template<typename _RealType
>
3215 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3216 const std::fisher_f_distribution
<_RealType
>& __d2
)
3217 { return !(__d1
== __d2
); }
3220 * @brief A student_t_distribution random number distribution.
3222 * The formula for the normal probability mass function is:
3224 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3225 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3228 template<typename _RealType
= double>
3229 class student_t_distribution
3231 static_assert(std::is_floating_point
<_RealType
>::value
,
3232 "result_type must be a floating point type");
3235 /** The type of the range of the distribution. */
3236 typedef _RealType result_type
;
3238 /** Parameter type. */
3241 typedef student_t_distribution
<_RealType
> distribution_type
;
3244 param_type(_RealType __n
= _RealType(1))
3253 operator==(const param_type
& __p1
, const param_type
& __p2
)
3254 { return __p1
._M_n
== __p2
._M_n
; }
3257 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3258 { return !(__p1
== __p2
); }
3265 student_t_distribution(_RealType __n
= _RealType(1))
3266 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3270 student_t_distribution(const param_type
& __p
)
3271 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3275 * @brief Resets the distribution state.
3289 { return _M_param
.n(); }
3292 * @brief Returns the parameter set of the distribution.
3296 { return _M_param
; }
3299 * @brief Sets the parameter set of the distribution.
3300 * @param __param The new parameter set of the distribution.
3303 param(const param_type
& __param
)
3304 { _M_param
= __param
; }
3307 * @brief Returns the greatest lower bound value of the distribution.
3311 { return std::numeric_limits
<result_type
>::lowest(); }
3314 * @brief Returns the least upper bound value of the distribution.
3318 { return std::numeric_limits
<result_type
>::max(); }
3321 * @brief Generating functions.
3323 template<typename _UniformRandomNumberGenerator
>
3325 operator()(_UniformRandomNumberGenerator
& __urng
)
3326 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3328 template<typename _UniformRandomNumberGenerator
>
3330 operator()(_UniformRandomNumberGenerator
& __urng
,
3331 const param_type
& __p
)
3333 typedef typename
std::gamma_distribution
<result_type
>::param_type
3336 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3337 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3340 template<typename _ForwardIterator
,
3341 typename _UniformRandomNumberGenerator
>
3343 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3344 _UniformRandomNumberGenerator
& __urng
)
3345 { this->__generate_impl(__f
, __t
, __urng
); }
3347 template<typename _ForwardIterator
,
3348 typename _UniformRandomNumberGenerator
>
3350 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3351 _UniformRandomNumberGenerator
& __urng
,
3352 const param_type
& __p
)
3353 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3355 template<typename _UniformRandomNumberGenerator
>
3357 __generate(result_type
* __f
, result_type
* __t
,
3358 _UniformRandomNumberGenerator
& __urng
)
3359 { this->__generate_impl(__f
, __t
, __urng
); }
3361 template<typename _UniformRandomNumberGenerator
>
3363 __generate(result_type
* __f
, result_type
* __t
,
3364 _UniformRandomNumberGenerator
& __urng
,
3365 const param_type
& __p
)
3366 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3369 * @brief Return true if two Student t distributions have
3370 * the same parameters and the sequences that would
3371 * be generated are equal.
3374 operator==(const student_t_distribution
& __d1
,
3375 const student_t_distribution
& __d2
)
3376 { return (__d1
._M_param
== __d2
._M_param
3377 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3380 * @brief Inserts a %student_t_distribution random number distribution
3381 * @p __x into the output stream @p __os.
3383 * @param __os An output stream.
3384 * @param __x A %student_t_distribution random number distribution.
3386 * @returns The output stream with the state of @p __x inserted or in
3389 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3390 friend std::basic_ostream
<_CharT
, _Traits
>&
3391 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3392 const std::student_t_distribution
<_RealType1
>& __x
);
3395 * @brief Extracts a %student_t_distribution random number distribution
3396 * @p __x from the input stream @p __is.
3398 * @param __is An input stream.
3399 * @param __x A %student_t_distribution random number
3402 * @returns The input stream with @p __x extracted or in an error state.
3404 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3405 friend std::basic_istream
<_CharT
, _Traits
>&
3406 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3407 std::student_t_distribution
<_RealType1
>& __x
);
3410 template<typename _ForwardIterator
,
3411 typename _UniformRandomNumberGenerator
>
3413 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3414 _UniformRandomNumberGenerator
& __urng
);
3415 template<typename _ForwardIterator
,
3416 typename _UniformRandomNumberGenerator
>
3418 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3419 _UniformRandomNumberGenerator
& __urng
,
3420 const param_type
& __p
);
3422 param_type _M_param
;
3424 std::normal_distribution
<result_type
> _M_nd
;
3425 std::gamma_distribution
<result_type
> _M_gd
;
3429 * @brief Return true if two Student t distributions are different.
3431 template<typename _RealType
>
3433 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3434 const std::student_t_distribution
<_RealType
>& __d2
)
3435 { return !(__d1
== __d2
); }
3438 /* @} */ // group random_distributions_normal
3441 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3442 * @ingroup random_distributions
3447 * @brief A Bernoulli random number distribution.
3449 * Generates a sequence of true and false values with likelihood @f$p@f$
3450 * that true will come up and @f$(1 - p)@f$ that false will appear.
3452 class bernoulli_distribution
3455 /** The type of the range of the distribution. */
3456 typedef bool result_type
;
3458 /** Parameter type. */
3461 typedef bernoulli_distribution distribution_type
;
3464 param_type(double __p
= 0.5)
3467 __glibcxx_assert((_M_p
>= 0.0) && (_M_p
<= 1.0));
3475 operator==(const param_type
& __p1
, const param_type
& __p2
)
3476 { return __p1
._M_p
== __p2
._M_p
; }
3479 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3480 { return !(__p1
== __p2
); }
3488 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3490 * @param __p [IN] The likelihood of a true result being returned.
3491 * Must be in the interval @f$[0, 1]@f$.
3494 bernoulli_distribution(double __p
= 0.5)
3499 bernoulli_distribution(const param_type
& __p
)
3504 * @brief Resets the distribution state.
3506 * Does nothing for a Bernoulli distribution.
3512 * @brief Returns the @p p parameter of the distribution.
3516 { return _M_param
.p(); }
3519 * @brief Returns the parameter set of the distribution.
3523 { return _M_param
; }
3526 * @brief Sets the parameter set of the distribution.
3527 * @param __param The new parameter set of the distribution.
3530 param(const param_type
& __param
)
3531 { _M_param
= __param
; }
3534 * @brief Returns the greatest lower bound value of the distribution.
3538 { return std::numeric_limits
<result_type
>::min(); }
3541 * @brief Returns the least upper bound value of the distribution.
3545 { return std::numeric_limits
<result_type
>::max(); }
3548 * @brief Generating functions.
3550 template<typename _UniformRandomNumberGenerator
>
3552 operator()(_UniformRandomNumberGenerator
& __urng
)
3553 { return this->operator()(__urng
, _M_param
); }
3555 template<typename _UniformRandomNumberGenerator
>
3557 operator()(_UniformRandomNumberGenerator
& __urng
,
3558 const param_type
& __p
)
3560 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3562 if ((__aurng() - __aurng
.min())
3563 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3568 template<typename _ForwardIterator
,
3569 typename _UniformRandomNumberGenerator
>
3571 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3572 _UniformRandomNumberGenerator
& __urng
)
3573 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3575 template<typename _ForwardIterator
,
3576 typename _UniformRandomNumberGenerator
>
3578 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3579 _UniformRandomNumberGenerator
& __urng
, const param_type
& __p
)
3580 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3582 template<typename _UniformRandomNumberGenerator
>
3584 __generate(result_type
* __f
, result_type
* __t
,
3585 _UniformRandomNumberGenerator
& __urng
,
3586 const param_type
& __p
)
3587 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3590 * @brief Return true if two Bernoulli distributions have
3591 * the same parameters.
3594 operator==(const bernoulli_distribution
& __d1
,
3595 const bernoulli_distribution
& __d2
)
3596 { return __d1
._M_param
== __d2
._M_param
; }
3599 template<typename _ForwardIterator
,
3600 typename _UniformRandomNumberGenerator
>
3602 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3603 _UniformRandomNumberGenerator
& __urng
,
3604 const param_type
& __p
);
3606 param_type _M_param
;
3610 * @brief Return true if two Bernoulli distributions have
3611 * different parameters.
3614 operator!=(const std::bernoulli_distribution
& __d1
,
3615 const std::bernoulli_distribution
& __d2
)
3616 { return !(__d1
== __d2
); }
3619 * @brief Inserts a %bernoulli_distribution random number distribution
3620 * @p __x into the output stream @p __os.
3622 * @param __os An output stream.
3623 * @param __x A %bernoulli_distribution random number distribution.
3625 * @returns The output stream with the state of @p __x inserted or in
3628 template<typename _CharT
, typename _Traits
>
3629 std::basic_ostream
<_CharT
, _Traits
>&
3630 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3631 const std::bernoulli_distribution
& __x
);
3634 * @brief Extracts a %bernoulli_distribution random number distribution
3635 * @p __x from the input stream @p __is.
3637 * @param __is An input stream.
3638 * @param __x A %bernoulli_distribution random number generator engine.
3640 * @returns The input stream with @p __x extracted or in an error state.
3642 template<typename _CharT
, typename _Traits
>
3643 std::basic_istream
<_CharT
, _Traits
>&
3644 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3645 std::bernoulli_distribution
& __x
)
3649 __x
.param(bernoulli_distribution::param_type(__p
));
3655 * @brief A discrete binomial random number distribution.
3657 * The formula for the binomial probability density function is
3658 * @f$p(i|t,p) = \binom{t}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3659 * and @f$p@f$ are the parameters of the distribution.
3661 template<typename _IntType
= int>
3662 class binomial_distribution
3664 static_assert(std::is_integral
<_IntType
>::value
,
3665 "result_type must be an integral type");
3668 /** The type of the range of the distribution. */
3669 typedef _IntType result_type
;
3671 /** Parameter type. */
3674 typedef binomial_distribution
<_IntType
> distribution_type
;
3675 friend class binomial_distribution
<_IntType
>;
3678 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3679 : _M_t(__t
), _M_p(__p
)
3681 __glibcxx_assert((_M_t
>= _IntType(0))
3696 operator==(const param_type
& __p1
, const param_type
& __p2
)
3697 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3700 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3701 { return !(__p1
== __p2
); }
3711 #if _GLIBCXX_USE_C99_MATH_TR1
3712 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3713 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3718 // constructors and member function
3720 binomial_distribution(_IntType __t
= _IntType(1),
3722 : _M_param(__t
, __p
), _M_nd()
3726 binomial_distribution(const param_type
& __p
)
3727 : _M_param(__p
), _M_nd()
3731 * @brief Resets the distribution state.
3738 * @brief Returns the distribution @p t parameter.
3742 { return _M_param
.t(); }
3745 * @brief Returns the distribution @p p parameter.
3749 { return _M_param
.p(); }
3752 * @brief Returns the parameter set of the distribution.
3756 { return _M_param
; }
3759 * @brief Sets the parameter set of the distribution.
3760 * @param __param The new parameter set of the distribution.
3763 param(const param_type
& __param
)
3764 { _M_param
= __param
; }
3767 * @brief Returns the greatest lower bound value of the distribution.
3774 * @brief Returns the least upper bound value of the distribution.
3778 { return _M_param
.t(); }
3781 * @brief Generating functions.
3783 template<typename _UniformRandomNumberGenerator
>
3785 operator()(_UniformRandomNumberGenerator
& __urng
)
3786 { return this->operator()(__urng
, _M_param
); }
3788 template<typename _UniformRandomNumberGenerator
>
3790 operator()(_UniformRandomNumberGenerator
& __urng
,
3791 const param_type
& __p
);
3793 template<typename _ForwardIterator
,
3794 typename _UniformRandomNumberGenerator
>
3796 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3797 _UniformRandomNumberGenerator
& __urng
)
3798 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3800 template<typename _ForwardIterator
,
3801 typename _UniformRandomNumberGenerator
>
3803 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3804 _UniformRandomNumberGenerator
& __urng
,
3805 const param_type
& __p
)
3806 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3808 template<typename _UniformRandomNumberGenerator
>
3810 __generate(result_type
* __f
, result_type
* __t
,
3811 _UniformRandomNumberGenerator
& __urng
,
3812 const param_type
& __p
)
3813 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3816 * @brief Return true if two binomial distributions have
3817 * the same parameters and the sequences that would
3818 * be generated are equal.
3821 operator==(const binomial_distribution
& __d1
,
3822 const binomial_distribution
& __d2
)
3823 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3824 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
3826 { return __d1
._M_param
== __d2
._M_param
; }
3830 * @brief Inserts a %binomial_distribution random number distribution
3831 * @p __x into the output stream @p __os.
3833 * @param __os An output stream.
3834 * @param __x A %binomial_distribution random number distribution.
3836 * @returns The output stream with the state of @p __x inserted or in
3839 template<typename _IntType1
,
3840 typename _CharT
, typename _Traits
>
3841 friend std::basic_ostream
<_CharT
, _Traits
>&
3842 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3843 const std::binomial_distribution
<_IntType1
>& __x
);
3846 * @brief Extracts a %binomial_distribution random number distribution
3847 * @p __x from the input stream @p __is.
3849 * @param __is An input stream.
3850 * @param __x A %binomial_distribution random number generator engine.
3852 * @returns The input stream with @p __x extracted or in an error
3855 template<typename _IntType1
,
3856 typename _CharT
, typename _Traits
>
3857 friend std::basic_istream
<_CharT
, _Traits
>&
3858 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3859 std::binomial_distribution
<_IntType1
>& __x
);
3862 template<typename _ForwardIterator
,
3863 typename _UniformRandomNumberGenerator
>
3865 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3866 _UniformRandomNumberGenerator
& __urng
,
3867 const param_type
& __p
);
3869 template<typename _UniformRandomNumberGenerator
>
3871 _M_waiting(_UniformRandomNumberGenerator
& __urng
,
3872 _IntType __t
, double __q
);
3874 param_type _M_param
;
3876 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3877 std::normal_distribution
<double> _M_nd
;
3881 * @brief Return true if two binomial distributions are different.
3883 template<typename _IntType
>
3885 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
3886 const std::binomial_distribution
<_IntType
>& __d2
)
3887 { return !(__d1
== __d2
); }
3891 * @brief A discrete geometric random number distribution.
3893 * The formula for the geometric probability density function is
3894 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
3897 template<typename _IntType
= int>
3898 class geometric_distribution
3900 static_assert(std::is_integral
<_IntType
>::value
,
3901 "result_type must be an integral type");
3904 /** The type of the range of the distribution. */
3905 typedef _IntType result_type
;
3907 /** Parameter type. */
3910 typedef geometric_distribution
<_IntType
> distribution_type
;
3911 friend class geometric_distribution
<_IntType
>;
3914 param_type(double __p
= 0.5)
3917 __glibcxx_assert((_M_p
> 0.0) && (_M_p
< 1.0));
3926 operator==(const param_type
& __p1
, const param_type
& __p2
)
3927 { return __p1
._M_p
== __p2
._M_p
; }
3930 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3931 { return !(__p1
== __p2
); }
3936 { _M_log_1_p
= std::log(1.0 - _M_p
); }
3943 // constructors and member function
3945 geometric_distribution(double __p
= 0.5)
3950 geometric_distribution(const param_type
& __p
)
3955 * @brief Resets the distribution state.
3957 * Does nothing for the geometric distribution.
3963 * @brief Returns the distribution parameter @p p.
3967 { return _M_param
.p(); }
3970 * @brief Returns the parameter set of the distribution.
3974 { return _M_param
; }
3977 * @brief Sets the parameter set of the distribution.
3978 * @param __param The new parameter set of the distribution.
3981 param(const param_type
& __param
)
3982 { _M_param
= __param
; }
3985 * @brief Returns the greatest lower bound value of the distribution.
3992 * @brief Returns the least upper bound value of the distribution.
3996 { return std::numeric_limits
<result_type
>::max(); }
3999 * @brief Generating functions.
4001 template<typename _UniformRandomNumberGenerator
>
4003 operator()(_UniformRandomNumberGenerator
& __urng
)
4004 { return this->operator()(__urng
, _M_param
); }
4006 template<typename _UniformRandomNumberGenerator
>
4008 operator()(_UniformRandomNumberGenerator
& __urng
,
4009 const param_type
& __p
);
4011 template<typename _ForwardIterator
,
4012 typename _UniformRandomNumberGenerator
>
4014 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4015 _UniformRandomNumberGenerator
& __urng
)
4016 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4018 template<typename _ForwardIterator
,
4019 typename _UniformRandomNumberGenerator
>
4021 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4022 _UniformRandomNumberGenerator
& __urng
,
4023 const param_type
& __p
)
4024 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4026 template<typename _UniformRandomNumberGenerator
>
4028 __generate(result_type
* __f
, result_type
* __t
,
4029 _UniformRandomNumberGenerator
& __urng
,
4030 const param_type
& __p
)
4031 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4034 * @brief Return true if two geometric distributions have
4035 * the same parameters.
4038 operator==(const geometric_distribution
& __d1
,
4039 const geometric_distribution
& __d2
)
4040 { return __d1
._M_param
== __d2
._M_param
; }
4043 template<typename _ForwardIterator
,
4044 typename _UniformRandomNumberGenerator
>
4046 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4047 _UniformRandomNumberGenerator
& __urng
,
4048 const param_type
& __p
);
4050 param_type _M_param
;
4054 * @brief Return true if two geometric distributions have
4055 * different parameters.
4057 template<typename _IntType
>
4059 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
4060 const std::geometric_distribution
<_IntType
>& __d2
)
4061 { return !(__d1
== __d2
); }
4064 * @brief Inserts a %geometric_distribution random number distribution
4065 * @p __x into the output stream @p __os.
4067 * @param __os An output stream.
4068 * @param __x A %geometric_distribution random number distribution.
4070 * @returns The output stream with the state of @p __x inserted or in
4073 template<typename _IntType
,
4074 typename _CharT
, typename _Traits
>
4075 std::basic_ostream
<_CharT
, _Traits
>&
4076 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4077 const std::geometric_distribution
<_IntType
>& __x
);
4080 * @brief Extracts a %geometric_distribution random number distribution
4081 * @p __x from the input stream @p __is.
4083 * @param __is An input stream.
4084 * @param __x A %geometric_distribution random number generator engine.
4086 * @returns The input stream with @p __x extracted or in an error state.
4088 template<typename _IntType
,
4089 typename _CharT
, typename _Traits
>
4090 std::basic_istream
<_CharT
, _Traits
>&
4091 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4092 std::geometric_distribution
<_IntType
>& __x
);
4096 * @brief A negative_binomial_distribution random number distribution.
4098 * The formula for the negative binomial probability mass function is
4099 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
4100 * and @f$p@f$ are the parameters of the distribution.
4102 template<typename _IntType
= int>
4103 class negative_binomial_distribution
4105 static_assert(std::is_integral
<_IntType
>::value
,
4106 "result_type must be an integral type");
4109 /** The type of the range of the distribution. */
4110 typedef _IntType result_type
;
4112 /** Parameter type. */
4115 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
4118 param_type(_IntType __k
= 1, double __p
= 0.5)
4119 : _M_k(__k
), _M_p(__p
)
4121 __glibcxx_assert((_M_k
> 0) && (_M_p
> 0.0) && (_M_p
<= 1.0));
4133 operator==(const param_type
& __p1
, const param_type
& __p2
)
4134 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
4137 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4138 { return !(__p1
== __p2
); }
4146 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
4147 : _M_param(__k
, __p
), _M_gd(__k
, (1.0 - __p
) / __p
)
4151 negative_binomial_distribution(const param_type
& __p
)
4152 : _M_param(__p
), _M_gd(__p
.k(), (1.0 - __p
.p()) / __p
.p())
4156 * @brief Resets the distribution state.
4163 * @brief Return the @f$k@f$ parameter of the distribution.
4167 { return _M_param
.k(); }
4170 * @brief Return the @f$p@f$ parameter of the distribution.
4174 { return _M_param
.p(); }
4177 * @brief Returns the parameter set of the distribution.
4181 { return _M_param
; }
4184 * @brief Sets the parameter set of the distribution.
4185 * @param __param The new parameter set of the distribution.
4188 param(const param_type
& __param
)
4189 { _M_param
= __param
; }
4192 * @brief Returns the greatest lower bound value of the distribution.
4196 { return result_type(0); }
4199 * @brief Returns the least upper bound value of the distribution.
4203 { return std::numeric_limits
<result_type
>::max(); }
4206 * @brief Generating functions.
4208 template<typename _UniformRandomNumberGenerator
>
4210 operator()(_UniformRandomNumberGenerator
& __urng
);
4212 template<typename _UniformRandomNumberGenerator
>
4214 operator()(_UniformRandomNumberGenerator
& __urng
,
4215 const param_type
& __p
);
4217 template<typename _ForwardIterator
,
4218 typename _UniformRandomNumberGenerator
>
4220 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4221 _UniformRandomNumberGenerator
& __urng
)
4222 { this->__generate_impl(__f
, __t
, __urng
); }
4224 template<typename _ForwardIterator
,
4225 typename _UniformRandomNumberGenerator
>
4227 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4228 _UniformRandomNumberGenerator
& __urng
,
4229 const param_type
& __p
)
4230 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4232 template<typename _UniformRandomNumberGenerator
>
4234 __generate(result_type
* __f
, result_type
* __t
,
4235 _UniformRandomNumberGenerator
& __urng
)
4236 { this->__generate_impl(__f
, __t
, __urng
); }
4238 template<typename _UniformRandomNumberGenerator
>
4240 __generate(result_type
* __f
, result_type
* __t
,
4241 _UniformRandomNumberGenerator
& __urng
,
4242 const param_type
& __p
)
4243 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4246 * @brief Return true if two negative binomial distributions have
4247 * the same parameters and the sequences that would be
4248 * generated are equal.
4251 operator==(const negative_binomial_distribution
& __d1
,
4252 const negative_binomial_distribution
& __d2
)
4253 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
4256 * @brief Inserts a %negative_binomial_distribution random
4257 * number distribution @p __x into the output stream @p __os.
4259 * @param __os An output stream.
4260 * @param __x A %negative_binomial_distribution random number
4263 * @returns The output stream with the state of @p __x inserted or in
4266 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4267 friend std::basic_ostream
<_CharT
, _Traits
>&
4268 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4269 const std::negative_binomial_distribution
<_IntType1
>& __x
);
4272 * @brief Extracts a %negative_binomial_distribution random number
4273 * distribution @p __x from the input stream @p __is.
4275 * @param __is An input stream.
4276 * @param __x A %negative_binomial_distribution random number
4279 * @returns The input stream with @p __x extracted or in an error state.
4281 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4282 friend std::basic_istream
<_CharT
, _Traits
>&
4283 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4284 std::negative_binomial_distribution
<_IntType1
>& __x
);
4287 template<typename _ForwardIterator
,
4288 typename _UniformRandomNumberGenerator
>
4290 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4291 _UniformRandomNumberGenerator
& __urng
);
4292 template<typename _ForwardIterator
,
4293 typename _UniformRandomNumberGenerator
>
4295 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4296 _UniformRandomNumberGenerator
& __urng
,
4297 const param_type
& __p
);
4299 param_type _M_param
;
4301 std::gamma_distribution
<double> _M_gd
;
4305 * @brief Return true if two negative binomial distributions are different.
4307 template<typename _IntType
>
4309 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
4310 const std::negative_binomial_distribution
<_IntType
>& __d2
)
4311 { return !(__d1
== __d2
); }
4314 /* @} */ // group random_distributions_bernoulli
4317 * @addtogroup random_distributions_poisson Poisson Distributions
4318 * @ingroup random_distributions
4323 * @brief A discrete Poisson random number distribution.
4325 * The formula for the Poisson probability density function is
4326 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
4327 * parameter of the distribution.
4329 template<typename _IntType
= int>
4330 class poisson_distribution
4332 static_assert(std::is_integral
<_IntType
>::value
,
4333 "result_type must be an integral type");
4336 /** The type of the range of the distribution. */
4337 typedef _IntType result_type
;
4339 /** Parameter type. */
4342 typedef poisson_distribution
<_IntType
> distribution_type
;
4343 friend class poisson_distribution
<_IntType
>;
4346 param_type(double __mean
= 1.0)
4349 __glibcxx_assert(_M_mean
> 0.0);
4358 operator==(const param_type
& __p1
, const param_type
& __p2
)
4359 { return __p1
._M_mean
== __p2
._M_mean
; }
4362 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4363 { return !(__p1
== __p2
); }
4366 // Hosts either log(mean) or the threshold of the simple method.
4373 #if _GLIBCXX_USE_C99_MATH_TR1
4374 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
4378 // constructors and member function
4380 poisson_distribution(double __mean
= 1.0)
4381 : _M_param(__mean
), _M_nd()
4385 poisson_distribution(const param_type
& __p
)
4386 : _M_param(__p
), _M_nd()
4390 * @brief Resets the distribution state.
4397 * @brief Returns the distribution parameter @p mean.
4401 { return _M_param
.mean(); }
4404 * @brief Returns the parameter set of the distribution.
4408 { return _M_param
; }
4411 * @brief Sets the parameter set of the distribution.
4412 * @param __param The new parameter set of the distribution.
4415 param(const param_type
& __param
)
4416 { _M_param
= __param
; }
4419 * @brief Returns the greatest lower bound value of the distribution.
4426 * @brief Returns the least upper bound value of the distribution.
4430 { return std::numeric_limits
<result_type
>::max(); }
4433 * @brief Generating functions.
4435 template<typename _UniformRandomNumberGenerator
>
4437 operator()(_UniformRandomNumberGenerator
& __urng
)
4438 { return this->operator()(__urng
, _M_param
); }
4440 template<typename _UniformRandomNumberGenerator
>
4442 operator()(_UniformRandomNumberGenerator
& __urng
,
4443 const param_type
& __p
);
4445 template<typename _ForwardIterator
,
4446 typename _UniformRandomNumberGenerator
>
4448 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4449 _UniformRandomNumberGenerator
& __urng
)
4450 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4452 template<typename _ForwardIterator
,
4453 typename _UniformRandomNumberGenerator
>
4455 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4456 _UniformRandomNumberGenerator
& __urng
,
4457 const param_type
& __p
)
4458 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4460 template<typename _UniformRandomNumberGenerator
>
4462 __generate(result_type
* __f
, result_type
* __t
,
4463 _UniformRandomNumberGenerator
& __urng
,
4464 const param_type
& __p
)
4465 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4468 * @brief Return true if two Poisson distributions have the same
4469 * parameters and the sequences that would be generated
4473 operator==(const poisson_distribution
& __d1
,
4474 const poisson_distribution
& __d2
)
4475 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4476 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
4478 { return __d1
._M_param
== __d2
._M_param
; }
4482 * @brief Inserts a %poisson_distribution random number distribution
4483 * @p __x into the output stream @p __os.
4485 * @param __os An output stream.
4486 * @param __x A %poisson_distribution random number distribution.
4488 * @returns The output stream with the state of @p __x inserted or in
4491 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4492 friend std::basic_ostream
<_CharT
, _Traits
>&
4493 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4494 const std::poisson_distribution
<_IntType1
>& __x
);
4497 * @brief Extracts a %poisson_distribution random number distribution
4498 * @p __x from the input stream @p __is.
4500 * @param __is An input stream.
4501 * @param __x A %poisson_distribution random number generator engine.
4503 * @returns The input stream with @p __x extracted or in an error
4506 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4507 friend std::basic_istream
<_CharT
, _Traits
>&
4508 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4509 std::poisson_distribution
<_IntType1
>& __x
);
4512 template<typename _ForwardIterator
,
4513 typename _UniformRandomNumberGenerator
>
4515 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4516 _UniformRandomNumberGenerator
& __urng
,
4517 const param_type
& __p
);
4519 param_type _M_param
;
4521 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4522 std::normal_distribution
<double> _M_nd
;
4526 * @brief Return true if two Poisson distributions are different.
4528 template<typename _IntType
>
4530 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4531 const std::poisson_distribution
<_IntType
>& __d2
)
4532 { return !(__d1
== __d2
); }
4536 * @brief An exponential continuous distribution for random numbers.
4538 * The formula for the exponential probability density function is
4539 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4541 * <table border=1 cellpadding=10 cellspacing=0>
4542 * <caption align=top>Distribution Statistics</caption>
4543 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4544 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4545 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4546 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4547 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4550 template<typename _RealType
= double>
4551 class exponential_distribution
4553 static_assert(std::is_floating_point
<_RealType
>::value
,
4554 "result_type must be a floating point type");
4557 /** The type of the range of the distribution. */
4558 typedef _RealType result_type
;
4560 /** Parameter type. */
4563 typedef exponential_distribution
<_RealType
> distribution_type
;
4566 param_type(_RealType __lambda
= _RealType(1))
4567 : _M_lambda(__lambda
)
4569 __glibcxx_assert(_M_lambda
> _RealType(0));
4574 { return _M_lambda
; }
4577 operator==(const param_type
& __p1
, const param_type
& __p2
)
4578 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4581 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4582 { return !(__p1
== __p2
); }
4585 _RealType _M_lambda
;
4590 * @brief Constructs an exponential distribution with inverse scale
4591 * parameter @f$\lambda@f$.
4594 exponential_distribution(const result_type
& __lambda
= result_type(1))
4595 : _M_param(__lambda
)
4599 exponential_distribution(const param_type
& __p
)
4604 * @brief Resets the distribution state.
4606 * Has no effect on exponential distributions.
4612 * @brief Returns the inverse scale parameter of the distribution.
4616 { return _M_param
.lambda(); }
4619 * @brief Returns the parameter set of the distribution.
4623 { return _M_param
; }
4626 * @brief Sets the parameter set of the distribution.
4627 * @param __param The new parameter set of the distribution.
4630 param(const param_type
& __param
)
4631 { _M_param
= __param
; }
4634 * @brief Returns the greatest lower bound value of the distribution.
4638 { return result_type(0); }
4641 * @brief Returns the least upper bound value of the distribution.
4645 { return std::numeric_limits
<result_type
>::max(); }
4648 * @brief Generating functions.
4650 template<typename _UniformRandomNumberGenerator
>
4652 operator()(_UniformRandomNumberGenerator
& __urng
)
4653 { return this->operator()(__urng
, _M_param
); }
4655 template<typename _UniformRandomNumberGenerator
>
4657 operator()(_UniformRandomNumberGenerator
& __urng
,
4658 const param_type
& __p
)
4660 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4662 return -std::log(result_type(1) - __aurng()) / __p
.lambda();
4665 template<typename _ForwardIterator
,
4666 typename _UniformRandomNumberGenerator
>
4668 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4669 _UniformRandomNumberGenerator
& __urng
)
4670 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4672 template<typename _ForwardIterator
,
4673 typename _UniformRandomNumberGenerator
>
4675 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4676 _UniformRandomNumberGenerator
& __urng
,
4677 const param_type
& __p
)
4678 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4680 template<typename _UniformRandomNumberGenerator
>
4682 __generate(result_type
* __f
, result_type
* __t
,
4683 _UniformRandomNumberGenerator
& __urng
,
4684 const param_type
& __p
)
4685 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4688 * @brief Return true if two exponential distributions have the same
4692 operator==(const exponential_distribution
& __d1
,
4693 const exponential_distribution
& __d2
)
4694 { return __d1
._M_param
== __d2
._M_param
; }
4697 template<typename _ForwardIterator
,
4698 typename _UniformRandomNumberGenerator
>
4700 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4701 _UniformRandomNumberGenerator
& __urng
,
4702 const param_type
& __p
);
4704 param_type _M_param
;
4708 * @brief Return true if two exponential distributions have different
4711 template<typename _RealType
>
4713 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4714 const std::exponential_distribution
<_RealType
>& __d2
)
4715 { return !(__d1
== __d2
); }
4718 * @brief Inserts a %exponential_distribution random number distribution
4719 * @p __x into the output stream @p __os.
4721 * @param __os An output stream.
4722 * @param __x A %exponential_distribution random number distribution.
4724 * @returns The output stream with the state of @p __x inserted or in
4727 template<typename _RealType
, typename _CharT
, typename _Traits
>
4728 std::basic_ostream
<_CharT
, _Traits
>&
4729 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4730 const std::exponential_distribution
<_RealType
>& __x
);
4733 * @brief Extracts a %exponential_distribution random number distribution
4734 * @p __x from the input stream @p __is.
4736 * @param __is An input stream.
4737 * @param __x A %exponential_distribution random number
4740 * @returns The input stream with @p __x extracted or in an error state.
4742 template<typename _RealType
, typename _CharT
, typename _Traits
>
4743 std::basic_istream
<_CharT
, _Traits
>&
4744 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4745 std::exponential_distribution
<_RealType
>& __x
);
4749 * @brief A weibull_distribution random number distribution.
4751 * The formula for the normal probability density function is:
4753 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4754 * \exp{(-(\frac{x}{\beta})^\alpha)}
4757 template<typename _RealType
= double>
4758 class weibull_distribution
4760 static_assert(std::is_floating_point
<_RealType
>::value
,
4761 "result_type must be a floating point type");
4764 /** The type of the range of the distribution. */
4765 typedef _RealType result_type
;
4767 /** Parameter type. */
4770 typedef weibull_distribution
<_RealType
> distribution_type
;
4773 param_type(_RealType __a
= _RealType(1),
4774 _RealType __b
= _RealType(1))
4775 : _M_a(__a
), _M_b(__b
)
4787 operator==(const param_type
& __p1
, const param_type
& __p2
)
4788 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4791 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4792 { return !(__p1
== __p2
); }
4800 weibull_distribution(_RealType __a
= _RealType(1),
4801 _RealType __b
= _RealType(1))
4802 : _M_param(__a
, __b
)
4806 weibull_distribution(const param_type
& __p
)
4811 * @brief Resets the distribution state.
4818 * @brief Return the @f$a@f$ parameter of the distribution.
4822 { return _M_param
.a(); }
4825 * @brief Return the @f$b@f$ parameter of the distribution.
4829 { return _M_param
.b(); }
4832 * @brief Returns the parameter set of the distribution.
4836 { return _M_param
; }
4839 * @brief Sets the parameter set of the distribution.
4840 * @param __param The new parameter set of the distribution.
4843 param(const param_type
& __param
)
4844 { _M_param
= __param
; }
4847 * @brief Returns the greatest lower bound value of the distribution.
4851 { return result_type(0); }
4854 * @brief Returns the least upper bound value of the distribution.
4858 { return std::numeric_limits
<result_type
>::max(); }
4861 * @brief Generating functions.
4863 template<typename _UniformRandomNumberGenerator
>
4865 operator()(_UniformRandomNumberGenerator
& __urng
)
4866 { return this->operator()(__urng
, _M_param
); }
4868 template<typename _UniformRandomNumberGenerator
>
4870 operator()(_UniformRandomNumberGenerator
& __urng
,
4871 const param_type
& __p
);
4873 template<typename _ForwardIterator
,
4874 typename _UniformRandomNumberGenerator
>
4876 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4877 _UniformRandomNumberGenerator
& __urng
)
4878 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4880 template<typename _ForwardIterator
,
4881 typename _UniformRandomNumberGenerator
>
4883 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4884 _UniformRandomNumberGenerator
& __urng
,
4885 const param_type
& __p
)
4886 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4888 template<typename _UniformRandomNumberGenerator
>
4890 __generate(result_type
* __f
, result_type
* __t
,
4891 _UniformRandomNumberGenerator
& __urng
,
4892 const param_type
& __p
)
4893 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4896 * @brief Return true if two Weibull distributions have the same
4900 operator==(const weibull_distribution
& __d1
,
4901 const weibull_distribution
& __d2
)
4902 { return __d1
._M_param
== __d2
._M_param
; }
4905 template<typename _ForwardIterator
,
4906 typename _UniformRandomNumberGenerator
>
4908 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4909 _UniformRandomNumberGenerator
& __urng
,
4910 const param_type
& __p
);
4912 param_type _M_param
;
4916 * @brief Return true if two Weibull distributions have different
4919 template<typename _RealType
>
4921 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
4922 const std::weibull_distribution
<_RealType
>& __d2
)
4923 { return !(__d1
== __d2
); }
4926 * @brief Inserts a %weibull_distribution random number distribution
4927 * @p __x into the output stream @p __os.
4929 * @param __os An output stream.
4930 * @param __x A %weibull_distribution random number distribution.
4932 * @returns The output stream with the state of @p __x inserted or in
4935 template<typename _RealType
, typename _CharT
, typename _Traits
>
4936 std::basic_ostream
<_CharT
, _Traits
>&
4937 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4938 const std::weibull_distribution
<_RealType
>& __x
);
4941 * @brief Extracts a %weibull_distribution random number distribution
4942 * @p __x from the input stream @p __is.
4944 * @param __is An input stream.
4945 * @param __x A %weibull_distribution random number
4948 * @returns The input stream with @p __x extracted or in an error state.
4950 template<typename _RealType
, typename _CharT
, typename _Traits
>
4951 std::basic_istream
<_CharT
, _Traits
>&
4952 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4953 std::weibull_distribution
<_RealType
>& __x
);
4957 * @brief A extreme_value_distribution random number distribution.
4959 * The formula for the normal probability mass function is
4961 * p(x|a,b) = \frac{1}{b}
4962 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
4965 template<typename _RealType
= double>
4966 class extreme_value_distribution
4968 static_assert(std::is_floating_point
<_RealType
>::value
,
4969 "result_type must be a floating point type");
4972 /** The type of the range of the distribution. */
4973 typedef _RealType result_type
;
4975 /** Parameter type. */
4978 typedef extreme_value_distribution
<_RealType
> distribution_type
;
4981 param_type(_RealType __a
= _RealType(0),
4982 _RealType __b
= _RealType(1))
4983 : _M_a(__a
), _M_b(__b
)
4995 operator==(const param_type
& __p1
, const param_type
& __p2
)
4996 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4999 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5000 { return !(__p1
== __p2
); }
5008 extreme_value_distribution(_RealType __a
= _RealType(0),
5009 _RealType __b
= _RealType(1))
5010 : _M_param(__a
, __b
)
5014 extreme_value_distribution(const param_type
& __p
)
5019 * @brief Resets the distribution state.
5026 * @brief Return the @f$a@f$ parameter of the distribution.
5030 { return _M_param
.a(); }
5033 * @brief Return the @f$b@f$ parameter of the distribution.
5037 { return _M_param
.b(); }
5040 * @brief Returns the parameter set of the distribution.
5044 { return _M_param
; }
5047 * @brief Sets the parameter set of the distribution.
5048 * @param __param The new parameter set of the distribution.
5051 param(const param_type
& __param
)
5052 { _M_param
= __param
; }
5055 * @brief Returns the greatest lower bound value of the distribution.
5059 { return std::numeric_limits
<result_type
>::lowest(); }
5062 * @brief Returns the least upper bound value of the distribution.
5066 { return std::numeric_limits
<result_type
>::max(); }
5069 * @brief Generating functions.
5071 template<typename _UniformRandomNumberGenerator
>
5073 operator()(_UniformRandomNumberGenerator
& __urng
)
5074 { return this->operator()(__urng
, _M_param
); }
5076 template<typename _UniformRandomNumberGenerator
>
5078 operator()(_UniformRandomNumberGenerator
& __urng
,
5079 const param_type
& __p
);
5081 template<typename _ForwardIterator
,
5082 typename _UniformRandomNumberGenerator
>
5084 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5085 _UniformRandomNumberGenerator
& __urng
)
5086 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5088 template<typename _ForwardIterator
,
5089 typename _UniformRandomNumberGenerator
>
5091 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5092 _UniformRandomNumberGenerator
& __urng
,
5093 const param_type
& __p
)
5094 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5096 template<typename _UniformRandomNumberGenerator
>
5098 __generate(result_type
* __f
, result_type
* __t
,
5099 _UniformRandomNumberGenerator
& __urng
,
5100 const param_type
& __p
)
5101 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5104 * @brief Return true if two extreme value distributions have the same
5108 operator==(const extreme_value_distribution
& __d1
,
5109 const extreme_value_distribution
& __d2
)
5110 { return __d1
._M_param
== __d2
._M_param
; }
5113 template<typename _ForwardIterator
,
5114 typename _UniformRandomNumberGenerator
>
5116 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5117 _UniformRandomNumberGenerator
& __urng
,
5118 const param_type
& __p
);
5120 param_type _M_param
;
5124 * @brief Return true if two extreme value distributions have different
5127 template<typename _RealType
>
5129 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
5130 const std::extreme_value_distribution
<_RealType
>& __d2
)
5131 { return !(__d1
== __d2
); }
5134 * @brief Inserts a %extreme_value_distribution random number distribution
5135 * @p __x into the output stream @p __os.
5137 * @param __os An output stream.
5138 * @param __x A %extreme_value_distribution random number distribution.
5140 * @returns The output stream with the state of @p __x inserted or in
5143 template<typename _RealType
, typename _CharT
, typename _Traits
>
5144 std::basic_ostream
<_CharT
, _Traits
>&
5145 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5146 const std::extreme_value_distribution
<_RealType
>& __x
);
5149 * @brief Extracts a %extreme_value_distribution random number
5150 * distribution @p __x from the input stream @p __is.
5152 * @param __is An input stream.
5153 * @param __x A %extreme_value_distribution random number
5156 * @returns The input stream with @p __x extracted or in an error state.
5158 template<typename _RealType
, typename _CharT
, typename _Traits
>
5159 std::basic_istream
<_CharT
, _Traits
>&
5160 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5161 std::extreme_value_distribution
<_RealType
>& __x
);
5165 * @brief A discrete_distribution random number distribution.
5167 * The formula for the discrete probability mass function is
5170 template<typename _IntType
= int>
5171 class discrete_distribution
5173 static_assert(std::is_integral
<_IntType
>::value
,
5174 "result_type must be an integral type");
5177 /** The type of the range of the distribution. */
5178 typedef _IntType result_type
;
5180 /** Parameter type. */
5183 typedef discrete_distribution
<_IntType
> distribution_type
;
5184 friend class discrete_distribution
<_IntType
>;
5187 : _M_prob(), _M_cp()
5190 template<typename _InputIterator
>
5191 param_type(_InputIterator __wbegin
,
5192 _InputIterator __wend
)
5193 : _M_prob(__wbegin
, __wend
), _M_cp()
5194 { _M_initialize(); }
5196 param_type(initializer_list
<double> __wil
)
5197 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
5198 { _M_initialize(); }
5200 template<typename _Func
>
5201 param_type(size_t __nw
, double __xmin
, double __xmax
,
5204 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5205 param_type(const param_type
&) = default;
5206 param_type
& operator=(const param_type
&) = default;
5209 probabilities() const
5210 { return _M_prob
.empty() ? std::vector
<double>(1, 1.0) : _M_prob
; }
5213 operator==(const param_type
& __p1
, const param_type
& __p2
)
5214 { return __p1
._M_prob
== __p2
._M_prob
; }
5217 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5218 { return !(__p1
== __p2
); }
5224 std::vector
<double> _M_prob
;
5225 std::vector
<double> _M_cp
;
5228 discrete_distribution()
5232 template<typename _InputIterator
>
5233 discrete_distribution(_InputIterator __wbegin
,
5234 _InputIterator __wend
)
5235 : _M_param(__wbegin
, __wend
)
5238 discrete_distribution(initializer_list
<double> __wl
)
5242 template<typename _Func
>
5243 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
5245 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5249 discrete_distribution(const param_type
& __p
)
5254 * @brief Resets the distribution state.
5261 * @brief Returns the probabilities of the distribution.
5264 probabilities() const
5266 return _M_param
._M_prob
.empty()
5267 ? std::vector
<double>(1, 1.0) : _M_param
._M_prob
;
5271 * @brief Returns the parameter set of the distribution.
5275 { return _M_param
; }
5278 * @brief Sets the parameter set of the distribution.
5279 * @param __param The new parameter set of the distribution.
5282 param(const param_type
& __param
)
5283 { _M_param
= __param
; }
5286 * @brief Returns the greatest lower bound value of the distribution.
5290 { return result_type(0); }
5293 * @brief Returns the least upper bound value of the distribution.
5298 return _M_param
._M_prob
.empty()
5299 ? result_type(0) : result_type(_M_param
._M_prob
.size() - 1);
5303 * @brief Generating functions.
5305 template<typename _UniformRandomNumberGenerator
>
5307 operator()(_UniformRandomNumberGenerator
& __urng
)
5308 { return this->operator()(__urng
, _M_param
); }
5310 template<typename _UniformRandomNumberGenerator
>
5312 operator()(_UniformRandomNumberGenerator
& __urng
,
5313 const param_type
& __p
);
5315 template<typename _ForwardIterator
,
5316 typename _UniformRandomNumberGenerator
>
5318 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5319 _UniformRandomNumberGenerator
& __urng
)
5320 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5322 template<typename _ForwardIterator
,
5323 typename _UniformRandomNumberGenerator
>
5325 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5326 _UniformRandomNumberGenerator
& __urng
,
5327 const param_type
& __p
)
5328 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5330 template<typename _UniformRandomNumberGenerator
>
5332 __generate(result_type
* __f
, result_type
* __t
,
5333 _UniformRandomNumberGenerator
& __urng
,
5334 const param_type
& __p
)
5335 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5338 * @brief Return true if two discrete distributions have the same
5342 operator==(const discrete_distribution
& __d1
,
5343 const discrete_distribution
& __d2
)
5344 { return __d1
._M_param
== __d2
._M_param
; }
5347 * @brief Inserts a %discrete_distribution random number distribution
5348 * @p __x into the output stream @p __os.
5350 * @param __os An output stream.
5351 * @param __x A %discrete_distribution random number distribution.
5353 * @returns The output stream with the state of @p __x inserted or in
5356 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5357 friend std::basic_ostream
<_CharT
, _Traits
>&
5358 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5359 const std::discrete_distribution
<_IntType1
>& __x
);
5362 * @brief Extracts a %discrete_distribution random number distribution
5363 * @p __x from the input stream @p __is.
5365 * @param __is An input stream.
5366 * @param __x A %discrete_distribution random number
5369 * @returns The input stream with @p __x extracted or in an error
5372 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5373 friend std::basic_istream
<_CharT
, _Traits
>&
5374 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5375 std::discrete_distribution
<_IntType1
>& __x
);
5378 template<typename _ForwardIterator
,
5379 typename _UniformRandomNumberGenerator
>
5381 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5382 _UniformRandomNumberGenerator
& __urng
,
5383 const param_type
& __p
);
5385 param_type _M_param
;
5389 * @brief Return true if two discrete distributions have different
5392 template<typename _IntType
>
5394 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
5395 const std::discrete_distribution
<_IntType
>& __d2
)
5396 { return !(__d1
== __d2
); }
5400 * @brief A piecewise_constant_distribution random number distribution.
5402 * The formula for the piecewise constant probability mass function is
5405 template<typename _RealType
= double>
5406 class piecewise_constant_distribution
5408 static_assert(std::is_floating_point
<_RealType
>::value
,
5409 "result_type must be a floating point type");
5412 /** The type of the range of the distribution. */
5413 typedef _RealType result_type
;
5415 /** Parameter type. */
5418 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
5419 friend class piecewise_constant_distribution
<_RealType
>;
5422 : _M_int(), _M_den(), _M_cp()
5425 template<typename _InputIteratorB
, typename _InputIteratorW
>
5426 param_type(_InputIteratorB __bfirst
,
5427 _InputIteratorB __bend
,
5428 _InputIteratorW __wbegin
);
5430 template<typename _Func
>
5431 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
5433 template<typename _Func
>
5434 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5437 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5438 param_type(const param_type
&) = default;
5439 param_type
& operator=(const param_type
&) = default;
5441 std::vector
<_RealType
>
5446 std::vector
<_RealType
> __tmp(2);
5447 __tmp
[1] = _RealType(1);
5456 { return _M_den
.empty() ? std::vector
<double>(1, 1.0) : _M_den
; }
5459 operator==(const param_type
& __p1
, const param_type
& __p2
)
5460 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
5463 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5464 { return !(__p1
== __p2
); }
5470 std::vector
<_RealType
> _M_int
;
5471 std::vector
<double> _M_den
;
5472 std::vector
<double> _M_cp
;
5476 piecewise_constant_distribution()
5480 template<typename _InputIteratorB
, typename _InputIteratorW
>
5481 piecewise_constant_distribution(_InputIteratorB __bfirst
,
5482 _InputIteratorB __bend
,
5483 _InputIteratorW __wbegin
)
5484 : _M_param(__bfirst
, __bend
, __wbegin
)
5487 template<typename _Func
>
5488 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
5490 : _M_param(__bl
, __fw
)
5493 template<typename _Func
>
5494 piecewise_constant_distribution(size_t __nw
,
5495 _RealType __xmin
, _RealType __xmax
,
5497 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5501 piecewise_constant_distribution(const param_type
& __p
)
5506 * @brief Resets the distribution state.
5513 * @brief Returns a vector of the intervals.
5515 std::vector
<_RealType
>
5518 if (_M_param
._M_int
.empty())
5520 std::vector
<_RealType
> __tmp(2);
5521 __tmp
[1] = _RealType(1);
5525 return _M_param
._M_int
;
5529 * @brief Returns a vector of the probability densities.
5534 return _M_param
._M_den
.empty()
5535 ? std::vector
<double>(1, 1.0) : _M_param
._M_den
;
5539 * @brief Returns the parameter set of the distribution.
5543 { return _M_param
; }
5546 * @brief Sets the parameter set of the distribution.
5547 * @param __param The new parameter set of the distribution.
5550 param(const param_type
& __param
)
5551 { _M_param
= __param
; }
5554 * @brief Returns the greatest lower bound value of the distribution.
5559 return _M_param
._M_int
.empty()
5560 ? result_type(0) : _M_param
._M_int
.front();
5564 * @brief Returns the least upper bound value of the distribution.
5569 return _M_param
._M_int
.empty()
5570 ? result_type(1) : _M_param
._M_int
.back();
5574 * @brief Generating functions.
5576 template<typename _UniformRandomNumberGenerator
>
5578 operator()(_UniformRandomNumberGenerator
& __urng
)
5579 { return this->operator()(__urng
, _M_param
); }
5581 template<typename _UniformRandomNumberGenerator
>
5583 operator()(_UniformRandomNumberGenerator
& __urng
,
5584 const param_type
& __p
);
5586 template<typename _ForwardIterator
,
5587 typename _UniformRandomNumberGenerator
>
5589 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5590 _UniformRandomNumberGenerator
& __urng
)
5591 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5593 template<typename _ForwardIterator
,
5594 typename _UniformRandomNumberGenerator
>
5596 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5597 _UniformRandomNumberGenerator
& __urng
,
5598 const param_type
& __p
)
5599 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5601 template<typename _UniformRandomNumberGenerator
>
5603 __generate(result_type
* __f
, result_type
* __t
,
5604 _UniformRandomNumberGenerator
& __urng
,
5605 const param_type
& __p
)
5606 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5609 * @brief Return true if two piecewise constant distributions have the
5613 operator==(const piecewise_constant_distribution
& __d1
,
5614 const piecewise_constant_distribution
& __d2
)
5615 { return __d1
._M_param
== __d2
._M_param
; }
5618 * @brief Inserts a %piecewise_constant_distribution random
5619 * number distribution @p __x into the output stream @p __os.
5621 * @param __os An output stream.
5622 * @param __x A %piecewise_constant_distribution random number
5625 * @returns The output stream with the state of @p __x inserted or in
5628 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5629 friend std::basic_ostream
<_CharT
, _Traits
>&
5630 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5631 const std::piecewise_constant_distribution
<_RealType1
>& __x
);
5634 * @brief Extracts a %piecewise_constant_distribution random
5635 * number distribution @p __x from the input stream @p __is.
5637 * @param __is An input stream.
5638 * @param __x A %piecewise_constant_distribution random number
5641 * @returns The input stream with @p __x extracted or in an error
5644 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5645 friend std::basic_istream
<_CharT
, _Traits
>&
5646 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5647 std::piecewise_constant_distribution
<_RealType1
>& __x
);
5650 template<typename _ForwardIterator
,
5651 typename _UniformRandomNumberGenerator
>
5653 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5654 _UniformRandomNumberGenerator
& __urng
,
5655 const param_type
& __p
);
5657 param_type _M_param
;
5661 * @brief Return true if two piecewise constant distributions have
5662 * different parameters.
5664 template<typename _RealType
>
5666 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5667 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5668 { return !(__d1
== __d2
); }
5672 * @brief A piecewise_linear_distribution random number distribution.
5674 * The formula for the piecewise linear probability mass function is
5677 template<typename _RealType
= double>
5678 class piecewise_linear_distribution
5680 static_assert(std::is_floating_point
<_RealType
>::value
,
5681 "result_type must be a floating point type");
5684 /** The type of the range of the distribution. */
5685 typedef _RealType result_type
;
5687 /** Parameter type. */
5690 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5691 friend class piecewise_linear_distribution
<_RealType
>;
5694 : _M_int(), _M_den(), _M_cp(), _M_m()
5697 template<typename _InputIteratorB
, typename _InputIteratorW
>
5698 param_type(_InputIteratorB __bfirst
,
5699 _InputIteratorB __bend
,
5700 _InputIteratorW __wbegin
);
5702 template<typename _Func
>
5703 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5705 template<typename _Func
>
5706 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5709 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5710 param_type(const param_type
&) = default;
5711 param_type
& operator=(const param_type
&) = default;
5713 std::vector
<_RealType
>
5718 std::vector
<_RealType
> __tmp(2);
5719 __tmp
[1] = _RealType(1);
5728 { return _M_den
.empty() ? std::vector
<double>(2, 1.0) : _M_den
; }
5731 operator==(const param_type
& __p1
, const param_type
& __p2
)
5732 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
5735 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5736 { return !(__p1
== __p2
); }
5742 std::vector
<_RealType
> _M_int
;
5743 std::vector
<double> _M_den
;
5744 std::vector
<double> _M_cp
;
5745 std::vector
<double> _M_m
;
5749 piecewise_linear_distribution()
5753 template<typename _InputIteratorB
, typename _InputIteratorW
>
5754 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5755 _InputIteratorB __bend
,
5756 _InputIteratorW __wbegin
)
5757 : _M_param(__bfirst
, __bend
, __wbegin
)
5760 template<typename _Func
>
5761 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5763 : _M_param(__bl
, __fw
)
5766 template<typename _Func
>
5767 piecewise_linear_distribution(size_t __nw
,
5768 _RealType __xmin
, _RealType __xmax
,
5770 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5774 piecewise_linear_distribution(const param_type
& __p
)
5779 * Resets the distribution state.
5786 * @brief Return the intervals of the distribution.
5788 std::vector
<_RealType
>
5791 if (_M_param
._M_int
.empty())
5793 std::vector
<_RealType
> __tmp(2);
5794 __tmp
[1] = _RealType(1);
5798 return _M_param
._M_int
;
5802 * @brief Return a vector of the probability densities of the
5808 return _M_param
._M_den
.empty()
5809 ? std::vector
<double>(2, 1.0) : _M_param
._M_den
;
5813 * @brief Returns the parameter set of the distribution.
5817 { return _M_param
; }
5820 * @brief Sets the parameter set of the distribution.
5821 * @param __param The new parameter set of the distribution.
5824 param(const param_type
& __param
)
5825 { _M_param
= __param
; }
5828 * @brief Returns the greatest lower bound value of the distribution.
5833 return _M_param
._M_int
.empty()
5834 ? result_type(0) : _M_param
._M_int
.front();
5838 * @brief Returns the least upper bound value of the distribution.
5843 return _M_param
._M_int
.empty()
5844 ? result_type(1) : _M_param
._M_int
.back();
5848 * @brief Generating functions.
5850 template<typename _UniformRandomNumberGenerator
>
5852 operator()(_UniformRandomNumberGenerator
& __urng
)
5853 { return this->operator()(__urng
, _M_param
); }
5855 template<typename _UniformRandomNumberGenerator
>
5857 operator()(_UniformRandomNumberGenerator
& __urng
,
5858 const param_type
& __p
);
5860 template<typename _ForwardIterator
,
5861 typename _UniformRandomNumberGenerator
>
5863 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5864 _UniformRandomNumberGenerator
& __urng
)
5865 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5867 template<typename _ForwardIterator
,
5868 typename _UniformRandomNumberGenerator
>
5870 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5871 _UniformRandomNumberGenerator
& __urng
,
5872 const param_type
& __p
)
5873 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5875 template<typename _UniformRandomNumberGenerator
>
5877 __generate(result_type
* __f
, result_type
* __t
,
5878 _UniformRandomNumberGenerator
& __urng
,
5879 const param_type
& __p
)
5880 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5883 * @brief Return true if two piecewise linear distributions have the
5887 operator==(const piecewise_linear_distribution
& __d1
,
5888 const piecewise_linear_distribution
& __d2
)
5889 { return __d1
._M_param
== __d2
._M_param
; }
5892 * @brief Inserts a %piecewise_linear_distribution random number
5893 * distribution @p __x into the output stream @p __os.
5895 * @param __os An output stream.
5896 * @param __x A %piecewise_linear_distribution random number
5899 * @returns The output stream with the state of @p __x inserted or in
5902 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5903 friend std::basic_ostream
<_CharT
, _Traits
>&
5904 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5905 const std::piecewise_linear_distribution
<_RealType1
>& __x
);
5908 * @brief Extracts a %piecewise_linear_distribution random number
5909 * distribution @p __x from the input stream @p __is.
5911 * @param __is An input stream.
5912 * @param __x A %piecewise_linear_distribution random number
5915 * @returns The input stream with @p __x extracted or in an error
5918 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5919 friend std::basic_istream
<_CharT
, _Traits
>&
5920 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5921 std::piecewise_linear_distribution
<_RealType1
>& __x
);
5924 template<typename _ForwardIterator
,
5925 typename _UniformRandomNumberGenerator
>
5927 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5928 _UniformRandomNumberGenerator
& __urng
,
5929 const param_type
& __p
);
5931 param_type _M_param
;
5935 * @brief Return true if two piecewise linear distributions have
5936 * different parameters.
5938 template<typename _RealType
>
5940 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5941 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5942 { return !(__d1
== __d2
); }
5945 /* @} */ // group random_distributions_poisson
5947 /* @} */ // group random_distributions
5950 * @addtogroup random_utilities Random Number Utilities
5956 * @brief The seed_seq class generates sequences of seeds for random
5957 * number generators.
5962 /** The type of the seed vales. */
5963 typedef uint_least32_t result_type
;
5965 /** Default constructor. */
5970 template<typename _IntType
>
5971 seed_seq(std::initializer_list
<_IntType
> il
);
5973 template<typename _InputIterator
>
5974 seed_seq(_InputIterator __begin
, _InputIterator __end
);
5976 // generating functions
5977 template<typename _RandomAccessIterator
>
5979 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
5981 // property functions
5982 size_t size() const noexcept
5983 { return _M_v
.size(); }
5985 template<typename OutputIterator
>
5987 param(OutputIterator __dest
) const
5988 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
5990 // no copy functions
5991 seed_seq(const seed_seq
&) = delete;
5992 seed_seq
& operator=(const seed_seq
&) = delete;
5995 std::vector
<result_type
> _M_v
;
5998 /* @} */ // group random_utilities
6000 /* @} */ // group random
6002 _GLIBCXX_END_NAMESPACE_VERSION