1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009-2024 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$.
263 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
264 class linear_congruential_engine
266 static_assert(std::is_unsigned
<_UIntType
>::value
,
267 "result_type must be an unsigned integral type");
268 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
269 "template argument substituting __m out of bounds");
271 template<typename _Sseq
>
273 = __detail::_If_seed_seq_for
<_Sseq
, linear_congruential_engine
,
277 /** The type of the generated random value. */
278 typedef _UIntType result_type
;
280 /** The multiplier. */
281 static constexpr result_type multiplier
= __a
;
283 static constexpr result_type increment
= __c
;
285 static constexpr result_type modulus
= __m
;
286 static constexpr result_type default_seed
= 1u;
289 * @brief Constructs a %linear_congruential_engine random number
290 * generator engine with seed 1.
292 linear_congruential_engine() : linear_congruential_engine(default_seed
)
296 * @brief Constructs a %linear_congruential_engine random number
297 * generator engine with seed @p __s. The default seed value
300 * @param __s The initial seed value.
303 linear_congruential_engine(result_type __s
)
307 * @brief Constructs a %linear_congruential_engine random number
308 * generator engine seeded from the seed sequence @p __q.
310 * @param __q the seed sequence.
312 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
314 linear_congruential_engine(_Sseq
& __q
)
318 * @brief Reseeds the %linear_congruential_engine random number generator
319 * engine sequence to the seed @p __s.
321 * @param __s The new seed.
324 seed(result_type __s
= default_seed
);
327 * @brief Reseeds the %linear_congruential_engine random number generator
329 * sequence using values from the seed sequence @p __q.
331 * @param __q the seed sequence.
333 template<typename _Sseq
>
338 * @brief Gets the smallest possible value in the output range.
340 * The minimum depends on the @p __c parameter: if it is zero, the
341 * minimum generated must be > 0, otherwise 0 is allowed.
343 static constexpr result_type
345 { return __c
== 0u ? 1u : 0u; }
348 * @brief Gets the largest possible value in the output range.
350 static constexpr result_type
355 * @brief Discard a sequence of random numbers.
358 discard(unsigned long long __z
)
360 for (; __z
!= 0ULL; --__z
)
365 * @brief Gets the next random number in the sequence.
370 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
375 * @brief Compares two linear congruential random number generator
376 * objects of the same type for equality.
378 * @param __lhs A linear congruential random number generator object.
379 * @param __rhs Another linear congruential random number generator
382 * @returns true if the infinite sequences of generated values
383 * would be equal, false otherwise.
386 operator==(const linear_congruential_engine
& __lhs
,
387 const linear_congruential_engine
& __rhs
)
388 { return __lhs
._M_x
== __rhs
._M_x
; }
391 * @brief Writes the textual representation of the state x(i) of x to
394 * @param __os The output stream.
395 * @param __lcr A % linear_congruential_engine random number generator.
398 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
399 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
400 friend std::basic_ostream
<_CharT
, _Traits
>&
401 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
402 const std::linear_congruential_engine
<_UIntType1
,
403 __a1
, __c1
, __m1
>& __lcr
);
406 * @brief Sets the state of the engine by reading its textual
407 * representation from @p __is.
409 * The textual representation must have been previously written using
410 * an output stream whose imbued locale and whose type's template
411 * specialization arguments _CharT and _Traits were the same as those
414 * @param __is The input stream.
415 * @param __lcr A % linear_congruential_engine random number generator.
418 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
419 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
420 friend std::basic_istream
<_CharT
, _Traits
>&
421 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
422 std::linear_congruential_engine
<_UIntType1
, __a1
,
429 #if __cpp_impl_three_way_comparison < 201907L
431 * @brief Compares two linear congruential random number generator
432 * objects of the same type for inequality.
434 * @param __lhs A linear congruential random number generator object.
435 * @param __rhs Another linear congruential random number generator
438 * @returns true if the infinite sequences of generated values
439 * would be different, false otherwise.
441 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
443 operator!=(const std::linear_congruential_engine
<_UIntType
, __a
,
445 const std::linear_congruential_engine
<_UIntType
, __a
,
447 { return !(__lhs
== __rhs
); }
451 * A generalized feedback shift register discrete random number generator.
453 * This algorithm avoids multiplication and division and is designed to be
454 * friendly to a pipelined architecture. If the parameters are chosen
455 * correctly, this generator will produce numbers with a very long period and
456 * fairly good apparent entropy, although still not cryptographically strong.
458 * The best way to use this generator is with the predefined mt19937 class.
460 * This algorithm was originally invented by Makoto Matsumoto and
463 * @tparam __w Word size, the number of bits in each element of
465 * @tparam __n The degree of recursion.
466 * @tparam __m The period parameter.
467 * @tparam __r The separation point bit index.
468 * @tparam __a The last row of the twist matrix.
469 * @tparam __u The first right-shift tempering matrix parameter.
470 * @tparam __d The first right-shift tempering matrix mask.
471 * @tparam __s The first left-shift tempering matrix parameter.
472 * @tparam __b The first left-shift tempering matrix mask.
473 * @tparam __t The second left-shift tempering matrix parameter.
474 * @tparam __c The second left-shift tempering matrix mask.
475 * @tparam __l The second right-shift tempering matrix parameter.
476 * @tparam __f Initialization multiplier.
481 template<typename _UIntType
, size_t __w
,
482 size_t __n
, size_t __m
, size_t __r
,
483 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
484 _UIntType __b
, size_t __t
,
485 _UIntType __c
, size_t __l
, _UIntType __f
>
486 class mersenne_twister_engine
488 static_assert(std::is_unsigned
<_UIntType
>::value
,
489 "result_type must be an unsigned integral type");
490 static_assert(1u <= __m
&& __m
<= __n
,
491 "template argument substituting __m out of bounds");
492 static_assert(__r
<= __w
, "template argument substituting "
494 static_assert(__u
<= __w
, "template argument substituting "
496 static_assert(__s
<= __w
, "template argument substituting "
498 static_assert(__t
<= __w
, "template argument substituting "
500 static_assert(__l
<= __w
, "template argument substituting "
502 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
503 "template argument substituting __w out of bound");
504 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
505 "template argument substituting __a out of bound");
506 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
507 "template argument substituting __b out of bound");
508 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
509 "template argument substituting __c out of bound");
510 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
511 "template argument substituting __d out of bound");
512 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
513 "template argument substituting __f out of bound");
515 template<typename _Sseq
>
517 = __detail::_If_seed_seq_for
<_Sseq
, mersenne_twister_engine
,
521 /** The type of the generated random value. */
522 typedef _UIntType result_type
;
525 static constexpr size_t word_size
= __w
;
526 static constexpr size_t state_size
= __n
;
527 static constexpr size_t shift_size
= __m
;
528 static constexpr size_t mask_bits
= __r
;
529 static constexpr result_type xor_mask
= __a
;
530 static constexpr size_t tempering_u
= __u
;
531 static constexpr result_type tempering_d
= __d
;
532 static constexpr size_t tempering_s
= __s
;
533 static constexpr result_type tempering_b
= __b
;
534 static constexpr size_t tempering_t
= __t
;
535 static constexpr result_type tempering_c
= __c
;
536 static constexpr size_t tempering_l
= __l
;
537 static constexpr result_type initialization_multiplier
= __f
;
538 static constexpr result_type default_seed
= 5489u;
540 // constructors and member functions
542 mersenne_twister_engine() : mersenne_twister_engine(default_seed
) { }
545 mersenne_twister_engine(result_type __sd
)
549 * @brief Constructs a %mersenne_twister_engine random number generator
550 * engine seeded from the seed sequence @p __q.
552 * @param __q the seed sequence.
554 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
556 mersenne_twister_engine(_Sseq
& __q
)
560 seed(result_type __sd
= default_seed
);
562 template<typename _Sseq
>
567 * @brief Gets the smallest possible value in the output range.
569 static constexpr result_type
574 * @brief Gets the largest possible value in the output range.
576 static constexpr result_type
578 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
581 * @brief Discard a sequence of random numbers.
584 discard(unsigned long long __z
);
590 * @brief Compares two % mersenne_twister_engine random number generator
591 * objects of the same type for equality.
593 * @param __lhs A % mersenne_twister_engine random number generator
595 * @param __rhs Another % mersenne_twister_engine random number
598 * @returns true if the infinite sequences of generated values
599 * would be equal, false otherwise.
602 operator==(const mersenne_twister_engine
& __lhs
,
603 const mersenne_twister_engine
& __rhs
)
604 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
)
605 && __lhs
._M_p
== __rhs
._M_p
); }
608 * @brief Inserts the current state of a % mersenne_twister_engine
609 * random number generator engine @p __x into the output stream
612 * @param __os An output stream.
613 * @param __x A % mersenne_twister_engine random number generator
616 * @returns The output stream with the state of @p __x inserted or in
619 template<typename _UIntType1
,
620 size_t __w1
, size_t __n1
,
621 size_t __m1
, size_t __r1
,
622 _UIntType1 __a1
, size_t __u1
,
623 _UIntType1 __d1
, size_t __s1
,
624 _UIntType1 __b1
, size_t __t1
,
625 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
626 typename _CharT
, typename _Traits
>
627 friend std::basic_ostream
<_CharT
, _Traits
>&
628 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
629 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
630 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
634 * @brief Extracts the current state of a % mersenne_twister_engine
635 * random number generator engine @p __x from the input stream
638 * @param __is An input stream.
639 * @param __x A % mersenne_twister_engine random number generator
642 * @returns The input stream with the state of @p __x extracted or in
645 template<typename _UIntType1
,
646 size_t __w1
, size_t __n1
,
647 size_t __m1
, size_t __r1
,
648 _UIntType1 __a1
, size_t __u1
,
649 _UIntType1 __d1
, size_t __s1
,
650 _UIntType1 __b1
, size_t __t1
,
651 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
652 typename _CharT
, typename _Traits
>
653 friend std::basic_istream
<_CharT
, _Traits
>&
654 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
655 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
656 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
662 _UIntType _M_x
[state_size
];
666 #if __cpp_impl_three_way_comparison < 201907L
668 * @brief Compares two % mersenne_twister_engine random number generator
669 * objects of the same type for inequality.
671 * @param __lhs A % mersenne_twister_engine random number generator
673 * @param __rhs Another % mersenne_twister_engine random number
676 * @returns true if the infinite sequences of generated values
677 * would be different, false otherwise.
679 template<typename _UIntType
, size_t __w
,
680 size_t __n
, size_t __m
, size_t __r
,
681 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
682 _UIntType __b
, size_t __t
,
683 _UIntType __c
, size_t __l
, _UIntType __f
>
685 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
686 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
687 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
688 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
689 { return !(__lhs
== __rhs
); }
693 * @brief The Marsaglia-Zaman generator.
695 * This is a model of a Generalized Fibonacci discrete random number
696 * generator, sometimes referred to as the SWC generator.
698 * A discrete random number generator that produces pseudorandom
701 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
704 * The size of the state is @f$r@f$
705 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
710 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
711 class subtract_with_carry_engine
713 static_assert(std::is_unsigned
<_UIntType
>::value
,
714 "result_type must be an unsigned integral type");
715 static_assert(0u < __s
&& __s
< __r
,
717 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
718 "template argument substituting __w out of bounds");
720 template<typename _Sseq
>
722 = __detail::_If_seed_seq_for
<_Sseq
, subtract_with_carry_engine
,
726 /** The type of the generated random value. */
727 typedef _UIntType result_type
;
730 static constexpr size_t word_size
= __w
;
731 static constexpr size_t short_lag
= __s
;
732 static constexpr size_t long_lag
= __r
;
733 static constexpr uint_least32_t default_seed
= 19780503u;
735 subtract_with_carry_engine() : subtract_with_carry_engine(0u)
739 * @brief Constructs an explicitly seeded %subtract_with_carry_engine
740 * random number generator.
743 subtract_with_carry_engine(result_type __sd
)
747 * @brief Constructs a %subtract_with_carry_engine random number engine
748 * seeded from the seed sequence @p __q.
750 * @param __q the seed sequence.
752 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
754 subtract_with_carry_engine(_Sseq
& __q
)
758 * @brief Seeds the initial state @f$x_0@f$ of the random number
761 * N1688[4.19] modifies this as follows. If @p __value == 0,
762 * sets value to 19780503. In any case, with a linear
763 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
764 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
765 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
766 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
767 * set carry to 1, otherwise sets carry to 0.
770 seed(result_type __sd
= 0u);
773 * @brief Seeds the initial state @f$x_0@f$ of the
774 * % subtract_with_carry_engine random number generator.
776 template<typename _Sseq
>
781 * @brief Gets the inclusive minimum value of the range of random
782 * integers returned by this generator.
784 static constexpr result_type
789 * @brief Gets the inclusive maximum value of the range of random
790 * integers returned by this generator.
792 static constexpr result_type
794 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
797 * @brief Discard a sequence of random numbers.
800 discard(unsigned long long __z
)
802 for (; __z
!= 0ULL; --__z
)
807 * @brief Gets the next random number in the sequence.
813 * @brief Compares two % subtract_with_carry_engine random number
814 * generator objects of the same type for equality.
816 * @param __lhs A % subtract_with_carry_engine random number generator
818 * @param __rhs Another % subtract_with_carry_engine random number
821 * @returns true if the infinite sequences of generated values
822 * would be equal, false otherwise.
825 operator==(const subtract_with_carry_engine
& __lhs
,
826 const subtract_with_carry_engine
& __rhs
)
827 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
)
828 && __lhs
._M_carry
== __rhs
._M_carry
829 && __lhs
._M_p
== __rhs
._M_p
); }
832 * @brief Inserts the current state of a % subtract_with_carry_engine
833 * random number generator engine @p __x into the output stream
836 * @param __os An output stream.
837 * @param __x A % subtract_with_carry_engine random number generator
840 * @returns The output stream with the state of @p __x inserted or in
843 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
844 typename _CharT
, typename _Traits
>
845 friend std::basic_ostream
<_CharT
, _Traits
>&
846 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
847 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
851 * @brief Extracts the current state of a % subtract_with_carry_engine
852 * random number generator engine @p __x from the input stream
855 * @param __is An input stream.
856 * @param __x A % subtract_with_carry_engine random number generator
859 * @returns The input stream with the state of @p __x extracted or in
862 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
863 typename _CharT
, typename _Traits
>
864 friend std::basic_istream
<_CharT
, _Traits
>&
865 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
866 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
870 /// The state of the generator. This is a ring buffer.
871 _UIntType _M_x
[long_lag
];
872 _UIntType _M_carry
; ///< The carry
873 size_t _M_p
; ///< Current index of x(i - r).
876 #if __cpp_impl_three_way_comparison < 201907L
878 * @brief Compares two % subtract_with_carry_engine random number
879 * generator objects of the same type for inequality.
881 * @param __lhs A % subtract_with_carry_engine random number generator
883 * @param __rhs Another % subtract_with_carry_engine random number
886 * @returns true if the infinite sequences of generated values
887 * would be different, false otherwise.
889 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
891 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
893 const std::subtract_with_carry_engine
<_UIntType
, __w
,
895 { return !(__lhs
== __rhs
); }
899 * Produces random numbers from some base engine by discarding blocks of
902 * @pre @f$ 0 \leq r \leq p @f$
907 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
908 class discard_block_engine
910 static_assert(1 <= __r
&& __r
<= __p
,
911 "template argument substituting __r out of bounds");
914 /** The type of the generated random value. */
915 typedef typename
_RandomNumberEngine::result_type result_type
;
917 template<typename _Sseq
>
919 = __detail::_If_seed_seq_for
<_Sseq
, discard_block_engine
,
923 static constexpr size_t block_size
= __p
;
924 static constexpr size_t used_block
= __r
;
927 * @brief Constructs a default %discard_block_engine engine.
929 * The underlying engine is default constructed as well.
931 discard_block_engine()
932 : _M_b(), _M_n(0) { }
935 * @brief Copy constructs a %discard_block_engine engine.
937 * Copies an existing base class random number generator.
938 * @param __rng An existing (base class) engine object.
941 discard_block_engine(const _RandomNumberEngine
& __rng
)
942 : _M_b(__rng
), _M_n(0) { }
945 * @brief Move constructs a %discard_block_engine engine.
947 * Copies an existing base class random number generator.
948 * @param __rng An existing (base class) engine object.
951 discard_block_engine(_RandomNumberEngine
&& __rng
)
952 : _M_b(std::move(__rng
)), _M_n(0) { }
955 * @brief Seed constructs a %discard_block_engine engine.
957 * Constructs the underlying generator engine seeded with @p __s.
958 * @param __s A seed value for the base class engine.
961 discard_block_engine(result_type __s
)
962 : _M_b(__s
), _M_n(0) { }
965 * @brief Generator construct a %discard_block_engine engine.
967 * @param __q A seed sequence.
969 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
971 discard_block_engine(_Sseq
& __q
)
976 * @brief Reseeds the %discard_block_engine object with the default
977 * seed for the underlying base class generator engine.
987 * @brief Reseeds the %discard_block_engine object with the default
988 * seed for the underlying base class generator engine.
991 seed(result_type __s
)
998 * @brief Reseeds the %discard_block_engine object with the given seed
1000 * @param __q A seed generator function.
1002 template<typename _Sseq
>
1011 * @brief Gets a const reference to the underlying generator engine
1014 const _RandomNumberEngine
&
1015 base() const noexcept
1019 * @brief Gets the minimum value in the generated random number range.
1021 static constexpr result_type
1023 { return _RandomNumberEngine::min(); }
1026 * @brief Gets the maximum value in the generated random number range.
1028 static constexpr result_type
1030 { return _RandomNumberEngine::max(); }
1033 * @brief Discard a sequence of random numbers.
1036 discard(unsigned long long __z
)
1038 for (; __z
!= 0ULL; --__z
)
1043 * @brief Gets the next value in the generated random number sequence.
1049 * @brief Compares two %discard_block_engine random number generator
1050 * objects of the same type for equality.
1052 * @param __lhs A %discard_block_engine random number generator object.
1053 * @param __rhs Another %discard_block_engine random number generator
1056 * @returns true if the infinite sequences of generated values
1057 * would be equal, false otherwise.
1060 operator==(const discard_block_engine
& __lhs
,
1061 const discard_block_engine
& __rhs
)
1062 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
1065 * @brief Inserts the current state of a %discard_block_engine random
1066 * number generator engine @p __x into the output stream
1069 * @param __os An output stream.
1070 * @param __x A %discard_block_engine random number generator engine.
1072 * @returns The output stream with the state of @p __x inserted or in
1075 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1076 typename _CharT
, typename _Traits
>
1077 friend std::basic_ostream
<_CharT
, _Traits
>&
1078 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1079 const std::discard_block_engine
<_RandomNumberEngine1
,
1083 * @brief Extracts the current state of a % subtract_with_carry_engine
1084 * random number generator engine @p __x from the input stream
1087 * @param __is An input stream.
1088 * @param __x A %discard_block_engine random number generator engine.
1090 * @returns The input stream with the state of @p __x extracted or in
1093 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
1094 typename _CharT
, typename _Traits
>
1095 friend std::basic_istream
<_CharT
, _Traits
>&
1096 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1097 std::discard_block_engine
<_RandomNumberEngine1
,
1101 _RandomNumberEngine _M_b
;
1105 #if __cpp_impl_three_way_comparison < 201907L
1107 * @brief Compares two %discard_block_engine random number generator
1108 * objects of the same type for inequality.
1110 * @param __lhs A %discard_block_engine random number generator object.
1111 * @param __rhs Another %discard_block_engine random number generator
1114 * @returns true if the infinite sequences of generated values
1115 * would be different, false otherwise.
1117 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
1119 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1121 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1123 { return !(__lhs
== __rhs
); }
1127 * Produces random numbers by combining random numbers from some base
1128 * engine to produce random numbers with a specified number of bits @p __w.
1130 * @headerfile random
1133 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1134 class independent_bits_engine
1136 static_assert(std::is_unsigned
<_UIntType
>::value
,
1137 "result_type must be an unsigned integral type");
1138 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
1139 "template argument substituting __w out of bounds");
1141 template<typename _Sseq
>
1143 = __detail::_If_seed_seq_for
<_Sseq
, independent_bits_engine
,
1147 /** The type of the generated random value. */
1148 typedef _UIntType result_type
;
1151 * @brief Constructs a default %independent_bits_engine engine.
1153 * The underlying engine is default constructed as well.
1155 independent_bits_engine()
1159 * @brief Copy constructs a %independent_bits_engine engine.
1161 * Copies an existing base class random number generator.
1162 * @param __rng An existing (base class) engine object.
1165 independent_bits_engine(const _RandomNumberEngine
& __rng
)
1169 * @brief Move constructs a %independent_bits_engine engine.
1171 * Copies an existing base class random number generator.
1172 * @param __rng An existing (base class) engine object.
1175 independent_bits_engine(_RandomNumberEngine
&& __rng
)
1176 : _M_b(std::move(__rng
)) { }
1179 * @brief Seed constructs a %independent_bits_engine engine.
1181 * Constructs the underlying generator engine seeded with @p __s.
1182 * @param __s A seed value for the base class engine.
1185 independent_bits_engine(result_type __s
)
1189 * @brief Generator construct a %independent_bits_engine engine.
1191 * @param __q A seed sequence.
1193 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
1195 independent_bits_engine(_Sseq
& __q
)
1200 * @brief Reseeds the %independent_bits_engine object with the default
1201 * seed for the underlying base class generator engine.
1208 * @brief Reseeds the %independent_bits_engine object with the default
1209 * seed for the underlying base class generator engine.
1212 seed(result_type __s
)
1216 * @brief Reseeds the %independent_bits_engine object with the given
1218 * @param __q A seed generator function.
1220 template<typename _Sseq
>
1226 * @brief Gets a const reference to the underlying generator engine
1229 const _RandomNumberEngine
&
1230 base() const noexcept
1234 * @brief Gets the minimum value in the generated random number range.
1236 static constexpr result_type
1241 * @brief Gets the maximum value in the generated random number range.
1243 static constexpr result_type
1245 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1248 * @brief Discard a sequence of random numbers.
1251 discard(unsigned long long __z
)
1253 for (; __z
!= 0ULL; --__z
)
1258 * @brief Gets the next value in the generated random number sequence.
1264 * @brief Compares two %independent_bits_engine random number generator
1265 * objects of the same type for equality.
1267 * @param __lhs A %independent_bits_engine random number generator
1269 * @param __rhs Another %independent_bits_engine random number generator
1272 * @returns true if the infinite sequences of generated values
1273 * would be equal, false otherwise.
1276 operator==(const independent_bits_engine
& __lhs
,
1277 const independent_bits_engine
& __rhs
)
1278 { return __lhs
._M_b
== __rhs
._M_b
; }
1281 * @brief Extracts the current state of a % subtract_with_carry_engine
1282 * random number generator engine @p __x from the input stream
1285 * @param __is An input stream.
1286 * @param __x A %independent_bits_engine random number generator
1289 * @returns The input stream with the state of @p __x extracted or in
1292 template<typename _CharT
, typename _Traits
>
1293 friend std::basic_istream
<_CharT
, _Traits
>&
1294 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1295 std::independent_bits_engine
<_RandomNumberEngine
,
1296 __w
, _UIntType
>& __x
)
1303 _RandomNumberEngine _M_b
;
1306 #if __cpp_impl_three_way_comparison < 201907L
1308 * @brief Compares two %independent_bits_engine random number generator
1309 * objects of the same type for inequality.
1311 * @param __lhs A %independent_bits_engine random number generator
1313 * @param __rhs Another %independent_bits_engine random number generator
1316 * @returns true if the infinite sequences of generated values
1317 * would be different, false otherwise.
1319 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1321 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1323 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1325 { return !(__lhs
== __rhs
); }
1329 * @brief Inserts the current state of a %independent_bits_engine random
1330 * number generator engine @p __x into the output stream @p __os.
1332 * @param __os An output stream.
1333 * @param __x A %independent_bits_engine random number generator engine.
1335 * @returns The output stream with the state of @p __x inserted or in
1338 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1339 typename _CharT
, typename _Traits
>
1340 std::basic_ostream
<_CharT
, _Traits
>&
1341 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1342 const std::independent_bits_engine
<_RandomNumberEngine
,
1343 __w
, _UIntType
>& __x
)
1351 * @brief Produces random numbers by reordering random numbers from some
1354 * The values from the base engine are stored in a sequence of size @p __k
1355 * and shuffled by an algorithm that depends on those values.
1357 * @headerfile random
1360 template<typename _RandomNumberEngine
, size_t __k
>
1361 class shuffle_order_engine
1363 static_assert(1u <= __k
, "template argument substituting "
1364 "__k out of bound");
1367 /** The type of the generated random value. */
1368 typedef typename
_RandomNumberEngine::result_type result_type
;
1370 template<typename _Sseq
>
1372 = __detail::_If_seed_seq_for
<_Sseq
, shuffle_order_engine
,
1375 static constexpr size_t table_size
= __k
;
1378 * @brief Constructs a default %shuffle_order_engine engine.
1380 * The underlying engine is default constructed as well.
1382 shuffle_order_engine()
1384 { _M_initialize(); }
1387 * @brief Copy constructs a %shuffle_order_engine engine.
1389 * Copies an existing base class random number generator.
1390 * @param __rng An existing (base class) engine object.
1393 shuffle_order_engine(const _RandomNumberEngine
& __rng
)
1395 { _M_initialize(); }
1398 * @brief Move constructs a %shuffle_order_engine engine.
1400 * Copies an existing base class random number generator.
1401 * @param __rng An existing (base class) engine object.
1404 shuffle_order_engine(_RandomNumberEngine
&& __rng
)
1405 : _M_b(std::move(__rng
))
1406 { _M_initialize(); }
1409 * @brief Seed constructs a %shuffle_order_engine engine.
1411 * Constructs the underlying generator engine seeded with @p __s.
1412 * @param __s A seed value for the base class engine.
1415 shuffle_order_engine(result_type __s
)
1417 { _M_initialize(); }
1420 * @brief Generator construct a %shuffle_order_engine engine.
1422 * @param __q A seed sequence.
1424 template<typename _Sseq
, typename
= _If_seed_seq
<_Sseq
>>
1426 shuffle_order_engine(_Sseq
& __q
)
1428 { _M_initialize(); }
1431 * @brief Reseeds the %shuffle_order_engine object with the default seed
1432 for the underlying base class generator engine.
1442 * @brief Reseeds the %shuffle_order_engine object with the default seed
1443 * for the underlying base class generator engine.
1446 seed(result_type __s
)
1453 * @brief Reseeds the %shuffle_order_engine object with the given seed
1455 * @param __q A seed generator function.
1457 template<typename _Sseq
>
1466 * Gets a const reference to the underlying generator engine object.
1468 const _RandomNumberEngine
&
1469 base() const noexcept
1473 * Gets the minimum value in the generated random number range.
1475 static constexpr result_type
1477 { return _RandomNumberEngine::min(); }
1480 * Gets the maximum value in the generated random number range.
1482 static constexpr result_type
1484 { return _RandomNumberEngine::max(); }
1487 * Discard a sequence of random numbers.
1490 discard(unsigned long long __z
)
1492 for (; __z
!= 0ULL; --__z
)
1497 * Gets the next value in the generated random number sequence.
1503 * Compares two %shuffle_order_engine random number generator objects
1504 * of the same type for equality.
1506 * @param __lhs A %shuffle_order_engine random number generator object.
1507 * @param __rhs Another %shuffle_order_engine random number generator
1510 * @returns true if the infinite sequences of generated values
1511 * would be equal, false otherwise.
1514 operator==(const shuffle_order_engine
& __lhs
,
1515 const shuffle_order_engine
& __rhs
)
1516 { return (__lhs
._M_b
== __rhs
._M_b
1517 && std::equal(__lhs
._M_v
, __lhs
._M_v
+ __k
, __rhs
._M_v
)
1518 && __lhs
._M_y
== __rhs
._M_y
); }
1521 * @brief Inserts the current state of a %shuffle_order_engine random
1522 * number generator engine @p __x into the output stream
1525 * @param __os An output stream.
1526 * @param __x A %shuffle_order_engine random number generator engine.
1528 * @returns The output stream with the state of @p __x inserted or in
1531 template<typename _RandomNumberEngine1
, size_t __k1
,
1532 typename _CharT
, typename _Traits
>
1533 friend std::basic_ostream
<_CharT
, _Traits
>&
1534 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1535 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1539 * @brief Extracts the current state of a % subtract_with_carry_engine
1540 * random number generator engine @p __x from the input stream
1543 * @param __is An input stream.
1544 * @param __x A %shuffle_order_engine random number generator engine.
1546 * @returns The input stream with the state of @p __x extracted or in
1549 template<typename _RandomNumberEngine1
, size_t __k1
,
1550 typename _CharT
, typename _Traits
>
1551 friend std::basic_istream
<_CharT
, _Traits
>&
1552 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1553 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>& __x
);
1556 void _M_initialize()
1558 for (size_t __i
= 0; __i
< __k
; ++__i
)
1563 _RandomNumberEngine _M_b
;
1564 result_type _M_v
[__k
];
1568 #if __cpp_impl_three_way_comparison < 201907L
1570 * Compares two %shuffle_order_engine random number generator objects
1571 * of the same type for inequality.
1573 * @param __lhs A %shuffle_order_engine random number generator object.
1574 * @param __rhs Another %shuffle_order_engine random number generator
1577 * @returns true if the infinite sequences of generated values
1578 * would be different, false otherwise.
1580 template<typename _RandomNumberEngine
, size_t __k
>
1582 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1584 const std::shuffle_order_engine
<_RandomNumberEngine
,
1586 { return !(__lhs
== __rhs
); }
1590 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1592 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1596 * An alternative LCR (Lehmer Generator function).
1598 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1602 * The classic Mersenne Twister.
1605 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1606 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1607 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1609 typedef mersenne_twister_engine
<
1615 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1618 * An alternative Mersenne Twister.
1620 typedef mersenne_twister_engine
<
1623 0xb5026f5aa96619e9ULL
, 29,
1624 0x5555555555555555ULL
, 17,
1625 0x71d67fffeda60000ULL
, 37,
1626 0xfff7eee000000000ULL
, 43,
1627 6364136223846793005ULL> mt19937_64
;
1629 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1632 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1635 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1637 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1639 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1641 typedef minstd_rand0 default_random_engine
;
1644 * A standard interface to a platform-specific non-deterministic
1645 * random number generator (if any are available).
1647 * @headerfile random
1653 /** The type of the generated random value. */
1654 typedef unsigned int result_type
;
1656 // constructors, destructors and member functions
1658 random_device() { _M_init("default"); }
1661 random_device(const std::string
& __token
) { _M_init(__token
); }
1666 static constexpr result_type
1668 { return std::numeric_limits
<result_type
>::min(); }
1670 static constexpr result_type
1672 { return std::numeric_limits
<result_type
>::max(); }
1675 entropy() const noexcept
1676 { return this->_M_getentropy(); }
1680 { return this->_M_getval(); }
1682 // No copy functions.
1683 random_device(const random_device
&) = delete;
1684 void operator=(const random_device
&) = delete;
1688 void _M_init(const std::string
& __token
);
1689 void _M_init_pretr1(const std::string
& __token
);
1692 result_type
_M_getval();
1693 result_type
_M_getval_pretr1();
1694 double _M_getentropy() const noexcept
;
1696 void _M_init(const char*, size_t); // not exported from the shared library
1703 result_type (*_M_func
)(void*);
1710 /// @} group random_generators
1713 * @addtogroup random_distributions Random Number Distributions
1719 * @addtogroup random_distributions_uniform Uniform Distributions
1720 * @ingroup random_distributions
1724 // std::uniform_int_distribution is defined in <bits/uniform_int_dist.h>
1726 #if __cpp_impl_three_way_comparison < 201907L
1728 * @brief Return true if two uniform integer distributions have
1729 * different parameters.
1731 template<typename _IntType
>
1733 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1734 const std::uniform_int_distribution
<_IntType
>& __d2
)
1735 { return !(__d1
== __d2
); }
1739 * @brief Inserts a %uniform_int_distribution random number
1740 * distribution @p __x into the output stream @p os.
1742 * @param __os An output stream.
1743 * @param __x A %uniform_int_distribution random number distribution.
1745 * @returns The output stream with the state of @p __x inserted or in
1748 template<typename _IntType
, typename _CharT
, typename _Traits
>
1749 std::basic_ostream
<_CharT
, _Traits
>&
1750 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1751 const std::uniform_int_distribution
<_IntType
>&);
1754 * @brief Extracts a %uniform_int_distribution random number distribution
1755 * @p __x from the input stream @p __is.
1757 * @param __is An input stream.
1758 * @param __x A %uniform_int_distribution random number generator engine.
1760 * @returns The input stream with @p __x extracted or in an error state.
1762 template<typename _IntType
, typename _CharT
, typename _Traits
>
1763 std::basic_istream
<_CharT
, _Traits
>&
1764 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1765 std::uniform_int_distribution
<_IntType
>&);
1769 * @brief Uniform continuous distribution for random numbers.
1771 * A continuous random distribution on the range [min, max) with equal
1772 * probability throughout the range. The URNG should be real-valued and
1773 * deliver number in the range [0, 1).
1775 * @headerfile random
1778 template<typename _RealType
= double>
1779 class uniform_real_distribution
1781 static_assert(std::is_floating_point
<_RealType
>::value
,
1782 "result_type must be a floating point type");
1785 /** The type of the range of the distribution. */
1786 typedef _RealType result_type
;
1788 /** Parameter type. */
1791 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1793 param_type() : param_type(0) { }
1796 param_type(_RealType __a
, _RealType __b
= _RealType(1))
1797 : _M_a(__a
), _M_b(__b
)
1799 __glibcxx_assert(_M_a
<= _M_b
);
1811 operator==(const param_type
& __p1
, const param_type
& __p2
)
1812 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1814 #if __cpp_impl_three_way_comparison < 201907L
1816 operator!=(const param_type
& __p1
, const param_type
& __p2
)
1817 { return !(__p1
== __p2
); }
1827 * @brief Constructs a uniform_real_distribution object.
1829 * The lower bound is set to 0.0 and the upper bound to 1.0
1831 uniform_real_distribution() : uniform_real_distribution(0.0) { }
1834 * @brief Constructs a uniform_real_distribution object.
1836 * @param __a [IN] The lower bound of the distribution.
1837 * @param __b [IN] The upper bound of the distribution.
1840 uniform_real_distribution(_RealType __a
, _RealType __b
= _RealType(1))
1841 : _M_param(__a
, __b
)
1845 uniform_real_distribution(const param_type
& __p
)
1850 * @brief Resets the distribution state.
1852 * Does nothing for the uniform real distribution.
1859 { return _M_param
.a(); }
1863 { return _M_param
.b(); }
1866 * @brief Returns the parameter set of the distribution.
1870 { return _M_param
; }
1873 * @brief Sets the parameter set of the distribution.
1874 * @param __param The new parameter set of the distribution.
1877 param(const param_type
& __param
)
1878 { _M_param
= __param
; }
1881 * @brief Returns the inclusive lower bound of the distribution range.
1885 { return this->a(); }
1888 * @brief Returns the inclusive upper bound of the distribution range.
1892 { return this->b(); }
1895 * @brief Generating functions.
1897 template<typename _UniformRandomNumberGenerator
>
1899 operator()(_UniformRandomNumberGenerator
& __urng
)
1900 { return this->operator()(__urng
, _M_param
); }
1902 template<typename _UniformRandomNumberGenerator
>
1904 operator()(_UniformRandomNumberGenerator
& __urng
,
1905 const param_type
& __p
)
1907 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1909 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1912 template<typename _ForwardIterator
,
1913 typename _UniformRandomNumberGenerator
>
1915 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1916 _UniformRandomNumberGenerator
& __urng
)
1917 { this->__generate(__f
, __t
, __urng
, _M_param
); }
1919 template<typename _ForwardIterator
,
1920 typename _UniformRandomNumberGenerator
>
1922 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
1923 _UniformRandomNumberGenerator
& __urng
,
1924 const param_type
& __p
)
1925 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1927 template<typename _UniformRandomNumberGenerator
>
1929 __generate(result_type
* __f
, result_type
* __t
,
1930 _UniformRandomNumberGenerator
& __urng
,
1931 const param_type
& __p
)
1932 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
1935 * @brief Return true if two uniform real distributions have
1936 * the same parameters.
1939 operator==(const uniform_real_distribution
& __d1
,
1940 const uniform_real_distribution
& __d2
)
1941 { return __d1
._M_param
== __d2
._M_param
; }
1944 template<typename _ForwardIterator
,
1945 typename _UniformRandomNumberGenerator
>
1947 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
1948 _UniformRandomNumberGenerator
& __urng
,
1949 const param_type
& __p
);
1951 param_type _M_param
;
1954 #if __cpp_impl_three_way_comparison < 201907L
1956 * @brief Return true if two uniform real distributions have
1957 * different parameters.
1959 template<typename _IntType
>
1961 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1962 const std::uniform_real_distribution
<_IntType
>& __d2
)
1963 { return !(__d1
== __d2
); }
1967 * @brief Inserts a %uniform_real_distribution random number
1968 * distribution @p __x into the output stream @p __os.
1970 * @param __os An output stream.
1971 * @param __x A %uniform_real_distribution random number distribution.
1973 * @returns The output stream with the state of @p __x inserted or in
1976 template<typename _RealType
, typename _CharT
, typename _Traits
>
1977 std::basic_ostream
<_CharT
, _Traits
>&
1978 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1979 const std::uniform_real_distribution
<_RealType
>&);
1982 * @brief Extracts a %uniform_real_distribution random number distribution
1983 * @p __x from the input stream @p __is.
1985 * @param __is An input stream.
1986 * @param __x A %uniform_real_distribution random number generator engine.
1988 * @returns The input stream with @p __x extracted or in an error state.
1990 template<typename _RealType
, typename _CharT
, typename _Traits
>
1991 std::basic_istream
<_CharT
, _Traits
>&
1992 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1993 std::uniform_real_distribution
<_RealType
>&);
1995 /// @} group random_distributions_uniform
1998 * @addtogroup random_distributions_normal Normal Distributions
1999 * @ingroup random_distributions
2004 * @brief A normal continuous distribution for random numbers.
2006 * The formula for the normal probability density function is
2008 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
2009 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
2012 * @headerfile random
2015 template<typename _RealType
= double>
2016 class normal_distribution
2018 static_assert(std::is_floating_point
<_RealType
>::value
,
2019 "result_type must be a floating point type");
2022 /** The type of the range of the distribution. */
2023 typedef _RealType result_type
;
2025 /** Parameter type. */
2028 typedef normal_distribution
<_RealType
> distribution_type
;
2030 param_type() : param_type(0.0) { }
2033 param_type(_RealType __mean
, _RealType __stddev
= _RealType(1))
2034 : _M_mean(__mean
), _M_stddev(__stddev
)
2036 __glibcxx_assert(_M_stddev
> _RealType(0));
2045 { return _M_stddev
; }
2048 operator==(const param_type
& __p1
, const param_type
& __p2
)
2049 { return (__p1
._M_mean
== __p2
._M_mean
2050 && __p1
._M_stddev
== __p2
._M_stddev
); }
2052 #if __cpp_impl_three_way_comparison < 201907L
2054 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2055 { return !(__p1
== __p2
); }
2060 _RealType _M_stddev
;
2064 normal_distribution() : normal_distribution(0.0) { }
2067 * Constructs a normal distribution with parameters @f$mean@f$ and
2068 * standard deviation.
2071 normal_distribution(result_type __mean
,
2072 result_type __stddev
= result_type(1))
2073 : _M_param(__mean
, __stddev
)
2077 normal_distribution(const param_type
& __p
)
2082 * @brief Resets the distribution state.
2086 { _M_saved_available
= false; }
2089 * @brief Returns the mean of the distribution.
2093 { return _M_param
.mean(); }
2096 * @brief Returns the standard deviation of the distribution.
2100 { return _M_param
.stddev(); }
2103 * @brief Returns the parameter set of the distribution.
2107 { return _M_param
; }
2110 * @brief Sets the parameter set of the distribution.
2111 * @param __param The new parameter set of the distribution.
2114 param(const param_type
& __param
)
2115 { _M_param
= __param
; }
2118 * @brief Returns the greatest lower bound value of the distribution.
2122 { return std::numeric_limits
<result_type
>::lowest(); }
2125 * @brief Returns the least upper bound value of the distribution.
2129 { return std::numeric_limits
<result_type
>::max(); }
2132 * @brief Generating functions.
2134 template<typename _UniformRandomNumberGenerator
>
2136 operator()(_UniformRandomNumberGenerator
& __urng
)
2137 { return this->operator()(__urng
, _M_param
); }
2139 template<typename _UniformRandomNumberGenerator
>
2141 operator()(_UniformRandomNumberGenerator
& __urng
,
2142 const param_type
& __p
);
2144 template<typename _ForwardIterator
,
2145 typename _UniformRandomNumberGenerator
>
2147 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2148 _UniformRandomNumberGenerator
& __urng
)
2149 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2151 template<typename _ForwardIterator
,
2152 typename _UniformRandomNumberGenerator
>
2154 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2155 _UniformRandomNumberGenerator
& __urng
,
2156 const param_type
& __p
)
2157 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2159 template<typename _UniformRandomNumberGenerator
>
2161 __generate(result_type
* __f
, result_type
* __t
,
2162 _UniformRandomNumberGenerator
& __urng
,
2163 const param_type
& __p
)
2164 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2167 * @brief Return true if two normal distributions have
2168 * the same parameters and the sequences that would
2169 * be generated are equal.
2171 template<typename _RealType1
>
2173 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2174 const std::normal_distribution
<_RealType1
>& __d2
);
2177 * @brief Inserts a %normal_distribution random number distribution
2178 * @p __x into the output stream @p __os.
2180 * @param __os An output stream.
2181 * @param __x A %normal_distribution random number distribution.
2183 * @returns The output stream with the state of @p __x inserted or in
2186 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2187 friend std::basic_ostream
<_CharT
, _Traits
>&
2188 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2189 const std::normal_distribution
<_RealType1
>& __x
);
2192 * @brief Extracts a %normal_distribution random number distribution
2193 * @p __x from the input stream @p __is.
2195 * @param __is An input stream.
2196 * @param __x A %normal_distribution random number generator engine.
2198 * @returns The input stream with @p __x extracted or in an error
2201 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2202 friend std::basic_istream
<_CharT
, _Traits
>&
2203 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2204 std::normal_distribution
<_RealType1
>& __x
);
2207 template<typename _ForwardIterator
,
2208 typename _UniformRandomNumberGenerator
>
2210 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2211 _UniformRandomNumberGenerator
& __urng
,
2212 const param_type
& __p
);
2214 param_type _M_param
;
2215 result_type _M_saved
= 0;
2216 bool _M_saved_available
= false;
2219 #if __cpp_impl_three_way_comparison < 201907L
2221 * @brief Return true if two normal distributions are different.
2223 template<typename _RealType
>
2225 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2226 const std::normal_distribution
<_RealType
>& __d2
)
2227 { return !(__d1
== __d2
); }
2231 * @brief A lognormal_distribution random number distribution.
2233 * The formula for the normal probability mass function is
2235 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2236 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2239 * @headerfile random
2242 template<typename _RealType
= double>
2243 class lognormal_distribution
2245 static_assert(std::is_floating_point
<_RealType
>::value
,
2246 "result_type must be a floating point type");
2249 /** The type of the range of the distribution. */
2250 typedef _RealType result_type
;
2252 /** Parameter type. */
2255 typedef lognormal_distribution
<_RealType
> distribution_type
;
2257 param_type() : param_type(0.0) { }
2260 param_type(_RealType __m
, _RealType __s
= _RealType(1))
2261 : _M_m(__m
), _M_s(__s
)
2273 operator==(const param_type
& __p1
, const param_type
& __p2
)
2274 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2276 #if __cpp_impl_three_way_comparison < 201907L
2278 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2279 { return !(__p1
== __p2
); }
2287 lognormal_distribution() : lognormal_distribution(0.0) { }
2290 lognormal_distribution(_RealType __m
, _RealType __s
= _RealType(1))
2291 : _M_param(__m
, __s
), _M_nd()
2295 lognormal_distribution(const param_type
& __p
)
2296 : _M_param(__p
), _M_nd()
2300 * Resets the distribution state.
2311 { return _M_param
.m(); }
2315 { return _M_param
.s(); }
2318 * @brief Returns the parameter set of the distribution.
2322 { return _M_param
; }
2325 * @brief Sets the parameter set of the distribution.
2326 * @param __param The new parameter set of the distribution.
2329 param(const param_type
& __param
)
2330 { _M_param
= __param
; }
2333 * @brief Returns the greatest lower bound value of the distribution.
2337 { return result_type(0); }
2340 * @brief Returns the least upper bound value of the distribution.
2344 { return std::numeric_limits
<result_type
>::max(); }
2347 * @brief Generating functions.
2349 template<typename _UniformRandomNumberGenerator
>
2351 operator()(_UniformRandomNumberGenerator
& __urng
)
2352 { return this->operator()(__urng
, _M_param
); }
2354 template<typename _UniformRandomNumberGenerator
>
2356 operator()(_UniformRandomNumberGenerator
& __urng
,
2357 const param_type
& __p
)
2358 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2360 template<typename _ForwardIterator
,
2361 typename _UniformRandomNumberGenerator
>
2363 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2364 _UniformRandomNumberGenerator
& __urng
)
2365 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2367 template<typename _ForwardIterator
,
2368 typename _UniformRandomNumberGenerator
>
2370 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2371 _UniformRandomNumberGenerator
& __urng
,
2372 const param_type
& __p
)
2373 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2375 template<typename _UniformRandomNumberGenerator
>
2377 __generate(result_type
* __f
, result_type
* __t
,
2378 _UniformRandomNumberGenerator
& __urng
,
2379 const param_type
& __p
)
2380 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2383 * @brief Return true if two lognormal distributions have
2384 * the same parameters and the sequences that would
2385 * be generated are equal.
2388 operator==(const lognormal_distribution
& __d1
,
2389 const lognormal_distribution
& __d2
)
2390 { return (__d1
._M_param
== __d2
._M_param
2391 && __d1
._M_nd
== __d2
._M_nd
); }
2394 * @brief Inserts a %lognormal_distribution random number distribution
2395 * @p __x into the output stream @p __os.
2397 * @param __os An output stream.
2398 * @param __x A %lognormal_distribution random number distribution.
2400 * @returns The output stream with the state of @p __x inserted or in
2403 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2404 friend std::basic_ostream
<_CharT
, _Traits
>&
2405 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2406 const std::lognormal_distribution
<_RealType1
>& __x
);
2409 * @brief Extracts a %lognormal_distribution random number distribution
2410 * @p __x from the input stream @p __is.
2412 * @param __is An input stream.
2413 * @param __x A %lognormal_distribution random number
2416 * @returns The input stream with @p __x extracted or in an error state.
2418 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2419 friend std::basic_istream
<_CharT
, _Traits
>&
2420 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2421 std::lognormal_distribution
<_RealType1
>& __x
);
2424 template<typename _ForwardIterator
,
2425 typename _UniformRandomNumberGenerator
>
2427 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2428 _UniformRandomNumberGenerator
& __urng
,
2429 const param_type
& __p
);
2431 param_type _M_param
;
2433 std::normal_distribution
<result_type
> _M_nd
;
2436 #if __cpp_impl_three_way_comparison < 201907L
2438 * @brief Return true if two lognormal distributions are different.
2440 template<typename _RealType
>
2442 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2443 const std::lognormal_distribution
<_RealType
>& __d2
)
2444 { return !(__d1
== __d2
); }
2447 /// @} group random_distributions_normal
2450 * @addtogroup random_distributions_poisson Poisson Distributions
2451 * @ingroup random_distributions
2456 * @brief A gamma continuous distribution for random numbers.
2458 * The formula for the gamma probability density function is:
2460 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2461 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2464 * @headerfile random
2467 template<typename _RealType
= double>
2468 class gamma_distribution
2470 static_assert(std::is_floating_point
<_RealType
>::value
,
2471 "result_type must be a floating point type");
2474 /** The type of the range of the distribution. */
2475 typedef _RealType result_type
;
2477 /** Parameter type. */
2480 typedef gamma_distribution
<_RealType
> distribution_type
;
2481 friend class gamma_distribution
<_RealType
>;
2483 param_type() : param_type(1.0) { }
2486 param_type(_RealType __alpha_val
, _RealType __beta_val
= _RealType(1))
2487 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2489 __glibcxx_assert(_M_alpha
> _RealType(0));
2495 { return _M_alpha
; }
2502 operator==(const param_type
& __p1
, const param_type
& __p2
)
2503 { return (__p1
._M_alpha
== __p2
._M_alpha
2504 && __p1
._M_beta
== __p2
._M_beta
); }
2506 #if __cpp_impl_three_way_comparison < 201907L
2508 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2509 { return !(__p1
== __p2
); }
2519 _RealType _M_malpha
, _M_a2
;
2524 * @brief Constructs a gamma distribution with parameters 1 and 1.
2526 gamma_distribution() : gamma_distribution(1.0) { }
2529 * @brief Constructs a gamma distribution with parameters
2530 * @f$\alpha@f$ and @f$\beta@f$.
2533 gamma_distribution(_RealType __alpha_val
,
2534 _RealType __beta_val
= _RealType(1))
2535 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2539 gamma_distribution(const param_type
& __p
)
2540 : _M_param(__p
), _M_nd()
2544 * @brief Resets the distribution state.
2551 * @brief Returns the @f$\alpha@f$ of the distribution.
2555 { return _M_param
.alpha(); }
2558 * @brief Returns the @f$\beta@f$ of the distribution.
2562 { return _M_param
.beta(); }
2565 * @brief Returns the parameter set of the distribution.
2569 { return _M_param
; }
2572 * @brief Sets the parameter set of the distribution.
2573 * @param __param The new parameter set of the distribution.
2576 param(const param_type
& __param
)
2577 { _M_param
= __param
; }
2580 * @brief Returns the greatest lower bound value of the distribution.
2584 { return result_type(0); }
2587 * @brief Returns the least upper bound value of the distribution.
2591 { return std::numeric_limits
<result_type
>::max(); }
2594 * @brief Generating functions.
2596 template<typename _UniformRandomNumberGenerator
>
2598 operator()(_UniformRandomNumberGenerator
& __urng
)
2599 { return this->operator()(__urng
, _M_param
); }
2601 template<typename _UniformRandomNumberGenerator
>
2603 operator()(_UniformRandomNumberGenerator
& __urng
,
2604 const param_type
& __p
);
2606 template<typename _ForwardIterator
,
2607 typename _UniformRandomNumberGenerator
>
2609 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2610 _UniformRandomNumberGenerator
& __urng
)
2611 { this->__generate(__f
, __t
, __urng
, _M_param
); }
2613 template<typename _ForwardIterator
,
2614 typename _UniformRandomNumberGenerator
>
2616 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2617 _UniformRandomNumberGenerator
& __urng
,
2618 const param_type
& __p
)
2619 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2621 template<typename _UniformRandomNumberGenerator
>
2623 __generate(result_type
* __f
, result_type
* __t
,
2624 _UniformRandomNumberGenerator
& __urng
,
2625 const param_type
& __p
)
2626 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
2629 * @brief Return true if two gamma distributions have the same
2630 * parameters and the sequences that would be generated
2634 operator==(const gamma_distribution
& __d1
,
2635 const gamma_distribution
& __d2
)
2636 { return (__d1
._M_param
== __d2
._M_param
2637 && __d1
._M_nd
== __d2
._M_nd
); }
2640 * @brief Inserts a %gamma_distribution random number distribution
2641 * @p __x into the output stream @p __os.
2643 * @param __os An output stream.
2644 * @param __x A %gamma_distribution random number distribution.
2646 * @returns The output stream with the state of @p __x inserted or in
2649 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2650 friend std::basic_ostream
<_CharT
, _Traits
>&
2651 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2652 const std::gamma_distribution
<_RealType1
>& __x
);
2655 * @brief Extracts a %gamma_distribution random number distribution
2656 * @p __x from the input stream @p __is.
2658 * @param __is An input stream.
2659 * @param __x A %gamma_distribution random number generator engine.
2661 * @returns The input stream with @p __x extracted or in an error state.
2663 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2664 friend std::basic_istream
<_CharT
, _Traits
>&
2665 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2666 std::gamma_distribution
<_RealType1
>& __x
);
2669 template<typename _ForwardIterator
,
2670 typename _UniformRandomNumberGenerator
>
2672 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2673 _UniformRandomNumberGenerator
& __urng
,
2674 const param_type
& __p
);
2676 param_type _M_param
;
2678 std::normal_distribution
<result_type
> _M_nd
;
2681 #if __cpp_impl_three_way_comparison < 201907L
2683 * @brief Return true if two gamma distributions are different.
2685 template<typename _RealType
>
2687 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2688 const std::gamma_distribution
<_RealType
>& __d2
)
2689 { return !(__d1
== __d2
); }
2692 /// @} group random_distributions_poisson
2695 * @addtogroup random_distributions_normal Normal Distributions
2696 * @ingroup random_distributions
2701 * @brief A chi_squared_distribution random number distribution.
2703 * The formula for the normal probability mass function is
2704 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2706 * @headerfile random
2709 template<typename _RealType
= double>
2710 class chi_squared_distribution
2712 static_assert(std::is_floating_point
<_RealType
>::value
,
2713 "result_type must be a floating point type");
2716 /** The type of the range of the distribution. */
2717 typedef _RealType result_type
;
2719 /** Parameter type. */
2722 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2724 param_type() : param_type(1) { }
2727 param_type(_RealType __n
)
2736 operator==(const param_type
& __p1
, const param_type
& __p2
)
2737 { return __p1
._M_n
== __p2
._M_n
; }
2739 #if __cpp_impl_three_way_comparison < 201907L
2741 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2742 { return !(__p1
== __p2
); }
2749 chi_squared_distribution() : chi_squared_distribution(1) { }
2752 chi_squared_distribution(_RealType __n
)
2753 : _M_param(__n
), _M_gd(__n
/ 2)
2757 chi_squared_distribution(const param_type
& __p
)
2758 : _M_param(__p
), _M_gd(__p
.n() / 2)
2762 * @brief Resets the distribution state.
2773 { return _M_param
.n(); }
2776 * @brief Returns the parameter set of the distribution.
2780 { return _M_param
; }
2783 * @brief Sets the parameter set of the distribution.
2784 * @param __param The new parameter set of the distribution.
2787 param(const param_type
& __param
)
2790 typedef typename
std::gamma_distribution
<result_type
>::param_type
2792 _M_gd
.param(param_type
{__param
.n() / 2});
2796 * @brief Returns the greatest lower bound value of the distribution.
2800 { return result_type(0); }
2803 * @brief Returns the least upper bound value of the distribution.
2807 { return std::numeric_limits
<result_type
>::max(); }
2810 * @brief Generating functions.
2812 template<typename _UniformRandomNumberGenerator
>
2814 operator()(_UniformRandomNumberGenerator
& __urng
)
2815 { return 2 * _M_gd(__urng
); }
2817 template<typename _UniformRandomNumberGenerator
>
2819 operator()(_UniformRandomNumberGenerator
& __urng
,
2820 const param_type
& __p
)
2822 typedef typename
std::gamma_distribution
<result_type
>::param_type
2824 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2827 template<typename _ForwardIterator
,
2828 typename _UniformRandomNumberGenerator
>
2830 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2831 _UniformRandomNumberGenerator
& __urng
)
2832 { this->__generate_impl(__f
, __t
, __urng
); }
2834 template<typename _ForwardIterator
,
2835 typename _UniformRandomNumberGenerator
>
2837 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
2838 _UniformRandomNumberGenerator
& __urng
,
2839 const param_type
& __p
)
2840 { typename
std::gamma_distribution
<result_type
>::param_type
2842 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2844 template<typename _UniformRandomNumberGenerator
>
2846 __generate(result_type
* __f
, result_type
* __t
,
2847 _UniformRandomNumberGenerator
& __urng
)
2848 { this->__generate_impl(__f
, __t
, __urng
); }
2850 template<typename _UniformRandomNumberGenerator
>
2852 __generate(result_type
* __f
, result_type
* __t
,
2853 _UniformRandomNumberGenerator
& __urng
,
2854 const param_type
& __p
)
2855 { typename
std::gamma_distribution
<result_type
>::param_type
2857 this->__generate_impl(__f
, __t
, __urng
, __p2
); }
2860 * @brief Return true if two Chi-squared distributions have
2861 * the same parameters and the sequences that would be
2862 * generated are equal.
2865 operator==(const chi_squared_distribution
& __d1
,
2866 const chi_squared_distribution
& __d2
)
2867 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
2870 * @brief Inserts a %chi_squared_distribution random number distribution
2871 * @p __x into the output stream @p __os.
2873 * @param __os An output stream.
2874 * @param __x A %chi_squared_distribution random number distribution.
2876 * @returns The output stream with the state of @p __x inserted or in
2879 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2880 friend std::basic_ostream
<_CharT
, _Traits
>&
2881 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2882 const std::chi_squared_distribution
<_RealType1
>& __x
);
2885 * @brief Extracts a %chi_squared_distribution random number distribution
2886 * @p __x from the input stream @p __is.
2888 * @param __is An input stream.
2889 * @param __x A %chi_squared_distribution random number
2892 * @returns The input stream with @p __x extracted or in an error state.
2894 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2895 friend std::basic_istream
<_CharT
, _Traits
>&
2896 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2897 std::chi_squared_distribution
<_RealType1
>& __x
);
2900 template<typename _ForwardIterator
,
2901 typename _UniformRandomNumberGenerator
>
2903 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2904 _UniformRandomNumberGenerator
& __urng
);
2906 template<typename _ForwardIterator
,
2907 typename _UniformRandomNumberGenerator
>
2909 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
2910 _UniformRandomNumberGenerator
& __urng
,
2912 std::gamma_distribution
<result_type
>::param_type
& __p
);
2914 param_type _M_param
;
2916 std::gamma_distribution
<result_type
> _M_gd
;
2919 #if __cpp_impl_three_way_comparison < 201907L
2921 * @brief Return true if two Chi-squared distributions are different.
2923 template<typename _RealType
>
2925 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2926 const std::chi_squared_distribution
<_RealType
>& __d2
)
2927 { return !(__d1
== __d2
); }
2931 * @brief A cauchy_distribution random number distribution.
2933 * The formula for the normal probability mass function is
2934 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2936 * @headerfile random
2939 template<typename _RealType
= double>
2940 class cauchy_distribution
2942 static_assert(std::is_floating_point
<_RealType
>::value
,
2943 "result_type must be a floating point type");
2946 /** The type of the range of the distribution. */
2947 typedef _RealType result_type
;
2949 /** Parameter type. */
2952 typedef cauchy_distribution
<_RealType
> distribution_type
;
2954 param_type() : param_type(0) { }
2957 param_type(_RealType __a
, _RealType __b
= _RealType(1))
2958 : _M_a(__a
), _M_b(__b
)
2970 operator==(const param_type
& __p1
, const param_type
& __p2
)
2971 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2973 #if __cpp_impl_three_way_comparison < 201907L
2975 operator!=(const param_type
& __p1
, const param_type
& __p2
)
2976 { return !(__p1
== __p2
); }
2984 cauchy_distribution() : cauchy_distribution(0.0) { }
2987 cauchy_distribution(_RealType __a
, _RealType __b
= 1.0)
2988 : _M_param(__a
, __b
)
2992 cauchy_distribution(const param_type
& __p
)
2997 * @brief Resets the distribution state.
3008 { return _M_param
.a(); }
3012 { return _M_param
.b(); }
3015 * @brief Returns the parameter set of the distribution.
3019 { return _M_param
; }
3022 * @brief Sets the parameter set of the distribution.
3023 * @param __param The new parameter set of the distribution.
3026 param(const param_type
& __param
)
3027 { _M_param
= __param
; }
3030 * @brief Returns the greatest lower bound value of the distribution.
3034 { return std::numeric_limits
<result_type
>::lowest(); }
3037 * @brief Returns the least upper bound value of the distribution.
3041 { return std::numeric_limits
<result_type
>::max(); }
3044 * @brief Generating functions.
3046 template<typename _UniformRandomNumberGenerator
>
3048 operator()(_UniformRandomNumberGenerator
& __urng
)
3049 { return this->operator()(__urng
, _M_param
); }
3051 template<typename _UniformRandomNumberGenerator
>
3053 operator()(_UniformRandomNumberGenerator
& __urng
,
3054 const param_type
& __p
);
3056 template<typename _ForwardIterator
,
3057 typename _UniformRandomNumberGenerator
>
3059 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3060 _UniformRandomNumberGenerator
& __urng
)
3061 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3063 template<typename _ForwardIterator
,
3064 typename _UniformRandomNumberGenerator
>
3066 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3067 _UniformRandomNumberGenerator
& __urng
,
3068 const param_type
& __p
)
3069 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3071 template<typename _UniformRandomNumberGenerator
>
3073 __generate(result_type
* __f
, result_type
* __t
,
3074 _UniformRandomNumberGenerator
& __urng
,
3075 const param_type
& __p
)
3076 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3079 * @brief Return true if two Cauchy distributions have
3080 * the same parameters.
3083 operator==(const cauchy_distribution
& __d1
,
3084 const cauchy_distribution
& __d2
)
3085 { return __d1
._M_param
== __d2
._M_param
; }
3088 template<typename _ForwardIterator
,
3089 typename _UniformRandomNumberGenerator
>
3091 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3092 _UniformRandomNumberGenerator
& __urng
,
3093 const param_type
& __p
);
3095 param_type _M_param
;
3098 #if __cpp_impl_three_way_comparison < 201907L
3100 * @brief Return true if two Cauchy distributions have
3101 * different parameters.
3103 template<typename _RealType
>
3105 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
3106 const std::cauchy_distribution
<_RealType
>& __d2
)
3107 { return !(__d1
== __d2
); }
3111 * @brief Inserts a %cauchy_distribution random number distribution
3112 * @p __x into the output stream @p __os.
3114 * @param __os An output stream.
3115 * @param __x A %cauchy_distribution random number distribution.
3117 * @returns The output stream with the state of @p __x inserted or in
3120 template<typename _RealType
, typename _CharT
, typename _Traits
>
3121 std::basic_ostream
<_CharT
, _Traits
>&
3122 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3123 const std::cauchy_distribution
<_RealType
>& __x
);
3126 * @brief Extracts a %cauchy_distribution random number distribution
3127 * @p __x from the input stream @p __is.
3129 * @param __is An input stream.
3130 * @param __x A %cauchy_distribution random number
3133 * @returns The input stream with @p __x extracted or in an error state.
3135 template<typename _RealType
, typename _CharT
, typename _Traits
>
3136 std::basic_istream
<_CharT
, _Traits
>&
3137 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3138 std::cauchy_distribution
<_RealType
>& __x
);
3142 * @brief A fisher_f_distribution random number distribution.
3144 * The formula for the normal probability mass function is
3146 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
3147 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
3148 * (1 + \frac{mx}{n})^{-(m+n)/2}
3151 * @headerfile random
3154 template<typename _RealType
= double>
3155 class fisher_f_distribution
3157 static_assert(std::is_floating_point
<_RealType
>::value
,
3158 "result_type must be a floating point type");
3161 /** The type of the range of the distribution. */
3162 typedef _RealType result_type
;
3164 /** Parameter type. */
3167 typedef fisher_f_distribution
<_RealType
> distribution_type
;
3169 param_type() : param_type(1) { }
3172 param_type(_RealType __m
, _RealType __n
= _RealType(1))
3173 : _M_m(__m
), _M_n(__n
)
3185 operator==(const param_type
& __p1
, const param_type
& __p2
)
3186 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
3188 #if __cpp_impl_three_way_comparison < 201907L
3190 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3191 { return !(__p1
== __p2
); }
3199 fisher_f_distribution() : fisher_f_distribution(1.0) { }
3202 fisher_f_distribution(_RealType __m
,
3203 _RealType __n
= _RealType(1))
3204 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
3208 fisher_f_distribution(const param_type
& __p
)
3209 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
3213 * @brief Resets the distribution state.
3227 { return _M_param
.m(); }
3231 { return _M_param
.n(); }
3234 * @brief Returns the parameter set of the distribution.
3238 { return _M_param
; }
3241 * @brief Sets the parameter set of the distribution.
3242 * @param __param The new parameter set of the distribution.
3245 param(const param_type
& __param
)
3246 { _M_param
= __param
; }
3249 * @brief Returns the greatest lower bound value of the distribution.
3253 { return result_type(0); }
3256 * @brief Returns the least upper bound value of the distribution.
3260 { return std::numeric_limits
<result_type
>::max(); }
3263 * @brief Generating functions.
3265 template<typename _UniformRandomNumberGenerator
>
3267 operator()(_UniformRandomNumberGenerator
& __urng
)
3268 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
3270 template<typename _UniformRandomNumberGenerator
>
3272 operator()(_UniformRandomNumberGenerator
& __urng
,
3273 const param_type
& __p
)
3275 typedef typename
std::gamma_distribution
<result_type
>::param_type
3277 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
3278 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
3281 template<typename _ForwardIterator
,
3282 typename _UniformRandomNumberGenerator
>
3284 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3285 _UniformRandomNumberGenerator
& __urng
)
3286 { this->__generate_impl(__f
, __t
, __urng
); }
3288 template<typename _ForwardIterator
,
3289 typename _UniformRandomNumberGenerator
>
3291 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3292 _UniformRandomNumberGenerator
& __urng
,
3293 const param_type
& __p
)
3294 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3296 template<typename _UniformRandomNumberGenerator
>
3298 __generate(result_type
* __f
, result_type
* __t
,
3299 _UniformRandomNumberGenerator
& __urng
)
3300 { this->__generate_impl(__f
, __t
, __urng
); }
3302 template<typename _UniformRandomNumberGenerator
>
3304 __generate(result_type
* __f
, result_type
* __t
,
3305 _UniformRandomNumberGenerator
& __urng
,
3306 const param_type
& __p
)
3307 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3310 * @brief Return true if two Fisher f distributions have
3311 * the same parameters and the sequences that would
3312 * be generated are equal.
3315 operator==(const fisher_f_distribution
& __d1
,
3316 const fisher_f_distribution
& __d2
)
3317 { return (__d1
._M_param
== __d2
._M_param
3318 && __d1
._M_gd_x
== __d2
._M_gd_x
3319 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
3322 * @brief Inserts a %fisher_f_distribution random number distribution
3323 * @p __x into the output stream @p __os.
3325 * @param __os An output stream.
3326 * @param __x A %fisher_f_distribution random number distribution.
3328 * @returns The output stream with the state of @p __x inserted or in
3331 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3332 friend std::basic_ostream
<_CharT
, _Traits
>&
3333 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3334 const std::fisher_f_distribution
<_RealType1
>& __x
);
3337 * @brief Extracts a %fisher_f_distribution random number distribution
3338 * @p __x from the input stream @p __is.
3340 * @param __is An input stream.
3341 * @param __x A %fisher_f_distribution random number
3344 * @returns The input stream with @p __x extracted or in an error state.
3346 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3347 friend std::basic_istream
<_CharT
, _Traits
>&
3348 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3349 std::fisher_f_distribution
<_RealType1
>& __x
);
3352 template<typename _ForwardIterator
,
3353 typename _UniformRandomNumberGenerator
>
3355 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3356 _UniformRandomNumberGenerator
& __urng
);
3358 template<typename _ForwardIterator
,
3359 typename _UniformRandomNumberGenerator
>
3361 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3362 _UniformRandomNumberGenerator
& __urng
,
3363 const param_type
& __p
);
3365 param_type _M_param
;
3367 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3370 #if __cpp_impl_three_way_comparison < 201907L
3372 * @brief Return true if two Fisher f distributions are different.
3374 template<typename _RealType
>
3376 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3377 const std::fisher_f_distribution
<_RealType
>& __d2
)
3378 { return !(__d1
== __d2
); }
3382 * @brief A student_t_distribution random number distribution.
3384 * The formula for the normal probability mass function is:
3386 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3387 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3390 * @headerfile random
3393 template<typename _RealType
= double>
3394 class student_t_distribution
3396 static_assert(std::is_floating_point
<_RealType
>::value
,
3397 "result_type must be a floating point type");
3400 /** The type of the range of the distribution. */
3401 typedef _RealType result_type
;
3403 /** Parameter type. */
3406 typedef student_t_distribution
<_RealType
> distribution_type
;
3408 param_type() : param_type(1) { }
3411 param_type(_RealType __n
)
3420 operator==(const param_type
& __p1
, const param_type
& __p2
)
3421 { return __p1
._M_n
== __p2
._M_n
; }
3423 #if __cpp_impl_three_way_comparison < 201907L
3425 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3426 { return !(__p1
== __p2
); }
3433 student_t_distribution() : student_t_distribution(1.0) { }
3436 student_t_distribution(_RealType __n
)
3437 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3441 student_t_distribution(const param_type
& __p
)
3442 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3446 * @brief Resets the distribution state.
3460 { return _M_param
.n(); }
3463 * @brief Returns the parameter set of the distribution.
3467 { return _M_param
; }
3470 * @brief Sets the parameter set of the distribution.
3471 * @param __param The new parameter set of the distribution.
3474 param(const param_type
& __param
)
3475 { _M_param
= __param
; }
3478 * @brief Returns the greatest lower bound value of the distribution.
3482 { return std::numeric_limits
<result_type
>::lowest(); }
3485 * @brief Returns the least upper bound value of the distribution.
3489 { return std::numeric_limits
<result_type
>::max(); }
3492 * @brief Generating functions.
3494 template<typename _UniformRandomNumberGenerator
>
3496 operator()(_UniformRandomNumberGenerator
& __urng
)
3497 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3499 template<typename _UniformRandomNumberGenerator
>
3501 operator()(_UniformRandomNumberGenerator
& __urng
,
3502 const param_type
& __p
)
3504 typedef typename
std::gamma_distribution
<result_type
>::param_type
3507 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3508 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3511 template<typename _ForwardIterator
,
3512 typename _UniformRandomNumberGenerator
>
3514 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3515 _UniformRandomNumberGenerator
& __urng
)
3516 { this->__generate_impl(__f
, __t
, __urng
); }
3518 template<typename _ForwardIterator
,
3519 typename _UniformRandomNumberGenerator
>
3521 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3522 _UniformRandomNumberGenerator
& __urng
,
3523 const param_type
& __p
)
3524 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3526 template<typename _UniformRandomNumberGenerator
>
3528 __generate(result_type
* __f
, result_type
* __t
,
3529 _UniformRandomNumberGenerator
& __urng
)
3530 { this->__generate_impl(__f
, __t
, __urng
); }
3532 template<typename _UniformRandomNumberGenerator
>
3534 __generate(result_type
* __f
, result_type
* __t
,
3535 _UniformRandomNumberGenerator
& __urng
,
3536 const param_type
& __p
)
3537 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3540 * @brief Return true if two Student t distributions have
3541 * the same parameters and the sequences that would
3542 * be generated are equal.
3545 operator==(const student_t_distribution
& __d1
,
3546 const student_t_distribution
& __d2
)
3547 { return (__d1
._M_param
== __d2
._M_param
3548 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3551 * @brief Inserts a %student_t_distribution random number distribution
3552 * @p __x into the output stream @p __os.
3554 * @param __os An output stream.
3555 * @param __x A %student_t_distribution random number distribution.
3557 * @returns The output stream with the state of @p __x inserted or in
3560 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3561 friend std::basic_ostream
<_CharT
, _Traits
>&
3562 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3563 const std::student_t_distribution
<_RealType1
>& __x
);
3566 * @brief Extracts a %student_t_distribution random number distribution
3567 * @p __x from the input stream @p __is.
3569 * @param __is An input stream.
3570 * @param __x A %student_t_distribution random number
3573 * @returns The input stream with @p __x extracted or in an error state.
3575 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3576 friend std::basic_istream
<_CharT
, _Traits
>&
3577 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3578 std::student_t_distribution
<_RealType1
>& __x
);
3581 template<typename _ForwardIterator
,
3582 typename _UniformRandomNumberGenerator
>
3584 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3585 _UniformRandomNumberGenerator
& __urng
);
3586 template<typename _ForwardIterator
,
3587 typename _UniformRandomNumberGenerator
>
3589 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3590 _UniformRandomNumberGenerator
& __urng
,
3591 const param_type
& __p
);
3593 param_type _M_param
;
3595 std::normal_distribution
<result_type
> _M_nd
;
3596 std::gamma_distribution
<result_type
> _M_gd
;
3599 #if __cpp_impl_three_way_comparison < 201907L
3601 * @brief Return true if two Student t distributions are different.
3603 template<typename _RealType
>
3605 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3606 const std::student_t_distribution
<_RealType
>& __d2
)
3607 { return !(__d1
== __d2
); }
3610 /// @} group random_distributions_normal
3613 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3614 * @ingroup random_distributions
3619 * @brief A Bernoulli random number distribution.
3621 * Generates a sequence of true and false values with likelihood @f$p@f$
3622 * that true will come up and @f$(1 - p)@f$ that false will appear.
3624 * @headerfile random
3627 class bernoulli_distribution
3630 /** The type of the range of the distribution. */
3631 typedef bool result_type
;
3633 /** Parameter type. */
3636 typedef bernoulli_distribution distribution_type
;
3638 param_type() : param_type(0.5) { }
3641 param_type(double __p
)
3644 __glibcxx_assert((_M_p
>= 0.0) && (_M_p
<= 1.0));
3652 operator==(const param_type
& __p1
, const param_type
& __p2
)
3653 { return __p1
._M_p
== __p2
._M_p
; }
3655 #if __cpp_impl_three_way_comparison < 201907L
3657 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3658 { return !(__p1
== __p2
); }
3667 * @brief Constructs a Bernoulli distribution with likelihood 0.5.
3669 bernoulli_distribution() : bernoulli_distribution(0.5) { }
3672 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3674 * @param __p [IN] The likelihood of a true result being returned.
3675 * Must be in the interval @f$[0, 1]@f$.
3678 bernoulli_distribution(double __p
)
3683 bernoulli_distribution(const param_type
& __p
)
3688 * @brief Resets the distribution state.
3690 * Does nothing for a Bernoulli distribution.
3696 * @brief Returns the @p p parameter of the distribution.
3700 { return _M_param
.p(); }
3703 * @brief Returns the parameter set of the distribution.
3707 { return _M_param
; }
3710 * @brief Sets the parameter set of the distribution.
3711 * @param __param The new parameter set of the distribution.
3714 param(const param_type
& __param
)
3715 { _M_param
= __param
; }
3718 * @brief Returns the greatest lower bound value of the distribution.
3722 { return std::numeric_limits
<result_type
>::min(); }
3725 * @brief Returns the least upper bound value of the distribution.
3729 { return std::numeric_limits
<result_type
>::max(); }
3732 * @brief Generating functions.
3734 template<typename _UniformRandomNumberGenerator
>
3736 operator()(_UniformRandomNumberGenerator
& __urng
)
3737 { return this->operator()(__urng
, _M_param
); }
3739 template<typename _UniformRandomNumberGenerator
>
3741 operator()(_UniformRandomNumberGenerator
& __urng
,
3742 const param_type
& __p
)
3744 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3746 if ((__aurng() - __aurng
.min())
3747 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3752 template<typename _ForwardIterator
,
3753 typename _UniformRandomNumberGenerator
>
3755 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3756 _UniformRandomNumberGenerator
& __urng
)
3757 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3759 template<typename _ForwardIterator
,
3760 typename _UniformRandomNumberGenerator
>
3762 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3763 _UniformRandomNumberGenerator
& __urng
, const param_type
& __p
)
3764 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3766 template<typename _UniformRandomNumberGenerator
>
3768 __generate(result_type
* __f
, result_type
* __t
,
3769 _UniformRandomNumberGenerator
& __urng
,
3770 const param_type
& __p
)
3771 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
3774 * @brief Return true if two Bernoulli distributions have
3775 * the same parameters.
3778 operator==(const bernoulli_distribution
& __d1
,
3779 const bernoulli_distribution
& __d2
)
3780 { return __d1
._M_param
== __d2
._M_param
; }
3783 template<typename _ForwardIterator
,
3784 typename _UniformRandomNumberGenerator
>
3786 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
3787 _UniformRandomNumberGenerator
& __urng
,
3788 const param_type
& __p
);
3790 param_type _M_param
;
3793 #if __cpp_impl_three_way_comparison < 201907L
3795 * @brief Return true if two Bernoulli distributions have
3796 * different parameters.
3799 operator!=(const std::bernoulli_distribution
& __d1
,
3800 const std::bernoulli_distribution
& __d2
)
3801 { return !(__d1
== __d2
); }
3805 * @brief Inserts a %bernoulli_distribution random number distribution
3806 * @p __x into the output stream @p __os.
3808 * @param __os An output stream.
3809 * @param __x A %bernoulli_distribution random number distribution.
3811 * @returns The output stream with the state of @p __x inserted or in
3814 template<typename _CharT
, typename _Traits
>
3815 std::basic_ostream
<_CharT
, _Traits
>&
3816 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3817 const std::bernoulli_distribution
& __x
);
3820 * @brief Extracts a %bernoulli_distribution random number distribution
3821 * @p __x from the input stream @p __is.
3823 * @param __is An input stream.
3824 * @param __x A %bernoulli_distribution random number generator engine.
3826 * @returns The input stream with @p __x extracted or in an error state.
3828 template<typename _CharT
, typename _Traits
>
3829 inline std::basic_istream
<_CharT
, _Traits
>&
3830 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3831 std::bernoulli_distribution
& __x
)
3835 __x
.param(bernoulli_distribution::param_type(__p
));
3841 * @brief A discrete binomial random number distribution.
3843 * The formula for the binomial probability density function is
3844 * @f$p(i|t,p) = \binom{t}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3845 * and @f$p@f$ are the parameters of the distribution.
3847 * @headerfile random
3850 template<typename _IntType
= int>
3851 class binomial_distribution
3853 static_assert(std::is_integral
<_IntType
>::value
,
3854 "result_type must be an integral type");
3857 /** The type of the range of the distribution. */
3858 typedef _IntType result_type
;
3860 /** Parameter type. */
3863 typedef binomial_distribution
<_IntType
> distribution_type
;
3864 friend class binomial_distribution
<_IntType
>;
3866 param_type() : param_type(1) { }
3869 param_type(_IntType __t
, double __p
= 0.5)
3870 : _M_t(__t
), _M_p(__p
)
3872 __glibcxx_assert((_M_t
>= _IntType(0))
3887 operator==(const param_type
& __p1
, const param_type
& __p2
)
3888 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3890 #if __cpp_impl_three_way_comparison < 201907L
3892 operator!=(const param_type
& __p1
, const param_type
& __p2
)
3893 { return !(__p1
== __p2
); }
3904 #if _GLIBCXX_USE_C99_MATH_FUNCS
3905 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3906 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3911 // constructors and member functions
3913 binomial_distribution() : binomial_distribution(1) { }
3916 binomial_distribution(_IntType __t
, double __p
= 0.5)
3917 : _M_param(__t
, __p
), _M_nd()
3921 binomial_distribution(const param_type
& __p
)
3922 : _M_param(__p
), _M_nd()
3926 * @brief Resets the distribution state.
3933 * @brief Returns the distribution @p t parameter.
3937 { return _M_param
.t(); }
3940 * @brief Returns the distribution @p p parameter.
3944 { return _M_param
.p(); }
3947 * @brief Returns the parameter set of the distribution.
3951 { return _M_param
; }
3954 * @brief Sets the parameter set of the distribution.
3955 * @param __param The new parameter set of the distribution.
3958 param(const param_type
& __param
)
3959 { _M_param
= __param
; }
3962 * @brief Returns the greatest lower bound value of the distribution.
3969 * @brief Returns the least upper bound value of the distribution.
3973 { return _M_param
.t(); }
3976 * @brief Generating functions.
3978 template<typename _UniformRandomNumberGenerator
>
3980 operator()(_UniformRandomNumberGenerator
& __urng
)
3981 { return this->operator()(__urng
, _M_param
); }
3983 template<typename _UniformRandomNumberGenerator
>
3985 operator()(_UniformRandomNumberGenerator
& __urng
,
3986 const param_type
& __p
);
3988 template<typename _ForwardIterator
,
3989 typename _UniformRandomNumberGenerator
>
3991 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3992 _UniformRandomNumberGenerator
& __urng
)
3993 { this->__generate(__f
, __t
, __urng
, _M_param
); }
3995 template<typename _ForwardIterator
,
3996 typename _UniformRandomNumberGenerator
>
3998 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
3999 _UniformRandomNumberGenerator
& __urng
,
4000 const param_type
& __p
)
4001 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4003 template<typename _UniformRandomNumberGenerator
>
4005 __generate(result_type
* __f
, result_type
* __t
,
4006 _UniformRandomNumberGenerator
& __urng
,
4007 const param_type
& __p
)
4008 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4011 * @brief Return true if two binomial distributions have
4012 * the same parameters and the sequences that would
4013 * be generated are equal.
4016 operator==(const binomial_distribution
& __d1
,
4017 const binomial_distribution
& __d2
)
4018 #ifdef _GLIBCXX_USE_C99_MATH_FUNCS
4019 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
4021 { return __d1
._M_param
== __d2
._M_param
; }
4025 * @brief Inserts a %binomial_distribution random number distribution
4026 * @p __x into the output stream @p __os.
4028 * @param __os An output stream.
4029 * @param __x A %binomial_distribution random number distribution.
4031 * @returns The output stream with the state of @p __x inserted or in
4034 template<typename _IntType1
,
4035 typename _CharT
, typename _Traits
>
4036 friend std::basic_ostream
<_CharT
, _Traits
>&
4037 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4038 const std::binomial_distribution
<_IntType1
>& __x
);
4041 * @brief Extracts a %binomial_distribution random number distribution
4042 * @p __x from the input stream @p __is.
4044 * @param __is An input stream.
4045 * @param __x A %binomial_distribution random number generator engine.
4047 * @returns The input stream with @p __x extracted or in an error
4050 template<typename _IntType1
,
4051 typename _CharT
, typename _Traits
>
4052 friend std::basic_istream
<_CharT
, _Traits
>&
4053 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4054 std::binomial_distribution
<_IntType1
>& __x
);
4057 template<typename _ForwardIterator
,
4058 typename _UniformRandomNumberGenerator
>
4060 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4061 _UniformRandomNumberGenerator
& __urng
,
4062 const param_type
& __p
);
4064 template<typename _UniformRandomNumberGenerator
>
4066 _M_waiting(_UniformRandomNumberGenerator
& __urng
,
4067 _IntType __t
, double __q
);
4069 param_type _M_param
;
4071 // NB: Unused when _GLIBCXX_USE_C99_MATH_FUNCS is undefined.
4072 std::normal_distribution
<double> _M_nd
;
4075 #if __cpp_impl_three_way_comparison < 201907L
4077 * @brief Return true if two binomial distributions are different.
4079 template<typename _IntType
>
4081 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
4082 const std::binomial_distribution
<_IntType
>& __d2
)
4083 { return !(__d1
== __d2
); }
4087 * @brief A discrete geometric random number distribution.
4089 * The formula for the geometric probability density function is
4090 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
4093 * @headerfile random
4096 template<typename _IntType
= int>
4097 class geometric_distribution
4099 static_assert(std::is_integral
<_IntType
>::value
,
4100 "result_type must be an integral type");
4103 /** The type of the range of the distribution. */
4104 typedef _IntType result_type
;
4106 /** Parameter type. */
4109 typedef geometric_distribution
<_IntType
> distribution_type
;
4110 friend class geometric_distribution
<_IntType
>;
4112 param_type() : param_type(0.5) { }
4115 param_type(double __p
)
4118 __glibcxx_assert((_M_p
> 0.0) && (_M_p
< 1.0));
4127 operator==(const param_type
& __p1
, const param_type
& __p2
)
4128 { return __p1
._M_p
== __p2
._M_p
; }
4130 #if __cpp_impl_three_way_comparison < 201907L
4132 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4133 { return !(__p1
== __p2
); }
4139 { _M_log_1_p
= std::log(1.0 - _M_p
); }
4146 // constructors and member functions
4148 geometric_distribution() : geometric_distribution(0.5) { }
4151 geometric_distribution(double __p
)
4156 geometric_distribution(const param_type
& __p
)
4161 * @brief Resets the distribution state.
4163 * Does nothing for the geometric distribution.
4169 * @brief Returns the distribution parameter @p p.
4173 { return _M_param
.p(); }
4176 * @brief Returns the parameter set of the distribution.
4180 { return _M_param
; }
4183 * @brief Sets the parameter set of the distribution.
4184 * @param __param The new parameter set of the distribution.
4187 param(const param_type
& __param
)
4188 { _M_param
= __param
; }
4191 * @brief Returns the greatest lower bound value of the distribution.
4198 * @brief Returns the least upper bound value of the distribution.
4202 { return std::numeric_limits
<result_type
>::max(); }
4205 * @brief Generating functions.
4207 template<typename _UniformRandomNumberGenerator
>
4209 operator()(_UniformRandomNumberGenerator
& __urng
)
4210 { return this->operator()(__urng
, _M_param
); }
4212 template<typename _UniformRandomNumberGenerator
>
4214 operator()(_UniformRandomNumberGenerator
& __urng
,
4215 const param_type
& __p
);
4217 template<typename _ForwardIterator
,
4218 typename _UniformRandomNumberGenerator
>
4220 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4221 _UniformRandomNumberGenerator
& __urng
)
4222 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4224 template<typename _ForwardIterator
,
4225 typename _UniformRandomNumberGenerator
>
4227 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4228 _UniformRandomNumberGenerator
& __urng
,
4229 const param_type
& __p
)
4230 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4232 template<typename _UniformRandomNumberGenerator
>
4234 __generate(result_type
* __f
, result_type
* __t
,
4235 _UniformRandomNumberGenerator
& __urng
,
4236 const param_type
& __p
)
4237 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4240 * @brief Return true if two geometric distributions have
4241 * the same parameters.
4244 operator==(const geometric_distribution
& __d1
,
4245 const geometric_distribution
& __d2
)
4246 { return __d1
._M_param
== __d2
._M_param
; }
4249 template<typename _ForwardIterator
,
4250 typename _UniformRandomNumberGenerator
>
4252 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4253 _UniformRandomNumberGenerator
& __urng
,
4254 const param_type
& __p
);
4256 param_type _M_param
;
4259 #if __cpp_impl_three_way_comparison < 201907L
4261 * @brief Return true if two geometric distributions have
4262 * different parameters.
4264 template<typename _IntType
>
4266 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
4267 const std::geometric_distribution
<_IntType
>& __d2
)
4268 { return !(__d1
== __d2
); }
4272 * @brief Inserts a %geometric_distribution random number distribution
4273 * @p __x into the output stream @p __os.
4275 * @param __os An output stream.
4276 * @param __x A %geometric_distribution random number distribution.
4278 * @returns The output stream with the state of @p __x inserted or in
4281 template<typename _IntType
,
4282 typename _CharT
, typename _Traits
>
4283 std::basic_ostream
<_CharT
, _Traits
>&
4284 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4285 const std::geometric_distribution
<_IntType
>& __x
);
4288 * @brief Extracts a %geometric_distribution random number distribution
4289 * @p __x from the input stream @p __is.
4291 * @param __is An input stream.
4292 * @param __x A %geometric_distribution random number generator engine.
4294 * @returns The input stream with @p __x extracted or in an error state.
4296 template<typename _IntType
,
4297 typename _CharT
, typename _Traits
>
4298 std::basic_istream
<_CharT
, _Traits
>&
4299 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4300 std::geometric_distribution
<_IntType
>& __x
);
4304 * @brief A negative_binomial_distribution random number distribution.
4306 * The formula for the negative binomial probability mass function is
4307 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
4308 * and @f$p@f$ are the parameters of the distribution.
4310 * @headerfile random
4313 template<typename _IntType
= int>
4314 class negative_binomial_distribution
4316 static_assert(std::is_integral
<_IntType
>::value
,
4317 "result_type must be an integral type");
4320 /** The type of the range of the distribution. */
4321 typedef _IntType result_type
;
4323 /** Parameter type. */
4326 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
4328 param_type() : param_type(1) { }
4331 param_type(_IntType __k
, double __p
= 0.5)
4332 : _M_k(__k
), _M_p(__p
)
4334 __glibcxx_assert((_M_k
> 0) && (_M_p
> 0.0) && (_M_p
<= 1.0));
4346 operator==(const param_type
& __p1
, const param_type
& __p2
)
4347 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
4349 #if __cpp_impl_three_way_comparison < 201907L
4351 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4352 { return !(__p1
== __p2
); }
4360 negative_binomial_distribution() : negative_binomial_distribution(1) { }
4363 negative_binomial_distribution(_IntType __k
, double __p
= 0.5)
4364 : _M_param(__k
, __p
), _M_gd(__k
, (1.0 - __p
) / __p
)
4368 negative_binomial_distribution(const param_type
& __p
)
4369 : _M_param(__p
), _M_gd(__p
.k(), (1.0 - __p
.p()) / __p
.p())
4373 * @brief Resets the distribution state.
4380 * @brief Return the @f$k@f$ parameter of the distribution.
4384 { return _M_param
.k(); }
4387 * @brief Return the @f$p@f$ parameter of the distribution.
4391 { return _M_param
.p(); }
4394 * @brief Returns the parameter set of the distribution.
4398 { return _M_param
; }
4401 * @brief Sets the parameter set of the distribution.
4402 * @param __param The new parameter set of the distribution.
4405 param(const param_type
& __param
)
4406 { _M_param
= __param
; }
4409 * @brief Returns the greatest lower bound value of the distribution.
4413 { return result_type(0); }
4416 * @brief Returns the least upper bound value of the distribution.
4420 { return std::numeric_limits
<result_type
>::max(); }
4423 * @brief Generating functions.
4425 template<typename _UniformRandomNumberGenerator
>
4427 operator()(_UniformRandomNumberGenerator
& __urng
);
4429 template<typename _UniformRandomNumberGenerator
>
4431 operator()(_UniformRandomNumberGenerator
& __urng
,
4432 const param_type
& __p
);
4434 template<typename _ForwardIterator
,
4435 typename _UniformRandomNumberGenerator
>
4437 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4438 _UniformRandomNumberGenerator
& __urng
)
4439 { this->__generate_impl(__f
, __t
, __urng
); }
4441 template<typename _ForwardIterator
,
4442 typename _UniformRandomNumberGenerator
>
4444 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4445 _UniformRandomNumberGenerator
& __urng
,
4446 const param_type
& __p
)
4447 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4449 template<typename _UniformRandomNumberGenerator
>
4451 __generate(result_type
* __f
, result_type
* __t
,
4452 _UniformRandomNumberGenerator
& __urng
)
4453 { this->__generate_impl(__f
, __t
, __urng
); }
4455 template<typename _UniformRandomNumberGenerator
>
4457 __generate(result_type
* __f
, result_type
* __t
,
4458 _UniformRandomNumberGenerator
& __urng
,
4459 const param_type
& __p
)
4460 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4463 * @brief Return true if two negative binomial distributions have
4464 * the same parameters and the sequences that would be
4465 * generated are equal.
4468 operator==(const negative_binomial_distribution
& __d1
,
4469 const negative_binomial_distribution
& __d2
)
4470 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_gd
== __d2
._M_gd
; }
4473 * @brief Inserts a %negative_binomial_distribution random
4474 * number distribution @p __x into the output stream @p __os.
4476 * @param __os An output stream.
4477 * @param __x A %negative_binomial_distribution random number
4480 * @returns The output stream with the state of @p __x inserted or in
4483 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4484 friend std::basic_ostream
<_CharT
, _Traits
>&
4485 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4486 const std::negative_binomial_distribution
<_IntType1
>& __x
);
4489 * @brief Extracts a %negative_binomial_distribution random number
4490 * distribution @p __x from the input stream @p __is.
4492 * @param __is An input stream.
4493 * @param __x A %negative_binomial_distribution random number
4496 * @returns The input stream with @p __x extracted or in an error state.
4498 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4499 friend std::basic_istream
<_CharT
, _Traits
>&
4500 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4501 std::negative_binomial_distribution
<_IntType1
>& __x
);
4504 template<typename _ForwardIterator
,
4505 typename _UniformRandomNumberGenerator
>
4507 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4508 _UniformRandomNumberGenerator
& __urng
);
4509 template<typename _ForwardIterator
,
4510 typename _UniformRandomNumberGenerator
>
4512 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4513 _UniformRandomNumberGenerator
& __urng
,
4514 const param_type
& __p
);
4516 param_type _M_param
;
4518 std::gamma_distribution
<double> _M_gd
;
4521 #if __cpp_impl_three_way_comparison < 201907L
4523 * @brief Return true if two negative binomial distributions are different.
4525 template<typename _IntType
>
4527 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
4528 const std::negative_binomial_distribution
<_IntType
>& __d2
)
4529 { return !(__d1
== __d2
); }
4532 /// @} group random_distributions_bernoulli
4535 * @addtogroup random_distributions_poisson Poisson Distributions
4536 * @ingroup random_distributions
4541 * @brief A discrete Poisson random number distribution.
4543 * The formula for the Poisson probability density function is
4544 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
4545 * parameter of the distribution.
4547 * @headerfile random
4550 template<typename _IntType
= int>
4551 class poisson_distribution
4553 static_assert(std::is_integral
<_IntType
>::value
,
4554 "result_type must be an integral type");
4557 /** The type of the range of the distribution. */
4558 typedef _IntType result_type
;
4560 /** Parameter type. */
4563 typedef poisson_distribution
<_IntType
> distribution_type
;
4564 friend class poisson_distribution
<_IntType
>;
4566 param_type() : param_type(1.0) { }
4569 param_type(double __mean
)
4572 __glibcxx_assert(_M_mean
> 0.0);
4581 operator==(const param_type
& __p1
, const param_type
& __p2
)
4582 { return __p1
._M_mean
== __p2
._M_mean
; }
4584 #if __cpp_impl_three_way_comparison < 201907L
4586 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4587 { return !(__p1
== __p2
); }
4591 // Hosts either log(mean) or the threshold of the simple method.
4598 #if _GLIBCXX_USE_C99_MATH_FUNCS
4599 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
4603 // constructors and member functions
4605 poisson_distribution() : poisson_distribution(1.0) { }
4608 poisson_distribution(double __mean
)
4609 : _M_param(__mean
), _M_nd()
4613 poisson_distribution(const param_type
& __p
)
4614 : _M_param(__p
), _M_nd()
4618 * @brief Resets the distribution state.
4625 * @brief Returns the distribution parameter @p mean.
4629 { return _M_param
.mean(); }
4632 * @brief Returns the parameter set of the distribution.
4636 { return _M_param
; }
4639 * @brief Sets the parameter set of the distribution.
4640 * @param __param The new parameter set of the distribution.
4643 param(const param_type
& __param
)
4644 { _M_param
= __param
; }
4647 * @brief Returns the greatest lower bound value of the distribution.
4654 * @brief Returns the least upper bound value of the distribution.
4658 { return std::numeric_limits
<result_type
>::max(); }
4661 * @brief Generating functions.
4663 template<typename _UniformRandomNumberGenerator
>
4665 operator()(_UniformRandomNumberGenerator
& __urng
)
4666 { return this->operator()(__urng
, _M_param
); }
4668 template<typename _UniformRandomNumberGenerator
>
4670 operator()(_UniformRandomNumberGenerator
& __urng
,
4671 const param_type
& __p
);
4673 template<typename _ForwardIterator
,
4674 typename _UniformRandomNumberGenerator
>
4676 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4677 _UniformRandomNumberGenerator
& __urng
)
4678 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4680 template<typename _ForwardIterator
,
4681 typename _UniformRandomNumberGenerator
>
4683 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4684 _UniformRandomNumberGenerator
& __urng
,
4685 const param_type
& __p
)
4686 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4688 template<typename _UniformRandomNumberGenerator
>
4690 __generate(result_type
* __f
, result_type
* __t
,
4691 _UniformRandomNumberGenerator
& __urng
,
4692 const param_type
& __p
)
4693 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4696 * @brief Return true if two Poisson distributions have the same
4697 * parameters and the sequences that would be generated
4701 operator==(const poisson_distribution
& __d1
,
4702 const poisson_distribution
& __d2
)
4703 #ifdef _GLIBCXX_USE_C99_MATH_FUNCS
4704 { return __d1
._M_param
== __d2
._M_param
&& __d1
._M_nd
== __d2
._M_nd
; }
4706 { return __d1
._M_param
== __d2
._M_param
; }
4710 * @brief Inserts a %poisson_distribution random number distribution
4711 * @p __x into the output stream @p __os.
4713 * @param __os An output stream.
4714 * @param __x A %poisson_distribution random number distribution.
4716 * @returns The output stream with the state of @p __x inserted or in
4719 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4720 friend std::basic_ostream
<_CharT
, _Traits
>&
4721 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4722 const std::poisson_distribution
<_IntType1
>& __x
);
4725 * @brief Extracts a %poisson_distribution random number distribution
4726 * @p __x from the input stream @p __is.
4728 * @param __is An input stream.
4729 * @param __x A %poisson_distribution random number generator engine.
4731 * @returns The input stream with @p __x extracted or in an error
4734 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4735 friend std::basic_istream
<_CharT
, _Traits
>&
4736 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4737 std::poisson_distribution
<_IntType1
>& __x
);
4740 template<typename _ForwardIterator
,
4741 typename _UniformRandomNumberGenerator
>
4743 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4744 _UniformRandomNumberGenerator
& __urng
,
4745 const param_type
& __p
);
4747 param_type _M_param
;
4749 // NB: Unused when _GLIBCXX_USE_C99_MATH_FUNCS is undefined.
4750 std::normal_distribution
<double> _M_nd
;
4753 #if __cpp_impl_three_way_comparison < 201907L
4755 * @brief Return true if two Poisson distributions are different.
4757 template<typename _IntType
>
4759 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4760 const std::poisson_distribution
<_IntType
>& __d2
)
4761 { return !(__d1
== __d2
); }
4765 * @brief An exponential continuous distribution for random numbers.
4767 * The formula for the exponential probability density function is
4768 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4770 * <table border=1 cellpadding=10 cellspacing=0>
4771 * <caption align=top>Distribution Statistics</caption>
4772 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4773 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4774 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4775 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4776 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4779 * @headerfile random
4782 template<typename _RealType
= double>
4783 class exponential_distribution
4785 static_assert(std::is_floating_point
<_RealType
>::value
,
4786 "result_type must be a floating point type");
4789 /** The type of the range of the distribution. */
4790 typedef _RealType result_type
;
4792 /** Parameter type. */
4795 typedef exponential_distribution
<_RealType
> distribution_type
;
4797 param_type() : param_type(1.0) { }
4800 param_type(_RealType __lambda
)
4801 : _M_lambda(__lambda
)
4803 __glibcxx_assert(_M_lambda
> _RealType(0));
4808 { return _M_lambda
; }
4811 operator==(const param_type
& __p1
, const param_type
& __p2
)
4812 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4814 #if __cpp_impl_three_way_comparison < 201907L
4816 operator!=(const param_type
& __p1
, const param_type
& __p2
)
4817 { return !(__p1
== __p2
); }
4821 _RealType _M_lambda
;
4826 * @brief Constructs an exponential distribution with inverse scale
4829 exponential_distribution() : exponential_distribution(1.0) { }
4832 * @brief Constructs an exponential distribution with inverse scale
4833 * parameter @f$\lambda@f$.
4836 exponential_distribution(_RealType __lambda
)
4837 : _M_param(__lambda
)
4841 exponential_distribution(const param_type
& __p
)
4846 * @brief Resets the distribution state.
4848 * Has no effect on exponential distributions.
4854 * @brief Returns the inverse scale parameter of the distribution.
4858 { return _M_param
.lambda(); }
4861 * @brief Returns the parameter set of the distribution.
4865 { return _M_param
; }
4868 * @brief Sets the parameter set of the distribution.
4869 * @param __param The new parameter set of the distribution.
4872 param(const param_type
& __param
)
4873 { _M_param
= __param
; }
4876 * @brief Returns the greatest lower bound value of the distribution.
4880 { return result_type(0); }
4883 * @brief Returns the least upper bound value of the distribution.
4887 { return std::numeric_limits
<result_type
>::max(); }
4890 * @brief Generating functions.
4892 template<typename _UniformRandomNumberGenerator
>
4894 operator()(_UniformRandomNumberGenerator
& __urng
)
4895 { return this->operator()(__urng
, _M_param
); }
4897 template<typename _UniformRandomNumberGenerator
>
4899 operator()(_UniformRandomNumberGenerator
& __urng
,
4900 const param_type
& __p
)
4902 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4904 return -std::log(result_type(1) - __aurng()) / __p
.lambda();
4907 template<typename _ForwardIterator
,
4908 typename _UniformRandomNumberGenerator
>
4910 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4911 _UniformRandomNumberGenerator
& __urng
)
4912 { this->__generate(__f
, __t
, __urng
, _M_param
); }
4914 template<typename _ForwardIterator
,
4915 typename _UniformRandomNumberGenerator
>
4917 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
4918 _UniformRandomNumberGenerator
& __urng
,
4919 const param_type
& __p
)
4920 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4922 template<typename _UniformRandomNumberGenerator
>
4924 __generate(result_type
* __f
, result_type
* __t
,
4925 _UniformRandomNumberGenerator
& __urng
,
4926 const param_type
& __p
)
4927 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
4930 * @brief Return true if two exponential distributions have the same
4934 operator==(const exponential_distribution
& __d1
,
4935 const exponential_distribution
& __d2
)
4936 { return __d1
._M_param
== __d2
._M_param
; }
4939 template<typename _ForwardIterator
,
4940 typename _UniformRandomNumberGenerator
>
4942 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
4943 _UniformRandomNumberGenerator
& __urng
,
4944 const param_type
& __p
);
4946 param_type _M_param
;
4949 #if __cpp_impl_three_way_comparison < 201907L
4951 * @brief Return true if two exponential distributions have different
4954 template<typename _RealType
>
4956 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4957 const std::exponential_distribution
<_RealType
>& __d2
)
4958 { return !(__d1
== __d2
); }
4962 * @brief Inserts a %exponential_distribution random number distribution
4963 * @p __x into the output stream @p __os.
4965 * @param __os An output stream.
4966 * @param __x A %exponential_distribution random number distribution.
4968 * @returns The output stream with the state of @p __x inserted or in
4971 template<typename _RealType
, typename _CharT
, typename _Traits
>
4972 std::basic_ostream
<_CharT
, _Traits
>&
4973 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4974 const std::exponential_distribution
<_RealType
>& __x
);
4977 * @brief Extracts a %exponential_distribution random number distribution
4978 * @p __x from the input stream @p __is.
4980 * @param __is An input stream.
4981 * @param __x A %exponential_distribution random number
4984 * @returns The input stream with @p __x extracted or in an error state.
4986 template<typename _RealType
, typename _CharT
, typename _Traits
>
4987 std::basic_istream
<_CharT
, _Traits
>&
4988 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4989 std::exponential_distribution
<_RealType
>& __x
);
4993 * @brief A weibull_distribution random number distribution.
4995 * The formula for the normal probability density function is:
4997 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4998 * \exp{(-(\frac{x}{\beta})^\alpha)}
5001 * @headerfile random
5004 template<typename _RealType
= double>
5005 class weibull_distribution
5007 static_assert(std::is_floating_point
<_RealType
>::value
,
5008 "result_type must be a floating point type");
5011 /** The type of the range of the distribution. */
5012 typedef _RealType result_type
;
5014 /** Parameter type. */
5017 typedef weibull_distribution
<_RealType
> distribution_type
;
5019 param_type() : param_type(1.0) { }
5022 param_type(_RealType __a
, _RealType __b
= _RealType(1.0))
5023 : _M_a(__a
), _M_b(__b
)
5035 operator==(const param_type
& __p1
, const param_type
& __p2
)
5036 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
5038 #if __cpp_impl_three_way_comparison < 201907L
5040 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5041 { return !(__p1
== __p2
); }
5049 weibull_distribution() : weibull_distribution(1.0) { }
5052 weibull_distribution(_RealType __a
, _RealType __b
= _RealType(1))
5053 : _M_param(__a
, __b
)
5057 weibull_distribution(const param_type
& __p
)
5062 * @brief Resets the distribution state.
5069 * @brief Return the @f$a@f$ parameter of the distribution.
5073 { return _M_param
.a(); }
5076 * @brief Return the @f$b@f$ parameter of the distribution.
5080 { return _M_param
.b(); }
5083 * @brief Returns the parameter set of the distribution.
5087 { return _M_param
; }
5090 * @brief Sets the parameter set of the distribution.
5091 * @param __param The new parameter set of the distribution.
5094 param(const param_type
& __param
)
5095 { _M_param
= __param
; }
5098 * @brief Returns the greatest lower bound value of the distribution.
5102 { return result_type(0); }
5105 * @brief Returns the least upper bound value of the distribution.
5109 { return std::numeric_limits
<result_type
>::max(); }
5112 * @brief Generating functions.
5114 template<typename _UniformRandomNumberGenerator
>
5116 operator()(_UniformRandomNumberGenerator
& __urng
)
5117 { return this->operator()(__urng
, _M_param
); }
5119 template<typename _UniformRandomNumberGenerator
>
5121 operator()(_UniformRandomNumberGenerator
& __urng
,
5122 const param_type
& __p
);
5124 template<typename _ForwardIterator
,
5125 typename _UniformRandomNumberGenerator
>
5127 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5128 _UniformRandomNumberGenerator
& __urng
)
5129 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5131 template<typename _ForwardIterator
,
5132 typename _UniformRandomNumberGenerator
>
5134 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5135 _UniformRandomNumberGenerator
& __urng
,
5136 const param_type
& __p
)
5137 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5139 template<typename _UniformRandomNumberGenerator
>
5141 __generate(result_type
* __f
, result_type
* __t
,
5142 _UniformRandomNumberGenerator
& __urng
,
5143 const param_type
& __p
)
5144 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5147 * @brief Return true if two Weibull distributions have the same
5151 operator==(const weibull_distribution
& __d1
,
5152 const weibull_distribution
& __d2
)
5153 { return __d1
._M_param
== __d2
._M_param
; }
5156 template<typename _ForwardIterator
,
5157 typename _UniformRandomNumberGenerator
>
5159 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5160 _UniformRandomNumberGenerator
& __urng
,
5161 const param_type
& __p
);
5163 param_type _M_param
;
5166 #if __cpp_impl_three_way_comparison < 201907L
5168 * @brief Return true if two Weibull distributions have different
5171 template<typename _RealType
>
5173 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
5174 const std::weibull_distribution
<_RealType
>& __d2
)
5175 { return !(__d1
== __d2
); }
5179 * @brief Inserts a %weibull_distribution random number distribution
5180 * @p __x into the output stream @p __os.
5182 * @param __os An output stream.
5183 * @param __x A %weibull_distribution random number distribution.
5185 * @returns The output stream with the state of @p __x inserted or in
5188 template<typename _RealType
, typename _CharT
, typename _Traits
>
5189 std::basic_ostream
<_CharT
, _Traits
>&
5190 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5191 const std::weibull_distribution
<_RealType
>& __x
);
5194 * @brief Extracts a %weibull_distribution random number distribution
5195 * @p __x from the input stream @p __is.
5197 * @param __is An input stream.
5198 * @param __x A %weibull_distribution random number
5201 * @returns The input stream with @p __x extracted or in an error state.
5203 template<typename _RealType
, typename _CharT
, typename _Traits
>
5204 std::basic_istream
<_CharT
, _Traits
>&
5205 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5206 std::weibull_distribution
<_RealType
>& __x
);
5210 * @brief A extreme_value_distribution random number distribution.
5212 * The formula for the normal probability mass function is
5214 * p(x|a,b) = \frac{1}{b}
5215 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
5218 * @headerfile random
5221 template<typename _RealType
= double>
5222 class extreme_value_distribution
5224 static_assert(std::is_floating_point
<_RealType
>::value
,
5225 "result_type must be a floating point type");
5228 /** The type of the range of the distribution. */
5229 typedef _RealType result_type
;
5231 /** Parameter type. */
5234 typedef extreme_value_distribution
<_RealType
> distribution_type
;
5236 param_type() : param_type(0.0) { }
5239 param_type(_RealType __a
, _RealType __b
= _RealType(1.0))
5240 : _M_a(__a
), _M_b(__b
)
5252 operator==(const param_type
& __p1
, const param_type
& __p2
)
5253 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
5255 #if __cpp_impl_three_way_comparison < 201907L
5257 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5258 { return !(__p1
== __p2
); }
5266 extreme_value_distribution() : extreme_value_distribution(0.0) { }
5269 extreme_value_distribution(_RealType __a
, _RealType __b
= _RealType(1))
5270 : _M_param(__a
, __b
)
5274 extreme_value_distribution(const param_type
& __p
)
5279 * @brief Resets the distribution state.
5286 * @brief Return the @f$a@f$ parameter of the distribution.
5290 { return _M_param
.a(); }
5293 * @brief Return the @f$b@f$ parameter of the distribution.
5297 { return _M_param
.b(); }
5300 * @brief Returns the parameter set of the distribution.
5304 { return _M_param
; }
5307 * @brief Sets the parameter set of the distribution.
5308 * @param __param The new parameter set of the distribution.
5311 param(const param_type
& __param
)
5312 { _M_param
= __param
; }
5315 * @brief Returns the greatest lower bound value of the distribution.
5319 { return std::numeric_limits
<result_type
>::lowest(); }
5322 * @brief Returns the least upper bound value of the distribution.
5326 { return std::numeric_limits
<result_type
>::max(); }
5329 * @brief Generating functions.
5331 template<typename _UniformRandomNumberGenerator
>
5333 operator()(_UniformRandomNumberGenerator
& __urng
)
5334 { return this->operator()(__urng
, _M_param
); }
5336 template<typename _UniformRandomNumberGenerator
>
5338 operator()(_UniformRandomNumberGenerator
& __urng
,
5339 const param_type
& __p
);
5341 template<typename _ForwardIterator
,
5342 typename _UniformRandomNumberGenerator
>
5344 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5345 _UniformRandomNumberGenerator
& __urng
)
5346 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5348 template<typename _ForwardIterator
,
5349 typename _UniformRandomNumberGenerator
>
5351 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5352 _UniformRandomNumberGenerator
& __urng
,
5353 const param_type
& __p
)
5354 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5356 template<typename _UniformRandomNumberGenerator
>
5358 __generate(result_type
* __f
, result_type
* __t
,
5359 _UniformRandomNumberGenerator
& __urng
,
5360 const param_type
& __p
)
5361 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5364 * @brief Return true if two extreme value distributions have the same
5368 operator==(const extreme_value_distribution
& __d1
,
5369 const extreme_value_distribution
& __d2
)
5370 { return __d1
._M_param
== __d2
._M_param
; }
5373 template<typename _ForwardIterator
,
5374 typename _UniformRandomNumberGenerator
>
5376 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5377 _UniformRandomNumberGenerator
& __urng
,
5378 const param_type
& __p
);
5380 param_type _M_param
;
5383 #if __cpp_impl_three_way_comparison < 201907L
5385 * @brief Return true if two extreme value distributions have different
5388 template<typename _RealType
>
5390 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
5391 const std::extreme_value_distribution
<_RealType
>& __d2
)
5392 { return !(__d1
== __d2
); }
5396 * @brief Inserts a %extreme_value_distribution random number distribution
5397 * @p __x into the output stream @p __os.
5399 * @param __os An output stream.
5400 * @param __x A %extreme_value_distribution random number distribution.
5402 * @returns The output stream with the state of @p __x inserted or in
5405 template<typename _RealType
, typename _CharT
, typename _Traits
>
5406 std::basic_ostream
<_CharT
, _Traits
>&
5407 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5408 const std::extreme_value_distribution
<_RealType
>& __x
);
5411 * @brief Extracts a %extreme_value_distribution random number
5412 * distribution @p __x from the input stream @p __is.
5414 * @param __is An input stream.
5415 * @param __x A %extreme_value_distribution random number
5418 * @returns The input stream with @p __x extracted or in an error state.
5420 template<typename _RealType
, typename _CharT
, typename _Traits
>
5421 std::basic_istream
<_CharT
, _Traits
>&
5422 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5423 std::extreme_value_distribution
<_RealType
>& __x
);
5425 /// @} group random_distributions_poisson
5428 * @addtogroup random_distributions_sampling Sampling Distributions
5429 * @ingroup random_distributions
5434 * @brief A discrete_distribution random number distribution.
5436 * This distribution produces random numbers @f$ i, 0 \leq i < n @f$,
5437 * distributed according to the probability mass function
5438 * @f$ p(i | p_0, ..., p_{n-1}) = p_i @f$.
5440 * @headerfile random
5443 template<typename _IntType
= int>
5444 class discrete_distribution
5446 static_assert(std::is_integral
<_IntType
>::value
,
5447 "result_type must be an integral type");
5450 /** The type of the range of the distribution. */
5451 typedef _IntType result_type
;
5453 /** Parameter type. */
5456 typedef discrete_distribution
<_IntType
> distribution_type
;
5457 friend class discrete_distribution
<_IntType
>;
5460 : _M_prob(), _M_cp()
5463 template<typename _InputIterator
>
5464 param_type(_InputIterator __wbegin
,
5465 _InputIterator __wend
)
5466 : _M_prob(__wbegin
, __wend
), _M_cp()
5467 { _M_initialize(); }
5469 param_type(initializer_list
<double> __wil
)
5470 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
5471 { _M_initialize(); }
5473 template<typename _Func
>
5474 param_type(size_t __nw
, double __xmin
, double __xmax
,
5477 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5478 param_type(const param_type
&) = default;
5479 param_type
& operator=(const param_type
&) = default;
5482 probabilities() const
5483 { return _M_prob
.empty() ? std::vector
<double>(1, 1.0) : _M_prob
; }
5486 operator==(const param_type
& __p1
, const param_type
& __p2
)
5487 { return __p1
._M_prob
== __p2
._M_prob
; }
5489 #if __cpp_impl_three_way_comparison < 201907L
5491 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5492 { return !(__p1
== __p2
); }
5499 std::vector
<double> _M_prob
;
5500 std::vector
<double> _M_cp
;
5503 discrete_distribution()
5507 template<typename _InputIterator
>
5508 discrete_distribution(_InputIterator __wbegin
,
5509 _InputIterator __wend
)
5510 : _M_param(__wbegin
, __wend
)
5513 discrete_distribution(initializer_list
<double> __wl
)
5517 template<typename _Func
>
5518 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
5520 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5524 discrete_distribution(const param_type
& __p
)
5529 * @brief Resets the distribution state.
5536 * @brief Returns the probabilities of the distribution.
5539 probabilities() const
5541 return _M_param
._M_prob
.empty()
5542 ? std::vector
<double>(1, 1.0) : _M_param
._M_prob
;
5546 * @brief Returns the parameter set of the distribution.
5550 { return _M_param
; }
5553 * @brief Sets the parameter set of the distribution.
5554 * @param __param The new parameter set of the distribution.
5557 param(const param_type
& __param
)
5558 { _M_param
= __param
; }
5561 * @brief Returns the greatest lower bound value of the distribution.
5565 { return result_type(0); }
5568 * @brief Returns the least upper bound value of the distribution.
5573 return _M_param
._M_prob
.empty()
5574 ? result_type(0) : result_type(_M_param
._M_prob
.size() - 1);
5578 * @brief Generating functions.
5580 template<typename _UniformRandomNumberGenerator
>
5582 operator()(_UniformRandomNumberGenerator
& __urng
)
5583 { return this->operator()(__urng
, _M_param
); }
5585 template<typename _UniformRandomNumberGenerator
>
5587 operator()(_UniformRandomNumberGenerator
& __urng
,
5588 const param_type
& __p
);
5590 template<typename _ForwardIterator
,
5591 typename _UniformRandomNumberGenerator
>
5593 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5594 _UniformRandomNumberGenerator
& __urng
)
5595 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5597 template<typename _ForwardIterator
,
5598 typename _UniformRandomNumberGenerator
>
5600 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5601 _UniformRandomNumberGenerator
& __urng
,
5602 const param_type
& __p
)
5603 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5605 template<typename _UniformRandomNumberGenerator
>
5607 __generate(result_type
* __f
, result_type
* __t
,
5608 _UniformRandomNumberGenerator
& __urng
,
5609 const param_type
& __p
)
5610 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5613 * @brief Return true if two discrete distributions have the same
5617 operator==(const discrete_distribution
& __d1
,
5618 const discrete_distribution
& __d2
)
5619 { return __d1
._M_param
== __d2
._M_param
; }
5622 * @brief Inserts a %discrete_distribution random number distribution
5623 * @p __x into the output stream @p __os.
5625 * @param __os An output stream.
5626 * @param __x A %discrete_distribution random number distribution.
5628 * @returns The output stream with the state of @p __x inserted or in
5631 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5632 friend std::basic_ostream
<_CharT
, _Traits
>&
5633 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5634 const std::discrete_distribution
<_IntType1
>& __x
);
5637 * @brief Extracts a %discrete_distribution random number distribution
5638 * @p __x from the input stream @p __is.
5640 * @param __is An input stream.
5641 * @param __x A %discrete_distribution random number
5644 * @returns The input stream with @p __x extracted or in an error
5647 template<typename _IntType1
, typename _CharT
, typename _Traits
>
5648 friend std::basic_istream
<_CharT
, _Traits
>&
5649 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5650 std::discrete_distribution
<_IntType1
>& __x
);
5653 template<typename _ForwardIterator
,
5654 typename _UniformRandomNumberGenerator
>
5656 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5657 _UniformRandomNumberGenerator
& __urng
,
5658 const param_type
& __p
);
5660 param_type _M_param
;
5663 #if __cpp_impl_three_way_comparison < 201907L
5665 * @brief Return true if two discrete distributions have different
5668 template<typename _IntType
>
5670 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
5671 const std::discrete_distribution
<_IntType
>& __d2
)
5672 { return !(__d1
== __d2
); }
5676 * @brief A piecewise_constant_distribution random number distribution.
5678 * This distribution produces random numbers @f$ x, b_0 \leq x < b_n @f$,
5679 * uniformly distributed over each subinterval @f$ [b_i, b_{i+1}) @f$
5680 * according to the probability mass function
5682 * p(x | b_0, ..., b_n, \rho_0, ..., \rho_{n-1})
5683 * = \rho_i \cdot \frac{b_{i+1} - x}{b_{i+1} - b_i}
5684 * + \rho_{i+1} \cdot \frac{ x - b_i}{b_{i+1} - b_i}
5686 * for @f$ b_i \leq x < b_{i+1} @f$.
5688 * @headerfile random
5691 template<typename _RealType
= double>
5692 class piecewise_constant_distribution
5694 static_assert(std::is_floating_point
<_RealType
>::value
,
5695 "result_type must be a floating point type");
5698 /** The type of the range of the distribution. */
5699 typedef _RealType result_type
;
5701 /** Parameter type. */
5704 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
5705 friend class piecewise_constant_distribution
<_RealType
>;
5708 : _M_int(), _M_den(), _M_cp()
5711 template<typename _InputIteratorB
, typename _InputIteratorW
>
5712 param_type(_InputIteratorB __bfirst
,
5713 _InputIteratorB __bend
,
5714 _InputIteratorW __wbegin
);
5716 template<typename _Func
>
5717 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
5719 template<typename _Func
>
5720 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5723 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5724 param_type(const param_type
&) = default;
5725 param_type
& operator=(const param_type
&) = default;
5727 std::vector
<_RealType
>
5732 std::vector
<_RealType
> __tmp(2);
5733 __tmp
[1] = _RealType(1);
5742 { return _M_den
.empty() ? std::vector
<double>(1, 1.0) : _M_den
; }
5745 operator==(const param_type
& __p1
, const param_type
& __p2
)
5746 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
5748 #if __cpp_impl_three_way_comparison < 201907L
5750 operator!=(const param_type
& __p1
, const param_type
& __p2
)
5751 { return !(__p1
== __p2
); }
5758 std::vector
<_RealType
> _M_int
;
5759 std::vector
<double> _M_den
;
5760 std::vector
<double> _M_cp
;
5763 piecewise_constant_distribution()
5767 template<typename _InputIteratorB
, typename _InputIteratorW
>
5768 piecewise_constant_distribution(_InputIteratorB __bfirst
,
5769 _InputIteratorB __bend
,
5770 _InputIteratorW __wbegin
)
5771 : _M_param(__bfirst
, __bend
, __wbegin
)
5774 template<typename _Func
>
5775 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
5777 : _M_param(__bl
, __fw
)
5780 template<typename _Func
>
5781 piecewise_constant_distribution(size_t __nw
,
5782 _RealType __xmin
, _RealType __xmax
,
5784 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5788 piecewise_constant_distribution(const param_type
& __p
)
5793 * @brief Resets the distribution state.
5800 * @brief Returns a vector of the intervals.
5802 std::vector
<_RealType
>
5805 if (_M_param
._M_int
.empty())
5807 std::vector
<_RealType
> __tmp(2);
5808 __tmp
[1] = _RealType(1);
5812 return _M_param
._M_int
;
5816 * @brief Returns a vector of the probability densities.
5821 return _M_param
._M_den
.empty()
5822 ? std::vector
<double>(1, 1.0) : _M_param
._M_den
;
5826 * @brief Returns the parameter set of the distribution.
5830 { return _M_param
; }
5833 * @brief Sets the parameter set of the distribution.
5834 * @param __param The new parameter set of the distribution.
5837 param(const param_type
& __param
)
5838 { _M_param
= __param
; }
5841 * @brief Returns the greatest lower bound value of the distribution.
5846 return _M_param
._M_int
.empty()
5847 ? result_type(0) : _M_param
._M_int
.front();
5851 * @brief Returns the least upper bound value of the distribution.
5856 return _M_param
._M_int
.empty()
5857 ? result_type(1) : _M_param
._M_int
.back();
5861 * @brief Generating functions.
5863 template<typename _UniformRandomNumberGenerator
>
5865 operator()(_UniformRandomNumberGenerator
& __urng
)
5866 { return this->operator()(__urng
, _M_param
); }
5868 template<typename _UniformRandomNumberGenerator
>
5870 operator()(_UniformRandomNumberGenerator
& __urng
,
5871 const param_type
& __p
);
5873 template<typename _ForwardIterator
,
5874 typename _UniformRandomNumberGenerator
>
5876 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5877 _UniformRandomNumberGenerator
& __urng
)
5878 { this->__generate(__f
, __t
, __urng
, _M_param
); }
5880 template<typename _ForwardIterator
,
5881 typename _UniformRandomNumberGenerator
>
5883 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
5884 _UniformRandomNumberGenerator
& __urng
,
5885 const param_type
& __p
)
5886 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5888 template<typename _UniformRandomNumberGenerator
>
5890 __generate(result_type
* __f
, result_type
* __t
,
5891 _UniformRandomNumberGenerator
& __urng
,
5892 const param_type
& __p
)
5893 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
5896 * @brief Return true if two piecewise constant distributions have the
5900 operator==(const piecewise_constant_distribution
& __d1
,
5901 const piecewise_constant_distribution
& __d2
)
5902 { return __d1
._M_param
== __d2
._M_param
; }
5905 * @brief Inserts a %piecewise_constant_distribution random
5906 * number distribution @p __x into the output stream @p __os.
5908 * @param __os An output stream.
5909 * @param __x A %piecewise_constant_distribution random number
5912 * @returns The output stream with the state of @p __x inserted or in
5915 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5916 friend std::basic_ostream
<_CharT
, _Traits
>&
5917 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5918 const std::piecewise_constant_distribution
<_RealType1
>& __x
);
5921 * @brief Extracts a %piecewise_constant_distribution random
5922 * number distribution @p __x from the input stream @p __is.
5924 * @param __is An input stream.
5925 * @param __x A %piecewise_constant_distribution random number
5928 * @returns The input stream with @p __x extracted or in an error
5931 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5932 friend std::basic_istream
<_CharT
, _Traits
>&
5933 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5934 std::piecewise_constant_distribution
<_RealType1
>& __x
);
5937 template<typename _ForwardIterator
,
5938 typename _UniformRandomNumberGenerator
>
5940 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
5941 _UniformRandomNumberGenerator
& __urng
,
5942 const param_type
& __p
);
5944 param_type _M_param
;
5947 #if __cpp_impl_three_way_comparison < 201907L
5949 * @brief Return true if two piecewise constant distributions have
5950 * different parameters.
5952 template<typename _RealType
>
5954 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5955 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5956 { return !(__d1
== __d2
); }
5960 * @brief A piecewise_linear_distribution random number distribution.
5962 * This distribution produces random numbers @f$ x, b_0 \leq x < b_n @f$,
5963 * distributed over each subinterval @f$ [b_i, b_{i+1}) @f$
5964 * according to the probability mass function
5965 * @f$ p(x | b_0, ..., b_n, \rho_0, ..., \rho_n) = \rho_i @f$,
5966 * for @f$ b_i \leq x < b_{i+1} @f$.
5968 * @headerfile random
5971 template<typename _RealType
= double>
5972 class piecewise_linear_distribution
5974 static_assert(std::is_floating_point
<_RealType
>::value
,
5975 "result_type must be a floating point type");
5978 /** The type of the range of the distribution. */
5979 typedef _RealType result_type
;
5981 /** Parameter type. */
5984 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5985 friend class piecewise_linear_distribution
<_RealType
>;
5988 : _M_int(), _M_den(), _M_cp(), _M_m()
5991 template<typename _InputIteratorB
, typename _InputIteratorW
>
5992 param_type(_InputIteratorB __bfirst
,
5993 _InputIteratorB __bend
,
5994 _InputIteratorW __wbegin
);
5996 template<typename _Func
>
5997 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5999 template<typename _Func
>
6000 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
6003 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
6004 param_type(const param_type
&) = default;
6005 param_type
& operator=(const param_type
&) = default;
6007 std::vector
<_RealType
>
6012 std::vector
<_RealType
> __tmp(2);
6013 __tmp
[1] = _RealType(1);
6022 { return _M_den
.empty() ? std::vector
<double>(2, 1.0) : _M_den
; }
6025 operator==(const param_type
& __p1
, const param_type
& __p2
)
6026 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
6028 #if __cpp_impl_three_way_comparison < 201907L
6030 operator!=(const param_type
& __p1
, const param_type
& __p2
)
6031 { return !(__p1
== __p2
); }
6038 std::vector
<_RealType
> _M_int
;
6039 std::vector
<double> _M_den
;
6040 std::vector
<double> _M_cp
;
6041 std::vector
<double> _M_m
;
6044 piecewise_linear_distribution()
6048 template<typename _InputIteratorB
, typename _InputIteratorW
>
6049 piecewise_linear_distribution(_InputIteratorB __bfirst
,
6050 _InputIteratorB __bend
,
6051 _InputIteratorW __wbegin
)
6052 : _M_param(__bfirst
, __bend
, __wbegin
)
6055 template<typename _Func
>
6056 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
6058 : _M_param(__bl
, __fw
)
6061 template<typename _Func
>
6062 piecewise_linear_distribution(size_t __nw
,
6063 _RealType __xmin
, _RealType __xmax
,
6065 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
6069 piecewise_linear_distribution(const param_type
& __p
)
6074 * Resets the distribution state.
6081 * @brief Return the intervals of the distribution.
6083 std::vector
<_RealType
>
6086 if (_M_param
._M_int
.empty())
6088 std::vector
<_RealType
> __tmp(2);
6089 __tmp
[1] = _RealType(1);
6093 return _M_param
._M_int
;
6097 * @brief Return a vector of the probability densities of the
6103 return _M_param
._M_den
.empty()
6104 ? std::vector
<double>(2, 1.0) : _M_param
._M_den
;
6108 * @brief Returns the parameter set of the distribution.
6112 { return _M_param
; }
6115 * @brief Sets the parameter set of the distribution.
6116 * @param __param The new parameter set of the distribution.
6119 param(const param_type
& __param
)
6120 { _M_param
= __param
; }
6123 * @brief Returns the greatest lower bound value of the distribution.
6128 return _M_param
._M_int
.empty()
6129 ? result_type(0) : _M_param
._M_int
.front();
6133 * @brief Returns the least upper bound value of the distribution.
6138 return _M_param
._M_int
.empty()
6139 ? result_type(1) : _M_param
._M_int
.back();
6143 * @brief Generating functions.
6145 template<typename _UniformRandomNumberGenerator
>
6147 operator()(_UniformRandomNumberGenerator
& __urng
)
6148 { return this->operator()(__urng
, _M_param
); }
6150 template<typename _UniformRandomNumberGenerator
>
6152 operator()(_UniformRandomNumberGenerator
& __urng
,
6153 const param_type
& __p
);
6155 template<typename _ForwardIterator
,
6156 typename _UniformRandomNumberGenerator
>
6158 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
6159 _UniformRandomNumberGenerator
& __urng
)
6160 { this->__generate(__f
, __t
, __urng
, _M_param
); }
6162 template<typename _ForwardIterator
,
6163 typename _UniformRandomNumberGenerator
>
6165 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
6166 _UniformRandomNumberGenerator
& __urng
,
6167 const param_type
& __p
)
6168 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
6170 template<typename _UniformRandomNumberGenerator
>
6172 __generate(result_type
* __f
, result_type
* __t
,
6173 _UniformRandomNumberGenerator
& __urng
,
6174 const param_type
& __p
)
6175 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
6178 * @brief Return true if two piecewise linear distributions have the
6182 operator==(const piecewise_linear_distribution
& __d1
,
6183 const piecewise_linear_distribution
& __d2
)
6184 { return __d1
._M_param
== __d2
._M_param
; }
6187 * @brief Inserts a %piecewise_linear_distribution random number
6188 * distribution @p __x into the output stream @p __os.
6190 * @param __os An output stream.
6191 * @param __x A %piecewise_linear_distribution random number
6194 * @returns The output stream with the state of @p __x inserted or in
6197 template<typename _RealType1
, typename _CharT
, typename _Traits
>
6198 friend std::basic_ostream
<_CharT
, _Traits
>&
6199 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
6200 const std::piecewise_linear_distribution
<_RealType1
>& __x
);
6203 * @brief Extracts a %piecewise_linear_distribution random number
6204 * distribution @p __x from the input stream @p __is.
6206 * @param __is An input stream.
6207 * @param __x A %piecewise_linear_distribution random number
6210 * @returns The input stream with @p __x extracted or in an error
6213 template<typename _RealType1
, typename _CharT
, typename _Traits
>
6214 friend std::basic_istream
<_CharT
, _Traits
>&
6215 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
6216 std::piecewise_linear_distribution
<_RealType1
>& __x
);
6219 template<typename _ForwardIterator
,
6220 typename _UniformRandomNumberGenerator
>
6222 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
6223 _UniformRandomNumberGenerator
& __urng
,
6224 const param_type
& __p
);
6226 param_type _M_param
;
6229 #if __cpp_impl_three_way_comparison < 201907L
6231 * @brief Return true if two piecewise linear distributions have
6232 * different parameters.
6234 template<typename _RealType
>
6236 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
6237 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
6238 { return !(__d1
== __d2
); }
6241 /// @} group random_distributions_sampling
6243 /// @} *group random_distributions
6246 * @addtogroup random_utilities Random Number Utilities
6252 * @brief The seed_seq class generates sequences of seeds for random
6253 * number generators.
6255 * @headerfile random
6261 /** The type of the seed vales. */
6262 typedef uint_least32_t result_type
;
6264 /** Default constructor. */
6269 template<typename _IntType
, typename
= _Require
<is_integral
<_IntType
>>>
6270 seed_seq(std::initializer_list
<_IntType
> __il
);
6272 template<typename _InputIterator
>
6273 seed_seq(_InputIterator __begin
, _InputIterator __end
);
6275 // generating functions
6276 template<typename _RandomAccessIterator
>
6278 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
6280 // property functions
6281 size_t size() const noexcept
6282 { return _M_v
.size(); }
6284 template<typename _OutputIterator
>
6286 param(_OutputIterator __dest
) const
6287 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
6289 // no copy functions
6290 seed_seq(const seed_seq
&) = delete;
6291 seed_seq
& operator=(const seed_seq
&) = delete;
6294 std::vector
<result_type
> _M_v
;
6297 /// @} group random_utilities
6301 _GLIBCXX_END_NAMESPACE_VERSION