1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{random}
36 namespace std
_GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 // [26.4] Random number generation
43 * @defgroup random Random Number Generation
46 * A facility for generating random numbers on selected distributions.
51 * @brief A function template for converting the output of a (integral)
52 * uniform random number generator to a floatng point result in the range
55 template<typename _RealType
, size_t __bits
,
56 typename _UniformRandomNumberGenerator
>
58 generate_canonical(_UniformRandomNumberGenerator
& __g
);
60 _GLIBCXX_END_NAMESPACE_VERSION
63 * Implementation-space details.
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
69 template<typename _UIntType
, size_t __w
,
70 bool = __w
< static_cast<size_t>
71 (std::numeric_limits
<_UIntType
>::digits
)>
73 { static const _UIntType __value
= 0; };
75 template<typename _UIntType
, size_t __w
>
76 struct _Shift
<_UIntType
, __w
, true>
77 { static const _UIntType __value
= _UIntType(1) << __w
; };
80 int __which
= ((__s
<= __CHAR_BIT__
* sizeof (int))
81 + (__s
<= __CHAR_BIT__
* sizeof (long))
82 + (__s
<= __CHAR_BIT__
* sizeof (long long))
83 /* assume long long no bigger than __int128 */
85 struct _Select_uint_least_t
87 static_assert(__which
< 0, /* needs to be dependent */
88 "sorry, would be too much trouble for a slow result");
92 struct _Select_uint_least_t
<__s
, 4>
93 { typedef unsigned int type
; };
96 struct _Select_uint_least_t
<__s
, 3>
97 { typedef unsigned long type
; };
100 struct _Select_uint_least_t
<__s
, 2>
101 { typedef unsigned long long type
; };
103 #ifdef _GLIBCXX_USE_INT128
105 struct _Select_uint_least_t
<__s
, 1>
106 { typedef unsigned __int128 type
; };
109 // Assume a != 0, a < m, c < m, x < m.
110 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
,
111 bool __big_enough
= (!(__m
& (__m
- 1))
112 || (_Tp(-1) - __c
) / __a
>= __m
- 1),
113 bool __schrage_ok
= __m
% __a
< __m
/ __a
>
116 typedef typename _Select_uint_least_t
<std::__lg(__a
)
117 + std::__lg(__m
) + 2>::type _Tp2
;
120 { return static_cast<_Tp
>((_Tp2(__a
) * __x
+ __c
) % __m
); }
124 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
>
125 struct _Mod
<_Tp
, __m
, __a
, __c
, false, true>
132 // - for m == 2^n or m == 0, unsigned integer overflow is safe.
133 // - a * (m - 1) + c fits in _Tp, there is no overflow.
134 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool __s
>
135 struct _Mod
<_Tp
, __m
, __a
, __c
, true, __s
>
140 _Tp __res
= __a
* __x
+ __c
;
147 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
150 { return _Mod
<_Tp
, __m
, __a
, __c
>::__calc(__x
); }
153 * An adaptor class for converting the output of any Generator into
154 * the input for a specific Distribution.
156 template<typename _Engine
, typename _DInputType
>
161 _Adaptor(_Engine
& __g
)
166 { return _DInputType(0); }
170 { return _DInputType(1); }
173 * Converts a value generated by the adapted random number generator
174 * into a value in the input domain for the dependent random number
180 return std::generate_canonical
<_DInputType
,
181 std::numeric_limits
<_DInputType
>::digits
,
189 _GLIBCXX_END_NAMESPACE_VERSION
190 } // namespace __detail
192 _GLIBCXX_BEGIN_NAMESPACE_VERSION
195 * @addtogroup random_generators Random Number Generators
198 * These classes define objects which provide random or pseudorandom
199 * numbers, either from a discrete or a continuous interval. The
200 * random number generator supplied as a part of this library are
201 * all uniform random number generators which provide a sequence of
202 * random number uniformly distributed over their range.
204 * A number generator is a function object with an operator() that
205 * takes zero arguments and returns a number.
207 * A compliant random number generator must satisfy the following
208 * requirements. <table border=1 cellpadding=10 cellspacing=0>
209 * <caption align=top>Random Number Generator Requirements</caption>
210 * <tr><td>To be documented.</td></tr> </table>
216 * @brief A model of a linear congruential random number generator.
218 * A random number generator that produces pseudorandom numbers via
221 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
224 * The template parameter @p _UIntType must be an unsigned integral type
225 * large enough to store values up to (__m-1). If the template parameter
226 * @p __m is 0, the modulus @p __m used is
227 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
228 * parameters @p __a and @p __c must be less than @p __m.
230 * The size of the state is @f$1@f$.
232 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
233 class linear_congruential_engine
235 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
236 "substituting _UIntType not an unsigned integral type");
237 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
238 "template argument substituting __m out of bounds");
241 /** The type of the generated random value. */
242 typedef _UIntType result_type
;
244 /** The multiplier. */
245 static constexpr result_type multiplier
= __a
;
247 static constexpr result_type increment
= __c
;
249 static constexpr result_type modulus
= __m
;
250 static constexpr result_type default_seed
= 1u;
253 * @brief Constructs a %linear_congruential_engine random number
254 * generator engine with seed @p __s. The default seed value
257 * @param __s The initial seed value.
260 linear_congruential_engine(result_type __s
= default_seed
)
264 * @brief Constructs a %linear_congruential_engine random number
265 * generator engine seeded from the seed sequence @p __q.
267 * @param __q the seed sequence.
269 template<typename _Sseq
, typename
= typename
270 std::enable_if
<!std::is_same
<_Sseq
, linear_congruential_engine
>::value
>
273 linear_congruential_engine(_Sseq
& __q
)
277 * @brief Reseeds the %linear_congruential_engine random number generator
278 * engine sequence to the seed @p __s.
280 * @param __s The new seed.
283 seed(result_type __s
= default_seed
);
286 * @brief Reseeds the %linear_congruential_engine random number generator
288 * sequence using values from the seed sequence @p __q.
290 * @param __q the seed sequence.
292 template<typename _Sseq
>
293 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
297 * @brief Gets the smallest possible value in the output range.
299 * The minimum depends on the @p __c parameter: if it is zero, the
300 * minimum generated must be > 0, otherwise 0 is allowed.
302 static constexpr result_type
304 { return __c
== 0u ? 1u : 0u; }
307 * @brief Gets the largest possible value in the output range.
309 static constexpr result_type
314 * @brief Discard a sequence of random numbers.
317 discard(unsigned long long __z
)
319 for (; __z
!= 0ULL; --__z
)
324 * @brief Gets the next random number in the sequence.
329 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
334 * @brief Compares two linear congruential random number generator
335 * objects of the same type for equality.
337 * @param __lhs A linear congruential random number generator object.
338 * @param __rhs Another linear congruential random number generator
341 * @returns true if the infinite sequences of generated values
342 * would be equal, false otherwise.
345 operator==(const linear_congruential_engine
& __lhs
,
346 const linear_congruential_engine
& __rhs
)
347 { return __lhs
._M_x
== __rhs
._M_x
; }
350 * @brief Writes the textual representation of the state x(i) of x to
353 * @param __os The output stream.
354 * @param __lcr A % linear_congruential_engine random number generator.
357 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
358 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
359 friend std::basic_ostream
<_CharT
, _Traits
>&
360 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
361 const std::linear_congruential_engine
<_UIntType1
,
362 __a1
, __c1
, __m1
>& __lcr
);
365 * @brief Sets the state of the engine by reading its textual
366 * representation from @p __is.
368 * The textual representation must have been previously written using
369 * an output stream whose imbued locale and whose type's template
370 * specialization arguments _CharT and _Traits were the same as those
373 * @param __is The input stream.
374 * @param __lcr A % linear_congruential_engine random number generator.
377 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
378 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
379 friend std::basic_istream
<_CharT
, _Traits
>&
380 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
381 std::linear_congruential_engine
<_UIntType1
, __a1
,
389 * @brief Compares two linear congruential random number generator
390 * objects of the same type for inequality.
392 * @param __lhs A linear congruential random number generator object.
393 * @param __rhs Another linear congruential random number generator
396 * @returns true if the infinite sequences of generated values
397 * would be different, false otherwise.
399 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
401 operator!=(const std::linear_congruential_engine
<_UIntType
, __a
,
403 const std::linear_congruential_engine
<_UIntType
, __a
,
405 { return !(__lhs
== __rhs
); }
409 * A generalized feedback shift register discrete random number generator.
411 * This algorithm avoids multiplication and division and is designed to be
412 * friendly to a pipelined architecture. If the parameters are chosen
413 * correctly, this generator will produce numbers with a very long period and
414 * fairly good apparent entropy, although still not cryptographically strong.
416 * The best way to use this generator is with the predefined mt19937 class.
418 * This algorithm was originally invented by Makoto Matsumoto and
421 * @tparam __w Word size, the number of bits in each element of
423 * @tparam __n The degree of recursion.
424 * @tparam __m The period parameter.
425 * @tparam __r The separation point bit index.
426 * @tparam __a The last row of the twist matrix.
427 * @tparam __u The first right-shift tempering matrix parameter.
428 * @tparam __d The first right-shift tempering matrix mask.
429 * @tparam __s The first left-shift tempering matrix parameter.
430 * @tparam __b The first left-shift tempering matrix mask.
431 * @tparam __t The second left-shift tempering matrix parameter.
432 * @tparam __c The second left-shift tempering matrix mask.
433 * @tparam __l The second right-shift tempering matrix parameter.
434 * @tparam __f Initialization multiplier.
436 template<typename _UIntType
, size_t __w
,
437 size_t __n
, size_t __m
, size_t __r
,
438 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
439 _UIntType __b
, size_t __t
,
440 _UIntType __c
, size_t __l
, _UIntType __f
>
441 class mersenne_twister_engine
443 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
444 "substituting _UIntType not an unsigned integral type");
445 static_assert(1u <= __m
&& __m
<= __n
,
446 "template argument substituting __m out of bounds");
447 static_assert(__r
<= __w
, "template argument substituting "
449 static_assert(__u
<= __w
, "template argument substituting "
451 static_assert(__s
<= __w
, "template argument substituting "
453 static_assert(__t
<= __w
, "template argument substituting "
455 static_assert(__l
<= __w
, "template argument substituting "
457 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
458 "template argument substituting __w out of bound");
459 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
460 "template argument substituting __a out of bound");
461 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
462 "template argument substituting __b out of bound");
463 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
464 "template argument substituting __c out of bound");
465 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
466 "template argument substituting __d out of bound");
467 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
468 "template argument substituting __f out of bound");
471 /** The type of the generated random value. */
472 typedef _UIntType result_type
;
475 static constexpr size_t word_size
= __w
;
476 static constexpr size_t state_size
= __n
;
477 static constexpr size_t shift_size
= __m
;
478 static constexpr size_t mask_bits
= __r
;
479 static constexpr result_type xor_mask
= __a
;
480 static constexpr size_t tempering_u
= __u
;
481 static constexpr result_type tempering_d
= __d
;
482 static constexpr size_t tempering_s
= __s
;
483 static constexpr result_type tempering_b
= __b
;
484 static constexpr size_t tempering_t
= __t
;
485 static constexpr result_type tempering_c
= __c
;
486 static constexpr size_t tempering_l
= __l
;
487 static constexpr result_type initialization_multiplier
= __f
;
488 static constexpr result_type default_seed
= 5489u;
490 // constructors and member function
492 mersenne_twister_engine(result_type __sd
= default_seed
)
496 * @brief Constructs a %mersenne_twister_engine random number generator
497 * engine seeded from the seed sequence @p __q.
499 * @param __q the seed sequence.
501 template<typename _Sseq
, typename
= typename
502 std::enable_if
<!std::is_same
<_Sseq
, mersenne_twister_engine
>::value
>
505 mersenne_twister_engine(_Sseq
& __q
)
509 seed(result_type __sd
= default_seed
);
511 template<typename _Sseq
>
512 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
516 * @brief Gets the smallest possible value in the output range.
518 static constexpr result_type
523 * @brief Gets the largest possible value in the output range.
525 static constexpr result_type
527 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
530 * @brief Discard a sequence of random numbers.
533 discard(unsigned long long __z
);
539 * @brief Compares two % mersenne_twister_engine random number generator
540 * objects of the same type for equality.
542 * @param __lhs A % mersenne_twister_engine random number generator
544 * @param __rhs Another % mersenne_twister_engine random number
547 * @returns true if the infinite sequences of generated values
548 * would be equal, false otherwise.
551 operator==(const mersenne_twister_engine
& __lhs
,
552 const mersenne_twister_engine
& __rhs
)
553 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
)
554 && __lhs
._M_p
== __rhs
._M_p
); }
557 * @brief Inserts the current state of a % mersenne_twister_engine
558 * random number generator engine @p __x into the output stream
561 * @param __os An output stream.
562 * @param __x A % mersenne_twister_engine random number generator
565 * @returns The output stream with the state of @p __x inserted or in
568 template<typename _UIntType1
,
569 size_t __w1
, size_t __n1
,
570 size_t __m1
, size_t __r1
,
571 _UIntType1 __a1
, size_t __u1
,
572 _UIntType1 __d1
, size_t __s1
,
573 _UIntType1 __b1
, size_t __t1
,
574 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
575 typename _CharT
, typename _Traits
>
576 friend std::basic_ostream
<_CharT
, _Traits
>&
577 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
578 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
579 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
583 * @brief Extracts the current state of a % mersenne_twister_engine
584 * random number generator engine @p __x from the input stream
587 * @param __is An input stream.
588 * @param __x A % mersenne_twister_engine random number generator
591 * @returns The input stream with the state of @p __x extracted or in
594 template<typename _UIntType1
,
595 size_t __w1
, size_t __n1
,
596 size_t __m1
, size_t __r1
,
597 _UIntType1 __a1
, size_t __u1
,
598 _UIntType1 __d1
, size_t __s1
,
599 _UIntType1 __b1
, size_t __t1
,
600 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
601 typename _CharT
, typename _Traits
>
602 friend std::basic_istream
<_CharT
, _Traits
>&
603 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
604 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
605 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
611 _UIntType _M_x
[state_size
];
616 * @brief Compares two % mersenne_twister_engine random number generator
617 * objects of the same type for inequality.
619 * @param __lhs A % mersenne_twister_engine random number generator
621 * @param __rhs Another % mersenne_twister_engine random number
624 * @returns true if the infinite sequences of generated values
625 * would be different, false otherwise.
627 template<typename _UIntType
, size_t __w
,
628 size_t __n
, size_t __m
, size_t __r
,
629 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
630 _UIntType __b
, size_t __t
,
631 _UIntType __c
, size_t __l
, _UIntType __f
>
633 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
634 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
635 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
636 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
637 { return !(__lhs
== __rhs
); }
641 * @brief The Marsaglia-Zaman generator.
643 * This is a model of a Generalized Fibonacci discrete random number
644 * generator, sometimes referred to as the SWC generator.
646 * A discrete random number generator that produces pseudorandom
649 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
652 * The size of the state is @f$r@f$
653 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
655 * @var _M_x The state of the generator. This is a ring buffer.
656 * @var _M_carry The carry.
657 * @var _M_p Current index of x(i - r).
659 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
660 class subtract_with_carry_engine
662 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
663 "substituting _UIntType not an unsigned integral type");
664 static_assert(0u < __s
&& __s
< __r
,
665 "template argument substituting __s out of bounds");
666 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
667 "template argument substituting __w out of bounds");
670 /** The type of the generated random value. */
671 typedef _UIntType result_type
;
674 static constexpr size_t word_size
= __w
;
675 static constexpr size_t short_lag
= __s
;
676 static constexpr size_t long_lag
= __r
;
677 static constexpr result_type default_seed
= 19780503u;
680 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
681 * random number generator.
684 subtract_with_carry_engine(result_type __sd
= default_seed
)
688 * @brief Constructs a %subtract_with_carry_engine random number engine
689 * seeded from the seed sequence @p __q.
691 * @param __q the seed sequence.
693 template<typename _Sseq
, typename
= typename
694 std::enable_if
<!std::is_same
<_Sseq
, subtract_with_carry_engine
>::value
>
697 subtract_with_carry_engine(_Sseq
& __q
)
701 * @brief Seeds the initial state @f$x_0@f$ of the random number
704 * N1688[4.19] modifies this as follows. If @p __value == 0,
705 * sets value to 19780503. In any case, with a linear
706 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
707 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
708 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
709 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
710 * set carry to 1, otherwise sets carry to 0.
713 seed(result_type __sd
= default_seed
);
716 * @brief Seeds the initial state @f$x_0@f$ of the
717 * % subtract_with_carry_engine random number generator.
719 template<typename _Sseq
>
720 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
724 * @brief Gets the inclusive minimum value of the range of random
725 * integers returned by this generator.
727 static constexpr result_type
732 * @brief Gets the inclusive maximum value of the range of random
733 * integers returned by this generator.
735 static constexpr result_type
737 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
740 * @brief Discard a sequence of random numbers.
743 discard(unsigned long long __z
)
745 for (; __z
!= 0ULL; --__z
)
750 * @brief Gets the next random number in the sequence.
756 * @brief Compares two % subtract_with_carry_engine random number
757 * generator objects of the same type for equality.
759 * @param __lhs A % subtract_with_carry_engine random number generator
761 * @param __rhs Another % subtract_with_carry_engine random number
764 * @returns true if the infinite sequences of generated values
765 * would be equal, false otherwise.
768 operator==(const subtract_with_carry_engine
& __lhs
,
769 const subtract_with_carry_engine
& __rhs
)
770 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
)
771 && __lhs
._M_carry
== __rhs
._M_carry
772 && __lhs
._M_p
== __rhs
._M_p
); }
775 * @brief Inserts the current state of a % subtract_with_carry_engine
776 * random number generator engine @p __x into the output stream
779 * @param __os An output stream.
780 * @param __x A % subtract_with_carry_engine random number generator
783 * @returns The output stream with the state of @p __x inserted or in
786 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
787 typename _CharT
, typename _Traits
>
788 friend std::basic_ostream
<_CharT
, _Traits
>&
789 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
790 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
794 * @brief Extracts the current state of a % subtract_with_carry_engine
795 * random number generator engine @p __x from the input stream
798 * @param __is An input stream.
799 * @param __x A % subtract_with_carry_engine random number generator
802 * @returns The input stream with the state of @p __x extracted or in
805 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
806 typename _CharT
, typename _Traits
>
807 friend std::basic_istream
<_CharT
, _Traits
>&
808 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
809 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
813 _UIntType _M_x
[long_lag
];
819 * @brief Compares two % subtract_with_carry_engine random number
820 * generator objects of the same type for inequality.
822 * @param __lhs A % subtract_with_carry_engine random number generator
824 * @param __rhs Another % subtract_with_carry_engine random number
827 * @returns true if the infinite sequences of generated values
828 * would be different, false otherwise.
830 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
832 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
834 const std::subtract_with_carry_engine
<_UIntType
, __w
,
836 { return !(__lhs
== __rhs
); }
840 * Produces random numbers from some base engine by discarding blocks of
843 * 0 <= @p __r <= @p __p
845 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
846 class discard_block_engine
848 static_assert(1 <= __r
&& __r
<= __p
,
849 "template argument substituting __r out of bounds");
852 /** The type of the generated random value. */
853 typedef typename
_RandomNumberEngine::result_type result_type
;
856 static constexpr size_t block_size
= __p
;
857 static constexpr size_t used_block
= __r
;
860 * @brief Constructs a default %discard_block_engine engine.
862 * The underlying engine is default constructed as well.
864 discard_block_engine()
865 : _M_b(), _M_n(0) { }
868 * @brief Copy constructs a %discard_block_engine engine.
870 * Copies an existing base class random number generator.
871 * @param __rng An existing (base class) engine object.
874 discard_block_engine(const _RandomNumberEngine
& __rng
)
875 : _M_b(__rng
), _M_n(0) { }
878 * @brief Move constructs a %discard_block_engine engine.
880 * Copies an existing base class random number generator.
881 * @param __rng An existing (base class) engine object.
884 discard_block_engine(_RandomNumberEngine
&& __rng
)
885 : _M_b(std::move(__rng
)), _M_n(0) { }
888 * @brief Seed constructs a %discard_block_engine engine.
890 * Constructs the underlying generator engine seeded with @p __s.
891 * @param __s A seed value for the base class engine.
894 discard_block_engine(result_type __s
)
895 : _M_b(__s
), _M_n(0) { }
898 * @brief Generator construct a %discard_block_engine engine.
900 * @param __q A seed sequence.
902 template<typename _Sseq
, typename
= typename
903 std::enable_if
<!std::is_same
<_Sseq
, discard_block_engine
>::value
904 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
907 discard_block_engine(_Sseq
& __q
)
912 * @brief Reseeds the %discard_block_engine object with the default
913 * seed for the underlying base class generator engine.
923 * @brief Reseeds the %discard_block_engine object with the default
924 * seed for the underlying base class generator engine.
927 seed(result_type __s
)
934 * @brief Reseeds the %discard_block_engine object with the given seed
936 * @param __q A seed generator function.
938 template<typename _Sseq
>
947 * @brief Gets a const reference to the underlying generator engine
950 const _RandomNumberEngine
&
951 base() const noexcept
955 * @brief Gets the minimum value in the generated random number range.
957 static constexpr result_type
959 { return _RandomNumberEngine::min(); }
962 * @brief Gets the maximum value in the generated random number range.
964 static constexpr result_type
966 { return _RandomNumberEngine::max(); }
969 * @brief Discard a sequence of random numbers.
972 discard(unsigned long long __z
)
974 for (; __z
!= 0ULL; --__z
)
979 * @brief Gets the next value in the generated random number sequence.
985 * @brief Compares two %discard_block_engine random number generator
986 * objects of the same type for equality.
988 * @param __lhs A %discard_block_engine random number generator object.
989 * @param __rhs Another %discard_block_engine random number generator
992 * @returns true if the infinite sequences of generated values
993 * would be equal, false otherwise.
996 operator==(const discard_block_engine
& __lhs
,
997 const discard_block_engine
& __rhs
)
998 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
1001 * @brief Inserts the current state of a %discard_block_engine random
1002 * number generator engine @p __x into the output stream
1005 * @param __os An output stream.
1006 * @param __x A %discard_block_engine random number generator engine.
1008 * @returns The output stream with the state of @p __x inserted or in
1011 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1012 typename _CharT
, typename _Traits
>
1013 friend std::basic_ostream
<_CharT
, _Traits
>&
1014 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1015 const std::discard_block_engine
<_RandomNumberEngine1
,
1019 * @brief Extracts the current state of a % subtract_with_carry_engine
1020 * random number generator engine @p __x from the input stream
1023 * @param __is An input stream.
1024 * @param __x A %discard_block_engine random number generator engine.
1026 * @returns The input stream with the state of @p __x extracted or in
1029 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1030 typename _CharT
, typename _Traits
>
1031 friend std::basic_istream
<_CharT
, _Traits
>&
1032 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1033 std::discard_block_engine
<_RandomNumberEngine1
,
1037 _RandomNumberEngine _M_b
;
1042 * @brief Compares two %discard_block_engine random number generator
1043 * objects of the same type for inequality.
1045 * @param __lhs A %discard_block_engine random number generator object.
1046 * @param __rhs Another %discard_block_engine random number generator
1049 * @returns true if the infinite sequences of generated values
1050 * would be different, false otherwise.
1052 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
1054 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1056 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1058 { return !(__lhs
== __rhs
); }
1062 * Produces random numbers by combining random numbers from some base
1063 * engine to produce random numbers with a specifies number of bits @p __w.
1065 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1066 class independent_bits_engine
1068 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
1069 "substituting _UIntType not an unsigned integral type");
1070 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
1071 "template argument substituting __w out of bounds");
1074 /** The type of the generated random value. */
1075 typedef _UIntType result_type
;
1078 * @brief Constructs a default %independent_bits_engine engine.
1080 * The underlying engine is default constructed as well.
1082 independent_bits_engine()
1086 * @brief Copy constructs a %independent_bits_engine engine.
1088 * Copies an existing base class random number generator.
1089 * @param __rng An existing (base class) engine object.
1092 independent_bits_engine(const _RandomNumberEngine
& __rng
)
1096 * @brief Move constructs a %independent_bits_engine engine.
1098 * Copies an existing base class random number generator.
1099 * @param __rng An existing (base class) engine object.
1102 independent_bits_engine(_RandomNumberEngine
&& __rng
)
1103 : _M_b(std::move(__rng
)) { }
1106 * @brief Seed constructs a %independent_bits_engine engine.
1108 * Constructs the underlying generator engine seeded with @p __s.
1109 * @param __s A seed value for the base class engine.
1112 independent_bits_engine(result_type __s
)
1116 * @brief Generator construct a %independent_bits_engine engine.
1118 * @param __q A seed sequence.
1120 template<typename _Sseq
, typename
= typename
1121 std::enable_if
<!std::is_same
<_Sseq
, independent_bits_engine
>::value
1122 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1125 independent_bits_engine(_Sseq
& __q
)
1130 * @brief Reseeds the %independent_bits_engine object with the default
1131 * seed for the underlying base class generator engine.
1138 * @brief Reseeds the %independent_bits_engine object with the default
1139 * seed for the underlying base class generator engine.
1142 seed(result_type __s
)
1146 * @brief Reseeds the %independent_bits_engine object with the given
1148 * @param __q A seed generator function.
1150 template<typename _Sseq
>
1156 * @brief Gets a const reference to the underlying generator engine
1159 const _RandomNumberEngine
&
1160 base() const noexcept
1164 * @brief Gets the minimum value in the generated random number range.
1166 static constexpr result_type
1171 * @brief Gets the maximum value in the generated random number range.
1173 static constexpr result_type
1175 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1178 * @brief Discard a sequence of random numbers.
1181 discard(unsigned long long __z
)
1183 for (; __z
!= 0ULL; --__z
)
1188 * @brief Gets the next value in the generated random number sequence.
1194 * @brief Compares two %independent_bits_engine random number generator
1195 * objects of the same type for equality.
1197 * @param __lhs A %independent_bits_engine random number generator
1199 * @param __rhs Another %independent_bits_engine random number generator
1202 * @returns true if the infinite sequences of generated values
1203 * would be equal, false otherwise.
1206 operator==(const independent_bits_engine
& __lhs
,
1207 const independent_bits_engine
& __rhs
)
1208 { return __lhs
._M_b
== __rhs
._M_b
; }
1211 * @brief Extracts the current state of a % subtract_with_carry_engine
1212 * random number generator engine @p __x from the input stream
1215 * @param __is An input stream.
1216 * @param __x A %independent_bits_engine random number generator
1219 * @returns The input stream with the state of @p __x extracted or in
1222 template<typename _CharT
, typename _Traits
>
1223 friend std::basic_istream
<_CharT
, _Traits
>&
1224 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1225 std::independent_bits_engine
<_RandomNumberEngine
,
1226 __w
, _UIntType
>& __x
)
1233 _RandomNumberEngine _M_b
;
1237 * @brief Compares two %independent_bits_engine random number generator
1238 * objects of the same type for inequality.
1240 * @param __lhs A %independent_bits_engine random number generator
1242 * @param __rhs Another %independent_bits_engine random number generator
1245 * @returns true if the infinite sequences of generated values
1246 * would be different, false otherwise.
1248 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1250 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1252 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1254 { return !(__lhs
== __rhs
); }
1257 * @brief Inserts the current state of a %independent_bits_engine random
1258 * number generator engine @p __x into the output stream @p __os.
1260 * @param __os An output stream.
1261 * @param __x A %independent_bits_engine random number generator engine.
1263 * @returns The output stream with the state of @p __x inserted or in
1266 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1267 typename _CharT
, typename _Traits
>
1268 std::basic_ostream
<_CharT
, _Traits
>&
1269 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1270 const std::independent_bits_engine
<_RandomNumberEngine
,
1271 __w
, _UIntType
>& __x
)
1279 * @brief Produces random numbers by combining random numbers from some
1280 * base engine to produce random numbers with a specifies number of bits
1283 template<typename _RandomNumberEngine
, size_t __k
>
1284 class shuffle_order_engine
1286 static_assert(1u <= __k
, "template argument substituting "
1287 "__k out of bound");
1290 /** The type of the generated random value. */
1291 typedef typename
_RandomNumberEngine::result_type result_type
;
1293 static constexpr size_t table_size
= __k
;
1296 * @brief Constructs a default %shuffle_order_engine engine.
1298 * The underlying engine is default constructed as well.
1300 shuffle_order_engine()
1302 { _M_initialize(); }
1305 * @brief Copy constructs a %shuffle_order_engine engine.
1307 * Copies an existing base class random number generator.
1308 * @param __rng An existing (base class) engine object.
1311 shuffle_order_engine(const _RandomNumberEngine
& __rng
)
1313 { _M_initialize(); }
1316 * @brief Move constructs a %shuffle_order_engine engine.
1318 * Copies an existing base class random number generator.
1319 * @param __rng An existing (base class) engine object.
1322 shuffle_order_engine(_RandomNumberEngine
&& __rng
)
1323 : _M_b(std::move(__rng
))
1324 { _M_initialize(); }
1327 * @brief Seed constructs a %shuffle_order_engine engine.
1329 * Constructs the underlying generator engine seeded with @p __s.
1330 * @param __s A seed value for the base class engine.
1333 shuffle_order_engine(result_type __s
)
1335 { _M_initialize(); }
1338 * @brief Generator construct a %shuffle_order_engine engine.
1340 * @param __q A seed sequence.
1342 template<typename _Sseq
, typename
= typename
1343 std::enable_if
<!std::is_same
<_Sseq
, shuffle_order_engine
>::value
1344 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1347 shuffle_order_engine(_Sseq
& __q
)
1349 { _M_initialize(); }
1352 * @brief Reseeds the %shuffle_order_engine object with the default seed
1353 for the underlying base class generator engine.
1363 * @brief Reseeds the %shuffle_order_engine object with the default seed
1364 * for the underlying base class generator engine.
1367 seed(result_type __s
)
1374 * @brief Reseeds the %shuffle_order_engine object with the given seed
1376 * @param __q A seed generator function.
1378 template<typename _Sseq
>
1387 * Gets a const reference to the underlying generator engine object.
1389 const _RandomNumberEngine
&
1390 base() const noexcept
1394 * Gets the minimum value in the generated random number range.
1396 static constexpr result_type
1398 { return _RandomNumberEngine::min(); }
1401 * Gets the maximum value in the generated random number range.
1403 static constexpr result_type
1405 { return _RandomNumberEngine::max(); }
1408 * Discard a sequence of random numbers.
1411 discard(unsigned long long __z
)
1413 for (; __z
!= 0ULL; --__z
)
1418 * Gets the next value in the generated random number sequence.
1424 * Compares two %shuffle_order_engine random number generator objects
1425 * of the same type for equality.
1427 * @param __lhs A %shuffle_order_engine random number generator object.
1428 * @param __rhs Another %shuffle_order_engine random number generator
1431 * @returns true if the infinite sequences of generated values
1432 * would be equal, false otherwise.
1435 operator==(const shuffle_order_engine
& __lhs
,
1436 const shuffle_order_engine
& __rhs
)
1437 { return (__lhs
._M_b
== __rhs
._M_b
1438 && std::equal(__lhs
._M_v
, __lhs
._M_v
+ __k
, __rhs
._M_v
)
1439 && __lhs
._M_y
== __rhs
._M_y
); }
1442 * @brief Inserts the current state of a %shuffle_order_engine random
1443 * number generator engine @p __x into the output stream
1446 * @param __os An output stream.
1447 * @param __x A %shuffle_order_engine random number generator engine.
1449 * @returns The output stream with the state of @p __x inserted or in
1452 template<typename _RandomNumberEngine1
, size_t __k1
,
1453 typename _CharT
, typename _Traits
>
1454 friend std::basic_ostream
<_CharT
, _Traits
>&
1455 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1456 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1460 * @brief Extracts the current state of a % subtract_with_carry_engine
1461 * random number generator engine @p __x from the input stream
1464 * @param __is An input stream.
1465 * @param __x A %shuffle_order_engine random number generator engine.
1467 * @returns The input stream with the state of @p __x extracted or in
1470 template<typename _RandomNumberEngine1
, size_t __k1
,
1471 typename _CharT
, typename _Traits
>
1472 friend std::basic_istream
<_CharT
, _Traits
>&
1473 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1474 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>& __x
);
1477 void _M_initialize()
1479 for (size_t __i
= 0; __i
< __k
; ++__i
)
1484 _RandomNumberEngine _M_b
;
1485 result_type _M_v
[__k
];
1490 * Compares two %shuffle_order_engine random number generator objects
1491 * of the same type for inequality.
1493 * @param __lhs A %shuffle_order_engine random number generator object.
1494 * @param __rhs Another %shuffle_order_engine random number generator
1497 * @returns true if the infinite sequences of generated values
1498 * would be different, false otherwise.
1500 template<typename _RandomNumberEngine
, size_t __k
>
1502 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1504 const std::shuffle_order_engine
<_RandomNumberEngine
,
1506 { return !(__lhs
== __rhs
); }
1510 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1512 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1516 * An alternative LCR (Lehmer Generator function).
1518 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1522 * The classic Mersenne Twister.
1525 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1526 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1527 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1529 typedef mersenne_twister_engine
<
1535 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1538 * An alternative Mersenne Twister.
1540 typedef mersenne_twister_engine
<
1543 0xb5026f5aa96619e9ULL
, 29,
1544 0x5555555555555555ULL
, 17,
1545 0x71d67fffeda60000ULL
, 37,
1546 0xfff7eee000000000ULL
, 43,
1547 6364136223846793005ULL> mt19937_64
;
1549 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1552 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1555 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1557 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1559 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1561 typedef minstd_rand0 default_random_engine
;
1564 * A standard interface to a platform-specific non-deterministic
1565 * random number generator (if any are available).
1570 /** The type of the generated random value. */
1571 typedef unsigned int result_type
;
1573 // constructors, destructors and member functions
1575 #ifdef _GLIBCXX_USE_RANDOM_TR1
1578 random_device(const std::string
& __token
= "/dev/urandom")
1580 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1581 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1582 std::__throw_runtime_error(__N("random_device::"
1583 "random_device(const std::string&)"));
1587 { std::fclose(_M_file
); }
1592 random_device(const std::string
& __token
= "mt19937")
1593 : _M_mt(_M_strtoul(__token
)) { }
1596 static unsigned long
1597 _M_strtoul(const std::string
& __str
)
1599 unsigned long __ret
= 5489UL;
1600 if (__str
!= "mt19937")
1602 const char* __nptr
= __str
.c_str();
1604 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1605 if (*__nptr
== '\0' || *__endptr
!= '\0')
1606 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1607 "(const std::string&)"));
1616 static constexpr result_type
1618 { return std::numeric_limits
<result_type
>::min(); }
1620 static constexpr result_type
1622 { return std::numeric_limits
<result_type
>::max(); }
1625 entropy() const noexcept
1631 #ifdef _GLIBCXX_USE_RANDOM_TR1
1633 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1641 // No copy functions.
1642 random_device(const random_device
&) = delete;
1643 void operator=(const random_device
&) = delete;
1647 #ifdef _GLIBCXX_USE_RANDOM_TR1
1654 /* @} */ // group random_generators
1657 * @addtogroup random_distributions Random Number Distributions
1663 * @addtogroup random_distributions_uniform Uniform Distributions
1664 * @ingroup random_distributions
1669 * @brief Uniform discrete distribution for random numbers.
1670 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1671 * probability throughout the range.
1673 template<typename _IntType
= int>
1674 class uniform_int_distribution
1676 static_assert(std::is_integral
<_IntType
>::value
,
1677 "template argument not an integral type");
1680 /** The type of the range of the distribution. */
1681 typedef _IntType result_type
;
1682 /** Parameter type. */
1685 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1688 param_type(_IntType __a
= 0,
1689 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1690 : _M_a(__a
), _M_b(__b
)
1692 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1704 operator==(const param_type
& __p1
, const param_type
& __p2
)
1705 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1714 * @brief Constructs a uniform distribution object.
1717 uniform_int_distribution(_IntType __a
= 0,
1718 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1719 : _M_param(__a
, __b
)
1723 uniform_int_distribution(const param_type
& __p
)
1728 * @brief Resets the distribution state.
1730 * Does nothing for the uniform integer distribution.
1737 { return _M_param
.a(); }
1741 { return _M_param
.b(); }
1744 * @brief Returns the parameter set of the distribution.
1748 { return _M_param
; }
1751 * @brief Sets the parameter set of the distribution.
1752 * @param __param The new parameter set of the distribution.
1755 param(const param_type
& __param
)
1756 { _M_param
= __param
; }
1759 * @brief Returns the inclusive lower bound of the distribution range.
1763 { return this->a(); }
1766 * @brief Returns the inclusive upper bound of the distribution range.
1770 { return this->b(); }
1773 * @brief Generating functions.
1775 template<typename _UniformRandomNumberGenerator
>
1777 operator()(_UniformRandomNumberGenerator
& __urng
)
1778 { return this->operator()(__urng
, this->param()); }
1780 template<typename _UniformRandomNumberGenerator
>
1782 operator()(_UniformRandomNumberGenerator
& __urng
,
1783 const param_type
& __p
);
1785 param_type _M_param
;
1789 * @brief Return true if two uniform integer distributions have
1790 * the same parameters.
1792 template<typename _IntType
>
1794 operator==(const std::uniform_int_distribution
<_IntType
>& __d1
,
1795 const std::uniform_int_distribution
<_IntType
>& __d2
)
1796 { return __d1
.param() == __d2
.param(); }
1799 * @brief Return true if two uniform integer distributions have
1800 * different parameters.
1802 template<typename _IntType
>
1804 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1805 const std::uniform_int_distribution
<_IntType
>& __d2
)
1806 { return !(__d1
== __d2
); }
1809 * @brief Inserts a %uniform_int_distribution random number
1810 * distribution @p __x into the output stream @p os.
1812 * @param __os An output stream.
1813 * @param __x A %uniform_int_distribution random number distribution.
1815 * @returns The output stream with the state of @p __x inserted or in
1818 template<typename _IntType
, typename _CharT
, typename _Traits
>
1819 std::basic_ostream
<_CharT
, _Traits
>&
1820 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1821 const std::uniform_int_distribution
<_IntType
>&);
1824 * @brief Extracts a %uniform_int_distribution random number distribution
1825 * @p __x from the input stream @p __is.
1827 * @param __is An input stream.
1828 * @param __x A %uniform_int_distribution random number generator engine.
1830 * @returns The input stream with @p __x extracted or in an error state.
1832 template<typename _IntType
, typename _CharT
, typename _Traits
>
1833 std::basic_istream
<_CharT
, _Traits
>&
1834 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1835 std::uniform_int_distribution
<_IntType
>&);
1839 * @brief Uniform continuous distribution for random numbers.
1841 * A continuous random distribution on the range [min, max) with equal
1842 * probability throughout the range. The URNG should be real-valued and
1843 * deliver number in the range [0, 1).
1845 template<typename _RealType
= double>
1846 class uniform_real_distribution
1848 static_assert(std::is_floating_point
<_RealType
>::value
,
1849 "template argument not a floating point type");
1852 /** The type of the range of the distribution. */
1853 typedef _RealType result_type
;
1854 /** Parameter type. */
1857 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1860 param_type(_RealType __a
= _RealType(0),
1861 _RealType __b
= _RealType(1))
1862 : _M_a(__a
), _M_b(__b
)
1864 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1876 operator==(const param_type
& __p1
, const param_type
& __p2
)
1877 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1886 * @brief Constructs a uniform_real_distribution object.
1888 * @param __a [IN] The lower bound of the distribution.
1889 * @param __b [IN] The upper bound of the distribution.
1892 uniform_real_distribution(_RealType __a
= _RealType(0),
1893 _RealType __b
= _RealType(1))
1894 : _M_param(__a
, __b
)
1898 uniform_real_distribution(const param_type
& __p
)
1903 * @brief Resets the distribution state.
1905 * Does nothing for the uniform real distribution.
1912 { return _M_param
.a(); }
1916 { return _M_param
.b(); }
1919 * @brief Returns the parameter set of the distribution.
1923 { return _M_param
; }
1926 * @brief Sets the parameter set of the distribution.
1927 * @param __param The new parameter set of the distribution.
1930 param(const param_type
& __param
)
1931 { _M_param
= __param
; }
1934 * @brief Returns the inclusive lower bound of the distribution range.
1938 { return this->a(); }
1941 * @brief Returns the inclusive upper bound of the distribution range.
1945 { return this->b(); }
1948 * @brief Generating functions.
1950 template<typename _UniformRandomNumberGenerator
>
1952 operator()(_UniformRandomNumberGenerator
& __urng
)
1953 { return this->operator()(__urng
, this->param()); }
1955 template<typename _UniformRandomNumberGenerator
>
1957 operator()(_UniformRandomNumberGenerator
& __urng
,
1958 const param_type
& __p
)
1960 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1962 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1966 param_type _M_param
;
1970 * @brief Return true if two uniform real distributions have
1971 * the same parameters.
1973 template<typename _IntType
>
1975 operator==(const std::uniform_real_distribution
<_IntType
>& __d1
,
1976 const std::uniform_real_distribution
<_IntType
>& __d2
)
1977 { return __d1
.param() == __d2
.param(); }
1980 * @brief Return true if two uniform real distributions have
1981 * different parameters.
1983 template<typename _IntType
>
1985 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1986 const std::uniform_real_distribution
<_IntType
>& __d2
)
1987 { return !(__d1
== __d2
); }
1990 * @brief Inserts a %uniform_real_distribution random number
1991 * distribution @p __x into the output stream @p __os.
1993 * @param __os An output stream.
1994 * @param __x A %uniform_real_distribution random number distribution.
1996 * @returns The output stream with the state of @p __x inserted or in
1999 template<typename _RealType
, typename _CharT
, typename _Traits
>
2000 std::basic_ostream
<_CharT
, _Traits
>&
2001 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2002 const std::uniform_real_distribution
<_RealType
>&);
2005 * @brief Extracts a %uniform_real_distribution random number distribution
2006 * @p __x from the input stream @p __is.
2008 * @param __is An input stream.
2009 * @param __x A %uniform_real_distribution random number generator engine.
2011 * @returns The input stream with @p __x extracted or in an error state.
2013 template<typename _RealType
, typename _CharT
, typename _Traits
>
2014 std::basic_istream
<_CharT
, _Traits
>&
2015 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2016 std::uniform_real_distribution
<_RealType
>&);
2018 /* @} */ // group random_distributions_uniform
2021 * @addtogroup random_distributions_normal Normal Distributions
2022 * @ingroup random_distributions
2027 * @brief A normal continuous distribution for random numbers.
2029 * The formula for the normal probability density function is
2031 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
2032 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
2035 template<typename _RealType
= double>
2036 class normal_distribution
2038 static_assert(std::is_floating_point
<_RealType
>::value
,
2039 "template argument not a floating point type");
2042 /** The type of the range of the distribution. */
2043 typedef _RealType result_type
;
2044 /** Parameter type. */
2047 typedef normal_distribution
<_RealType
> distribution_type
;
2050 param_type(_RealType __mean
= _RealType(0),
2051 _RealType __stddev
= _RealType(1))
2052 : _M_mean(__mean
), _M_stddev(__stddev
)
2054 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
2063 { return _M_stddev
; }
2066 operator==(const param_type
& __p1
, const param_type
& __p2
)
2067 { return (__p1
._M_mean
== __p2
._M_mean
2068 && __p1
._M_stddev
== __p2
._M_stddev
); }
2072 _RealType _M_stddev
;
2077 * Constructs a normal distribution with parameters @f$mean@f$ and
2078 * standard deviation.
2081 normal_distribution(result_type __mean
= result_type(0),
2082 result_type __stddev
= result_type(1))
2083 : _M_param(__mean
, __stddev
), _M_saved_available(false)
2087 normal_distribution(const param_type
& __p
)
2088 : _M_param(__p
), _M_saved_available(false)
2092 * @brief Resets the distribution state.
2096 { _M_saved_available
= false; }
2099 * @brief Returns the mean of the distribution.
2103 { return _M_param
.mean(); }
2106 * @brief Returns the standard deviation of the distribution.
2110 { return _M_param
.stddev(); }
2113 * @brief Returns the parameter set of the distribution.
2117 { return _M_param
; }
2120 * @brief Sets the parameter set of the distribution.
2121 * @param __param The new parameter set of the distribution.
2124 param(const param_type
& __param
)
2125 { _M_param
= __param
; }
2128 * @brief Returns the greatest lower bound value of the distribution.
2132 { return std::numeric_limits
<result_type
>::min(); }
2135 * @brief Returns the least upper bound value of the distribution.
2139 { return std::numeric_limits
<result_type
>::max(); }
2142 * @brief Generating functions.
2144 template<typename _UniformRandomNumberGenerator
>
2146 operator()(_UniformRandomNumberGenerator
& __urng
)
2147 { return this->operator()(__urng
, this->param()); }
2149 template<typename _UniformRandomNumberGenerator
>
2151 operator()(_UniformRandomNumberGenerator
& __urng
,
2152 const param_type
& __p
);
2155 * @brief Return true if two normal distributions have
2156 * the same parameters and the sequences that would
2157 * be generated are equal.
2159 template<typename _RealType1
>
2161 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2162 const std::normal_distribution
<_RealType1
>& __d2
);
2165 * @brief Inserts a %normal_distribution random number distribution
2166 * @p __x into the output stream @p __os.
2168 * @param __os An output stream.
2169 * @param __x A %normal_distribution random number distribution.
2171 * @returns The output stream with the state of @p __x inserted or in
2174 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2175 friend std::basic_ostream
<_CharT
, _Traits
>&
2176 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2177 const std::normal_distribution
<_RealType1
>& __x
);
2180 * @brief Extracts a %normal_distribution random number distribution
2181 * @p __x from the input stream @p __is.
2183 * @param __is An input stream.
2184 * @param __x A %normal_distribution random number generator engine.
2186 * @returns The input stream with @p __x extracted or in an error
2189 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2190 friend std::basic_istream
<_CharT
, _Traits
>&
2191 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2192 std::normal_distribution
<_RealType1
>& __x
);
2195 param_type _M_param
;
2196 result_type _M_saved
;
2197 bool _M_saved_available
;
2201 * @brief Return true if two normal distributions are different.
2203 template<typename _RealType
>
2205 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2206 const std::normal_distribution
<_RealType
>& __d2
)
2207 { return !(__d1
== __d2
); }
2211 * @brief A lognormal_distribution random number distribution.
2213 * The formula for the normal probability mass function is
2215 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2216 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2219 template<typename _RealType
= double>
2220 class lognormal_distribution
2222 static_assert(std::is_floating_point
<_RealType
>::value
,
2223 "template argument not a floating point type");
2226 /** The type of the range of the distribution. */
2227 typedef _RealType result_type
;
2228 /** Parameter type. */
2231 typedef lognormal_distribution
<_RealType
> distribution_type
;
2234 param_type(_RealType __m
= _RealType(0),
2235 _RealType __s
= _RealType(1))
2236 : _M_m(__m
), _M_s(__s
)
2248 operator==(const param_type
& __p1
, const param_type
& __p2
)
2249 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2257 lognormal_distribution(_RealType __m
= _RealType(0),
2258 _RealType __s
= _RealType(1))
2259 : _M_param(__m
, __s
), _M_nd()
2263 lognormal_distribution(const param_type
& __p
)
2264 : _M_param(__p
), _M_nd()
2268 * Resets the distribution state.
2279 { return _M_param
.m(); }
2283 { return _M_param
.s(); }
2286 * @brief Returns the parameter set of the distribution.
2290 { return _M_param
; }
2293 * @brief Sets the parameter set of the distribution.
2294 * @param __param The new parameter set of the distribution.
2297 param(const param_type
& __param
)
2298 { _M_param
= __param
; }
2301 * @brief Returns the greatest lower bound value of the distribution.
2305 { return result_type(0); }
2308 * @brief Returns the least upper bound value of the distribution.
2312 { return std::numeric_limits
<result_type
>::max(); }
2315 * @brief Generating functions.
2317 template<typename _UniformRandomNumberGenerator
>
2319 operator()(_UniformRandomNumberGenerator
& __urng
)
2320 { return this->operator()(__urng
, this->param()); }
2322 template<typename _UniformRandomNumberGenerator
>
2324 operator()(_UniformRandomNumberGenerator
& __urng
,
2325 const param_type
& __p
)
2326 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2329 * @brief Return true if two lognormal distributions have
2330 * the same parameters and the sequences that would
2331 * be generated are equal.
2334 operator==(const lognormal_distribution
& __d1
,
2335 const lognormal_distribution
& __d2
)
2336 { return (__d1
.param() == __d2
.param()
2337 && __d1
._M_nd
== __d2
._M_nd
); }
2340 * @brief Inserts a %lognormal_distribution random number distribution
2341 * @p __x into the output stream @p __os.
2343 * @param __os An output stream.
2344 * @param __x A %lognormal_distribution random number distribution.
2346 * @returns The output stream with the state of @p __x inserted or in
2349 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2350 friend std::basic_ostream
<_CharT
, _Traits
>&
2351 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2352 const std::lognormal_distribution
<_RealType1
>& __x
);
2355 * @brief Extracts a %lognormal_distribution random number distribution
2356 * @p __x from the input stream @p __is.
2358 * @param __is An input stream.
2359 * @param __x A %lognormal_distribution random number
2362 * @returns The input stream with @p __x extracted or in an error state.
2364 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2365 friend std::basic_istream
<_CharT
, _Traits
>&
2366 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2367 std::lognormal_distribution
<_RealType1
>& __x
);
2370 param_type _M_param
;
2372 std::normal_distribution
<result_type
> _M_nd
;
2376 * @brief Return true if two lognormal distributions are different.
2378 template<typename _RealType
>
2380 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2381 const std::lognormal_distribution
<_RealType
>& __d2
)
2382 { return !(__d1
== __d2
); }
2386 * @brief A gamma continuous distribution for random numbers.
2388 * The formula for the gamma probability density function is:
2390 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2391 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2394 template<typename _RealType
= double>
2395 class gamma_distribution
2397 static_assert(std::is_floating_point
<_RealType
>::value
,
2398 "template argument not a floating point type");
2401 /** The type of the range of the distribution. */
2402 typedef _RealType result_type
;
2403 /** Parameter type. */
2406 typedef gamma_distribution
<_RealType
> distribution_type
;
2407 friend class gamma_distribution
<_RealType
>;
2410 param_type(_RealType __alpha_val
= _RealType(1),
2411 _RealType __beta_val
= _RealType(1))
2412 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2414 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2420 { return _M_alpha
; }
2427 operator==(const param_type
& __p1
, const param_type
& __p2
)
2428 { return (__p1
._M_alpha
== __p2
._M_alpha
2429 && __p1
._M_beta
== __p2
._M_beta
); }
2438 _RealType _M_malpha
, _M_a2
;
2443 * @brief Constructs a gamma distribution with parameters
2444 * @f$\alpha@f$ and @f$\beta@f$.
2447 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2448 _RealType __beta_val
= _RealType(1))
2449 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2453 gamma_distribution(const param_type
& __p
)
2454 : _M_param(__p
), _M_nd()
2458 * @brief Resets the distribution state.
2465 * @brief Returns the @f$\alpha@f$ of the distribution.
2469 { return _M_param
.alpha(); }
2472 * @brief Returns the @f$\beta@f$ of the distribution.
2476 { return _M_param
.beta(); }
2479 * @brief Returns the parameter set of the distribution.
2483 { return _M_param
; }
2486 * @brief Sets the parameter set of the distribution.
2487 * @param __param The new parameter set of the distribution.
2490 param(const param_type
& __param
)
2491 { _M_param
= __param
; }
2494 * @brief Returns the greatest lower bound value of the distribution.
2498 { return result_type(0); }
2501 * @brief Returns the least upper bound value of the distribution.
2505 { return std::numeric_limits
<result_type
>::max(); }
2508 * @brief Generating functions.
2510 template<typename _UniformRandomNumberGenerator
>
2512 operator()(_UniformRandomNumberGenerator
& __urng
)
2513 { return this->operator()(__urng
, this->param()); }
2515 template<typename _UniformRandomNumberGenerator
>
2517 operator()(_UniformRandomNumberGenerator
& __urng
,
2518 const param_type
& __p
);
2521 * @brief Return true if two gamma distributions have the same
2522 * parameters and the sequences that would be generated
2526 operator==(const gamma_distribution
& __d1
,
2527 const gamma_distribution
& __d2
)
2528 { return (__d1
.param() == __d2
.param()
2529 && __d1
._M_nd
== __d2
._M_nd
); }
2532 * @brief Inserts a %gamma_distribution random number distribution
2533 * @p __x into the output stream @p __os.
2535 * @param __os An output stream.
2536 * @param __x A %gamma_distribution random number distribution.
2538 * @returns The output stream with the state of @p __x inserted or in
2541 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2542 friend std::basic_ostream
<_CharT
, _Traits
>&
2543 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2544 const std::gamma_distribution
<_RealType1
>& __x
);
2547 * @brief Extracts a %gamma_distribution random number distribution
2548 * @p __x from the input stream @p __is.
2550 * @param __is An input stream.
2551 * @param __x A %gamma_distribution random number generator engine.
2553 * @returns The input stream with @p __x extracted or in an error state.
2555 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2556 friend std::basic_istream
<_CharT
, _Traits
>&
2557 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2558 std::gamma_distribution
<_RealType1
>& __x
);
2561 param_type _M_param
;
2563 std::normal_distribution
<result_type
> _M_nd
;
2567 * @brief Return true if two gamma distributions are different.
2569 template<typename _RealType
>
2571 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2572 const std::gamma_distribution
<_RealType
>& __d2
)
2573 { return !(__d1
== __d2
); }
2577 * @brief A chi_squared_distribution random number distribution.
2579 * The formula for the normal probability mass function is
2580 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2582 template<typename _RealType
= double>
2583 class chi_squared_distribution
2585 static_assert(std::is_floating_point
<_RealType
>::value
,
2586 "template argument not a floating point type");
2589 /** The type of the range of the distribution. */
2590 typedef _RealType result_type
;
2591 /** Parameter type. */
2594 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2597 param_type(_RealType __n
= _RealType(1))
2606 operator==(const param_type
& __p1
, const param_type
& __p2
)
2607 { return __p1
._M_n
== __p2
._M_n
; }
2614 chi_squared_distribution(_RealType __n
= _RealType(1))
2615 : _M_param(__n
), _M_gd(__n
/ 2)
2619 chi_squared_distribution(const param_type
& __p
)
2620 : _M_param(__p
), _M_gd(__p
.n() / 2)
2624 * @brief Resets the distribution state.
2635 { return _M_param
.n(); }
2638 * @brief Returns the parameter set of the distribution.
2642 { return _M_param
; }
2645 * @brief Sets the parameter set of the distribution.
2646 * @param __param The new parameter set of the distribution.
2649 param(const param_type
& __param
)
2650 { _M_param
= __param
; }
2653 * @brief Returns the greatest lower bound value of the distribution.
2657 { return result_type(0); }
2660 * @brief Returns the least upper bound value of the distribution.
2664 { return std::numeric_limits
<result_type
>::max(); }
2667 * @brief Generating functions.
2669 template<typename _UniformRandomNumberGenerator
>
2671 operator()(_UniformRandomNumberGenerator
& __urng
)
2672 { return 2 * _M_gd(__urng
); }
2674 template<typename _UniformRandomNumberGenerator
>
2676 operator()(_UniformRandomNumberGenerator
& __urng
,
2677 const param_type
& __p
)
2679 typedef typename
std::gamma_distribution
<result_type
>::param_type
2681 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2685 * @brief Return true if two Chi-squared distributions have
2686 * the same parameters and the sequences that would be
2687 * generated are equal.
2690 operator==(const chi_squared_distribution
& __d1
,
2691 const chi_squared_distribution
& __d2
)
2692 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
2695 * @brief Inserts a %chi_squared_distribution random number distribution
2696 * @p __x into the output stream @p __os.
2698 * @param __os An output stream.
2699 * @param __x A %chi_squared_distribution random number distribution.
2701 * @returns The output stream with the state of @p __x inserted or in
2704 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2705 friend std::basic_ostream
<_CharT
, _Traits
>&
2706 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2707 const std::chi_squared_distribution
<_RealType1
>& __x
);
2710 * @brief Extracts a %chi_squared_distribution random number distribution
2711 * @p __x from the input stream @p __is.
2713 * @param __is An input stream.
2714 * @param __x A %chi_squared_distribution random number
2717 * @returns The input stream with @p __x extracted or in an error state.
2719 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2720 friend std::basic_istream
<_CharT
, _Traits
>&
2721 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2722 std::chi_squared_distribution
<_RealType1
>& __x
);
2725 param_type _M_param
;
2727 std::gamma_distribution
<result_type
> _M_gd
;
2731 * @brief Return true if two Chi-squared distributions are different.
2733 template<typename _RealType
>
2735 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2736 const std::chi_squared_distribution
<_RealType
>& __d2
)
2737 { return !(__d1
== __d2
); }
2741 * @brief A cauchy_distribution random number distribution.
2743 * The formula for the normal probability mass function is
2744 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2746 template<typename _RealType
= double>
2747 class cauchy_distribution
2749 static_assert(std::is_floating_point
<_RealType
>::value
,
2750 "template argument not a floating point type");
2753 /** The type of the range of the distribution. */
2754 typedef _RealType result_type
;
2755 /** Parameter type. */
2758 typedef cauchy_distribution
<_RealType
> distribution_type
;
2761 param_type(_RealType __a
= _RealType(0),
2762 _RealType __b
= _RealType(1))
2763 : _M_a(__a
), _M_b(__b
)
2775 operator==(const param_type
& __p1
, const param_type
& __p2
)
2776 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2784 cauchy_distribution(_RealType __a
= _RealType(0),
2785 _RealType __b
= _RealType(1))
2786 : _M_param(__a
, __b
)
2790 cauchy_distribution(const param_type
& __p
)
2795 * @brief Resets the distribution state.
2806 { return _M_param
.a(); }
2810 { return _M_param
.b(); }
2813 * @brief Returns the parameter set of the distribution.
2817 { return _M_param
; }
2820 * @brief Sets the parameter set of the distribution.
2821 * @param __param The new parameter set of the distribution.
2824 param(const param_type
& __param
)
2825 { _M_param
= __param
; }
2828 * @brief Returns the greatest lower bound value of the distribution.
2832 { return std::numeric_limits
<result_type
>::min(); }
2835 * @brief Returns the least upper bound value of the distribution.
2839 { return std::numeric_limits
<result_type
>::max(); }
2842 * @brief Generating functions.
2844 template<typename _UniformRandomNumberGenerator
>
2846 operator()(_UniformRandomNumberGenerator
& __urng
)
2847 { return this->operator()(__urng
, this->param()); }
2849 template<typename _UniformRandomNumberGenerator
>
2851 operator()(_UniformRandomNumberGenerator
& __urng
,
2852 const param_type
& __p
);
2855 param_type _M_param
;
2859 * @brief Return true if two Cauchy distributions have
2860 * the same parameters.
2862 template<typename _RealType
>
2864 operator==(const std::cauchy_distribution
<_RealType
>& __d1
,
2865 const std::cauchy_distribution
<_RealType
>& __d2
)
2866 { return __d1
.param() == __d2
.param(); }
2869 * @brief Return true if two Cauchy distributions have
2870 * different parameters.
2872 template<typename _RealType
>
2874 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
2875 const std::cauchy_distribution
<_RealType
>& __d2
)
2876 { return !(__d1
== __d2
); }
2879 * @brief Inserts a %cauchy_distribution random number distribution
2880 * @p __x into the output stream @p __os.
2882 * @param __os An output stream.
2883 * @param __x A %cauchy_distribution random number distribution.
2885 * @returns The output stream with the state of @p __x inserted or in
2888 template<typename _RealType
, typename _CharT
, typename _Traits
>
2889 std::basic_ostream
<_CharT
, _Traits
>&
2890 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2891 const std::cauchy_distribution
<_RealType
>& __x
);
2894 * @brief Extracts a %cauchy_distribution random number distribution
2895 * @p __x from the input stream @p __is.
2897 * @param __is An input stream.
2898 * @param __x A %cauchy_distribution random number
2901 * @returns The input stream with @p __x extracted or in an error state.
2903 template<typename _RealType
, typename _CharT
, typename _Traits
>
2904 std::basic_istream
<_CharT
, _Traits
>&
2905 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2906 std::cauchy_distribution
<_RealType
>& __x
);
2910 * @brief A fisher_f_distribution random number distribution.
2912 * The formula for the normal probability mass function is
2914 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2915 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2916 * (1 + \frac{mx}{n})^{-(m+n)/2}
2919 template<typename _RealType
= double>
2920 class fisher_f_distribution
2922 static_assert(std::is_floating_point
<_RealType
>::value
,
2923 "template argument not a floating point type");
2926 /** The type of the range of the distribution. */
2927 typedef _RealType result_type
;
2928 /** Parameter type. */
2931 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2934 param_type(_RealType __m
= _RealType(1),
2935 _RealType __n
= _RealType(1))
2936 : _M_m(__m
), _M_n(__n
)
2948 operator==(const param_type
& __p1
, const param_type
& __p2
)
2949 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
2957 fisher_f_distribution(_RealType __m
= _RealType(1),
2958 _RealType __n
= _RealType(1))
2959 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2963 fisher_f_distribution(const param_type
& __p
)
2964 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2968 * @brief Resets the distribution state.
2982 { return _M_param
.m(); }
2986 { return _M_param
.n(); }
2989 * @brief Returns the parameter set of the distribution.
2993 { return _M_param
; }
2996 * @brief Sets the parameter set of the distribution.
2997 * @param __param The new parameter set of the distribution.
3000 param(const param_type
& __param
)
3001 { _M_param
= __param
; }
3004 * @brief Returns the greatest lower bound value of the distribution.
3008 { return result_type(0); }
3011 * @brief Returns the least upper bound value of the distribution.
3015 { return std::numeric_limits
<result_type
>::max(); }
3018 * @brief Generating functions.
3020 template<typename _UniformRandomNumberGenerator
>
3022 operator()(_UniformRandomNumberGenerator
& __urng
)
3023 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
3025 template<typename _UniformRandomNumberGenerator
>
3027 operator()(_UniformRandomNumberGenerator
& __urng
,
3028 const param_type
& __p
)
3030 typedef typename
std::gamma_distribution
<result_type
>::param_type
3032 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
3033 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
3037 * @brief Return true if two Fisher f distributions have
3038 * the same parameters and the sequences that would
3039 * be generated are equal.
3042 operator==(const fisher_f_distribution
& __d1
,
3043 const fisher_f_distribution
& __d2
)
3044 { return (__d1
.param() == __d2
.param()
3045 && __d1
._M_gd_x
== __d2
._M_gd_x
3046 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
3049 * @brief Inserts a %fisher_f_distribution random number distribution
3050 * @p __x into the output stream @p __os.
3052 * @param __os An output stream.
3053 * @param __x A %fisher_f_distribution random number distribution.
3055 * @returns The output stream with the state of @p __x inserted or in
3058 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3059 friend std::basic_ostream
<_CharT
, _Traits
>&
3060 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3061 const std::fisher_f_distribution
<_RealType1
>& __x
);
3064 * @brief Extracts a %fisher_f_distribution random number distribution
3065 * @p __x from the input stream @p __is.
3067 * @param __is An input stream.
3068 * @param __x A %fisher_f_distribution random number
3071 * @returns The input stream with @p __x extracted or in an error state.
3073 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3074 friend std::basic_istream
<_CharT
, _Traits
>&
3075 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3076 std::fisher_f_distribution
<_RealType1
>& __x
);
3079 param_type _M_param
;
3081 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3085 * @brief Return true if two Fisher f distributions are diferent.
3087 template<typename _RealType
>
3089 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3090 const std::fisher_f_distribution
<_RealType
>& __d2
)
3091 { return !(__d1
== __d2
); }
3094 * @brief A student_t_distribution random number distribution.
3096 * The formula for the normal probability mass function is:
3098 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3099 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3102 template<typename _RealType
= double>
3103 class student_t_distribution
3105 static_assert(std::is_floating_point
<_RealType
>::value
,
3106 "template argument not a floating point type");
3109 /** The type of the range of the distribution. */
3110 typedef _RealType result_type
;
3111 /** Parameter type. */
3114 typedef student_t_distribution
<_RealType
> distribution_type
;
3117 param_type(_RealType __n
= _RealType(1))
3126 operator==(const param_type
& __p1
, const param_type
& __p2
)
3127 { return __p1
._M_n
== __p2
._M_n
; }
3134 student_t_distribution(_RealType __n
= _RealType(1))
3135 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3139 student_t_distribution(const param_type
& __p
)
3140 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3144 * @brief Resets the distribution state.
3158 { return _M_param
.n(); }
3161 * @brief Returns the parameter set of the distribution.
3165 { return _M_param
; }
3168 * @brief Sets the parameter set of the distribution.
3169 * @param __param The new parameter set of the distribution.
3172 param(const param_type
& __param
)
3173 { _M_param
= __param
; }
3176 * @brief Returns the greatest lower bound value of the distribution.
3180 { return std::numeric_limits
<result_type
>::min(); }
3183 * @brief Returns the least upper bound value of the distribution.
3187 { return std::numeric_limits
<result_type
>::max(); }
3190 * @brief Generating functions.
3192 template<typename _UniformRandomNumberGenerator
>
3194 operator()(_UniformRandomNumberGenerator
& __urng
)
3195 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3197 template<typename _UniformRandomNumberGenerator
>
3199 operator()(_UniformRandomNumberGenerator
& __urng
,
3200 const param_type
& __p
)
3202 typedef typename
std::gamma_distribution
<result_type
>::param_type
3205 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3206 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3210 * @brief Return true if two Student t distributions have
3211 * the same parameters and the sequences that would
3212 * be generated are equal.
3215 operator==(const student_t_distribution
& __d1
,
3216 const student_t_distribution
& __d2
)
3217 { return (__d1
.param() == __d2
.param()
3218 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3221 * @brief Inserts a %student_t_distribution random number distribution
3222 * @p __x into the output stream @p __os.
3224 * @param __os An output stream.
3225 * @param __x A %student_t_distribution random number distribution.
3227 * @returns The output stream with the state of @p __x inserted or in
3230 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3231 friend std::basic_ostream
<_CharT
, _Traits
>&
3232 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3233 const std::student_t_distribution
<_RealType1
>& __x
);
3236 * @brief Extracts a %student_t_distribution random number distribution
3237 * @p __x from the input stream @p __is.
3239 * @param __is An input stream.
3240 * @param __x A %student_t_distribution random number
3243 * @returns The input stream with @p __x extracted or in an error state.
3245 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3246 friend std::basic_istream
<_CharT
, _Traits
>&
3247 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3248 std::student_t_distribution
<_RealType1
>& __x
);
3251 param_type _M_param
;
3253 std::normal_distribution
<result_type
> _M_nd
;
3254 std::gamma_distribution
<result_type
> _M_gd
;
3258 * @brief Return true if two Student t distributions are different.
3260 template<typename _RealType
>
3262 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3263 const std::student_t_distribution
<_RealType
>& __d2
)
3264 { return !(__d1
== __d2
); }
3267 /* @} */ // group random_distributions_normal
3270 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3271 * @ingroup random_distributions
3276 * @brief A Bernoulli random number distribution.
3278 * Generates a sequence of true and false values with likelihood @f$p@f$
3279 * that true will come up and @f$(1 - p)@f$ that false will appear.
3281 class bernoulli_distribution
3284 /** The type of the range of the distribution. */
3285 typedef bool result_type
;
3286 /** Parameter type. */
3289 typedef bernoulli_distribution distribution_type
;
3292 param_type(double __p
= 0.5)
3295 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
3303 operator==(const param_type
& __p1
, const param_type
& __p2
)
3304 { return __p1
._M_p
== __p2
._M_p
; }
3312 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3314 * @param __p [IN] The likelihood of a true result being returned.
3315 * Must be in the interval @f$[0, 1]@f$.
3318 bernoulli_distribution(double __p
= 0.5)
3323 bernoulli_distribution(const param_type
& __p
)
3328 * @brief Resets the distribution state.
3330 * Does nothing for a Bernoulli distribution.
3336 * @brief Returns the @p p parameter of the distribution.
3340 { return _M_param
.p(); }
3343 * @brief Returns the parameter set of the distribution.
3347 { return _M_param
; }
3350 * @brief Sets the parameter set of the distribution.
3351 * @param __param The new parameter set of the distribution.
3354 param(const param_type
& __param
)
3355 { _M_param
= __param
; }
3358 * @brief Returns the greatest lower bound value of the distribution.
3362 { return std::numeric_limits
<result_type
>::min(); }
3365 * @brief Returns the least upper bound value of the distribution.
3369 { return std::numeric_limits
<result_type
>::max(); }
3372 * @brief Generating functions.
3374 template<typename _UniformRandomNumberGenerator
>
3376 operator()(_UniformRandomNumberGenerator
& __urng
)
3377 { return this->operator()(__urng
, this->param()); }
3379 template<typename _UniformRandomNumberGenerator
>
3381 operator()(_UniformRandomNumberGenerator
& __urng
,
3382 const param_type
& __p
)
3384 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3386 if ((__aurng() - __aurng
.min())
3387 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3393 param_type _M_param
;
3397 * @brief Return true if two Bernoulli distributions have
3398 * the same parameters.
3401 operator==(const std::bernoulli_distribution
& __d1
,
3402 const std::bernoulli_distribution
& __d2
)
3403 { return __d1
.param() == __d2
.param(); }
3406 * @brief Return true if two Bernoulli distributions have
3407 * different parameters.
3410 operator!=(const std::bernoulli_distribution
& __d1
,
3411 const std::bernoulli_distribution
& __d2
)
3412 { return !(__d1
== __d2
); }
3415 * @brief Inserts a %bernoulli_distribution random number distribution
3416 * @p __x into the output stream @p __os.
3418 * @param __os An output stream.
3419 * @param __x A %bernoulli_distribution random number distribution.
3421 * @returns The output stream with the state of @p __x inserted or in
3424 template<typename _CharT
, typename _Traits
>
3425 std::basic_ostream
<_CharT
, _Traits
>&
3426 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3427 const std::bernoulli_distribution
& __x
);
3430 * @brief Extracts a %bernoulli_distribution random number distribution
3431 * @p __x from the input stream @p __is.
3433 * @param __is An input stream.
3434 * @param __x A %bernoulli_distribution random number generator engine.
3436 * @returns The input stream with @p __x extracted or in an error state.
3438 template<typename _CharT
, typename _Traits
>
3439 std::basic_istream
<_CharT
, _Traits
>&
3440 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3441 std::bernoulli_distribution
& __x
)
3445 __x
.param(bernoulli_distribution::param_type(__p
));
3451 * @brief A discrete binomial random number distribution.
3453 * The formula for the binomial probability density function is
3454 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3455 * and @f$p@f$ are the parameters of the distribution.
3457 template<typename _IntType
= int>
3458 class binomial_distribution
3460 static_assert(std::is_integral
<_IntType
>::value
,
3461 "template argument not an integral type");
3464 /** The type of the range of the distribution. */
3465 typedef _IntType result_type
;
3466 /** Parameter type. */
3469 typedef binomial_distribution
<_IntType
> distribution_type
;
3470 friend class binomial_distribution
<_IntType
>;
3473 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3474 : _M_t(__t
), _M_p(__p
)
3476 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3491 operator==(const param_type
& __p1
, const param_type
& __p2
)
3492 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3502 #if _GLIBCXX_USE_C99_MATH_TR1
3503 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3504 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3509 // constructors and member function
3511 binomial_distribution(_IntType __t
= _IntType(1),
3513 : _M_param(__t
, __p
), _M_nd()
3517 binomial_distribution(const param_type
& __p
)
3518 : _M_param(__p
), _M_nd()
3522 * @brief Resets the distribution state.
3529 * @brief Returns the distribution @p t parameter.
3533 { return _M_param
.t(); }
3536 * @brief Returns the distribution @p p parameter.
3540 { return _M_param
.p(); }
3543 * @brief Returns the parameter set of the distribution.
3547 { return _M_param
; }
3550 * @brief Sets the parameter set of the distribution.
3551 * @param __param The new parameter set of the distribution.
3554 param(const param_type
& __param
)
3555 { _M_param
= __param
; }
3558 * @brief Returns the greatest lower bound value of the distribution.
3565 * @brief Returns the least upper bound value of the distribution.
3569 { return _M_param
.t(); }
3572 * @brief Generating functions.
3574 template<typename _UniformRandomNumberGenerator
>
3576 operator()(_UniformRandomNumberGenerator
& __urng
)
3577 { return this->operator()(__urng
, this->param()); }
3579 template<typename _UniformRandomNumberGenerator
>
3581 operator()(_UniformRandomNumberGenerator
& __urng
,
3582 const param_type
& __p
);
3585 * @brief Return true if two binomial distributions have
3586 * the same parameters and the sequences that would
3587 * be generated are equal.
3590 operator==(const binomial_distribution
& __d1
,
3591 const binomial_distribution
& __d2
)
3592 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3593 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
3595 { return __d1
.param() == __d2
.param(); }
3599 * @brief Inserts a %binomial_distribution random number distribution
3600 * @p __x into the output stream @p __os.
3602 * @param __os An output stream.
3603 * @param __x A %binomial_distribution random number distribution.
3605 * @returns The output stream with the state of @p __x inserted or in
3608 template<typename _IntType1
,
3609 typename _CharT
, typename _Traits
>
3610 friend std::basic_ostream
<_CharT
, _Traits
>&
3611 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3612 const std::binomial_distribution
<_IntType1
>& __x
);
3615 * @brief Extracts a %binomial_distribution random number distribution
3616 * @p __x from the input stream @p __is.
3618 * @param __is An input stream.
3619 * @param __x A %binomial_distribution random number generator engine.
3621 * @returns The input stream with @p __x extracted or in an error
3624 template<typename _IntType1
,
3625 typename _CharT
, typename _Traits
>
3626 friend std::basic_istream
<_CharT
, _Traits
>&
3627 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3628 std::binomial_distribution
<_IntType1
>& __x
);
3631 template<typename _UniformRandomNumberGenerator
>
3633 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3635 param_type _M_param
;
3637 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3638 std::normal_distribution
<double> _M_nd
;
3642 * @brief Return true if two binomial distributions are different.
3644 template<typename _IntType
>
3646 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
3647 const std::binomial_distribution
<_IntType
>& __d2
)
3648 { return !(__d1
== __d2
); }
3652 * @brief A discrete geometric random number distribution.
3654 * The formula for the geometric probability density function is
3655 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
3658 template<typename _IntType
= int>
3659 class geometric_distribution
3661 static_assert(std::is_integral
<_IntType
>::value
,
3662 "template argument not an integral type");
3665 /** The type of the range of the distribution. */
3666 typedef _IntType result_type
;
3667 /** Parameter type. */
3670 typedef geometric_distribution
<_IntType
> distribution_type
;
3671 friend class geometric_distribution
<_IntType
>;
3674 param_type(double __p
= 0.5)
3677 _GLIBCXX_DEBUG_ASSERT((_M_p
> 0.0) && (_M_p
< 1.0));
3686 operator==(const param_type
& __p1
, const param_type
& __p2
)
3687 { return __p1
._M_p
== __p2
._M_p
; }
3692 { _M_log_1_p
= std::log(1.0 - _M_p
); }
3699 // constructors and member function
3701 geometric_distribution(double __p
= 0.5)
3706 geometric_distribution(const param_type
& __p
)
3711 * @brief Resets the distribution state.
3713 * Does nothing for the geometric distribution.
3719 * @brief Returns the distribution parameter @p p.
3723 { return _M_param
.p(); }
3726 * @brief Returns the parameter set of the distribution.
3730 { return _M_param
; }
3733 * @brief Sets the parameter set of the distribution.
3734 * @param __param The new parameter set of the distribution.
3737 param(const param_type
& __param
)
3738 { _M_param
= __param
; }
3741 * @brief Returns the greatest lower bound value of the distribution.
3748 * @brief Returns the least upper bound value of the distribution.
3752 { return std::numeric_limits
<result_type
>::max(); }
3755 * @brief Generating functions.
3757 template<typename _UniformRandomNumberGenerator
>
3759 operator()(_UniformRandomNumberGenerator
& __urng
)
3760 { return this->operator()(__urng
, this->param()); }
3762 template<typename _UniformRandomNumberGenerator
>
3764 operator()(_UniformRandomNumberGenerator
& __urng
,
3765 const param_type
& __p
);
3768 param_type _M_param
;
3772 * @brief Return true if two geometric distributions have
3773 * the same parameters.
3775 template<typename _IntType
>
3777 operator==(const std::geometric_distribution
<_IntType
>& __d1
,
3778 const std::geometric_distribution
<_IntType
>& __d2
)
3779 { return __d1
.param() == __d2
.param(); }
3782 * @brief Return true if two geometric distributions have
3783 * different parameters.
3785 template<typename _IntType
>
3787 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
3788 const std::geometric_distribution
<_IntType
>& __d2
)
3789 { return !(__d1
== __d2
); }
3792 * @brief Inserts a %geometric_distribution random number distribution
3793 * @p __x into the output stream @p __os.
3795 * @param __os An output stream.
3796 * @param __x A %geometric_distribution random number distribution.
3798 * @returns The output stream with the state of @p __x inserted or in
3801 template<typename _IntType
,
3802 typename _CharT
, typename _Traits
>
3803 std::basic_ostream
<_CharT
, _Traits
>&
3804 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3805 const std::geometric_distribution
<_IntType
>& __x
);
3808 * @brief Extracts a %geometric_distribution random number distribution
3809 * @p __x from the input stream @p __is.
3811 * @param __is An input stream.
3812 * @param __x A %geometric_distribution random number generator engine.
3814 * @returns The input stream with @p __x extracted or in an error state.
3816 template<typename _IntType
,
3817 typename _CharT
, typename _Traits
>
3818 std::basic_istream
<_CharT
, _Traits
>&
3819 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3820 std::geometric_distribution
<_IntType
>& __x
);
3824 * @brief A negative_binomial_distribution random number distribution.
3826 * The formula for the negative binomial probability mass function is
3827 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3828 * and @f$p@f$ are the parameters of the distribution.
3830 template<typename _IntType
= int>
3831 class negative_binomial_distribution
3833 static_assert(std::is_integral
<_IntType
>::value
,
3834 "template argument not an integral type");
3837 /** The type of the range of the distribution. */
3838 typedef _IntType result_type
;
3839 /** Parameter type. */
3842 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3845 param_type(_IntType __k
= 1, double __p
= 0.5)
3846 : _M_k(__k
), _M_p(__p
)
3848 _GLIBCXX_DEBUG_ASSERT((_M_k
> 0) && (_M_p
> 0.0) && (_M_p
<= 1.0));
3860 operator==(const param_type
& __p1
, const param_type
& __p2
)
3861 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
3869 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3870 : _M_param(__k
, __p
), _M_gd(__k
, (1.0 - __p
) / __p
)
3874 negative_binomial_distribution(const param_type
& __p
)
3875 : _M_param(__p
), _M_gd(__p
.k(), (1.0 - __p
.p()) / __p
.p())
3879 * @brief Resets the distribution state.
3886 * @brief Return the @f$k@f$ parameter of the distribution.
3890 { return _M_param
.k(); }
3893 * @brief Return the @f$p@f$ parameter of the distribution.
3897 { return _M_param
.p(); }
3900 * @brief Returns the parameter set of the distribution.
3904 { return _M_param
; }
3907 * @brief Sets the parameter set of the distribution.
3908 * @param __param The new parameter set of the distribution.
3911 param(const param_type
& __param
)
3912 { _M_param
= __param
; }
3915 * @brief Returns the greatest lower bound value of the distribution.
3919 { return result_type(0); }
3922 * @brief Returns the least upper bound value of the distribution.
3926 { return std::numeric_limits
<result_type
>::max(); }
3929 * @brief Generating functions.
3931 template<typename _UniformRandomNumberGenerator
>
3933 operator()(_UniformRandomNumberGenerator
& __urng
);
3935 template<typename _UniformRandomNumberGenerator
>
3937 operator()(_UniformRandomNumberGenerator
& __urng
,
3938 const param_type
& __p
);
3941 * @brief Return true if two negative binomial distributions have
3942 * the same parameters and the sequences that would be
3943 * generated are equal.
3946 operator==(const negative_binomial_distribution
& __d1
,
3947 const negative_binomial_distribution
& __d2
)
3948 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
3951 * @brief Inserts a %negative_binomial_distribution random
3952 * number distribution @p __x into the output stream @p __os.
3954 * @param __os An output stream.
3955 * @param __x A %negative_binomial_distribution random number
3958 * @returns The output stream with the state of @p __x inserted or in
3961 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3962 friend std::basic_ostream
<_CharT
, _Traits
>&
3963 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3964 const std::negative_binomial_distribution
<_IntType1
>& __x
);
3967 * @brief Extracts a %negative_binomial_distribution random number
3968 * distribution @p __x from the input stream @p __is.
3970 * @param __is An input stream.
3971 * @param __x A %negative_binomial_distribution random number
3974 * @returns The input stream with @p __x extracted or in an error state.
3976 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3977 friend std::basic_istream
<_CharT
, _Traits
>&
3978 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3979 std::negative_binomial_distribution
<_IntType1
>& __x
);
3982 param_type _M_param
;
3984 std::gamma_distribution
<double> _M_gd
;
3988 * @brief Return true if two negative binomial distributions are different.
3990 template<typename _IntType
>
3992 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
3993 const std::negative_binomial_distribution
<_IntType
>& __d2
)
3994 { return !(__d1
== __d2
); }
3997 /* @} */ // group random_distributions_bernoulli
4000 * @addtogroup random_distributions_poisson Poisson Distributions
4001 * @ingroup random_distributions
4006 * @brief A discrete Poisson random number distribution.
4008 * The formula for the Poisson probability density function is
4009 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
4010 * parameter of the distribution.
4012 template<typename _IntType
= int>
4013 class poisson_distribution
4015 static_assert(std::is_integral
<_IntType
>::value
,
4016 "template argument not an integral type");
4019 /** The type of the range of the distribution. */
4020 typedef _IntType result_type
;
4021 /** Parameter type. */
4024 typedef poisson_distribution
<_IntType
> distribution_type
;
4025 friend class poisson_distribution
<_IntType
>;
4028 param_type(double __mean
= 1.0)
4031 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
4040 operator==(const param_type
& __p1
, const param_type
& __p2
)
4041 { return __p1
._M_mean
== __p2
._M_mean
; }
4044 // Hosts either log(mean) or the threshold of the simple method.
4051 #if _GLIBCXX_USE_C99_MATH_TR1
4052 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
4056 // constructors and member function
4058 poisson_distribution(double __mean
= 1.0)
4059 : _M_param(__mean
), _M_nd()
4063 poisson_distribution(const param_type
& __p
)
4064 : _M_param(__p
), _M_nd()
4068 * @brief Resets the distribution state.
4075 * @brief Returns the distribution parameter @p mean.
4079 { return _M_param
.mean(); }
4082 * @brief Returns the parameter set of the distribution.
4086 { return _M_param
; }
4089 * @brief Sets the parameter set of the distribution.
4090 * @param __param The new parameter set of the distribution.
4093 param(const param_type
& __param
)
4094 { _M_param
= __param
; }
4097 * @brief Returns the greatest lower bound value of the distribution.
4104 * @brief Returns the least upper bound value of the distribution.
4108 { return std::numeric_limits
<result_type
>::max(); }
4111 * @brief Generating functions.
4113 template<typename _UniformRandomNumberGenerator
>
4115 operator()(_UniformRandomNumberGenerator
& __urng
)
4116 { return this->operator()(__urng
, this->param()); }
4118 template<typename _UniformRandomNumberGenerator
>
4120 operator()(_UniformRandomNumberGenerator
& __urng
,
4121 const param_type
& __p
);
4124 * @brief Return true if two Poisson distributions have the same
4125 * parameters and the sequences that would be generated
4129 operator==(const poisson_distribution
& __d1
,
4130 const poisson_distribution
& __d2
)
4131 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4132 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
4134 { return __d1
.param() == __d2
.param(); }
4138 * @brief Inserts a %poisson_distribution random number distribution
4139 * @p __x into the output stream @p __os.
4141 * @param __os An output stream.
4142 * @param __x A %poisson_distribution random number distribution.
4144 * @returns The output stream with the state of @p __x inserted or in
4147 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4148 friend std::basic_ostream
<_CharT
, _Traits
>&
4149 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4150 const std::poisson_distribution
<_IntType1
>& __x
);
4153 * @brief Extracts a %poisson_distribution random number distribution
4154 * @p __x from the input stream @p __is.
4156 * @param __is An input stream.
4157 * @param __x A %poisson_distribution random number generator engine.
4159 * @returns The input stream with @p __x extracted or in an error
4162 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4163 friend std::basic_istream
<_CharT
, _Traits
>&
4164 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4165 std::poisson_distribution
<_IntType1
>& __x
);
4168 param_type _M_param
;
4170 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4171 std::normal_distribution
<double> _M_nd
;
4175 * @brief Return true if two Poisson distributions are different.
4177 template<typename _IntType
>
4179 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4180 const std::poisson_distribution
<_IntType
>& __d2
)
4181 { return !(__d1
== __d2
); }
4185 * @brief An exponential continuous distribution for random numbers.
4187 * The formula for the exponential probability density function is
4188 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4190 * <table border=1 cellpadding=10 cellspacing=0>
4191 * <caption align=top>Distribution Statistics</caption>
4192 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4193 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4194 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4195 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4196 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4199 template<typename _RealType
= double>
4200 class exponential_distribution
4202 static_assert(std::is_floating_point
<_RealType
>::value
,
4203 "template argument not a floating point type");
4206 /** The type of the range of the distribution. */
4207 typedef _RealType result_type
;
4208 /** Parameter type. */
4211 typedef exponential_distribution
<_RealType
> distribution_type
;
4214 param_type(_RealType __lambda
= _RealType(1))
4215 : _M_lambda(__lambda
)
4217 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
4222 { return _M_lambda
; }
4225 operator==(const param_type
& __p1
, const param_type
& __p2
)
4226 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4229 _RealType _M_lambda
;
4234 * @brief Constructs an exponential distribution with inverse scale
4235 * parameter @f$\lambda@f$.
4238 exponential_distribution(const result_type
& __lambda
= result_type(1))
4239 : _M_param(__lambda
)
4243 exponential_distribution(const param_type
& __p
)
4248 * @brief Resets the distribution state.
4250 * Has no effect on exponential distributions.
4256 * @brief Returns the inverse scale parameter of the distribution.
4260 { return _M_param
.lambda(); }
4263 * @brief Returns the parameter set of the distribution.
4267 { return _M_param
; }
4270 * @brief Sets the parameter set of the distribution.
4271 * @param __param The new parameter set of the distribution.
4274 param(const param_type
& __param
)
4275 { _M_param
= __param
; }
4278 * @brief Returns the greatest lower bound value of the distribution.
4282 { return result_type(0); }
4285 * @brief Returns the least upper bound value of the distribution.
4289 { return std::numeric_limits
<result_type
>::max(); }
4292 * @brief Generating functions.
4294 template<typename _UniformRandomNumberGenerator
>
4296 operator()(_UniformRandomNumberGenerator
& __urng
)
4297 { return this->operator()(__urng
, this->param()); }
4299 template<typename _UniformRandomNumberGenerator
>
4301 operator()(_UniformRandomNumberGenerator
& __urng
,
4302 const param_type
& __p
)
4304 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4306 return -std::log(__aurng()) / __p
.lambda();
4310 param_type _M_param
;
4314 * @brief Return true if two exponential distributions have the same
4317 template<typename _RealType
>
4319 operator==(const std::exponential_distribution
<_RealType
>& __d1
,
4320 const std::exponential_distribution
<_RealType
>& __d2
)
4321 { return __d1
.param() == __d2
.param(); }
4324 * @brief Return true if two exponential distributions have different
4327 template<typename _RealType
>
4329 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4330 const std::exponential_distribution
<_RealType
>& __d2
)
4331 { return !(__d1
== __d2
); }
4334 * @brief Inserts a %exponential_distribution random number distribution
4335 * @p __x into the output stream @p __os.
4337 * @param __os An output stream.
4338 * @param __x A %exponential_distribution random number distribution.
4340 * @returns The output stream with the state of @p __x inserted or in
4343 template<typename _RealType
, typename _CharT
, typename _Traits
>
4344 std::basic_ostream
<_CharT
, _Traits
>&
4345 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4346 const std::exponential_distribution
<_RealType
>& __x
);
4349 * @brief Extracts a %exponential_distribution random number distribution
4350 * @p __x from the input stream @p __is.
4352 * @param __is An input stream.
4353 * @param __x A %exponential_distribution random number
4356 * @returns The input stream with @p __x extracted or in an error state.
4358 template<typename _RealType
, typename _CharT
, typename _Traits
>
4359 std::basic_istream
<_CharT
, _Traits
>&
4360 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4361 std::exponential_distribution
<_RealType
>& __x
);
4365 * @brief A weibull_distribution random number distribution.
4367 * The formula for the normal probability density function is:
4369 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4370 * \exp{(-(\frac{x}{\beta})^\alpha)}
4373 template<typename _RealType
= double>
4374 class weibull_distribution
4376 static_assert(std::is_floating_point
<_RealType
>::value
,
4377 "template argument not a floating point type");
4380 /** The type of the range of the distribution. */
4381 typedef _RealType result_type
;
4382 /** Parameter type. */
4385 typedef weibull_distribution
<_RealType
> distribution_type
;
4388 param_type(_RealType __a
= _RealType(1),
4389 _RealType __b
= _RealType(1))
4390 : _M_a(__a
), _M_b(__b
)
4402 operator==(const param_type
& __p1
, const param_type
& __p2
)
4403 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4411 weibull_distribution(_RealType __a
= _RealType(1),
4412 _RealType __b
= _RealType(1))
4413 : _M_param(__a
, __b
)
4417 weibull_distribution(const param_type
& __p
)
4422 * @brief Resets the distribution state.
4429 * @brief Return the @f$a@f$ parameter of the distribution.
4433 { return _M_param
.a(); }
4436 * @brief Return the @f$b@f$ parameter of the distribution.
4440 { return _M_param
.b(); }
4443 * @brief Returns the parameter set of the distribution.
4447 { return _M_param
; }
4450 * @brief Sets the parameter set of the distribution.
4451 * @param __param The new parameter set of the distribution.
4454 param(const param_type
& __param
)
4455 { _M_param
= __param
; }
4458 * @brief Returns the greatest lower bound value of the distribution.
4462 { return result_type(0); }
4465 * @brief Returns the least upper bound value of the distribution.
4469 { return std::numeric_limits
<result_type
>::max(); }
4472 * @brief Generating functions.
4474 template<typename _UniformRandomNumberGenerator
>
4476 operator()(_UniformRandomNumberGenerator
& __urng
)
4477 { return this->operator()(__urng
, this->param()); }
4479 template<typename _UniformRandomNumberGenerator
>
4481 operator()(_UniformRandomNumberGenerator
& __urng
,
4482 const param_type
& __p
);
4485 param_type _M_param
;
4489 * @brief Return true if two Weibull distributions have the same
4492 template<typename _RealType
>
4494 operator==(const std::weibull_distribution
<_RealType
>& __d1
,
4495 const std::weibull_distribution
<_RealType
>& __d2
)
4496 { return __d1
.param() == __d2
.param(); }
4499 * @brief Return true if two Weibull distributions have different
4502 template<typename _RealType
>
4504 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
4505 const std::weibull_distribution
<_RealType
>& __d2
)
4506 { return !(__d1
== __d2
); }
4509 * @brief Inserts a %weibull_distribution random number distribution
4510 * @p __x into the output stream @p __os.
4512 * @param __os An output stream.
4513 * @param __x A %weibull_distribution random number distribution.
4515 * @returns The output stream with the state of @p __x inserted or in
4518 template<typename _RealType
, typename _CharT
, typename _Traits
>
4519 std::basic_ostream
<_CharT
, _Traits
>&
4520 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4521 const std::weibull_distribution
<_RealType
>& __x
);
4524 * @brief Extracts a %weibull_distribution random number distribution
4525 * @p __x from the input stream @p __is.
4527 * @param __is An input stream.
4528 * @param __x A %weibull_distribution random number
4531 * @returns The input stream with @p __x extracted or in an error state.
4533 template<typename _RealType
, typename _CharT
, typename _Traits
>
4534 std::basic_istream
<_CharT
, _Traits
>&
4535 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4536 std::weibull_distribution
<_RealType
>& __x
);
4540 * @brief A extreme_value_distribution random number distribution.
4542 * The formula for the normal probability mass function is
4544 * p(x|a,b) = \frac{1}{b}
4545 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
4548 template<typename _RealType
= double>
4549 class extreme_value_distribution
4551 static_assert(std::is_floating_point
<_RealType
>::value
,
4552 "template argument not a floating point type");
4555 /** The type of the range of the distribution. */
4556 typedef _RealType result_type
;
4557 /** Parameter type. */
4560 typedef extreme_value_distribution
<_RealType
> distribution_type
;
4563 param_type(_RealType __a
= _RealType(0),
4564 _RealType __b
= _RealType(1))
4565 : _M_a(__a
), _M_b(__b
)
4577 operator==(const param_type
& __p1
, const param_type
& __p2
)
4578 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4586 extreme_value_distribution(_RealType __a
= _RealType(0),
4587 _RealType __b
= _RealType(1))
4588 : _M_param(__a
, __b
)
4592 extreme_value_distribution(const param_type
& __p
)
4597 * @brief Resets the distribution state.
4604 * @brief Return the @f$a@f$ parameter of the distribution.
4608 { return _M_param
.a(); }
4611 * @brief Return the @f$b@f$ parameter of the distribution.
4615 { return _M_param
.b(); }
4618 * @brief Returns the parameter set of the distribution.
4622 { return _M_param
; }
4625 * @brief Sets the parameter set of the distribution.
4626 * @param __param The new parameter set of the distribution.
4629 param(const param_type
& __param
)
4630 { _M_param
= __param
; }
4633 * @brief Returns the greatest lower bound value of the distribution.
4637 { return std::numeric_limits
<result_type
>::min(); }
4640 * @brief Returns the least upper bound value of the distribution.
4644 { return std::numeric_limits
<result_type
>::max(); }
4647 * @brief Generating functions.
4649 template<typename _UniformRandomNumberGenerator
>
4651 operator()(_UniformRandomNumberGenerator
& __urng
)
4652 { return this->operator()(__urng
, this->param()); }
4654 template<typename _UniformRandomNumberGenerator
>
4656 operator()(_UniformRandomNumberGenerator
& __urng
,
4657 const param_type
& __p
);
4660 param_type _M_param
;
4664 * @brief Return true if two extreme value distributions have the same
4667 template<typename _RealType
>
4669 operator==(const std::extreme_value_distribution
<_RealType
>& __d1
,
4670 const std::extreme_value_distribution
<_RealType
>& __d2
)
4671 { return __d1
.param() == __d2
.param(); }
4674 * @brief Return true if two extreme value distributions have different
4677 template<typename _RealType
>
4679 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
4680 const std::extreme_value_distribution
<_RealType
>& __d2
)
4681 { return !(__d1
== __d2
); }
4684 * @brief Inserts a %extreme_value_distribution random number distribution
4685 * @p __x into the output stream @p __os.
4687 * @param __os An output stream.
4688 * @param __x A %extreme_value_distribution random number distribution.
4690 * @returns The output stream with the state of @p __x inserted or in
4693 template<typename _RealType
, typename _CharT
, typename _Traits
>
4694 std::basic_ostream
<_CharT
, _Traits
>&
4695 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4696 const std::extreme_value_distribution
<_RealType
>& __x
);
4699 * @brief Extracts a %extreme_value_distribution random number
4700 * distribution @p __x from the input stream @p __is.
4702 * @param __is An input stream.
4703 * @param __x A %extreme_value_distribution random number
4706 * @returns The input stream with @p __x extracted or in an error state.
4708 template<typename _RealType
, typename _CharT
, typename _Traits
>
4709 std::basic_istream
<_CharT
, _Traits
>&
4710 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4711 std::extreme_value_distribution
<_RealType
>& __x
);
4715 * @brief A discrete_distribution random number distribution.
4717 * The formula for the discrete probability mass function is
4720 template<typename _IntType
= int>
4721 class discrete_distribution
4723 static_assert(std::is_integral
<_IntType
>::value
,
4724 "template argument not an integral type");
4727 /** The type of the range of the distribution. */
4728 typedef _IntType result_type
;
4729 /** Parameter type. */
4732 typedef discrete_distribution
<_IntType
> distribution_type
;
4733 friend class discrete_distribution
<_IntType
>;
4736 : _M_prob(), _M_cp()
4739 template<typename _InputIterator
>
4740 param_type(_InputIterator __wbegin
,
4741 _InputIterator __wend
)
4742 : _M_prob(__wbegin
, __wend
), _M_cp()
4743 { _M_initialize(); }
4745 param_type(initializer_list
<double> __wil
)
4746 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4747 { _M_initialize(); }
4749 template<typename _Func
>
4750 param_type(size_t __nw
, double __xmin
, double __xmax
,
4753 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4754 param_type(const param_type
&) = default;
4755 param_type
& operator=(const param_type
&) = default;
4758 probabilities() const
4759 { return _M_prob
.empty() ? std::vector
<double>(1, 1.0) : _M_prob
; }
4762 operator==(const param_type
& __p1
, const param_type
& __p2
)
4763 { return __p1
._M_prob
== __p2
._M_prob
; }
4769 std::vector
<double> _M_prob
;
4770 std::vector
<double> _M_cp
;
4773 discrete_distribution()
4777 template<typename _InputIterator
>
4778 discrete_distribution(_InputIterator __wbegin
,
4779 _InputIterator __wend
)
4780 : _M_param(__wbegin
, __wend
)
4783 discrete_distribution(initializer_list
<double> __wl
)
4787 template<typename _Func
>
4788 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4790 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4794 discrete_distribution(const param_type
& __p
)
4799 * @brief Resets the distribution state.
4806 * @brief Returns the probabilities of the distribution.
4809 probabilities() const
4811 return _M_param
._M_prob
.empty()
4812 ? std::vector
<double>(1, 1.0) : _M_param
._M_prob
;
4816 * @brief Returns the parameter set of the distribution.
4820 { return _M_param
; }
4823 * @brief Sets the parameter set of the distribution.
4824 * @param __param The new parameter set of the distribution.
4827 param(const param_type
& __param
)
4828 { _M_param
= __param
; }
4831 * @brief Returns the greatest lower bound value of the distribution.
4835 { return result_type(0); }
4838 * @brief Returns the least upper bound value of the distribution.
4843 return _M_param
._M_prob
.empty()
4844 ? result_type(0) : result_type(_M_param
._M_prob
.size() - 1);
4848 * @brief Generating functions.
4850 template<typename _UniformRandomNumberGenerator
>
4852 operator()(_UniformRandomNumberGenerator
& __urng
)
4853 { return this->operator()(__urng
, this->param()); }
4855 template<typename _UniformRandomNumberGenerator
>
4857 operator()(_UniformRandomNumberGenerator
& __urng
,
4858 const param_type
& __p
);
4861 * @brief Inserts a %discrete_distribution random number distribution
4862 * @p __x into the output stream @p __os.
4864 * @param __os An output stream.
4865 * @param __x A %discrete_distribution random number distribution.
4867 * @returns The output stream with the state of @p __x inserted or in
4870 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4871 friend std::basic_ostream
<_CharT
, _Traits
>&
4872 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4873 const std::discrete_distribution
<_IntType1
>& __x
);
4876 * @brief Extracts a %discrete_distribution random number distribution
4877 * @p __x from the input stream @p __is.
4879 * @param __is An input stream.
4880 * @param __x A %discrete_distribution random number
4883 * @returns The input stream with @p __x extracted or in an error
4886 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4887 friend std::basic_istream
<_CharT
, _Traits
>&
4888 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4889 std::discrete_distribution
<_IntType1
>& __x
);
4892 param_type _M_param
;
4896 * @brief Return true if two discrete distributions have the same
4899 template<typename _IntType
>
4901 operator==(const std::discrete_distribution
<_IntType
>& __d1
,
4902 const std::discrete_distribution
<_IntType
>& __d2
)
4903 { return __d1
.param() == __d2
.param(); }
4906 * @brief Return true if two discrete distributions have different
4909 template<typename _IntType
>
4911 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
4912 const std::discrete_distribution
<_IntType
>& __d2
)
4913 { return !(__d1
== __d2
); }
4917 * @brief A piecewise_constant_distribution random number distribution.
4919 * The formula for the piecewise constant probability mass function is
4922 template<typename _RealType
= double>
4923 class piecewise_constant_distribution
4925 static_assert(std::is_floating_point
<_RealType
>::value
,
4926 "template argument not a floating point type");
4929 /** The type of the range of the distribution. */
4930 typedef _RealType result_type
;
4931 /** Parameter type. */
4934 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4935 friend class piecewise_constant_distribution
<_RealType
>;
4938 : _M_int(), _M_den(), _M_cp()
4941 template<typename _InputIteratorB
, typename _InputIteratorW
>
4942 param_type(_InputIteratorB __bfirst
,
4943 _InputIteratorB __bend
,
4944 _InputIteratorW __wbegin
);
4946 template<typename _Func
>
4947 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
4949 template<typename _Func
>
4950 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4953 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4954 param_type(const param_type
&) = default;
4955 param_type
& operator=(const param_type
&) = default;
4957 std::vector
<_RealType
>
4962 std::vector
<_RealType
> __tmp(2);
4963 __tmp
[1] = _RealType(1);
4972 { return _M_den
.empty() ? std::vector
<double>(1, 1.0) : _M_den
; }
4975 operator==(const param_type
& __p1
, const param_type
& __p2
)
4976 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
4982 std::vector
<_RealType
> _M_int
;
4983 std::vector
<double> _M_den
;
4984 std::vector
<double> _M_cp
;
4988 piecewise_constant_distribution()
4992 template<typename _InputIteratorB
, typename _InputIteratorW
>
4993 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4994 _InputIteratorB __bend
,
4995 _InputIteratorW __wbegin
)
4996 : _M_param(__bfirst
, __bend
, __wbegin
)
4999 template<typename _Func
>
5000 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
5002 : _M_param(__bl
, __fw
)
5005 template<typename _Func
>
5006 piecewise_constant_distribution(size_t __nw
,
5007 _RealType __xmin
, _RealType __xmax
,
5009 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5013 piecewise_constant_distribution(const param_type
& __p
)
5018 * @brief Resets the distribution state.
5025 * @brief Returns a vector of the intervals.
5027 std::vector
<_RealType
>
5030 if (_M_param
._M_int
.empty())
5032 std::vector
<_RealType
> __tmp(2);
5033 __tmp
[1] = _RealType(1);
5037 return _M_param
._M_int
;
5041 * @brief Returns a vector of the probability densities.
5046 return _M_param
._M_den
.empty()
5047 ? std::vector
<double>(1, 1.0) : _M_param
._M_den
;
5051 * @brief Returns the parameter set of the distribution.
5055 { return _M_param
; }
5058 * @brief Sets the parameter set of the distribution.
5059 * @param __param The new parameter set of the distribution.
5062 param(const param_type
& __param
)
5063 { _M_param
= __param
; }
5066 * @brief Returns the greatest lower bound value of the distribution.
5071 return _M_param
._M_int
.empty()
5072 ? result_type(0) : _M_param
._M_int
.front();
5076 * @brief Returns the least upper bound value of the distribution.
5081 return _M_param
._M_int
.empty()
5082 ? result_type(1) : _M_param
._M_int
.back();
5086 * @brief Generating functions.
5088 template<typename _UniformRandomNumberGenerator
>
5090 operator()(_UniformRandomNumberGenerator
& __urng
)
5091 { return this->operator()(__urng
, this->param()); }
5093 template<typename _UniformRandomNumberGenerator
>
5095 operator()(_UniformRandomNumberGenerator
& __urng
,
5096 const param_type
& __p
);
5099 * @brief Inserts a %piecewise_constan_distribution random
5100 * number distribution @p __x into the output stream @p __os.
5102 * @param __os An output stream.
5103 * @param __x A %piecewise_constan_distribution random number
5106 * @returns The output stream with the state of @p __x inserted or in
5109 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5110 friend std::basic_ostream
<_CharT
, _Traits
>&
5111 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5112 const std::piecewise_constant_distribution
<_RealType1
>& __x
);
5115 * @brief Extracts a %piecewise_constan_distribution random
5116 * number distribution @p __x from the input stream @p __is.
5118 * @param __is An input stream.
5119 * @param __x A %piecewise_constan_distribution random number
5122 * @returns The input stream with @p __x extracted or in an error
5125 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5126 friend std::basic_istream
<_CharT
, _Traits
>&
5127 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5128 std::piecewise_constant_distribution
<_RealType1
>& __x
);
5131 param_type _M_param
;
5135 * @brief Return true if two piecewise constant distributions have the
5138 template<typename _RealType
>
5140 operator==(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5141 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5142 { return __d1
.param() == __d2
.param(); }
5145 * @brief Return true if two piecewise constant distributions have
5146 * different parameters.
5148 template<typename _RealType
>
5150 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5151 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5152 { return !(__d1
== __d2
); }
5156 * @brief A piecewise_linear_distribution random number distribution.
5158 * The formula for the piecewise linear probability mass function is
5161 template<typename _RealType
= double>
5162 class piecewise_linear_distribution
5164 static_assert(std::is_floating_point
<_RealType
>::value
,
5165 "template argument not a floating point type");
5168 /** The type of the range of the distribution. */
5169 typedef _RealType result_type
;
5170 /** Parameter type. */
5173 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5174 friend class piecewise_linear_distribution
<_RealType
>;
5177 : _M_int(), _M_den(), _M_cp(), _M_m()
5180 template<typename _InputIteratorB
, typename _InputIteratorW
>
5181 param_type(_InputIteratorB __bfirst
,
5182 _InputIteratorB __bend
,
5183 _InputIteratorW __wbegin
);
5185 template<typename _Func
>
5186 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5188 template<typename _Func
>
5189 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5192 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5193 param_type(const param_type
&) = default;
5194 param_type
& operator=(const param_type
&) = default;
5196 std::vector
<_RealType
>
5201 std::vector
<_RealType
> __tmp(2);
5202 __tmp
[1] = _RealType(1);
5211 { return _M_den
.empty() ? std::vector
<double>(2, 1.0) : _M_den
; }
5214 operator==(const param_type
& __p1
, const param_type
& __p2
)
5215 { return (__p1
._M_int
== __p2
._M_int
5216 && __p1
._M_den
== __p2
._M_den
); }
5222 std::vector
<_RealType
> _M_int
;
5223 std::vector
<double> _M_den
;
5224 std::vector
<double> _M_cp
;
5225 std::vector
<double> _M_m
;
5229 piecewise_linear_distribution()
5233 template<typename _InputIteratorB
, typename _InputIteratorW
>
5234 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5235 _InputIteratorB __bend
,
5236 _InputIteratorW __wbegin
)
5237 : _M_param(__bfirst
, __bend
, __wbegin
)
5240 template<typename _Func
>
5241 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5243 : _M_param(__bl
, __fw
)
5246 template<typename _Func
>
5247 piecewise_linear_distribution(size_t __nw
,
5248 _RealType __xmin
, _RealType __xmax
,
5250 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5254 piecewise_linear_distribution(const param_type
& __p
)
5259 * Resets the distribution state.
5266 * @brief Return the intervals of the distribution.
5268 std::vector
<_RealType
>
5271 if (_M_param
._M_int
.empty())
5273 std::vector
<_RealType
> __tmp(2);
5274 __tmp
[1] = _RealType(1);
5278 return _M_param
._M_int
;
5282 * @brief Return a vector of the probability densities of the
5288 return _M_param
._M_den
.empty()
5289 ? std::vector
<double>(2, 1.0) : _M_param
._M_den
;
5293 * @brief Returns the parameter set of the distribution.
5297 { return _M_param
; }
5300 * @brief Sets the parameter set of the distribution.
5301 * @param __param The new parameter set of the distribution.
5304 param(const param_type
& __param
)
5305 { _M_param
= __param
; }
5308 * @brief Returns the greatest lower bound value of the distribution.
5313 return _M_param
._M_int
.empty()
5314 ? result_type(0) : _M_param
._M_int
.front();
5318 * @brief Returns the least upper bound value of the distribution.
5323 return _M_param
._M_int
.empty()
5324 ? result_type(1) : _M_param
._M_int
.back();
5328 * @brief Generating functions.
5330 template<typename _UniformRandomNumberGenerator
>
5332 operator()(_UniformRandomNumberGenerator
& __urng
)
5333 { return this->operator()(__urng
, this->param()); }
5335 template<typename _UniformRandomNumberGenerator
>
5337 operator()(_UniformRandomNumberGenerator
& __urng
,
5338 const param_type
& __p
);
5341 * @brief Inserts a %piecewise_linear_distribution random number
5342 * distribution @p __x into the output stream @p __os.
5344 * @param __os An output stream.
5345 * @param __x A %piecewise_linear_distribution random number
5348 * @returns The output stream with the state of @p __x inserted or in
5351 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5352 friend std::basic_ostream
<_CharT
, _Traits
>&
5353 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5354 const std::piecewise_linear_distribution
<_RealType1
>& __x
);
5357 * @brief Extracts a %piecewise_linear_distribution random number
5358 * distribution @p __x from the input stream @p __is.
5360 * @param __is An input stream.
5361 * @param __x A %piecewise_linear_distribution random number
5364 * @returns The input stream with @p __x extracted or in an error
5367 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5368 friend std::basic_istream
<_CharT
, _Traits
>&
5369 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5370 std::piecewise_linear_distribution
<_RealType1
>& __x
);
5373 param_type _M_param
;
5377 * @brief Return true if two piecewise linear distributions have the
5380 template<typename _RealType
>
5382 operator==(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5383 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5384 { return __d1
.param() == __d2
.param(); }
5387 * @brief Return true if two piecewise linear distributions have
5388 * different parameters.
5390 template<typename _RealType
>
5392 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5393 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5394 { return !(__d1
== __d2
); }
5397 /* @} */ // group random_distributions_poisson
5399 /* @} */ // group random_distributions
5402 * @addtogroup random_utilities Random Number Utilities
5408 * @brief The seed_seq class generates sequences of seeds for random
5409 * number generators.
5415 /** The type of the seed vales. */
5416 typedef uint_least32_t result_type
;
5418 /** Default constructor. */
5423 template<typename _IntType
>
5424 seed_seq(std::initializer_list
<_IntType
> il
);
5426 template<typename _InputIterator
>
5427 seed_seq(_InputIterator __begin
, _InputIterator __end
);
5429 // generating functions
5430 template<typename _RandomAccessIterator
>
5432 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
5434 // property functions
5436 { return _M_v
.size(); }
5438 template<typename OutputIterator
>
5440 param(OutputIterator __dest
) const
5441 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
5445 std::vector
<result_type
> _M_v
;
5448 /* @} */ // group random_utilities
5450 /* @} */ // group random
5452 _GLIBCXX_END_NAMESPACE_VERSION