1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009-2023 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.
51 // std::uniform_random_bit_generator is defined in <bits/uniform_int_dist.h>
54 * @brief A function template for converting the output of a (integral)
55 * uniform random number generator to a floatng point result in the range
58 template<typename _RealType
, size_t __bits
,
59 typename _UniformRandomNumberGenerator
>
61 generate_canonical(_UniformRandomNumberGenerator
& __g
);
63 /// @cond undocumented
64 // Implementation-space details.
67 template<typename _UIntType
, size_t __w
,
68 bool = __w
< static_cast<size_t>
69 (std::numeric_limits
<_UIntType
>::digits
)>
71 { static constexpr _UIntType __value
= 0; };
73 template<typename _UIntType
, size_t __w
>
74 struct _Shift
<_UIntType
, __w
, true>
75 { static constexpr _UIntType __value
= _UIntType(1) << __w
; };
78 int __which
= ((__s
<= __CHAR_BIT__
* sizeof (int))
79 + (__s
<= __CHAR_BIT__
* sizeof (long))
80 + (__s
<= __CHAR_BIT__
* sizeof (long long))
81 /* assume long long no bigger than __int128 */
83 struct _Select_uint_least_t
85 static_assert(__which
< 0, /* needs to be dependent */
86 "sorry, would be too much trouble for a slow result");
90 struct _Select_uint_least_t
<__s
, 4>
91 { using type
= unsigned int; };
94 struct _Select_uint_least_t
<__s
, 3>
95 { using type
= unsigned long; };
98 struct _Select_uint_least_t
<__s
, 2>
99 { using type
= unsigned long long; };
101 #if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
103 struct _Select_uint_least_t
<__s
, 1>
104 { __extension__
using type
= unsigned __int128
; };
107 // Assume a != 0, a < m, c < m, x < m.
108 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
,
109 bool __big_enough
= (!(__m
& (__m
- 1))
110 || (_Tp(-1) - __c
) / __a
>= __m
- 1),
111 bool __schrage_ok
= __m
% __a
< __m
/ __a
>
118 = typename _Select_uint_least_t
<std::__lg(__a
)
119 + std::__lg(__m
) + 2>::type
;
120 return static_cast<_Tp
>((_Tp2(__a
) * __x
+ __c
) % __m
);
125 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
>
126 struct _Mod
<_Tp
, __m
, __a
, __c
, false, true>
133 // - for m == 2^n or m == 0, unsigned integer overflow is safe.
134 // - a * (m - 1) + c fits in _Tp, there is no overflow.
135 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool __s
>
136 struct _Mod
<_Tp
, __m
, __a
, __c
, true, __s
>
141 _Tp __res
= __a
* __x
+ __c
;
148 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
152 if _GLIBCXX17_CONSTEXPR (__a
== 0)
156 // _Mod must not be instantiated with a == 0
157 constexpr _Tp __a1
= __a
? __a
: 1;
158 return _Mod
<_Tp
, __m
, __a1
, __c
>::__calc(__x
);
163 * An adaptor class for converting the output of any Generator into
164 * the input for a specific Distribution.
166 template<typename _Engine
, typename _DInputType
>
169 static_assert(std::is_floating_point
<_DInputType
>::value
,
170 "template argument must be a floating point type");
173 _Adaptor(_Engine
& __g
)
178 { return _DInputType(0); }
182 { return _DInputType(1); }
185 * Converts a value generated by the adapted random number generator
186 * into a value in the input domain for the dependent random number
192 return std::generate_canonical
<_DInputType
,
193 std::numeric_limits
<_DInputType
>::digits
,
201 // Detect whether a template argument _Sseq is a valid seed sequence for
202 // a random number engine _Engine with result type _Res.
203 // Used to constrain _Engine::_Engine(_Sseq&) and _Engine::seed(_Sseq&)
204 // as required by [rand.eng.general].
206 template<typename _Sseq
>
207 using __seed_seq_generate_t
= decltype(
208 std::declval
<_Sseq
&>().generate(std::declval
<uint_least32_t*>(),
209 std::declval
<uint_least32_t*>()));
211 template<typename _Sseq
, typename _Engine
, typename _Res
,
212 typename _GenerateCheck
= __seed_seq_generate_t
<_Sseq
>>
213 using _If_seed_seq_for
= _Require
<
214 __not_
<is_same
<__remove_cvref_t
<_Sseq
>, _Engine
>>,
215 is_unsigned
<typename
_Sseq::result_type
>,
216 __not_
<is_convertible
<_Sseq
, _Res
>>
219 } // namespace __detail
223 * @addtogroup random_generators Random Number Generators
226 * These classes define objects which provide random or pseudorandom
227 * numbers, either from a discrete or a continuous interval. The
228 * random number generator supplied as a part of this library are
229 * all uniform random number generators which provide a sequence of
230 * random number uniformly distributed over their range.
232 * A number generator is a function object with an operator() that
233 * takes zero arguments and returns a number.
235 * A compliant random number generator must satisfy the following
236 * requirements. <table border=1 cellpadding=10 cellspacing=0>
237 * <caption align=top>Random Number Generator Requirements</caption>
238 * <tr><td>To be documented.</td></tr> </table>
244 * @brief A model of a linear congruential random number generator.
246 * A random number generator that produces pseudorandom numbers via
249 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
252 * The template parameter @p _UIntType must be an unsigned integral type
253 * large enough to store values up to (__m-1). If the template parameter
254 * @p __m is 0, the modulus @p __m used is
255 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
256 * parameters @p __a and @p __c must be less than @p __m.
258 * The size of the state is @f$1@f$.
260 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
261 class linear_congruential_engine
263 static_assert(std::is_unsigned
<_UIntType
>::value
,
264 "result_type must be an unsigned integral type");
265 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
266 "template argument substituting __m out of bounds");
268 template<typename _Sseq
>
270 = __detail::_If_seed_seq_for
<_Sseq
, linear_congruential_engine
,
274 /** The type of the generated random value. */
275 typedef _UIntType result_type
;
277 /** The multiplier. */
278 static constexpr result_type multiplier
= __a
;
280 static constexpr result_type increment
= __c
;
282 static constexpr result_type modulus
= __m
;
283 static constexpr result_type default_seed
= 1u;
286 * @brief Constructs a %linear_congruential_engine random number
287 * generator engine with seed 1.
289 linear_congruential_engine() : linear_congruential_engine(default_seed
)
293 * @brief Constructs a %linear_congruential_engine random number
294 * generator engine with seed @p __s. The default seed value
297 * @param __s The initial seed value.
300 linear_congruential_engine(result_type __s
)
304 * @brief Constructs a %linear_congruential_engine random number
305 * generator engine seeded from the seed sequence @p __q.
307 * @param __q the seed sequence.
309 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
311 linear_congruential_engine(_Sseq
& __q
)
315 * @brief Reseeds the %linear_congruential_engine random number generator
316 * engine sequence to the seed @p __s.
318 * @param __s The new seed.
321 seed(result_type __s
= default_seed
);
324 * @brief Reseeds the %linear_congruential_engine random number generator
326 * sequence using values from the seed sequence @p __q.
328 * @param __q the seed sequence.
330 template<typename _Sseq
>
335 * @brief Gets the smallest possible value in the output range.
337 * The minimum depends on the @p __c parameter: if it is zero, the
338 * minimum generated must be > 0, otherwise 0 is allowed.
340 static constexpr result_type
342 { return __c
== 0u ? 1u : 0u; }
345 * @brief Gets the largest possible value in the output range.
347 static constexpr result_type
352 * @brief Discard a sequence of random numbers.
355 discard(unsigned long long __z
)
357 for (; __z
!= 0ULL; --__z
)
362 * @brief Gets the next random number in the sequence.
367 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
372 * @brief Compares two linear congruential random number generator
373 * objects of the same type for equality.
375 * @param __lhs A linear congruential random number generator object.
376 * @param __rhs Another linear congruential random number generator
379 * @returns true if the infinite sequences of generated values
380 * would be equal, false otherwise.
383 operator==(const linear_congruential_engine
& __lhs
,
384 const linear_congruential_engine
& __rhs
)
385 { return __lhs
._M_x
== __rhs
._M_x
; }
388 * @brief Writes the textual representation of the state x(i) of x to
391 * @param __os The output stream.
392 * @param __lcr A % linear_congruential_engine random number generator.
395 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
396 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
397 friend std::basic_ostream
<_CharT
, _Traits
>&
398 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
399 const std::linear_congruential_engine
<_UIntType1
,
400 __a1
, __c1
, __m1
>& __lcr
);
403 * @brief Sets the state of the engine by reading its textual
404 * representation from @p __is.
406 * The textual representation must have been previously written using
407 * an output stream whose imbued locale and whose type's template
408 * specialization arguments _CharT and _Traits were the same as those
411 * @param __is The input stream.
412 * @param __lcr A % linear_congruential_engine random number generator.
415 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
416 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
417 friend std::basic_istream
<_CharT
, _Traits
>&
418 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
419 std::linear_congruential_engine
<_UIntType1
, __a1
,
426 #if __cpp_impl_three_way_comparison < 201907L
428 * @brief Compares two linear congruential random number generator
429 * objects of the same type for inequality.
431 * @param __lhs A linear congruential random number generator object.
432 * @param __rhs Another linear congruential random number generator
435 * @returns true if the infinite sequences of generated values
436 * would be different, false otherwise.
438 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
440 operator!=(const std::linear_congruential_engine
<_UIntType
, __a
,
442 const std::linear_congruential_engine
<_UIntType
, __a
,
444 { return !(__lhs
== __rhs
); }
448 * A generalized feedback shift register discrete random number generator.
450 * This algorithm avoids multiplication and division and is designed to be
451 * friendly to a pipelined architecture. If the parameters are chosen
452 * correctly, this generator will produce numbers with a very long period and
453 * fairly good apparent entropy, although still not cryptographically strong.
455 * The best way to use this generator is with the predefined mt19937 class.
457 * This algorithm was originally invented by Makoto Matsumoto and
460 * @tparam __w Word size, the number of bits in each element of
462 * @tparam __n The degree of recursion.
463 * @tparam __m The period parameter.
464 * @tparam __r The separation point bit index.
465 * @tparam __a The last row of the twist matrix.
466 * @tparam __u The first right-shift tempering matrix parameter.
467 * @tparam __d The first right-shift tempering matrix mask.
468 * @tparam __s The first left-shift tempering matrix parameter.
469 * @tparam __b The first left-shift tempering matrix mask.
470 * @tparam __t The second left-shift tempering matrix parameter.
471 * @tparam __c The second left-shift tempering matrix mask.
472 * @tparam __l The second right-shift tempering matrix parameter.
473 * @tparam __f Initialization multiplier.
475 template<typename _UIntType
, size_t __w
,
476 size_t __n
, size_t __m
, size_t __r
,
477 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
478 _UIntType __b
, size_t __t
,
479 _UIntType __c
, size_t __l
, _UIntType __f
>
480 class mersenne_twister_engine
482 static_assert(std::is_unsigned
<_UIntType
>::value
,
483 "result_type must be an unsigned integral type");
484 static_assert(1u <= __m
&& __m
<= __n
,
485 "template argument substituting __m out of bounds");
486 static_assert(__r
<= __w
, "template argument substituting "
488 static_assert(__u
<= __w
, "template argument substituting "
490 static_assert(__s
<= __w
, "template argument substituting "
492 static_assert(__t
<= __w
, "template argument substituting "
494 static_assert(__l
<= __w
, "template argument substituting "
496 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
497 "template argument substituting __w out of bound");
498 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
499 "template argument substituting __a out of bound");
500 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
501 "template argument substituting __b out of bound");
502 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
503 "template argument substituting __c out of bound");
504 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
505 "template argument substituting __d out of bound");
506 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
507 "template argument substituting __f out of bound");
509 template<typename _Sseq
>
511 = __detail::_If_seed_seq_for
<_Sseq
, mersenne_twister_engine
,
515 /** The type of the generated random value. */
516 typedef _UIntType result_type
;
519 static constexpr size_t word_size
= __w
;
520 static constexpr size_t state_size
= __n
;
521 static constexpr size_t shift_size
= __m
;
522 static constexpr size_t mask_bits
= __r
;
523 static constexpr result_type xor_mask
= __a
;
524 static constexpr size_t tempering_u
= __u
;
525 static constexpr result_type tempering_d
= __d
;
526 static constexpr size_t tempering_s
= __s
;
527 static constexpr result_type tempering_b
= __b
;
528 static constexpr size_t tempering_t
= __t
;
529 static constexpr result_type tempering_c
= __c
;
530 static constexpr size_t tempering_l
= __l
;
531 static constexpr result_type initialization_multiplier
= __f
;
532 static constexpr result_type default_seed
= 5489u;
534 // constructors and member functions
536 mersenne_twister_engine() : mersenne_twister_engine(default_seed
) { }
539 mersenne_twister_engine(result_type __sd
)
543 * @brief Constructs a %mersenne_twister_engine random number generator
544 * engine seeded from the seed sequence @p __q.
546 * @param __q the seed sequence.
548 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
550 mersenne_twister_engine(_Sseq
& __q
)
554 seed(result_type __sd
= default_seed
);
556 template<typename _Sseq
>
561 * @brief Gets the smallest possible value in the output range.
563 static constexpr result_type
568 * @brief Gets the largest possible value in the output range.
570 static constexpr result_type
572 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
575 * @brief Discard a sequence of random numbers.
578 discard(unsigned long long __z
);
584 * @brief Compares two % mersenne_twister_engine random number generator
585 * objects of the same type for equality.
587 * @param __lhs A % mersenne_twister_engine random number generator
589 * @param __rhs Another % mersenne_twister_engine random number
592 * @returns true if the infinite sequences of generated values
593 * would be equal, false otherwise.
596 operator==(const mersenne_twister_engine
& __lhs
,
597 const mersenne_twister_engine
& __rhs
)
598 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
)
599 && __lhs
._M_p
== __rhs
._M_p
); }
602 * @brief Inserts the current state of a % mersenne_twister_engine
603 * random number generator engine @p __x into the output stream
606 * @param __os An output stream.
607 * @param __x A % mersenne_twister_engine random number generator
610 * @returns The output stream with the state of @p __x inserted or in
613 template<typename _UIntType1
,
614 size_t __w1
, size_t __n1
,
615 size_t __m1
, size_t __r1
,
616 _UIntType1 __a1
, size_t __u1
,
617 _UIntType1 __d1
, size_t __s1
,
618 _UIntType1 __b1
, size_t __t1
,
619 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
620 typename _CharT
, typename _Traits
>
621 friend std::basic_ostream
<_CharT
, _Traits
>&
622 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
623 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
624 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
628 * @brief Extracts the current state of a % mersenne_twister_engine
629 * random number generator engine @p __x from the input stream
632 * @param __is An input stream.
633 * @param __x A % mersenne_twister_engine random number generator
636 * @returns The input stream with the state of @p __x extracted or in
639 template<typename _UIntType1
,
640 size_t __w1
, size_t __n1
,
641 size_t __m1
, size_t __r1
,
642 _UIntType1 __a1
, size_t __u1
,
643 _UIntType1 __d1
, size_t __s1
,
644 _UIntType1 __b1
, size_t __t1
,
645 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
646 typename _CharT
, typename _Traits
>
647 friend std::basic_istream
<_CharT
, _Traits
>&
648 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
649 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
650 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
656 _UIntType _M_x
[state_size
];
660 #if __cpp_impl_three_way_comparison < 201907L
662 * @brief Compares two % mersenne_twister_engine random number generator
663 * objects of the same type for inequality.
665 * @param __lhs A % mersenne_twister_engine random number generator
667 * @param __rhs Another % mersenne_twister_engine random number
670 * @returns true if the infinite sequences of generated values
671 * would be different, false otherwise.
673 template<typename _UIntType
, size_t __w
,
674 size_t __n
, size_t __m
, size_t __r
,
675 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
676 _UIntType __b
, size_t __t
,
677 _UIntType __c
, size_t __l
, _UIntType __f
>
679 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
680 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
681 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
682 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
683 { return !(__lhs
== __rhs
); }
687 * @brief The Marsaglia-Zaman generator.
689 * This is a model of a Generalized Fibonacci discrete random number
690 * generator, sometimes referred to as the SWC generator.
692 * A discrete random number generator that produces pseudorandom
695 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
698 * The size of the state is @f$r@f$
699 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
701 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
702 class subtract_with_carry_engine
704 static_assert(std::is_unsigned
<_UIntType
>::value
,
705 "result_type must be an unsigned integral type");
706 static_assert(0u < __s
&& __s
< __r
,
708 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
709 "template argument substituting __w out of bounds");
711 template<typename _Sseq
>
713 = __detail::_If_seed_seq_for
<_Sseq
, subtract_with_carry_engine
,
717 /** The type of the generated random value. */
718 typedef _UIntType result_type
;
721 static constexpr size_t word_size
= __w
;
722 static constexpr size_t short_lag
= __s
;
723 static constexpr size_t long_lag
= __r
;
724 static constexpr uint_least32_t default_seed
= 19780503u;
726 subtract_with_carry_engine() : subtract_with_carry_engine(0u)
730 * @brief Constructs an explicitly seeded %subtract_with_carry_engine
731 * random number generator.
734 subtract_with_carry_engine(result_type __sd
)
738 * @brief Constructs a %subtract_with_carry_engine random number engine
739 * seeded from the seed sequence @p __q.
741 * @param __q the seed sequence.
743 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
745 subtract_with_carry_engine(_Sseq
& __q
)
749 * @brief Seeds the initial state @f$x_0@f$ of the random number
752 * N1688[4.19] modifies this as follows. If @p __value == 0,
753 * sets value to 19780503. In any case, with a linear
754 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
755 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
756 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
757 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
758 * set carry to 1, otherwise sets carry to 0.
761 seed(result_type __sd
= 0u);
764 * @brief Seeds the initial state @f$x_0@f$ of the
765 * % subtract_with_carry_engine random number generator.
767 template<typename _Sseq
>
772 * @brief Gets the inclusive minimum value of the range of random
773 * integers returned by this generator.
775 static constexpr result_type
780 * @brief Gets the inclusive maximum value of the range of random
781 * integers returned by this generator.
783 static constexpr result_type
785 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
788 * @brief Discard a sequence of random numbers.
791 discard(unsigned long long __z
)
793 for (; __z
!= 0ULL; --__z
)
798 * @brief Gets the next random number in the sequence.
804 * @brief Compares two % subtract_with_carry_engine random number
805 * generator objects of the same type for equality.
807 * @param __lhs A % subtract_with_carry_engine random number generator
809 * @param __rhs Another % subtract_with_carry_engine random number
812 * @returns true if the infinite sequences of generated values
813 * would be equal, false otherwise.
816 operator==(const subtract_with_carry_engine
& __lhs
,
817 const subtract_with_carry_engine
& __rhs
)
818 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
)
819 && __lhs
._M_carry
== __rhs
._M_carry
820 && __lhs
._M_p
== __rhs
._M_p
); }
823 * @brief Inserts the current state of a % subtract_with_carry_engine
824 * random number generator engine @p __x into the output stream
827 * @param __os An output stream.
828 * @param __x A % subtract_with_carry_engine random number generator
831 * @returns The output stream with the state of @p __x inserted or in
834 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
835 typename _CharT
, typename _Traits
>
836 friend std::basic_ostream
<_CharT
, _Traits
>&
837 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
838 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
842 * @brief Extracts the current state of a % subtract_with_carry_engine
843 * random number generator engine @p __x from the input stream
846 * @param __is An input stream.
847 * @param __x A % subtract_with_carry_engine random number generator
850 * @returns The input stream with the state of @p __x extracted or in
853 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
854 typename _CharT
, typename _Traits
>
855 friend std::basic_istream
<_CharT
, _Traits
>&
856 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
857 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
861 /// The state of the generator. This is a ring buffer.
862 _UIntType _M_x
[long_lag
];
863 _UIntType _M_carry
; ///< The carry
864 size_t _M_p
; ///< Current index of x(i - r).
867 #if __cpp_impl_three_way_comparison < 201907L
869 * @brief Compares two % subtract_with_carry_engine random number
870 * generator objects of the same type for inequality.
872 * @param __lhs A % subtract_with_carry_engine random number generator
874 * @param __rhs Another % subtract_with_carry_engine random number
877 * @returns true if the infinite sequences of generated values
878 * would be different, false otherwise.
880 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
882 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
884 const std::subtract_with_carry_engine
<_UIntType
, __w
,
886 { return !(__lhs
== __rhs
); }
890 * Produces random numbers from some base engine by discarding blocks of
893 * 0 <= @p __r <= @p __p
895 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
896 class discard_block_engine
898 static_assert(1 <= __r
&& __r
<= __p
,
899 "template argument substituting __r out of bounds");
902 /** The type of the generated random value. */
903 typedef typename
_RandomNumberEngine::result_type result_type
;
905 template<typename _Sseq
>
907 = __detail::_If_seed_seq_for
<_Sseq
, discard_block_engine
,
911 static constexpr size_t block_size
= __p
;
912 static constexpr size_t used_block
= __r
;
915 * @brief Constructs a default %discard_block_engine engine.
917 * The underlying engine is default constructed as well.
919 discard_block_engine()
920 : _M_b(), _M_n(0) { }
923 * @brief Copy constructs a %discard_block_engine engine.
925 * Copies an existing base class random number generator.
926 * @param __rng An existing (base class) engine object.
929 discard_block_engine(const _RandomNumberEngine
& __rng
)
930 : _M_b(__rng
), _M_n(0) { }
933 * @brief Move constructs a %discard_block_engine engine.
935 * Copies an existing base class random number generator.
936 * @param __rng An existing (base class) engine object.
939 discard_block_engine(_RandomNumberEngine
&& __rng
)
940 : _M_b(std::move(__rng
)), _M_n(0) { }
943 * @brief Seed constructs a %discard_block_engine engine.
945 * Constructs the underlying generator engine seeded with @p __s.
946 * @param __s A seed value for the base class engine.
949 discard_block_engine(result_type __s
)
950 : _M_b(__s
), _M_n(0) { }
953 * @brief Generator construct a %discard_block_engine engine.
955 * @param __q A seed sequence.
957 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
959 discard_block_engine(_Sseq
& __q
)
964 * @brief Reseeds the %discard_block_engine object with the default
965 * seed for the underlying base class generator engine.
975 * @brief Reseeds the %discard_block_engine object with the default
976 * seed for the underlying base class generator engine.
979 seed(result_type __s
)
986 * @brief Reseeds the %discard_block_engine object with the given seed
988 * @param __q A seed generator function.
990 template<typename _Sseq
>
999 * @brief Gets a const reference to the underlying generator engine
1002 const _RandomNumberEngine
&
1003 base() const noexcept
1007 * @brief Gets the minimum value in the generated random number range.
1009 static constexpr result_type
1011 { return _RandomNumberEngine::min(); }
1014 * @brief Gets the maximum value in the generated random number range.
1016 static constexpr result_type
1018 { return _RandomNumberEngine::max(); }
1021 * @brief Discard a sequence of random numbers.
1024 discard(unsigned long long __z
)
1026 for (; __z
!= 0ULL; --__z
)
1031 * @brief Gets the next value in the generated random number sequence.
1037 * @brief Compares two %discard_block_engine random number generator
1038 * objects of the same type for equality.
1040 * @param __lhs A %discard_block_engine random number generator object.
1041 * @param __rhs Another %discard_block_engine random number generator
1044 * @returns true if the infinite sequences of generated values
1045 * would be equal, false otherwise.
1048 operator==(const discard_block_engine
& __lhs
,
1049 const discard_block_engine
& __rhs
)
1050 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
1053 * @brief Inserts the current state of a %discard_block_engine random
1054 * number generator engine @p __x into the output stream
1057 * @param __os An output stream.
1058 * @param __x A %discard_block_engine random number generator engine.
1060 * @returns The output stream with the state of @p __x inserted or in
1063 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1064 typename _CharT
, typename _Traits
>
1065 friend std::basic_ostream
<_CharT
, _Traits
>&
1066 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1067 const std::discard_block_engine
<_RandomNumberEngine1
,
1071 * @brief Extracts the current state of a % subtract_with_carry_engine
1072 * random number generator engine @p __x from the input stream
1075 * @param __is An input stream.
1076 * @param __x A %discard_block_engine random number generator engine.
1078 * @returns The input stream with the state of @p __x extracted or in
1081 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1082 typename _CharT
, typename _Traits
>
1083 friend std::basic_istream
<_CharT
, _Traits
>&
1084 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1085 std::discard_block_engine
<_RandomNumberEngine1
,
1089 _RandomNumberEngine _M_b
;
1093 #if __cpp_impl_three_way_comparison < 201907L
1095 * @brief Compares two %discard_block_engine random number generator
1096 * objects of the same type for inequality.
1098 * @param __lhs A %discard_block_engine random number generator object.
1099 * @param __rhs Another %discard_block_engine random number generator
1102 * @returns true if the infinite sequences of generated values
1103 * would be different, false otherwise.
1105 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
1107 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1109 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1111 { return !(__lhs
== __rhs
); }
1115 * Produces random numbers by combining random numbers from some base
1116 * engine to produce random numbers with a specified number of bits @p __w.
1118 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1119 class independent_bits_engine
1121 static_assert(std::is_unsigned
<_UIntType
>::value
,
1122 "result_type must be an unsigned integral type");
1123 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
1124 "template argument substituting __w out of bounds");
1126 template<typename _Sseq
>
1128 = __detail::_If_seed_seq_for
<_Sseq
, independent_bits_engine
,
1132 /** The type of the generated random value. */
1133 typedef _UIntType result_type
;
1136 * @brief Constructs a default %independent_bits_engine engine.
1138 * The underlying engine is default constructed as well.
1140 independent_bits_engine()
1144 * @brief Copy constructs a %independent_bits_engine engine.
1146 * Copies an existing base class random number generator.
1147 * @param __rng An existing (base class) engine object.
1150 independent_bits_engine(const _RandomNumberEngine
& __rng
)
1154 * @brief Move constructs a %independent_bits_engine engine.
1156 * Copies an existing base class random number generator.
1157 * @param __rng An existing (base class) engine object.
1160 independent_bits_engine(_RandomNumberEngine
&& __rng
)
1161 : _M_b(std::move(__rng
)) { }
1164 * @brief Seed constructs a %independent_bits_engine engine.
1166 * Constructs the underlying generator engine seeded with @p __s.
1167 * @param __s A seed value for the base class engine.
1170 independent_bits_engine(result_type __s
)
1174 * @brief Generator construct a %independent_bits_engine engine.
1176 * @param __q A seed sequence.
1178 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
1180 independent_bits_engine(_Sseq
& __q
)
1185 * @brief Reseeds the %independent_bits_engine object with the default
1186 * seed for the underlying base class generator engine.
1193 * @brief Reseeds the %independent_bits_engine object with the default
1194 * seed for the underlying base class generator engine.
1197 seed(result_type __s
)
1201 * @brief Reseeds the %independent_bits_engine object with the given
1203 * @param __q A seed generator function.
1205 template<typename _Sseq
>
1211 * @brief Gets a const reference to the underlying generator engine
1214 const _RandomNumberEngine
&
1215 base() const noexcept
1219 * @brief Gets the minimum value in the generated random number range.
1221 static constexpr result_type
1226 * @brief Gets the maximum value in the generated random number range.
1228 static constexpr result_type
1230 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1233 * @brief Discard a sequence of random numbers.
1236 discard(unsigned long long __z
)
1238 for (; __z
!= 0ULL; --__z
)
1243 * @brief Gets the next value in the generated random number sequence.
1249 * @brief Compares two %independent_bits_engine random number generator
1250 * objects of the same type for equality.
1252 * @param __lhs A %independent_bits_engine random number generator
1254 * @param __rhs Another %independent_bits_engine random number generator
1257 * @returns true if the infinite sequences of generated values
1258 * would be equal, false otherwise.
1261 operator==(const independent_bits_engine
& __lhs
,
1262 const independent_bits_engine
& __rhs
)
1263 { return __lhs
._M_b
== __rhs
._M_b
; }
1266 * @brief Extracts the current state of a % subtract_with_carry_engine
1267 * random number generator engine @p __x from the input stream
1270 * @param __is An input stream.
1271 * @param __x A %independent_bits_engine random number generator
1274 * @returns The input stream with the state of @p __x extracted or in
1277 template<typename _CharT
, typename _Traits
>
1278 friend std::basic_istream
<_CharT
, _Traits
>&
1279 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1280 std::independent_bits_engine
<_RandomNumberEngine
,
1281 __w
, _UIntType
>& __x
)
1288 _RandomNumberEngine _M_b
;
1291 #if __cpp_impl_three_way_comparison < 201907L
1293 * @brief Compares two %independent_bits_engine random number generator
1294 * objects of the same type for inequality.
1296 * @param __lhs A %independent_bits_engine random number generator
1298 * @param __rhs Another %independent_bits_engine random number generator
1301 * @returns true if the infinite sequences of generated values
1302 * would be different, false otherwise.
1304 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1306 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1308 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1310 { return !(__lhs
== __rhs
); }
1314 * @brief Inserts the current state of a %independent_bits_engine random
1315 * number generator engine @p __x into the output stream @p __os.
1317 * @param __os An output stream.
1318 * @param __x A %independent_bits_engine random number generator engine.
1320 * @returns The output stream with the state of @p __x inserted or in
1323 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1324 typename _CharT
, typename _Traits
>
1325 std::basic_ostream
<_CharT
, _Traits
>&
1326 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1327 const std::independent_bits_engine
<_RandomNumberEngine
,
1328 __w
, _UIntType
>& __x
)
1336 * @brief Produces random numbers by reordering random numbers from some
1339 * The values from the base engine are stored in a sequence of size @p __k
1340 * and shuffled by an algorithm that depends on those values.
1342 template<typename _RandomNumberEngine
, size_t __k
>
1343 class shuffle_order_engine
1345 static_assert(1u <= __k
, "template argument substituting "
1346 "__k out of bound");
1349 /** The type of the generated random value. */
1350 typedef typename
_RandomNumberEngine::result_type result_type
;
1352 template<typename _Sseq
>
1354 = __detail::_If_seed_seq_for
<_Sseq
, shuffle_order_engine
,
1357 static constexpr size_t table_size
= __k
;
1360 * @brief Constructs a default %shuffle_order_engine engine.
1362 * The underlying engine is default constructed as well.
1364 shuffle_order_engine()
1366 { _M_initialize(); }
1369 * @brief Copy constructs a %shuffle_order_engine engine.
1371 * Copies an existing base class random number generator.
1372 * @param __rng An existing (base class) engine object.
1375 shuffle_order_engine(const _RandomNumberEngine
& __rng
)
1377 { _M_initialize(); }
1380 * @brief Move constructs a %shuffle_order_engine engine.
1382 * Copies an existing base class random number generator.
1383 * @param __rng An existing (base class) engine object.
1386 shuffle_order_engine(_RandomNumberEngine
&& __rng
)
1387 : _M_b(std::move(__rng
))
1388 { _M_initialize(); }
1391 * @brief Seed constructs a %shuffle_order_engine engine.
1393 * Constructs the underlying generator engine seeded with @p __s.
1394 * @param __s A seed value for the base class engine.
1397 shuffle_order_engine(result_type __s
)
1399 { _M_initialize(); }
1402 * @brief Generator construct a %shuffle_order_engine engine.
1404 * @param __q A seed sequence.
1406 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
1408 shuffle_order_engine(_Sseq
& __q
)
1410 { _M_initialize(); }
1413 * @brief Reseeds the %shuffle_order_engine object with the default seed
1414 for the underlying base class generator engine.
1424 * @brief Reseeds the %shuffle_order_engine object with the default seed
1425 * for the underlying base class generator engine.
1428 seed(result_type __s
)
1435 * @brief Reseeds the %shuffle_order_engine object with the given seed
1437 * @param __q A seed generator function.
1439 template<typename _Sseq
>
1448 * Gets a const reference to the underlying generator engine object.
1450 const _RandomNumberEngine
&
1451 base() const noexcept
1455 * Gets the minimum value in the generated random number range.
1457 static constexpr result_type
1459 { return _RandomNumberEngine::min(); }
1462 * Gets the maximum value in the generated random number range.
1464 static constexpr result_type
1466 { return _RandomNumberEngine::max(); }
1469 * Discard a sequence of random numbers.
1472 discard(unsigned long long __z
)
1474 for (; __z
!= 0ULL; --__z
)
1479 * Gets the next value in the generated random number sequence.
1485 * Compares two %shuffle_order_engine random number generator objects
1486 * of the same type for equality.
1488 * @param __lhs A %shuffle_order_engine random number generator object.
1489 * @param __rhs Another %shuffle_order_engine random number generator
1492 * @returns true if the infinite sequences of generated values
1493 * would be equal, false otherwise.
1496 operator==(const shuffle_order_engine
& __lhs
,
1497 const shuffle_order_engine
& __rhs
)
1498 { return (__lhs
._M_b
== __rhs
._M_b
1499 && std::equal(__lhs
._M_v
, __lhs
._M_v
+ __k
, __rhs
._M_v
)
1500 && __lhs
._M_y
== __rhs
._M_y
); }
1503 * @brief Inserts the current state of a %shuffle_order_engine random
1504 * number generator engine @p __x into the output stream
1507 * @param __os An output stream.
1508 * @param __x A %shuffle_order_engine random number generator engine.
1510 * @returns The output stream with the state of @p __x inserted or in
1513 template<typename _RandomNumberEngine1
, size_t __k1
,
1514 typename _CharT
, typename _Traits
>
1515 friend std::basic_ostream
<_CharT
, _Traits
>&
1516 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1517 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1521 * @brief Extracts the current state of a % subtract_with_carry_engine
1522 * random number generator engine @p __x from the input stream
1525 * @param __is An input stream.
1526 * @param __x A %shuffle_order_engine random number generator engine.
1528 * @returns The input stream with the state of @p __x extracted or in
1531 template<typename _RandomNumberEngine1
, size_t __k1
,
1532 typename _CharT
, typename _Traits
>
1533 friend std::basic_istream
<_CharT
, _Traits
>&
1534 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1535 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>& __x
);
1538 void _M_initialize()
1540 for (size_t __i
= 0; __i
< __k
; ++__i
)
1545 _RandomNumberEngine _M_b
;
1546 result_type _M_v
[__k
];
1550 #if __cpp_impl_three_way_comparison < 201907L
1552 * Compares two %shuffle_order_engine random number generator objects
1553 * of the same type for inequality.
1555 * @param __lhs A %shuffle_order_engine random number generator object.
1556 * @param __rhs Another %shuffle_order_engine random number generator
1559 * @returns true if the infinite sequences of generated values
1560 * would be different, false otherwise.
1562 template<typename _RandomNumberEngine
, size_t __k
>
1564 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1566 const std::shuffle_order_engine
<_RandomNumberEngine
,
1568 { return !(__lhs
== __rhs
); }
1572 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1574 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1578 * An alternative LCR (Lehmer Generator function).
1580 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1584 * The classic Mersenne Twister.
1587 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1588 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1589 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1591 typedef mersenne_twister_engine
<
1597 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1600 * An alternative Mersenne Twister.
1602 typedef mersenne_twister_engine
<
1605 0xb5026f5aa96619e9ULL
, 29,
1606 0x5555555555555555ULL
, 17,
1607 0x71d67fffeda60000ULL
, 37,
1608 0xfff7eee000000000ULL
, 43,
1609 6364136223846793005ULL> mt19937_64
;
1611 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1614 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1617 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1619 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1621 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1623 typedef minstd_rand0 default_random_engine
;
1626 * A standard interface to a platform-specific non-deterministic
1627 * random number generator (if any are available).
1632 /** The type of the generated random value. */
1633 typedef unsigned int result_type
;
1635 // constructors, destructors and member functions
1637 random_device() { _M_init("default"); }
1640 random_device(const std::string
& __token
) { _M_init(__token
); }
1642 #if defined _GLIBCXX_USE_DEV_RANDOM
1647 static constexpr result_type
1649 { return std::numeric_limits
<result_type
>::min(); }
1651 static constexpr result_type
1653 { return std::numeric_limits
<result_type
>::max(); }
1656 entropy() const noexcept
1658 #ifdef _GLIBCXX_USE_DEV_RANDOM
1659 return this->_M_getentropy();
1667 { return this->_M_getval(); }
1669 // No copy functions.
1670 random_device(const random_device
&) = delete;
1671 void operator=(const random_device
&) = delete;
1675 void _M_init(const std::string
& __token
);
1676 void _M_init_pretr1(const std::string
& __token
);
1679 result_type
_M_getval();
1680 result_type
_M_getval_pretr1();
1681 double _M_getentropy() const noexcept
;
1683 void _M_init(const char*, size_t); // not exported from the shared library
1690 result_type (*_M_func
)(void*);
1697 /// @} group random_generators
1700 * @addtogroup random_distributions Random Number Distributions
1706 * @addtogroup random_distributions_uniform Uniform Distributions
1707 * @ingroup random_distributions
1711 // std::uniform_int_distribution is defined in <bits/uniform_int_dist.h>
1713 #if __cpp_impl_three_way_comparison < 201907L
1715 * @brief Return true if two uniform integer distributions have
1716 * different parameters.
1718 template<typename _IntType
>
1720 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1721 const std::uniform_int_distribution
<_IntType
>& __d2
)
1722 { return !(__d1
== __d2
); }
1726 * @brief Inserts a %uniform_int_distribution random number
1727 * distribution @p __x into the output stream @p os.
1729 * @param __os An output stream.
1730 * @param __x A %uniform_int_distribution random number distribution.
1732 * @returns The output stream with the state of @p __x inserted or in
1735 template<typename _IntType
, typename _CharT
, typename _Traits
>
1736 std::basic_ostream
<_CharT
, _Traits
>&
1737 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1738 const std::uniform_int_distribution
<_IntType
>&);
1741 * @brief Extracts a %uniform_int_distribution random number distribution
1742 * @p __x from the input stream @p __is.
1744 * @param __is An input stream.
1745 * @param __x A %uniform_int_distribution random number generator engine.
1747 * @returns The input stream with @p __x extracted or in an error state.
1749 template<typename _IntType
, typename _CharT
, typename _Traits
>
1750 std::basic_istream
<_CharT
, _Traits
>&
1751 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1752 std::uniform_int_distribution
<_IntType
>&);
1756 * @brief Uniform continuous distribution for random numbers.
1758 * A continuous random distribution on the range [min, max) with equal
1759 * probability throughout the range. The URNG should be real-valued and
1760 * deliver number in the range [0, 1).
1762 template<typename _RealType
= double>
1763 class uniform_real_distribution
1765 static_assert(std::is_floating_point
<_RealType
>::value
,
1766 "result_type must be a floating point type");
1769 /** The type of the range of the distribution. */
1770 typedef _RealType result_type
;
1772 /** Parameter type. */
1775 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1777 param_type() : param_type(0) { }
1780 param_type(_RealType __a
, _RealType __b
= _RealType(1))
1781 : _M_a(__a
), _M_b(__b
)
1783 __glibcxx_assert(_M_a
<= _M_b
);
1795 operator==(const param_type
& __p1
, const param_type
& __p2
)
1796 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1798 #if __cpp_impl_three_way_comparison < 201907L
1800 operator!=(const param_type
& __p1
, const param_type
& __p2
)
1801 { return !(__p1
== __p2
); }
1811 * @brief Constructs a uniform_real_distribution object.
1813 * The lower bound is set to 0.0 and the upper bound to 1.0
1815 uniform_real_distribution() : uniform_real_distribution(0.0) { }
1818 * @brief Constructs a uniform_real_distribution object.
1820 * @param __a [IN] The lower bound of the distribution.
1821 * @param __b [IN] The upper bound of the distribution.
1824 uniform_real_distribution(_RealType __a
, _RealType __b
= _RealType(1))
1825 : _M_param(__a
, __b
)
1829 uniform_real_distribution(const param_type
& __p
)
1834 * @brief Resets the distribution state.
1836 * Does nothing for the uniform real distribution.
1843 { return _M_param
.a(); }
1847 { return _M_param
.b(); }
1850 * @brief Returns the parameter set of the distribution.
1854 { return _M_param
; }
1857 * @brief Sets the parameter set of the distribution.
1858 * @param __param The new parameter set of the distribution.
1861 param(const param_type
& __param
)
1862 { _M_param
= __param
; }
1865 * @brief Returns the inclusive lower bound of the distribution range.
1869 { return this->a(); }
1872 * @brief Returns the inclusive upper bound of the distribution range.
1876 { return this->b(); }
1879 * @brief Generating functions.
1881 template<typename _UniformRandomNumberGenerator
>
1883 operator()(_UniformRandomNumberGenerator
& __urng
)
1884 { return this->operator()(__urng
, _M_param
); }
1886 template<typename _UniformRandomNumberGenerator
>
1888 operator()(_UniformRandomNumberGenerator
& __urng
,
1889 const param_type
& __p
)
1891 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1893 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1896 template<typename _ForwardIterator
,
1897 typename _UniformRandomNumberGenerator
>
1899 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1900 _UniformRandomNumberGenerator
& __urng
)
1901 { this->__generate(__f
, __t
, __urng
, _M_param
); }
1903 template<typename _ForwardIterator
,
1904 typename _UniformRandomNumberGenerator
>
1906 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1907 _UniformRandomNumberGenerator
& __urng
,
1908 const param_type
& __p
)
1909 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1911 template<typename _UniformRandomNumberGenerator
>
1913 __generate(result_type
* __f
, result_type
* __t
,
1914 _UniformRandomNumberGenerator
& __urng
,
1915 const param_type
& __p
)
1916 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1919 * @brief Return true if two uniform real distributions have
1920 * the same parameters.
1923 operator==(const uniform_real_distribution
& __d1
,
1924 const uniform_real_distribution
& __d2
)
1925 { return __d1
._M_param
== __d2
._M_param
; }
1928 template<typename _ForwardIterator
,
1929 typename _UniformRandomNumberGenerator
>
1931 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
1932 _UniformRandomNumberGenerator
& __urng
,
1933 const param_type
& __p
);
1935 param_type _M_param
;
1938 #if __cpp_impl_three_way_comparison < 201907L
1940 * @brief Return true if two uniform real distributions have
1941 * different parameters.
1943 template<typename _IntType
>
1945 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1946 const std::uniform_real_distribution
<_IntType
>& __d2
)
1947 { return !(__d1
== __d2
); }
1951 * @brief Inserts a %uniform_real_distribution random number
1952 * distribution @p __x into the output stream @p __os.
1954 * @param __os An output stream.
1955 * @param __x A %uniform_real_distribution random number distribution.
1957 * @returns The output stream with the state of @p __x inserted or in
1960 template<typename _RealType
, typename _CharT
, typename _Traits
>
1961 std::basic_ostream
<_CharT
, _Traits
>&
1962 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1963 const std::uniform_real_distribution
<_RealType
>&);
1966 * @brief Extracts a %uniform_real_distribution random number distribution
1967 * @p __x from the input stream @p __is.
1969 * @param __is An input stream.
1970 * @param __x A %uniform_real_distribution random number generator engine.
1972 * @returns The input stream with @p __x extracted or in an error state.
1974 template<typename _RealType
, typename _CharT
, typename _Traits
>
1975 std::basic_istream
<_CharT
, _Traits
>&
1976 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1977 std::uniform_real_distribution
<_RealType
>&);
1979 /// @} group random_distributions_uniform
1982 * @addtogroup random_distributions_normal Normal Distributions
1983 * @ingroup random_distributions
1988 * @brief A normal continuous distribution for random numbers.
1990 * The formula for the normal probability density function is
1992 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1993 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1996 template<typename _RealType
= double>
1997 class normal_distribution
1999 static_assert(std::is_floating_point
<_RealType
>::value
,
2000 "result_type must be a floating point type");
2003 /** The type of the range of the distribution. */
2004 typedef _RealType result_type
;
2006 /** Parameter type. */
2009 typedef normal_distribution
<_RealType
> distribution_type
;
2011 param_type() : param_type(0.0) { }
2014 param_type(_RealType __mean
, _RealType __stddev
= _RealType(1))
2015 : _M_mean(__mean
), _M_stddev(__stddev
)
2017 __glibcxx_assert(_M_stddev
> _RealType(0));
2026 { return _M_stddev
; }
2029 operator==(const param_type
& __p1
, const param_type
& __p2
)
2030 { return (__p1
._M_mean
== __p2
._M_mean
2031 && __p1
._M_stddev
== __p2
._M_stddev
); }
2033 #if __cpp_impl_three_way_comparison < 201907L
2035 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2036 { return !(__p1
== __p2
); }
2041 _RealType _M_stddev
;
2045 normal_distribution() : normal_distribution(0.0) { }
2048 * Constructs a normal distribution with parameters @f$mean@f$ and
2049 * standard deviation.
2052 normal_distribution(result_type __mean
,
2053 result_type __stddev
= result_type(1))
2054 : _M_param(__mean
, __stddev
)
2058 normal_distribution(const param_type
& __p
)
2063 * @brief Resets the distribution state.
2067 { _M_saved_available
= false; }
2070 * @brief Returns the mean of the distribution.
2074 { return _M_param
.mean(); }
2077 * @brief Returns the standard deviation of the distribution.
2081 { return _M_param
.stddev(); }
2084 * @brief Returns the parameter set of the distribution.
2088 { return _M_param
; }
2091 * @brief Sets the parameter set of the distribution.
2092 * @param __param The new parameter set of the distribution.
2095 param(const param_type
& __param
)
2096 { _M_param
= __param
; }
2099 * @brief Returns the greatest lower bound value of the distribution.
2103 { return std::numeric_limits
<result_type
>::lowest(); }
2106 * @brief Returns the least upper bound value of the distribution.
2110 { return std::numeric_limits
<result_type
>::max(); }
2113 * @brief Generating functions.
2115 template<typename _UniformRandomNumberGenerator
>
2117 operator()(_UniformRandomNumberGenerator
& __urng
)
2118 { return this->operator()(__urng
, _M_param
); }
2120 template<typename _UniformRandomNumberGenerator
>
2122 operator()(_UniformRandomNumberGenerator
& __urng
,
2123 const param_type
& __p
);
2125 template<typename _ForwardIterator
,
2126 typename _UniformRandomNumberGenerator
>
2128 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2129 _UniformRandomNumberGenerator
& __urng
)
2130 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2132 template<typename _ForwardIterator
,
2133 typename _UniformRandomNumberGenerator
>
2135 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2136 _UniformRandomNumberGenerator
& __urng
,
2137 const param_type
& __p
)
2138 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2140 template<typename _UniformRandomNumberGenerator
>
2142 __generate(result_type
* __f
, result_type
* __t
,
2143 _UniformRandomNumberGenerator
& __urng
,
2144 const param_type
& __p
)
2145 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2148 * @brief Return true if two normal distributions have
2149 * the same parameters and the sequences that would
2150 * be generated are equal.
2152 template<typename _RealType1
>
2154 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2155 const std::normal_distribution
<_RealType1
>& __d2
);
2158 * @brief Inserts a %normal_distribution random number distribution
2159 * @p __x into the output stream @p __os.
2161 * @param __os An output stream.
2162 * @param __x A %normal_distribution random number distribution.
2164 * @returns The output stream with the state of @p __x inserted or in
2167 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2168 friend std::basic_ostream
<_CharT
, _Traits
>&
2169 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2170 const std::normal_distribution
<_RealType1
>& __x
);
2173 * @brief Extracts a %normal_distribution random number distribution
2174 * @p __x from the input stream @p __is.
2176 * @param __is An input stream.
2177 * @param __x A %normal_distribution random number generator engine.
2179 * @returns The input stream with @p __x extracted or in an error
2182 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2183 friend std::basic_istream
<_CharT
, _Traits
>&
2184 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2185 std::normal_distribution
<_RealType1
>& __x
);
2188 template<typename _ForwardIterator
,
2189 typename _UniformRandomNumberGenerator
>
2191 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2192 _UniformRandomNumberGenerator
& __urng
,
2193 const param_type
& __p
);
2195 param_type _M_param
;
2196 result_type _M_saved
= 0;
2197 bool _M_saved_available
= false;
2200 #if __cpp_impl_three_way_comparison < 201907L
2202 * @brief Return true if two normal distributions are different.
2204 template<typename _RealType
>
2206 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2207 const std::normal_distribution
<_RealType
>& __d2
)
2208 { return !(__d1
== __d2
); }
2212 * @brief A lognormal_distribution random number distribution.
2214 * The formula for the normal probability mass function is
2216 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2217 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2220 template<typename _RealType
= double>
2221 class lognormal_distribution
2223 static_assert(std::is_floating_point
<_RealType
>::value
,
2224 "result_type must be a floating point type");
2227 /** The type of the range of the distribution. */
2228 typedef _RealType result_type
;
2230 /** Parameter type. */
2233 typedef lognormal_distribution
<_RealType
> distribution_type
;
2235 param_type() : param_type(0.0) { }
2238 param_type(_RealType __m
, _RealType __s
= _RealType(1))
2239 : _M_m(__m
), _M_s(__s
)
2251 operator==(const param_type
& __p1
, const param_type
& __p2
)
2252 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2254 #if __cpp_impl_three_way_comparison < 201907L
2256 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2257 { return !(__p1
== __p2
); }
2265 lognormal_distribution() : lognormal_distribution(0.0) { }
2268 lognormal_distribution(_RealType __m
, _RealType __s
= _RealType(1))
2269 : _M_param(__m
, __s
), _M_nd()
2273 lognormal_distribution(const param_type
& __p
)
2274 : _M_param(__p
), _M_nd()
2278 * Resets the distribution state.
2289 { return _M_param
.m(); }
2293 { return _M_param
.s(); }
2296 * @brief Returns the parameter set of the distribution.
2300 { return _M_param
; }
2303 * @brief Sets the parameter set of the distribution.
2304 * @param __param The new parameter set of the distribution.
2307 param(const param_type
& __param
)
2308 { _M_param
= __param
; }
2311 * @brief Returns the greatest lower bound value of the distribution.
2315 { return result_type(0); }
2318 * @brief Returns the least upper bound value of the distribution.
2322 { return std::numeric_limits
<result_type
>::max(); }
2325 * @brief Generating functions.
2327 template<typename _UniformRandomNumberGenerator
>
2329 operator()(_UniformRandomNumberGenerator
& __urng
)
2330 { return this->operator()(__urng
, _M_param
); }
2332 template<typename _UniformRandomNumberGenerator
>
2334 operator()(_UniformRandomNumberGenerator
& __urng
,
2335 const param_type
& __p
)
2336 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2338 template<typename _ForwardIterator
,
2339 typename _UniformRandomNumberGenerator
>
2341 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2342 _UniformRandomNumberGenerator
& __urng
)
2343 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2345 template<typename _ForwardIterator
,
2346 typename _UniformRandomNumberGenerator
>
2348 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2349 _UniformRandomNumberGenerator
& __urng
,
2350 const param_type
& __p
)
2351 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2353 template<typename _UniformRandomNumberGenerator
>
2355 __generate(result_type
* __f
, result_type
* __t
,
2356 _UniformRandomNumberGenerator
& __urng
,
2357 const param_type
& __p
)
2358 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2361 * @brief Return true if two lognormal distributions have
2362 * the same parameters and the sequences that would
2363 * be generated are equal.
2366 operator==(const lognormal_distribution
& __d1
,
2367 const lognormal_distribution
& __d2
)
2368 { return (__d1
._M_param
== __d2
._M_param
2369 && __d1
._M_nd
== __d2
._M_nd
); }
2372 * @brief Inserts a %lognormal_distribution random number distribution
2373 * @p __x into the output stream @p __os.
2375 * @param __os An output stream.
2376 * @param __x A %lognormal_distribution random number distribution.
2378 * @returns The output stream with the state of @p __x inserted or in
2381 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2382 friend std::basic_ostream
<_CharT
, _Traits
>&
2383 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2384 const std::lognormal_distribution
<_RealType1
>& __x
);
2387 * @brief Extracts a %lognormal_distribution random number distribution
2388 * @p __x from the input stream @p __is.
2390 * @param __is An input stream.
2391 * @param __x A %lognormal_distribution random number
2394 * @returns The input stream with @p __x extracted or in an error state.
2396 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2397 friend std::basic_istream
<_CharT
, _Traits
>&
2398 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2399 std::lognormal_distribution
<_RealType1
>& __x
);
2402 template<typename _ForwardIterator
,
2403 typename _UniformRandomNumberGenerator
>
2405 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2406 _UniformRandomNumberGenerator
& __urng
,
2407 const param_type
& __p
);
2409 param_type _M_param
;
2411 std::normal_distribution
<result_type
> _M_nd
;
2414 #if __cpp_impl_three_way_comparison < 201907L
2416 * @brief Return true if two lognormal distributions are different.
2418 template<typename _RealType
>
2420 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2421 const std::lognormal_distribution
<_RealType
>& __d2
)
2422 { return !(__d1
== __d2
); }
2426 * @brief A gamma continuous distribution for random numbers.
2428 * The formula for the gamma probability density function is:
2430 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2431 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2434 template<typename _RealType
= double>
2435 class gamma_distribution
2437 static_assert(std::is_floating_point
<_RealType
>::value
,
2438 "result_type must be a floating point type");
2441 /** The type of the range of the distribution. */
2442 typedef _RealType result_type
;
2444 /** Parameter type. */
2447 typedef gamma_distribution
<_RealType
> distribution_type
;
2448 friend class gamma_distribution
<_RealType
>;
2450 param_type() : param_type(1.0) { }
2453 param_type(_RealType __alpha_val
, _RealType __beta_val
= _RealType(1))
2454 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2456 __glibcxx_assert(_M_alpha
> _RealType(0));
2462 { return _M_alpha
; }
2469 operator==(const param_type
& __p1
, const param_type
& __p2
)
2470 { return (__p1
._M_alpha
== __p2
._M_alpha
2471 && __p1
._M_beta
== __p2
._M_beta
); }
2473 #if __cpp_impl_three_way_comparison < 201907L
2475 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2476 { return !(__p1
== __p2
); }
2486 _RealType _M_malpha
, _M_a2
;
2491 * @brief Constructs a gamma distribution with parameters 1 and 1.
2493 gamma_distribution() : gamma_distribution(1.0) { }
2496 * @brief Constructs a gamma distribution with parameters
2497 * @f$\alpha@f$ and @f$\beta@f$.
2500 gamma_distribution(_RealType __alpha_val
,
2501 _RealType __beta_val
= _RealType(1))
2502 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2506 gamma_distribution(const param_type
& __p
)
2507 : _M_param(__p
), _M_nd()
2511 * @brief Resets the distribution state.
2518 * @brief Returns the @f$\alpha@f$ of the distribution.
2522 { return _M_param
.alpha(); }
2525 * @brief Returns the @f$\beta@f$ of the distribution.
2529 { return _M_param
.beta(); }
2532 * @brief Returns the parameter set of the distribution.
2536 { return _M_param
; }
2539 * @brief Sets the parameter set of the distribution.
2540 * @param __param The new parameter set of the distribution.
2543 param(const param_type
& __param
)
2544 { _M_param
= __param
; }
2547 * @brief Returns the greatest lower bound value of the distribution.
2551 { return result_type(0); }
2554 * @brief Returns the least upper bound value of the distribution.
2558 { return std::numeric_limits
<result_type
>::max(); }
2561 * @brief Generating functions.
2563 template<typename _UniformRandomNumberGenerator
>
2565 operator()(_UniformRandomNumberGenerator
& __urng
)
2566 { return this->operator()(__urng
, _M_param
); }
2568 template<typename _UniformRandomNumberGenerator
>
2570 operator()(_UniformRandomNumberGenerator
& __urng
,
2571 const param_type
& __p
);
2573 template<typename _ForwardIterator
,
2574 typename _UniformRandomNumberGenerator
>
2576 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2577 _UniformRandomNumberGenerator
& __urng
)
2578 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2580 template<typename _ForwardIterator
,
2581 typename _UniformRandomNumberGenerator
>
2583 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2584 _UniformRandomNumberGenerator
& __urng
,
2585 const param_type
& __p
)
2586 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2588 template<typename _UniformRandomNumberGenerator
>
2590 __generate(result_type
* __f
, result_type
* __t
,
2591 _UniformRandomNumberGenerator
& __urng
,
2592 const param_type
& __p
)
2593 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2596 * @brief Return true if two gamma distributions have the same
2597 * parameters and the sequences that would be generated
2601 operator==(const gamma_distribution
& __d1
,
2602 const gamma_distribution
& __d2
)
2603 { return (__d1
._M_param
== __d2
._M_param
2604 && __d1
._M_nd
== __d2
._M_nd
); }
2607 * @brief Inserts a %gamma_distribution random number distribution
2608 * @p __x into the output stream @p __os.
2610 * @param __os An output stream.
2611 * @param __x A %gamma_distribution random number distribution.
2613 * @returns The output stream with the state of @p __x inserted or in
2616 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2617 friend std::basic_ostream
<_CharT
, _Traits
>&
2618 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2619 const std::gamma_distribution
<_RealType1
>& __x
);
2622 * @brief Extracts a %gamma_distribution random number distribution
2623 * @p __x from the input stream @p __is.
2625 * @param __is An input stream.
2626 * @param __x A %gamma_distribution random number generator engine.
2628 * @returns The input stream with @p __x extracted or in an error state.
2630 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2631 friend std::basic_istream
<_CharT
, _Traits
>&
2632 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2633 std::gamma_distribution
<_RealType1
>& __x
);
2636 template<typename _ForwardIterator
,
2637 typename _UniformRandomNumberGenerator
>
2639 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2640 _UniformRandomNumberGenerator
& __urng
,
2641 const param_type
& __p
);
2643 param_type _M_param
;
2645 std::normal_distribution
<result_type
> _M_nd
;
2648 #if __cpp_impl_three_way_comparison < 201907L
2650 * @brief Return true if two gamma distributions are different.
2652 template<typename _RealType
>
2654 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2655 const std::gamma_distribution
<_RealType
>& __d2
)
2656 { return !(__d1
== __d2
); }
2660 * @brief A chi_squared_distribution random number distribution.
2662 * The formula for the normal probability mass function is
2663 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2665 template<typename _RealType
= double>
2666 class chi_squared_distribution
2668 static_assert(std::is_floating_point
<_RealType
>::value
,
2669 "result_type must be a floating point type");
2672 /** The type of the range of the distribution. */
2673 typedef _RealType result_type
;
2675 /** Parameter type. */
2678 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2680 param_type() : param_type(1) { }
2683 param_type(_RealType __n
)
2692 operator==(const param_type
& __p1
, const param_type
& __p2
)
2693 { return __p1
._M_n
== __p2
._M_n
; }
2695 #if __cpp_impl_three_way_comparison < 201907L
2697 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2698 { return !(__p1
== __p2
); }
2705 chi_squared_distribution() : chi_squared_distribution(1) { }
2708 chi_squared_distribution(_RealType __n
)
2709 : _M_param(__n
), _M_gd(__n
/ 2)
2713 chi_squared_distribution(const param_type
& __p
)
2714 : _M_param(__p
), _M_gd(__p
.n() / 2)
2718 * @brief Resets the distribution state.
2729 { return _M_param
.n(); }
2732 * @brief Returns the parameter set of the distribution.
2736 { return _M_param
; }
2739 * @brief Sets the parameter set of the distribution.
2740 * @param __param The new parameter set of the distribution.
2743 param(const param_type
& __param
)
2746 typedef typename
std::gamma_distribution
<result_type
>::param_type
2748 _M_gd
.param(param_type
{__param
.n() / 2});
2752 * @brief Returns the greatest lower bound value of the distribution.
2756 { return result_type(0); }
2759 * @brief Returns the least upper bound value of the distribution.
2763 { return std::numeric_limits
<result_type
>::max(); }
2766 * @brief Generating functions.
2768 template<typename _UniformRandomNumberGenerator
>
2770 operator()(_UniformRandomNumberGenerator
& __urng
)
2771 { return 2 * _M_gd(__urng
); }
2773 template<typename _UniformRandomNumberGenerator
>
2775 operator()(_UniformRandomNumberGenerator
& __urng
,
2776 const param_type
& __p
)
2778 typedef typename
std::gamma_distribution
<result_type
>::param_type
2780 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2783 template<typename _ForwardIterator
,
2784 typename _UniformRandomNumberGenerator
>
2786 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2787 _UniformRandomNumberGenerator
& __urng
)
2788 { this->__generate_impl(__f
, __t
, __urng
); }
2790 template<typename _ForwardIterator
,
2791 typename _UniformRandomNumberGenerator
>
2793 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2794 _UniformRandomNumberGenerator
& __urng
,
2795 const param_type
& __p
)
2796 { typename
std::gamma_distribution
<result_type
>::param_type
2798 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2800 template<typename _UniformRandomNumberGenerator
>
2802 __generate(result_type
* __f
, result_type
* __t
,
2803 _UniformRandomNumberGenerator
& __urng
)
2804 { this->__generate_impl(__f
, __t
, __urng
); }
2806 template<typename _UniformRandomNumberGenerator
>
2808 __generate(result_type
* __f
, result_type
* __t
,
2809 _UniformRandomNumberGenerator
& __urng
,
2810 const param_type
& __p
)
2811 { typename
std::gamma_distribution
<result_type
>::param_type
2813 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2816 * @brief Return true if two Chi-squared distributions have
2817 * the same parameters and the sequences that would be
2818 * generated are equal.
2821 operator==(const chi_squared_distribution
& __d1
,
2822 const chi_squared_distribution
& __d2
)
2823 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
2826 * @brief Inserts a %chi_squared_distribution random number distribution
2827 * @p __x into the output stream @p __os.
2829 * @param __os An output stream.
2830 * @param __x A %chi_squared_distribution random number distribution.
2832 * @returns The output stream with the state of @p __x inserted or in
2835 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2836 friend std::basic_ostream
<_CharT
, _Traits
>&
2837 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2838 const std::chi_squared_distribution
<_RealType1
>& __x
);
2841 * @brief Extracts a %chi_squared_distribution random number distribution
2842 * @p __x from the input stream @p __is.
2844 * @param __is An input stream.
2845 * @param __x A %chi_squared_distribution random number
2848 * @returns The input stream with @p __x extracted or in an error state.
2850 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2851 friend std::basic_istream
<_CharT
, _Traits
>&
2852 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2853 std::chi_squared_distribution
<_RealType1
>& __x
);
2856 template<typename _ForwardIterator
,
2857 typename _UniformRandomNumberGenerator
>
2859 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2860 _UniformRandomNumberGenerator
& __urng
);
2862 template<typename _ForwardIterator
,
2863 typename _UniformRandomNumberGenerator
>
2865 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2866 _UniformRandomNumberGenerator
& __urng
,
2868 std::gamma_distribution
<result_type
>::param_type
& __p
);
2870 param_type _M_param
;
2872 std::gamma_distribution
<result_type
> _M_gd
;
2875 #if __cpp_impl_three_way_comparison < 201907L
2877 * @brief Return true if two Chi-squared distributions are different.
2879 template<typename _RealType
>
2881 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2882 const std::chi_squared_distribution
<_RealType
>& __d2
)
2883 { return !(__d1
== __d2
); }
2887 * @brief A cauchy_distribution random number distribution.
2889 * The formula for the normal probability mass function is
2890 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2892 template<typename _RealType
= double>
2893 class cauchy_distribution
2895 static_assert(std::is_floating_point
<_RealType
>::value
,
2896 "result_type must be a floating point type");
2899 /** The type of the range of the distribution. */
2900 typedef _RealType result_type
;
2902 /** Parameter type. */
2905 typedef cauchy_distribution
<_RealType
> distribution_type
;
2907 param_type() : param_type(0) { }
2910 param_type(_RealType __a
, _RealType __b
= _RealType(1))
2911 : _M_a(__a
), _M_b(__b
)
2923 operator==(const param_type
& __p1
, const param_type
& __p2
)
2924 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2926 #if __cpp_impl_three_way_comparison < 201907L
2928 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2929 { return !(__p1
== __p2
); }
2937 cauchy_distribution() : cauchy_distribution(0.0) { }
2940 cauchy_distribution(_RealType __a
, _RealType __b
= 1.0)
2941 : _M_param(__a
, __b
)
2945 cauchy_distribution(const param_type
& __p
)
2950 * @brief Resets the distribution state.
2961 { return _M_param
.a(); }
2965 { return _M_param
.b(); }
2968 * @brief Returns the parameter set of the distribution.
2972 { return _M_param
; }
2975 * @brief Sets the parameter set of the distribution.
2976 * @param __param The new parameter set of the distribution.
2979 param(const param_type
& __param
)
2980 { _M_param
= __param
; }
2983 * @brief Returns the greatest lower bound value of the distribution.
2987 { return std::numeric_limits
<result_type
>::lowest(); }
2990 * @brief Returns the least upper bound value of the distribution.
2994 { return std::numeric_limits
<result_type
>::max(); }
2997 * @brief Generating functions.
2999 template<typename _UniformRandomNumberGenerator
>
3001 operator()(_UniformRandomNumberGenerator
& __urng
)
3002 { return this->operator()(__urng
, _M_param
); }
3004 template<typename _UniformRandomNumberGenerator
>
3006 operator()(_UniformRandomNumberGenerator
& __urng
,
3007 const param_type
& __p
);
3009 template<typename _ForwardIterator
,
3010 typename _UniformRandomNumberGenerator
>
3012 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3013 _UniformRandomNumberGenerator
& __urng
)
3014 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3016 template<typename _ForwardIterator
,
3017 typename _UniformRandomNumberGenerator
>
3019 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3020 _UniformRandomNumberGenerator
& __urng
,
3021 const param_type
& __p
)
3022 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3024 template<typename _UniformRandomNumberGenerator
>
3026 __generate(result_type
* __f
, result_type
* __t
,
3027 _UniformRandomNumberGenerator
& __urng
,
3028 const param_type
& __p
)
3029 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3032 * @brief Return true if two Cauchy distributions have
3033 * the same parameters.
3036 operator==(const cauchy_distribution
& __d1
,
3037 const cauchy_distribution
& __d2
)
3038 { return __d1
._M_param
== __d2
._M_param
; }
3041 template<typename _ForwardIterator
,
3042 typename _UniformRandomNumberGenerator
>
3044 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3045 _UniformRandomNumberGenerator
& __urng
,
3046 const param_type
& __p
);
3048 param_type _M_param
;
3051 #if __cpp_impl_three_way_comparison < 201907L
3053 * @brief Return true if two Cauchy distributions have
3054 * different parameters.
3056 template<typename _RealType
>
3058 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
3059 const std::cauchy_distribution
<_RealType
>& __d2
)
3060 { return !(__d1
== __d2
); }
3064 * @brief Inserts a %cauchy_distribution random number distribution
3065 * @p __x into the output stream @p __os.
3067 * @param __os An output stream.
3068 * @param __x A %cauchy_distribution random number distribution.
3070 * @returns The output stream with the state of @p __x inserted or in
3073 template<typename _RealType
, typename _CharT
, typename _Traits
>
3074 std::basic_ostream
<_CharT
, _Traits
>&
3075 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3076 const std::cauchy_distribution
<_RealType
>& __x
);
3079 * @brief Extracts a %cauchy_distribution random number distribution
3080 * @p __x from the input stream @p __is.
3082 * @param __is An input stream.
3083 * @param __x A %cauchy_distribution random number
3086 * @returns The input stream with @p __x extracted or in an error state.
3088 template<typename _RealType
, typename _CharT
, typename _Traits
>
3089 std::basic_istream
<_CharT
, _Traits
>&
3090 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3091 std::cauchy_distribution
<_RealType
>& __x
);
3095 * @brief A fisher_f_distribution random number distribution.
3097 * The formula for the normal probability mass function is
3099 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
3100 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
3101 * (1 + \frac{mx}{n})^{-(m+n)/2}
3104 template<typename _RealType
= double>
3105 class fisher_f_distribution
3107 static_assert(std::is_floating_point
<_RealType
>::value
,
3108 "result_type must be a floating point type");
3111 /** The type of the range of the distribution. */
3112 typedef _RealType result_type
;
3114 /** Parameter type. */
3117 typedef fisher_f_distribution
<_RealType
> distribution_type
;
3119 param_type() : param_type(1) { }
3122 param_type(_RealType __m
, _RealType __n
= _RealType(1))
3123 : _M_m(__m
), _M_n(__n
)
3135 operator==(const param_type
& __p1
, const param_type
& __p2
)
3136 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
3138 #if __cpp_impl_three_way_comparison < 201907L
3140 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3141 { return !(__p1
== __p2
); }
3149 fisher_f_distribution() : fisher_f_distribution(1.0) { }
3152 fisher_f_distribution(_RealType __m
,
3153 _RealType __n
= _RealType(1))
3154 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
3158 fisher_f_distribution(const param_type
& __p
)
3159 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
3163 * @brief Resets the distribution state.
3177 { return _M_param
.m(); }
3181 { return _M_param
.n(); }
3184 * @brief Returns the parameter set of the distribution.
3188 { return _M_param
; }
3191 * @brief Sets the parameter set of the distribution.
3192 * @param __param The new parameter set of the distribution.
3195 param(const param_type
& __param
)
3196 { _M_param
= __param
; }
3199 * @brief Returns the greatest lower bound value of the distribution.
3203 { return result_type(0); }
3206 * @brief Returns the least upper bound value of the distribution.
3210 { return std::numeric_limits
<result_type
>::max(); }
3213 * @brief Generating functions.
3215 template<typename _UniformRandomNumberGenerator
>
3217 operator()(_UniformRandomNumberGenerator
& __urng
)
3218 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
3220 template<typename _UniformRandomNumberGenerator
>
3222 operator()(_UniformRandomNumberGenerator
& __urng
,
3223 const param_type
& __p
)
3225 typedef typename
std::gamma_distribution
<result_type
>::param_type
3227 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
3228 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
3231 template<typename _ForwardIterator
,
3232 typename _UniformRandomNumberGenerator
>
3234 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3235 _UniformRandomNumberGenerator
& __urng
)
3236 { this->__generate_impl(__f
, __t
, __urng
); }
3238 template<typename _ForwardIterator
,
3239 typename _UniformRandomNumberGenerator
>
3241 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3242 _UniformRandomNumberGenerator
& __urng
,
3243 const param_type
& __p
)
3244 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3246 template<typename _UniformRandomNumberGenerator
>
3248 __generate(result_type
* __f
, result_type
* __t
,
3249 _UniformRandomNumberGenerator
& __urng
)
3250 { this->__generate_impl(__f
, __t
, __urng
); }
3252 template<typename _UniformRandomNumberGenerator
>
3254 __generate(result_type
* __f
, result_type
* __t
,
3255 _UniformRandomNumberGenerator
& __urng
,
3256 const param_type
& __p
)
3257 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3260 * @brief Return true if two Fisher f distributions have
3261 * the same parameters and the sequences that would
3262 * be generated are equal.
3265 operator==(const fisher_f_distribution
& __d1
,
3266 const fisher_f_distribution
& __d2
)
3267 { return (__d1
._M_param
== __d2
._M_param
3268 && __d1
._M_gd_x
== __d2
._M_gd_x
3269 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
3272 * @brief Inserts a %fisher_f_distribution random number distribution
3273 * @p __x into the output stream @p __os.
3275 * @param __os An output stream.
3276 * @param __x A %fisher_f_distribution random number distribution.
3278 * @returns The output stream with the state of @p __x inserted or in
3281 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3282 friend std::basic_ostream
<_CharT
, _Traits
>&
3283 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3284 const std::fisher_f_distribution
<_RealType1
>& __x
);
3287 * @brief Extracts a %fisher_f_distribution random number distribution
3288 * @p __x from the input stream @p __is.
3290 * @param __is An input stream.
3291 * @param __x A %fisher_f_distribution random number
3294 * @returns The input stream with @p __x extracted or in an error state.
3296 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3297 friend std::basic_istream
<_CharT
, _Traits
>&
3298 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3299 std::fisher_f_distribution
<_RealType1
>& __x
);
3302 template<typename _ForwardIterator
,
3303 typename _UniformRandomNumberGenerator
>
3305 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3306 _UniformRandomNumberGenerator
& __urng
);
3308 template<typename _ForwardIterator
,
3309 typename _UniformRandomNumberGenerator
>
3311 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3312 _UniformRandomNumberGenerator
& __urng
,
3313 const param_type
& __p
);
3315 param_type _M_param
;
3317 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3320 #if __cpp_impl_three_way_comparison < 201907L
3322 * @brief Return true if two Fisher f distributions are different.
3324 template<typename _RealType
>
3326 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3327 const std::fisher_f_distribution
<_RealType
>& __d2
)
3328 { return !(__d1
== __d2
); }
3332 * @brief A student_t_distribution random number distribution.
3334 * The formula for the normal probability mass function is:
3336 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3337 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3340 template<typename _RealType
= double>
3341 class student_t_distribution
3343 static_assert(std::is_floating_point
<_RealType
>::value
,
3344 "result_type must be a floating point type");
3347 /** The type of the range of the distribution. */
3348 typedef _RealType result_type
;
3350 /** Parameter type. */
3353 typedef student_t_distribution
<_RealType
> distribution_type
;
3355 param_type() : param_type(1) { }
3358 param_type(_RealType __n
)
3367 operator==(const param_type
& __p1
, const param_type
& __p2
)
3368 { return __p1
._M_n
== __p2
._M_n
; }
3370 #if __cpp_impl_three_way_comparison < 201907L
3372 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3373 { return !(__p1
== __p2
); }
3380 student_t_distribution() : student_t_distribution(1.0) { }
3383 student_t_distribution(_RealType __n
)
3384 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3388 student_t_distribution(const param_type
& __p
)
3389 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3393 * @brief Resets the distribution state.
3407 { return _M_param
.n(); }
3410 * @brief Returns the parameter set of the distribution.
3414 { return _M_param
; }
3417 * @brief Sets the parameter set of the distribution.
3418 * @param __param The new parameter set of the distribution.
3421 param(const param_type
& __param
)
3422 { _M_param
= __param
; }
3425 * @brief Returns the greatest lower bound value of the distribution.
3429 { return std::numeric_limits
<result_type
>::lowest(); }
3432 * @brief Returns the least upper bound value of the distribution.
3436 { return std::numeric_limits
<result_type
>::max(); }
3439 * @brief Generating functions.
3441 template<typename _UniformRandomNumberGenerator
>
3443 operator()(_UniformRandomNumberGenerator
& __urng
)
3444 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3446 template<typename _UniformRandomNumberGenerator
>
3448 operator()(_UniformRandomNumberGenerator
& __urng
,
3449 const param_type
& __p
)
3451 typedef typename
std::gamma_distribution
<result_type
>::param_type
3454 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3455 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3458 template<typename _ForwardIterator
,
3459 typename _UniformRandomNumberGenerator
>
3461 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3462 _UniformRandomNumberGenerator
& __urng
)
3463 { this->__generate_impl(__f
, __t
, __urng
); }
3465 template<typename _ForwardIterator
,
3466 typename _UniformRandomNumberGenerator
>
3468 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3469 _UniformRandomNumberGenerator
& __urng
,
3470 const param_type
& __p
)
3471 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3473 template<typename _UniformRandomNumberGenerator
>
3475 __generate(result_type
* __f
, result_type
* __t
,
3476 _UniformRandomNumberGenerator
& __urng
)
3477 { this->__generate_impl(__f
, __t
, __urng
); }
3479 template<typename _UniformRandomNumberGenerator
>
3481 __generate(result_type
* __f
, result_type
* __t
,
3482 _UniformRandomNumberGenerator
& __urng
,
3483 const param_type
& __p
)
3484 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3487 * @brief Return true if two Student t distributions have
3488 * the same parameters and the sequences that would
3489 * be generated are equal.
3492 operator==(const student_t_distribution
& __d1
,
3493 const student_t_distribution
& __d2
)
3494 { return (__d1
._M_param
== __d2
._M_param
3495 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3498 * @brief Inserts a %student_t_distribution random number distribution
3499 * @p __x into the output stream @p __os.
3501 * @param __os An output stream.
3502 * @param __x A %student_t_distribution random number distribution.
3504 * @returns The output stream with the state of @p __x inserted or in
3507 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3508 friend std::basic_ostream
<_CharT
, _Traits
>&
3509 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3510 const std::student_t_distribution
<_RealType1
>& __x
);
3513 * @brief Extracts a %student_t_distribution random number distribution
3514 * @p __x from the input stream @p __is.
3516 * @param __is An input stream.
3517 * @param __x A %student_t_distribution random number
3520 * @returns The input stream with @p __x extracted or in an error state.
3522 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3523 friend std::basic_istream
<_CharT
, _Traits
>&
3524 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3525 std::student_t_distribution
<_RealType1
>& __x
);
3528 template<typename _ForwardIterator
,
3529 typename _UniformRandomNumberGenerator
>
3531 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3532 _UniformRandomNumberGenerator
& __urng
);
3533 template<typename _ForwardIterator
,
3534 typename _UniformRandomNumberGenerator
>
3536 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3537 _UniformRandomNumberGenerator
& __urng
,
3538 const param_type
& __p
);
3540 param_type _M_param
;
3542 std::normal_distribution
<result_type
> _M_nd
;
3543 std::gamma_distribution
<result_type
> _M_gd
;
3546 #if __cpp_impl_three_way_comparison < 201907L
3548 * @brief Return true if two Student t distributions are different.
3550 template<typename _RealType
>
3552 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3553 const std::student_t_distribution
<_RealType
>& __d2
)
3554 { return !(__d1
== __d2
); }
3557 /// @} group random_distributions_normal
3560 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3561 * @ingroup random_distributions
3566 * @brief A Bernoulli random number distribution.
3568 * Generates a sequence of true and false values with likelihood @f$p@f$
3569 * that true will come up and @f$(1 - p)@f$ that false will appear.
3571 class bernoulli_distribution
3574 /** The type of the range of the distribution. */
3575 typedef bool result_type
;
3577 /** Parameter type. */
3580 typedef bernoulli_distribution distribution_type
;
3582 param_type() : param_type(0.5) { }
3585 param_type(double __p
)
3588 __glibcxx_assert((_M_p
>= 0.0) && (_M_p
<= 1.0));
3596 operator==(const param_type
& __p1
, const param_type
& __p2
)
3597 { return __p1
._M_p
== __p2
._M_p
; }
3599 #if __cpp_impl_three_way_comparison < 201907L
3601 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3602 { return !(__p1
== __p2
); }
3611 * @brief Constructs a Bernoulli distribution with likelihood 0.5.
3613 bernoulli_distribution() : bernoulli_distribution(0.5) { }
3616 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3618 * @param __p [IN] The likelihood of a true result being returned.
3619 * Must be in the interval @f$[0, 1]@f$.
3622 bernoulli_distribution(double __p
)
3627 bernoulli_distribution(const param_type
& __p
)
3632 * @brief Resets the distribution state.
3634 * Does nothing for a Bernoulli distribution.
3640 * @brief Returns the @p p parameter of the distribution.
3644 { return _M_param
.p(); }
3647 * @brief Returns the parameter set of the distribution.
3651 { return _M_param
; }
3654 * @brief Sets the parameter set of the distribution.
3655 * @param __param The new parameter set of the distribution.
3658 param(const param_type
& __param
)
3659 { _M_param
= __param
; }
3662 * @brief Returns the greatest lower bound value of the distribution.
3666 { return std::numeric_limits
<result_type
>::min(); }
3669 * @brief Returns the least upper bound value of the distribution.
3673 { return std::numeric_limits
<result_type
>::max(); }
3676 * @brief Generating functions.
3678 template<typename _UniformRandomNumberGenerator
>
3680 operator()(_UniformRandomNumberGenerator
& __urng
)
3681 { return this->operator()(__urng
, _M_param
); }
3683 template<typename _UniformRandomNumberGenerator
>
3685 operator()(_UniformRandomNumberGenerator
& __urng
,
3686 const param_type
& __p
)
3688 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3690 if ((__aurng() - __aurng
.min())
3691 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3696 template<typename _ForwardIterator
,
3697 typename _UniformRandomNumberGenerator
>
3699 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3700 _UniformRandomNumberGenerator
& __urng
)
3701 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3703 template<typename _ForwardIterator
,
3704 typename _UniformRandomNumberGenerator
>
3706 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3707 _UniformRandomNumberGenerator
& __urng
, const param_type
& __p
)
3708 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3710 template<typename _UniformRandomNumberGenerator
>
3712 __generate(result_type
* __f
, result_type
* __t
,
3713 _UniformRandomNumberGenerator
& __urng
,
3714 const param_type
& __p
)
3715 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3718 * @brief Return true if two Bernoulli distributions have
3719 * the same parameters.
3722 operator==(const bernoulli_distribution
& __d1
,
3723 const bernoulli_distribution
& __d2
)
3724 { return __d1
._M_param
== __d2
._M_param
; }
3727 template<typename _ForwardIterator
,
3728 typename _UniformRandomNumberGenerator
>
3730 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3731 _UniformRandomNumberGenerator
& __urng
,
3732 const param_type
& __p
);
3734 param_type _M_param
;
3737 #if __cpp_impl_three_way_comparison < 201907L
3739 * @brief Return true if two Bernoulli distributions have
3740 * different parameters.
3743 operator!=(const std::bernoulli_distribution
& __d1
,
3744 const std::bernoulli_distribution
& __d2
)
3745 { return !(__d1
== __d2
); }
3749 * @brief Inserts a %bernoulli_distribution random number distribution
3750 * @p __x into the output stream @p __os.
3752 * @param __os An output stream.
3753 * @param __x A %bernoulli_distribution random number distribution.
3755 * @returns The output stream with the state of @p __x inserted or in
3758 template<typename _CharT
, typename _Traits
>
3759 std::basic_ostream
<_CharT
, _Traits
>&
3760 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3761 const std::bernoulli_distribution
& __x
);
3764 * @brief Extracts a %bernoulli_distribution random number distribution
3765 * @p __x from the input stream @p __is.
3767 * @param __is An input stream.
3768 * @param __x A %bernoulli_distribution random number generator engine.
3770 * @returns The input stream with @p __x extracted or in an error state.
3772 template<typename _CharT
, typename _Traits
>
3773 inline std::basic_istream
<_CharT
, _Traits
>&
3774 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3775 std::bernoulli_distribution
& __x
)
3779 __x
.param(bernoulli_distribution::param_type(__p
));
3785 * @brief A discrete binomial random number distribution.
3787 * The formula for the binomial probability density function is
3788 * @f$p(i|t,p) = \binom{t}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3789 * and @f$p@f$ are the parameters of the distribution.
3791 template<typename _IntType
= int>
3792 class binomial_distribution
3794 static_assert(std::is_integral
<_IntType
>::value
,
3795 "result_type must be an integral type");
3798 /** The type of the range of the distribution. */
3799 typedef _IntType result_type
;
3801 /** Parameter type. */
3804 typedef binomial_distribution
<_IntType
> distribution_type
;
3805 friend class binomial_distribution
<_IntType
>;
3807 param_type() : param_type(1) { }
3810 param_type(_IntType __t
, double __p
= 0.5)
3811 : _M_t(__t
), _M_p(__p
)
3813 __glibcxx_assert((_M_t
>= _IntType(0))
3828 operator==(const param_type
& __p1
, const param_type
& __p2
)
3829 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3831 #if __cpp_impl_three_way_comparison < 201907L
3833 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3834 { return !(__p1
== __p2
); }
3845 #if _GLIBCXX_USE_C99_MATH_TR1
3846 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3847 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3852 // constructors and member functions
3854 binomial_distribution() : binomial_distribution(1) { }
3857 binomial_distribution(_IntType __t
, double __p
= 0.5)
3858 : _M_param(__t
, __p
), _M_nd()
3862 binomial_distribution(const param_type
& __p
)
3863 : _M_param(__p
), _M_nd()
3867 * @brief Resets the distribution state.
3874 * @brief Returns the distribution @p t parameter.
3878 { return _M_param
.t(); }
3881 * @brief Returns the distribution @p p parameter.
3885 { return _M_param
.p(); }
3888 * @brief Returns the parameter set of the distribution.
3892 { return _M_param
; }
3895 * @brief Sets the parameter set of the distribution.
3896 * @param __param The new parameter set of the distribution.
3899 param(const param_type
& __param
)
3900 { _M_param
= __param
; }
3903 * @brief Returns the greatest lower bound value of the distribution.
3910 * @brief Returns the least upper bound value of the distribution.
3914 { return _M_param
.t(); }
3917 * @brief Generating functions.
3919 template<typename _UniformRandomNumberGenerator
>
3921 operator()(_UniformRandomNumberGenerator
& __urng
)
3922 { return this->operator()(__urng
, _M_param
); }
3924 template<typename _UniformRandomNumberGenerator
>
3926 operator()(_UniformRandomNumberGenerator
& __urng
,
3927 const param_type
& __p
);
3929 template<typename _ForwardIterator
,
3930 typename _UniformRandomNumberGenerator
>
3932 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3933 _UniformRandomNumberGenerator
& __urng
)
3934 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3936 template<typename _ForwardIterator
,
3937 typename _UniformRandomNumberGenerator
>
3939 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3940 _UniformRandomNumberGenerator
& __urng
,
3941 const param_type
& __p
)
3942 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3944 template<typename _UniformRandomNumberGenerator
>
3946 __generate(result_type
* __f
, result_type
* __t
,
3947 _UniformRandomNumberGenerator
& __urng
,
3948 const param_type
& __p
)
3949 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3952 * @brief Return true if two binomial distributions have
3953 * the same parameters and the sequences that would
3954 * be generated are equal.
3957 operator==(const binomial_distribution
& __d1
,
3958 const binomial_distribution
& __d2
)
3959 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3960 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
3962 { return __d1
._M_param
== __d2
._M_param
; }
3966 * @brief Inserts a %binomial_distribution random number distribution
3967 * @p __x into the output stream @p __os.
3969 * @param __os An output stream.
3970 * @param __x A %binomial_distribution random number distribution.
3972 * @returns The output stream with the state of @p __x inserted or in
3975 template<typename _IntType1
,
3976 typename _CharT
, typename _Traits
>
3977 friend std::basic_ostream
<_CharT
, _Traits
>&
3978 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3979 const std::binomial_distribution
<_IntType1
>& __x
);
3982 * @brief Extracts a %binomial_distribution random number distribution
3983 * @p __x from the input stream @p __is.
3985 * @param __is An input stream.
3986 * @param __x A %binomial_distribution random number generator engine.
3988 * @returns The input stream with @p __x extracted or in an error
3991 template<typename _IntType1
,
3992 typename _CharT
, typename _Traits
>
3993 friend std::basic_istream
<_CharT
, _Traits
>&
3994 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3995 std::binomial_distribution
<_IntType1
>& __x
);
3998 template<typename _ForwardIterator
,
3999 typename _UniformRandomNumberGenerator
>
4001 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4002 _UniformRandomNumberGenerator
& __urng
,
4003 const param_type
& __p
);
4005 template<typename _UniformRandomNumberGenerator
>
4007 _M_waiting(_UniformRandomNumberGenerator
& __urng
,
4008 _IntType __t
, double __q
);
4010 param_type _M_param
;
4012 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4013 std::normal_distribution
<double> _M_nd
;
4016 #if __cpp_impl_three_way_comparison < 201907L
4018 * @brief Return true if two binomial distributions are different.
4020 template<typename _IntType
>
4022 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
4023 const std::binomial_distribution
<_IntType
>& __d2
)
4024 { return !(__d1
== __d2
); }
4028 * @brief A discrete geometric random number distribution.
4030 * The formula for the geometric probability density function is
4031 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
4034 template<typename _IntType
= int>
4035 class geometric_distribution
4037 static_assert(std::is_integral
<_IntType
>::value
,
4038 "result_type must be an integral type");
4041 /** The type of the range of the distribution. */
4042 typedef _IntType result_type
;
4044 /** Parameter type. */
4047 typedef geometric_distribution
<_IntType
> distribution_type
;
4048 friend class geometric_distribution
<_IntType
>;
4050 param_type() : param_type(0.5) { }
4053 param_type(double __p
)
4056 __glibcxx_assert((_M_p
> 0.0) && (_M_p
< 1.0));
4065 operator==(const param_type
& __p1
, const param_type
& __p2
)
4066 { return __p1
._M_p
== __p2
._M_p
; }
4068 #if __cpp_impl_three_way_comparison < 201907L
4070 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4071 { return !(__p1
== __p2
); }
4077 { _M_log_1_p
= std::log(1.0 - _M_p
); }
4084 // constructors and member functions
4086 geometric_distribution() : geometric_distribution(0.5) { }
4089 geometric_distribution(double __p
)
4094 geometric_distribution(const param_type
& __p
)
4099 * @brief Resets the distribution state.
4101 * Does nothing for the geometric distribution.
4107 * @brief Returns the distribution parameter @p p.
4111 { return _M_param
.p(); }
4114 * @brief Returns the parameter set of the distribution.
4118 { return _M_param
; }
4121 * @brief Sets the parameter set of the distribution.
4122 * @param __param The new parameter set of the distribution.
4125 param(const param_type
& __param
)
4126 { _M_param
= __param
; }
4129 * @brief Returns the greatest lower bound value of the distribution.
4136 * @brief Returns the least upper bound value of the distribution.
4140 { return std::numeric_limits
<result_type
>::max(); }
4143 * @brief Generating functions.
4145 template<typename _UniformRandomNumberGenerator
>
4147 operator()(_UniformRandomNumberGenerator
& __urng
)
4148 { return this->operator()(__urng
, _M_param
); }
4150 template<typename _UniformRandomNumberGenerator
>
4152 operator()(_UniformRandomNumberGenerator
& __urng
,
4153 const param_type
& __p
);
4155 template<typename _ForwardIterator
,
4156 typename _UniformRandomNumberGenerator
>
4158 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4159 _UniformRandomNumberGenerator
& __urng
)
4160 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4162 template<typename _ForwardIterator
,
4163 typename _UniformRandomNumberGenerator
>
4165 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4166 _UniformRandomNumberGenerator
& __urng
,
4167 const param_type
& __p
)
4168 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4170 template<typename _UniformRandomNumberGenerator
>
4172 __generate(result_type
* __f
, result_type
* __t
,
4173 _UniformRandomNumberGenerator
& __urng
,
4174 const param_type
& __p
)
4175 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4178 * @brief Return true if two geometric distributions have
4179 * the same parameters.
4182 operator==(const geometric_distribution
& __d1
,
4183 const geometric_distribution
& __d2
)
4184 { return __d1
._M_param
== __d2
._M_param
; }
4187 template<typename _ForwardIterator
,
4188 typename _UniformRandomNumberGenerator
>
4190 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4191 _UniformRandomNumberGenerator
& __urng
,
4192 const param_type
& __p
);
4194 param_type _M_param
;
4197 #if __cpp_impl_three_way_comparison < 201907L
4199 * @brief Return true if two geometric distributions have
4200 * different parameters.
4202 template<typename _IntType
>
4204 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
4205 const std::geometric_distribution
<_IntType
>& __d2
)
4206 { return !(__d1
== __d2
); }
4210 * @brief Inserts a %geometric_distribution random number distribution
4211 * @p __x into the output stream @p __os.
4213 * @param __os An output stream.
4214 * @param __x A %geometric_distribution random number distribution.
4216 * @returns The output stream with the state of @p __x inserted or in
4219 template<typename _IntType
,
4220 typename _CharT
, typename _Traits
>
4221 std::basic_ostream
<_CharT
, _Traits
>&
4222 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4223 const std::geometric_distribution
<_IntType
>& __x
);
4226 * @brief Extracts a %geometric_distribution random number distribution
4227 * @p __x from the input stream @p __is.
4229 * @param __is An input stream.
4230 * @param __x A %geometric_distribution random number generator engine.
4232 * @returns The input stream with @p __x extracted or in an error state.
4234 template<typename _IntType
,
4235 typename _CharT
, typename _Traits
>
4236 std::basic_istream
<_CharT
, _Traits
>&
4237 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4238 std::geometric_distribution
<_IntType
>& __x
);
4242 * @brief A negative_binomial_distribution random number distribution.
4244 * The formula for the negative binomial probability mass function is
4245 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
4246 * and @f$p@f$ are the parameters of the distribution.
4248 template<typename _IntType
= int>
4249 class negative_binomial_distribution
4251 static_assert(std::is_integral
<_IntType
>::value
,
4252 "result_type must be an integral type");
4255 /** The type of the range of the distribution. */
4256 typedef _IntType result_type
;
4258 /** Parameter type. */
4261 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
4263 param_type() : param_type(1) { }
4266 param_type(_IntType __k
, double __p
= 0.5)
4267 : _M_k(__k
), _M_p(__p
)
4269 __glibcxx_assert((_M_k
> 0) && (_M_p
> 0.0) && (_M_p
<= 1.0));
4281 operator==(const param_type
& __p1
, const param_type
& __p2
)
4282 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
4284 #if __cpp_impl_three_way_comparison < 201907L
4286 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4287 { return !(__p1
== __p2
); }
4295 negative_binomial_distribution() : negative_binomial_distribution(1) { }
4298 negative_binomial_distribution(_IntType __k
, double __p
= 0.5)
4299 : _M_param(__k
, __p
), _M_gd(__k
, (1.0 - __p
) / __p
)
4303 negative_binomial_distribution(const param_type
& __p
)
4304 : _M_param(__p
), _M_gd(__p
.k(), (1.0 - __p
.p()) / __p
.p())
4308 * @brief Resets the distribution state.
4315 * @brief Return the @f$k@f$ parameter of the distribution.
4319 { return _M_param
.k(); }
4322 * @brief Return the @f$p@f$ parameter of the distribution.
4326 { return _M_param
.p(); }
4329 * @brief Returns the parameter set of the distribution.
4333 { return _M_param
; }
4336 * @brief Sets the parameter set of the distribution.
4337 * @param __param The new parameter set of the distribution.
4340 param(const param_type
& __param
)
4341 { _M_param
= __param
; }
4344 * @brief Returns the greatest lower bound value of the distribution.
4348 { return result_type(0); }
4351 * @brief Returns the least upper bound value of the distribution.
4355 { return std::numeric_limits
<result_type
>::max(); }
4358 * @brief Generating functions.
4360 template<typename _UniformRandomNumberGenerator
>
4362 operator()(_UniformRandomNumberGenerator
& __urng
);
4364 template<typename _UniformRandomNumberGenerator
>
4366 operator()(_UniformRandomNumberGenerator
& __urng
,
4367 const param_type
& __p
);
4369 template<typename _ForwardIterator
,
4370 typename _UniformRandomNumberGenerator
>
4372 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4373 _UniformRandomNumberGenerator
& __urng
)
4374 { this->__generate_impl(__f
, __t
, __urng
); }
4376 template<typename _ForwardIterator
,
4377 typename _UniformRandomNumberGenerator
>
4379 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4380 _UniformRandomNumberGenerator
& __urng
,
4381 const param_type
& __p
)
4382 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4384 template<typename _UniformRandomNumberGenerator
>
4386 __generate(result_type
* __f
, result_type
* __t
,
4387 _UniformRandomNumberGenerator
& __urng
)
4388 { this->__generate_impl(__f
, __t
, __urng
); }
4390 template<typename _UniformRandomNumberGenerator
>
4392 __generate(result_type
* __f
, result_type
* __t
,
4393 _UniformRandomNumberGenerator
& __urng
,
4394 const param_type
& __p
)
4395 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4398 * @brief Return true if two negative binomial distributions have
4399 * the same parameters and the sequences that would be
4400 * generated are equal.
4403 operator==(const negative_binomial_distribution
& __d1
,
4404 const negative_binomial_distribution
& __d2
)
4405 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
4408 * @brief Inserts a %negative_binomial_distribution random
4409 * number distribution @p __x into the output stream @p __os.
4411 * @param __os An output stream.
4412 * @param __x A %negative_binomial_distribution random number
4415 * @returns The output stream with the state of @p __x inserted or in
4418 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4419 friend std::basic_ostream
<_CharT
, _Traits
>&
4420 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4421 const std::negative_binomial_distribution
<_IntType1
>& __x
);
4424 * @brief Extracts a %negative_binomial_distribution random number
4425 * distribution @p __x from the input stream @p __is.
4427 * @param __is An input stream.
4428 * @param __x A %negative_binomial_distribution random number
4431 * @returns The input stream with @p __x extracted or in an error state.
4433 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4434 friend std::basic_istream
<_CharT
, _Traits
>&
4435 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4436 std::negative_binomial_distribution
<_IntType1
>& __x
);
4439 template<typename _ForwardIterator
,
4440 typename _UniformRandomNumberGenerator
>
4442 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4443 _UniformRandomNumberGenerator
& __urng
);
4444 template<typename _ForwardIterator
,
4445 typename _UniformRandomNumberGenerator
>
4447 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4448 _UniformRandomNumberGenerator
& __urng
,
4449 const param_type
& __p
);
4451 param_type _M_param
;
4453 std::gamma_distribution
<double> _M_gd
;
4456 #if __cpp_impl_three_way_comparison < 201907L
4458 * @brief Return true if two negative binomial distributions are different.
4460 template<typename _IntType
>
4462 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
4463 const std::negative_binomial_distribution
<_IntType
>& __d2
)
4464 { return !(__d1
== __d2
); }
4467 /// @} group random_distributions_bernoulli
4470 * @addtogroup random_distributions_poisson Poisson Distributions
4471 * @ingroup random_distributions
4476 * @brief A discrete Poisson random number distribution.
4478 * The formula for the Poisson probability density function is
4479 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
4480 * parameter of the distribution.
4482 template<typename _IntType
= int>
4483 class poisson_distribution
4485 static_assert(std::is_integral
<_IntType
>::value
,
4486 "result_type must be an integral type");
4489 /** The type of the range of the distribution. */
4490 typedef _IntType result_type
;
4492 /** Parameter type. */
4495 typedef poisson_distribution
<_IntType
> distribution_type
;
4496 friend class poisson_distribution
<_IntType
>;
4498 param_type() : param_type(1.0) { }
4501 param_type(double __mean
)
4504 __glibcxx_assert(_M_mean
> 0.0);
4513 operator==(const param_type
& __p1
, const param_type
& __p2
)
4514 { return __p1
._M_mean
== __p2
._M_mean
; }
4516 #if __cpp_impl_three_way_comparison < 201907L
4518 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4519 { return !(__p1
== __p2
); }
4523 // Hosts either log(mean) or the threshold of the simple method.
4530 #if _GLIBCXX_USE_C99_MATH_TR1
4531 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
4535 // constructors and member functions
4537 poisson_distribution() : poisson_distribution(1.0) { }
4540 poisson_distribution(double __mean
)
4541 : _M_param(__mean
), _M_nd()
4545 poisson_distribution(const param_type
& __p
)
4546 : _M_param(__p
), _M_nd()
4550 * @brief Resets the distribution state.
4557 * @brief Returns the distribution parameter @p mean.
4561 { return _M_param
.mean(); }
4564 * @brief Returns the parameter set of the distribution.
4568 { return _M_param
; }
4571 * @brief Sets the parameter set of the distribution.
4572 * @param __param The new parameter set of the distribution.
4575 param(const param_type
& __param
)
4576 { _M_param
= __param
; }
4579 * @brief Returns the greatest lower bound value of the distribution.
4586 * @brief Returns the least upper bound value of the distribution.
4590 { return std::numeric_limits
<result_type
>::max(); }
4593 * @brief Generating functions.
4595 template<typename _UniformRandomNumberGenerator
>
4597 operator()(_UniformRandomNumberGenerator
& __urng
)
4598 { return this->operator()(__urng
, _M_param
); }
4600 template<typename _UniformRandomNumberGenerator
>
4602 operator()(_UniformRandomNumberGenerator
& __urng
,
4603 const param_type
& __p
);
4605 template<typename _ForwardIterator
,
4606 typename _UniformRandomNumberGenerator
>
4608 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4609 _UniformRandomNumberGenerator
& __urng
)
4610 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4612 template<typename _ForwardIterator
,
4613 typename _UniformRandomNumberGenerator
>
4615 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4616 _UniformRandomNumberGenerator
& __urng
,
4617 const param_type
& __p
)
4618 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4620 template<typename _UniformRandomNumberGenerator
>
4622 __generate(result_type
* __f
, result_type
* __t
,
4623 _UniformRandomNumberGenerator
& __urng
,
4624 const param_type
& __p
)
4625 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4628 * @brief Return true if two Poisson distributions have the same
4629 * parameters and the sequences that would be generated
4633 operator==(const poisson_distribution
& __d1
,
4634 const poisson_distribution
& __d2
)
4635 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4636 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
4638 { return __d1
._M_param
== __d2
._M_param
; }
4642 * @brief Inserts a %poisson_distribution random number distribution
4643 * @p __x into the output stream @p __os.
4645 * @param __os An output stream.
4646 * @param __x A %poisson_distribution random number distribution.
4648 * @returns The output stream with the state of @p __x inserted or in
4651 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4652 friend std::basic_ostream
<_CharT
, _Traits
>&
4653 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4654 const std::poisson_distribution
<_IntType1
>& __x
);
4657 * @brief Extracts a %poisson_distribution random number distribution
4658 * @p __x from the input stream @p __is.
4660 * @param __is An input stream.
4661 * @param __x A %poisson_distribution random number generator engine.
4663 * @returns The input stream with @p __x extracted or in an error
4666 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4667 friend std::basic_istream
<_CharT
, _Traits
>&
4668 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4669 std::poisson_distribution
<_IntType1
>& __x
);
4672 template<typename _ForwardIterator
,
4673 typename _UniformRandomNumberGenerator
>
4675 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4676 _UniformRandomNumberGenerator
& __urng
,
4677 const param_type
& __p
);
4679 param_type _M_param
;
4681 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4682 std::normal_distribution
<double> _M_nd
;
4685 #if __cpp_impl_three_way_comparison < 201907L
4687 * @brief Return true if two Poisson distributions are different.
4689 template<typename _IntType
>
4691 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4692 const std::poisson_distribution
<_IntType
>& __d2
)
4693 { return !(__d1
== __d2
); }
4697 * @brief An exponential continuous distribution for random numbers.
4699 * The formula for the exponential probability density function is
4700 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4702 * <table border=1 cellpadding=10 cellspacing=0>
4703 * <caption align=top>Distribution Statistics</caption>
4704 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4705 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4706 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4707 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4708 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4711 template<typename _RealType
= double>
4712 class exponential_distribution
4714 static_assert(std::is_floating_point
<_RealType
>::value
,
4715 "result_type must be a floating point type");
4718 /** The type of the range of the distribution. */
4719 typedef _RealType result_type
;
4721 /** Parameter type. */
4724 typedef exponential_distribution
<_RealType
> distribution_type
;
4726 param_type() : param_type(1.0) { }
4729 param_type(_RealType __lambda
)
4730 : _M_lambda(__lambda
)
4732 __glibcxx_assert(_M_lambda
> _RealType(0));
4737 { return _M_lambda
; }
4740 operator==(const param_type
& __p1
, const param_type
& __p2
)
4741 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4743 #if __cpp_impl_three_way_comparison < 201907L
4745 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4746 { return !(__p1
== __p2
); }
4750 _RealType _M_lambda
;
4755 * @brief Constructs an exponential distribution with inverse scale
4758 exponential_distribution() : exponential_distribution(1.0) { }
4761 * @brief Constructs an exponential distribution with inverse scale
4762 * parameter @f$\lambda@f$.
4765 exponential_distribution(_RealType __lambda
)
4766 : _M_param(__lambda
)
4770 exponential_distribution(const param_type
& __p
)
4775 * @brief Resets the distribution state.
4777 * Has no effect on exponential distributions.
4783 * @brief Returns the inverse scale parameter of the distribution.
4787 { return _M_param
.lambda(); }
4790 * @brief Returns the parameter set of the distribution.
4794 { return _M_param
; }
4797 * @brief Sets the parameter set of the distribution.
4798 * @param __param The new parameter set of the distribution.
4801 param(const param_type
& __param
)
4802 { _M_param
= __param
; }
4805 * @brief Returns the greatest lower bound value of the distribution.
4809 { return result_type(0); }
4812 * @brief Returns the least upper bound value of the distribution.
4816 { return std::numeric_limits
<result_type
>::max(); }
4819 * @brief Generating functions.
4821 template<typename _UniformRandomNumberGenerator
>
4823 operator()(_UniformRandomNumberGenerator
& __urng
)
4824 { return this->operator()(__urng
, _M_param
); }
4826 template<typename _UniformRandomNumberGenerator
>
4828 operator()(_UniformRandomNumberGenerator
& __urng
,
4829 const param_type
& __p
)
4831 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4833 return -std::log(result_type(1) - __aurng()) / __p
.lambda();
4836 template<typename _ForwardIterator
,
4837 typename _UniformRandomNumberGenerator
>
4839 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4840 _UniformRandomNumberGenerator
& __urng
)
4841 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4843 template<typename _ForwardIterator
,
4844 typename _UniformRandomNumberGenerator
>
4846 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4847 _UniformRandomNumberGenerator
& __urng
,
4848 const param_type
& __p
)
4849 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4851 template<typename _UniformRandomNumberGenerator
>
4853 __generate(result_type
* __f
, result_type
* __t
,
4854 _UniformRandomNumberGenerator
& __urng
,
4855 const param_type
& __p
)
4856 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4859 * @brief Return true if two exponential distributions have the same
4863 operator==(const exponential_distribution
& __d1
,
4864 const exponential_distribution
& __d2
)
4865 { return __d1
._M_param
== __d2
._M_param
; }
4868 template<typename _ForwardIterator
,
4869 typename _UniformRandomNumberGenerator
>
4871 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4872 _UniformRandomNumberGenerator
& __urng
,
4873 const param_type
& __p
);
4875 param_type _M_param
;
4878 #if __cpp_impl_three_way_comparison < 201907L
4880 * @brief Return true if two exponential distributions have different
4883 template<typename _RealType
>
4885 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4886 const std::exponential_distribution
<_RealType
>& __d2
)
4887 { return !(__d1
== __d2
); }
4891 * @brief Inserts a %exponential_distribution random number distribution
4892 * @p __x into the output stream @p __os.
4894 * @param __os An output stream.
4895 * @param __x A %exponential_distribution random number distribution.
4897 * @returns The output stream with the state of @p __x inserted or in
4900 template<typename _RealType
, typename _CharT
, typename _Traits
>
4901 std::basic_ostream
<_CharT
, _Traits
>&
4902 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4903 const std::exponential_distribution
<_RealType
>& __x
);
4906 * @brief Extracts a %exponential_distribution random number distribution
4907 * @p __x from the input stream @p __is.
4909 * @param __is An input stream.
4910 * @param __x A %exponential_distribution random number
4913 * @returns The input stream with @p __x extracted or in an error state.
4915 template<typename _RealType
, typename _CharT
, typename _Traits
>
4916 std::basic_istream
<_CharT
, _Traits
>&
4917 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4918 std::exponential_distribution
<_RealType
>& __x
);
4922 * @brief A weibull_distribution random number distribution.
4924 * The formula for the normal probability density function is:
4926 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4927 * \exp{(-(\frac{x}{\beta})^\alpha)}
4930 template<typename _RealType
= double>
4931 class weibull_distribution
4933 static_assert(std::is_floating_point
<_RealType
>::value
,
4934 "result_type must be a floating point type");
4937 /** The type of the range of the distribution. */
4938 typedef _RealType result_type
;
4940 /** Parameter type. */
4943 typedef weibull_distribution
<_RealType
> distribution_type
;
4945 param_type() : param_type(1.0) { }
4948 param_type(_RealType __a
, _RealType __b
= _RealType(1.0))
4949 : _M_a(__a
), _M_b(__b
)
4961 operator==(const param_type
& __p1
, const param_type
& __p2
)
4962 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4964 #if __cpp_impl_three_way_comparison < 201907L
4966 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4967 { return !(__p1
== __p2
); }
4975 weibull_distribution() : weibull_distribution(1.0) { }
4978 weibull_distribution(_RealType __a
, _RealType __b
= _RealType(1))
4979 : _M_param(__a
, __b
)
4983 weibull_distribution(const param_type
& __p
)
4988 * @brief Resets the distribution state.
4995 * @brief Return the @f$a@f$ parameter of the distribution.
4999 { return _M_param
.a(); }
5002 * @brief Return the @f$b@f$ parameter of the distribution.
5006 { return _M_param
.b(); }
5009 * @brief Returns the parameter set of the distribution.
5013 { return _M_param
; }
5016 * @brief Sets the parameter set of the distribution.
5017 * @param __param The new parameter set of the distribution.
5020 param(const param_type
& __param
)
5021 { _M_param
= __param
; }
5024 * @brief Returns the greatest lower bound value of the distribution.
5028 { return result_type(0); }
5031 * @brief Returns the least upper bound value of the distribution.
5035 { return std::numeric_limits
<result_type
>::max(); }
5038 * @brief Generating functions.
5040 template<typename _UniformRandomNumberGenerator
>
5042 operator()(_UniformRandomNumberGenerator
& __urng
)
5043 { return this->operator()(__urng
, _M_param
); }
5045 template<typename _UniformRandomNumberGenerator
>
5047 operator()(_UniformRandomNumberGenerator
& __urng
,
5048 const param_type
& __p
);
5050 template<typename _ForwardIterator
,
5051 typename _UniformRandomNumberGenerator
>
5053 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5054 _UniformRandomNumberGenerator
& __urng
)
5055 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5057 template<typename _ForwardIterator
,
5058 typename _UniformRandomNumberGenerator
>
5060 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5061 _UniformRandomNumberGenerator
& __urng
,
5062 const param_type
& __p
)
5063 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5065 template<typename _UniformRandomNumberGenerator
>
5067 __generate(result_type
* __f
, result_type
* __t
,
5068 _UniformRandomNumberGenerator
& __urng
,
5069 const param_type
& __p
)
5070 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5073 * @brief Return true if two Weibull distributions have the same
5077 operator==(const weibull_distribution
& __d1
,
5078 const weibull_distribution
& __d2
)
5079 { return __d1
._M_param
== __d2
._M_param
; }
5082 template<typename _ForwardIterator
,
5083 typename _UniformRandomNumberGenerator
>
5085 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5086 _UniformRandomNumberGenerator
& __urng
,
5087 const param_type
& __p
);
5089 param_type _M_param
;
5092 #if __cpp_impl_three_way_comparison < 201907L
5094 * @brief Return true if two Weibull distributions have different
5097 template<typename _RealType
>
5099 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
5100 const std::weibull_distribution
<_RealType
>& __d2
)
5101 { return !(__d1
== __d2
); }
5105 * @brief Inserts a %weibull_distribution random number distribution
5106 * @p __x into the output stream @p __os.
5108 * @param __os An output stream.
5109 * @param __x A %weibull_distribution random number distribution.
5111 * @returns The output stream with the state of @p __x inserted or in
5114 template<typename _RealType
, typename _CharT
, typename _Traits
>
5115 std::basic_ostream
<_CharT
, _Traits
>&
5116 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5117 const std::weibull_distribution
<_RealType
>& __x
);
5120 * @brief Extracts a %weibull_distribution random number distribution
5121 * @p __x from the input stream @p __is.
5123 * @param __is An input stream.
5124 * @param __x A %weibull_distribution random number
5127 * @returns The input stream with @p __x extracted or in an error state.
5129 template<typename _RealType
, typename _CharT
, typename _Traits
>
5130 std::basic_istream
<_CharT
, _Traits
>&
5131 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5132 std::weibull_distribution
<_RealType
>& __x
);
5136 * @brief A extreme_value_distribution random number distribution.
5138 * The formula for the normal probability mass function is
5140 * p(x|a,b) = \frac{1}{b}
5141 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
5144 template<typename _RealType
= double>
5145 class extreme_value_distribution
5147 static_assert(std::is_floating_point
<_RealType
>::value
,
5148 "result_type must be a floating point type");
5151 /** The type of the range of the distribution. */
5152 typedef _RealType result_type
;
5154 /** Parameter type. */
5157 typedef extreme_value_distribution
<_RealType
> distribution_type
;
5159 param_type() : param_type(0.0) { }
5162 param_type(_RealType __a
, _RealType __b
= _RealType(1.0))
5163 : _M_a(__a
), _M_b(__b
)
5175 operator==(const param_type
& __p1
, const param_type
& __p2
)
5176 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
5178 #if __cpp_impl_three_way_comparison < 201907L
5180 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5181 { return !(__p1
== __p2
); }
5189 extreme_value_distribution() : extreme_value_distribution(0.0) { }
5192 extreme_value_distribution(_RealType __a
, _RealType __b
= _RealType(1))
5193 : _M_param(__a
, __b
)
5197 extreme_value_distribution(const param_type
& __p
)
5202 * @brief Resets the distribution state.
5209 * @brief Return the @f$a@f$ parameter of the distribution.
5213 { return _M_param
.a(); }
5216 * @brief Return the @f$b@f$ parameter of the distribution.
5220 { return _M_param
.b(); }
5223 * @brief Returns the parameter set of the distribution.
5227 { return _M_param
; }
5230 * @brief Sets the parameter set of the distribution.
5231 * @param __param The new parameter set of the distribution.
5234 param(const param_type
& __param
)
5235 { _M_param
= __param
; }
5238 * @brief Returns the greatest lower bound value of the distribution.
5242 { return std::numeric_limits
<result_type
>::lowest(); }
5245 * @brief Returns the least upper bound value of the distribution.
5249 { return std::numeric_limits
<result_type
>::max(); }
5252 * @brief Generating functions.
5254 template<typename _UniformRandomNumberGenerator
>
5256 operator()(_UniformRandomNumberGenerator
& __urng
)
5257 { return this->operator()(__urng
, _M_param
); }
5259 template<typename _UniformRandomNumberGenerator
>
5261 operator()(_UniformRandomNumberGenerator
& __urng
,
5262 const param_type
& __p
);
5264 template<typename _ForwardIterator
,
5265 typename _UniformRandomNumberGenerator
>
5267 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5268 _UniformRandomNumberGenerator
& __urng
)
5269 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5271 template<typename _ForwardIterator
,
5272 typename _UniformRandomNumberGenerator
>
5274 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5275 _UniformRandomNumberGenerator
& __urng
,
5276 const param_type
& __p
)
5277 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5279 template<typename _UniformRandomNumberGenerator
>
5281 __generate(result_type
* __f
, result_type
* __t
,
5282 _UniformRandomNumberGenerator
& __urng
,
5283 const param_type
& __p
)
5284 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5287 * @brief Return true if two extreme value distributions have the same
5291 operator==(const extreme_value_distribution
& __d1
,
5292 const extreme_value_distribution
& __d2
)
5293 { return __d1
._M_param
== __d2
._M_param
; }
5296 template<typename _ForwardIterator
,
5297 typename _UniformRandomNumberGenerator
>
5299 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5300 _UniformRandomNumberGenerator
& __urng
,
5301 const param_type
& __p
);
5303 param_type _M_param
;
5306 #if __cpp_impl_three_way_comparison < 201907L
5308 * @brief Return true if two extreme value distributions have different
5311 template<typename _RealType
>
5313 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
5314 const std::extreme_value_distribution
<_RealType
>& __d2
)
5315 { return !(__d1
== __d2
); }
5319 * @brief Inserts a %extreme_value_distribution random number distribution
5320 * @p __x into the output stream @p __os.
5322 * @param __os An output stream.
5323 * @param __x A %extreme_value_distribution random number distribution.
5325 * @returns The output stream with the state of @p __x inserted or in
5328 template<typename _RealType
, typename _CharT
, typename _Traits
>
5329 std::basic_ostream
<_CharT
, _Traits
>&
5330 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5331 const std::extreme_value_distribution
<_RealType
>& __x
);
5334 * @brief Extracts a %extreme_value_distribution random number
5335 * distribution @p __x from the input stream @p __is.
5337 * @param __is An input stream.
5338 * @param __x A %extreme_value_distribution random number
5341 * @returns The input stream with @p __x extracted or in an error state.
5343 template<typename _RealType
, typename _CharT
, typename _Traits
>
5344 std::basic_istream
<_CharT
, _Traits
>&
5345 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5346 std::extreme_value_distribution
<_RealType
>& __x
);
5350 * @brief A discrete_distribution random number distribution.
5352 * The formula for the discrete probability mass function is
5355 template<typename _IntType
= int>
5356 class discrete_distribution
5358 static_assert(std::is_integral
<_IntType
>::value
,
5359 "result_type must be an integral type");
5362 /** The type of the range of the distribution. */
5363 typedef _IntType result_type
;
5365 /** Parameter type. */
5368 typedef discrete_distribution
<_IntType
> distribution_type
;
5369 friend class discrete_distribution
<_IntType
>;
5372 : _M_prob(), _M_cp()
5375 template<typename _InputIterator
>
5376 param_type(_InputIterator __wbegin
,
5377 _InputIterator __wend
)
5378 : _M_prob(__wbegin
, __wend
), _M_cp()
5379 { _M_initialize(); }
5381 param_type(initializer_list
<double> __wil
)
5382 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
5383 { _M_initialize(); }
5385 template<typename _Func
>
5386 param_type(size_t __nw
, double __xmin
, double __xmax
,
5389 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5390 param_type(const param_type
&) = default;
5391 param_type
& operator=(const param_type
&) = default;
5394 probabilities() const
5395 { return _M_prob
.empty() ? std::vector
<double>(1, 1.0) : _M_prob
; }
5398 operator==(const param_type
& __p1
, const param_type
& __p2
)
5399 { return __p1
._M_prob
== __p2
._M_prob
; }
5401 #if __cpp_impl_three_way_comparison < 201907L
5403 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5404 { return !(__p1
== __p2
); }
5411 std::vector
<double> _M_prob
;
5412 std::vector
<double> _M_cp
;
5415 discrete_distribution()
5419 template<typename _InputIterator
>
5420 discrete_distribution(_InputIterator __wbegin
,
5421 _InputIterator __wend
)
5422 : _M_param(__wbegin
, __wend
)
5425 discrete_distribution(initializer_list
<double> __wl
)
5429 template<typename _Func
>
5430 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
5432 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5436 discrete_distribution(const param_type
& __p
)
5441 * @brief Resets the distribution state.
5448 * @brief Returns the probabilities of the distribution.
5451 probabilities() const
5453 return _M_param
._M_prob
.empty()
5454 ? std::vector
<double>(1, 1.0) : _M_param
._M_prob
;
5458 * @brief Returns the parameter set of the distribution.
5462 { return _M_param
; }
5465 * @brief Sets the parameter set of the distribution.
5466 * @param __param The new parameter set of the distribution.
5469 param(const param_type
& __param
)
5470 { _M_param
= __param
; }
5473 * @brief Returns the greatest lower bound value of the distribution.
5477 { return result_type(0); }
5480 * @brief Returns the least upper bound value of the distribution.
5485 return _M_param
._M_prob
.empty()
5486 ? result_type(0) : result_type(_M_param
._M_prob
.size() - 1);
5490 * @brief Generating functions.
5492 template<typename _UniformRandomNumberGenerator
>
5494 operator()(_UniformRandomNumberGenerator
& __urng
)
5495 { return this->operator()(__urng
, _M_param
); }
5497 template<typename _UniformRandomNumberGenerator
>
5499 operator()(_UniformRandomNumberGenerator
& __urng
,
5500 const param_type
& __p
);
5502 template<typename _ForwardIterator
,
5503 typename _UniformRandomNumberGenerator
>
5505 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5506 _UniformRandomNumberGenerator
& __urng
)
5507 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5509 template<typename _ForwardIterator
,
5510 typename _UniformRandomNumberGenerator
>
5512 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5513 _UniformRandomNumberGenerator
& __urng
,
5514 const param_type
& __p
)
5515 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5517 template<typename _UniformRandomNumberGenerator
>
5519 __generate(result_type
* __f
, result_type
* __t
,
5520 _UniformRandomNumberGenerator
& __urng
,
5521 const param_type
& __p
)
5522 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5525 * @brief Return true if two discrete distributions have the same
5529 operator==(const discrete_distribution
& __d1
,
5530 const discrete_distribution
& __d2
)
5531 { return __d1
._M_param
== __d2
._M_param
; }
5534 * @brief Inserts a %discrete_distribution random number distribution
5535 * @p __x into the output stream @p __os.
5537 * @param __os An output stream.
5538 * @param __x A %discrete_distribution random number distribution.
5540 * @returns The output stream with the state of @p __x inserted or in
5543 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5544 friend std::basic_ostream
<_CharT
, _Traits
>&
5545 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5546 const std::discrete_distribution
<_IntType1
>& __x
);
5549 * @brief Extracts a %discrete_distribution random number distribution
5550 * @p __x from the input stream @p __is.
5552 * @param __is An input stream.
5553 * @param __x A %discrete_distribution random number
5556 * @returns The input stream with @p __x extracted or in an error
5559 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5560 friend std::basic_istream
<_CharT
, _Traits
>&
5561 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5562 std::discrete_distribution
<_IntType1
>& __x
);
5565 template<typename _ForwardIterator
,
5566 typename _UniformRandomNumberGenerator
>
5568 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5569 _UniformRandomNumberGenerator
& __urng
,
5570 const param_type
& __p
);
5572 param_type _M_param
;
5575 #if __cpp_impl_three_way_comparison < 201907L
5577 * @brief Return true if two discrete distributions have different
5580 template<typename _IntType
>
5582 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
5583 const std::discrete_distribution
<_IntType
>& __d2
)
5584 { return !(__d1
== __d2
); }
5588 * @brief A piecewise_constant_distribution random number distribution.
5590 * The formula for the piecewise constant probability mass function is
5593 template<typename _RealType
= double>
5594 class piecewise_constant_distribution
5596 static_assert(std::is_floating_point
<_RealType
>::value
,
5597 "result_type must be a floating point type");
5600 /** The type of the range of the distribution. */
5601 typedef _RealType result_type
;
5603 /** Parameter type. */
5606 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
5607 friend class piecewise_constant_distribution
<_RealType
>;
5610 : _M_int(), _M_den(), _M_cp()
5613 template<typename _InputIteratorB
, typename _InputIteratorW
>
5614 param_type(_InputIteratorB __bfirst
,
5615 _InputIteratorB __bend
,
5616 _InputIteratorW __wbegin
);
5618 template<typename _Func
>
5619 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
5621 template<typename _Func
>
5622 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5625 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5626 param_type(const param_type
&) = default;
5627 param_type
& operator=(const param_type
&) = default;
5629 std::vector
<_RealType
>
5634 std::vector
<_RealType
> __tmp(2);
5635 __tmp
[1] = _RealType(1);
5644 { return _M_den
.empty() ? std::vector
<double>(1, 1.0) : _M_den
; }
5647 operator==(const param_type
& __p1
, const param_type
& __p2
)
5648 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
5650 #if __cpp_impl_three_way_comparison < 201907L
5652 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5653 { return !(__p1
== __p2
); }
5660 std::vector
<_RealType
> _M_int
;
5661 std::vector
<double> _M_den
;
5662 std::vector
<double> _M_cp
;
5665 piecewise_constant_distribution()
5669 template<typename _InputIteratorB
, typename _InputIteratorW
>
5670 piecewise_constant_distribution(_InputIteratorB __bfirst
,
5671 _InputIteratorB __bend
,
5672 _InputIteratorW __wbegin
)
5673 : _M_param(__bfirst
, __bend
, __wbegin
)
5676 template<typename _Func
>
5677 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
5679 : _M_param(__bl
, __fw
)
5682 template<typename _Func
>
5683 piecewise_constant_distribution(size_t __nw
,
5684 _RealType __xmin
, _RealType __xmax
,
5686 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5690 piecewise_constant_distribution(const param_type
& __p
)
5695 * @brief Resets the distribution state.
5702 * @brief Returns a vector of the intervals.
5704 std::vector
<_RealType
>
5707 if (_M_param
._M_int
.empty())
5709 std::vector
<_RealType
> __tmp(2);
5710 __tmp
[1] = _RealType(1);
5714 return _M_param
._M_int
;
5718 * @brief Returns a vector of the probability densities.
5723 return _M_param
._M_den
.empty()
5724 ? std::vector
<double>(1, 1.0) : _M_param
._M_den
;
5728 * @brief Returns the parameter set of the distribution.
5732 { return _M_param
; }
5735 * @brief Sets the parameter set of the distribution.
5736 * @param __param The new parameter set of the distribution.
5739 param(const param_type
& __param
)
5740 { _M_param
= __param
; }
5743 * @brief Returns the greatest lower bound value of the distribution.
5748 return _M_param
._M_int
.empty()
5749 ? result_type(0) : _M_param
._M_int
.front();
5753 * @brief Returns the least upper bound value of the distribution.
5758 return _M_param
._M_int
.empty()
5759 ? result_type(1) : _M_param
._M_int
.back();
5763 * @brief Generating functions.
5765 template<typename _UniformRandomNumberGenerator
>
5767 operator()(_UniformRandomNumberGenerator
& __urng
)
5768 { return this->operator()(__urng
, _M_param
); }
5770 template<typename _UniformRandomNumberGenerator
>
5772 operator()(_UniformRandomNumberGenerator
& __urng
,
5773 const param_type
& __p
);
5775 template<typename _ForwardIterator
,
5776 typename _UniformRandomNumberGenerator
>
5778 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5779 _UniformRandomNumberGenerator
& __urng
)
5780 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5782 template<typename _ForwardIterator
,
5783 typename _UniformRandomNumberGenerator
>
5785 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5786 _UniformRandomNumberGenerator
& __urng
,
5787 const param_type
& __p
)
5788 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5790 template<typename _UniformRandomNumberGenerator
>
5792 __generate(result_type
* __f
, result_type
* __t
,
5793 _UniformRandomNumberGenerator
& __urng
,
5794 const param_type
& __p
)
5795 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5798 * @brief Return true if two piecewise constant distributions have the
5802 operator==(const piecewise_constant_distribution
& __d1
,
5803 const piecewise_constant_distribution
& __d2
)
5804 { return __d1
._M_param
== __d2
._M_param
; }
5807 * @brief Inserts a %piecewise_constant_distribution random
5808 * number distribution @p __x into the output stream @p __os.
5810 * @param __os An output stream.
5811 * @param __x A %piecewise_constant_distribution random number
5814 * @returns The output stream with the state of @p __x inserted or in
5817 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5818 friend std::basic_ostream
<_CharT
, _Traits
>&
5819 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5820 const std::piecewise_constant_distribution
<_RealType1
>& __x
);
5823 * @brief Extracts a %piecewise_constant_distribution random
5824 * number distribution @p __x from the input stream @p __is.
5826 * @param __is An input stream.
5827 * @param __x A %piecewise_constant_distribution random number
5830 * @returns The input stream with @p __x extracted or in an error
5833 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5834 friend std::basic_istream
<_CharT
, _Traits
>&
5835 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5836 std::piecewise_constant_distribution
<_RealType1
>& __x
);
5839 template<typename _ForwardIterator
,
5840 typename _UniformRandomNumberGenerator
>
5842 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5843 _UniformRandomNumberGenerator
& __urng
,
5844 const param_type
& __p
);
5846 param_type _M_param
;
5849 #if __cpp_impl_three_way_comparison < 201907L
5851 * @brief Return true if two piecewise constant distributions have
5852 * different parameters.
5854 template<typename _RealType
>
5856 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5857 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5858 { return !(__d1
== __d2
); }
5862 * @brief A piecewise_linear_distribution random number distribution.
5864 * The formula for the piecewise linear probability mass function is
5867 template<typename _RealType
= double>
5868 class piecewise_linear_distribution
5870 static_assert(std::is_floating_point
<_RealType
>::value
,
5871 "result_type must be a floating point type");
5874 /** The type of the range of the distribution. */
5875 typedef _RealType result_type
;
5877 /** Parameter type. */
5880 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5881 friend class piecewise_linear_distribution
<_RealType
>;
5884 : _M_int(), _M_den(), _M_cp(), _M_m()
5887 template<typename _InputIteratorB
, typename _InputIteratorW
>
5888 param_type(_InputIteratorB __bfirst
,
5889 _InputIteratorB __bend
,
5890 _InputIteratorW __wbegin
);
5892 template<typename _Func
>
5893 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5895 template<typename _Func
>
5896 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5899 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5900 param_type(const param_type
&) = default;
5901 param_type
& operator=(const param_type
&) = default;
5903 std::vector
<_RealType
>
5908 std::vector
<_RealType
> __tmp(2);
5909 __tmp
[1] = _RealType(1);
5918 { return _M_den
.empty() ? std::vector
<double>(2, 1.0) : _M_den
; }
5921 operator==(const param_type
& __p1
, const param_type
& __p2
)
5922 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
5924 #if __cpp_impl_three_way_comparison < 201907L
5926 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5927 { return !(__p1
== __p2
); }
5934 std::vector
<_RealType
> _M_int
;
5935 std::vector
<double> _M_den
;
5936 std::vector
<double> _M_cp
;
5937 std::vector
<double> _M_m
;
5940 piecewise_linear_distribution()
5944 template<typename _InputIteratorB
, typename _InputIteratorW
>
5945 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5946 _InputIteratorB __bend
,
5947 _InputIteratorW __wbegin
)
5948 : _M_param(__bfirst
, __bend
, __wbegin
)
5951 template<typename _Func
>
5952 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5954 : _M_param(__bl
, __fw
)
5957 template<typename _Func
>
5958 piecewise_linear_distribution(size_t __nw
,
5959 _RealType __xmin
, _RealType __xmax
,
5961 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5965 piecewise_linear_distribution(const param_type
& __p
)
5970 * Resets the distribution state.
5977 * @brief Return the intervals of the distribution.
5979 std::vector
<_RealType
>
5982 if (_M_param
._M_int
.empty())
5984 std::vector
<_RealType
> __tmp(2);
5985 __tmp
[1] = _RealType(1);
5989 return _M_param
._M_int
;
5993 * @brief Return a vector of the probability densities of the
5999 return _M_param
._M_den
.empty()
6000 ? std::vector
<double>(2, 1.0) : _M_param
._M_den
;
6004 * @brief Returns the parameter set of the distribution.
6008 { return _M_param
; }
6011 * @brief Sets the parameter set of the distribution.
6012 * @param __param The new parameter set of the distribution.
6015 param(const param_type
& __param
)
6016 { _M_param
= __param
; }
6019 * @brief Returns the greatest lower bound value of the distribution.
6024 return _M_param
._M_int
.empty()
6025 ? result_type(0) : _M_param
._M_int
.front();
6029 * @brief Returns the least upper bound value of the distribution.
6034 return _M_param
._M_int
.empty()
6035 ? result_type(1) : _M_param
._M_int
.back();
6039 * @brief Generating functions.
6041 template<typename _UniformRandomNumberGenerator
>
6043 operator()(_UniformRandomNumberGenerator
& __urng
)
6044 { return this->operator()(__urng
, _M_param
); }
6046 template<typename _UniformRandomNumberGenerator
>
6048 operator()(_UniformRandomNumberGenerator
& __urng
,
6049 const param_type
& __p
);
6051 template<typename _ForwardIterator
,
6052 typename _UniformRandomNumberGenerator
>
6054 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
6055 _UniformRandomNumberGenerator
& __urng
)
6056 { this->__generate(__f
, __t
, __urng
, _M_param
); }
6058 template<typename _ForwardIterator
,
6059 typename _UniformRandomNumberGenerator
>
6061 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
6062 _UniformRandomNumberGenerator
& __urng
,
6063 const param_type
& __p
)
6064 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
6066 template<typename _UniformRandomNumberGenerator
>
6068 __generate(result_type
* __f
, result_type
* __t
,
6069 _UniformRandomNumberGenerator
& __urng
,
6070 const param_type
& __p
)
6071 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
6074 * @brief Return true if two piecewise linear distributions have the
6078 operator==(const piecewise_linear_distribution
& __d1
,
6079 const piecewise_linear_distribution
& __d2
)
6080 { return __d1
._M_param
== __d2
._M_param
; }
6083 * @brief Inserts a %piecewise_linear_distribution random number
6084 * distribution @p __x into the output stream @p __os.
6086 * @param __os An output stream.
6087 * @param __x A %piecewise_linear_distribution random number
6090 * @returns The output stream with the state of @p __x inserted or in
6093 template<typename _RealType1
, typename _CharT
, typename _Traits
>
6094 friend std::basic_ostream
<_CharT
, _Traits
>&
6095 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
6096 const std::piecewise_linear_distribution
<_RealType1
>& __x
);
6099 * @brief Extracts a %piecewise_linear_distribution random number
6100 * distribution @p __x from the input stream @p __is.
6102 * @param __is An input stream.
6103 * @param __x A %piecewise_linear_distribution random number
6106 * @returns The input stream with @p __x extracted or in an error
6109 template<typename _RealType1
, typename _CharT
, typename _Traits
>
6110 friend std::basic_istream
<_CharT
, _Traits
>&
6111 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
6112 std::piecewise_linear_distribution
<_RealType1
>& __x
);
6115 template<typename _ForwardIterator
,
6116 typename _UniformRandomNumberGenerator
>
6118 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
6119 _UniformRandomNumberGenerator
& __urng
,
6120 const param_type
& __p
);
6122 param_type _M_param
;
6125 #if __cpp_impl_three_way_comparison < 201907L
6127 * @brief Return true if two piecewise linear distributions have
6128 * different parameters.
6130 template<typename _RealType
>
6132 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
6133 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
6134 { return !(__d1
== __d2
); }
6137 /// @} group random_distributions_poisson
6139 /// @} *group random_distributions
6142 * @addtogroup random_utilities Random Number Utilities
6148 * @brief The seed_seq class generates sequences of seeds for random
6149 * number generators.
6154 /** The type of the seed vales. */
6155 typedef uint_least32_t result_type
;
6157 /** Default constructor. */
6162 template<typename _IntType
, typename
= _Require
<is_integral
<_IntType
>>>
6163 seed_seq(std::initializer_list
<_IntType
> __il
);
6165 template<typename _InputIterator
>
6166 seed_seq(_InputIterator __begin
, _InputIterator __end
);
6168 // generating functions
6169 template<typename _RandomAccessIterator
>
6171 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
6173 // property functions
6174 size_t size() const noexcept
6175 { return _M_v
.size(); }
6177 template<typename _OutputIterator
>
6179 param(_OutputIterator __dest
) const
6180 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
6182 // no copy functions
6183 seed_seq(const seed_seq
&) = delete;
6184 seed_seq
& operator=(const seed_seq
&) = delete;
6187 std::vector
<result_type
> _M_v
;
6190 /// @} group random_utilities
6194 _GLIBCXX_END_NAMESPACE_VERSION