1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{random}
35 #include <bits/uniform_int_dist.h>
37 namespace std
_GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 // [26.4] Random number generation
44 * @defgroup random Random Number Generation
47 * A facility for generating random numbers on selected distributions.
52 * @brief A function template for converting the output of a (integral)
53 * uniform random number generator to a floatng point result in the range
56 template<typename _RealType
, size_t __bits
,
57 typename _UniformRandomNumberGenerator
>
59 generate_canonical(_UniformRandomNumberGenerator
& __g
);
62 * Implementation-space details.
66 template<typename _UIntType
, size_t __w
,
67 bool = __w
< static_cast<size_t>
68 (std::numeric_limits
<_UIntType
>::digits
)>
70 { static const _UIntType __value
= 0; };
72 template<typename _UIntType
, size_t __w
>
73 struct _Shift
<_UIntType
, __w
, true>
74 { static const _UIntType __value
= _UIntType(1) << __w
; };
77 int __which
= ((__s
<= __CHAR_BIT__
* sizeof (int))
78 + (__s
<= __CHAR_BIT__
* sizeof (long))
79 + (__s
<= __CHAR_BIT__
* sizeof (long long))
80 /* assume long long no bigger than __int128 */
82 struct _Select_uint_least_t
84 static_assert(__which
< 0, /* needs to be dependent */
85 "sorry, would be too much trouble for a slow result");
89 struct _Select_uint_least_t
<__s
, 4>
90 { typedef unsigned int type
; };
93 struct _Select_uint_least_t
<__s
, 3>
94 { typedef unsigned long type
; };
97 struct _Select_uint_least_t
<__s
, 2>
98 { typedef unsigned long long type
; };
100 #ifdef _GLIBCXX_USE_INT128
102 struct _Select_uint_least_t
<__s
, 1>
103 { typedef unsigned __int128 type
; };
106 // Assume a != 0, a < m, c < m, x < m.
107 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
,
108 bool __big_enough
= (!(__m
& (__m
- 1))
109 || (_Tp(-1) - __c
) / __a
>= __m
- 1),
110 bool __schrage_ok
= __m
% __a
< __m
/ __a
>
113 typedef typename _Select_uint_least_t
<std::__lg(__a
)
114 + std::__lg(__m
) + 2>::type _Tp2
;
117 { return static_cast<_Tp
>((_Tp2(__a
) * __x
+ __c
) % __m
); }
121 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
>
122 struct _Mod
<_Tp
, __m
, __a
, __c
, false, true>
129 // - for m == 2^n or m == 0, unsigned integer overflow is safe.
130 // - a * (m - 1) + c fits in _Tp, there is no overflow.
131 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool __s
>
132 struct _Mod
<_Tp
, __m
, __a
, __c
, true, __s
>
137 _Tp __res
= __a
* __x
+ __c
;
144 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
147 { return _Mod
<_Tp
, __m
, __a
, __c
>::__calc(__x
); }
150 * An adaptor class for converting the output of any Generator into
151 * the input for a specific Distribution.
153 template<typename _Engine
, typename _DInputType
>
156 static_assert(std::is_floating_point
<_DInputType
>::value
,
157 "template argument must be a floating point type");
160 _Adaptor(_Engine
& __g
)
165 { return _DInputType(0); }
169 { return _DInputType(1); }
172 * Converts a value generated by the adapted random number generator
173 * into a value in the input domain for the dependent random number
179 return std::generate_canonical
<_DInputType
,
180 std::numeric_limits
<_DInputType
>::digits
,
188 template<typename _Sseq
>
189 using __seed_seq_generate_t
= decltype(
190 std::declval
<_Sseq
&>().generate(std::declval
<uint_least32_t*>(),
191 std::declval
<uint_least32_t*>()));
193 // Detect whether _Sseq is a valid seed sequence for
194 // a random number engine _Engine with result type _Res.
195 template<typename _Sseq
, typename _Engine
, typename _Res
,
196 typename _GenerateCheck
= __seed_seq_generate_t
<_Sseq
>>
197 using __is_seed_seq
= __and_
<
198 __not_
<is_same
<__remove_cvref_t
<_Sseq
>, _Engine
>>,
199 is_unsigned
<typename
_Sseq::result_type
>,
200 __not_
<is_convertible
<_Sseq
, _Res
>>
203 } // namespace __detail
206 * @addtogroup random_generators Random Number Generators
209 * These classes define objects which provide random or pseudorandom
210 * numbers, either from a discrete or a continuous interval. The
211 * random number generator supplied as a part of this library are
212 * all uniform random number generators which provide a sequence of
213 * random number uniformly distributed over their range.
215 * A number generator is a function object with an operator() that
216 * takes zero arguments and returns a number.
218 * A compliant random number generator must satisfy the following
219 * requirements. <table border=1 cellpadding=10 cellspacing=0>
220 * <caption align=top>Random Number Generator Requirements</caption>
221 * <tr><td>To be documented.</td></tr> </table>
227 * @brief A model of a linear congruential random number generator.
229 * A random number generator that produces pseudorandom numbers via
232 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
235 * The template parameter @p _UIntType must be an unsigned integral type
236 * large enough to store values up to (__m-1). If the template parameter
237 * @p __m is 0, the modulus @p __m used is
238 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
239 * parameters @p __a and @p __c must be less than @p __m.
241 * The size of the state is @f$1@f$.
243 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
244 class linear_congruential_engine
246 static_assert(std::is_unsigned
<_UIntType
>::value
,
247 "result_type must be an unsigned integral type");
248 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
249 "template argument substituting __m out of bounds");
251 template<typename _Sseq
>
252 using _If_seed_seq
= typename enable_if
<__detail::__is_seed_seq
<
253 _Sseq
, linear_congruential_engine
, _UIntType
>::value
>::type
;
256 /** The type of the generated random value. */
257 typedef _UIntType result_type
;
259 /** The multiplier. */
260 static constexpr result_type multiplier
= __a
;
262 static constexpr result_type increment
= __c
;
264 static constexpr result_type modulus
= __m
;
265 static constexpr result_type default_seed
= 1u;
268 * @brief Constructs a %linear_congruential_engine random number
269 * generator engine with seed 1.
271 linear_congruential_engine() : linear_congruential_engine(default_seed
)
275 * @brief Constructs a %linear_congruential_engine random number
276 * generator engine with seed @p __s. The default seed value
279 * @param __s The initial seed value.
282 linear_congruential_engine(result_type __s
)
286 * @brief Constructs a %linear_congruential_engine random number
287 * generator engine seeded from the seed sequence @p __q.
289 * @param __q the seed sequence.
291 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
293 linear_congruential_engine(_Sseq
& __q
)
297 * @brief Reseeds the %linear_congruential_engine random number generator
298 * engine sequence to the seed @p __s.
300 * @param __s The new seed.
303 seed(result_type __s
= default_seed
);
306 * @brief Reseeds the %linear_congruential_engine random number generator
308 * sequence using values from the seed sequence @p __q.
310 * @param __q the seed sequence.
312 template<typename _Sseq
>
317 * @brief Gets the smallest possible value in the output range.
319 * The minimum depends on the @p __c parameter: if it is zero, the
320 * minimum generated must be > 0, otherwise 0 is allowed.
322 static constexpr result_type
324 { return __c
== 0u ? 1u : 0u; }
327 * @brief Gets the largest possible value in the output range.
329 static constexpr result_type
334 * @brief Discard a sequence of random numbers.
337 discard(unsigned long long __z
)
339 for (; __z
!= 0ULL; --__z
)
344 * @brief Gets the next random number in the sequence.
349 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
354 * @brief Compares two linear congruential random number generator
355 * objects of the same type for equality.
357 * @param __lhs A linear congruential random number generator object.
358 * @param __rhs Another linear congruential random number generator
361 * @returns true if the infinite sequences of generated values
362 * would be equal, false otherwise.
365 operator==(const linear_congruential_engine
& __lhs
,
366 const linear_congruential_engine
& __rhs
)
367 { return __lhs
._M_x
== __rhs
._M_x
; }
370 * @brief Writes the textual representation of the state x(i) of x to
373 * @param __os The output 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_ostream
<_CharT
, _Traits
>&
380 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
381 const std::linear_congruential_engine
<_UIntType1
,
382 __a1
, __c1
, __m1
>& __lcr
);
385 * @brief Sets the state of the engine by reading its textual
386 * representation from @p __is.
388 * The textual representation must have been previously written using
389 * an output stream whose imbued locale and whose type's template
390 * specialization arguments _CharT and _Traits were the same as those
393 * @param __is The input stream.
394 * @param __lcr A % linear_congruential_engine random number generator.
397 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
398 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
399 friend std::basic_istream
<_CharT
, _Traits
>&
400 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
401 std::linear_congruential_engine
<_UIntType1
, __a1
,
409 * @brief Compares two linear congruential random number generator
410 * objects of the same type for inequality.
412 * @param __lhs A linear congruential random number generator object.
413 * @param __rhs Another linear congruential random number generator
416 * @returns true if the infinite sequences of generated values
417 * would be different, false otherwise.
419 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
421 operator!=(const std::linear_congruential_engine
<_UIntType
, __a
,
423 const std::linear_congruential_engine
<_UIntType
, __a
,
425 { return !(__lhs
== __rhs
); }
429 * A generalized feedback shift register discrete random number generator.
431 * This algorithm avoids multiplication and division and is designed to be
432 * friendly to a pipelined architecture. If the parameters are chosen
433 * correctly, this generator will produce numbers with a very long period and
434 * fairly good apparent entropy, although still not cryptographically strong.
436 * The best way to use this generator is with the predefined mt19937 class.
438 * This algorithm was originally invented by Makoto Matsumoto and
441 * @tparam __w Word size, the number of bits in each element of
443 * @tparam __n The degree of recursion.
444 * @tparam __m The period parameter.
445 * @tparam __r The separation point bit index.
446 * @tparam __a The last row of the twist matrix.
447 * @tparam __u The first right-shift tempering matrix parameter.
448 * @tparam __d The first right-shift tempering matrix mask.
449 * @tparam __s The first left-shift tempering matrix parameter.
450 * @tparam __b The first left-shift tempering matrix mask.
451 * @tparam __t The second left-shift tempering matrix parameter.
452 * @tparam __c The second left-shift tempering matrix mask.
453 * @tparam __l The second right-shift tempering matrix parameter.
454 * @tparam __f Initialization multiplier.
456 template<typename _UIntType
, size_t __w
,
457 size_t __n
, size_t __m
, size_t __r
,
458 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
459 _UIntType __b
, size_t __t
,
460 _UIntType __c
, size_t __l
, _UIntType __f
>
461 class mersenne_twister_engine
463 static_assert(std::is_unsigned
<_UIntType
>::value
,
464 "result_type must be an unsigned integral type");
465 static_assert(1u <= __m
&& __m
<= __n
,
466 "template argument substituting __m out of bounds");
467 static_assert(__r
<= __w
, "template argument substituting "
469 static_assert(__u
<= __w
, "template argument substituting "
471 static_assert(__s
<= __w
, "template argument substituting "
473 static_assert(__t
<= __w
, "template argument substituting "
475 static_assert(__l
<= __w
, "template argument substituting "
477 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
478 "template argument substituting __w out of bound");
479 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
480 "template argument substituting __a out of bound");
481 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
482 "template argument substituting __b out of bound");
483 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
484 "template argument substituting __c out of bound");
485 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
486 "template argument substituting __d out of bound");
487 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
488 "template argument substituting __f out of bound");
490 template<typename _Sseq
>
491 using _If_seed_seq
= typename enable_if
<__detail::__is_seed_seq
<
492 _Sseq
, mersenne_twister_engine
, _UIntType
>::value
>::type
;
495 /** The type of the generated random value. */
496 typedef _UIntType result_type
;
499 static constexpr size_t word_size
= __w
;
500 static constexpr size_t state_size
= __n
;
501 static constexpr size_t shift_size
= __m
;
502 static constexpr size_t mask_bits
= __r
;
503 static constexpr result_type xor_mask
= __a
;
504 static constexpr size_t tempering_u
= __u
;
505 static constexpr result_type tempering_d
= __d
;
506 static constexpr size_t tempering_s
= __s
;
507 static constexpr result_type tempering_b
= __b
;
508 static constexpr size_t tempering_t
= __t
;
509 static constexpr result_type tempering_c
= __c
;
510 static constexpr size_t tempering_l
= __l
;
511 static constexpr result_type initialization_multiplier
= __f
;
512 static constexpr result_type default_seed
= 5489u;
514 // constructors and member functions
516 mersenne_twister_engine() : mersenne_twister_engine(default_seed
) { }
519 mersenne_twister_engine(result_type __sd
)
523 * @brief Constructs a %mersenne_twister_engine random number generator
524 * engine seeded from the seed sequence @p __q.
526 * @param __q the seed sequence.
528 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
530 mersenne_twister_engine(_Sseq
& __q
)
534 seed(result_type __sd
= default_seed
);
536 template<typename _Sseq
>
541 * @brief Gets the smallest possible value in the output range.
543 static constexpr result_type
548 * @brief Gets the largest possible value in the output range.
550 static constexpr result_type
552 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
555 * @brief Discard a sequence of random numbers.
558 discard(unsigned long long __z
);
564 * @brief Compares two % mersenne_twister_engine random number generator
565 * objects of the same type for equality.
567 * @param __lhs A % mersenne_twister_engine random number generator
569 * @param __rhs Another % mersenne_twister_engine random number
572 * @returns true if the infinite sequences of generated values
573 * would be equal, false otherwise.
576 operator==(const mersenne_twister_engine
& __lhs
,
577 const mersenne_twister_engine
& __rhs
)
578 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
)
579 && __lhs
._M_p
== __rhs
._M_p
); }
582 * @brief Inserts the current state of a % mersenne_twister_engine
583 * random number generator engine @p __x into the output stream
586 * @param __os An output stream.
587 * @param __x A % mersenne_twister_engine random number generator
590 * @returns The output stream with the state of @p __x inserted or in
593 template<typename _UIntType1
,
594 size_t __w1
, size_t __n1
,
595 size_t __m1
, size_t __r1
,
596 _UIntType1 __a1
, size_t __u1
,
597 _UIntType1 __d1
, size_t __s1
,
598 _UIntType1 __b1
, size_t __t1
,
599 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
600 typename _CharT
, typename _Traits
>
601 friend std::basic_ostream
<_CharT
, _Traits
>&
602 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
603 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
604 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
608 * @brief Extracts the current state of a % mersenne_twister_engine
609 * random number generator engine @p __x from the input stream
612 * @param __is An input stream.
613 * @param __x A % mersenne_twister_engine random number generator
616 * @returns The input stream with the state of @p __x extracted or in
619 template<typename _UIntType1
,
620 size_t __w1
, size_t __n1
,
621 size_t __m1
, size_t __r1
,
622 _UIntType1 __a1
, size_t __u1
,
623 _UIntType1 __d1
, size_t __s1
,
624 _UIntType1 __b1
, size_t __t1
,
625 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
626 typename _CharT
, typename _Traits
>
627 friend std::basic_istream
<_CharT
, _Traits
>&
628 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
629 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
630 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
636 _UIntType _M_x
[state_size
];
641 * @brief Compares two % mersenne_twister_engine random number generator
642 * objects of the same type for inequality.
644 * @param __lhs A % mersenne_twister_engine random number generator
646 * @param __rhs Another % mersenne_twister_engine random number
649 * @returns true if the infinite sequences of generated values
650 * would be different, false otherwise.
652 template<typename _UIntType
, size_t __w
,
653 size_t __n
, size_t __m
, size_t __r
,
654 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
655 _UIntType __b
, size_t __t
,
656 _UIntType __c
, size_t __l
, _UIntType __f
>
658 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
659 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
660 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
661 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
662 { return !(__lhs
== __rhs
); }
666 * @brief The Marsaglia-Zaman generator.
668 * This is a model of a Generalized Fibonacci discrete random number
669 * generator, sometimes referred to as the SWC generator.
671 * A discrete random number generator that produces pseudorandom
674 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
677 * The size of the state is @f$r@f$
678 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
680 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
681 class subtract_with_carry_engine
683 static_assert(std::is_unsigned
<_UIntType
>::value
,
684 "result_type must be an unsigned integral type");
685 static_assert(0u < __s
&& __s
< __r
,
687 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
688 "template argument substituting __w out of bounds");
690 template<typename _Sseq
>
691 using _If_seed_seq
= typename enable_if
<__detail::__is_seed_seq
<
692 _Sseq
, subtract_with_carry_engine
, _UIntType
>::value
>::type
;
695 /** The type of the generated random value. */
696 typedef _UIntType result_type
;
699 static constexpr size_t word_size
= __w
;
700 static constexpr size_t short_lag
= __s
;
701 static constexpr size_t long_lag
= __r
;
702 static constexpr result_type default_seed
= 19780503u;
704 subtract_with_carry_engine() : subtract_with_carry_engine(default_seed
)
708 * @brief Constructs an explicitly seeded %subtract_with_carry_engine
709 * random number generator.
712 subtract_with_carry_engine(result_type __sd
)
716 * @brief Constructs a %subtract_with_carry_engine random number engine
717 * seeded from the seed sequence @p __q.
719 * @param __q the seed sequence.
721 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
723 subtract_with_carry_engine(_Sseq
& __q
)
727 * @brief Seeds the initial state @f$x_0@f$ of the random number
730 * N1688[4.19] modifies this as follows. If @p __value == 0,
731 * sets value to 19780503. In any case, with a linear
732 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
733 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
734 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
735 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
736 * set carry to 1, otherwise sets carry to 0.
739 seed(result_type __sd
= default_seed
);
742 * @brief Seeds the initial state @f$x_0@f$ of the
743 * % subtract_with_carry_engine random number generator.
745 template<typename _Sseq
>
750 * @brief Gets the inclusive minimum value of the range of random
751 * integers returned by this generator.
753 static constexpr result_type
758 * @brief Gets the inclusive maximum value of the range of random
759 * integers returned by this generator.
761 static constexpr result_type
763 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
766 * @brief Discard a sequence of random numbers.
769 discard(unsigned long long __z
)
771 for (; __z
!= 0ULL; --__z
)
776 * @brief Gets the next random number in the sequence.
782 * @brief Compares two % subtract_with_carry_engine random number
783 * generator objects of the same type for equality.
785 * @param __lhs A % subtract_with_carry_engine random number generator
787 * @param __rhs Another % subtract_with_carry_engine random number
790 * @returns true if the infinite sequences of generated values
791 * would be equal, false otherwise.
794 operator==(const subtract_with_carry_engine
& __lhs
,
795 const subtract_with_carry_engine
& __rhs
)
796 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
)
797 && __lhs
._M_carry
== __rhs
._M_carry
798 && __lhs
._M_p
== __rhs
._M_p
); }
801 * @brief Inserts the current state of a % subtract_with_carry_engine
802 * random number generator engine @p __x into the output stream
805 * @param __os An output stream.
806 * @param __x A % subtract_with_carry_engine random number generator
809 * @returns The output stream with the state of @p __x inserted or in
812 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
813 typename _CharT
, typename _Traits
>
814 friend std::basic_ostream
<_CharT
, _Traits
>&
815 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
816 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
820 * @brief Extracts the current state of a % subtract_with_carry_engine
821 * random number generator engine @p __x from the input stream
824 * @param __is An input stream.
825 * @param __x A % subtract_with_carry_engine random number generator
828 * @returns The input stream with the state of @p __x extracted or in
831 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
832 typename _CharT
, typename _Traits
>
833 friend std::basic_istream
<_CharT
, _Traits
>&
834 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
835 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
839 /// The state of the generator. This is a ring buffer.
840 _UIntType _M_x
[long_lag
];
841 _UIntType _M_carry
; ///< The carry
842 size_t _M_p
; ///< Current index of x(i - r).
846 * @brief Compares two % subtract_with_carry_engine random number
847 * generator objects of the same type for inequality.
849 * @param __lhs A % subtract_with_carry_engine random number generator
851 * @param __rhs Another % subtract_with_carry_engine random number
854 * @returns true if the infinite sequences of generated values
855 * would be different, false otherwise.
857 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
859 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
861 const std::subtract_with_carry_engine
<_UIntType
, __w
,
863 { return !(__lhs
== __rhs
); }
867 * Produces random numbers from some base engine by discarding blocks of
870 * 0 <= @p __r <= @p __p
872 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
873 class discard_block_engine
875 static_assert(1 <= __r
&& __r
<= __p
,
876 "template argument substituting __r out of bounds");
879 /** The type of the generated random value. */
880 typedef typename
_RandomNumberEngine::result_type result_type
;
882 template<typename _Sseq
>
883 using _If_seed_seq
= typename enable_if
<__detail::__is_seed_seq
<
884 _Sseq
, discard_block_engine
, result_type
>::value
>::type
;
887 static constexpr size_t block_size
= __p
;
888 static constexpr size_t used_block
= __r
;
891 * @brief Constructs a default %discard_block_engine engine.
893 * The underlying engine is default constructed as well.
895 discard_block_engine()
896 : _M_b(), _M_n(0) { }
899 * @brief Copy constructs a %discard_block_engine engine.
901 * Copies an existing base class random number generator.
902 * @param __rng An existing (base class) engine object.
905 discard_block_engine(const _RandomNumberEngine
& __rng
)
906 : _M_b(__rng
), _M_n(0) { }
909 * @brief Move constructs a %discard_block_engine engine.
911 * Copies an existing base class random number generator.
912 * @param __rng An existing (base class) engine object.
915 discard_block_engine(_RandomNumberEngine
&& __rng
)
916 : _M_b(std::move(__rng
)), _M_n(0) { }
919 * @brief Seed constructs a %discard_block_engine engine.
921 * Constructs the underlying generator engine seeded with @p __s.
922 * @param __s A seed value for the base class engine.
925 discard_block_engine(result_type __s
)
926 : _M_b(__s
), _M_n(0) { }
929 * @brief Generator construct a %discard_block_engine engine.
931 * @param __q A seed sequence.
933 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
935 discard_block_engine(_Sseq
& __q
)
940 * @brief Reseeds the %discard_block_engine object with the default
941 * seed for the underlying base class generator engine.
951 * @brief Reseeds the %discard_block_engine object with the default
952 * seed for the underlying base class generator engine.
955 seed(result_type __s
)
962 * @brief Reseeds the %discard_block_engine object with the given seed
964 * @param __q A seed generator function.
966 template<typename _Sseq
>
975 * @brief Gets a const reference to the underlying generator engine
978 const _RandomNumberEngine
&
979 base() const noexcept
983 * @brief Gets the minimum value in the generated random number range.
985 static constexpr result_type
987 { return _RandomNumberEngine::min(); }
990 * @brief Gets the maximum value in the generated random number range.
992 static constexpr result_type
994 { return _RandomNumberEngine::max(); }
997 * @brief Discard a sequence of random numbers.
1000 discard(unsigned long long __z
)
1002 for (; __z
!= 0ULL; --__z
)
1007 * @brief Gets the next value in the generated random number sequence.
1013 * @brief Compares two %discard_block_engine random number generator
1014 * objects of the same type for equality.
1016 * @param __lhs A %discard_block_engine random number generator object.
1017 * @param __rhs Another %discard_block_engine random number generator
1020 * @returns true if the infinite sequences of generated values
1021 * would be equal, false otherwise.
1024 operator==(const discard_block_engine
& __lhs
,
1025 const discard_block_engine
& __rhs
)
1026 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
1029 * @brief Inserts the current state of a %discard_block_engine random
1030 * number generator engine @p __x into the output stream
1033 * @param __os An output stream.
1034 * @param __x A %discard_block_engine random number generator engine.
1036 * @returns The output stream with the state of @p __x inserted or in
1039 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1040 typename _CharT
, typename _Traits
>
1041 friend std::basic_ostream
<_CharT
, _Traits
>&
1042 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1043 const std::discard_block_engine
<_RandomNumberEngine1
,
1047 * @brief Extracts the current state of a % subtract_with_carry_engine
1048 * random number generator engine @p __x from the input stream
1051 * @param __is An input stream.
1052 * @param __x A %discard_block_engine random number generator engine.
1054 * @returns The input stream with the state of @p __x extracted or in
1057 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1058 typename _CharT
, typename _Traits
>
1059 friend std::basic_istream
<_CharT
, _Traits
>&
1060 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1061 std::discard_block_engine
<_RandomNumberEngine1
,
1065 _RandomNumberEngine _M_b
;
1070 * @brief Compares two %discard_block_engine random number generator
1071 * objects of the same type for inequality.
1073 * @param __lhs A %discard_block_engine random number generator object.
1074 * @param __rhs Another %discard_block_engine random number generator
1077 * @returns true if the infinite sequences of generated values
1078 * would be different, false otherwise.
1080 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
1082 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1084 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1086 { return !(__lhs
== __rhs
); }
1090 * Produces random numbers by combining random numbers from some base
1091 * engine to produce random numbers with a specifies number of bits @p __w.
1093 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1094 class independent_bits_engine
1096 static_assert(std::is_unsigned
<_UIntType
>::value
,
1097 "result_type must be an unsigned integral type");
1098 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
1099 "template argument substituting __w out of bounds");
1101 template<typename _Sseq
>
1102 using _If_seed_seq
= typename enable_if
<__detail::__is_seed_seq
<
1103 _Sseq
, independent_bits_engine
, _UIntType
>::value
>::type
;
1106 /** The type of the generated random value. */
1107 typedef _UIntType result_type
;
1110 * @brief Constructs a default %independent_bits_engine engine.
1112 * The underlying engine is default constructed as well.
1114 independent_bits_engine()
1118 * @brief Copy constructs a %independent_bits_engine engine.
1120 * Copies an existing base class random number generator.
1121 * @param __rng An existing (base class) engine object.
1124 independent_bits_engine(const _RandomNumberEngine
& __rng
)
1128 * @brief Move constructs a %independent_bits_engine engine.
1130 * Copies an existing base class random number generator.
1131 * @param __rng An existing (base class) engine object.
1134 independent_bits_engine(_RandomNumberEngine
&& __rng
)
1135 : _M_b(std::move(__rng
)) { }
1138 * @brief Seed constructs a %independent_bits_engine engine.
1140 * Constructs the underlying generator engine seeded with @p __s.
1141 * @param __s A seed value for the base class engine.
1144 independent_bits_engine(result_type __s
)
1148 * @brief Generator construct a %independent_bits_engine engine.
1150 * @param __q A seed sequence.
1152 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
1154 independent_bits_engine(_Sseq
& __q
)
1159 * @brief Reseeds the %independent_bits_engine object with the default
1160 * seed for the underlying base class generator engine.
1167 * @brief Reseeds the %independent_bits_engine object with the default
1168 * seed for the underlying base class generator engine.
1171 seed(result_type __s
)
1175 * @brief Reseeds the %independent_bits_engine object with the given
1177 * @param __q A seed generator function.
1179 template<typename _Sseq
>
1185 * @brief Gets a const reference to the underlying generator engine
1188 const _RandomNumberEngine
&
1189 base() const noexcept
1193 * @brief Gets the minimum value in the generated random number range.
1195 static constexpr result_type
1200 * @brief Gets the maximum value in the generated random number range.
1202 static constexpr result_type
1204 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1207 * @brief Discard a sequence of random numbers.
1210 discard(unsigned long long __z
)
1212 for (; __z
!= 0ULL; --__z
)
1217 * @brief Gets the next value in the generated random number sequence.
1223 * @brief Compares two %independent_bits_engine random number generator
1224 * objects of the same type for equality.
1226 * @param __lhs A %independent_bits_engine random number generator
1228 * @param __rhs Another %independent_bits_engine random number generator
1231 * @returns true if the infinite sequences of generated values
1232 * would be equal, false otherwise.
1235 operator==(const independent_bits_engine
& __lhs
,
1236 const independent_bits_engine
& __rhs
)
1237 { return __lhs
._M_b
== __rhs
._M_b
; }
1240 * @brief Extracts the current state of a % subtract_with_carry_engine
1241 * random number generator engine @p __x from the input stream
1244 * @param __is An input stream.
1245 * @param __x A %independent_bits_engine random number generator
1248 * @returns The input stream with the state of @p __x extracted or in
1251 template<typename _CharT
, typename _Traits
>
1252 friend std::basic_istream
<_CharT
, _Traits
>&
1253 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1254 std::independent_bits_engine
<_RandomNumberEngine
,
1255 __w
, _UIntType
>& __x
)
1262 _RandomNumberEngine _M_b
;
1266 * @brief Compares two %independent_bits_engine random number generator
1267 * objects of the same type for inequality.
1269 * @param __lhs A %independent_bits_engine random number generator
1271 * @param __rhs Another %independent_bits_engine random number generator
1274 * @returns true if the infinite sequences of generated values
1275 * would be different, false otherwise.
1277 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1279 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1281 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1283 { return !(__lhs
== __rhs
); }
1286 * @brief Inserts the current state of a %independent_bits_engine random
1287 * number generator engine @p __x into the output stream @p __os.
1289 * @param __os An output stream.
1290 * @param __x A %independent_bits_engine random number generator engine.
1292 * @returns The output stream with the state of @p __x inserted or in
1295 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1296 typename _CharT
, typename _Traits
>
1297 std::basic_ostream
<_CharT
, _Traits
>&
1298 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1299 const std::independent_bits_engine
<_RandomNumberEngine
,
1300 __w
, _UIntType
>& __x
)
1308 * @brief Produces random numbers by combining random numbers from some
1309 * base engine to produce random numbers with a specifies number of bits
1312 template<typename _RandomNumberEngine
, size_t __k
>
1313 class shuffle_order_engine
1315 static_assert(1u <= __k
, "template argument substituting "
1316 "__k out of bound");
1319 /** The type of the generated random value. */
1320 typedef typename
_RandomNumberEngine::result_type result_type
;
1322 template<typename _Sseq
>
1323 using _If_seed_seq
= typename enable_if
<__detail::__is_seed_seq
<
1324 _Sseq
, shuffle_order_engine
, result_type
>::value
>::type
;
1326 static constexpr size_t table_size
= __k
;
1329 * @brief Constructs a default %shuffle_order_engine engine.
1331 * The underlying engine is default constructed as well.
1333 shuffle_order_engine()
1335 { _M_initialize(); }
1338 * @brief Copy constructs a %shuffle_order_engine engine.
1340 * Copies an existing base class random number generator.
1341 * @param __rng An existing (base class) engine object.
1344 shuffle_order_engine(const _RandomNumberEngine
& __rng
)
1346 { _M_initialize(); }
1349 * @brief Move constructs a %shuffle_order_engine engine.
1351 * Copies an existing base class random number generator.
1352 * @param __rng An existing (base class) engine object.
1355 shuffle_order_engine(_RandomNumberEngine
&& __rng
)
1356 : _M_b(std::move(__rng
))
1357 { _M_initialize(); }
1360 * @brief Seed constructs a %shuffle_order_engine engine.
1362 * Constructs the underlying generator engine seeded with @p __s.
1363 * @param __s A seed value for the base class engine.
1366 shuffle_order_engine(result_type __s
)
1368 { _M_initialize(); }
1371 * @brief Generator construct a %shuffle_order_engine engine.
1373 * @param __q A seed sequence.
1375 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
1377 shuffle_order_engine(_Sseq
& __q
)
1379 { _M_initialize(); }
1382 * @brief Reseeds the %shuffle_order_engine object with the default seed
1383 for the underlying base class generator engine.
1393 * @brief Reseeds the %shuffle_order_engine object with the default seed
1394 * for the underlying base class generator engine.
1397 seed(result_type __s
)
1404 * @brief Reseeds the %shuffle_order_engine object with the given seed
1406 * @param __q A seed generator function.
1408 template<typename _Sseq
>
1417 * Gets a const reference to the underlying generator engine object.
1419 const _RandomNumberEngine
&
1420 base() const noexcept
1424 * Gets the minimum value in the generated random number range.
1426 static constexpr result_type
1428 { return _RandomNumberEngine::min(); }
1431 * Gets the maximum value in the generated random number range.
1433 static constexpr result_type
1435 { return _RandomNumberEngine::max(); }
1438 * Discard a sequence of random numbers.
1441 discard(unsigned long long __z
)
1443 for (; __z
!= 0ULL; --__z
)
1448 * Gets the next value in the generated random number sequence.
1454 * Compares two %shuffle_order_engine random number generator objects
1455 * of the same type for equality.
1457 * @param __lhs A %shuffle_order_engine random number generator object.
1458 * @param __rhs Another %shuffle_order_engine random number generator
1461 * @returns true if the infinite sequences of generated values
1462 * would be equal, false otherwise.
1465 operator==(const shuffle_order_engine
& __lhs
,
1466 const shuffle_order_engine
& __rhs
)
1467 { return (__lhs
._M_b
== __rhs
._M_b
1468 && std::equal(__lhs
._M_v
, __lhs
._M_v
+ __k
, __rhs
._M_v
)
1469 && __lhs
._M_y
== __rhs
._M_y
); }
1472 * @brief Inserts the current state of a %shuffle_order_engine random
1473 * number generator engine @p __x into the output stream
1476 * @param __os An output stream.
1477 * @param __x A %shuffle_order_engine random number generator engine.
1479 * @returns The output stream with the state of @p __x inserted or in
1482 template<typename _RandomNumberEngine1
, size_t __k1
,
1483 typename _CharT
, typename _Traits
>
1484 friend std::basic_ostream
<_CharT
, _Traits
>&
1485 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1486 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1490 * @brief Extracts the current state of a % subtract_with_carry_engine
1491 * random number generator engine @p __x from the input stream
1494 * @param __is An input stream.
1495 * @param __x A %shuffle_order_engine random number generator engine.
1497 * @returns The input stream with the state of @p __x extracted or in
1500 template<typename _RandomNumberEngine1
, size_t __k1
,
1501 typename _CharT
, typename _Traits
>
1502 friend std::basic_istream
<_CharT
, _Traits
>&
1503 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1504 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>& __x
);
1507 void _M_initialize()
1509 for (size_t __i
= 0; __i
< __k
; ++__i
)
1514 _RandomNumberEngine _M_b
;
1515 result_type _M_v
[__k
];
1520 * Compares two %shuffle_order_engine random number generator objects
1521 * of the same type for inequality.
1523 * @param __lhs A %shuffle_order_engine random number generator object.
1524 * @param __rhs Another %shuffle_order_engine random number generator
1527 * @returns true if the infinite sequences of generated values
1528 * would be different, false otherwise.
1530 template<typename _RandomNumberEngine
, size_t __k
>
1532 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1534 const std::shuffle_order_engine
<_RandomNumberEngine
,
1536 { return !(__lhs
== __rhs
); }
1540 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1542 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1546 * An alternative LCR (Lehmer Generator function).
1548 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1552 * The classic Mersenne Twister.
1555 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1556 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1557 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1559 typedef mersenne_twister_engine
<
1565 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1568 * An alternative Mersenne Twister.
1570 typedef mersenne_twister_engine
<
1573 0xb5026f5aa96619e9ULL
, 29,
1574 0x5555555555555555ULL
, 17,
1575 0x71d67fffeda60000ULL
, 37,
1576 0xfff7eee000000000ULL
, 43,
1577 6364136223846793005ULL> mt19937_64
;
1579 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1582 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1585 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1587 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1589 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1591 typedef minstd_rand0 default_random_engine
;
1594 * A standard interface to a platform-specific non-deterministic
1595 * random number generator (if any are available).
1600 /** The type of the generated random value. */
1601 typedef unsigned int result_type
;
1603 // constructors, destructors and member functions
1605 #ifdef _GLIBCXX_USE_RANDOM_TR1
1606 random_device() { _M_init("default"); }
1609 random_device(const std::string
& __token
) { _M_init(__token
); }
1614 random_device() { _M_init("mt19937"); }
1617 random_device(const std::string
& __token
)
1618 { _M_init_pretr1(__token
); }
1621 static constexpr result_type
1623 { return std::numeric_limits
<result_type
>::min(); }
1625 static constexpr result_type
1627 { return std::numeric_limits
<result_type
>::max(); }
1630 entropy() const noexcept
1632 #ifdef _GLIBCXX_USE_RANDOM_TR1
1633 return this->_M_getentropy();
1642 #ifdef _GLIBCXX_USE_RANDOM_TR1
1643 return this->_M_getval();
1645 return this->_M_getval_pretr1();
1649 // No copy functions.
1650 random_device(const random_device
&) = delete;
1651 void operator=(const random_device
&) = delete;
1655 void _M_init(const std::string
& __token
);
1656 void _M_init_pretr1(const std::string
& __token
);
1659 result_type
_M_getval();
1660 result_type
_M_getval_pretr1();
1661 double _M_getentropy() const noexcept
;
1670 /* @} */ // group random_generators
1673 * @addtogroup random_distributions Random Number Distributions
1679 * @addtogroup random_distributions_uniform Uniform Distributions
1680 * @ingroup random_distributions
1684 // std::uniform_int_distribution is defined in <bits/uniform_int_dist.h>
1687 * @brief Return true if two uniform integer distributions have
1688 * different parameters.
1690 template<typename _IntType
>
1692 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1693 const std::uniform_int_distribution
<_IntType
>& __d2
)
1694 { return !(__d1
== __d2
); }
1697 * @brief Inserts a %uniform_int_distribution random number
1698 * distribution @p __x into the output stream @p os.
1700 * @param __os An output stream.
1701 * @param __x A %uniform_int_distribution random number distribution.
1703 * @returns The output stream with the state of @p __x inserted or in
1706 template<typename _IntType
, typename _CharT
, typename _Traits
>
1707 std::basic_ostream
<_CharT
, _Traits
>&
1708 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1709 const std::uniform_int_distribution
<_IntType
>&);
1712 * @brief Extracts a %uniform_int_distribution random number distribution
1713 * @p __x from the input stream @p __is.
1715 * @param __is An input stream.
1716 * @param __x A %uniform_int_distribution random number generator engine.
1718 * @returns The input stream with @p __x extracted or in an error state.
1720 template<typename _IntType
, typename _CharT
, typename _Traits
>
1721 std::basic_istream
<_CharT
, _Traits
>&
1722 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1723 std::uniform_int_distribution
<_IntType
>&);
1727 * @brief Uniform continuous distribution for random numbers.
1729 * A continuous random distribution on the range [min, max) with equal
1730 * probability throughout the range. The URNG should be real-valued and
1731 * deliver number in the range [0, 1).
1733 template<typename _RealType
= double>
1734 class uniform_real_distribution
1736 static_assert(std::is_floating_point
<_RealType
>::value
,
1737 "result_type must be a floating point type");
1740 /** The type of the range of the distribution. */
1741 typedef _RealType result_type
;
1743 /** Parameter type. */
1746 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1748 param_type() : param_type(0) { }
1751 param_type(_RealType __a
, _RealType __b
= _RealType(1))
1752 : _M_a(__a
), _M_b(__b
)
1754 __glibcxx_assert(_M_a
<= _M_b
);
1766 operator==(const param_type
& __p1
, const param_type
& __p2
)
1767 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1770 operator!=(const param_type
& __p1
, const param_type
& __p2
)
1771 { return !(__p1
== __p2
); }
1780 * @brief Constructs a uniform_real_distribution object.
1782 * The lower bound is set to 0.0 and the upper bound to 1.0
1784 uniform_real_distribution() : uniform_real_distribution(0.0) { }
1787 * @brief Constructs a uniform_real_distribution object.
1789 * @param __a [IN] The lower bound of the distribution.
1790 * @param __b [IN] The upper bound of the distribution.
1793 uniform_real_distribution(_RealType __a
, _RealType __b
= _RealType(1))
1794 : _M_param(__a
, __b
)
1798 uniform_real_distribution(const param_type
& __p
)
1803 * @brief Resets the distribution state.
1805 * Does nothing for the uniform real distribution.
1812 { return _M_param
.a(); }
1816 { return _M_param
.b(); }
1819 * @brief Returns the parameter set of the distribution.
1823 { return _M_param
; }
1826 * @brief Sets the parameter set of the distribution.
1827 * @param __param The new parameter set of the distribution.
1830 param(const param_type
& __param
)
1831 { _M_param
= __param
; }
1834 * @brief Returns the inclusive lower bound of the distribution range.
1838 { return this->a(); }
1841 * @brief Returns the inclusive upper bound of the distribution range.
1845 { return this->b(); }
1848 * @brief Generating functions.
1850 template<typename _UniformRandomNumberGenerator
>
1852 operator()(_UniformRandomNumberGenerator
& __urng
)
1853 { return this->operator()(__urng
, _M_param
); }
1855 template<typename _UniformRandomNumberGenerator
>
1857 operator()(_UniformRandomNumberGenerator
& __urng
,
1858 const param_type
& __p
)
1860 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1862 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1865 template<typename _ForwardIterator
,
1866 typename _UniformRandomNumberGenerator
>
1868 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1869 _UniformRandomNumberGenerator
& __urng
)
1870 { this->__generate(__f
, __t
, __urng
, _M_param
); }
1872 template<typename _ForwardIterator
,
1873 typename _UniformRandomNumberGenerator
>
1875 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1876 _UniformRandomNumberGenerator
& __urng
,
1877 const param_type
& __p
)
1878 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1880 template<typename _UniformRandomNumberGenerator
>
1882 __generate(result_type
* __f
, result_type
* __t
,
1883 _UniformRandomNumberGenerator
& __urng
,
1884 const param_type
& __p
)
1885 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1888 * @brief Return true if two uniform real distributions have
1889 * the same parameters.
1892 operator==(const uniform_real_distribution
& __d1
,
1893 const uniform_real_distribution
& __d2
)
1894 { return __d1
._M_param
== __d2
._M_param
; }
1897 template<typename _ForwardIterator
,
1898 typename _UniformRandomNumberGenerator
>
1900 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
1901 _UniformRandomNumberGenerator
& __urng
,
1902 const param_type
& __p
);
1904 param_type _M_param
;
1908 * @brief Return true if two uniform real distributions have
1909 * different parameters.
1911 template<typename _IntType
>
1913 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1914 const std::uniform_real_distribution
<_IntType
>& __d2
)
1915 { return !(__d1
== __d2
); }
1918 * @brief Inserts a %uniform_real_distribution random number
1919 * distribution @p __x into the output stream @p __os.
1921 * @param __os An output stream.
1922 * @param __x A %uniform_real_distribution random number distribution.
1924 * @returns The output stream with the state of @p __x inserted or in
1927 template<typename _RealType
, typename _CharT
, typename _Traits
>
1928 std::basic_ostream
<_CharT
, _Traits
>&
1929 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1930 const std::uniform_real_distribution
<_RealType
>&);
1933 * @brief Extracts a %uniform_real_distribution random number distribution
1934 * @p __x from the input stream @p __is.
1936 * @param __is An input stream.
1937 * @param __x A %uniform_real_distribution random number generator engine.
1939 * @returns The input stream with @p __x extracted or in an error state.
1941 template<typename _RealType
, typename _CharT
, typename _Traits
>
1942 std::basic_istream
<_CharT
, _Traits
>&
1943 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1944 std::uniform_real_distribution
<_RealType
>&);
1946 /* @} */ // group random_distributions_uniform
1949 * @addtogroup random_distributions_normal Normal Distributions
1950 * @ingroup random_distributions
1955 * @brief A normal continuous distribution for random numbers.
1957 * The formula for the normal probability density function is
1959 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1960 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1963 template<typename _RealType
= double>
1964 class normal_distribution
1966 static_assert(std::is_floating_point
<_RealType
>::value
,
1967 "result_type must be a floating point type");
1970 /** The type of the range of the distribution. */
1971 typedef _RealType result_type
;
1973 /** Parameter type. */
1976 typedef normal_distribution
<_RealType
> distribution_type
;
1978 param_type() : param_type(0.0) { }
1981 param_type(_RealType __mean
, _RealType __stddev
= _RealType(1))
1982 : _M_mean(__mean
), _M_stddev(__stddev
)
1984 __glibcxx_assert(_M_stddev
> _RealType(0));
1993 { return _M_stddev
; }
1996 operator==(const param_type
& __p1
, const param_type
& __p2
)
1997 { return (__p1
._M_mean
== __p2
._M_mean
1998 && __p1
._M_stddev
== __p2
._M_stddev
); }
2001 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2002 { return !(__p1
== __p2
); }
2006 _RealType _M_stddev
;
2010 normal_distribution() : normal_distribution(0.0) { }
2013 * Constructs a normal distribution with parameters @f$mean@f$ and
2014 * standard deviation.
2017 normal_distribution(result_type __mean
,
2018 result_type __stddev
= result_type(1))
2019 : _M_param(__mean
, __stddev
), _M_saved_available(false)
2023 normal_distribution(const param_type
& __p
)
2024 : _M_param(__p
), _M_saved_available(false)
2028 * @brief Resets the distribution state.
2032 { _M_saved_available
= false; }
2035 * @brief Returns the mean of the distribution.
2039 { return _M_param
.mean(); }
2042 * @brief Returns the standard deviation of the distribution.
2046 { return _M_param
.stddev(); }
2049 * @brief Returns the parameter set of the distribution.
2053 { return _M_param
; }
2056 * @brief Sets the parameter set of the distribution.
2057 * @param __param The new parameter set of the distribution.
2060 param(const param_type
& __param
)
2061 { _M_param
= __param
; }
2064 * @brief Returns the greatest lower bound value of the distribution.
2068 { return std::numeric_limits
<result_type
>::lowest(); }
2071 * @brief Returns the least upper bound value of the distribution.
2075 { return std::numeric_limits
<result_type
>::max(); }
2078 * @brief Generating functions.
2080 template<typename _UniformRandomNumberGenerator
>
2082 operator()(_UniformRandomNumberGenerator
& __urng
)
2083 { return this->operator()(__urng
, _M_param
); }
2085 template<typename _UniformRandomNumberGenerator
>
2087 operator()(_UniformRandomNumberGenerator
& __urng
,
2088 const param_type
& __p
);
2090 template<typename _ForwardIterator
,
2091 typename _UniformRandomNumberGenerator
>
2093 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2094 _UniformRandomNumberGenerator
& __urng
)
2095 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2097 template<typename _ForwardIterator
,
2098 typename _UniformRandomNumberGenerator
>
2100 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2101 _UniformRandomNumberGenerator
& __urng
,
2102 const param_type
& __p
)
2103 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2105 template<typename _UniformRandomNumberGenerator
>
2107 __generate(result_type
* __f
, result_type
* __t
,
2108 _UniformRandomNumberGenerator
& __urng
,
2109 const param_type
& __p
)
2110 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2113 * @brief Return true if two normal distributions have
2114 * the same parameters and the sequences that would
2115 * be generated are equal.
2117 template<typename _RealType1
>
2119 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2120 const std::normal_distribution
<_RealType1
>& __d2
);
2123 * @brief Inserts a %normal_distribution random number distribution
2124 * @p __x into the output stream @p __os.
2126 * @param __os An output stream.
2127 * @param __x A %normal_distribution random number distribution.
2129 * @returns The output stream with the state of @p __x inserted or in
2132 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2133 friend std::basic_ostream
<_CharT
, _Traits
>&
2134 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2135 const std::normal_distribution
<_RealType1
>& __x
);
2138 * @brief Extracts a %normal_distribution random number distribution
2139 * @p __x from the input stream @p __is.
2141 * @param __is An input stream.
2142 * @param __x A %normal_distribution random number generator engine.
2144 * @returns The input stream with @p __x extracted or in an error
2147 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2148 friend std::basic_istream
<_CharT
, _Traits
>&
2149 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2150 std::normal_distribution
<_RealType1
>& __x
);
2153 template<typename _ForwardIterator
,
2154 typename _UniformRandomNumberGenerator
>
2156 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2157 _UniformRandomNumberGenerator
& __urng
,
2158 const param_type
& __p
);
2160 param_type _M_param
;
2161 result_type _M_saved
;
2162 bool _M_saved_available
;
2166 * @brief Return true if two normal distributions are different.
2168 template<typename _RealType
>
2170 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2171 const std::normal_distribution
<_RealType
>& __d2
)
2172 { return !(__d1
== __d2
); }
2176 * @brief A lognormal_distribution random number distribution.
2178 * The formula for the normal probability mass function is
2180 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2181 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2184 template<typename _RealType
= double>
2185 class lognormal_distribution
2187 static_assert(std::is_floating_point
<_RealType
>::value
,
2188 "result_type must be a floating point type");
2191 /** The type of the range of the distribution. */
2192 typedef _RealType result_type
;
2194 /** Parameter type. */
2197 typedef lognormal_distribution
<_RealType
> distribution_type
;
2199 param_type() : param_type(0.0) { }
2202 param_type(_RealType __m
, _RealType __s
= _RealType(1))
2203 : _M_m(__m
), _M_s(__s
)
2215 operator==(const param_type
& __p1
, const param_type
& __p2
)
2216 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2219 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2220 { return !(__p1
== __p2
); }
2227 lognormal_distribution() : lognormal_distribution(0.0) { }
2230 lognormal_distribution(_RealType __m
, _RealType __s
= _RealType(1))
2231 : _M_param(__m
, __s
), _M_nd()
2235 lognormal_distribution(const param_type
& __p
)
2236 : _M_param(__p
), _M_nd()
2240 * Resets the distribution state.
2251 { return _M_param
.m(); }
2255 { return _M_param
.s(); }
2258 * @brief Returns the parameter set of the distribution.
2262 { return _M_param
; }
2265 * @brief Sets the parameter set of the distribution.
2266 * @param __param The new parameter set of the distribution.
2269 param(const param_type
& __param
)
2270 { _M_param
= __param
; }
2273 * @brief Returns the greatest lower bound value of the distribution.
2277 { return result_type(0); }
2280 * @brief Returns the least upper bound value of the distribution.
2284 { return std::numeric_limits
<result_type
>::max(); }
2287 * @brief Generating functions.
2289 template<typename _UniformRandomNumberGenerator
>
2291 operator()(_UniformRandomNumberGenerator
& __urng
)
2292 { return this->operator()(__urng
, _M_param
); }
2294 template<typename _UniformRandomNumberGenerator
>
2296 operator()(_UniformRandomNumberGenerator
& __urng
,
2297 const param_type
& __p
)
2298 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2300 template<typename _ForwardIterator
,
2301 typename _UniformRandomNumberGenerator
>
2303 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2304 _UniformRandomNumberGenerator
& __urng
)
2305 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2307 template<typename _ForwardIterator
,
2308 typename _UniformRandomNumberGenerator
>
2310 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2311 _UniformRandomNumberGenerator
& __urng
,
2312 const param_type
& __p
)
2313 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2315 template<typename _UniformRandomNumberGenerator
>
2317 __generate(result_type
* __f
, result_type
* __t
,
2318 _UniformRandomNumberGenerator
& __urng
,
2319 const param_type
& __p
)
2320 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2323 * @brief Return true if two lognormal distributions have
2324 * the same parameters and the sequences that would
2325 * be generated are equal.
2328 operator==(const lognormal_distribution
& __d1
,
2329 const lognormal_distribution
& __d2
)
2330 { return (__d1
._M_param
== __d2
._M_param
2331 && __d1
._M_nd
== __d2
._M_nd
); }
2334 * @brief Inserts a %lognormal_distribution random number distribution
2335 * @p __x into the output stream @p __os.
2337 * @param __os An output stream.
2338 * @param __x A %lognormal_distribution random number distribution.
2340 * @returns The output stream with the state of @p __x inserted or in
2343 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2344 friend std::basic_ostream
<_CharT
, _Traits
>&
2345 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2346 const std::lognormal_distribution
<_RealType1
>& __x
);
2349 * @brief Extracts a %lognormal_distribution random number distribution
2350 * @p __x from the input stream @p __is.
2352 * @param __is An input stream.
2353 * @param __x A %lognormal_distribution random number
2356 * @returns The input stream with @p __x extracted or in an error state.
2358 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2359 friend std::basic_istream
<_CharT
, _Traits
>&
2360 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2361 std::lognormal_distribution
<_RealType1
>& __x
);
2364 template<typename _ForwardIterator
,
2365 typename _UniformRandomNumberGenerator
>
2367 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2368 _UniformRandomNumberGenerator
& __urng
,
2369 const param_type
& __p
);
2371 param_type _M_param
;
2373 std::normal_distribution
<result_type
> _M_nd
;
2377 * @brief Return true if two lognormal distributions are different.
2379 template<typename _RealType
>
2381 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2382 const std::lognormal_distribution
<_RealType
>& __d2
)
2383 { return !(__d1
== __d2
); }
2387 * @brief A gamma continuous distribution for random numbers.
2389 * The formula for the gamma probability density function is:
2391 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2392 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2395 template<typename _RealType
= double>
2396 class gamma_distribution
2398 static_assert(std::is_floating_point
<_RealType
>::value
,
2399 "result_type must be a floating point type");
2402 /** The type of the range of the distribution. */
2403 typedef _RealType result_type
;
2405 /** Parameter type. */
2408 typedef gamma_distribution
<_RealType
> distribution_type
;
2409 friend class gamma_distribution
<_RealType
>;
2411 param_type() : param_type(1.0) { }
2414 param_type(_RealType __alpha_val
, _RealType __beta_val
= _RealType(1))
2415 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2417 __glibcxx_assert(_M_alpha
> _RealType(0));
2423 { return _M_alpha
; }
2430 operator==(const param_type
& __p1
, const param_type
& __p2
)
2431 { return (__p1
._M_alpha
== __p2
._M_alpha
2432 && __p1
._M_beta
== __p2
._M_beta
); }
2435 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2436 { return !(__p1
== __p2
); }
2445 _RealType _M_malpha
, _M_a2
;
2450 * @brief Constructs a gamma distribution with parameters 1 and 1.
2452 gamma_distribution() : gamma_distribution(1.0) { }
2455 * @brief Constructs a gamma distribution with parameters
2456 * @f$\alpha@f$ and @f$\beta@f$.
2459 gamma_distribution(_RealType __alpha_val
,
2460 _RealType __beta_val
= _RealType(1))
2461 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2465 gamma_distribution(const param_type
& __p
)
2466 : _M_param(__p
), _M_nd()
2470 * @brief Resets the distribution state.
2477 * @brief Returns the @f$\alpha@f$ of the distribution.
2481 { return _M_param
.alpha(); }
2484 * @brief Returns the @f$\beta@f$ of the distribution.
2488 { return _M_param
.beta(); }
2491 * @brief Returns the parameter set of the distribution.
2495 { return _M_param
; }
2498 * @brief Sets the parameter set of the distribution.
2499 * @param __param The new parameter set of the distribution.
2502 param(const param_type
& __param
)
2503 { _M_param
= __param
; }
2506 * @brief Returns the greatest lower bound value of the distribution.
2510 { return result_type(0); }
2513 * @brief Returns the least upper bound value of the distribution.
2517 { return std::numeric_limits
<result_type
>::max(); }
2520 * @brief Generating functions.
2522 template<typename _UniformRandomNumberGenerator
>
2524 operator()(_UniformRandomNumberGenerator
& __urng
)
2525 { return this->operator()(__urng
, _M_param
); }
2527 template<typename _UniformRandomNumberGenerator
>
2529 operator()(_UniformRandomNumberGenerator
& __urng
,
2530 const param_type
& __p
);
2532 template<typename _ForwardIterator
,
2533 typename _UniformRandomNumberGenerator
>
2535 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2536 _UniformRandomNumberGenerator
& __urng
)
2537 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2539 template<typename _ForwardIterator
,
2540 typename _UniformRandomNumberGenerator
>
2542 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2543 _UniformRandomNumberGenerator
& __urng
,
2544 const param_type
& __p
)
2545 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2547 template<typename _UniformRandomNumberGenerator
>
2549 __generate(result_type
* __f
, result_type
* __t
,
2550 _UniformRandomNumberGenerator
& __urng
,
2551 const param_type
& __p
)
2552 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2555 * @brief Return true if two gamma distributions have the same
2556 * parameters and the sequences that would be generated
2560 operator==(const gamma_distribution
& __d1
,
2561 const gamma_distribution
& __d2
)
2562 { return (__d1
._M_param
== __d2
._M_param
2563 && __d1
._M_nd
== __d2
._M_nd
); }
2566 * @brief Inserts a %gamma_distribution random number distribution
2567 * @p __x into the output stream @p __os.
2569 * @param __os An output stream.
2570 * @param __x A %gamma_distribution random number distribution.
2572 * @returns The output stream with the state of @p __x inserted or in
2575 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2576 friend std::basic_ostream
<_CharT
, _Traits
>&
2577 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2578 const std::gamma_distribution
<_RealType1
>& __x
);
2581 * @brief Extracts a %gamma_distribution random number distribution
2582 * @p __x from the input stream @p __is.
2584 * @param __is An input stream.
2585 * @param __x A %gamma_distribution random number generator engine.
2587 * @returns The input stream with @p __x extracted or in an error state.
2589 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2590 friend std::basic_istream
<_CharT
, _Traits
>&
2591 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2592 std::gamma_distribution
<_RealType1
>& __x
);
2595 template<typename _ForwardIterator
,
2596 typename _UniformRandomNumberGenerator
>
2598 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2599 _UniformRandomNumberGenerator
& __urng
,
2600 const param_type
& __p
);
2602 param_type _M_param
;
2604 std::normal_distribution
<result_type
> _M_nd
;
2608 * @brief Return true if two gamma distributions are different.
2610 template<typename _RealType
>
2612 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2613 const std::gamma_distribution
<_RealType
>& __d2
)
2614 { return !(__d1
== __d2
); }
2618 * @brief A chi_squared_distribution random number distribution.
2620 * The formula for the normal probability mass function is
2621 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2623 template<typename _RealType
= double>
2624 class chi_squared_distribution
2626 static_assert(std::is_floating_point
<_RealType
>::value
,
2627 "result_type must be a floating point type");
2630 /** The type of the range of the distribution. */
2631 typedef _RealType result_type
;
2633 /** Parameter type. */
2636 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2638 param_type() : param_type(1) { }
2641 param_type(_RealType __n
)
2650 operator==(const param_type
& __p1
, const param_type
& __p2
)
2651 { return __p1
._M_n
== __p2
._M_n
; }
2654 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2655 { return !(__p1
== __p2
); }
2661 chi_squared_distribution() : chi_squared_distribution(1) { }
2664 chi_squared_distribution(_RealType __n
)
2665 : _M_param(__n
), _M_gd(__n
/ 2)
2669 chi_squared_distribution(const param_type
& __p
)
2670 : _M_param(__p
), _M_gd(__p
.n() / 2)
2674 * @brief Resets the distribution state.
2685 { return _M_param
.n(); }
2688 * @brief Returns the parameter set of the distribution.
2692 { return _M_param
; }
2695 * @brief Sets the parameter set of the distribution.
2696 * @param __param The new parameter set of the distribution.
2699 param(const param_type
& __param
)
2702 typedef typename
std::gamma_distribution
<result_type
>::param_type
2704 _M_gd
.param(param_type
{__param
.n() / 2});
2708 * @brief Returns the greatest lower bound value of the distribution.
2712 { return result_type(0); }
2715 * @brief Returns the least upper bound value of the distribution.
2719 { return std::numeric_limits
<result_type
>::max(); }
2722 * @brief Generating functions.
2724 template<typename _UniformRandomNumberGenerator
>
2726 operator()(_UniformRandomNumberGenerator
& __urng
)
2727 { return 2 * _M_gd(__urng
); }
2729 template<typename _UniformRandomNumberGenerator
>
2731 operator()(_UniformRandomNumberGenerator
& __urng
,
2732 const param_type
& __p
)
2734 typedef typename
std::gamma_distribution
<result_type
>::param_type
2736 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2739 template<typename _ForwardIterator
,
2740 typename _UniformRandomNumberGenerator
>
2742 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2743 _UniformRandomNumberGenerator
& __urng
)
2744 { this->__generate_impl(__f
, __t
, __urng
); }
2746 template<typename _ForwardIterator
,
2747 typename _UniformRandomNumberGenerator
>
2749 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2750 _UniformRandomNumberGenerator
& __urng
,
2751 const param_type
& __p
)
2752 { typename
std::gamma_distribution
<result_type
>::param_type
2754 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2756 template<typename _UniformRandomNumberGenerator
>
2758 __generate(result_type
* __f
, result_type
* __t
,
2759 _UniformRandomNumberGenerator
& __urng
)
2760 { this->__generate_impl(__f
, __t
, __urng
); }
2762 template<typename _UniformRandomNumberGenerator
>
2764 __generate(result_type
* __f
, result_type
* __t
,
2765 _UniformRandomNumberGenerator
& __urng
,
2766 const param_type
& __p
)
2767 { typename
std::gamma_distribution
<result_type
>::param_type
2769 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2772 * @brief Return true if two Chi-squared distributions have
2773 * the same parameters and the sequences that would be
2774 * generated are equal.
2777 operator==(const chi_squared_distribution
& __d1
,
2778 const chi_squared_distribution
& __d2
)
2779 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
2782 * @brief Inserts a %chi_squared_distribution random number distribution
2783 * @p __x into the output stream @p __os.
2785 * @param __os An output stream.
2786 * @param __x A %chi_squared_distribution random number distribution.
2788 * @returns The output stream with the state of @p __x inserted or in
2791 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2792 friend std::basic_ostream
<_CharT
, _Traits
>&
2793 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2794 const std::chi_squared_distribution
<_RealType1
>& __x
);
2797 * @brief Extracts a %chi_squared_distribution random number distribution
2798 * @p __x from the input stream @p __is.
2800 * @param __is An input stream.
2801 * @param __x A %chi_squared_distribution random number
2804 * @returns The input stream with @p __x extracted or in an error state.
2806 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2807 friend std::basic_istream
<_CharT
, _Traits
>&
2808 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2809 std::chi_squared_distribution
<_RealType1
>& __x
);
2812 template<typename _ForwardIterator
,
2813 typename _UniformRandomNumberGenerator
>
2815 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2816 _UniformRandomNumberGenerator
& __urng
);
2818 template<typename _ForwardIterator
,
2819 typename _UniformRandomNumberGenerator
>
2821 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2822 _UniformRandomNumberGenerator
& __urng
,
2824 std::gamma_distribution
<result_type
>::param_type
& __p
);
2826 param_type _M_param
;
2828 std::gamma_distribution
<result_type
> _M_gd
;
2832 * @brief Return true if two Chi-squared distributions are different.
2834 template<typename _RealType
>
2836 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2837 const std::chi_squared_distribution
<_RealType
>& __d2
)
2838 { return !(__d1
== __d2
); }
2842 * @brief A cauchy_distribution random number distribution.
2844 * The formula for the normal probability mass function is
2845 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2847 template<typename _RealType
= double>
2848 class cauchy_distribution
2850 static_assert(std::is_floating_point
<_RealType
>::value
,
2851 "result_type must be a floating point type");
2854 /** The type of the range of the distribution. */
2855 typedef _RealType result_type
;
2857 /** Parameter type. */
2860 typedef cauchy_distribution
<_RealType
> distribution_type
;
2862 param_type() : param_type(0) { }
2865 param_type(_RealType __a
, _RealType __b
= _RealType(1))
2866 : _M_a(__a
), _M_b(__b
)
2878 operator==(const param_type
& __p1
, const param_type
& __p2
)
2879 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2882 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2883 { return !(__p1
== __p2
); }
2890 cauchy_distribution() : cauchy_distribution(0.0) { }
2893 cauchy_distribution(_RealType __a
, _RealType __b
= 1.0)
2894 : _M_param(__a
, __b
)
2898 cauchy_distribution(const param_type
& __p
)
2903 * @brief Resets the distribution state.
2914 { return _M_param
.a(); }
2918 { return _M_param
.b(); }
2921 * @brief Returns the parameter set of the distribution.
2925 { return _M_param
; }
2928 * @brief Sets the parameter set of the distribution.
2929 * @param __param The new parameter set of the distribution.
2932 param(const param_type
& __param
)
2933 { _M_param
= __param
; }
2936 * @brief Returns the greatest lower bound value of the distribution.
2940 { return std::numeric_limits
<result_type
>::lowest(); }
2943 * @brief Returns the least upper bound value of the distribution.
2947 { return std::numeric_limits
<result_type
>::max(); }
2950 * @brief Generating functions.
2952 template<typename _UniformRandomNumberGenerator
>
2954 operator()(_UniformRandomNumberGenerator
& __urng
)
2955 { return this->operator()(__urng
, _M_param
); }
2957 template<typename _UniformRandomNumberGenerator
>
2959 operator()(_UniformRandomNumberGenerator
& __urng
,
2960 const param_type
& __p
);
2962 template<typename _ForwardIterator
,
2963 typename _UniformRandomNumberGenerator
>
2965 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2966 _UniformRandomNumberGenerator
& __urng
)
2967 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2969 template<typename _ForwardIterator
,
2970 typename _UniformRandomNumberGenerator
>
2972 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2973 _UniformRandomNumberGenerator
& __urng
,
2974 const param_type
& __p
)
2975 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2977 template<typename _UniformRandomNumberGenerator
>
2979 __generate(result_type
* __f
, result_type
* __t
,
2980 _UniformRandomNumberGenerator
& __urng
,
2981 const param_type
& __p
)
2982 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2985 * @brief Return true if two Cauchy distributions have
2986 * the same parameters.
2989 operator==(const cauchy_distribution
& __d1
,
2990 const cauchy_distribution
& __d2
)
2991 { return __d1
._M_param
== __d2
._M_param
; }
2994 template<typename _ForwardIterator
,
2995 typename _UniformRandomNumberGenerator
>
2997 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2998 _UniformRandomNumberGenerator
& __urng
,
2999 const param_type
& __p
);
3001 param_type _M_param
;
3005 * @brief Return true if two Cauchy distributions have
3006 * different parameters.
3008 template<typename _RealType
>
3010 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
3011 const std::cauchy_distribution
<_RealType
>& __d2
)
3012 { return !(__d1
== __d2
); }
3015 * @brief Inserts a %cauchy_distribution random number distribution
3016 * @p __x into the output stream @p __os.
3018 * @param __os An output stream.
3019 * @param __x A %cauchy_distribution random number distribution.
3021 * @returns The output stream with the state of @p __x inserted or in
3024 template<typename _RealType
, typename _CharT
, typename _Traits
>
3025 std::basic_ostream
<_CharT
, _Traits
>&
3026 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3027 const std::cauchy_distribution
<_RealType
>& __x
);
3030 * @brief Extracts a %cauchy_distribution random number distribution
3031 * @p __x from the input stream @p __is.
3033 * @param __is An input stream.
3034 * @param __x A %cauchy_distribution random number
3037 * @returns The input stream with @p __x extracted or in an error state.
3039 template<typename _RealType
, typename _CharT
, typename _Traits
>
3040 std::basic_istream
<_CharT
, _Traits
>&
3041 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3042 std::cauchy_distribution
<_RealType
>& __x
);
3046 * @brief A fisher_f_distribution random number distribution.
3048 * The formula for the normal probability mass function is
3050 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
3051 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
3052 * (1 + \frac{mx}{n})^{-(m+n)/2}
3055 template<typename _RealType
= double>
3056 class fisher_f_distribution
3058 static_assert(std::is_floating_point
<_RealType
>::value
,
3059 "result_type must be a floating point type");
3062 /** The type of the range of the distribution. */
3063 typedef _RealType result_type
;
3065 /** Parameter type. */
3068 typedef fisher_f_distribution
<_RealType
> distribution_type
;
3070 param_type() : param_type(1) { }
3073 param_type(_RealType __m
, _RealType __n
= _RealType(1))
3074 : _M_m(__m
), _M_n(__n
)
3086 operator==(const param_type
& __p1
, const param_type
& __p2
)
3087 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
3090 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3091 { return !(__p1
== __p2
); }
3098 fisher_f_distribution() : fisher_f_distribution(1.0) { }
3101 fisher_f_distribution(_RealType __m
,
3102 _RealType __n
= _RealType(1))
3103 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
3107 fisher_f_distribution(const param_type
& __p
)
3108 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
3112 * @brief Resets the distribution state.
3126 { return _M_param
.m(); }
3130 { return _M_param
.n(); }
3133 * @brief Returns the parameter set of the distribution.
3137 { return _M_param
; }
3140 * @brief Sets the parameter set of the distribution.
3141 * @param __param The new parameter set of the distribution.
3144 param(const param_type
& __param
)
3145 { _M_param
= __param
; }
3148 * @brief Returns the greatest lower bound value of the distribution.
3152 { return result_type(0); }
3155 * @brief Returns the least upper bound value of the distribution.
3159 { return std::numeric_limits
<result_type
>::max(); }
3162 * @brief Generating functions.
3164 template<typename _UniformRandomNumberGenerator
>
3166 operator()(_UniformRandomNumberGenerator
& __urng
)
3167 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
3169 template<typename _UniformRandomNumberGenerator
>
3171 operator()(_UniformRandomNumberGenerator
& __urng
,
3172 const param_type
& __p
)
3174 typedef typename
std::gamma_distribution
<result_type
>::param_type
3176 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
3177 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
3180 template<typename _ForwardIterator
,
3181 typename _UniformRandomNumberGenerator
>
3183 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3184 _UniformRandomNumberGenerator
& __urng
)
3185 { this->__generate_impl(__f
, __t
, __urng
); }
3187 template<typename _ForwardIterator
,
3188 typename _UniformRandomNumberGenerator
>
3190 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3191 _UniformRandomNumberGenerator
& __urng
,
3192 const param_type
& __p
)
3193 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3195 template<typename _UniformRandomNumberGenerator
>
3197 __generate(result_type
* __f
, result_type
* __t
,
3198 _UniformRandomNumberGenerator
& __urng
)
3199 { this->__generate_impl(__f
, __t
, __urng
); }
3201 template<typename _UniformRandomNumberGenerator
>
3203 __generate(result_type
* __f
, result_type
* __t
,
3204 _UniformRandomNumberGenerator
& __urng
,
3205 const param_type
& __p
)
3206 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3209 * @brief Return true if two Fisher f distributions have
3210 * the same parameters and the sequences that would
3211 * be generated are equal.
3214 operator==(const fisher_f_distribution
& __d1
,
3215 const fisher_f_distribution
& __d2
)
3216 { return (__d1
._M_param
== __d2
._M_param
3217 && __d1
._M_gd_x
== __d2
._M_gd_x
3218 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
3221 * @brief Inserts a %fisher_f_distribution random number distribution
3222 * @p __x into the output stream @p __os.
3224 * @param __os An output stream.
3225 * @param __x A %fisher_f_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::fisher_f_distribution
<_RealType1
>& __x
);
3236 * @brief Extracts a %fisher_f_distribution random number distribution
3237 * @p __x from the input stream @p __is.
3239 * @param __is An input stream.
3240 * @param __x A %fisher_f_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::fisher_f_distribution
<_RealType1
>& __x
);
3251 template<typename _ForwardIterator
,
3252 typename _UniformRandomNumberGenerator
>
3254 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3255 _UniformRandomNumberGenerator
& __urng
);
3257 template<typename _ForwardIterator
,
3258 typename _UniformRandomNumberGenerator
>
3260 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3261 _UniformRandomNumberGenerator
& __urng
,
3262 const param_type
& __p
);
3264 param_type _M_param
;
3266 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3270 * @brief Return true if two Fisher f distributions are different.
3272 template<typename _RealType
>
3274 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3275 const std::fisher_f_distribution
<_RealType
>& __d2
)
3276 { return !(__d1
== __d2
); }
3279 * @brief A student_t_distribution random number distribution.
3281 * The formula for the normal probability mass function is:
3283 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3284 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3287 template<typename _RealType
= double>
3288 class student_t_distribution
3290 static_assert(std::is_floating_point
<_RealType
>::value
,
3291 "result_type must be a floating point type");
3294 /** The type of the range of the distribution. */
3295 typedef _RealType result_type
;
3297 /** Parameter type. */
3300 typedef student_t_distribution
<_RealType
> distribution_type
;
3302 param_type() : param_type(1) { }
3305 param_type(_RealType __n
)
3314 operator==(const param_type
& __p1
, const param_type
& __p2
)
3315 { return __p1
._M_n
== __p2
._M_n
; }
3318 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3319 { return !(__p1
== __p2
); }
3325 student_t_distribution() : student_t_distribution(1.0) { }
3328 student_t_distribution(_RealType __n
)
3329 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3333 student_t_distribution(const param_type
& __p
)
3334 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3338 * @brief Resets the distribution state.
3352 { return _M_param
.n(); }
3355 * @brief Returns the parameter set of the distribution.
3359 { return _M_param
; }
3362 * @brief Sets the parameter set of the distribution.
3363 * @param __param The new parameter set of the distribution.
3366 param(const param_type
& __param
)
3367 { _M_param
= __param
; }
3370 * @brief Returns the greatest lower bound value of the distribution.
3374 { return std::numeric_limits
<result_type
>::lowest(); }
3377 * @brief Returns the least upper bound value of the distribution.
3381 { return std::numeric_limits
<result_type
>::max(); }
3384 * @brief Generating functions.
3386 template<typename _UniformRandomNumberGenerator
>
3388 operator()(_UniformRandomNumberGenerator
& __urng
)
3389 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3391 template<typename _UniformRandomNumberGenerator
>
3393 operator()(_UniformRandomNumberGenerator
& __urng
,
3394 const param_type
& __p
)
3396 typedef typename
std::gamma_distribution
<result_type
>::param_type
3399 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3400 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3403 template<typename _ForwardIterator
,
3404 typename _UniformRandomNumberGenerator
>
3406 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3407 _UniformRandomNumberGenerator
& __urng
)
3408 { this->__generate_impl(__f
, __t
, __urng
); }
3410 template<typename _ForwardIterator
,
3411 typename _UniformRandomNumberGenerator
>
3413 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3414 _UniformRandomNumberGenerator
& __urng
,
3415 const param_type
& __p
)
3416 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3418 template<typename _UniformRandomNumberGenerator
>
3420 __generate(result_type
* __f
, result_type
* __t
,
3421 _UniformRandomNumberGenerator
& __urng
)
3422 { this->__generate_impl(__f
, __t
, __urng
); }
3424 template<typename _UniformRandomNumberGenerator
>
3426 __generate(result_type
* __f
, result_type
* __t
,
3427 _UniformRandomNumberGenerator
& __urng
,
3428 const param_type
& __p
)
3429 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3432 * @brief Return true if two Student t distributions have
3433 * the same parameters and the sequences that would
3434 * be generated are equal.
3437 operator==(const student_t_distribution
& __d1
,
3438 const student_t_distribution
& __d2
)
3439 { return (__d1
._M_param
== __d2
._M_param
3440 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3443 * @brief Inserts a %student_t_distribution random number distribution
3444 * @p __x into the output stream @p __os.
3446 * @param __os An output stream.
3447 * @param __x A %student_t_distribution random number distribution.
3449 * @returns The output stream with the state of @p __x inserted or in
3452 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3453 friend std::basic_ostream
<_CharT
, _Traits
>&
3454 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3455 const std::student_t_distribution
<_RealType1
>& __x
);
3458 * @brief Extracts a %student_t_distribution random number distribution
3459 * @p __x from the input stream @p __is.
3461 * @param __is An input stream.
3462 * @param __x A %student_t_distribution random number
3465 * @returns The input stream with @p __x extracted or in an error state.
3467 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3468 friend std::basic_istream
<_CharT
, _Traits
>&
3469 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3470 std::student_t_distribution
<_RealType1
>& __x
);
3473 template<typename _ForwardIterator
,
3474 typename _UniformRandomNumberGenerator
>
3476 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3477 _UniformRandomNumberGenerator
& __urng
);
3478 template<typename _ForwardIterator
,
3479 typename _UniformRandomNumberGenerator
>
3481 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3482 _UniformRandomNumberGenerator
& __urng
,
3483 const param_type
& __p
);
3485 param_type _M_param
;
3487 std::normal_distribution
<result_type
> _M_nd
;
3488 std::gamma_distribution
<result_type
> _M_gd
;
3492 * @brief Return true if two Student t distributions are different.
3494 template<typename _RealType
>
3496 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3497 const std::student_t_distribution
<_RealType
>& __d2
)
3498 { return !(__d1
== __d2
); }
3501 /* @} */ // group random_distributions_normal
3504 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3505 * @ingroup random_distributions
3510 * @brief A Bernoulli random number distribution.
3512 * Generates a sequence of true and false values with likelihood @f$p@f$
3513 * that true will come up and @f$(1 - p)@f$ that false will appear.
3515 class bernoulli_distribution
3518 /** The type of the range of the distribution. */
3519 typedef bool result_type
;
3521 /** Parameter type. */
3524 typedef bernoulli_distribution distribution_type
;
3526 param_type() : param_type(0.5) { }
3529 param_type(double __p
)
3532 __glibcxx_assert((_M_p
>= 0.0) && (_M_p
<= 1.0));
3540 operator==(const param_type
& __p1
, const param_type
& __p2
)
3541 { return __p1
._M_p
== __p2
._M_p
; }
3544 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3545 { return !(__p1
== __p2
); }
3553 * @brief Constructs a Bernoulli distribution with likelihood 0.5.
3555 bernoulli_distribution() : bernoulli_distribution(0.5) { }
3558 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3560 * @param __p [IN] The likelihood of a true result being returned.
3561 * Must be in the interval @f$[0, 1]@f$.
3564 bernoulli_distribution(double __p
)
3569 bernoulli_distribution(const param_type
& __p
)
3574 * @brief Resets the distribution state.
3576 * Does nothing for a Bernoulli distribution.
3582 * @brief Returns the @p p parameter of the distribution.
3586 { return _M_param
.p(); }
3589 * @brief Returns the parameter set of the distribution.
3593 { return _M_param
; }
3596 * @brief Sets the parameter set of the distribution.
3597 * @param __param The new parameter set of the distribution.
3600 param(const param_type
& __param
)
3601 { _M_param
= __param
; }
3604 * @brief Returns the greatest lower bound value of the distribution.
3608 { return std::numeric_limits
<result_type
>::min(); }
3611 * @brief Returns the least upper bound value of the distribution.
3615 { return std::numeric_limits
<result_type
>::max(); }
3618 * @brief Generating functions.
3620 template<typename _UniformRandomNumberGenerator
>
3622 operator()(_UniformRandomNumberGenerator
& __urng
)
3623 { return this->operator()(__urng
, _M_param
); }
3625 template<typename _UniformRandomNumberGenerator
>
3627 operator()(_UniformRandomNumberGenerator
& __urng
,
3628 const param_type
& __p
)
3630 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3632 if ((__aurng() - __aurng
.min())
3633 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3638 template<typename _ForwardIterator
,
3639 typename _UniformRandomNumberGenerator
>
3641 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3642 _UniformRandomNumberGenerator
& __urng
)
3643 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3645 template<typename _ForwardIterator
,
3646 typename _UniformRandomNumberGenerator
>
3648 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3649 _UniformRandomNumberGenerator
& __urng
, const param_type
& __p
)
3650 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3652 template<typename _UniformRandomNumberGenerator
>
3654 __generate(result_type
* __f
, result_type
* __t
,
3655 _UniformRandomNumberGenerator
& __urng
,
3656 const param_type
& __p
)
3657 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3660 * @brief Return true if two Bernoulli distributions have
3661 * the same parameters.
3664 operator==(const bernoulli_distribution
& __d1
,
3665 const bernoulli_distribution
& __d2
)
3666 { return __d1
._M_param
== __d2
._M_param
; }
3669 template<typename _ForwardIterator
,
3670 typename _UniformRandomNumberGenerator
>
3672 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3673 _UniformRandomNumberGenerator
& __urng
,
3674 const param_type
& __p
);
3676 param_type _M_param
;
3680 * @brief Return true if two Bernoulli distributions have
3681 * different parameters.
3684 operator!=(const std::bernoulli_distribution
& __d1
,
3685 const std::bernoulli_distribution
& __d2
)
3686 { return !(__d1
== __d2
); }
3689 * @brief Inserts a %bernoulli_distribution random number distribution
3690 * @p __x into the output stream @p __os.
3692 * @param __os An output stream.
3693 * @param __x A %bernoulli_distribution random number distribution.
3695 * @returns The output stream with the state of @p __x inserted or in
3698 template<typename _CharT
, typename _Traits
>
3699 std::basic_ostream
<_CharT
, _Traits
>&
3700 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3701 const std::bernoulli_distribution
& __x
);
3704 * @brief Extracts a %bernoulli_distribution random number distribution
3705 * @p __x from the input stream @p __is.
3707 * @param __is An input stream.
3708 * @param __x A %bernoulli_distribution random number generator engine.
3710 * @returns The input stream with @p __x extracted or in an error state.
3712 template<typename _CharT
, typename _Traits
>
3713 std::basic_istream
<_CharT
, _Traits
>&
3714 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3715 std::bernoulli_distribution
& __x
)
3719 __x
.param(bernoulli_distribution::param_type(__p
));
3725 * @brief A discrete binomial random number distribution.
3727 * The formula for the binomial probability density function is
3728 * @f$p(i|t,p) = \binom{t}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3729 * and @f$p@f$ are the parameters of the distribution.
3731 template<typename _IntType
= int>
3732 class binomial_distribution
3734 static_assert(std::is_integral
<_IntType
>::value
,
3735 "result_type must be an integral type");
3738 /** The type of the range of the distribution. */
3739 typedef _IntType result_type
;
3741 /** Parameter type. */
3744 typedef binomial_distribution
<_IntType
> distribution_type
;
3745 friend class binomial_distribution
<_IntType
>;
3747 param_type() : param_type(1) { }
3750 param_type(_IntType __t
, double __p
= 0.5)
3751 : _M_t(__t
), _M_p(__p
)
3753 __glibcxx_assert((_M_t
>= _IntType(0))
3768 operator==(const param_type
& __p1
, const param_type
& __p2
)
3769 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3772 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3773 { return !(__p1
== __p2
); }
3783 #if _GLIBCXX_USE_C99_MATH_TR1
3784 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3785 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3790 // constructors and member functions
3792 binomial_distribution() : binomial_distribution(1) { }
3795 binomial_distribution(_IntType __t
, double __p
= 0.5)
3796 : _M_param(__t
, __p
), _M_nd()
3800 binomial_distribution(const param_type
& __p
)
3801 : _M_param(__p
), _M_nd()
3805 * @brief Resets the distribution state.
3812 * @brief Returns the distribution @p t parameter.
3816 { return _M_param
.t(); }
3819 * @brief Returns the distribution @p p parameter.
3823 { return _M_param
.p(); }
3826 * @brief Returns the parameter set of the distribution.
3830 { return _M_param
; }
3833 * @brief Sets the parameter set of the distribution.
3834 * @param __param The new parameter set of the distribution.
3837 param(const param_type
& __param
)
3838 { _M_param
= __param
; }
3841 * @brief Returns the greatest lower bound value of the distribution.
3848 * @brief Returns the least upper bound value of the distribution.
3852 { return _M_param
.t(); }
3855 * @brief Generating functions.
3857 template<typename _UniformRandomNumberGenerator
>
3859 operator()(_UniformRandomNumberGenerator
& __urng
)
3860 { return this->operator()(__urng
, _M_param
); }
3862 template<typename _UniformRandomNumberGenerator
>
3864 operator()(_UniformRandomNumberGenerator
& __urng
,
3865 const param_type
& __p
);
3867 template<typename _ForwardIterator
,
3868 typename _UniformRandomNumberGenerator
>
3870 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3871 _UniformRandomNumberGenerator
& __urng
)
3872 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3874 template<typename _ForwardIterator
,
3875 typename _UniformRandomNumberGenerator
>
3877 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3878 _UniformRandomNumberGenerator
& __urng
,
3879 const param_type
& __p
)
3880 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3882 template<typename _UniformRandomNumberGenerator
>
3884 __generate(result_type
* __f
, result_type
* __t
,
3885 _UniformRandomNumberGenerator
& __urng
,
3886 const param_type
& __p
)
3887 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3890 * @brief Return true if two binomial distributions have
3891 * the same parameters and the sequences that would
3892 * be generated are equal.
3895 operator==(const binomial_distribution
& __d1
,
3896 const binomial_distribution
& __d2
)
3897 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3898 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
3900 { return __d1
._M_param
== __d2
._M_param
; }
3904 * @brief Inserts a %binomial_distribution random number distribution
3905 * @p __x into the output stream @p __os.
3907 * @param __os An output stream.
3908 * @param __x A %binomial_distribution random number distribution.
3910 * @returns The output stream with the state of @p __x inserted or in
3913 template<typename _IntType1
,
3914 typename _CharT
, typename _Traits
>
3915 friend std::basic_ostream
<_CharT
, _Traits
>&
3916 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3917 const std::binomial_distribution
<_IntType1
>& __x
);
3920 * @brief Extracts a %binomial_distribution random number distribution
3921 * @p __x from the input stream @p __is.
3923 * @param __is An input stream.
3924 * @param __x A %binomial_distribution random number generator engine.
3926 * @returns The input stream with @p __x extracted or in an error
3929 template<typename _IntType1
,
3930 typename _CharT
, typename _Traits
>
3931 friend std::basic_istream
<_CharT
, _Traits
>&
3932 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3933 std::binomial_distribution
<_IntType1
>& __x
);
3936 template<typename _ForwardIterator
,
3937 typename _UniformRandomNumberGenerator
>
3939 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3940 _UniformRandomNumberGenerator
& __urng
,
3941 const param_type
& __p
);
3943 template<typename _UniformRandomNumberGenerator
>
3945 _M_waiting(_UniformRandomNumberGenerator
& __urng
,
3946 _IntType __t
, double __q
);
3948 param_type _M_param
;
3950 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3951 std::normal_distribution
<double> _M_nd
;
3955 * @brief Return true if two binomial distributions are different.
3957 template<typename _IntType
>
3959 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
3960 const std::binomial_distribution
<_IntType
>& __d2
)
3961 { return !(__d1
== __d2
); }
3965 * @brief A discrete geometric random number distribution.
3967 * The formula for the geometric probability density function is
3968 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
3971 template<typename _IntType
= int>
3972 class geometric_distribution
3974 static_assert(std::is_integral
<_IntType
>::value
,
3975 "result_type must be an integral type");
3978 /** The type of the range of the distribution. */
3979 typedef _IntType result_type
;
3981 /** Parameter type. */
3984 typedef geometric_distribution
<_IntType
> distribution_type
;
3985 friend class geometric_distribution
<_IntType
>;
3987 param_type() : param_type(0.5) { }
3990 param_type(double __p
)
3993 __glibcxx_assert((_M_p
> 0.0) && (_M_p
< 1.0));
4002 operator==(const param_type
& __p1
, const param_type
& __p2
)
4003 { return __p1
._M_p
== __p2
._M_p
; }
4006 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4007 { return !(__p1
== __p2
); }
4012 { _M_log_1_p
= std::log(1.0 - _M_p
); }
4019 // constructors and member functions
4021 geometric_distribution() : geometric_distribution(0.5) { }
4024 geometric_distribution(double __p
)
4029 geometric_distribution(const param_type
& __p
)
4034 * @brief Resets the distribution state.
4036 * Does nothing for the geometric distribution.
4042 * @brief Returns the distribution parameter @p p.
4046 { return _M_param
.p(); }
4049 * @brief Returns the parameter set of the distribution.
4053 { return _M_param
; }
4056 * @brief Sets the parameter set of the distribution.
4057 * @param __param The new parameter set of the distribution.
4060 param(const param_type
& __param
)
4061 { _M_param
= __param
; }
4064 * @brief Returns the greatest lower bound value of the distribution.
4071 * @brief Returns the least upper bound value of the distribution.
4075 { return std::numeric_limits
<result_type
>::max(); }
4078 * @brief Generating functions.
4080 template<typename _UniformRandomNumberGenerator
>
4082 operator()(_UniformRandomNumberGenerator
& __urng
)
4083 { return this->operator()(__urng
, _M_param
); }
4085 template<typename _UniformRandomNumberGenerator
>
4087 operator()(_UniformRandomNumberGenerator
& __urng
,
4088 const param_type
& __p
);
4090 template<typename _ForwardIterator
,
4091 typename _UniformRandomNumberGenerator
>
4093 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4094 _UniformRandomNumberGenerator
& __urng
)
4095 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4097 template<typename _ForwardIterator
,
4098 typename _UniformRandomNumberGenerator
>
4100 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4101 _UniformRandomNumberGenerator
& __urng
,
4102 const param_type
& __p
)
4103 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4105 template<typename _UniformRandomNumberGenerator
>
4107 __generate(result_type
* __f
, result_type
* __t
,
4108 _UniformRandomNumberGenerator
& __urng
,
4109 const param_type
& __p
)
4110 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4113 * @brief Return true if two geometric distributions have
4114 * the same parameters.
4117 operator==(const geometric_distribution
& __d1
,
4118 const geometric_distribution
& __d2
)
4119 { return __d1
._M_param
== __d2
._M_param
; }
4122 template<typename _ForwardIterator
,
4123 typename _UniformRandomNumberGenerator
>
4125 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4126 _UniformRandomNumberGenerator
& __urng
,
4127 const param_type
& __p
);
4129 param_type _M_param
;
4133 * @brief Return true if two geometric distributions have
4134 * different parameters.
4136 template<typename _IntType
>
4138 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
4139 const std::geometric_distribution
<_IntType
>& __d2
)
4140 { return !(__d1
== __d2
); }
4143 * @brief Inserts a %geometric_distribution random number distribution
4144 * @p __x into the output stream @p __os.
4146 * @param __os An output stream.
4147 * @param __x A %geometric_distribution random number distribution.
4149 * @returns The output stream with the state of @p __x inserted or in
4152 template<typename _IntType
,
4153 typename _CharT
, typename _Traits
>
4154 std::basic_ostream
<_CharT
, _Traits
>&
4155 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4156 const std::geometric_distribution
<_IntType
>& __x
);
4159 * @brief Extracts a %geometric_distribution random number distribution
4160 * @p __x from the input stream @p __is.
4162 * @param __is An input stream.
4163 * @param __x A %geometric_distribution random number generator engine.
4165 * @returns The input stream with @p __x extracted or in an error state.
4167 template<typename _IntType
,
4168 typename _CharT
, typename _Traits
>
4169 std::basic_istream
<_CharT
, _Traits
>&
4170 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4171 std::geometric_distribution
<_IntType
>& __x
);
4175 * @brief A negative_binomial_distribution random number distribution.
4177 * The formula for the negative binomial probability mass function is
4178 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
4179 * and @f$p@f$ are the parameters of the distribution.
4181 template<typename _IntType
= int>
4182 class negative_binomial_distribution
4184 static_assert(std::is_integral
<_IntType
>::value
,
4185 "result_type must be an integral type");
4188 /** The type of the range of the distribution. */
4189 typedef _IntType result_type
;
4191 /** Parameter type. */
4194 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
4196 param_type() : param_type(1) { }
4199 param_type(_IntType __k
, double __p
= 0.5)
4200 : _M_k(__k
), _M_p(__p
)
4202 __glibcxx_assert((_M_k
> 0) && (_M_p
> 0.0) && (_M_p
<= 1.0));
4214 operator==(const param_type
& __p1
, const param_type
& __p2
)
4215 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
4218 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4219 { return !(__p1
== __p2
); }
4226 negative_binomial_distribution() : negative_binomial_distribution(1) { }
4229 negative_binomial_distribution(_IntType __k
, double __p
= 0.5)
4230 : _M_param(__k
, __p
), _M_gd(__k
, (1.0 - __p
) / __p
)
4234 negative_binomial_distribution(const param_type
& __p
)
4235 : _M_param(__p
), _M_gd(__p
.k(), (1.0 - __p
.p()) / __p
.p())
4239 * @brief Resets the distribution state.
4246 * @brief Return the @f$k@f$ parameter of the distribution.
4250 { return _M_param
.k(); }
4253 * @brief Return the @f$p@f$ parameter of the distribution.
4257 { return _M_param
.p(); }
4260 * @brief Returns the parameter set of the distribution.
4264 { return _M_param
; }
4267 * @brief Sets the parameter set of the distribution.
4268 * @param __param The new parameter set of the distribution.
4271 param(const param_type
& __param
)
4272 { _M_param
= __param
; }
4275 * @brief Returns the greatest lower bound value of the distribution.
4279 { return result_type(0); }
4282 * @brief Returns the least upper bound value of the distribution.
4286 { return std::numeric_limits
<result_type
>::max(); }
4289 * @brief Generating functions.
4291 template<typename _UniformRandomNumberGenerator
>
4293 operator()(_UniformRandomNumberGenerator
& __urng
);
4295 template<typename _UniformRandomNumberGenerator
>
4297 operator()(_UniformRandomNumberGenerator
& __urng
,
4298 const param_type
& __p
);
4300 template<typename _ForwardIterator
,
4301 typename _UniformRandomNumberGenerator
>
4303 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4304 _UniformRandomNumberGenerator
& __urng
)
4305 { this->__generate_impl(__f
, __t
, __urng
); }
4307 template<typename _ForwardIterator
,
4308 typename _UniformRandomNumberGenerator
>
4310 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4311 _UniformRandomNumberGenerator
& __urng
,
4312 const param_type
& __p
)
4313 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4315 template<typename _UniformRandomNumberGenerator
>
4317 __generate(result_type
* __f
, result_type
* __t
,
4318 _UniformRandomNumberGenerator
& __urng
)
4319 { this->__generate_impl(__f
, __t
, __urng
); }
4321 template<typename _UniformRandomNumberGenerator
>
4323 __generate(result_type
* __f
, result_type
* __t
,
4324 _UniformRandomNumberGenerator
& __urng
,
4325 const param_type
& __p
)
4326 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4329 * @brief Return true if two negative binomial distributions have
4330 * the same parameters and the sequences that would be
4331 * generated are equal.
4334 operator==(const negative_binomial_distribution
& __d1
,
4335 const negative_binomial_distribution
& __d2
)
4336 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
4339 * @brief Inserts a %negative_binomial_distribution random
4340 * number distribution @p __x into the output stream @p __os.
4342 * @param __os An output stream.
4343 * @param __x A %negative_binomial_distribution random number
4346 * @returns The output stream with the state of @p __x inserted or in
4349 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4350 friend std::basic_ostream
<_CharT
, _Traits
>&
4351 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4352 const std::negative_binomial_distribution
<_IntType1
>& __x
);
4355 * @brief Extracts a %negative_binomial_distribution random number
4356 * distribution @p __x from the input stream @p __is.
4358 * @param __is An input stream.
4359 * @param __x A %negative_binomial_distribution random number
4362 * @returns The input stream with @p __x extracted or in an error state.
4364 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4365 friend std::basic_istream
<_CharT
, _Traits
>&
4366 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4367 std::negative_binomial_distribution
<_IntType1
>& __x
);
4370 template<typename _ForwardIterator
,
4371 typename _UniformRandomNumberGenerator
>
4373 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4374 _UniformRandomNumberGenerator
& __urng
);
4375 template<typename _ForwardIterator
,
4376 typename _UniformRandomNumberGenerator
>
4378 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4379 _UniformRandomNumberGenerator
& __urng
,
4380 const param_type
& __p
);
4382 param_type _M_param
;
4384 std::gamma_distribution
<double> _M_gd
;
4388 * @brief Return true if two negative binomial distributions are different.
4390 template<typename _IntType
>
4392 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
4393 const std::negative_binomial_distribution
<_IntType
>& __d2
)
4394 { return !(__d1
== __d2
); }
4397 /* @} */ // group random_distributions_bernoulli
4400 * @addtogroup random_distributions_poisson Poisson Distributions
4401 * @ingroup random_distributions
4406 * @brief A discrete Poisson random number distribution.
4408 * The formula for the Poisson probability density function is
4409 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
4410 * parameter of the distribution.
4412 template<typename _IntType
= int>
4413 class poisson_distribution
4415 static_assert(std::is_integral
<_IntType
>::value
,
4416 "result_type must be an integral type");
4419 /** The type of the range of the distribution. */
4420 typedef _IntType result_type
;
4422 /** Parameter type. */
4425 typedef poisson_distribution
<_IntType
> distribution_type
;
4426 friend class poisson_distribution
<_IntType
>;
4428 param_type() : param_type(1.0) { }
4431 param_type(double __mean
)
4434 __glibcxx_assert(_M_mean
> 0.0);
4443 operator==(const param_type
& __p1
, const param_type
& __p2
)
4444 { return __p1
._M_mean
== __p2
._M_mean
; }
4447 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4448 { return !(__p1
== __p2
); }
4451 // Hosts either log(mean) or the threshold of the simple method.
4458 #if _GLIBCXX_USE_C99_MATH_TR1
4459 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
4463 // constructors and member functions
4465 poisson_distribution() : poisson_distribution(1.0) { }
4468 poisson_distribution(double __mean
)
4469 : _M_param(__mean
), _M_nd()
4473 poisson_distribution(const param_type
& __p
)
4474 : _M_param(__p
), _M_nd()
4478 * @brief Resets the distribution state.
4485 * @brief Returns the distribution parameter @p mean.
4489 { return _M_param
.mean(); }
4492 * @brief Returns the parameter set of the distribution.
4496 { return _M_param
; }
4499 * @brief Sets the parameter set of the distribution.
4500 * @param __param The new parameter set of the distribution.
4503 param(const param_type
& __param
)
4504 { _M_param
= __param
; }
4507 * @brief Returns the greatest lower bound value of the distribution.
4514 * @brief Returns the least upper bound value of the distribution.
4518 { return std::numeric_limits
<result_type
>::max(); }
4521 * @brief Generating functions.
4523 template<typename _UniformRandomNumberGenerator
>
4525 operator()(_UniformRandomNumberGenerator
& __urng
)
4526 { return this->operator()(__urng
, _M_param
); }
4528 template<typename _UniformRandomNumberGenerator
>
4530 operator()(_UniformRandomNumberGenerator
& __urng
,
4531 const param_type
& __p
);
4533 template<typename _ForwardIterator
,
4534 typename _UniformRandomNumberGenerator
>
4536 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4537 _UniformRandomNumberGenerator
& __urng
)
4538 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4540 template<typename _ForwardIterator
,
4541 typename _UniformRandomNumberGenerator
>
4543 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4544 _UniformRandomNumberGenerator
& __urng
,
4545 const param_type
& __p
)
4546 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4548 template<typename _UniformRandomNumberGenerator
>
4550 __generate(result_type
* __f
, result_type
* __t
,
4551 _UniformRandomNumberGenerator
& __urng
,
4552 const param_type
& __p
)
4553 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4556 * @brief Return true if two Poisson distributions have the same
4557 * parameters and the sequences that would be generated
4561 operator==(const poisson_distribution
& __d1
,
4562 const poisson_distribution
& __d2
)
4563 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4564 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
4566 { return __d1
._M_param
== __d2
._M_param
; }
4570 * @brief Inserts a %poisson_distribution random number distribution
4571 * @p __x into the output stream @p __os.
4573 * @param __os An output stream.
4574 * @param __x A %poisson_distribution random number distribution.
4576 * @returns The output stream with the state of @p __x inserted or in
4579 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4580 friend std::basic_ostream
<_CharT
, _Traits
>&
4581 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4582 const std::poisson_distribution
<_IntType1
>& __x
);
4585 * @brief Extracts a %poisson_distribution random number distribution
4586 * @p __x from the input stream @p __is.
4588 * @param __is An input stream.
4589 * @param __x A %poisson_distribution random number generator engine.
4591 * @returns The input stream with @p __x extracted or in an error
4594 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4595 friend std::basic_istream
<_CharT
, _Traits
>&
4596 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4597 std::poisson_distribution
<_IntType1
>& __x
);
4600 template<typename _ForwardIterator
,
4601 typename _UniformRandomNumberGenerator
>
4603 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4604 _UniformRandomNumberGenerator
& __urng
,
4605 const param_type
& __p
);
4607 param_type _M_param
;
4609 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4610 std::normal_distribution
<double> _M_nd
;
4614 * @brief Return true if two Poisson distributions are different.
4616 template<typename _IntType
>
4618 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4619 const std::poisson_distribution
<_IntType
>& __d2
)
4620 { return !(__d1
== __d2
); }
4624 * @brief An exponential continuous distribution for random numbers.
4626 * The formula for the exponential probability density function is
4627 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4629 * <table border=1 cellpadding=10 cellspacing=0>
4630 * <caption align=top>Distribution Statistics</caption>
4631 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4632 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4633 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4634 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4635 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4638 template<typename _RealType
= double>
4639 class exponential_distribution
4641 static_assert(std::is_floating_point
<_RealType
>::value
,
4642 "result_type must be a floating point type");
4645 /** The type of the range of the distribution. */
4646 typedef _RealType result_type
;
4648 /** Parameter type. */
4651 typedef exponential_distribution
<_RealType
> distribution_type
;
4653 param_type() : param_type(1.0) { }
4656 param_type(_RealType __lambda
)
4657 : _M_lambda(__lambda
)
4659 __glibcxx_assert(_M_lambda
> _RealType(0));
4664 { return _M_lambda
; }
4667 operator==(const param_type
& __p1
, const param_type
& __p2
)
4668 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4671 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4672 { return !(__p1
== __p2
); }
4675 _RealType _M_lambda
;
4680 * @brief Constructs an exponential distribution with inverse scale
4683 exponential_distribution() : exponential_distribution(1.0) { }
4686 * @brief Constructs an exponential distribution with inverse scale
4687 * parameter @f$\lambda@f$.
4690 exponential_distribution(_RealType __lambda
)
4691 : _M_param(__lambda
)
4695 exponential_distribution(const param_type
& __p
)
4700 * @brief Resets the distribution state.
4702 * Has no effect on exponential distributions.
4708 * @brief Returns the inverse scale parameter of the distribution.
4712 { return _M_param
.lambda(); }
4715 * @brief Returns the parameter set of the distribution.
4719 { return _M_param
; }
4722 * @brief Sets the parameter set of the distribution.
4723 * @param __param The new parameter set of the distribution.
4726 param(const param_type
& __param
)
4727 { _M_param
= __param
; }
4730 * @brief Returns the greatest lower bound value of the distribution.
4734 { return result_type(0); }
4737 * @brief Returns the least upper bound value of the distribution.
4741 { return std::numeric_limits
<result_type
>::max(); }
4744 * @brief Generating functions.
4746 template<typename _UniformRandomNumberGenerator
>
4748 operator()(_UniformRandomNumberGenerator
& __urng
)
4749 { return this->operator()(__urng
, _M_param
); }
4751 template<typename _UniformRandomNumberGenerator
>
4753 operator()(_UniformRandomNumberGenerator
& __urng
,
4754 const param_type
& __p
)
4756 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4758 return -std::log(result_type(1) - __aurng()) / __p
.lambda();
4761 template<typename _ForwardIterator
,
4762 typename _UniformRandomNumberGenerator
>
4764 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4765 _UniformRandomNumberGenerator
& __urng
)
4766 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4768 template<typename _ForwardIterator
,
4769 typename _UniformRandomNumberGenerator
>
4771 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4772 _UniformRandomNumberGenerator
& __urng
,
4773 const param_type
& __p
)
4774 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4776 template<typename _UniformRandomNumberGenerator
>
4778 __generate(result_type
* __f
, result_type
* __t
,
4779 _UniformRandomNumberGenerator
& __urng
,
4780 const param_type
& __p
)
4781 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4784 * @brief Return true if two exponential distributions have the same
4788 operator==(const exponential_distribution
& __d1
,
4789 const exponential_distribution
& __d2
)
4790 { return __d1
._M_param
== __d2
._M_param
; }
4793 template<typename _ForwardIterator
,
4794 typename _UniformRandomNumberGenerator
>
4796 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4797 _UniformRandomNumberGenerator
& __urng
,
4798 const param_type
& __p
);
4800 param_type _M_param
;
4804 * @brief Return true if two exponential distributions have different
4807 template<typename _RealType
>
4809 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4810 const std::exponential_distribution
<_RealType
>& __d2
)
4811 { return !(__d1
== __d2
); }
4814 * @brief Inserts a %exponential_distribution random number distribution
4815 * @p __x into the output stream @p __os.
4817 * @param __os An output stream.
4818 * @param __x A %exponential_distribution random number distribution.
4820 * @returns The output stream with the state of @p __x inserted or in
4823 template<typename _RealType
, typename _CharT
, typename _Traits
>
4824 std::basic_ostream
<_CharT
, _Traits
>&
4825 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4826 const std::exponential_distribution
<_RealType
>& __x
);
4829 * @brief Extracts a %exponential_distribution random number distribution
4830 * @p __x from the input stream @p __is.
4832 * @param __is An input stream.
4833 * @param __x A %exponential_distribution random number
4836 * @returns The input stream with @p __x extracted or in an error state.
4838 template<typename _RealType
, typename _CharT
, typename _Traits
>
4839 std::basic_istream
<_CharT
, _Traits
>&
4840 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4841 std::exponential_distribution
<_RealType
>& __x
);
4845 * @brief A weibull_distribution random number distribution.
4847 * The formula for the normal probability density function is:
4849 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4850 * \exp{(-(\frac{x}{\beta})^\alpha)}
4853 template<typename _RealType
= double>
4854 class weibull_distribution
4856 static_assert(std::is_floating_point
<_RealType
>::value
,
4857 "result_type must be a floating point type");
4860 /** The type of the range of the distribution. */
4861 typedef _RealType result_type
;
4863 /** Parameter type. */
4866 typedef weibull_distribution
<_RealType
> distribution_type
;
4868 param_type() : param_type(1.0) { }
4871 param_type(_RealType __a
, _RealType __b
= _RealType(1.0))
4872 : _M_a(__a
), _M_b(__b
)
4884 operator==(const param_type
& __p1
, const param_type
& __p2
)
4885 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4888 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4889 { return !(__p1
== __p2
); }
4896 weibull_distribution() : weibull_distribution(1.0) { }
4899 weibull_distribution(_RealType __a
, _RealType __b
= _RealType(1))
4900 : _M_param(__a
, __b
)
4904 weibull_distribution(const param_type
& __p
)
4909 * @brief Resets the distribution state.
4916 * @brief Return the @f$a@f$ parameter of the distribution.
4920 { return _M_param
.a(); }
4923 * @brief Return the @f$b@f$ parameter of the distribution.
4927 { return _M_param
.b(); }
4930 * @brief Returns the parameter set of the distribution.
4934 { return _M_param
; }
4937 * @brief Sets the parameter set of the distribution.
4938 * @param __param The new parameter set of the distribution.
4941 param(const param_type
& __param
)
4942 { _M_param
= __param
; }
4945 * @brief Returns the greatest lower bound value of the distribution.
4949 { return result_type(0); }
4952 * @brief Returns the least upper bound value of the distribution.
4956 { return std::numeric_limits
<result_type
>::max(); }
4959 * @brief Generating functions.
4961 template<typename _UniformRandomNumberGenerator
>
4963 operator()(_UniformRandomNumberGenerator
& __urng
)
4964 { return this->operator()(__urng
, _M_param
); }
4966 template<typename _UniformRandomNumberGenerator
>
4968 operator()(_UniformRandomNumberGenerator
& __urng
,
4969 const param_type
& __p
);
4971 template<typename _ForwardIterator
,
4972 typename _UniformRandomNumberGenerator
>
4974 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4975 _UniformRandomNumberGenerator
& __urng
)
4976 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4978 template<typename _ForwardIterator
,
4979 typename _UniformRandomNumberGenerator
>
4981 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4982 _UniformRandomNumberGenerator
& __urng
,
4983 const param_type
& __p
)
4984 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4986 template<typename _UniformRandomNumberGenerator
>
4988 __generate(result_type
* __f
, result_type
* __t
,
4989 _UniformRandomNumberGenerator
& __urng
,
4990 const param_type
& __p
)
4991 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4994 * @brief Return true if two Weibull distributions have the same
4998 operator==(const weibull_distribution
& __d1
,
4999 const weibull_distribution
& __d2
)
5000 { return __d1
._M_param
== __d2
._M_param
; }
5003 template<typename _ForwardIterator
,
5004 typename _UniformRandomNumberGenerator
>
5006 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5007 _UniformRandomNumberGenerator
& __urng
,
5008 const param_type
& __p
);
5010 param_type _M_param
;
5014 * @brief Return true if two Weibull distributions have different
5017 template<typename _RealType
>
5019 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
5020 const std::weibull_distribution
<_RealType
>& __d2
)
5021 { return !(__d1
== __d2
); }
5024 * @brief Inserts a %weibull_distribution random number distribution
5025 * @p __x into the output stream @p __os.
5027 * @param __os An output stream.
5028 * @param __x A %weibull_distribution random number distribution.
5030 * @returns The output stream with the state of @p __x inserted or in
5033 template<typename _RealType
, typename _CharT
, typename _Traits
>
5034 std::basic_ostream
<_CharT
, _Traits
>&
5035 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5036 const std::weibull_distribution
<_RealType
>& __x
);
5039 * @brief Extracts a %weibull_distribution random number distribution
5040 * @p __x from the input stream @p __is.
5042 * @param __is An input stream.
5043 * @param __x A %weibull_distribution random number
5046 * @returns The input stream with @p __x extracted or in an error state.
5048 template<typename _RealType
, typename _CharT
, typename _Traits
>
5049 std::basic_istream
<_CharT
, _Traits
>&
5050 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5051 std::weibull_distribution
<_RealType
>& __x
);
5055 * @brief A extreme_value_distribution random number distribution.
5057 * The formula for the normal probability mass function is
5059 * p(x|a,b) = \frac{1}{b}
5060 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
5063 template<typename _RealType
= double>
5064 class extreme_value_distribution
5066 static_assert(std::is_floating_point
<_RealType
>::value
,
5067 "result_type must be a floating point type");
5070 /** The type of the range of the distribution. */
5071 typedef _RealType result_type
;
5073 /** Parameter type. */
5076 typedef extreme_value_distribution
<_RealType
> distribution_type
;
5078 param_type() : param_type(0.0) { }
5081 param_type(_RealType __a
, _RealType __b
= _RealType(1.0))
5082 : _M_a(__a
), _M_b(__b
)
5094 operator==(const param_type
& __p1
, const param_type
& __p2
)
5095 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
5098 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5099 { return !(__p1
== __p2
); }
5106 extreme_value_distribution() : extreme_value_distribution(0.0) { }
5109 extreme_value_distribution(_RealType __a
, _RealType __b
= _RealType(1))
5110 : _M_param(__a
, __b
)
5114 extreme_value_distribution(const param_type
& __p
)
5119 * @brief Resets the distribution state.
5126 * @brief Return the @f$a@f$ parameter of the distribution.
5130 { return _M_param
.a(); }
5133 * @brief Return the @f$b@f$ parameter of the distribution.
5137 { return _M_param
.b(); }
5140 * @brief Returns the parameter set of the distribution.
5144 { return _M_param
; }
5147 * @brief Sets the parameter set of the distribution.
5148 * @param __param The new parameter set of the distribution.
5151 param(const param_type
& __param
)
5152 { _M_param
= __param
; }
5155 * @brief Returns the greatest lower bound value of the distribution.
5159 { return std::numeric_limits
<result_type
>::lowest(); }
5162 * @brief Returns the least upper bound value of the distribution.
5166 { return std::numeric_limits
<result_type
>::max(); }
5169 * @brief Generating functions.
5171 template<typename _UniformRandomNumberGenerator
>
5173 operator()(_UniformRandomNumberGenerator
& __urng
)
5174 { return this->operator()(__urng
, _M_param
); }
5176 template<typename _UniformRandomNumberGenerator
>
5178 operator()(_UniformRandomNumberGenerator
& __urng
,
5179 const param_type
& __p
);
5181 template<typename _ForwardIterator
,
5182 typename _UniformRandomNumberGenerator
>
5184 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5185 _UniformRandomNumberGenerator
& __urng
)
5186 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5188 template<typename _ForwardIterator
,
5189 typename _UniformRandomNumberGenerator
>
5191 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5192 _UniformRandomNumberGenerator
& __urng
,
5193 const param_type
& __p
)
5194 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5196 template<typename _UniformRandomNumberGenerator
>
5198 __generate(result_type
* __f
, result_type
* __t
,
5199 _UniformRandomNumberGenerator
& __urng
,
5200 const param_type
& __p
)
5201 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5204 * @brief Return true if two extreme value distributions have the same
5208 operator==(const extreme_value_distribution
& __d1
,
5209 const extreme_value_distribution
& __d2
)
5210 { return __d1
._M_param
== __d2
._M_param
; }
5213 template<typename _ForwardIterator
,
5214 typename _UniformRandomNumberGenerator
>
5216 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5217 _UniformRandomNumberGenerator
& __urng
,
5218 const param_type
& __p
);
5220 param_type _M_param
;
5224 * @brief Return true if two extreme value distributions have different
5227 template<typename _RealType
>
5229 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
5230 const std::extreme_value_distribution
<_RealType
>& __d2
)
5231 { return !(__d1
== __d2
); }
5234 * @brief Inserts a %extreme_value_distribution random number distribution
5235 * @p __x into the output stream @p __os.
5237 * @param __os An output stream.
5238 * @param __x A %extreme_value_distribution random number distribution.
5240 * @returns The output stream with the state of @p __x inserted or in
5243 template<typename _RealType
, typename _CharT
, typename _Traits
>
5244 std::basic_ostream
<_CharT
, _Traits
>&
5245 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5246 const std::extreme_value_distribution
<_RealType
>& __x
);
5249 * @brief Extracts a %extreme_value_distribution random number
5250 * distribution @p __x from the input stream @p __is.
5252 * @param __is An input stream.
5253 * @param __x A %extreme_value_distribution random number
5256 * @returns The input stream with @p __x extracted or in an error state.
5258 template<typename _RealType
, typename _CharT
, typename _Traits
>
5259 std::basic_istream
<_CharT
, _Traits
>&
5260 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5261 std::extreme_value_distribution
<_RealType
>& __x
);
5265 * @brief A discrete_distribution random number distribution.
5267 * The formula for the discrete probability mass function is
5270 template<typename _IntType
= int>
5271 class discrete_distribution
5273 static_assert(std::is_integral
<_IntType
>::value
,
5274 "result_type must be an integral type");
5277 /** The type of the range of the distribution. */
5278 typedef _IntType result_type
;
5280 /** Parameter type. */
5283 typedef discrete_distribution
<_IntType
> distribution_type
;
5284 friend class discrete_distribution
<_IntType
>;
5287 : _M_prob(), _M_cp()
5290 template<typename _InputIterator
>
5291 param_type(_InputIterator __wbegin
,
5292 _InputIterator __wend
)
5293 : _M_prob(__wbegin
, __wend
), _M_cp()
5294 { _M_initialize(); }
5296 param_type(initializer_list
<double> __wil
)
5297 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
5298 { _M_initialize(); }
5300 template<typename _Func
>
5301 param_type(size_t __nw
, double __xmin
, double __xmax
,
5304 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5305 param_type(const param_type
&) = default;
5306 param_type
& operator=(const param_type
&) = default;
5309 probabilities() const
5310 { return _M_prob
.empty() ? std::vector
<double>(1, 1.0) : _M_prob
; }
5313 operator==(const param_type
& __p1
, const param_type
& __p2
)
5314 { return __p1
._M_prob
== __p2
._M_prob
; }
5317 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5318 { return !(__p1
== __p2
); }
5324 std::vector
<double> _M_prob
;
5325 std::vector
<double> _M_cp
;
5328 discrete_distribution()
5332 template<typename _InputIterator
>
5333 discrete_distribution(_InputIterator __wbegin
,
5334 _InputIterator __wend
)
5335 : _M_param(__wbegin
, __wend
)
5338 discrete_distribution(initializer_list
<double> __wl
)
5342 template<typename _Func
>
5343 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
5345 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5349 discrete_distribution(const param_type
& __p
)
5354 * @brief Resets the distribution state.
5361 * @brief Returns the probabilities of the distribution.
5364 probabilities() const
5366 return _M_param
._M_prob
.empty()
5367 ? std::vector
<double>(1, 1.0) : _M_param
._M_prob
;
5371 * @brief Returns the parameter set of the distribution.
5375 { return _M_param
; }
5378 * @brief Sets the parameter set of the distribution.
5379 * @param __param The new parameter set of the distribution.
5382 param(const param_type
& __param
)
5383 { _M_param
= __param
; }
5386 * @brief Returns the greatest lower bound value of the distribution.
5390 { return result_type(0); }
5393 * @brief Returns the least upper bound value of the distribution.
5398 return _M_param
._M_prob
.empty()
5399 ? result_type(0) : result_type(_M_param
._M_prob
.size() - 1);
5403 * @brief Generating functions.
5405 template<typename _UniformRandomNumberGenerator
>
5407 operator()(_UniformRandomNumberGenerator
& __urng
)
5408 { return this->operator()(__urng
, _M_param
); }
5410 template<typename _UniformRandomNumberGenerator
>
5412 operator()(_UniformRandomNumberGenerator
& __urng
,
5413 const param_type
& __p
);
5415 template<typename _ForwardIterator
,
5416 typename _UniformRandomNumberGenerator
>
5418 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5419 _UniformRandomNumberGenerator
& __urng
)
5420 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5422 template<typename _ForwardIterator
,
5423 typename _UniformRandomNumberGenerator
>
5425 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5426 _UniformRandomNumberGenerator
& __urng
,
5427 const param_type
& __p
)
5428 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5430 template<typename _UniformRandomNumberGenerator
>
5432 __generate(result_type
* __f
, result_type
* __t
,
5433 _UniformRandomNumberGenerator
& __urng
,
5434 const param_type
& __p
)
5435 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5438 * @brief Return true if two discrete distributions have the same
5442 operator==(const discrete_distribution
& __d1
,
5443 const discrete_distribution
& __d2
)
5444 { return __d1
._M_param
== __d2
._M_param
; }
5447 * @brief Inserts a %discrete_distribution random number distribution
5448 * @p __x into the output stream @p __os.
5450 * @param __os An output stream.
5451 * @param __x A %discrete_distribution random number distribution.
5453 * @returns The output stream with the state of @p __x inserted or in
5456 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5457 friend std::basic_ostream
<_CharT
, _Traits
>&
5458 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5459 const std::discrete_distribution
<_IntType1
>& __x
);
5462 * @brief Extracts a %discrete_distribution random number distribution
5463 * @p __x from the input stream @p __is.
5465 * @param __is An input stream.
5466 * @param __x A %discrete_distribution random number
5469 * @returns The input stream with @p __x extracted or in an error
5472 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5473 friend std::basic_istream
<_CharT
, _Traits
>&
5474 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5475 std::discrete_distribution
<_IntType1
>& __x
);
5478 template<typename _ForwardIterator
,
5479 typename _UniformRandomNumberGenerator
>
5481 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5482 _UniformRandomNumberGenerator
& __urng
,
5483 const param_type
& __p
);
5485 param_type _M_param
;
5489 * @brief Return true if two discrete distributions have different
5492 template<typename _IntType
>
5494 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
5495 const std::discrete_distribution
<_IntType
>& __d2
)
5496 { return !(__d1
== __d2
); }
5500 * @brief A piecewise_constant_distribution random number distribution.
5502 * The formula for the piecewise constant probability mass function is
5505 template<typename _RealType
= double>
5506 class piecewise_constant_distribution
5508 static_assert(std::is_floating_point
<_RealType
>::value
,
5509 "result_type must be a floating point type");
5512 /** The type of the range of the distribution. */
5513 typedef _RealType result_type
;
5515 /** Parameter type. */
5518 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
5519 friend class piecewise_constant_distribution
<_RealType
>;
5522 : _M_int(), _M_den(), _M_cp()
5525 template<typename _InputIteratorB
, typename _InputIteratorW
>
5526 param_type(_InputIteratorB __bfirst
,
5527 _InputIteratorB __bend
,
5528 _InputIteratorW __wbegin
);
5530 template<typename _Func
>
5531 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
5533 template<typename _Func
>
5534 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5537 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5538 param_type(const param_type
&) = default;
5539 param_type
& operator=(const param_type
&) = default;
5541 std::vector
<_RealType
>
5546 std::vector
<_RealType
> __tmp(2);
5547 __tmp
[1] = _RealType(1);
5556 { return _M_den
.empty() ? std::vector
<double>(1, 1.0) : _M_den
; }
5559 operator==(const param_type
& __p1
, const param_type
& __p2
)
5560 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
5563 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5564 { return !(__p1
== __p2
); }
5570 std::vector
<_RealType
> _M_int
;
5571 std::vector
<double> _M_den
;
5572 std::vector
<double> _M_cp
;
5575 piecewise_constant_distribution()
5579 template<typename _InputIteratorB
, typename _InputIteratorW
>
5580 piecewise_constant_distribution(_InputIteratorB __bfirst
,
5581 _InputIteratorB __bend
,
5582 _InputIteratorW __wbegin
)
5583 : _M_param(__bfirst
, __bend
, __wbegin
)
5586 template<typename _Func
>
5587 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
5589 : _M_param(__bl
, __fw
)
5592 template<typename _Func
>
5593 piecewise_constant_distribution(size_t __nw
,
5594 _RealType __xmin
, _RealType __xmax
,
5596 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5600 piecewise_constant_distribution(const param_type
& __p
)
5605 * @brief Resets the distribution state.
5612 * @brief Returns a vector of the intervals.
5614 std::vector
<_RealType
>
5617 if (_M_param
._M_int
.empty())
5619 std::vector
<_RealType
> __tmp(2);
5620 __tmp
[1] = _RealType(1);
5624 return _M_param
._M_int
;
5628 * @brief Returns a vector of the probability densities.
5633 return _M_param
._M_den
.empty()
5634 ? std::vector
<double>(1, 1.0) : _M_param
._M_den
;
5638 * @brief Returns the parameter set of the distribution.
5642 { return _M_param
; }
5645 * @brief Sets the parameter set of the distribution.
5646 * @param __param The new parameter set of the distribution.
5649 param(const param_type
& __param
)
5650 { _M_param
= __param
; }
5653 * @brief Returns the greatest lower bound value of the distribution.
5658 return _M_param
._M_int
.empty()
5659 ? result_type(0) : _M_param
._M_int
.front();
5663 * @brief Returns the least upper bound value of the distribution.
5668 return _M_param
._M_int
.empty()
5669 ? result_type(1) : _M_param
._M_int
.back();
5673 * @brief Generating functions.
5675 template<typename _UniformRandomNumberGenerator
>
5677 operator()(_UniformRandomNumberGenerator
& __urng
)
5678 { return this->operator()(__urng
, _M_param
); }
5680 template<typename _UniformRandomNumberGenerator
>
5682 operator()(_UniformRandomNumberGenerator
& __urng
,
5683 const param_type
& __p
);
5685 template<typename _ForwardIterator
,
5686 typename _UniformRandomNumberGenerator
>
5688 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5689 _UniformRandomNumberGenerator
& __urng
)
5690 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5692 template<typename _ForwardIterator
,
5693 typename _UniformRandomNumberGenerator
>
5695 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5696 _UniformRandomNumberGenerator
& __urng
,
5697 const param_type
& __p
)
5698 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5700 template<typename _UniformRandomNumberGenerator
>
5702 __generate(result_type
* __f
, result_type
* __t
,
5703 _UniformRandomNumberGenerator
& __urng
,
5704 const param_type
& __p
)
5705 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5708 * @brief Return true if two piecewise constant distributions have the
5712 operator==(const piecewise_constant_distribution
& __d1
,
5713 const piecewise_constant_distribution
& __d2
)
5714 { return __d1
._M_param
== __d2
._M_param
; }
5717 * @brief Inserts a %piecewise_constant_distribution random
5718 * number distribution @p __x into the output stream @p __os.
5720 * @param __os An output stream.
5721 * @param __x A %piecewise_constant_distribution random number
5724 * @returns The output stream with the state of @p __x inserted or in
5727 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5728 friend std::basic_ostream
<_CharT
, _Traits
>&
5729 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5730 const std::piecewise_constant_distribution
<_RealType1
>& __x
);
5733 * @brief Extracts a %piecewise_constant_distribution random
5734 * number distribution @p __x from the input stream @p __is.
5736 * @param __is An input stream.
5737 * @param __x A %piecewise_constant_distribution random number
5740 * @returns The input stream with @p __x extracted or in an error
5743 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5744 friend std::basic_istream
<_CharT
, _Traits
>&
5745 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5746 std::piecewise_constant_distribution
<_RealType1
>& __x
);
5749 template<typename _ForwardIterator
,
5750 typename _UniformRandomNumberGenerator
>
5752 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5753 _UniformRandomNumberGenerator
& __urng
,
5754 const param_type
& __p
);
5756 param_type _M_param
;
5760 * @brief Return true if two piecewise constant distributions have
5761 * different parameters.
5763 template<typename _RealType
>
5765 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5766 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5767 { return !(__d1
== __d2
); }
5771 * @brief A piecewise_linear_distribution random number distribution.
5773 * The formula for the piecewise linear probability mass function is
5776 template<typename _RealType
= double>
5777 class piecewise_linear_distribution
5779 static_assert(std::is_floating_point
<_RealType
>::value
,
5780 "result_type must be a floating point type");
5783 /** The type of the range of the distribution. */
5784 typedef _RealType result_type
;
5786 /** Parameter type. */
5789 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5790 friend class piecewise_linear_distribution
<_RealType
>;
5793 : _M_int(), _M_den(), _M_cp(), _M_m()
5796 template<typename _InputIteratorB
, typename _InputIteratorW
>
5797 param_type(_InputIteratorB __bfirst
,
5798 _InputIteratorB __bend
,
5799 _InputIteratorW __wbegin
);
5801 template<typename _Func
>
5802 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5804 template<typename _Func
>
5805 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5808 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5809 param_type(const param_type
&) = default;
5810 param_type
& operator=(const param_type
&) = default;
5812 std::vector
<_RealType
>
5817 std::vector
<_RealType
> __tmp(2);
5818 __tmp
[1] = _RealType(1);
5827 { return _M_den
.empty() ? std::vector
<double>(2, 1.0) : _M_den
; }
5830 operator==(const param_type
& __p1
, const param_type
& __p2
)
5831 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
5834 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5835 { return !(__p1
== __p2
); }
5841 std::vector
<_RealType
> _M_int
;
5842 std::vector
<double> _M_den
;
5843 std::vector
<double> _M_cp
;
5844 std::vector
<double> _M_m
;
5847 piecewise_linear_distribution()
5851 template<typename _InputIteratorB
, typename _InputIteratorW
>
5852 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5853 _InputIteratorB __bend
,
5854 _InputIteratorW __wbegin
)
5855 : _M_param(__bfirst
, __bend
, __wbegin
)
5858 template<typename _Func
>
5859 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5861 : _M_param(__bl
, __fw
)
5864 template<typename _Func
>
5865 piecewise_linear_distribution(size_t __nw
,
5866 _RealType __xmin
, _RealType __xmax
,
5868 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5872 piecewise_linear_distribution(const param_type
& __p
)
5877 * Resets the distribution state.
5884 * @brief Return the intervals of the distribution.
5886 std::vector
<_RealType
>
5889 if (_M_param
._M_int
.empty())
5891 std::vector
<_RealType
> __tmp(2);
5892 __tmp
[1] = _RealType(1);
5896 return _M_param
._M_int
;
5900 * @brief Return a vector of the probability densities of the
5906 return _M_param
._M_den
.empty()
5907 ? std::vector
<double>(2, 1.0) : _M_param
._M_den
;
5911 * @brief Returns the parameter set of the distribution.
5915 { return _M_param
; }
5918 * @brief Sets the parameter set of the distribution.
5919 * @param __param The new parameter set of the distribution.
5922 param(const param_type
& __param
)
5923 { _M_param
= __param
; }
5926 * @brief Returns the greatest lower bound value of the distribution.
5931 return _M_param
._M_int
.empty()
5932 ? result_type(0) : _M_param
._M_int
.front();
5936 * @brief Returns the least upper bound value of the distribution.
5941 return _M_param
._M_int
.empty()
5942 ? result_type(1) : _M_param
._M_int
.back();
5946 * @brief Generating functions.
5948 template<typename _UniformRandomNumberGenerator
>
5950 operator()(_UniformRandomNumberGenerator
& __urng
)
5951 { return this->operator()(__urng
, _M_param
); }
5953 template<typename _UniformRandomNumberGenerator
>
5955 operator()(_UniformRandomNumberGenerator
& __urng
,
5956 const param_type
& __p
);
5958 template<typename _ForwardIterator
,
5959 typename _UniformRandomNumberGenerator
>
5961 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5962 _UniformRandomNumberGenerator
& __urng
)
5963 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5965 template<typename _ForwardIterator
,
5966 typename _UniformRandomNumberGenerator
>
5968 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5969 _UniformRandomNumberGenerator
& __urng
,
5970 const param_type
& __p
)
5971 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5973 template<typename _UniformRandomNumberGenerator
>
5975 __generate(result_type
* __f
, result_type
* __t
,
5976 _UniformRandomNumberGenerator
& __urng
,
5977 const param_type
& __p
)
5978 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5981 * @brief Return true if two piecewise linear distributions have the
5985 operator==(const piecewise_linear_distribution
& __d1
,
5986 const piecewise_linear_distribution
& __d2
)
5987 { return __d1
._M_param
== __d2
._M_param
; }
5990 * @brief Inserts a %piecewise_linear_distribution random number
5991 * distribution @p __x into the output stream @p __os.
5993 * @param __os An output stream.
5994 * @param __x A %piecewise_linear_distribution random number
5997 * @returns The output stream with the state of @p __x inserted or in
6000 template<typename _RealType1
, typename _CharT
, typename _Traits
>
6001 friend std::basic_ostream
<_CharT
, _Traits
>&
6002 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
6003 const std::piecewise_linear_distribution
<_RealType1
>& __x
);
6006 * @brief Extracts a %piecewise_linear_distribution random number
6007 * distribution @p __x from the input stream @p __is.
6009 * @param __is An input stream.
6010 * @param __x A %piecewise_linear_distribution random number
6013 * @returns The input stream with @p __x extracted or in an error
6016 template<typename _RealType1
, typename _CharT
, typename _Traits
>
6017 friend std::basic_istream
<_CharT
, _Traits
>&
6018 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
6019 std::piecewise_linear_distribution
<_RealType1
>& __x
);
6022 template<typename _ForwardIterator
,
6023 typename _UniformRandomNumberGenerator
>
6025 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
6026 _UniformRandomNumberGenerator
& __urng
,
6027 const param_type
& __p
);
6029 param_type _M_param
;
6033 * @brief Return true if two piecewise linear distributions have
6034 * different parameters.
6036 template<typename _RealType
>
6038 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
6039 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
6040 { return !(__d1
== __d2
); }
6043 /* @} */ // group random_distributions_poisson
6045 /* @} */ // group random_distributions
6048 * @addtogroup random_utilities Random Number Utilities
6054 * @brief The seed_seq class generates sequences of seeds for random
6055 * number generators.
6060 /** The type of the seed vales. */
6061 typedef uint_least32_t result_type
;
6063 /** Default constructor. */
6068 template<typename _IntType
>
6069 seed_seq(std::initializer_list
<_IntType
> il
);
6071 template<typename _InputIterator
>
6072 seed_seq(_InputIterator __begin
, _InputIterator __end
);
6074 // generating functions
6075 template<typename _RandomAccessIterator
>
6077 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
6079 // property functions
6080 size_t size() const noexcept
6081 { return _M_v
.size(); }
6083 template<typename OutputIterator
>
6085 param(OutputIterator __dest
) const
6086 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
6088 // no copy functions
6089 seed_seq(const seed_seq
&) = delete;
6090 seed_seq
& operator=(const seed_seq
&) = delete;
6093 std::vector
<result_type
> _M_v
;
6096 /* @} */ // group random_utilities
6098 /* @} */ // group random
6100 _GLIBCXX_END_NAMESPACE_VERSION