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 @p __s. The default seed value
272 * @param __s The initial seed value.
275 linear_congruential_engine(result_type __s
= default_seed
)
279 * @brief Constructs a %linear_congruential_engine random number
280 * generator engine seeded from the seed sequence @p __q.
282 * @param __q the seed sequence.
284 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
286 linear_congruential_engine(_Sseq
& __q
)
290 * @brief Reseeds the %linear_congruential_engine random number generator
291 * engine sequence to the seed @p __s.
293 * @param __s The new seed.
296 seed(result_type __s
= default_seed
);
299 * @brief Reseeds the %linear_congruential_engine random number generator
301 * sequence using values from the seed sequence @p __q.
303 * @param __q the seed sequence.
305 template<typename _Sseq
>
310 * @brief Gets the smallest possible value in the output range.
312 * The minimum depends on the @p __c parameter: if it is zero, the
313 * minimum generated must be > 0, otherwise 0 is allowed.
315 static constexpr result_type
317 { return __c
== 0u ? 1u : 0u; }
320 * @brief Gets the largest possible value in the output range.
322 static constexpr result_type
327 * @brief Discard a sequence of random numbers.
330 discard(unsigned long long __z
)
332 for (; __z
!= 0ULL; --__z
)
337 * @brief Gets the next random number in the sequence.
342 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
347 * @brief Compares two linear congruential random number generator
348 * objects of the same type for equality.
350 * @param __lhs A linear congruential random number generator object.
351 * @param __rhs Another linear congruential random number generator
354 * @returns true if the infinite sequences of generated values
355 * would be equal, false otherwise.
358 operator==(const linear_congruential_engine
& __lhs
,
359 const linear_congruential_engine
& __rhs
)
360 { return __lhs
._M_x
== __rhs
._M_x
; }
363 * @brief Writes the textual representation of the state x(i) of x to
366 * @param __os The output stream.
367 * @param __lcr A % linear_congruential_engine random number generator.
370 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
371 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
372 friend std::basic_ostream
<_CharT
, _Traits
>&
373 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
374 const std::linear_congruential_engine
<_UIntType1
,
375 __a1
, __c1
, __m1
>& __lcr
);
378 * @brief Sets the state of the engine by reading its textual
379 * representation from @p __is.
381 * The textual representation must have been previously written using
382 * an output stream whose imbued locale and whose type's template
383 * specialization arguments _CharT and _Traits were the same as those
386 * @param __is The input stream.
387 * @param __lcr A % linear_congruential_engine random number generator.
390 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
391 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
392 friend std::basic_istream
<_CharT
, _Traits
>&
393 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
394 std::linear_congruential_engine
<_UIntType1
, __a1
,
402 * @brief Compares two linear congruential random number generator
403 * objects of the same type for inequality.
405 * @param __lhs A linear congruential random number generator object.
406 * @param __rhs Another linear congruential random number generator
409 * @returns true if the infinite sequences of generated values
410 * would be different, false otherwise.
412 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
414 operator!=(const std::linear_congruential_engine
<_UIntType
, __a
,
416 const std::linear_congruential_engine
<_UIntType
, __a
,
418 { return !(__lhs
== __rhs
); }
422 * A generalized feedback shift register discrete random number generator.
424 * This algorithm avoids multiplication and division and is designed to be
425 * friendly to a pipelined architecture. If the parameters are chosen
426 * correctly, this generator will produce numbers with a very long period and
427 * fairly good apparent entropy, although still not cryptographically strong.
429 * The best way to use this generator is with the predefined mt19937 class.
431 * This algorithm was originally invented by Makoto Matsumoto and
434 * @tparam __w Word size, the number of bits in each element of
436 * @tparam __n The degree of recursion.
437 * @tparam __m The period parameter.
438 * @tparam __r The separation point bit index.
439 * @tparam __a The last row of the twist matrix.
440 * @tparam __u The first right-shift tempering matrix parameter.
441 * @tparam __d The first right-shift tempering matrix mask.
442 * @tparam __s The first left-shift tempering matrix parameter.
443 * @tparam __b The first left-shift tempering matrix mask.
444 * @tparam __t The second left-shift tempering matrix parameter.
445 * @tparam __c The second left-shift tempering matrix mask.
446 * @tparam __l The second right-shift tempering matrix parameter.
447 * @tparam __f Initialization multiplier.
449 template<typename _UIntType
, size_t __w
,
450 size_t __n
, size_t __m
, size_t __r
,
451 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
452 _UIntType __b
, size_t __t
,
453 _UIntType __c
, size_t __l
, _UIntType __f
>
454 class mersenne_twister_engine
456 static_assert(std::is_unsigned
<_UIntType
>::value
,
457 "result_type must be an unsigned integral type");
458 static_assert(1u <= __m
&& __m
<= __n
,
459 "template argument substituting __m out of bounds");
460 static_assert(__r
<= __w
, "template argument substituting "
462 static_assert(__u
<= __w
, "template argument substituting "
464 static_assert(__s
<= __w
, "template argument substituting "
466 static_assert(__t
<= __w
, "template argument substituting "
468 static_assert(__l
<= __w
, "template argument substituting "
470 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
471 "template argument substituting __w out of bound");
472 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
473 "template argument substituting __a out of bound");
474 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
475 "template argument substituting __b out of bound");
476 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
477 "template argument substituting __c out of bound");
478 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
479 "template argument substituting __d out of bound");
480 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
481 "template argument substituting __f out of bound");
483 template<typename _Sseq
>
484 using _If_seed_seq
= typename enable_if
<__detail::__is_seed_seq
<
485 _Sseq
, mersenne_twister_engine
, _UIntType
>::value
>::type
;
488 /** The type of the generated random value. */
489 typedef _UIntType result_type
;
492 static constexpr size_t word_size
= __w
;
493 static constexpr size_t state_size
= __n
;
494 static constexpr size_t shift_size
= __m
;
495 static constexpr size_t mask_bits
= __r
;
496 static constexpr result_type xor_mask
= __a
;
497 static constexpr size_t tempering_u
= __u
;
498 static constexpr result_type tempering_d
= __d
;
499 static constexpr size_t tempering_s
= __s
;
500 static constexpr result_type tempering_b
= __b
;
501 static constexpr size_t tempering_t
= __t
;
502 static constexpr result_type tempering_c
= __c
;
503 static constexpr size_t tempering_l
= __l
;
504 static constexpr result_type initialization_multiplier
= __f
;
505 static constexpr result_type default_seed
= 5489u;
507 // constructors and member function
509 mersenne_twister_engine(result_type __sd
= default_seed
)
513 * @brief Constructs a %mersenne_twister_engine random number generator
514 * engine seeded from the seed sequence @p __q.
516 * @param __q the seed sequence.
518 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
520 mersenne_twister_engine(_Sseq
& __q
)
524 seed(result_type __sd
= default_seed
);
526 template<typename _Sseq
>
531 * @brief Gets the smallest possible value in the output range.
533 static constexpr result_type
538 * @brief Gets the largest possible value in the output range.
540 static constexpr result_type
542 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
545 * @brief Discard a sequence of random numbers.
548 discard(unsigned long long __z
);
554 * @brief Compares two % mersenne_twister_engine random number generator
555 * objects of the same type for equality.
557 * @param __lhs A % mersenne_twister_engine random number generator
559 * @param __rhs Another % mersenne_twister_engine random number
562 * @returns true if the infinite sequences of generated values
563 * would be equal, false otherwise.
566 operator==(const mersenne_twister_engine
& __lhs
,
567 const mersenne_twister_engine
& __rhs
)
568 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
)
569 && __lhs
._M_p
== __rhs
._M_p
); }
572 * @brief Inserts the current state of a % mersenne_twister_engine
573 * random number generator engine @p __x into the output stream
576 * @param __os An output stream.
577 * @param __x A % mersenne_twister_engine random number generator
580 * @returns The output stream with the state of @p __x inserted or in
583 template<typename _UIntType1
,
584 size_t __w1
, size_t __n1
,
585 size_t __m1
, size_t __r1
,
586 _UIntType1 __a1
, size_t __u1
,
587 _UIntType1 __d1
, size_t __s1
,
588 _UIntType1 __b1
, size_t __t1
,
589 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
590 typename _CharT
, typename _Traits
>
591 friend std::basic_ostream
<_CharT
, _Traits
>&
592 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
593 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
594 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
598 * @brief Extracts the current state of a % mersenne_twister_engine
599 * random number generator engine @p __x from the input stream
602 * @param __is An input stream.
603 * @param __x A % mersenne_twister_engine random number generator
606 * @returns The input stream with the state of @p __x extracted or in
609 template<typename _UIntType1
,
610 size_t __w1
, size_t __n1
,
611 size_t __m1
, size_t __r1
,
612 _UIntType1 __a1
, size_t __u1
,
613 _UIntType1 __d1
, size_t __s1
,
614 _UIntType1 __b1
, size_t __t1
,
615 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
616 typename _CharT
, typename _Traits
>
617 friend std::basic_istream
<_CharT
, _Traits
>&
618 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
619 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
620 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
626 _UIntType _M_x
[state_size
];
631 * @brief Compares two % mersenne_twister_engine random number generator
632 * objects of the same type for inequality.
634 * @param __lhs A % mersenne_twister_engine random number generator
636 * @param __rhs Another % mersenne_twister_engine random number
639 * @returns true if the infinite sequences of generated values
640 * would be different, false otherwise.
642 template<typename _UIntType
, size_t __w
,
643 size_t __n
, size_t __m
, size_t __r
,
644 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
645 _UIntType __b
, size_t __t
,
646 _UIntType __c
, size_t __l
, _UIntType __f
>
648 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
649 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
650 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
651 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
652 { return !(__lhs
== __rhs
); }
656 * @brief The Marsaglia-Zaman generator.
658 * This is a model of a Generalized Fibonacci discrete random number
659 * generator, sometimes referred to as the SWC generator.
661 * A discrete random number generator that produces pseudorandom
664 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
667 * The size of the state is @f$r@f$
668 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
670 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
671 class subtract_with_carry_engine
673 static_assert(std::is_unsigned
<_UIntType
>::value
,
674 "result_type must be an unsigned integral type");
675 static_assert(0u < __s
&& __s
< __r
,
677 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
678 "template argument substituting __w out of bounds");
680 template<typename _Sseq
>
681 using _If_seed_seq
= typename enable_if
<__detail::__is_seed_seq
<
682 _Sseq
, subtract_with_carry_engine
, _UIntType
>::value
>::type
;
685 /** The type of the generated random value. */
686 typedef _UIntType result_type
;
689 static constexpr size_t word_size
= __w
;
690 static constexpr size_t short_lag
= __s
;
691 static constexpr size_t long_lag
= __r
;
692 static constexpr result_type default_seed
= 19780503u;
695 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
696 * random number generator.
699 subtract_with_carry_engine(result_type __sd
= default_seed
)
703 * @brief Constructs a %subtract_with_carry_engine random number engine
704 * seeded from the seed sequence @p __q.
706 * @param __q the seed sequence.
708 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
710 subtract_with_carry_engine(_Sseq
& __q
)
714 * @brief Seeds the initial state @f$x_0@f$ of the random number
717 * N1688[4.19] modifies this as follows. If @p __value == 0,
718 * sets value to 19780503. In any case, with a linear
719 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
720 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
721 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
722 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
723 * set carry to 1, otherwise sets carry to 0.
726 seed(result_type __sd
= default_seed
);
729 * @brief Seeds the initial state @f$x_0@f$ of the
730 * % subtract_with_carry_engine random number generator.
732 template<typename _Sseq
>
737 * @brief Gets the inclusive minimum value of the range of random
738 * integers returned by this generator.
740 static constexpr result_type
745 * @brief Gets the inclusive maximum value of the range of random
746 * integers returned by this generator.
748 static constexpr result_type
750 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
753 * @brief Discard a sequence of random numbers.
756 discard(unsigned long long __z
)
758 for (; __z
!= 0ULL; --__z
)
763 * @brief Gets the next random number in the sequence.
769 * @brief Compares two % subtract_with_carry_engine random number
770 * generator objects of the same type for equality.
772 * @param __lhs A % subtract_with_carry_engine random number generator
774 * @param __rhs Another % subtract_with_carry_engine random number
777 * @returns true if the infinite sequences of generated values
778 * would be equal, false otherwise.
781 operator==(const subtract_with_carry_engine
& __lhs
,
782 const subtract_with_carry_engine
& __rhs
)
783 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
)
784 && __lhs
._M_carry
== __rhs
._M_carry
785 && __lhs
._M_p
== __rhs
._M_p
); }
788 * @brief Inserts the current state of a % subtract_with_carry_engine
789 * random number generator engine @p __x into the output stream
792 * @param __os An output stream.
793 * @param __x A % subtract_with_carry_engine random number generator
796 * @returns The output stream with the state of @p __x inserted or in
799 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
800 typename _CharT
, typename _Traits
>
801 friend std::basic_ostream
<_CharT
, _Traits
>&
802 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
803 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
807 * @brief Extracts the current state of a % subtract_with_carry_engine
808 * random number generator engine @p __x from the input stream
811 * @param __is An input stream.
812 * @param __x A % subtract_with_carry_engine random number generator
815 * @returns The input stream with the state of @p __x extracted or in
818 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
819 typename _CharT
, typename _Traits
>
820 friend std::basic_istream
<_CharT
, _Traits
>&
821 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
822 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
826 /// The state of the generator. This is a ring buffer.
827 _UIntType _M_x
[long_lag
];
828 _UIntType _M_carry
; ///< The carry
829 size_t _M_p
; ///< Current index of x(i - r).
833 * @brief Compares two % subtract_with_carry_engine random number
834 * generator objects of the same type for inequality.
836 * @param __lhs A % subtract_with_carry_engine random number generator
838 * @param __rhs Another % subtract_with_carry_engine random number
841 * @returns true if the infinite sequences of generated values
842 * would be different, false otherwise.
844 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
846 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
848 const std::subtract_with_carry_engine
<_UIntType
, __w
,
850 { return !(__lhs
== __rhs
); }
854 * Produces random numbers from some base engine by discarding blocks of
857 * 0 <= @p __r <= @p __p
859 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
860 class discard_block_engine
862 static_assert(1 <= __r
&& __r
<= __p
,
863 "template argument substituting __r out of bounds");
866 /** The type of the generated random value. */
867 typedef typename
_RandomNumberEngine::result_type result_type
;
869 template<typename _Sseq
>
870 using _If_seed_seq
= typename enable_if
<__detail::__is_seed_seq
<
871 _Sseq
, discard_block_engine
, result_type
>::value
>::type
;
874 static constexpr size_t block_size
= __p
;
875 static constexpr size_t used_block
= __r
;
878 * @brief Constructs a default %discard_block_engine engine.
880 * The underlying engine is default constructed as well.
882 discard_block_engine()
883 : _M_b(), _M_n(0) { }
886 * @brief Copy constructs a %discard_block_engine engine.
888 * Copies an existing base class random number generator.
889 * @param __rng An existing (base class) engine object.
892 discard_block_engine(const _RandomNumberEngine
& __rng
)
893 : _M_b(__rng
), _M_n(0) { }
896 * @brief Move constructs a %discard_block_engine engine.
898 * Copies an existing base class random number generator.
899 * @param __rng An existing (base class) engine object.
902 discard_block_engine(_RandomNumberEngine
&& __rng
)
903 : _M_b(std::move(__rng
)), _M_n(0) { }
906 * @brief Seed constructs a %discard_block_engine engine.
908 * Constructs the underlying generator engine seeded with @p __s.
909 * @param __s A seed value for the base class engine.
912 discard_block_engine(result_type __s
)
913 : _M_b(__s
), _M_n(0) { }
916 * @brief Generator construct a %discard_block_engine engine.
918 * @param __q A seed sequence.
920 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
922 discard_block_engine(_Sseq
& __q
)
927 * @brief Reseeds the %discard_block_engine object with the default
928 * seed for the underlying base class generator engine.
938 * @brief Reseeds the %discard_block_engine object with the default
939 * seed for the underlying base class generator engine.
942 seed(result_type __s
)
949 * @brief Reseeds the %discard_block_engine object with the given seed
951 * @param __q A seed generator function.
953 template<typename _Sseq
>
962 * @brief Gets a const reference to the underlying generator engine
965 const _RandomNumberEngine
&
966 base() const noexcept
970 * @brief Gets the minimum value in the generated random number range.
972 static constexpr result_type
974 { return _RandomNumberEngine::min(); }
977 * @brief Gets the maximum value in the generated random number range.
979 static constexpr result_type
981 { return _RandomNumberEngine::max(); }
984 * @brief Discard a sequence of random numbers.
987 discard(unsigned long long __z
)
989 for (; __z
!= 0ULL; --__z
)
994 * @brief Gets the next value in the generated random number sequence.
1000 * @brief Compares two %discard_block_engine random number generator
1001 * objects of the same type for equality.
1003 * @param __lhs A %discard_block_engine random number generator object.
1004 * @param __rhs Another %discard_block_engine random number generator
1007 * @returns true if the infinite sequences of generated values
1008 * would be equal, false otherwise.
1011 operator==(const discard_block_engine
& __lhs
,
1012 const discard_block_engine
& __rhs
)
1013 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
1016 * @brief Inserts the current state of a %discard_block_engine random
1017 * number generator engine @p __x into the output stream
1020 * @param __os An output stream.
1021 * @param __x A %discard_block_engine random number generator engine.
1023 * @returns The output stream with the state of @p __x inserted or in
1026 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1027 typename _CharT
, typename _Traits
>
1028 friend std::basic_ostream
<_CharT
, _Traits
>&
1029 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1030 const std::discard_block_engine
<_RandomNumberEngine1
,
1034 * @brief Extracts the current state of a % subtract_with_carry_engine
1035 * random number generator engine @p __x from the input stream
1038 * @param __is An input stream.
1039 * @param __x A %discard_block_engine random number generator engine.
1041 * @returns The input stream with the state of @p __x extracted or in
1044 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1045 typename _CharT
, typename _Traits
>
1046 friend std::basic_istream
<_CharT
, _Traits
>&
1047 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1048 std::discard_block_engine
<_RandomNumberEngine1
,
1052 _RandomNumberEngine _M_b
;
1057 * @brief Compares two %discard_block_engine random number generator
1058 * objects of the same type for inequality.
1060 * @param __lhs A %discard_block_engine random number generator object.
1061 * @param __rhs Another %discard_block_engine random number generator
1064 * @returns true if the infinite sequences of generated values
1065 * would be different, false otherwise.
1067 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
1069 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1071 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1073 { return !(__lhs
== __rhs
); }
1077 * Produces random numbers by combining random numbers from some base
1078 * engine to produce random numbers with a specifies number of bits @p __w.
1080 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1081 class independent_bits_engine
1083 static_assert(std::is_unsigned
<_UIntType
>::value
,
1084 "result_type must be an unsigned integral type");
1085 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
1086 "template argument substituting __w out of bounds");
1088 template<typename _Sseq
>
1089 using _If_seed_seq
= typename enable_if
<__detail::__is_seed_seq
<
1090 _Sseq
, independent_bits_engine
, _UIntType
>::value
>::type
;
1093 /** The type of the generated random value. */
1094 typedef _UIntType result_type
;
1097 * @brief Constructs a default %independent_bits_engine engine.
1099 * The underlying engine is default constructed as well.
1101 independent_bits_engine()
1105 * @brief Copy constructs a %independent_bits_engine engine.
1107 * Copies an existing base class random number generator.
1108 * @param __rng An existing (base class) engine object.
1111 independent_bits_engine(const _RandomNumberEngine
& __rng
)
1115 * @brief Move constructs a %independent_bits_engine engine.
1117 * Copies an existing base class random number generator.
1118 * @param __rng An existing (base class) engine object.
1121 independent_bits_engine(_RandomNumberEngine
&& __rng
)
1122 : _M_b(std::move(__rng
)) { }
1125 * @brief Seed constructs a %independent_bits_engine engine.
1127 * Constructs the underlying generator engine seeded with @p __s.
1128 * @param __s A seed value for the base class engine.
1131 independent_bits_engine(result_type __s
)
1135 * @brief Generator construct a %independent_bits_engine engine.
1137 * @param __q A seed sequence.
1139 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
1141 independent_bits_engine(_Sseq
& __q
)
1146 * @brief Reseeds the %independent_bits_engine object with the default
1147 * seed for the underlying base class generator engine.
1154 * @brief Reseeds the %independent_bits_engine object with the default
1155 * seed for the underlying base class generator engine.
1158 seed(result_type __s
)
1162 * @brief Reseeds the %independent_bits_engine object with the given
1164 * @param __q A seed generator function.
1166 template<typename _Sseq
>
1172 * @brief Gets a const reference to the underlying generator engine
1175 const _RandomNumberEngine
&
1176 base() const noexcept
1180 * @brief Gets the minimum value in the generated random number range.
1182 static constexpr result_type
1187 * @brief Gets the maximum value in the generated random number range.
1189 static constexpr result_type
1191 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1194 * @brief Discard a sequence of random numbers.
1197 discard(unsigned long long __z
)
1199 for (; __z
!= 0ULL; --__z
)
1204 * @brief Gets the next value in the generated random number sequence.
1210 * @brief Compares two %independent_bits_engine random number generator
1211 * objects of the same type for equality.
1213 * @param __lhs A %independent_bits_engine random number generator
1215 * @param __rhs Another %independent_bits_engine random number generator
1218 * @returns true if the infinite sequences of generated values
1219 * would be equal, false otherwise.
1222 operator==(const independent_bits_engine
& __lhs
,
1223 const independent_bits_engine
& __rhs
)
1224 { return __lhs
._M_b
== __rhs
._M_b
; }
1227 * @brief Extracts the current state of a % subtract_with_carry_engine
1228 * random number generator engine @p __x from the input stream
1231 * @param __is An input stream.
1232 * @param __x A %independent_bits_engine random number generator
1235 * @returns The input stream with the state of @p __x extracted or in
1238 template<typename _CharT
, typename _Traits
>
1239 friend std::basic_istream
<_CharT
, _Traits
>&
1240 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1241 std::independent_bits_engine
<_RandomNumberEngine
,
1242 __w
, _UIntType
>& __x
)
1249 _RandomNumberEngine _M_b
;
1253 * @brief Compares two %independent_bits_engine random number generator
1254 * objects of the same type for inequality.
1256 * @param __lhs A %independent_bits_engine random number generator
1258 * @param __rhs Another %independent_bits_engine random number generator
1261 * @returns true if the infinite sequences of generated values
1262 * would be different, false otherwise.
1264 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1266 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1268 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1270 { return !(__lhs
== __rhs
); }
1273 * @brief Inserts the current state of a %independent_bits_engine random
1274 * number generator engine @p __x into the output stream @p __os.
1276 * @param __os An output stream.
1277 * @param __x A %independent_bits_engine random number generator engine.
1279 * @returns The output stream with the state of @p __x inserted or in
1282 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1283 typename _CharT
, typename _Traits
>
1284 std::basic_ostream
<_CharT
, _Traits
>&
1285 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1286 const std::independent_bits_engine
<_RandomNumberEngine
,
1287 __w
, _UIntType
>& __x
)
1295 * @brief Produces random numbers by combining random numbers from some
1296 * base engine to produce random numbers with a specifies number of bits
1299 template<typename _RandomNumberEngine
, size_t __k
>
1300 class shuffle_order_engine
1302 static_assert(1u <= __k
, "template argument substituting "
1303 "__k out of bound");
1306 /** The type of the generated random value. */
1307 typedef typename
_RandomNumberEngine::result_type result_type
;
1309 template<typename _Sseq
>
1310 using _If_seed_seq
= typename enable_if
<__detail::__is_seed_seq
<
1311 _Sseq
, shuffle_order_engine
, result_type
>::value
>::type
;
1313 static constexpr size_t table_size
= __k
;
1316 * @brief Constructs a default %shuffle_order_engine engine.
1318 * The underlying engine is default constructed as well.
1320 shuffle_order_engine()
1322 { _M_initialize(); }
1325 * @brief Copy constructs a %shuffle_order_engine engine.
1327 * Copies an existing base class random number generator.
1328 * @param __rng An existing (base class) engine object.
1331 shuffle_order_engine(const _RandomNumberEngine
& __rng
)
1333 { _M_initialize(); }
1336 * @brief Move constructs a %shuffle_order_engine engine.
1338 * Copies an existing base class random number generator.
1339 * @param __rng An existing (base class) engine object.
1342 shuffle_order_engine(_RandomNumberEngine
&& __rng
)
1343 : _M_b(std::move(__rng
))
1344 { _M_initialize(); }
1347 * @brief Seed constructs a %shuffle_order_engine engine.
1349 * Constructs the underlying generator engine seeded with @p __s.
1350 * @param __s A seed value for the base class engine.
1353 shuffle_order_engine(result_type __s
)
1355 { _M_initialize(); }
1358 * @brief Generator construct a %shuffle_order_engine engine.
1360 * @param __q A seed sequence.
1362 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
1364 shuffle_order_engine(_Sseq
& __q
)
1366 { _M_initialize(); }
1369 * @brief Reseeds the %shuffle_order_engine object with the default seed
1370 for the underlying base class generator engine.
1380 * @brief Reseeds the %shuffle_order_engine object with the default seed
1381 * for the underlying base class generator engine.
1384 seed(result_type __s
)
1391 * @brief Reseeds the %shuffle_order_engine object with the given seed
1393 * @param __q A seed generator function.
1395 template<typename _Sseq
>
1404 * Gets a const reference to the underlying generator engine object.
1406 const _RandomNumberEngine
&
1407 base() const noexcept
1411 * Gets the minimum value in the generated random number range.
1413 static constexpr result_type
1415 { return _RandomNumberEngine::min(); }
1418 * Gets the maximum value in the generated random number range.
1420 static constexpr result_type
1422 { return _RandomNumberEngine::max(); }
1425 * Discard a sequence of random numbers.
1428 discard(unsigned long long __z
)
1430 for (; __z
!= 0ULL; --__z
)
1435 * Gets the next value in the generated random number sequence.
1441 * Compares two %shuffle_order_engine random number generator objects
1442 * of the same type for equality.
1444 * @param __lhs A %shuffle_order_engine random number generator object.
1445 * @param __rhs Another %shuffle_order_engine random number generator
1448 * @returns true if the infinite sequences of generated values
1449 * would be equal, false otherwise.
1452 operator==(const shuffle_order_engine
& __lhs
,
1453 const shuffle_order_engine
& __rhs
)
1454 { return (__lhs
._M_b
== __rhs
._M_b
1455 && std::equal(__lhs
._M_v
, __lhs
._M_v
+ __k
, __rhs
._M_v
)
1456 && __lhs
._M_y
== __rhs
._M_y
); }
1459 * @brief Inserts the current state of a %shuffle_order_engine random
1460 * number generator engine @p __x into the output stream
1463 * @param __os An output stream.
1464 * @param __x A %shuffle_order_engine random number generator engine.
1466 * @returns The output stream with the state of @p __x inserted or in
1469 template<typename _RandomNumberEngine1
, size_t __k1
,
1470 typename _CharT
, typename _Traits
>
1471 friend std::basic_ostream
<_CharT
, _Traits
>&
1472 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1473 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1477 * @brief Extracts the current state of a % subtract_with_carry_engine
1478 * random number generator engine @p __x from the input stream
1481 * @param __is An input stream.
1482 * @param __x A %shuffle_order_engine random number generator engine.
1484 * @returns The input stream with the state of @p __x extracted or in
1487 template<typename _RandomNumberEngine1
, size_t __k1
,
1488 typename _CharT
, typename _Traits
>
1489 friend std::basic_istream
<_CharT
, _Traits
>&
1490 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1491 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>& __x
);
1494 void _M_initialize()
1496 for (size_t __i
= 0; __i
< __k
; ++__i
)
1501 _RandomNumberEngine _M_b
;
1502 result_type _M_v
[__k
];
1507 * Compares two %shuffle_order_engine random number generator objects
1508 * of the same type for inequality.
1510 * @param __lhs A %shuffle_order_engine random number generator object.
1511 * @param __rhs Another %shuffle_order_engine random number generator
1514 * @returns true if the infinite sequences of generated values
1515 * would be different, false otherwise.
1517 template<typename _RandomNumberEngine
, size_t __k
>
1519 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1521 const std::shuffle_order_engine
<_RandomNumberEngine
,
1523 { return !(__lhs
== __rhs
); }
1527 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1529 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1533 * An alternative LCR (Lehmer Generator function).
1535 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1539 * The classic Mersenne Twister.
1542 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1543 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1544 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1546 typedef mersenne_twister_engine
<
1552 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1555 * An alternative Mersenne Twister.
1557 typedef mersenne_twister_engine
<
1560 0xb5026f5aa96619e9ULL
, 29,
1561 0x5555555555555555ULL
, 17,
1562 0x71d67fffeda60000ULL
, 37,
1563 0xfff7eee000000000ULL
, 43,
1564 6364136223846793005ULL> mt19937_64
;
1566 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1569 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1572 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1574 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1576 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1578 typedef minstd_rand0 default_random_engine
;
1581 * A standard interface to a platform-specific non-deterministic
1582 * random number generator (if any are available).
1587 /** The type of the generated random value. */
1588 typedef unsigned int result_type
;
1590 // constructors, destructors and member functions
1592 #ifdef _GLIBCXX_USE_RANDOM_TR1
1595 random_device(const std::string
& __token
= "default")
1606 random_device(const std::string
& __token
= "mt19937")
1607 { _M_init_pretr1(__token
); }
1613 static constexpr result_type
1615 { return std::numeric_limits
<result_type
>::min(); }
1617 static constexpr result_type
1619 { return std::numeric_limits
<result_type
>::max(); }
1622 entropy() const noexcept
1624 #ifdef _GLIBCXX_USE_RANDOM_TR1
1625 return this->_M_getentropy();
1634 #ifdef _GLIBCXX_USE_RANDOM_TR1
1635 return this->_M_getval();
1637 return this->_M_getval_pretr1();
1641 // No copy functions.
1642 random_device(const random_device
&) = delete;
1643 void operator=(const random_device
&) = delete;
1647 void _M_init(const std::string
& __token
);
1648 void _M_init_pretr1(const std::string
& __token
);
1651 result_type
_M_getval();
1652 result_type
_M_getval_pretr1();
1653 double _M_getentropy() const noexcept
;
1662 /* @} */ // group random_generators
1665 * @addtogroup random_distributions Random Number Distributions
1671 * @addtogroup random_distributions_uniform Uniform Distributions
1672 * @ingroup random_distributions
1676 // std::uniform_int_distribution is defined in <bits/uniform_int_dist.h>
1679 * @brief Return true if two uniform integer distributions have
1680 * different parameters.
1682 template<typename _IntType
>
1684 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1685 const std::uniform_int_distribution
<_IntType
>& __d2
)
1686 { return !(__d1
== __d2
); }
1689 * @brief Inserts a %uniform_int_distribution random number
1690 * distribution @p __x into the output stream @p os.
1692 * @param __os An output stream.
1693 * @param __x A %uniform_int_distribution random number distribution.
1695 * @returns The output stream with the state of @p __x inserted or in
1698 template<typename _IntType
, typename _CharT
, typename _Traits
>
1699 std::basic_ostream
<_CharT
, _Traits
>&
1700 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1701 const std::uniform_int_distribution
<_IntType
>&);
1704 * @brief Extracts a %uniform_int_distribution random number distribution
1705 * @p __x from the input stream @p __is.
1707 * @param __is An input stream.
1708 * @param __x A %uniform_int_distribution random number generator engine.
1710 * @returns The input stream with @p __x extracted or in an error state.
1712 template<typename _IntType
, typename _CharT
, typename _Traits
>
1713 std::basic_istream
<_CharT
, _Traits
>&
1714 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1715 std::uniform_int_distribution
<_IntType
>&);
1719 * @brief Uniform continuous distribution for random numbers.
1721 * A continuous random distribution on the range [min, max) with equal
1722 * probability throughout the range. The URNG should be real-valued and
1723 * deliver number in the range [0, 1).
1725 template<typename _RealType
= double>
1726 class uniform_real_distribution
1728 static_assert(std::is_floating_point
<_RealType
>::value
,
1729 "result_type must be a floating point type");
1732 /** The type of the range of the distribution. */
1733 typedef _RealType result_type
;
1735 /** Parameter type. */
1738 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1741 param_type(_RealType __a
= _RealType(0),
1742 _RealType __b
= _RealType(1))
1743 : _M_a(__a
), _M_b(__b
)
1745 __glibcxx_assert(_M_a
<= _M_b
);
1757 operator==(const param_type
& __p1
, const param_type
& __p2
)
1758 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1761 operator!=(const param_type
& __p1
, const param_type
& __p2
)
1762 { return !(__p1
== __p2
); }
1771 * @brief Constructs a uniform_real_distribution object.
1773 * @param __a [IN] The lower bound of the distribution.
1774 * @param __b [IN] The upper bound of the distribution.
1777 uniform_real_distribution(_RealType __a
= _RealType(0),
1778 _RealType __b
= _RealType(1))
1779 : _M_param(__a
, __b
)
1783 uniform_real_distribution(const param_type
& __p
)
1788 * @brief Resets the distribution state.
1790 * Does nothing for the uniform real distribution.
1797 { return _M_param
.a(); }
1801 { return _M_param
.b(); }
1804 * @brief Returns the parameter set of the distribution.
1808 { return _M_param
; }
1811 * @brief Sets the parameter set of the distribution.
1812 * @param __param The new parameter set of the distribution.
1815 param(const param_type
& __param
)
1816 { _M_param
= __param
; }
1819 * @brief Returns the inclusive lower bound of the distribution range.
1823 { return this->a(); }
1826 * @brief Returns the inclusive upper bound of the distribution range.
1830 { return this->b(); }
1833 * @brief Generating functions.
1835 template<typename _UniformRandomNumberGenerator
>
1837 operator()(_UniformRandomNumberGenerator
& __urng
)
1838 { return this->operator()(__urng
, _M_param
); }
1840 template<typename _UniformRandomNumberGenerator
>
1842 operator()(_UniformRandomNumberGenerator
& __urng
,
1843 const param_type
& __p
)
1845 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1847 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1850 template<typename _ForwardIterator
,
1851 typename _UniformRandomNumberGenerator
>
1853 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1854 _UniformRandomNumberGenerator
& __urng
)
1855 { this->__generate(__f
, __t
, __urng
, _M_param
); }
1857 template<typename _ForwardIterator
,
1858 typename _UniformRandomNumberGenerator
>
1860 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1861 _UniformRandomNumberGenerator
& __urng
,
1862 const param_type
& __p
)
1863 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1865 template<typename _UniformRandomNumberGenerator
>
1867 __generate(result_type
* __f
, result_type
* __t
,
1868 _UniformRandomNumberGenerator
& __urng
,
1869 const param_type
& __p
)
1870 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1873 * @brief Return true if two uniform real distributions have
1874 * the same parameters.
1877 operator==(const uniform_real_distribution
& __d1
,
1878 const uniform_real_distribution
& __d2
)
1879 { return __d1
._M_param
== __d2
._M_param
; }
1882 template<typename _ForwardIterator
,
1883 typename _UniformRandomNumberGenerator
>
1885 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
1886 _UniformRandomNumberGenerator
& __urng
,
1887 const param_type
& __p
);
1889 param_type _M_param
;
1893 * @brief Return true if two uniform real distributions have
1894 * different parameters.
1896 template<typename _IntType
>
1898 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1899 const std::uniform_real_distribution
<_IntType
>& __d2
)
1900 { return !(__d1
== __d2
); }
1903 * @brief Inserts a %uniform_real_distribution random number
1904 * distribution @p __x into the output stream @p __os.
1906 * @param __os An output stream.
1907 * @param __x A %uniform_real_distribution random number distribution.
1909 * @returns The output stream with the state of @p __x inserted or in
1912 template<typename _RealType
, typename _CharT
, typename _Traits
>
1913 std::basic_ostream
<_CharT
, _Traits
>&
1914 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1915 const std::uniform_real_distribution
<_RealType
>&);
1918 * @brief Extracts a %uniform_real_distribution random number distribution
1919 * @p __x from the input stream @p __is.
1921 * @param __is An input stream.
1922 * @param __x A %uniform_real_distribution random number generator engine.
1924 * @returns The input stream with @p __x extracted or in an error state.
1926 template<typename _RealType
, typename _CharT
, typename _Traits
>
1927 std::basic_istream
<_CharT
, _Traits
>&
1928 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1929 std::uniform_real_distribution
<_RealType
>&);
1931 /* @} */ // group random_distributions_uniform
1934 * @addtogroup random_distributions_normal Normal Distributions
1935 * @ingroup random_distributions
1940 * @brief A normal continuous distribution for random numbers.
1942 * The formula for the normal probability density function is
1944 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1945 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1948 template<typename _RealType
= double>
1949 class normal_distribution
1951 static_assert(std::is_floating_point
<_RealType
>::value
,
1952 "result_type must be a floating point type");
1955 /** The type of the range of the distribution. */
1956 typedef _RealType result_type
;
1958 /** Parameter type. */
1961 typedef normal_distribution
<_RealType
> distribution_type
;
1964 param_type(_RealType __mean
= _RealType(0),
1965 _RealType __stddev
= _RealType(1))
1966 : _M_mean(__mean
), _M_stddev(__stddev
)
1968 __glibcxx_assert(_M_stddev
> _RealType(0));
1977 { return _M_stddev
; }
1980 operator==(const param_type
& __p1
, const param_type
& __p2
)
1981 { return (__p1
._M_mean
== __p2
._M_mean
1982 && __p1
._M_stddev
== __p2
._M_stddev
); }
1985 operator!=(const param_type
& __p1
, const param_type
& __p2
)
1986 { return !(__p1
== __p2
); }
1990 _RealType _M_stddev
;
1995 * Constructs a normal distribution with parameters @f$mean@f$ and
1996 * standard deviation.
1999 normal_distribution(result_type __mean
= result_type(0),
2000 result_type __stddev
= result_type(1))
2001 : _M_param(__mean
, __stddev
), _M_saved_available(false)
2005 normal_distribution(const param_type
& __p
)
2006 : _M_param(__p
), _M_saved_available(false)
2010 * @brief Resets the distribution state.
2014 { _M_saved_available
= false; }
2017 * @brief Returns the mean of the distribution.
2021 { return _M_param
.mean(); }
2024 * @brief Returns the standard deviation of the distribution.
2028 { return _M_param
.stddev(); }
2031 * @brief Returns the parameter set of the distribution.
2035 { return _M_param
; }
2038 * @brief Sets the parameter set of the distribution.
2039 * @param __param The new parameter set of the distribution.
2042 param(const param_type
& __param
)
2043 { _M_param
= __param
; }
2046 * @brief Returns the greatest lower bound value of the distribution.
2050 { return std::numeric_limits
<result_type
>::lowest(); }
2053 * @brief Returns the least upper bound value of the distribution.
2057 { return std::numeric_limits
<result_type
>::max(); }
2060 * @brief Generating functions.
2062 template<typename _UniformRandomNumberGenerator
>
2064 operator()(_UniformRandomNumberGenerator
& __urng
)
2065 { return this->operator()(__urng
, _M_param
); }
2067 template<typename _UniformRandomNumberGenerator
>
2069 operator()(_UniformRandomNumberGenerator
& __urng
,
2070 const param_type
& __p
);
2072 template<typename _ForwardIterator
,
2073 typename _UniformRandomNumberGenerator
>
2075 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2076 _UniformRandomNumberGenerator
& __urng
)
2077 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2079 template<typename _ForwardIterator
,
2080 typename _UniformRandomNumberGenerator
>
2082 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2083 _UniformRandomNumberGenerator
& __urng
,
2084 const param_type
& __p
)
2085 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2087 template<typename _UniformRandomNumberGenerator
>
2089 __generate(result_type
* __f
, result_type
* __t
,
2090 _UniformRandomNumberGenerator
& __urng
,
2091 const param_type
& __p
)
2092 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2095 * @brief Return true if two normal distributions have
2096 * the same parameters and the sequences that would
2097 * be generated are equal.
2099 template<typename _RealType1
>
2101 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2102 const std::normal_distribution
<_RealType1
>& __d2
);
2105 * @brief Inserts a %normal_distribution random number distribution
2106 * @p __x into the output stream @p __os.
2108 * @param __os An output stream.
2109 * @param __x A %normal_distribution random number distribution.
2111 * @returns The output stream with the state of @p __x inserted or in
2114 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2115 friend std::basic_ostream
<_CharT
, _Traits
>&
2116 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2117 const std::normal_distribution
<_RealType1
>& __x
);
2120 * @brief Extracts a %normal_distribution random number distribution
2121 * @p __x from the input stream @p __is.
2123 * @param __is An input stream.
2124 * @param __x A %normal_distribution random number generator engine.
2126 * @returns The input stream with @p __x extracted or in an error
2129 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2130 friend std::basic_istream
<_CharT
, _Traits
>&
2131 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2132 std::normal_distribution
<_RealType1
>& __x
);
2135 template<typename _ForwardIterator
,
2136 typename _UniformRandomNumberGenerator
>
2138 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2139 _UniformRandomNumberGenerator
& __urng
,
2140 const param_type
& __p
);
2142 param_type _M_param
;
2143 result_type _M_saved
;
2144 bool _M_saved_available
;
2148 * @brief Return true if two normal distributions are different.
2150 template<typename _RealType
>
2152 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2153 const std::normal_distribution
<_RealType
>& __d2
)
2154 { return !(__d1
== __d2
); }
2158 * @brief A lognormal_distribution random number distribution.
2160 * The formula for the normal probability mass function is
2162 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2163 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2166 template<typename _RealType
= double>
2167 class lognormal_distribution
2169 static_assert(std::is_floating_point
<_RealType
>::value
,
2170 "result_type must be a floating point type");
2173 /** The type of the range of the distribution. */
2174 typedef _RealType result_type
;
2176 /** Parameter type. */
2179 typedef lognormal_distribution
<_RealType
> distribution_type
;
2182 param_type(_RealType __m
= _RealType(0),
2183 _RealType __s
= _RealType(1))
2184 : _M_m(__m
), _M_s(__s
)
2196 operator==(const param_type
& __p1
, const param_type
& __p2
)
2197 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2200 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2201 { return !(__p1
== __p2
); }
2209 lognormal_distribution(_RealType __m
= _RealType(0),
2210 _RealType __s
= _RealType(1))
2211 : _M_param(__m
, __s
), _M_nd()
2215 lognormal_distribution(const param_type
& __p
)
2216 : _M_param(__p
), _M_nd()
2220 * Resets the distribution state.
2231 { return _M_param
.m(); }
2235 { return _M_param
.s(); }
2238 * @brief Returns the parameter set of the distribution.
2242 { return _M_param
; }
2245 * @brief Sets the parameter set of the distribution.
2246 * @param __param The new parameter set of the distribution.
2249 param(const param_type
& __param
)
2250 { _M_param
= __param
; }
2253 * @brief Returns the greatest lower bound value of the distribution.
2257 { return result_type(0); }
2260 * @brief Returns the least upper bound value of the distribution.
2264 { return std::numeric_limits
<result_type
>::max(); }
2267 * @brief Generating functions.
2269 template<typename _UniformRandomNumberGenerator
>
2271 operator()(_UniformRandomNumberGenerator
& __urng
)
2272 { return this->operator()(__urng
, _M_param
); }
2274 template<typename _UniformRandomNumberGenerator
>
2276 operator()(_UniformRandomNumberGenerator
& __urng
,
2277 const param_type
& __p
)
2278 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2280 template<typename _ForwardIterator
,
2281 typename _UniformRandomNumberGenerator
>
2283 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2284 _UniformRandomNumberGenerator
& __urng
)
2285 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2287 template<typename _ForwardIterator
,
2288 typename _UniformRandomNumberGenerator
>
2290 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2291 _UniformRandomNumberGenerator
& __urng
,
2292 const param_type
& __p
)
2293 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2295 template<typename _UniformRandomNumberGenerator
>
2297 __generate(result_type
* __f
, result_type
* __t
,
2298 _UniformRandomNumberGenerator
& __urng
,
2299 const param_type
& __p
)
2300 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2303 * @brief Return true if two lognormal distributions have
2304 * the same parameters and the sequences that would
2305 * be generated are equal.
2308 operator==(const lognormal_distribution
& __d1
,
2309 const lognormal_distribution
& __d2
)
2310 { return (__d1
._M_param
== __d2
._M_param
2311 && __d1
._M_nd
== __d2
._M_nd
); }
2314 * @brief Inserts a %lognormal_distribution random number distribution
2315 * @p __x into the output stream @p __os.
2317 * @param __os An output stream.
2318 * @param __x A %lognormal_distribution random number distribution.
2320 * @returns The output stream with the state of @p __x inserted or in
2323 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2324 friend std::basic_ostream
<_CharT
, _Traits
>&
2325 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2326 const std::lognormal_distribution
<_RealType1
>& __x
);
2329 * @brief Extracts a %lognormal_distribution random number distribution
2330 * @p __x from the input stream @p __is.
2332 * @param __is An input stream.
2333 * @param __x A %lognormal_distribution random number
2336 * @returns The input stream with @p __x extracted or in an error state.
2338 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2339 friend std::basic_istream
<_CharT
, _Traits
>&
2340 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2341 std::lognormal_distribution
<_RealType1
>& __x
);
2344 template<typename _ForwardIterator
,
2345 typename _UniformRandomNumberGenerator
>
2347 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2348 _UniformRandomNumberGenerator
& __urng
,
2349 const param_type
& __p
);
2351 param_type _M_param
;
2353 std::normal_distribution
<result_type
> _M_nd
;
2357 * @brief Return true if two lognormal distributions are different.
2359 template<typename _RealType
>
2361 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2362 const std::lognormal_distribution
<_RealType
>& __d2
)
2363 { return !(__d1
== __d2
); }
2367 * @brief A gamma continuous distribution for random numbers.
2369 * The formula for the gamma probability density function is:
2371 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2372 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2375 template<typename _RealType
= double>
2376 class gamma_distribution
2378 static_assert(std::is_floating_point
<_RealType
>::value
,
2379 "result_type must be a floating point type");
2382 /** The type of the range of the distribution. */
2383 typedef _RealType result_type
;
2385 /** Parameter type. */
2388 typedef gamma_distribution
<_RealType
> distribution_type
;
2389 friend class gamma_distribution
<_RealType
>;
2392 param_type(_RealType __alpha_val
= _RealType(1),
2393 _RealType __beta_val
= _RealType(1))
2394 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2396 __glibcxx_assert(_M_alpha
> _RealType(0));
2402 { return _M_alpha
; }
2409 operator==(const param_type
& __p1
, const param_type
& __p2
)
2410 { return (__p1
._M_alpha
== __p2
._M_alpha
2411 && __p1
._M_beta
== __p2
._M_beta
); }
2414 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2415 { return !(__p1
== __p2
); }
2424 _RealType _M_malpha
, _M_a2
;
2429 * @brief Constructs a gamma distribution with parameters
2430 * @f$\alpha@f$ and @f$\beta@f$.
2433 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2434 _RealType __beta_val
= _RealType(1))
2435 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2439 gamma_distribution(const param_type
& __p
)
2440 : _M_param(__p
), _M_nd()
2444 * @brief Resets the distribution state.
2451 * @brief Returns the @f$\alpha@f$ of the distribution.
2455 { return _M_param
.alpha(); }
2458 * @brief Returns the @f$\beta@f$ of the distribution.
2462 { return _M_param
.beta(); }
2465 * @brief Returns the parameter set of the distribution.
2469 { return _M_param
; }
2472 * @brief Sets the parameter set of the distribution.
2473 * @param __param The new parameter set of the distribution.
2476 param(const param_type
& __param
)
2477 { _M_param
= __param
; }
2480 * @brief Returns the greatest lower bound value of the distribution.
2484 { return result_type(0); }
2487 * @brief Returns the least upper bound value of the distribution.
2491 { return std::numeric_limits
<result_type
>::max(); }
2494 * @brief Generating functions.
2496 template<typename _UniformRandomNumberGenerator
>
2498 operator()(_UniformRandomNumberGenerator
& __urng
)
2499 { return this->operator()(__urng
, _M_param
); }
2501 template<typename _UniformRandomNumberGenerator
>
2503 operator()(_UniformRandomNumberGenerator
& __urng
,
2504 const param_type
& __p
);
2506 template<typename _ForwardIterator
,
2507 typename _UniformRandomNumberGenerator
>
2509 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2510 _UniformRandomNumberGenerator
& __urng
)
2511 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2513 template<typename _ForwardIterator
,
2514 typename _UniformRandomNumberGenerator
>
2516 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2517 _UniformRandomNumberGenerator
& __urng
,
2518 const param_type
& __p
)
2519 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2521 template<typename _UniformRandomNumberGenerator
>
2523 __generate(result_type
* __f
, result_type
* __t
,
2524 _UniformRandomNumberGenerator
& __urng
,
2525 const param_type
& __p
)
2526 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2529 * @brief Return true if two gamma distributions have the same
2530 * parameters and the sequences that would be generated
2534 operator==(const gamma_distribution
& __d1
,
2535 const gamma_distribution
& __d2
)
2536 { return (__d1
._M_param
== __d2
._M_param
2537 && __d1
._M_nd
== __d2
._M_nd
); }
2540 * @brief Inserts a %gamma_distribution random number distribution
2541 * @p __x into the output stream @p __os.
2543 * @param __os An output stream.
2544 * @param __x A %gamma_distribution random number distribution.
2546 * @returns The output stream with the state of @p __x inserted or in
2549 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2550 friend std::basic_ostream
<_CharT
, _Traits
>&
2551 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2552 const std::gamma_distribution
<_RealType1
>& __x
);
2555 * @brief Extracts a %gamma_distribution random number distribution
2556 * @p __x from the input stream @p __is.
2558 * @param __is An input stream.
2559 * @param __x A %gamma_distribution random number generator engine.
2561 * @returns The input stream with @p __x extracted or in an error state.
2563 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2564 friend std::basic_istream
<_CharT
, _Traits
>&
2565 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2566 std::gamma_distribution
<_RealType1
>& __x
);
2569 template<typename _ForwardIterator
,
2570 typename _UniformRandomNumberGenerator
>
2572 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2573 _UniformRandomNumberGenerator
& __urng
,
2574 const param_type
& __p
);
2576 param_type _M_param
;
2578 std::normal_distribution
<result_type
> _M_nd
;
2582 * @brief Return true if two gamma distributions are different.
2584 template<typename _RealType
>
2586 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2587 const std::gamma_distribution
<_RealType
>& __d2
)
2588 { return !(__d1
== __d2
); }
2592 * @brief A chi_squared_distribution random number distribution.
2594 * The formula for the normal probability mass function is
2595 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2597 template<typename _RealType
= double>
2598 class chi_squared_distribution
2600 static_assert(std::is_floating_point
<_RealType
>::value
,
2601 "result_type must be a floating point type");
2604 /** The type of the range of the distribution. */
2605 typedef _RealType result_type
;
2607 /** Parameter type. */
2610 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2613 param_type(_RealType __n
= _RealType(1))
2622 operator==(const param_type
& __p1
, const param_type
& __p2
)
2623 { return __p1
._M_n
== __p2
._M_n
; }
2626 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2627 { return !(__p1
== __p2
); }
2634 chi_squared_distribution(_RealType __n
= _RealType(1))
2635 : _M_param(__n
), _M_gd(__n
/ 2)
2639 chi_squared_distribution(const param_type
& __p
)
2640 : _M_param(__p
), _M_gd(__p
.n() / 2)
2644 * @brief Resets the distribution state.
2655 { return _M_param
.n(); }
2658 * @brief Returns the parameter set of the distribution.
2662 { return _M_param
; }
2665 * @brief Sets the parameter set of the distribution.
2666 * @param __param The new parameter set of the distribution.
2669 param(const param_type
& __param
)
2672 typedef typename
std::gamma_distribution
<result_type
>::param_type
2674 _M_gd
.param(param_type
{__param
.n() / 2});
2678 * @brief Returns the greatest lower bound value of the distribution.
2682 { return result_type(0); }
2685 * @brief Returns the least upper bound value of the distribution.
2689 { return std::numeric_limits
<result_type
>::max(); }
2692 * @brief Generating functions.
2694 template<typename _UniformRandomNumberGenerator
>
2696 operator()(_UniformRandomNumberGenerator
& __urng
)
2697 { return 2 * _M_gd(__urng
); }
2699 template<typename _UniformRandomNumberGenerator
>
2701 operator()(_UniformRandomNumberGenerator
& __urng
,
2702 const param_type
& __p
)
2704 typedef typename
std::gamma_distribution
<result_type
>::param_type
2706 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2709 template<typename _ForwardIterator
,
2710 typename _UniformRandomNumberGenerator
>
2712 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2713 _UniformRandomNumberGenerator
& __urng
)
2714 { this->__generate_impl(__f
, __t
, __urng
); }
2716 template<typename _ForwardIterator
,
2717 typename _UniformRandomNumberGenerator
>
2719 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2720 _UniformRandomNumberGenerator
& __urng
,
2721 const param_type
& __p
)
2722 { typename
std::gamma_distribution
<result_type
>::param_type
2724 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2726 template<typename _UniformRandomNumberGenerator
>
2728 __generate(result_type
* __f
, result_type
* __t
,
2729 _UniformRandomNumberGenerator
& __urng
)
2730 { this->__generate_impl(__f
, __t
, __urng
); }
2732 template<typename _UniformRandomNumberGenerator
>
2734 __generate(result_type
* __f
, result_type
* __t
,
2735 _UniformRandomNumberGenerator
& __urng
,
2736 const param_type
& __p
)
2737 { typename
std::gamma_distribution
<result_type
>::param_type
2739 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2742 * @brief Return true if two Chi-squared distributions have
2743 * the same parameters and the sequences that would be
2744 * generated are equal.
2747 operator==(const chi_squared_distribution
& __d1
,
2748 const chi_squared_distribution
& __d2
)
2749 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
2752 * @brief Inserts a %chi_squared_distribution random number distribution
2753 * @p __x into the output stream @p __os.
2755 * @param __os An output stream.
2756 * @param __x A %chi_squared_distribution random number distribution.
2758 * @returns The output stream with the state of @p __x inserted or in
2761 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2762 friend std::basic_ostream
<_CharT
, _Traits
>&
2763 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2764 const std::chi_squared_distribution
<_RealType1
>& __x
);
2767 * @brief Extracts a %chi_squared_distribution random number distribution
2768 * @p __x from the input stream @p __is.
2770 * @param __is An input stream.
2771 * @param __x A %chi_squared_distribution random number
2774 * @returns The input stream with @p __x extracted or in an error state.
2776 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2777 friend std::basic_istream
<_CharT
, _Traits
>&
2778 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2779 std::chi_squared_distribution
<_RealType1
>& __x
);
2782 template<typename _ForwardIterator
,
2783 typename _UniformRandomNumberGenerator
>
2785 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2786 _UniformRandomNumberGenerator
& __urng
);
2788 template<typename _ForwardIterator
,
2789 typename _UniformRandomNumberGenerator
>
2791 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2792 _UniformRandomNumberGenerator
& __urng
,
2794 std::gamma_distribution
<result_type
>::param_type
& __p
);
2796 param_type _M_param
;
2798 std::gamma_distribution
<result_type
> _M_gd
;
2802 * @brief Return true if two Chi-squared distributions are different.
2804 template<typename _RealType
>
2806 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2807 const std::chi_squared_distribution
<_RealType
>& __d2
)
2808 { return !(__d1
== __d2
); }
2812 * @brief A cauchy_distribution random number distribution.
2814 * The formula for the normal probability mass function is
2815 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2817 template<typename _RealType
= double>
2818 class cauchy_distribution
2820 static_assert(std::is_floating_point
<_RealType
>::value
,
2821 "result_type must be a floating point type");
2824 /** The type of the range of the distribution. */
2825 typedef _RealType result_type
;
2827 /** Parameter type. */
2830 typedef cauchy_distribution
<_RealType
> distribution_type
;
2833 param_type(_RealType __a
= _RealType(0),
2834 _RealType __b
= _RealType(1))
2835 : _M_a(__a
), _M_b(__b
)
2847 operator==(const param_type
& __p1
, const param_type
& __p2
)
2848 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2851 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2852 { return !(__p1
== __p2
); }
2860 cauchy_distribution(_RealType __a
= _RealType(0),
2861 _RealType __b
= _RealType(1))
2862 : _M_param(__a
, __b
)
2866 cauchy_distribution(const param_type
& __p
)
2871 * @brief Resets the distribution state.
2882 { return _M_param
.a(); }
2886 { return _M_param
.b(); }
2889 * @brief Returns the parameter set of the distribution.
2893 { return _M_param
; }
2896 * @brief Sets the parameter set of the distribution.
2897 * @param __param The new parameter set of the distribution.
2900 param(const param_type
& __param
)
2901 { _M_param
= __param
; }
2904 * @brief Returns the greatest lower bound value of the distribution.
2908 { return std::numeric_limits
<result_type
>::lowest(); }
2911 * @brief Returns the least upper bound value of the distribution.
2915 { return std::numeric_limits
<result_type
>::max(); }
2918 * @brief Generating functions.
2920 template<typename _UniformRandomNumberGenerator
>
2922 operator()(_UniformRandomNumberGenerator
& __urng
)
2923 { return this->operator()(__urng
, _M_param
); }
2925 template<typename _UniformRandomNumberGenerator
>
2927 operator()(_UniformRandomNumberGenerator
& __urng
,
2928 const param_type
& __p
);
2930 template<typename _ForwardIterator
,
2931 typename _UniformRandomNumberGenerator
>
2933 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2934 _UniformRandomNumberGenerator
& __urng
)
2935 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2937 template<typename _ForwardIterator
,
2938 typename _UniformRandomNumberGenerator
>
2940 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2941 _UniformRandomNumberGenerator
& __urng
,
2942 const param_type
& __p
)
2943 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2945 template<typename _UniformRandomNumberGenerator
>
2947 __generate(result_type
* __f
, result_type
* __t
,
2948 _UniformRandomNumberGenerator
& __urng
,
2949 const param_type
& __p
)
2950 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2953 * @brief Return true if two Cauchy distributions have
2954 * the same parameters.
2957 operator==(const cauchy_distribution
& __d1
,
2958 const cauchy_distribution
& __d2
)
2959 { return __d1
._M_param
== __d2
._M_param
; }
2962 template<typename _ForwardIterator
,
2963 typename _UniformRandomNumberGenerator
>
2965 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2966 _UniformRandomNumberGenerator
& __urng
,
2967 const param_type
& __p
);
2969 param_type _M_param
;
2973 * @brief Return true if two Cauchy distributions have
2974 * different parameters.
2976 template<typename _RealType
>
2978 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
2979 const std::cauchy_distribution
<_RealType
>& __d2
)
2980 { return !(__d1
== __d2
); }
2983 * @brief Inserts a %cauchy_distribution random number distribution
2984 * @p __x into the output stream @p __os.
2986 * @param __os An output stream.
2987 * @param __x A %cauchy_distribution random number distribution.
2989 * @returns The output stream with the state of @p __x inserted or in
2992 template<typename _RealType
, typename _CharT
, typename _Traits
>
2993 std::basic_ostream
<_CharT
, _Traits
>&
2994 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2995 const std::cauchy_distribution
<_RealType
>& __x
);
2998 * @brief Extracts a %cauchy_distribution random number distribution
2999 * @p __x from the input stream @p __is.
3001 * @param __is An input stream.
3002 * @param __x A %cauchy_distribution random number
3005 * @returns The input stream with @p __x extracted or in an error state.
3007 template<typename _RealType
, typename _CharT
, typename _Traits
>
3008 std::basic_istream
<_CharT
, _Traits
>&
3009 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3010 std::cauchy_distribution
<_RealType
>& __x
);
3014 * @brief A fisher_f_distribution random number distribution.
3016 * The formula for the normal probability mass function is
3018 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
3019 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
3020 * (1 + \frac{mx}{n})^{-(m+n)/2}
3023 template<typename _RealType
= double>
3024 class fisher_f_distribution
3026 static_assert(std::is_floating_point
<_RealType
>::value
,
3027 "result_type must be a floating point type");
3030 /** The type of the range of the distribution. */
3031 typedef _RealType result_type
;
3033 /** Parameter type. */
3036 typedef fisher_f_distribution
<_RealType
> distribution_type
;
3039 param_type(_RealType __m
= _RealType(1),
3040 _RealType __n
= _RealType(1))
3041 : _M_m(__m
), _M_n(__n
)
3053 operator==(const param_type
& __p1
, const param_type
& __p2
)
3054 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
3057 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3058 { return !(__p1
== __p2
); }
3066 fisher_f_distribution(_RealType __m
= _RealType(1),
3067 _RealType __n
= _RealType(1))
3068 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
3072 fisher_f_distribution(const param_type
& __p
)
3073 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
3077 * @brief Resets the distribution state.
3091 { return _M_param
.m(); }
3095 { return _M_param
.n(); }
3098 * @brief Returns the parameter set of the distribution.
3102 { return _M_param
; }
3105 * @brief Sets the parameter set of the distribution.
3106 * @param __param The new parameter set of the distribution.
3109 param(const param_type
& __param
)
3110 { _M_param
= __param
; }
3113 * @brief Returns the greatest lower bound value of the distribution.
3117 { return result_type(0); }
3120 * @brief Returns the least upper bound value of the distribution.
3124 { return std::numeric_limits
<result_type
>::max(); }
3127 * @brief Generating functions.
3129 template<typename _UniformRandomNumberGenerator
>
3131 operator()(_UniformRandomNumberGenerator
& __urng
)
3132 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
3134 template<typename _UniformRandomNumberGenerator
>
3136 operator()(_UniformRandomNumberGenerator
& __urng
,
3137 const param_type
& __p
)
3139 typedef typename
std::gamma_distribution
<result_type
>::param_type
3141 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
3142 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
3145 template<typename _ForwardIterator
,
3146 typename _UniformRandomNumberGenerator
>
3148 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3149 _UniformRandomNumberGenerator
& __urng
)
3150 { this->__generate_impl(__f
, __t
, __urng
); }
3152 template<typename _ForwardIterator
,
3153 typename _UniformRandomNumberGenerator
>
3155 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3156 _UniformRandomNumberGenerator
& __urng
,
3157 const param_type
& __p
)
3158 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3160 template<typename _UniformRandomNumberGenerator
>
3162 __generate(result_type
* __f
, result_type
* __t
,
3163 _UniformRandomNumberGenerator
& __urng
)
3164 { this->__generate_impl(__f
, __t
, __urng
); }
3166 template<typename _UniformRandomNumberGenerator
>
3168 __generate(result_type
* __f
, result_type
* __t
,
3169 _UniformRandomNumberGenerator
& __urng
,
3170 const param_type
& __p
)
3171 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3174 * @brief Return true if two Fisher f distributions have
3175 * the same parameters and the sequences that would
3176 * be generated are equal.
3179 operator==(const fisher_f_distribution
& __d1
,
3180 const fisher_f_distribution
& __d2
)
3181 { return (__d1
._M_param
== __d2
._M_param
3182 && __d1
._M_gd_x
== __d2
._M_gd_x
3183 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
3186 * @brief Inserts a %fisher_f_distribution random number distribution
3187 * @p __x into the output stream @p __os.
3189 * @param __os An output stream.
3190 * @param __x A %fisher_f_distribution random number distribution.
3192 * @returns The output stream with the state of @p __x inserted or in
3195 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3196 friend std::basic_ostream
<_CharT
, _Traits
>&
3197 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3198 const std::fisher_f_distribution
<_RealType1
>& __x
);
3201 * @brief Extracts a %fisher_f_distribution random number distribution
3202 * @p __x from the input stream @p __is.
3204 * @param __is An input stream.
3205 * @param __x A %fisher_f_distribution random number
3208 * @returns The input stream with @p __x extracted or in an error state.
3210 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3211 friend std::basic_istream
<_CharT
, _Traits
>&
3212 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3213 std::fisher_f_distribution
<_RealType1
>& __x
);
3216 template<typename _ForwardIterator
,
3217 typename _UniformRandomNumberGenerator
>
3219 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3220 _UniformRandomNumberGenerator
& __urng
);
3222 template<typename _ForwardIterator
,
3223 typename _UniformRandomNumberGenerator
>
3225 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3226 _UniformRandomNumberGenerator
& __urng
,
3227 const param_type
& __p
);
3229 param_type _M_param
;
3231 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3235 * @brief Return true if two Fisher f distributions are different.
3237 template<typename _RealType
>
3239 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3240 const std::fisher_f_distribution
<_RealType
>& __d2
)
3241 { return !(__d1
== __d2
); }
3244 * @brief A student_t_distribution random number distribution.
3246 * The formula for the normal probability mass function is:
3248 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3249 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3252 template<typename _RealType
= double>
3253 class student_t_distribution
3255 static_assert(std::is_floating_point
<_RealType
>::value
,
3256 "result_type must be a floating point type");
3259 /** The type of the range of the distribution. */
3260 typedef _RealType result_type
;
3262 /** Parameter type. */
3265 typedef student_t_distribution
<_RealType
> distribution_type
;
3268 param_type(_RealType __n
= _RealType(1))
3277 operator==(const param_type
& __p1
, const param_type
& __p2
)
3278 { return __p1
._M_n
== __p2
._M_n
; }
3281 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3282 { return !(__p1
== __p2
); }
3289 student_t_distribution(_RealType __n
= _RealType(1))
3290 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3294 student_t_distribution(const param_type
& __p
)
3295 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3299 * @brief Resets the distribution state.
3313 { return _M_param
.n(); }
3316 * @brief Returns the parameter set of the distribution.
3320 { return _M_param
; }
3323 * @brief Sets the parameter set of the distribution.
3324 * @param __param The new parameter set of the distribution.
3327 param(const param_type
& __param
)
3328 { _M_param
= __param
; }
3331 * @brief Returns the greatest lower bound value of the distribution.
3335 { return std::numeric_limits
<result_type
>::lowest(); }
3338 * @brief Returns the least upper bound value of the distribution.
3342 { return std::numeric_limits
<result_type
>::max(); }
3345 * @brief Generating functions.
3347 template<typename _UniformRandomNumberGenerator
>
3349 operator()(_UniformRandomNumberGenerator
& __urng
)
3350 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3352 template<typename _UniformRandomNumberGenerator
>
3354 operator()(_UniformRandomNumberGenerator
& __urng
,
3355 const param_type
& __p
)
3357 typedef typename
std::gamma_distribution
<result_type
>::param_type
3360 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3361 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3364 template<typename _ForwardIterator
,
3365 typename _UniformRandomNumberGenerator
>
3367 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3368 _UniformRandomNumberGenerator
& __urng
)
3369 { this->__generate_impl(__f
, __t
, __urng
); }
3371 template<typename _ForwardIterator
,
3372 typename _UniformRandomNumberGenerator
>
3374 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3375 _UniformRandomNumberGenerator
& __urng
,
3376 const param_type
& __p
)
3377 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3379 template<typename _UniformRandomNumberGenerator
>
3381 __generate(result_type
* __f
, result_type
* __t
,
3382 _UniformRandomNumberGenerator
& __urng
)
3383 { this->__generate_impl(__f
, __t
, __urng
); }
3385 template<typename _UniformRandomNumberGenerator
>
3387 __generate(result_type
* __f
, result_type
* __t
,
3388 _UniformRandomNumberGenerator
& __urng
,
3389 const param_type
& __p
)
3390 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3393 * @brief Return true if two Student t distributions have
3394 * the same parameters and the sequences that would
3395 * be generated are equal.
3398 operator==(const student_t_distribution
& __d1
,
3399 const student_t_distribution
& __d2
)
3400 { return (__d1
._M_param
== __d2
._M_param
3401 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3404 * @brief Inserts a %student_t_distribution random number distribution
3405 * @p __x into the output stream @p __os.
3407 * @param __os An output stream.
3408 * @param __x A %student_t_distribution random number distribution.
3410 * @returns The output stream with the state of @p __x inserted or in
3413 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3414 friend std::basic_ostream
<_CharT
, _Traits
>&
3415 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3416 const std::student_t_distribution
<_RealType1
>& __x
);
3419 * @brief Extracts a %student_t_distribution random number distribution
3420 * @p __x from the input stream @p __is.
3422 * @param __is An input stream.
3423 * @param __x A %student_t_distribution random number
3426 * @returns The input stream with @p __x extracted or in an error state.
3428 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3429 friend std::basic_istream
<_CharT
, _Traits
>&
3430 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3431 std::student_t_distribution
<_RealType1
>& __x
);
3434 template<typename _ForwardIterator
,
3435 typename _UniformRandomNumberGenerator
>
3437 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3438 _UniformRandomNumberGenerator
& __urng
);
3439 template<typename _ForwardIterator
,
3440 typename _UniformRandomNumberGenerator
>
3442 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3443 _UniformRandomNumberGenerator
& __urng
,
3444 const param_type
& __p
);
3446 param_type _M_param
;
3448 std::normal_distribution
<result_type
> _M_nd
;
3449 std::gamma_distribution
<result_type
> _M_gd
;
3453 * @brief Return true if two Student t distributions are different.
3455 template<typename _RealType
>
3457 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3458 const std::student_t_distribution
<_RealType
>& __d2
)
3459 { return !(__d1
== __d2
); }
3462 /* @} */ // group random_distributions_normal
3465 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3466 * @ingroup random_distributions
3471 * @brief A Bernoulli random number distribution.
3473 * Generates a sequence of true and false values with likelihood @f$p@f$
3474 * that true will come up and @f$(1 - p)@f$ that false will appear.
3476 class bernoulli_distribution
3479 /** The type of the range of the distribution. */
3480 typedef bool result_type
;
3482 /** Parameter type. */
3485 typedef bernoulli_distribution distribution_type
;
3488 param_type(double __p
= 0.5)
3491 __glibcxx_assert((_M_p
>= 0.0) && (_M_p
<= 1.0));
3499 operator==(const param_type
& __p1
, const param_type
& __p2
)
3500 { return __p1
._M_p
== __p2
._M_p
; }
3503 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3504 { return !(__p1
== __p2
); }
3512 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3514 * @param __p [IN] The likelihood of a true result being returned.
3515 * Must be in the interval @f$[0, 1]@f$.
3518 bernoulli_distribution(double __p
= 0.5)
3523 bernoulli_distribution(const param_type
& __p
)
3528 * @brief Resets the distribution state.
3530 * Does nothing for a Bernoulli distribution.
3536 * @brief Returns the @p p parameter of the distribution.
3540 { return _M_param
.p(); }
3543 * @brief Returns the parameter set of the distribution.
3547 { return _M_param
; }
3550 * @brief Sets the parameter set of the distribution.
3551 * @param __param The new parameter set of the distribution.
3554 param(const param_type
& __param
)
3555 { _M_param
= __param
; }
3558 * @brief Returns the greatest lower bound value of the distribution.
3562 { return std::numeric_limits
<result_type
>::min(); }
3565 * @brief Returns the least upper bound value of the distribution.
3569 { return std::numeric_limits
<result_type
>::max(); }
3572 * @brief Generating functions.
3574 template<typename _UniformRandomNumberGenerator
>
3576 operator()(_UniformRandomNumberGenerator
& __urng
)
3577 { return this->operator()(__urng
, _M_param
); }
3579 template<typename _UniformRandomNumberGenerator
>
3581 operator()(_UniformRandomNumberGenerator
& __urng
,
3582 const param_type
& __p
)
3584 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3586 if ((__aurng() - __aurng
.min())
3587 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3592 template<typename _ForwardIterator
,
3593 typename _UniformRandomNumberGenerator
>
3595 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3596 _UniformRandomNumberGenerator
& __urng
)
3597 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3599 template<typename _ForwardIterator
,
3600 typename _UniformRandomNumberGenerator
>
3602 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3603 _UniformRandomNumberGenerator
& __urng
, const param_type
& __p
)
3604 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3606 template<typename _UniformRandomNumberGenerator
>
3608 __generate(result_type
* __f
, result_type
* __t
,
3609 _UniformRandomNumberGenerator
& __urng
,
3610 const param_type
& __p
)
3611 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3614 * @brief Return true if two Bernoulli distributions have
3615 * the same parameters.
3618 operator==(const bernoulli_distribution
& __d1
,
3619 const bernoulli_distribution
& __d2
)
3620 { return __d1
._M_param
== __d2
._M_param
; }
3623 template<typename _ForwardIterator
,
3624 typename _UniformRandomNumberGenerator
>
3626 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3627 _UniformRandomNumberGenerator
& __urng
,
3628 const param_type
& __p
);
3630 param_type _M_param
;
3634 * @brief Return true if two Bernoulli distributions have
3635 * different parameters.
3638 operator!=(const std::bernoulli_distribution
& __d1
,
3639 const std::bernoulli_distribution
& __d2
)
3640 { return !(__d1
== __d2
); }
3643 * @brief Inserts a %bernoulli_distribution random number distribution
3644 * @p __x into the output stream @p __os.
3646 * @param __os An output stream.
3647 * @param __x A %bernoulli_distribution random number distribution.
3649 * @returns The output stream with the state of @p __x inserted or in
3652 template<typename _CharT
, typename _Traits
>
3653 std::basic_ostream
<_CharT
, _Traits
>&
3654 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3655 const std::bernoulli_distribution
& __x
);
3658 * @brief Extracts a %bernoulli_distribution random number distribution
3659 * @p __x from the input stream @p __is.
3661 * @param __is An input stream.
3662 * @param __x A %bernoulli_distribution random number generator engine.
3664 * @returns The input stream with @p __x extracted or in an error state.
3666 template<typename _CharT
, typename _Traits
>
3667 std::basic_istream
<_CharT
, _Traits
>&
3668 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3669 std::bernoulli_distribution
& __x
)
3673 __x
.param(bernoulli_distribution::param_type(__p
));
3679 * @brief A discrete binomial random number distribution.
3681 * The formula for the binomial probability density function is
3682 * @f$p(i|t,p) = \binom{t}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3683 * and @f$p@f$ are the parameters of the distribution.
3685 template<typename _IntType
= int>
3686 class binomial_distribution
3688 static_assert(std::is_integral
<_IntType
>::value
,
3689 "result_type must be an integral type");
3692 /** The type of the range of the distribution. */
3693 typedef _IntType result_type
;
3695 /** Parameter type. */
3698 typedef binomial_distribution
<_IntType
> distribution_type
;
3699 friend class binomial_distribution
<_IntType
>;
3702 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3703 : _M_t(__t
), _M_p(__p
)
3705 __glibcxx_assert((_M_t
>= _IntType(0))
3720 operator==(const param_type
& __p1
, const param_type
& __p2
)
3721 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3724 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3725 { return !(__p1
== __p2
); }
3735 #if _GLIBCXX_USE_C99_MATH_TR1
3736 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3737 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3742 // constructors and member function
3744 binomial_distribution(_IntType __t
= _IntType(1),
3746 : _M_param(__t
, __p
), _M_nd()
3750 binomial_distribution(const param_type
& __p
)
3751 : _M_param(__p
), _M_nd()
3755 * @brief Resets the distribution state.
3762 * @brief Returns the distribution @p t parameter.
3766 { return _M_param
.t(); }
3769 * @brief Returns the distribution @p p parameter.
3773 { return _M_param
.p(); }
3776 * @brief Returns the parameter set of the distribution.
3780 { return _M_param
; }
3783 * @brief Sets the parameter set of the distribution.
3784 * @param __param The new parameter set of the distribution.
3787 param(const param_type
& __param
)
3788 { _M_param
= __param
; }
3791 * @brief Returns the greatest lower bound value of the distribution.
3798 * @brief Returns the least upper bound value of the distribution.
3802 { return _M_param
.t(); }
3805 * @brief Generating functions.
3807 template<typename _UniformRandomNumberGenerator
>
3809 operator()(_UniformRandomNumberGenerator
& __urng
)
3810 { return this->operator()(__urng
, _M_param
); }
3812 template<typename _UniformRandomNumberGenerator
>
3814 operator()(_UniformRandomNumberGenerator
& __urng
,
3815 const param_type
& __p
);
3817 template<typename _ForwardIterator
,
3818 typename _UniformRandomNumberGenerator
>
3820 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3821 _UniformRandomNumberGenerator
& __urng
)
3822 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3824 template<typename _ForwardIterator
,
3825 typename _UniformRandomNumberGenerator
>
3827 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3828 _UniformRandomNumberGenerator
& __urng
,
3829 const param_type
& __p
)
3830 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3832 template<typename _UniformRandomNumberGenerator
>
3834 __generate(result_type
* __f
, result_type
* __t
,
3835 _UniformRandomNumberGenerator
& __urng
,
3836 const param_type
& __p
)
3837 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3840 * @brief Return true if two binomial distributions have
3841 * the same parameters and the sequences that would
3842 * be generated are equal.
3845 operator==(const binomial_distribution
& __d1
,
3846 const binomial_distribution
& __d2
)
3847 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3848 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
3850 { return __d1
._M_param
== __d2
._M_param
; }
3854 * @brief Inserts a %binomial_distribution random number distribution
3855 * @p __x into the output stream @p __os.
3857 * @param __os An output stream.
3858 * @param __x A %binomial_distribution random number distribution.
3860 * @returns The output stream with the state of @p __x inserted or in
3863 template<typename _IntType1
,
3864 typename _CharT
, typename _Traits
>
3865 friend std::basic_ostream
<_CharT
, _Traits
>&
3866 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3867 const std::binomial_distribution
<_IntType1
>& __x
);
3870 * @brief Extracts a %binomial_distribution random number distribution
3871 * @p __x from the input stream @p __is.
3873 * @param __is An input stream.
3874 * @param __x A %binomial_distribution random number generator engine.
3876 * @returns The input stream with @p __x extracted or in an error
3879 template<typename _IntType1
,
3880 typename _CharT
, typename _Traits
>
3881 friend std::basic_istream
<_CharT
, _Traits
>&
3882 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3883 std::binomial_distribution
<_IntType1
>& __x
);
3886 template<typename _ForwardIterator
,
3887 typename _UniformRandomNumberGenerator
>
3889 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3890 _UniformRandomNumberGenerator
& __urng
,
3891 const param_type
& __p
);
3893 template<typename _UniformRandomNumberGenerator
>
3895 _M_waiting(_UniformRandomNumberGenerator
& __urng
,
3896 _IntType __t
, double __q
);
3898 param_type _M_param
;
3900 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3901 std::normal_distribution
<double> _M_nd
;
3905 * @brief Return true if two binomial distributions are different.
3907 template<typename _IntType
>
3909 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
3910 const std::binomial_distribution
<_IntType
>& __d2
)
3911 { return !(__d1
== __d2
); }
3915 * @brief A discrete geometric random number distribution.
3917 * The formula for the geometric probability density function is
3918 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
3921 template<typename _IntType
= int>
3922 class geometric_distribution
3924 static_assert(std::is_integral
<_IntType
>::value
,
3925 "result_type must be an integral type");
3928 /** The type of the range of the distribution. */
3929 typedef _IntType result_type
;
3931 /** Parameter type. */
3934 typedef geometric_distribution
<_IntType
> distribution_type
;
3935 friend class geometric_distribution
<_IntType
>;
3938 param_type(double __p
= 0.5)
3941 __glibcxx_assert((_M_p
> 0.0) && (_M_p
< 1.0));
3950 operator==(const param_type
& __p1
, const param_type
& __p2
)
3951 { return __p1
._M_p
== __p2
._M_p
; }
3954 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3955 { return !(__p1
== __p2
); }
3960 { _M_log_1_p
= std::log(1.0 - _M_p
); }
3967 // constructors and member function
3969 geometric_distribution(double __p
= 0.5)
3974 geometric_distribution(const param_type
& __p
)
3979 * @brief Resets the distribution state.
3981 * Does nothing for the geometric distribution.
3987 * @brief Returns the distribution parameter @p p.
3991 { return _M_param
.p(); }
3994 * @brief Returns the parameter set of the distribution.
3998 { return _M_param
; }
4001 * @brief Sets the parameter set of the distribution.
4002 * @param __param The new parameter set of the distribution.
4005 param(const param_type
& __param
)
4006 { _M_param
= __param
; }
4009 * @brief Returns the greatest lower bound value of the distribution.
4016 * @brief Returns the least upper bound value of the distribution.
4020 { return std::numeric_limits
<result_type
>::max(); }
4023 * @brief Generating functions.
4025 template<typename _UniformRandomNumberGenerator
>
4027 operator()(_UniformRandomNumberGenerator
& __urng
)
4028 { return this->operator()(__urng
, _M_param
); }
4030 template<typename _UniformRandomNumberGenerator
>
4032 operator()(_UniformRandomNumberGenerator
& __urng
,
4033 const param_type
& __p
);
4035 template<typename _ForwardIterator
,
4036 typename _UniformRandomNumberGenerator
>
4038 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4039 _UniformRandomNumberGenerator
& __urng
)
4040 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4042 template<typename _ForwardIterator
,
4043 typename _UniformRandomNumberGenerator
>
4045 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4046 _UniformRandomNumberGenerator
& __urng
,
4047 const param_type
& __p
)
4048 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4050 template<typename _UniformRandomNumberGenerator
>
4052 __generate(result_type
* __f
, result_type
* __t
,
4053 _UniformRandomNumberGenerator
& __urng
,
4054 const param_type
& __p
)
4055 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4058 * @brief Return true if two geometric distributions have
4059 * the same parameters.
4062 operator==(const geometric_distribution
& __d1
,
4063 const geometric_distribution
& __d2
)
4064 { return __d1
._M_param
== __d2
._M_param
; }
4067 template<typename _ForwardIterator
,
4068 typename _UniformRandomNumberGenerator
>
4070 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4071 _UniformRandomNumberGenerator
& __urng
,
4072 const param_type
& __p
);
4074 param_type _M_param
;
4078 * @brief Return true if two geometric distributions have
4079 * different parameters.
4081 template<typename _IntType
>
4083 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
4084 const std::geometric_distribution
<_IntType
>& __d2
)
4085 { return !(__d1
== __d2
); }
4088 * @brief Inserts a %geometric_distribution random number distribution
4089 * @p __x into the output stream @p __os.
4091 * @param __os An output stream.
4092 * @param __x A %geometric_distribution random number distribution.
4094 * @returns The output stream with the state of @p __x inserted or in
4097 template<typename _IntType
,
4098 typename _CharT
, typename _Traits
>
4099 std::basic_ostream
<_CharT
, _Traits
>&
4100 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4101 const std::geometric_distribution
<_IntType
>& __x
);
4104 * @brief Extracts a %geometric_distribution random number distribution
4105 * @p __x from the input stream @p __is.
4107 * @param __is An input stream.
4108 * @param __x A %geometric_distribution random number generator engine.
4110 * @returns The input stream with @p __x extracted or in an error state.
4112 template<typename _IntType
,
4113 typename _CharT
, typename _Traits
>
4114 std::basic_istream
<_CharT
, _Traits
>&
4115 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4116 std::geometric_distribution
<_IntType
>& __x
);
4120 * @brief A negative_binomial_distribution random number distribution.
4122 * The formula for the negative binomial probability mass function is
4123 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
4124 * and @f$p@f$ are the parameters of the distribution.
4126 template<typename _IntType
= int>
4127 class negative_binomial_distribution
4129 static_assert(std::is_integral
<_IntType
>::value
,
4130 "result_type must be an integral type");
4133 /** The type of the range of the distribution. */
4134 typedef _IntType result_type
;
4136 /** Parameter type. */
4139 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
4142 param_type(_IntType __k
= 1, double __p
= 0.5)
4143 : _M_k(__k
), _M_p(__p
)
4145 __glibcxx_assert((_M_k
> 0) && (_M_p
> 0.0) && (_M_p
<= 1.0));
4157 operator==(const param_type
& __p1
, const param_type
& __p2
)
4158 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
4161 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4162 { return !(__p1
== __p2
); }
4170 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
4171 : _M_param(__k
, __p
), _M_gd(__k
, (1.0 - __p
) / __p
)
4175 negative_binomial_distribution(const param_type
& __p
)
4176 : _M_param(__p
), _M_gd(__p
.k(), (1.0 - __p
.p()) / __p
.p())
4180 * @brief Resets the distribution state.
4187 * @brief Return the @f$k@f$ parameter of the distribution.
4191 { return _M_param
.k(); }
4194 * @brief Return the @f$p@f$ parameter of the distribution.
4198 { return _M_param
.p(); }
4201 * @brief Returns the parameter set of the distribution.
4205 { return _M_param
; }
4208 * @brief Sets the parameter set of the distribution.
4209 * @param __param The new parameter set of the distribution.
4212 param(const param_type
& __param
)
4213 { _M_param
= __param
; }
4216 * @brief Returns the greatest lower bound value of the distribution.
4220 { return result_type(0); }
4223 * @brief Returns the least upper bound value of the distribution.
4227 { return std::numeric_limits
<result_type
>::max(); }
4230 * @brief Generating functions.
4232 template<typename _UniformRandomNumberGenerator
>
4234 operator()(_UniformRandomNumberGenerator
& __urng
);
4236 template<typename _UniformRandomNumberGenerator
>
4238 operator()(_UniformRandomNumberGenerator
& __urng
,
4239 const param_type
& __p
);
4241 template<typename _ForwardIterator
,
4242 typename _UniformRandomNumberGenerator
>
4244 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4245 _UniformRandomNumberGenerator
& __urng
)
4246 { this->__generate_impl(__f
, __t
, __urng
); }
4248 template<typename _ForwardIterator
,
4249 typename _UniformRandomNumberGenerator
>
4251 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4252 _UniformRandomNumberGenerator
& __urng
,
4253 const param_type
& __p
)
4254 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4256 template<typename _UniformRandomNumberGenerator
>
4258 __generate(result_type
* __f
, result_type
* __t
,
4259 _UniformRandomNumberGenerator
& __urng
)
4260 { this->__generate_impl(__f
, __t
, __urng
); }
4262 template<typename _UniformRandomNumberGenerator
>
4264 __generate(result_type
* __f
, result_type
* __t
,
4265 _UniformRandomNumberGenerator
& __urng
,
4266 const param_type
& __p
)
4267 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4270 * @brief Return true if two negative binomial distributions have
4271 * the same parameters and the sequences that would be
4272 * generated are equal.
4275 operator==(const negative_binomial_distribution
& __d1
,
4276 const negative_binomial_distribution
& __d2
)
4277 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
4280 * @brief Inserts a %negative_binomial_distribution random
4281 * number distribution @p __x into the output stream @p __os.
4283 * @param __os An output stream.
4284 * @param __x A %negative_binomial_distribution random number
4287 * @returns The output stream with the state of @p __x inserted or in
4290 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4291 friend std::basic_ostream
<_CharT
, _Traits
>&
4292 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4293 const std::negative_binomial_distribution
<_IntType1
>& __x
);
4296 * @brief Extracts a %negative_binomial_distribution random number
4297 * distribution @p __x from the input stream @p __is.
4299 * @param __is An input stream.
4300 * @param __x A %negative_binomial_distribution random number
4303 * @returns The input stream with @p __x extracted or in an error state.
4305 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4306 friend std::basic_istream
<_CharT
, _Traits
>&
4307 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4308 std::negative_binomial_distribution
<_IntType1
>& __x
);
4311 template<typename _ForwardIterator
,
4312 typename _UniformRandomNumberGenerator
>
4314 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4315 _UniformRandomNumberGenerator
& __urng
);
4316 template<typename _ForwardIterator
,
4317 typename _UniformRandomNumberGenerator
>
4319 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4320 _UniformRandomNumberGenerator
& __urng
,
4321 const param_type
& __p
);
4323 param_type _M_param
;
4325 std::gamma_distribution
<double> _M_gd
;
4329 * @brief Return true if two negative binomial distributions are different.
4331 template<typename _IntType
>
4333 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
4334 const std::negative_binomial_distribution
<_IntType
>& __d2
)
4335 { return !(__d1
== __d2
); }
4338 /* @} */ // group random_distributions_bernoulli
4341 * @addtogroup random_distributions_poisson Poisson Distributions
4342 * @ingroup random_distributions
4347 * @brief A discrete Poisson random number distribution.
4349 * The formula for the Poisson probability density function is
4350 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
4351 * parameter of the distribution.
4353 template<typename _IntType
= int>
4354 class poisson_distribution
4356 static_assert(std::is_integral
<_IntType
>::value
,
4357 "result_type must be an integral type");
4360 /** The type of the range of the distribution. */
4361 typedef _IntType result_type
;
4363 /** Parameter type. */
4366 typedef poisson_distribution
<_IntType
> distribution_type
;
4367 friend class poisson_distribution
<_IntType
>;
4370 param_type(double __mean
= 1.0)
4373 __glibcxx_assert(_M_mean
> 0.0);
4382 operator==(const param_type
& __p1
, const param_type
& __p2
)
4383 { return __p1
._M_mean
== __p2
._M_mean
; }
4386 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4387 { return !(__p1
== __p2
); }
4390 // Hosts either log(mean) or the threshold of the simple method.
4397 #if _GLIBCXX_USE_C99_MATH_TR1
4398 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
4402 // constructors and member function
4404 poisson_distribution(double __mean
= 1.0)
4405 : _M_param(__mean
), _M_nd()
4409 poisson_distribution(const param_type
& __p
)
4410 : _M_param(__p
), _M_nd()
4414 * @brief Resets the distribution state.
4421 * @brief Returns the distribution parameter @p mean.
4425 { return _M_param
.mean(); }
4428 * @brief Returns the parameter set of the distribution.
4432 { return _M_param
; }
4435 * @brief Sets the parameter set of the distribution.
4436 * @param __param The new parameter set of the distribution.
4439 param(const param_type
& __param
)
4440 { _M_param
= __param
; }
4443 * @brief Returns the greatest lower bound value of the distribution.
4450 * @brief Returns the least upper bound value of the distribution.
4454 { return std::numeric_limits
<result_type
>::max(); }
4457 * @brief Generating functions.
4459 template<typename _UniformRandomNumberGenerator
>
4461 operator()(_UniformRandomNumberGenerator
& __urng
)
4462 { return this->operator()(__urng
, _M_param
); }
4464 template<typename _UniformRandomNumberGenerator
>
4466 operator()(_UniformRandomNumberGenerator
& __urng
,
4467 const param_type
& __p
);
4469 template<typename _ForwardIterator
,
4470 typename _UniformRandomNumberGenerator
>
4472 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4473 _UniformRandomNumberGenerator
& __urng
)
4474 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4476 template<typename _ForwardIterator
,
4477 typename _UniformRandomNumberGenerator
>
4479 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4480 _UniformRandomNumberGenerator
& __urng
,
4481 const param_type
& __p
)
4482 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4484 template<typename _UniformRandomNumberGenerator
>
4486 __generate(result_type
* __f
, result_type
* __t
,
4487 _UniformRandomNumberGenerator
& __urng
,
4488 const param_type
& __p
)
4489 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4492 * @brief Return true if two Poisson distributions have the same
4493 * parameters and the sequences that would be generated
4497 operator==(const poisson_distribution
& __d1
,
4498 const poisson_distribution
& __d2
)
4499 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4500 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
4502 { return __d1
._M_param
== __d2
._M_param
; }
4506 * @brief Inserts a %poisson_distribution random number distribution
4507 * @p __x into the output stream @p __os.
4509 * @param __os An output stream.
4510 * @param __x A %poisson_distribution random number distribution.
4512 * @returns The output stream with the state of @p __x inserted or in
4515 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4516 friend std::basic_ostream
<_CharT
, _Traits
>&
4517 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4518 const std::poisson_distribution
<_IntType1
>& __x
);
4521 * @brief Extracts a %poisson_distribution random number distribution
4522 * @p __x from the input stream @p __is.
4524 * @param __is An input stream.
4525 * @param __x A %poisson_distribution random number generator engine.
4527 * @returns The input stream with @p __x extracted or in an error
4530 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4531 friend std::basic_istream
<_CharT
, _Traits
>&
4532 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4533 std::poisson_distribution
<_IntType1
>& __x
);
4536 template<typename _ForwardIterator
,
4537 typename _UniformRandomNumberGenerator
>
4539 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4540 _UniformRandomNumberGenerator
& __urng
,
4541 const param_type
& __p
);
4543 param_type _M_param
;
4545 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4546 std::normal_distribution
<double> _M_nd
;
4550 * @brief Return true if two Poisson distributions are different.
4552 template<typename _IntType
>
4554 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4555 const std::poisson_distribution
<_IntType
>& __d2
)
4556 { return !(__d1
== __d2
); }
4560 * @brief An exponential continuous distribution for random numbers.
4562 * The formula for the exponential probability density function is
4563 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4565 * <table border=1 cellpadding=10 cellspacing=0>
4566 * <caption align=top>Distribution Statistics</caption>
4567 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4568 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4569 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4570 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4571 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4574 template<typename _RealType
= double>
4575 class exponential_distribution
4577 static_assert(std::is_floating_point
<_RealType
>::value
,
4578 "result_type must be a floating point type");
4581 /** The type of the range of the distribution. */
4582 typedef _RealType result_type
;
4584 /** Parameter type. */
4587 typedef exponential_distribution
<_RealType
> distribution_type
;
4590 param_type(_RealType __lambda
= _RealType(1))
4591 : _M_lambda(__lambda
)
4593 __glibcxx_assert(_M_lambda
> _RealType(0));
4598 { return _M_lambda
; }
4601 operator==(const param_type
& __p1
, const param_type
& __p2
)
4602 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4605 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4606 { return !(__p1
== __p2
); }
4609 _RealType _M_lambda
;
4614 * @brief Constructs an exponential distribution with inverse scale
4615 * parameter @f$\lambda@f$.
4618 exponential_distribution(const result_type
& __lambda
= result_type(1))
4619 : _M_param(__lambda
)
4623 exponential_distribution(const param_type
& __p
)
4628 * @brief Resets the distribution state.
4630 * Has no effect on exponential distributions.
4636 * @brief Returns the inverse scale parameter of the distribution.
4640 { return _M_param
.lambda(); }
4643 * @brief Returns the parameter set of the distribution.
4647 { return _M_param
; }
4650 * @brief Sets the parameter set of the distribution.
4651 * @param __param The new parameter set of the distribution.
4654 param(const param_type
& __param
)
4655 { _M_param
= __param
; }
4658 * @brief Returns the greatest lower bound value of the distribution.
4662 { return result_type(0); }
4665 * @brief Returns the least upper bound value of the distribution.
4669 { return std::numeric_limits
<result_type
>::max(); }
4672 * @brief Generating functions.
4674 template<typename _UniformRandomNumberGenerator
>
4676 operator()(_UniformRandomNumberGenerator
& __urng
)
4677 { return this->operator()(__urng
, _M_param
); }
4679 template<typename _UniformRandomNumberGenerator
>
4681 operator()(_UniformRandomNumberGenerator
& __urng
,
4682 const param_type
& __p
)
4684 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4686 return -std::log(result_type(1) - __aurng()) / __p
.lambda();
4689 template<typename _ForwardIterator
,
4690 typename _UniformRandomNumberGenerator
>
4692 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4693 _UniformRandomNumberGenerator
& __urng
)
4694 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4696 template<typename _ForwardIterator
,
4697 typename _UniformRandomNumberGenerator
>
4699 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4700 _UniformRandomNumberGenerator
& __urng
,
4701 const param_type
& __p
)
4702 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4704 template<typename _UniformRandomNumberGenerator
>
4706 __generate(result_type
* __f
, result_type
* __t
,
4707 _UniformRandomNumberGenerator
& __urng
,
4708 const param_type
& __p
)
4709 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4712 * @brief Return true if two exponential distributions have the same
4716 operator==(const exponential_distribution
& __d1
,
4717 const exponential_distribution
& __d2
)
4718 { return __d1
._M_param
== __d2
._M_param
; }
4721 template<typename _ForwardIterator
,
4722 typename _UniformRandomNumberGenerator
>
4724 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4725 _UniformRandomNumberGenerator
& __urng
,
4726 const param_type
& __p
);
4728 param_type _M_param
;
4732 * @brief Return true if two exponential distributions have different
4735 template<typename _RealType
>
4737 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4738 const std::exponential_distribution
<_RealType
>& __d2
)
4739 { return !(__d1
== __d2
); }
4742 * @brief Inserts a %exponential_distribution random number distribution
4743 * @p __x into the output stream @p __os.
4745 * @param __os An output stream.
4746 * @param __x A %exponential_distribution random number distribution.
4748 * @returns The output stream with the state of @p __x inserted or in
4751 template<typename _RealType
, typename _CharT
, typename _Traits
>
4752 std::basic_ostream
<_CharT
, _Traits
>&
4753 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4754 const std::exponential_distribution
<_RealType
>& __x
);
4757 * @brief Extracts a %exponential_distribution random number distribution
4758 * @p __x from the input stream @p __is.
4760 * @param __is An input stream.
4761 * @param __x A %exponential_distribution random number
4764 * @returns The input stream with @p __x extracted or in an error state.
4766 template<typename _RealType
, typename _CharT
, typename _Traits
>
4767 std::basic_istream
<_CharT
, _Traits
>&
4768 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4769 std::exponential_distribution
<_RealType
>& __x
);
4773 * @brief A weibull_distribution random number distribution.
4775 * The formula for the normal probability density function is:
4777 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4778 * \exp{(-(\frac{x}{\beta})^\alpha)}
4781 template<typename _RealType
= double>
4782 class weibull_distribution
4784 static_assert(std::is_floating_point
<_RealType
>::value
,
4785 "result_type must be a floating point type");
4788 /** The type of the range of the distribution. */
4789 typedef _RealType result_type
;
4791 /** Parameter type. */
4794 typedef weibull_distribution
<_RealType
> distribution_type
;
4797 param_type(_RealType __a
= _RealType(1),
4798 _RealType __b
= _RealType(1))
4799 : _M_a(__a
), _M_b(__b
)
4811 operator==(const param_type
& __p1
, const param_type
& __p2
)
4812 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4815 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4816 { return !(__p1
== __p2
); }
4824 weibull_distribution(_RealType __a
= _RealType(1),
4825 _RealType __b
= _RealType(1))
4826 : _M_param(__a
, __b
)
4830 weibull_distribution(const param_type
& __p
)
4835 * @brief Resets the distribution state.
4842 * @brief Return the @f$a@f$ parameter of the distribution.
4846 { return _M_param
.a(); }
4849 * @brief Return the @f$b@f$ parameter of the distribution.
4853 { return _M_param
.b(); }
4856 * @brief Returns the parameter set of the distribution.
4860 { return _M_param
; }
4863 * @brief Sets the parameter set of the distribution.
4864 * @param __param The new parameter set of the distribution.
4867 param(const param_type
& __param
)
4868 { _M_param
= __param
; }
4871 * @brief Returns the greatest lower bound value of the distribution.
4875 { return result_type(0); }
4878 * @brief Returns the least upper bound value of the distribution.
4882 { return std::numeric_limits
<result_type
>::max(); }
4885 * @brief Generating functions.
4887 template<typename _UniformRandomNumberGenerator
>
4889 operator()(_UniformRandomNumberGenerator
& __urng
)
4890 { return this->operator()(__urng
, _M_param
); }
4892 template<typename _UniformRandomNumberGenerator
>
4894 operator()(_UniformRandomNumberGenerator
& __urng
,
4895 const param_type
& __p
);
4897 template<typename _ForwardIterator
,
4898 typename _UniformRandomNumberGenerator
>
4900 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4901 _UniformRandomNumberGenerator
& __urng
)
4902 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4904 template<typename _ForwardIterator
,
4905 typename _UniformRandomNumberGenerator
>
4907 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4908 _UniformRandomNumberGenerator
& __urng
,
4909 const param_type
& __p
)
4910 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4912 template<typename _UniformRandomNumberGenerator
>
4914 __generate(result_type
* __f
, result_type
* __t
,
4915 _UniformRandomNumberGenerator
& __urng
,
4916 const param_type
& __p
)
4917 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4920 * @brief Return true if two Weibull distributions have the same
4924 operator==(const weibull_distribution
& __d1
,
4925 const weibull_distribution
& __d2
)
4926 { return __d1
._M_param
== __d2
._M_param
; }
4929 template<typename _ForwardIterator
,
4930 typename _UniformRandomNumberGenerator
>
4932 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4933 _UniformRandomNumberGenerator
& __urng
,
4934 const param_type
& __p
);
4936 param_type _M_param
;
4940 * @brief Return true if two Weibull distributions have different
4943 template<typename _RealType
>
4945 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
4946 const std::weibull_distribution
<_RealType
>& __d2
)
4947 { return !(__d1
== __d2
); }
4950 * @brief Inserts a %weibull_distribution random number distribution
4951 * @p __x into the output stream @p __os.
4953 * @param __os An output stream.
4954 * @param __x A %weibull_distribution random number distribution.
4956 * @returns The output stream with the state of @p __x inserted or in
4959 template<typename _RealType
, typename _CharT
, typename _Traits
>
4960 std::basic_ostream
<_CharT
, _Traits
>&
4961 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4962 const std::weibull_distribution
<_RealType
>& __x
);
4965 * @brief Extracts a %weibull_distribution random number distribution
4966 * @p __x from the input stream @p __is.
4968 * @param __is An input stream.
4969 * @param __x A %weibull_distribution random number
4972 * @returns The input stream with @p __x extracted or in an error state.
4974 template<typename _RealType
, typename _CharT
, typename _Traits
>
4975 std::basic_istream
<_CharT
, _Traits
>&
4976 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4977 std::weibull_distribution
<_RealType
>& __x
);
4981 * @brief A extreme_value_distribution random number distribution.
4983 * The formula for the normal probability mass function is
4985 * p(x|a,b) = \frac{1}{b}
4986 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
4989 template<typename _RealType
= double>
4990 class extreme_value_distribution
4992 static_assert(std::is_floating_point
<_RealType
>::value
,
4993 "result_type must be a floating point type");
4996 /** The type of the range of the distribution. */
4997 typedef _RealType result_type
;
4999 /** Parameter type. */
5002 typedef extreme_value_distribution
<_RealType
> distribution_type
;
5005 param_type(_RealType __a
= _RealType(0),
5006 _RealType __b
= _RealType(1))
5007 : _M_a(__a
), _M_b(__b
)
5019 operator==(const param_type
& __p1
, const param_type
& __p2
)
5020 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
5023 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5024 { return !(__p1
== __p2
); }
5032 extreme_value_distribution(_RealType __a
= _RealType(0),
5033 _RealType __b
= _RealType(1))
5034 : _M_param(__a
, __b
)
5038 extreme_value_distribution(const param_type
& __p
)
5043 * @brief Resets the distribution state.
5050 * @brief Return the @f$a@f$ parameter of the distribution.
5054 { return _M_param
.a(); }
5057 * @brief Return the @f$b@f$ parameter of the distribution.
5061 { return _M_param
.b(); }
5064 * @brief Returns the parameter set of the distribution.
5068 { return _M_param
; }
5071 * @brief Sets the parameter set of the distribution.
5072 * @param __param The new parameter set of the distribution.
5075 param(const param_type
& __param
)
5076 { _M_param
= __param
; }
5079 * @brief Returns the greatest lower bound value of the distribution.
5083 { return std::numeric_limits
<result_type
>::lowest(); }
5086 * @brief Returns the least upper bound value of the distribution.
5090 { return std::numeric_limits
<result_type
>::max(); }
5093 * @brief Generating functions.
5095 template<typename _UniformRandomNumberGenerator
>
5097 operator()(_UniformRandomNumberGenerator
& __urng
)
5098 { return this->operator()(__urng
, _M_param
); }
5100 template<typename _UniformRandomNumberGenerator
>
5102 operator()(_UniformRandomNumberGenerator
& __urng
,
5103 const param_type
& __p
);
5105 template<typename _ForwardIterator
,
5106 typename _UniformRandomNumberGenerator
>
5108 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5109 _UniformRandomNumberGenerator
& __urng
)
5110 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5112 template<typename _ForwardIterator
,
5113 typename _UniformRandomNumberGenerator
>
5115 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5116 _UniformRandomNumberGenerator
& __urng
,
5117 const param_type
& __p
)
5118 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5120 template<typename _UniformRandomNumberGenerator
>
5122 __generate(result_type
* __f
, result_type
* __t
,
5123 _UniformRandomNumberGenerator
& __urng
,
5124 const param_type
& __p
)
5125 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5128 * @brief Return true if two extreme value distributions have the same
5132 operator==(const extreme_value_distribution
& __d1
,
5133 const extreme_value_distribution
& __d2
)
5134 { return __d1
._M_param
== __d2
._M_param
; }
5137 template<typename _ForwardIterator
,
5138 typename _UniformRandomNumberGenerator
>
5140 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5141 _UniformRandomNumberGenerator
& __urng
,
5142 const param_type
& __p
);
5144 param_type _M_param
;
5148 * @brief Return true if two extreme value distributions have different
5151 template<typename _RealType
>
5153 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
5154 const std::extreme_value_distribution
<_RealType
>& __d2
)
5155 { return !(__d1
== __d2
); }
5158 * @brief Inserts a %extreme_value_distribution random number distribution
5159 * @p __x into the output stream @p __os.
5161 * @param __os An output stream.
5162 * @param __x A %extreme_value_distribution random number distribution.
5164 * @returns The output stream with the state of @p __x inserted or in
5167 template<typename _RealType
, typename _CharT
, typename _Traits
>
5168 std::basic_ostream
<_CharT
, _Traits
>&
5169 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5170 const std::extreme_value_distribution
<_RealType
>& __x
);
5173 * @brief Extracts a %extreme_value_distribution random number
5174 * distribution @p __x from the input stream @p __is.
5176 * @param __is An input stream.
5177 * @param __x A %extreme_value_distribution random number
5180 * @returns The input stream with @p __x extracted or in an error state.
5182 template<typename _RealType
, typename _CharT
, typename _Traits
>
5183 std::basic_istream
<_CharT
, _Traits
>&
5184 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5185 std::extreme_value_distribution
<_RealType
>& __x
);
5189 * @brief A discrete_distribution random number distribution.
5191 * The formula for the discrete probability mass function is
5194 template<typename _IntType
= int>
5195 class discrete_distribution
5197 static_assert(std::is_integral
<_IntType
>::value
,
5198 "result_type must be an integral type");
5201 /** The type of the range of the distribution. */
5202 typedef _IntType result_type
;
5204 /** Parameter type. */
5207 typedef discrete_distribution
<_IntType
> distribution_type
;
5208 friend class discrete_distribution
<_IntType
>;
5211 : _M_prob(), _M_cp()
5214 template<typename _InputIterator
>
5215 param_type(_InputIterator __wbegin
,
5216 _InputIterator __wend
)
5217 : _M_prob(__wbegin
, __wend
), _M_cp()
5218 { _M_initialize(); }
5220 param_type(initializer_list
<double> __wil
)
5221 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
5222 { _M_initialize(); }
5224 template<typename _Func
>
5225 param_type(size_t __nw
, double __xmin
, double __xmax
,
5228 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5229 param_type(const param_type
&) = default;
5230 param_type
& operator=(const param_type
&) = default;
5233 probabilities() const
5234 { return _M_prob
.empty() ? std::vector
<double>(1, 1.0) : _M_prob
; }
5237 operator==(const param_type
& __p1
, const param_type
& __p2
)
5238 { return __p1
._M_prob
== __p2
._M_prob
; }
5241 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5242 { return !(__p1
== __p2
); }
5248 std::vector
<double> _M_prob
;
5249 std::vector
<double> _M_cp
;
5252 discrete_distribution()
5256 template<typename _InputIterator
>
5257 discrete_distribution(_InputIterator __wbegin
,
5258 _InputIterator __wend
)
5259 : _M_param(__wbegin
, __wend
)
5262 discrete_distribution(initializer_list
<double> __wl
)
5266 template<typename _Func
>
5267 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
5269 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5273 discrete_distribution(const param_type
& __p
)
5278 * @brief Resets the distribution state.
5285 * @brief Returns the probabilities of the distribution.
5288 probabilities() const
5290 return _M_param
._M_prob
.empty()
5291 ? std::vector
<double>(1, 1.0) : _M_param
._M_prob
;
5295 * @brief Returns the parameter set of the distribution.
5299 { return _M_param
; }
5302 * @brief Sets the parameter set of the distribution.
5303 * @param __param The new parameter set of the distribution.
5306 param(const param_type
& __param
)
5307 { _M_param
= __param
; }
5310 * @brief Returns the greatest lower bound value of the distribution.
5314 { return result_type(0); }
5317 * @brief Returns the least upper bound value of the distribution.
5322 return _M_param
._M_prob
.empty()
5323 ? result_type(0) : result_type(_M_param
._M_prob
.size() - 1);
5327 * @brief Generating functions.
5329 template<typename _UniformRandomNumberGenerator
>
5331 operator()(_UniformRandomNumberGenerator
& __urng
)
5332 { return this->operator()(__urng
, _M_param
); }
5334 template<typename _UniformRandomNumberGenerator
>
5336 operator()(_UniformRandomNumberGenerator
& __urng
,
5337 const param_type
& __p
);
5339 template<typename _ForwardIterator
,
5340 typename _UniformRandomNumberGenerator
>
5342 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5343 _UniformRandomNumberGenerator
& __urng
)
5344 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5346 template<typename _ForwardIterator
,
5347 typename _UniformRandomNumberGenerator
>
5349 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5350 _UniformRandomNumberGenerator
& __urng
,
5351 const param_type
& __p
)
5352 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5354 template<typename _UniformRandomNumberGenerator
>
5356 __generate(result_type
* __f
, result_type
* __t
,
5357 _UniformRandomNumberGenerator
& __urng
,
5358 const param_type
& __p
)
5359 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5362 * @brief Return true if two discrete distributions have the same
5366 operator==(const discrete_distribution
& __d1
,
5367 const discrete_distribution
& __d2
)
5368 { return __d1
._M_param
== __d2
._M_param
; }
5371 * @brief Inserts a %discrete_distribution random number distribution
5372 * @p __x into the output stream @p __os.
5374 * @param __os An output stream.
5375 * @param __x A %discrete_distribution random number distribution.
5377 * @returns The output stream with the state of @p __x inserted or in
5380 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5381 friend std::basic_ostream
<_CharT
, _Traits
>&
5382 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5383 const std::discrete_distribution
<_IntType1
>& __x
);
5386 * @brief Extracts a %discrete_distribution random number distribution
5387 * @p __x from the input stream @p __is.
5389 * @param __is An input stream.
5390 * @param __x A %discrete_distribution random number
5393 * @returns The input stream with @p __x extracted or in an error
5396 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5397 friend std::basic_istream
<_CharT
, _Traits
>&
5398 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5399 std::discrete_distribution
<_IntType1
>& __x
);
5402 template<typename _ForwardIterator
,
5403 typename _UniformRandomNumberGenerator
>
5405 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5406 _UniformRandomNumberGenerator
& __urng
,
5407 const param_type
& __p
);
5409 param_type _M_param
;
5413 * @brief Return true if two discrete distributions have different
5416 template<typename _IntType
>
5418 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
5419 const std::discrete_distribution
<_IntType
>& __d2
)
5420 { return !(__d1
== __d2
); }
5424 * @brief A piecewise_constant_distribution random number distribution.
5426 * The formula for the piecewise constant probability mass function is
5429 template<typename _RealType
= double>
5430 class piecewise_constant_distribution
5432 static_assert(std::is_floating_point
<_RealType
>::value
,
5433 "result_type must be a floating point type");
5436 /** The type of the range of the distribution. */
5437 typedef _RealType result_type
;
5439 /** Parameter type. */
5442 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
5443 friend class piecewise_constant_distribution
<_RealType
>;
5446 : _M_int(), _M_den(), _M_cp()
5449 template<typename _InputIteratorB
, typename _InputIteratorW
>
5450 param_type(_InputIteratorB __bfirst
,
5451 _InputIteratorB __bend
,
5452 _InputIteratorW __wbegin
);
5454 template<typename _Func
>
5455 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
5457 template<typename _Func
>
5458 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5461 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5462 param_type(const param_type
&) = default;
5463 param_type
& operator=(const param_type
&) = default;
5465 std::vector
<_RealType
>
5470 std::vector
<_RealType
> __tmp(2);
5471 __tmp
[1] = _RealType(1);
5480 { return _M_den
.empty() ? std::vector
<double>(1, 1.0) : _M_den
; }
5483 operator==(const param_type
& __p1
, const param_type
& __p2
)
5484 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
5487 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5488 { return !(__p1
== __p2
); }
5494 std::vector
<_RealType
> _M_int
;
5495 std::vector
<double> _M_den
;
5496 std::vector
<double> _M_cp
;
5500 piecewise_constant_distribution()
5504 template<typename _InputIteratorB
, typename _InputIteratorW
>
5505 piecewise_constant_distribution(_InputIteratorB __bfirst
,
5506 _InputIteratorB __bend
,
5507 _InputIteratorW __wbegin
)
5508 : _M_param(__bfirst
, __bend
, __wbegin
)
5511 template<typename _Func
>
5512 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
5514 : _M_param(__bl
, __fw
)
5517 template<typename _Func
>
5518 piecewise_constant_distribution(size_t __nw
,
5519 _RealType __xmin
, _RealType __xmax
,
5521 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5525 piecewise_constant_distribution(const param_type
& __p
)
5530 * @brief Resets the distribution state.
5537 * @brief Returns a vector of the intervals.
5539 std::vector
<_RealType
>
5542 if (_M_param
._M_int
.empty())
5544 std::vector
<_RealType
> __tmp(2);
5545 __tmp
[1] = _RealType(1);
5549 return _M_param
._M_int
;
5553 * @brief Returns a vector of the probability densities.
5558 return _M_param
._M_den
.empty()
5559 ? std::vector
<double>(1, 1.0) : _M_param
._M_den
;
5563 * @brief Returns the parameter set of the distribution.
5567 { return _M_param
; }
5570 * @brief Sets the parameter set of the distribution.
5571 * @param __param The new parameter set of the distribution.
5574 param(const param_type
& __param
)
5575 { _M_param
= __param
; }
5578 * @brief Returns the greatest lower bound value of the distribution.
5583 return _M_param
._M_int
.empty()
5584 ? result_type(0) : _M_param
._M_int
.front();
5588 * @brief Returns the least upper bound value of the distribution.
5593 return _M_param
._M_int
.empty()
5594 ? result_type(1) : _M_param
._M_int
.back();
5598 * @brief Generating functions.
5600 template<typename _UniformRandomNumberGenerator
>
5602 operator()(_UniformRandomNumberGenerator
& __urng
)
5603 { return this->operator()(__urng
, _M_param
); }
5605 template<typename _UniformRandomNumberGenerator
>
5607 operator()(_UniformRandomNumberGenerator
& __urng
,
5608 const param_type
& __p
);
5610 template<typename _ForwardIterator
,
5611 typename _UniformRandomNumberGenerator
>
5613 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5614 _UniformRandomNumberGenerator
& __urng
)
5615 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5617 template<typename _ForwardIterator
,
5618 typename _UniformRandomNumberGenerator
>
5620 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5621 _UniformRandomNumberGenerator
& __urng
,
5622 const param_type
& __p
)
5623 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5625 template<typename _UniformRandomNumberGenerator
>
5627 __generate(result_type
* __f
, result_type
* __t
,
5628 _UniformRandomNumberGenerator
& __urng
,
5629 const param_type
& __p
)
5630 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5633 * @brief Return true if two piecewise constant distributions have the
5637 operator==(const piecewise_constant_distribution
& __d1
,
5638 const piecewise_constant_distribution
& __d2
)
5639 { return __d1
._M_param
== __d2
._M_param
; }
5642 * @brief Inserts a %piecewise_constant_distribution random
5643 * number distribution @p __x into the output stream @p __os.
5645 * @param __os An output stream.
5646 * @param __x A %piecewise_constant_distribution random number
5649 * @returns The output stream with the state of @p __x inserted or in
5652 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5653 friend std::basic_ostream
<_CharT
, _Traits
>&
5654 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5655 const std::piecewise_constant_distribution
<_RealType1
>& __x
);
5658 * @brief Extracts a %piecewise_constant_distribution random
5659 * number distribution @p __x from the input stream @p __is.
5661 * @param __is An input stream.
5662 * @param __x A %piecewise_constant_distribution random number
5665 * @returns The input stream with @p __x extracted or in an error
5668 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5669 friend std::basic_istream
<_CharT
, _Traits
>&
5670 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5671 std::piecewise_constant_distribution
<_RealType1
>& __x
);
5674 template<typename _ForwardIterator
,
5675 typename _UniformRandomNumberGenerator
>
5677 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5678 _UniformRandomNumberGenerator
& __urng
,
5679 const param_type
& __p
);
5681 param_type _M_param
;
5685 * @brief Return true if two piecewise constant distributions have
5686 * different parameters.
5688 template<typename _RealType
>
5690 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5691 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5692 { return !(__d1
== __d2
); }
5696 * @brief A piecewise_linear_distribution random number distribution.
5698 * The formula for the piecewise linear probability mass function is
5701 template<typename _RealType
= double>
5702 class piecewise_linear_distribution
5704 static_assert(std::is_floating_point
<_RealType
>::value
,
5705 "result_type must be a floating point type");
5708 /** The type of the range of the distribution. */
5709 typedef _RealType result_type
;
5711 /** Parameter type. */
5714 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5715 friend class piecewise_linear_distribution
<_RealType
>;
5718 : _M_int(), _M_den(), _M_cp(), _M_m()
5721 template<typename _InputIteratorB
, typename _InputIteratorW
>
5722 param_type(_InputIteratorB __bfirst
,
5723 _InputIteratorB __bend
,
5724 _InputIteratorW __wbegin
);
5726 template<typename _Func
>
5727 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5729 template<typename _Func
>
5730 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5733 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5734 param_type(const param_type
&) = default;
5735 param_type
& operator=(const param_type
&) = default;
5737 std::vector
<_RealType
>
5742 std::vector
<_RealType
> __tmp(2);
5743 __tmp
[1] = _RealType(1);
5752 { return _M_den
.empty() ? std::vector
<double>(2, 1.0) : _M_den
; }
5755 operator==(const param_type
& __p1
, const param_type
& __p2
)
5756 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
5759 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5760 { return !(__p1
== __p2
); }
5766 std::vector
<_RealType
> _M_int
;
5767 std::vector
<double> _M_den
;
5768 std::vector
<double> _M_cp
;
5769 std::vector
<double> _M_m
;
5773 piecewise_linear_distribution()
5777 template<typename _InputIteratorB
, typename _InputIteratorW
>
5778 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5779 _InputIteratorB __bend
,
5780 _InputIteratorW __wbegin
)
5781 : _M_param(__bfirst
, __bend
, __wbegin
)
5784 template<typename _Func
>
5785 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5787 : _M_param(__bl
, __fw
)
5790 template<typename _Func
>
5791 piecewise_linear_distribution(size_t __nw
,
5792 _RealType __xmin
, _RealType __xmax
,
5794 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5798 piecewise_linear_distribution(const param_type
& __p
)
5803 * Resets the distribution state.
5810 * @brief Return the intervals of the distribution.
5812 std::vector
<_RealType
>
5815 if (_M_param
._M_int
.empty())
5817 std::vector
<_RealType
> __tmp(2);
5818 __tmp
[1] = _RealType(1);
5822 return _M_param
._M_int
;
5826 * @brief Return a vector of the probability densities of the
5832 return _M_param
._M_den
.empty()
5833 ? std::vector
<double>(2, 1.0) : _M_param
._M_den
;
5837 * @brief Returns the parameter set of the distribution.
5841 { return _M_param
; }
5844 * @brief Sets the parameter set of the distribution.
5845 * @param __param The new parameter set of the distribution.
5848 param(const param_type
& __param
)
5849 { _M_param
= __param
; }
5852 * @brief Returns the greatest lower bound value of the distribution.
5857 return _M_param
._M_int
.empty()
5858 ? result_type(0) : _M_param
._M_int
.front();
5862 * @brief Returns the least upper bound value of the distribution.
5867 return _M_param
._M_int
.empty()
5868 ? result_type(1) : _M_param
._M_int
.back();
5872 * @brief Generating functions.
5874 template<typename _UniformRandomNumberGenerator
>
5876 operator()(_UniformRandomNumberGenerator
& __urng
)
5877 { return this->operator()(__urng
, _M_param
); }
5879 template<typename _UniformRandomNumberGenerator
>
5881 operator()(_UniformRandomNumberGenerator
& __urng
,
5882 const param_type
& __p
);
5884 template<typename _ForwardIterator
,
5885 typename _UniformRandomNumberGenerator
>
5887 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5888 _UniformRandomNumberGenerator
& __urng
)
5889 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5891 template<typename _ForwardIterator
,
5892 typename _UniformRandomNumberGenerator
>
5894 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5895 _UniformRandomNumberGenerator
& __urng
,
5896 const param_type
& __p
)
5897 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5899 template<typename _UniformRandomNumberGenerator
>
5901 __generate(result_type
* __f
, result_type
* __t
,
5902 _UniformRandomNumberGenerator
& __urng
,
5903 const param_type
& __p
)
5904 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5907 * @brief Return true if two piecewise linear distributions have the
5911 operator==(const piecewise_linear_distribution
& __d1
,
5912 const piecewise_linear_distribution
& __d2
)
5913 { return __d1
._M_param
== __d2
._M_param
; }
5916 * @brief Inserts a %piecewise_linear_distribution random number
5917 * distribution @p __x into the output stream @p __os.
5919 * @param __os An output stream.
5920 * @param __x A %piecewise_linear_distribution random number
5923 * @returns The output stream with the state of @p __x inserted or in
5926 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5927 friend std::basic_ostream
<_CharT
, _Traits
>&
5928 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5929 const std::piecewise_linear_distribution
<_RealType1
>& __x
);
5932 * @brief Extracts a %piecewise_linear_distribution random number
5933 * distribution @p __x from the input stream @p __is.
5935 * @param __is An input stream.
5936 * @param __x A %piecewise_linear_distribution random number
5939 * @returns The input stream with @p __x extracted or in an error
5942 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5943 friend std::basic_istream
<_CharT
, _Traits
>&
5944 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5945 std::piecewise_linear_distribution
<_RealType1
>& __x
);
5948 template<typename _ForwardIterator
,
5949 typename _UniformRandomNumberGenerator
>
5951 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5952 _UniformRandomNumberGenerator
& __urng
,
5953 const param_type
& __p
);
5955 param_type _M_param
;
5959 * @brief Return true if two piecewise linear distributions have
5960 * different parameters.
5962 template<typename _RealType
>
5964 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5965 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5966 { return !(__d1
== __d2
); }
5969 /* @} */ // group random_distributions_poisson
5971 /* @} */ // group random_distributions
5974 * @addtogroup random_utilities Random Number Utilities
5980 * @brief The seed_seq class generates sequences of seeds for random
5981 * number generators.
5986 /** The type of the seed vales. */
5987 typedef uint_least32_t result_type
;
5989 /** Default constructor. */
5994 template<typename _IntType
>
5995 seed_seq(std::initializer_list
<_IntType
> il
);
5997 template<typename _InputIterator
>
5998 seed_seq(_InputIterator __begin
, _InputIterator __end
);
6000 // generating functions
6001 template<typename _RandomAccessIterator
>
6003 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
6005 // property functions
6006 size_t size() const noexcept
6007 { return _M_v
.size(); }
6009 template<typename OutputIterator
>
6011 param(OutputIterator __dest
) const
6012 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
6014 // no copy functions
6015 seed_seq(const seed_seq
&) = delete;
6016 seed_seq
& operator=(const seed_seq
&) = delete;
6019 std::vector
<result_type
> _M_v
;
6022 /* @} */ // group random_utilities
6024 /* @} */ // group random
6026 _GLIBCXX_END_NAMESPACE_VERSION