1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009, 2010 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 * You should not attempt to use it directly.
35 // [26.4] Random number generation
38 * @defgroup random Random Number Generation
41 * A facility for generating random numbers on selected distributions.
46 * @brief A function template for converting the output of a (integral)
47 * uniform random number generator to a floatng point result in the range
50 template<typename _RealType
, size_t __bits
,
51 typename _UniformRandomNumberGenerator
>
53 generate_canonical(_UniformRandomNumberGenerator
& __g
);
56 * Implementation-space details.
60 template<typename _UIntType
, size_t __w
,
61 bool = __w
< static_cast<size_t>
62 (std::numeric_limits
<_UIntType
>::digits
)>
64 { static const _UIntType __value
= 0; };
66 template<typename _UIntType
, size_t __w
>
67 struct _Shift
<_UIntType
, __w
, true>
68 { static const _UIntType __value
= _UIntType(1) << __w
; };
70 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool>
73 // Dispatch based on modulus value to prevent divide-by-zero compile-time
74 // errors when m == 0.
75 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
78 { return _Mod
<_Tp
, __m
, __a
, __c
, __m
== 0>::__calc(__x
); }
81 * An adaptor class for converting the output of any Generator into
82 * the input for a specific Distribution.
84 template<typename _Engine
, typename _DInputType
>
89 _Adaptor(_Engine
& __g
)
94 { return _DInputType(0); }
98 { return _DInputType(1); }
101 * Converts a value generated by the adapted random number generator
102 * into a value in the input domain for the dependent random number
108 return std::generate_canonical
<_DInputType
,
109 std::numeric_limits
<_DInputType
>::digits
,
116 } // namespace __detail
119 * @addtogroup random_generators Random Number Generators
122 * These classes define objects which provide random or pseudorandom
123 * numbers, either from a discrete or a continuous interval. The
124 * random number generator supplied as a part of this library are
125 * all uniform random number generators which provide a sequence of
126 * random number uniformly distributed over their range.
128 * A number generator is a function object with an operator() that
129 * takes zero arguments and returns a number.
131 * A compliant random number generator must satisfy the following
132 * requirements. <table border=1 cellpadding=10 cellspacing=0>
133 * <caption align=top>Random Number Generator Requirements</caption>
134 * <tr><td>To be documented.</td></tr> </table>
140 * @brief A model of a linear congruential random number generator.
142 * A random number generator that produces pseudorandom numbers via
145 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
148 * The template parameter @p _UIntType must be an unsigned integral type
149 * large enough to store values up to (__m-1). If the template parameter
150 * @p __m is 0, the modulus @p __m used is
151 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
152 * parameters @p __a and @p __c must be less than @p __m.
154 * The size of the state is @f$1@f$.
156 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
157 class linear_congruential_engine
159 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
160 "substituting _UIntType not an unsigned integral type");
161 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
162 "template argument substituting __m out of bounds");
165 /** The type of the generated random value. */
166 typedef _UIntType result_type
;
168 /** The multiplier. */
169 static const result_type multiplier
= __a
;
171 static const result_type increment
= __c
;
173 static const result_type modulus
= __m
;
174 static const result_type default_seed
= 1u;
177 * @brief Constructs a %linear_congruential_engine random number
178 * generator engine with seed @p __s. The default seed value
181 * @param __s The initial seed value.
184 linear_congruential_engine(result_type __s
= default_seed
)
188 * @brief Constructs a %linear_congruential_engine random number
189 * generator engine seeded from the seed sequence @p __q.
191 * @param __q the seed sequence.
193 template<typename _Sseq
, typename
= typename
194 std::enable_if
<!std::is_same
<_Sseq
, linear_congruential_engine
>::value
>
197 linear_congruential_engine(_Sseq
& __q
)
201 * @brief Reseeds the %linear_congruential_engine random number generator
202 * engine sequence to the seed @p __s.
204 * @param __s The new seed.
207 seed(result_type __s
= default_seed
);
210 * @brief Reseeds the %linear_congruential_engine random number generator
212 * sequence using values from the seed sequence @p __q.
214 * @param __q the seed sequence.
216 template<typename _Sseq
>
217 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
221 * @brief Gets the smallest possible value in the output range.
223 * The minimum depends on the @p __c parameter: if it is zero, the
224 * minimum generated must be > 0, otherwise 0 is allowed.
226 * @todo This should be constexpr.
230 { return __c
== 0u ? 1u : 0u; }
233 * @brief Gets the largest possible value in the output range.
235 * @todo This should be constexpr.
242 * @brief Discard a sequence of random numbers.
244 * @todo Look for a faster way to do discard.
247 discard(unsigned long long __z
)
249 for (; __z
!= 0ULL; --__z
)
254 * @brief Gets the next random number in the sequence.
259 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
264 * @brief Compares two linear congruential random number generator
265 * objects of the same type for equality.
267 * @param __lhs A linear congruential random number generator object.
268 * @param __rhs Another linear congruential random number generator
271 * @returns true if the infinite sequences of generated values
272 * would be equal, false otherwise.
275 operator==(const linear_congruential_engine
& __lhs
,
276 const linear_congruential_engine
& __rhs
)
277 { return __lhs
._M_x
== __rhs
._M_x
; }
280 * @brief Writes the textual representation of the state x(i) of x to
283 * @param __os The output stream.
284 * @param __lcr A % linear_congruential_engine random number generator.
287 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
288 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
289 friend std::basic_ostream
<_CharT
, _Traits
>&
290 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
291 const std::linear_congruential_engine
<_UIntType1
,
295 * @brief Sets the state of the engine by reading its textual
296 * representation from @p __is.
298 * The textual representation must have been previously written using
299 * an output stream whose imbued locale and whose type's template
300 * specialization arguments _CharT and _Traits were the same as those
303 * @param __is The input stream.
304 * @param __lcr A % linear_congruential_engine random number generator.
307 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
308 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
309 friend std::basic_istream
<_CharT
, _Traits
>&
310 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
311 std::linear_congruential_engine
<_UIntType1
, __a1
,
319 * @brief Compares two linear congruential random number generator
320 * objects of the same type for inequality.
322 * @param __lhs A linear congruential random number generator object.
323 * @param __rhs Another linear congruential random number generator
326 * @returns true if the infinite sequences of generated values
327 * would be different, false otherwise.
329 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
331 operator!=(const std::linear_congruential_engine
<_UIntType
, __a
,
333 const std::linear_congruential_engine
<_UIntType
, __a
,
335 { return !(__lhs
== __rhs
); }
339 * A generalized feedback shift register discrete random number generator.
341 * This algorithm avoids multiplication and division and is designed to be
342 * friendly to a pipelined architecture. If the parameters are chosen
343 * correctly, this generator will produce numbers with a very long period and
344 * fairly good apparent entropy, although still not cryptographically strong.
346 * The best way to use this generator is with the predefined mt19937 class.
348 * This algorithm was originally invented by Makoto Matsumoto and
351 * @var word_size The number of bits in each element of the state vector.
352 * @var state_size The degree of recursion.
353 * @var shift_size The period parameter.
354 * @var mask_bits The separation point bit index.
355 * @var parameter_a The last row of the twist matrix.
356 * @var output_u The first right-shift tempering matrix parameter.
357 * @var output_s The first left-shift tempering matrix parameter.
358 * @var output_b The first left-shift tempering matrix mask.
359 * @var output_t The second left-shift tempering matrix parameter.
360 * @var output_c The second left-shift tempering matrix mask.
361 * @var output_l The second right-shift tempering matrix parameter.
363 template<typename _UIntType
, size_t __w
,
364 size_t __n
, size_t __m
, size_t __r
,
365 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
366 _UIntType __b
, size_t __t
,
367 _UIntType __c
, size_t __l
, _UIntType __f
>
368 class mersenne_twister_engine
370 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
371 "substituting _UIntType not an unsigned integral type");
372 static_assert(1u <= __m
&& __m
<= __n
,
373 "template argument substituting __m out of bounds");
374 static_assert(__r
<= __w
, "template argument substituting "
376 static_assert(__u
<= __w
, "template argument substituting "
378 static_assert(__s
<= __w
, "template argument substituting "
380 static_assert(__t
<= __w
, "template argument substituting "
382 static_assert(__l
<= __w
, "template argument substituting "
384 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
385 "template argument substituting __w out of bound");
386 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
387 "template argument substituting __a out of bound");
388 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
389 "template argument substituting __b out of bound");
390 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
391 "template argument substituting __c out of bound");
392 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
393 "template argument substituting __d out of bound");
394 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
395 "template argument substituting __f out of bound");
398 /** The type of the generated random value. */
399 typedef _UIntType result_type
;
402 static const size_t word_size
= __w
;
403 static const size_t state_size
= __n
;
404 static const size_t shift_size
= __m
;
405 static const size_t mask_bits
= __r
;
406 static const result_type xor_mask
= __a
;
407 static const size_t tempering_u
= __u
;
408 static const result_type tempering_d
= __d
;
409 static const size_t tempering_s
= __s
;
410 static const result_type tempering_b
= __b
;
411 static const size_t tempering_t
= __t
;
412 static const result_type tempering_c
= __c
;
413 static const size_t tempering_l
= __l
;
414 static const result_type initialization_multiplier
= __f
;
415 static const result_type default_seed
= 5489u;
417 // constructors and member function
419 mersenne_twister_engine(result_type __sd
= default_seed
)
423 * @brief Constructs a %mersenne_twister_engine random number generator
424 * engine seeded from the seed sequence @p __q.
426 * @param __q the seed sequence.
428 template<typename _Sseq
, typename
= typename
429 std::enable_if
<!std::is_same
<_Sseq
, mersenne_twister_engine
>::value
>
432 mersenne_twister_engine(_Sseq
& __q
)
436 seed(result_type __sd
= default_seed
);
438 template<typename _Sseq
>
439 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
443 * @brief Gets the smallest possible value in the output range.
445 * @todo This should be constexpr.
452 * @brief Gets the largest possible value in the output range.
454 * @todo This should be constexpr.
458 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
461 * @brief Discard a sequence of random numbers.
463 * @todo Look for a faster way to do discard.
466 discard(unsigned long long __z
)
468 for (; __z
!= 0ULL; --__z
)
476 * @brief Compares two % mersenne_twister_engine random number generator
477 * objects of the same type for equality.
479 * @param __lhs A % mersenne_twister_engine random number generator
481 * @param __rhs Another % mersenne_twister_engine random number
484 * @returns true if the infinite sequences of generated values
485 * would be equal, false otherwise.
488 operator==(const mersenne_twister_engine
& __lhs
,
489 const mersenne_twister_engine
& __rhs
)
490 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
493 * @brief Inserts the current state of a % mersenne_twister_engine
494 * random number generator engine @p __x into the output stream
497 * @param __os An output stream.
498 * @param __x A % mersenne_twister_engine random number generator
501 * @returns The output stream with the state of @p __x inserted or in
504 template<typename _UIntType1
,
505 size_t __w1
, size_t __n1
,
506 size_t __m1
, size_t __r1
,
507 _UIntType1 __a1
, size_t __u1
,
508 _UIntType1 __d1
, size_t __s1
,
509 _UIntType1 __b1
, size_t __t1
,
510 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
511 typename _CharT
, typename _Traits
>
512 friend std::basic_ostream
<_CharT
, _Traits
>&
513 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
514 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
515 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
519 * @brief Extracts the current state of a % mersenne_twister_engine
520 * random number generator engine @p __x from the input stream
523 * @param __is An input stream.
524 * @param __x A % mersenne_twister_engine random number generator
527 * @returns The input stream with the state of @p __x extracted or in
530 template<typename _UIntType1
,
531 size_t __w1
, size_t __n1
,
532 size_t __m1
, size_t __r1
,
533 _UIntType1 __a1
, size_t __u1
,
534 _UIntType1 __d1
, size_t __s1
,
535 _UIntType1 __b1
, size_t __t1
,
536 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
537 typename _CharT
, typename _Traits
>
538 friend std::basic_istream
<_CharT
, _Traits
>&
539 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
540 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
541 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
545 _UIntType _M_x
[state_size
];
550 * @brief Compares two % mersenne_twister_engine random number generator
551 * objects of the same type for inequality.
553 * @param __lhs A % mersenne_twister_engine random number generator
555 * @param __rhs Another % mersenne_twister_engine random number
558 * @returns true if the infinite sequences of generated values
559 * would be different, false otherwise.
561 template<typename _UIntType
, size_t __w
,
562 size_t __n
, size_t __m
, size_t __r
,
563 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
564 _UIntType __b
, size_t __t
,
565 _UIntType __c
, size_t __l
, _UIntType __f
>
567 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
568 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
569 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
570 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
571 { return !(__lhs
== __rhs
); }
575 * @brief The Marsaglia-Zaman generator.
577 * This is a model of a Generalized Fibonacci discrete random number
578 * generator, sometimes referred to as the SWC generator.
580 * A discrete random number generator that produces pseudorandom
583 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
586 * The size of the state is @f$r@f$
587 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
589 * @var _M_x The state of the generator. This is a ring buffer.
590 * @var _M_carry The carry.
591 * @var _M_p Current index of x(i - r).
593 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
594 class subtract_with_carry_engine
596 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
597 "substituting _UIntType not an unsigned integral type");
598 static_assert(0u < __s
&& __s
< __r
,
599 "template argument substituting __s out of bounds");
600 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
601 "template argument substituting __w out of bounds");
604 /** The type of the generated random value. */
605 typedef _UIntType result_type
;
608 static const size_t word_size
= __w
;
609 static const size_t short_lag
= __s
;
610 static const size_t long_lag
= __r
;
611 static const result_type default_seed
= 19780503u;
614 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
615 * random number generator.
618 subtract_with_carry_engine(result_type __sd
= default_seed
)
622 * @brief Constructs a %subtract_with_carry_engine random number engine
623 * seeded from the seed sequence @p __q.
625 * @param __q the seed sequence.
627 template<typename _Sseq
, typename
= typename
628 std::enable_if
<!std::is_same
<_Sseq
, subtract_with_carry_engine
>::value
>
631 subtract_with_carry_engine(_Sseq
& __q
)
635 * @brief Seeds the initial state @f$x_0@f$ of the random number
638 * N1688[4.19] modifies this as follows. If @p __value == 0,
639 * sets value to 19780503. In any case, with a linear
640 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
641 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
642 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
643 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
644 * set carry to 1, otherwise sets carry to 0.
647 seed(result_type __sd
= default_seed
);
650 * @brief Seeds the initial state @f$x_0@f$ of the
651 * % subtract_with_carry_engine random number generator.
653 template<typename _Sseq
>
654 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
658 * @brief Gets the inclusive minimum value of the range of random
659 * integers returned by this generator.
661 * @todo This should be constexpr.
668 * @brief Gets the inclusive maximum value of the range of random
669 * integers returned by this generator.
671 * @todo This should be constexpr.
675 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
678 * @brief Discard a sequence of random numbers.
680 * @todo Look for a faster way to do discard.
683 discard(unsigned long long __z
)
685 for (; __z
!= 0ULL; --__z
)
690 * @brief Gets the next random number in the sequence.
696 * @brief Compares two % subtract_with_carry_engine random number
697 * generator objects of the same type for equality.
699 * @param __lhs A % subtract_with_carry_engine random number generator
701 * @param __rhs Another % subtract_with_carry_engine random number
704 * @returns true if the infinite sequences of generated values
705 * would be equal, false otherwise.
708 operator==(const subtract_with_carry_engine
& __lhs
,
709 const subtract_with_carry_engine
& __rhs
)
710 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
713 * @brief Inserts the current state of a % subtract_with_carry_engine
714 * random number generator engine @p __x into the output stream
717 * @param __os An output stream.
718 * @param __x A % subtract_with_carry_engine random number generator
721 * @returns The output stream with the state of @p __x inserted or in
724 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
725 typename _CharT
, typename _Traits
>
726 friend std::basic_ostream
<_CharT
, _Traits
>&
727 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
728 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
732 * @brief Extracts the current state of a % subtract_with_carry_engine
733 * random number generator engine @p __x from the input stream
736 * @param __is An input stream.
737 * @param __x A % subtract_with_carry_engine random number generator
740 * @returns The input stream with the state of @p __x extracted or in
743 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
744 typename _CharT
, typename _Traits
>
745 friend std::basic_istream
<_CharT
, _Traits
>&
746 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
747 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
751 _UIntType _M_x
[long_lag
];
757 * @brief Compares two % subtract_with_carry_engine random number
758 * generator objects of the same type for inequality.
760 * @param __lhs A % subtract_with_carry_engine random number generator
762 * @param __rhs Another % subtract_with_carry_engine random number
765 * @returns true if the infinite sequences of generated values
766 * would be different, false otherwise.
768 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
770 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
772 const std::subtract_with_carry_engine
<_UIntType
, __w
,
774 { return !(__lhs
== __rhs
); }
778 * Produces random numbers from some base engine by discarding blocks of
781 * 0 <= @p __r <= @p __p
783 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
784 class discard_block_engine
786 static_assert(1 <= __r
&& __r
<= __p
,
787 "template argument substituting __r out of bounds");
790 /** The type of the generated random value. */
791 typedef typename
_RandomNumberEngine::result_type result_type
;
794 static const size_t block_size
= __p
;
795 static const size_t used_block
= __r
;
798 * @brief Constructs a default %discard_block_engine engine.
800 * The underlying engine is default constructed as well.
802 discard_block_engine()
803 : _M_b(), _M_n(0) { }
806 * @brief Copy constructs a %discard_block_engine engine.
808 * Copies an existing base class random number generator.
809 * @param rng An existing (base class) engine object.
812 discard_block_engine(const _RandomNumberEngine
& __rne
)
813 : _M_b(__rne
), _M_n(0) { }
816 * @brief Move constructs a %discard_block_engine engine.
818 * Copies an existing base class random number generator.
819 * @param rng An existing (base class) engine object.
822 discard_block_engine(_RandomNumberEngine
&& __rne
)
823 : _M_b(std::move(__rne
)), _M_n(0) { }
826 * @brief Seed constructs a %discard_block_engine engine.
828 * Constructs the underlying generator engine seeded with @p __s.
829 * @param __s A seed value for the base class engine.
832 discard_block_engine(result_type __s
)
833 : _M_b(__s
), _M_n(0) { }
836 * @brief Generator construct a %discard_block_engine engine.
838 * @param __q A seed sequence.
840 template<typename _Sseq
, typename
= typename
841 std::enable_if
<!std::is_same
<_Sseq
, discard_block_engine
>::value
842 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
845 discard_block_engine(_Sseq
& __q
)
850 * @brief Reseeds the %discard_block_engine object with the default
851 * seed for the underlying base class generator engine.
861 * @brief Reseeds the %discard_block_engine object with the default
862 * seed for the underlying base class generator engine.
865 seed(result_type __s
)
872 * @brief Reseeds the %discard_block_engine object with the given seed
874 * @param __q A seed generator function.
876 template<typename _Sseq
>
885 * @brief Gets a const reference to the underlying generator engine
888 const _RandomNumberEngine
&
893 * @brief Gets the minimum value in the generated random number range.
895 * @todo This should be constexpr.
899 { return _M_b
.min(); }
902 * @brief Gets the maximum value in the generated random number range.
904 * @todo This should be constexpr.
908 { return _M_b
.max(); }
911 * @brief Discard a sequence of random numbers.
913 * @todo Look for a faster way to do discard.
916 discard(unsigned long long __z
)
918 for (; __z
!= 0ULL; --__z
)
923 * @brief Gets the next value in the generated random number sequence.
929 * @brief Compares two %discard_block_engine random number generator
930 * objects of the same type for equality.
932 * @param __lhs A %discard_block_engine random number generator object.
933 * @param __rhs Another %discard_block_engine random number generator
936 * @returns true if the infinite sequences of generated values
937 * would be equal, false otherwise.
940 operator==(const discard_block_engine
& __lhs
,
941 const discard_block_engine
& __rhs
)
942 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
945 * @brief Inserts the current state of a %discard_block_engine random
946 * number generator engine @p __x into the output stream
949 * @param __os An output stream.
950 * @param __x A %discard_block_engine random number generator engine.
952 * @returns The output stream with the state of @p __x inserted or in
955 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
956 typename _CharT
, typename _Traits
>
957 friend std::basic_ostream
<_CharT
, _Traits
>&
958 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
959 const std::discard_block_engine
<_RandomNumberEngine1
,
963 * @brief Extracts the current state of a % subtract_with_carry_engine
964 * random number generator engine @p __x from the input stream
967 * @param __is An input stream.
968 * @param __x A %discard_block_engine random number generator engine.
970 * @returns The input stream with the state of @p __x extracted or in
973 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
974 typename _CharT
, typename _Traits
>
975 friend std::basic_istream
<_CharT
, _Traits
>&
976 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
977 std::discard_block_engine
<_RandomNumberEngine1
,
981 _RandomNumberEngine _M_b
;
986 * @brief Compares two %discard_block_engine random number generator
987 * objects of the same type for inequality.
989 * @param __lhs A %discard_block_engine random number generator object.
990 * @param __rhs Another %discard_block_engine random number generator
993 * @returns true if the infinite sequences of generated values
994 * would be different, false otherwise.
996 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
998 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1000 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
1002 { return !(__lhs
== __rhs
); }
1006 * Produces random numbers by combining random numbers from some base
1007 * engine to produce random numbers with a specifies number of bits @p __w.
1009 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1010 class independent_bits_engine
1012 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
1013 "substituting _UIntType not an unsigned integral type");
1014 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
1015 "template argument substituting __w out of bounds");
1018 /** The type of the generated random value. */
1019 typedef _UIntType result_type
;
1022 * @brief Constructs a default %independent_bits_engine engine.
1024 * The underlying engine is default constructed as well.
1026 independent_bits_engine()
1030 * @brief Copy constructs a %independent_bits_engine engine.
1032 * Copies an existing base class random number generator.
1033 * @param rng An existing (base class) engine object.
1036 independent_bits_engine(const _RandomNumberEngine
& __rne
)
1040 * @brief Move constructs a %independent_bits_engine engine.
1042 * Copies an existing base class random number generator.
1043 * @param rng An existing (base class) engine object.
1046 independent_bits_engine(_RandomNumberEngine
&& __rne
)
1047 : _M_b(std::move(__rne
)) { }
1050 * @brief Seed constructs a %independent_bits_engine engine.
1052 * Constructs the underlying generator engine seeded with @p __s.
1053 * @param __s A seed value for the base class engine.
1056 independent_bits_engine(result_type __s
)
1060 * @brief Generator construct a %independent_bits_engine engine.
1062 * @param __q A seed sequence.
1064 template<typename _Sseq
, typename
= typename
1065 std::enable_if
<!std::is_same
<_Sseq
, independent_bits_engine
>::value
1066 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1069 independent_bits_engine(_Sseq
& __q
)
1074 * @brief Reseeds the %independent_bits_engine object with the default
1075 * seed for the underlying base class generator engine.
1082 * @brief Reseeds the %independent_bits_engine object with the default
1083 * seed for the underlying base class generator engine.
1086 seed(result_type __s
)
1090 * @brief Reseeds the %independent_bits_engine object with the given
1092 * @param __q A seed generator function.
1094 template<typename _Sseq
>
1100 * @brief Gets a const reference to the underlying generator engine
1103 const _RandomNumberEngine
&
1108 * @brief Gets the minimum value in the generated random number range.
1110 * @todo This should be constexpr.
1117 * @brief Gets the maximum value in the generated random number range.
1119 * @todo This should be constexpr.
1123 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1126 * @brief Discard a sequence of random numbers.
1128 * @todo Look for a faster way to do discard.
1131 discard(unsigned long long __z
)
1133 for (; __z
!= 0ULL; --__z
)
1138 * @brief Gets the next value in the generated random number sequence.
1144 * @brief Compares two %independent_bits_engine random number generator
1145 * objects of the same type for equality.
1147 * @param __lhs A %independent_bits_engine random number generator
1149 * @param __rhs Another %independent_bits_engine random number generator
1152 * @returns true if the infinite sequences of generated values
1153 * would be equal, false otherwise.
1156 operator==(const independent_bits_engine
& __lhs
,
1157 const independent_bits_engine
& __rhs
)
1158 { return __lhs
._M_b
== __rhs
._M_b
; }
1161 * @brief Extracts the current state of a % subtract_with_carry_engine
1162 * random number generator engine @p __x from the input stream
1165 * @param __is An input stream.
1166 * @param __x A %independent_bits_engine random number generator
1169 * @returns The input stream with the state of @p __x extracted or in
1172 template<typename _CharT
, typename _Traits
>
1173 friend std::basic_istream
<_CharT
, _Traits
>&
1174 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1175 std::independent_bits_engine
<_RandomNumberEngine
,
1176 __w
, _UIntType
>& __x
)
1183 _RandomNumberEngine _M_b
;
1187 * @brief Compares two %independent_bits_engine random number generator
1188 * objects of the same type for inequality.
1190 * @param __lhs A %independent_bits_engine random number generator
1192 * @param __rhs Another %independent_bits_engine random number generator
1195 * @returns true if the infinite sequences of generated values
1196 * would be different, false otherwise.
1198 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1200 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1202 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1204 { return !(__lhs
== __rhs
); }
1207 * @brief Inserts the current state of a %independent_bits_engine random
1208 * number generator engine @p __x into the output stream @p __os.
1210 * @param __os An output stream.
1211 * @param __x A %independent_bits_engine random number generator engine.
1213 * @returns The output stream with the state of @p __x inserted or in
1216 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1217 typename _CharT
, typename _Traits
>
1218 std::basic_ostream
<_CharT
, _Traits
>&
1219 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1220 const std::independent_bits_engine
<_RandomNumberEngine
,
1221 __w
, _UIntType
>& __x
)
1229 * @brief Produces random numbers by combining random numbers from some
1230 * base engine to produce random numbers with a specifies number of bits
1233 template<typename _RandomNumberEngine
, size_t __k
>
1234 class shuffle_order_engine
1236 static_assert(1u <= __k
, "template argument substituting "
1237 "__k out of bound");
1240 /** The type of the generated random value. */
1241 typedef typename
_RandomNumberEngine::result_type result_type
;
1243 static const size_t table_size
= __k
;
1246 * @brief Constructs a default %shuffle_order_engine engine.
1248 * The underlying engine is default constructed as well.
1250 shuffle_order_engine()
1252 { _M_initialize(); }
1255 * @brief Copy constructs a %shuffle_order_engine engine.
1257 * Copies an existing base class random number generator.
1258 * @param rng An existing (base class) engine object.
1261 shuffle_order_engine(const _RandomNumberEngine
& __rne
)
1263 { _M_initialize(); }
1266 * @brief Move constructs a %shuffle_order_engine engine.
1268 * Copies an existing base class random number generator.
1269 * @param rng An existing (base class) engine object.
1272 shuffle_order_engine(_RandomNumberEngine
&& __rne
)
1273 : _M_b(std::move(__rne
))
1274 { _M_initialize(); }
1277 * @brief Seed constructs a %shuffle_order_engine engine.
1279 * Constructs the underlying generator engine seeded with @p __s.
1280 * @param __s A seed value for the base class engine.
1283 shuffle_order_engine(result_type __s
)
1285 { _M_initialize(); }
1288 * @brief Generator construct a %shuffle_order_engine engine.
1290 * @param __q A seed sequence.
1292 template<typename _Sseq
, typename
= typename
1293 std::enable_if
<!std::is_same
<_Sseq
, shuffle_order_engine
>::value
1294 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1297 shuffle_order_engine(_Sseq
& __q
)
1299 { _M_initialize(); }
1302 * @brief Reseeds the %shuffle_order_engine object with the default seed
1303 for the underlying base class generator engine.
1313 * @brief Reseeds the %shuffle_order_engine object with the default seed
1314 * for the underlying base class generator engine.
1317 seed(result_type __s
)
1324 * @brief Reseeds the %shuffle_order_engine object with the given seed
1326 * @param __q A seed generator function.
1328 template<typename _Sseq
>
1337 * Gets a const reference to the underlying generator engine object.
1339 const _RandomNumberEngine
&
1344 * Gets the minimum value in the generated random number range.
1346 * @todo This should be constexpr.
1350 { return _M_b
.min(); }
1353 * Gets the maximum value in the generated random number range.
1355 * @todo This should be constexpr.
1359 { return _M_b
.max(); }
1362 * Discard a sequence of random numbers.
1364 * @todo Look for a faster way to do discard.
1367 discard(unsigned long long __z
)
1369 for (; __z
!= 0ULL; --__z
)
1374 * Gets the next value in the generated random number sequence.
1380 * Compares two %shuffle_order_engine random number generator objects
1381 * of the same type for equality.
1383 * @param __lhs A %shuffle_order_engine random number generator object.
1384 * @param __rhs Another %shuffle_order_engine random number generator
1387 * @returns true if the infinite sequences of generated values
1388 * would be equal, false otherwise.
1391 operator==(const shuffle_order_engine
& __lhs
,
1392 const shuffle_order_engine
& __rhs
)
1393 { return __lhs
._M_b
== __rhs
._M_b
; }
1396 * @brief Inserts the current state of a %shuffle_order_engine random
1397 * number generator engine @p __x into the output stream
1400 * @param __os An output stream.
1401 * @param __x A %shuffle_order_engine random number generator engine.
1403 * @returns The output stream with the state of @p __x inserted or in
1406 template<typename _RandomNumberEngine1
, size_t __k1
,
1407 typename _CharT
, typename _Traits
>
1408 friend std::basic_ostream
<_CharT
, _Traits
>&
1409 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1410 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1414 * @brief Extracts the current state of a % subtract_with_carry_engine
1415 * random number generator engine @p __x from the input stream
1418 * @param __is An input stream.
1419 * @param __x A %shuffle_order_engine random number generator engine.
1421 * @returns The input stream with the state of @p __x extracted or in
1424 template<typename _RandomNumberEngine1
, size_t __k1
,
1425 typename _CharT
, typename _Traits
>
1426 friend std::basic_istream
<_CharT
, _Traits
>&
1427 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1428 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>&);
1431 void _M_initialize()
1433 for (size_t __i
= 0; __i
< __k
; ++__i
)
1438 _RandomNumberEngine _M_b
;
1439 result_type _M_v
[__k
];
1444 * Compares two %shuffle_order_engine random number generator objects
1445 * of the same type for inequality.
1447 * @param __lhs A %shuffle_order_engine random number generator object.
1448 * @param __rhs Another %shuffle_order_engine random number generator
1451 * @returns true if the infinite sequences of generated values
1452 * would be different, false otherwise.
1454 template<typename _RandomNumberEngine
, size_t __k
>
1456 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1458 const std::shuffle_order_engine
<_RandomNumberEngine
,
1460 { return !(__lhs
== __rhs
); }
1464 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1466 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1470 * An alternative LCR (Lehmer Generator function).
1472 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1476 * The classic Mersenne Twister.
1479 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1480 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1481 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1483 typedef mersenne_twister_engine
<
1489 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1492 * An alternative Mersenne Twister.
1494 typedef mersenne_twister_engine
<
1497 0xb5026f5aa96619e9ULL
, 29,
1498 0x5555555555555555ULL
, 17,
1499 0x71d67fffeda60000ULL
, 37,
1500 0xfff7eee000000000ULL
, 43,
1501 6364136223846793005ULL> mt19937_64
;
1503 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1506 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1509 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1511 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1513 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1515 typedef minstd_rand0 default_random_engine
;
1518 * A standard interface to a platform-specific non-deterministic
1519 * random number generator (if any are available).
1524 /** The type of the generated random value. */
1525 typedef unsigned int result_type
;
1527 // constructors, destructors and member functions
1529 #ifdef _GLIBCXX_USE_RANDOM_TR1
1532 random_device(const std::string
& __token
= "/dev/urandom")
1534 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1535 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1536 std::__throw_runtime_error(__N("random_device::"
1537 "random_device(const std::string&)"));
1541 { std::fclose(_M_file
); }
1546 random_device(const std::string
& __token
= "mt19937")
1547 : _M_mt(_M_strtoul(__token
)) { }
1550 static unsigned long
1551 _M_strtoul(const std::string
& __str
)
1553 unsigned long __ret
= 5489UL;
1554 if (__str
!= "mt19937")
1556 const char* __nptr
= __str
.c_str();
1558 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1559 if (*__nptr
== '\0' || *__endptr
!= '\0')
1560 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1561 "(const std::string&)"));
1572 { return std::numeric_limits
<result_type
>::min(); }
1576 { return std::numeric_limits
<result_type
>::max(); }
1585 #ifdef _GLIBCXX_USE_RANDOM_TR1
1587 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1595 // No copy functions.
1596 random_device(const random_device
&) = delete;
1597 void operator=(const random_device
&) = delete;
1601 #ifdef _GLIBCXX_USE_RANDOM_TR1
1608 /* @} */ // group random_generators
1611 * @addtogroup random_distributions Random Number Distributions
1617 * @addtogroup random_distributions_uniform Uniform Distributions
1618 * @ingroup random_distributions
1623 * @brief Uniform discrete distribution for random numbers.
1624 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1625 * probability throughout the range.
1627 template<typename _IntType
= int>
1628 class uniform_int_distribution
1630 static_assert(std::is_integral
<_IntType
>::value
,
1631 "template argument not an integral type");
1634 /** The type of the range of the distribution. */
1635 typedef _IntType result_type
;
1636 /** Parameter type. */
1639 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1642 param_type(_IntType __a
= 0,
1643 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1644 : _M_a(__a
), _M_b(__b
)
1646 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1658 operator==(const param_type
& __p1
, const param_type
& __p2
)
1659 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1668 * @brief Constructs a uniform distribution object.
1671 uniform_int_distribution(_IntType __a
= 0,
1672 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1673 : _M_param(__a
, __b
)
1677 uniform_int_distribution(const param_type
& __p
)
1682 * @brief Resets the distribution state.
1684 * Does nothing for the uniform integer distribution.
1691 { return _M_param
.a(); }
1695 { return _M_param
.b(); }
1698 * @brief Returns the parameter set of the distribution.
1702 { return _M_param
; }
1705 * @brief Sets the parameter set of the distribution.
1706 * @param __param The new parameter set of the distribution.
1709 param(const param_type
& __param
)
1710 { _M_param
= __param
; }
1713 * @brief Returns the inclusive lower bound of the distribution range.
1717 { return this->a(); }
1720 * @brief Returns the inclusive upper bound of the distribution range.
1724 { return this->b(); }
1727 * @brief Generating functions.
1729 template<typename _UniformRandomNumberGenerator
>
1731 operator()(_UniformRandomNumberGenerator
& __urng
)
1732 { return this->operator()(__urng
, this->param()); }
1734 template<typename _UniformRandomNumberGenerator
>
1736 operator()(_UniformRandomNumberGenerator
& __urng
,
1737 const param_type
& __p
);
1739 param_type _M_param
;
1743 * @brief Return true if two uniform integer distributions have
1744 * the same parameters.
1746 template<typename _IntType
>
1748 operator==(const std::uniform_int_distribution
<_IntType
>& __d1
,
1749 const std::uniform_int_distribution
<_IntType
>& __d2
)
1750 { return __d1
.param() == __d2
.param(); }
1753 * @brief Return true if two uniform integer distributions have
1754 * different parameters.
1756 template<typename _IntType
>
1758 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1759 const std::uniform_int_distribution
<_IntType
>& __d2
)
1760 { return !(__d1
== __d2
); }
1763 * @brief Inserts a %uniform_int_distribution random number
1764 * distribution @p __x into the output stream @p os.
1766 * @param __os An output stream.
1767 * @param __x A %uniform_int_distribution random number distribution.
1769 * @returns The output stream with the state of @p __x inserted or in
1772 template<typename _IntType
, typename _CharT
, typename _Traits
>
1773 std::basic_ostream
<_CharT
, _Traits
>&
1774 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1775 const std::uniform_int_distribution
<_IntType
>&);
1778 * @brief Extracts a %uniform_int_distribution random number distribution
1779 * @p __x from the input stream @p __is.
1781 * @param __is An input stream.
1782 * @param __x A %uniform_int_distribution random number generator engine.
1784 * @returns The input stream with @p __x extracted or in an error state.
1786 template<typename _IntType
, typename _CharT
, typename _Traits
>
1787 std::basic_istream
<_CharT
, _Traits
>&
1788 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1789 std::uniform_int_distribution
<_IntType
>&);
1793 * @brief Uniform continuous distribution for random numbers.
1795 * A continuous random distribution on the range [min, max) with equal
1796 * probability throughout the range. The URNG should be real-valued and
1797 * deliver number in the range [0, 1).
1799 template<typename _RealType
= double>
1800 class uniform_real_distribution
1802 static_assert(std::is_floating_point
<_RealType
>::value
,
1803 "template argument not a floating point type");
1806 /** The type of the range of the distribution. */
1807 typedef _RealType result_type
;
1808 /** Parameter type. */
1811 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1814 param_type(_RealType __a
= _RealType(0),
1815 _RealType __b
= _RealType(1))
1816 : _M_a(__a
), _M_b(__b
)
1818 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1830 operator==(const param_type
& __p1
, const param_type
& __p2
)
1831 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1840 * @brief Constructs a uniform_real_distribution object.
1842 * @param __min [IN] The lower bound of the distribution.
1843 * @param __max [IN] The upper bound of the distribution.
1846 uniform_real_distribution(_RealType __a
= _RealType(0),
1847 _RealType __b
= _RealType(1))
1848 : _M_param(__a
, __b
)
1852 uniform_real_distribution(const param_type
& __p
)
1857 * @brief Resets the distribution state.
1859 * Does nothing for the uniform real distribution.
1866 { return _M_param
.a(); }
1870 { return _M_param
.b(); }
1873 * @brief Returns the parameter set of the distribution.
1877 { return _M_param
; }
1880 * @brief Sets the parameter set of the distribution.
1881 * @param __param The new parameter set of the distribution.
1884 param(const param_type
& __param
)
1885 { _M_param
= __param
; }
1888 * @brief Returns the inclusive lower bound of the distribution range.
1892 { return this->a(); }
1895 * @brief Returns the inclusive upper bound of the distribution range.
1899 { return this->b(); }
1902 * @brief Generating functions.
1904 template<typename _UniformRandomNumberGenerator
>
1906 operator()(_UniformRandomNumberGenerator
& __urng
)
1907 { return this->operator()(__urng
, this->param()); }
1909 template<typename _UniformRandomNumberGenerator
>
1911 operator()(_UniformRandomNumberGenerator
& __urng
,
1912 const param_type
& __p
)
1914 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1916 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1920 param_type _M_param
;
1924 * @brief Return true if two uniform real distributions have
1925 * the same parameters.
1927 template<typename _IntType
>
1929 operator==(const std::uniform_real_distribution
<_IntType
>& __d1
,
1930 const std::uniform_real_distribution
<_IntType
>& __d2
)
1931 { return __d1
.param() == __d2
.param(); }
1934 * @brief Return true if two uniform real distributions have
1935 * different parameters.
1937 template<typename _IntType
>
1939 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1940 const std::uniform_real_distribution
<_IntType
>& __d2
)
1941 { return !(__d1
== __d2
); }
1944 * @brief Inserts a %uniform_real_distribution random number
1945 * distribution @p __x into the output stream @p __os.
1947 * @param __os An output stream.
1948 * @param __x A %uniform_real_distribution random number distribution.
1950 * @returns The output stream with the state of @p __x inserted or in
1953 template<typename _RealType
, typename _CharT
, typename _Traits
>
1954 std::basic_ostream
<_CharT
, _Traits
>&
1955 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1956 const std::uniform_real_distribution
<_RealType
>&);
1959 * @brief Extracts a %uniform_real_distribution random number distribution
1960 * @p __x from the input stream @p __is.
1962 * @param __is An input stream.
1963 * @param __x A %uniform_real_distribution random number generator engine.
1965 * @returns The input stream with @p __x extracted or in an error state.
1967 template<typename _RealType
, typename _CharT
, typename _Traits
>
1968 std::basic_istream
<_CharT
, _Traits
>&
1969 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1970 std::uniform_real_distribution
<_RealType
>&);
1972 /* @} */ // group random_distributions_uniform
1975 * @addtogroup random_distributions_normal Normal Distributions
1976 * @ingroup random_distributions
1981 * @brief A normal continuous distribution for random numbers.
1983 * The formula for the normal probability density function is
1985 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1986 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1989 template<typename _RealType
= double>
1990 class normal_distribution
1992 static_assert(std::is_floating_point
<_RealType
>::value
,
1993 "template argument not a floating point type");
1996 /** The type of the range of the distribution. */
1997 typedef _RealType result_type
;
1998 /** Parameter type. */
2001 typedef normal_distribution
<_RealType
> distribution_type
;
2004 param_type(_RealType __mean
= _RealType(0),
2005 _RealType __stddev
= _RealType(1))
2006 : _M_mean(__mean
), _M_stddev(__stddev
)
2008 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
2017 { return _M_stddev
; }
2020 operator==(const param_type
& __p1
, const param_type
& __p2
)
2021 { return (__p1
._M_mean
== __p2
._M_mean
2022 && __p1
._M_stddev
== __p2
._M_stddev
); }
2026 _RealType _M_stddev
;
2031 * Constructs a normal distribution with parameters @f$mean@f$ and
2032 * standard deviation.
2035 normal_distribution(result_type __mean
= result_type(0),
2036 result_type __stddev
= result_type(1))
2037 : _M_param(__mean
, __stddev
), _M_saved_available(false)
2041 normal_distribution(const param_type
& __p
)
2042 : _M_param(__p
), _M_saved_available(false)
2046 * @brief Resets the distribution state.
2050 { _M_saved_available
= false; }
2053 * @brief Returns the mean of the distribution.
2057 { return _M_param
.mean(); }
2060 * @brief Returns the standard deviation of the distribution.
2064 { return _M_param
.stddev(); }
2067 * @brief Returns the parameter set of the distribution.
2071 { return _M_param
; }
2074 * @brief Sets the parameter set of the distribution.
2075 * @param __param The new parameter set of the distribution.
2078 param(const param_type
& __param
)
2079 { _M_param
= __param
; }
2082 * @brief Returns the greatest lower bound value of the distribution.
2086 { return std::numeric_limits
<result_type
>::min(); }
2089 * @brief Returns the least upper bound value of the distribution.
2093 { return std::numeric_limits
<result_type
>::max(); }
2096 * @brief Generating functions.
2098 template<typename _UniformRandomNumberGenerator
>
2100 operator()(_UniformRandomNumberGenerator
& __urng
)
2101 { return this->operator()(__urng
, this->param()); }
2103 template<typename _UniformRandomNumberGenerator
>
2105 operator()(_UniformRandomNumberGenerator
& __urng
,
2106 const param_type
& __p
);
2109 * @brief Return true if two normal distributions have
2110 * the same parameters and the sequences that would
2111 * be generated are equal.
2113 template<typename _RealType1
>
2115 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2116 const std::normal_distribution
<_RealType1
>& __d2
);
2119 * @brief Inserts a %normal_distribution random number distribution
2120 * @p __x into the output stream @p __os.
2122 * @param __os An output stream.
2123 * @param __x A %normal_distribution random number distribution.
2125 * @returns The output stream with the state of @p __x inserted or in
2128 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2129 friend std::basic_ostream
<_CharT
, _Traits
>&
2130 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2131 const std::normal_distribution
<_RealType1
>&);
2134 * @brief Extracts a %normal_distribution random number distribution
2135 * @p __x from the input stream @p __is.
2137 * @param __is An input stream.
2138 * @param __x A %normal_distribution random number generator engine.
2140 * @returns The input stream with @p __x extracted or in an error
2143 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2144 friend std::basic_istream
<_CharT
, _Traits
>&
2145 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2146 std::normal_distribution
<_RealType1
>&);
2149 param_type _M_param
;
2150 result_type _M_saved
;
2151 bool _M_saved_available
;
2155 * @brief Return true if two normal distributions are different.
2157 template<typename _RealType
>
2159 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2160 const std::normal_distribution
<_RealType
>& __d2
)
2161 { return !(__d1
== __d2
); }
2165 * @brief A lognormal_distribution random number distribution.
2167 * The formula for the normal probability mass function is
2169 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2170 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2173 template<typename _RealType
= double>
2174 class lognormal_distribution
2176 static_assert(std::is_floating_point
<_RealType
>::value
,
2177 "template argument not a floating point type");
2180 /** The type of the range of the distribution. */
2181 typedef _RealType result_type
;
2182 /** Parameter type. */
2185 typedef lognormal_distribution
<_RealType
> distribution_type
;
2188 param_type(_RealType __m
= _RealType(0),
2189 _RealType __s
= _RealType(1))
2190 : _M_m(__m
), _M_s(__s
)
2202 operator==(const param_type
& __p1
, const param_type
& __p2
)
2203 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2211 lognormal_distribution(_RealType __m
= _RealType(0),
2212 _RealType __s
= _RealType(1))
2213 : _M_param(__m
, __s
), _M_nd()
2217 lognormal_distribution(const param_type
& __p
)
2218 : _M_param(__p
), _M_nd()
2222 * Resets the distribution state.
2233 { return _M_param
.m(); }
2237 { return _M_param
.s(); }
2240 * @brief Returns the parameter set of the distribution.
2244 { return _M_param
; }
2247 * @brief Sets the parameter set of the distribution.
2248 * @param __param The new parameter set of the distribution.
2251 param(const param_type
& __param
)
2252 { _M_param
= __param
; }
2255 * @brief Returns the greatest lower bound value of the distribution.
2259 { return result_type(0); }
2262 * @brief Returns the least upper bound value of the distribution.
2266 { return std::numeric_limits
<result_type
>::max(); }
2269 * @brief Generating functions.
2271 template<typename _UniformRandomNumberGenerator
>
2273 operator()(_UniformRandomNumberGenerator
& __urng
)
2274 { return this->operator()(__urng
, this->param()); }
2276 template<typename _UniformRandomNumberGenerator
>
2278 operator()(_UniformRandomNumberGenerator
& __urng
,
2279 const param_type
& __p
)
2280 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2283 * @brief Return true if two lognormal distributions have
2284 * the same parameters and the sequences that would
2285 * be generated are equal.
2287 template<typename _RealType1
>
2289 operator==(const std::lognormal_distribution
<_RealType1
>& __d1
,
2290 const std::lognormal_distribution
<_RealType1
>& __d2
)
2291 { return (__d1
.param() == __d2
.param()
2292 && __d1
._M_nd
== __d2
._M_nd
); }
2295 * @brief Inserts a %lognormal_distribution random number distribution
2296 * @p __x into the output stream @p __os.
2298 * @param __os An output stream.
2299 * @param __x A %lognormal_distribution random number distribution.
2301 * @returns The output stream with the state of @p __x inserted or in
2304 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2305 friend std::basic_ostream
<_CharT
, _Traits
>&
2306 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2307 const std::lognormal_distribution
<_RealType1
>&);
2310 * @brief Extracts a %lognormal_distribution random number distribution
2311 * @p __x from the input stream @p __is.
2313 * @param __is An input stream.
2314 * @param __x A %lognormal_distribution random number
2317 * @returns The input stream with @p __x extracted or in an error state.
2319 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2320 friend std::basic_istream
<_CharT
, _Traits
>&
2321 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2322 std::lognormal_distribution
<_RealType1
>&);
2325 param_type _M_param
;
2327 std::normal_distribution
<result_type
> _M_nd
;
2331 * @brief Return true if two lognormal distributions are different.
2333 template<typename _RealType
>
2335 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2336 const std::lognormal_distribution
<_RealType
>& __d2
)
2337 { return !(__d1
== __d2
); }
2341 * @brief A gamma continuous distribution for random numbers.
2343 * The formula for the gamma probability density function is:
2345 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2346 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2349 template<typename _RealType
= double>
2350 class gamma_distribution
2352 static_assert(std::is_floating_point
<_RealType
>::value
,
2353 "template argument not a floating point type");
2356 /** The type of the range of the distribution. */
2357 typedef _RealType result_type
;
2358 /** Parameter type. */
2361 typedef gamma_distribution
<_RealType
> distribution_type
;
2362 friend class gamma_distribution
<_RealType
>;
2365 param_type(_RealType __alpha_val
= _RealType(1),
2366 _RealType __beta_val
= _RealType(1))
2367 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2369 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2375 { return _M_alpha
; }
2382 operator==(const param_type
& __p1
, const param_type
& __p2
)
2383 { return (__p1
._M_alpha
== __p2
._M_alpha
2384 && __p1
._M_beta
== __p2
._M_beta
); }
2393 _RealType _M_malpha
, _M_a2
;
2398 * @brief Constructs a gamma distribution with parameters
2399 * @f$\alpha@f$ and @f$\beta@f$.
2402 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2403 _RealType __beta_val
= _RealType(1))
2404 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2408 gamma_distribution(const param_type
& __p
)
2409 : _M_param(__p
), _M_nd()
2413 * @brief Resets the distribution state.
2420 * @brief Returns the @f$\alpha@f$ of the distribution.
2424 { return _M_param
.alpha(); }
2427 * @brief Returns the @f$\beta@f$ of the distribution.
2431 { return _M_param
.beta(); }
2434 * @brief Returns the parameter set of the distribution.
2438 { return _M_param
; }
2441 * @brief Sets the parameter set of the distribution.
2442 * @param __param The new parameter set of the distribution.
2445 param(const param_type
& __param
)
2446 { _M_param
= __param
; }
2449 * @brief Returns the greatest lower bound value of the distribution.
2453 { return result_type(0); }
2456 * @brief Returns the least upper bound value of the distribution.
2460 { return std::numeric_limits
<result_type
>::max(); }
2463 * @brief Generating functions.
2465 template<typename _UniformRandomNumberGenerator
>
2467 operator()(_UniformRandomNumberGenerator
& __urng
)
2468 { return this->operator()(__urng
, this->param()); }
2470 template<typename _UniformRandomNumberGenerator
>
2472 operator()(_UniformRandomNumberGenerator
& __urng
,
2473 const param_type
& __p
);
2476 * @brief Return true if two gamma distributions have the same
2477 * parameters and the sequences that would be generated
2480 template<typename _RealType1
>
2482 operator==(const std::gamma_distribution
<_RealType1
>& __d1
,
2483 const std::gamma_distribution
<_RealType1
>& __d2
)
2484 { return (__d1
.param() == __d2
.param()
2485 && __d1
._M_nd
== __d2
._M_nd
); }
2488 * @brief Inserts a %gamma_distribution random number distribution
2489 * @p __x into the output stream @p __os.
2491 * @param __os An output stream.
2492 * @param __x A %gamma_distribution random number distribution.
2494 * @returns The output stream with the state of @p __x inserted or in
2497 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2498 friend std::basic_ostream
<_CharT
, _Traits
>&
2499 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2500 const std::gamma_distribution
<_RealType1
>&);
2503 * @brief Extracts a %gamma_distribution random number distribution
2504 * @p __x from the input stream @p __is.
2506 * @param __is An input stream.
2507 * @param __x A %gamma_distribution random number generator engine.
2509 * @returns The input stream with @p __x extracted or in an error state.
2511 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2512 friend std::basic_istream
<_CharT
, _Traits
>&
2513 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2514 std::gamma_distribution
<_RealType1
>&);
2517 param_type _M_param
;
2519 std::normal_distribution
<result_type
> _M_nd
;
2523 * @brief Return true if two gamma distributions are different.
2525 template<typename _RealType
>
2527 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2528 const std::gamma_distribution
<_RealType
>& __d2
)
2529 { return !(__d1
== __d2
); }
2533 * @brief A chi_squared_distribution random number distribution.
2535 * The formula for the normal probability mass function is
2536 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2538 template<typename _RealType
= double>
2539 class chi_squared_distribution
2541 static_assert(std::is_floating_point
<_RealType
>::value
,
2542 "template argument not a floating point type");
2545 /** The type of the range of the distribution. */
2546 typedef _RealType result_type
;
2547 /** Parameter type. */
2550 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2553 param_type(_RealType __n
= _RealType(1))
2562 operator==(const param_type
& __p1
, const param_type
& __p2
)
2563 { return __p1
._M_n
== __p2
._M_n
; }
2570 chi_squared_distribution(_RealType __n
= _RealType(1))
2571 : _M_param(__n
), _M_gd(__n
/ 2)
2575 chi_squared_distribution(const param_type
& __p
)
2576 : _M_param(__p
), _M_gd(__p
.n() / 2)
2580 * @brief Resets the distribution state.
2591 { return _M_param
.n(); }
2594 * @brief Returns the parameter set of the distribution.
2598 { return _M_param
; }
2601 * @brief Sets the parameter set of the distribution.
2602 * @param __param The new parameter set of the distribution.
2605 param(const param_type
& __param
)
2606 { _M_param
= __param
; }
2609 * @brief Returns the greatest lower bound value of the distribution.
2613 { return result_type(0); }
2616 * @brief Returns the least upper bound value of the distribution.
2620 { return std::numeric_limits
<result_type
>::max(); }
2623 * @brief Generating functions.
2625 template<typename _UniformRandomNumberGenerator
>
2627 operator()(_UniformRandomNumberGenerator
& __urng
)
2628 { return 2 * _M_gd(__urng
); }
2630 template<typename _UniformRandomNumberGenerator
>
2632 operator()(_UniformRandomNumberGenerator
& __urng
,
2633 const param_type
& __p
)
2635 typedef typename
std::gamma_distribution
<result_type
>::param_type
2637 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2641 * @brief Return true if two Chi-squared distributions have
2642 * the same parameters and the sequences that would be
2643 * generated are equal.
2645 template<typename _RealType1
>
2647 operator==(const std::chi_squared_distribution
<_RealType1
>& __d1
,
2648 const std::chi_squared_distribution
<_RealType1
>& __d2
)
2649 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
2652 * @brief Inserts a %chi_squared_distribution random number distribution
2653 * @p __x into the output stream @p __os.
2655 * @param __os An output stream.
2656 * @param __x A %chi_squared_distribution random number distribution.
2658 * @returns The output stream with the state of @p __x inserted or in
2661 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2662 friend std::basic_ostream
<_CharT
, _Traits
>&
2663 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2664 const std::chi_squared_distribution
<_RealType1
>&);
2667 * @brief Extracts a %chi_squared_distribution random number distribution
2668 * @p __x from the input stream @p __is.
2670 * @param __is An input stream.
2671 * @param __x A %chi_squared_distribution random number
2674 * @returns The input stream with @p __x extracted or in an error state.
2676 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2677 friend std::basic_istream
<_CharT
, _Traits
>&
2678 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2679 std::chi_squared_distribution
<_RealType1
>&);
2682 param_type _M_param
;
2684 std::gamma_distribution
<result_type
> _M_gd
;
2688 * @brief Return true if two Chi-squared distributions are different.
2690 template<typename _RealType
>
2692 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2693 const std::chi_squared_distribution
<_RealType
>& __d2
)
2694 { return !(__d1
== __d2
); }
2698 * @brief A cauchy_distribution random number distribution.
2700 * The formula for the normal probability mass function is
2701 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2703 template<typename _RealType
= double>
2704 class cauchy_distribution
2706 static_assert(std::is_floating_point
<_RealType
>::value
,
2707 "template argument not a floating point type");
2710 /** The type of the range of the distribution. */
2711 typedef _RealType result_type
;
2712 /** Parameter type. */
2715 typedef cauchy_distribution
<_RealType
> distribution_type
;
2718 param_type(_RealType __a
= _RealType(0),
2719 _RealType __b
= _RealType(1))
2720 : _M_a(__a
), _M_b(__b
)
2732 operator==(const param_type
& __p1
, const param_type
& __p2
)
2733 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2741 cauchy_distribution(_RealType __a
= _RealType(0),
2742 _RealType __b
= _RealType(1))
2743 : _M_param(__a
, __b
)
2747 cauchy_distribution(const param_type
& __p
)
2752 * @brief Resets the distribution state.
2763 { return _M_param
.a(); }
2767 { return _M_param
.b(); }
2770 * @brief Returns the parameter set of the distribution.
2774 { return _M_param
; }
2777 * @brief Sets the parameter set of the distribution.
2778 * @param __param The new parameter set of the distribution.
2781 param(const param_type
& __param
)
2782 { _M_param
= __param
; }
2785 * @brief Returns the greatest lower bound value of the distribution.
2789 { return std::numeric_limits
<result_type
>::min(); }
2792 * @brief Returns the least upper bound value of the distribution.
2796 { return std::numeric_limits
<result_type
>::max(); }
2799 * @brief Generating functions.
2801 template<typename _UniformRandomNumberGenerator
>
2803 operator()(_UniformRandomNumberGenerator
& __urng
)
2804 { return this->operator()(__urng
, this->param()); }
2806 template<typename _UniformRandomNumberGenerator
>
2808 operator()(_UniformRandomNumberGenerator
& __urng
,
2809 const param_type
& __p
);
2812 param_type _M_param
;
2816 * @brief Return true if two Cauchy distributions have
2817 * the same parameters.
2819 template<typename _RealType
>
2821 operator==(const std::cauchy_distribution
<_RealType
>& __d1
,
2822 const std::cauchy_distribution
<_RealType
>& __d2
)
2823 { return __d1
.param() == __d2
.param(); }
2826 * @brief Return true if two Cauchy distributions have
2827 * different parameters.
2829 template<typename _RealType
>
2831 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
2832 const std::cauchy_distribution
<_RealType
>& __d2
)
2833 { return !(__d1
== __d2
); }
2836 * @brief Inserts a %cauchy_distribution random number distribution
2837 * @p __x into the output stream @p __os.
2839 * @param __os An output stream.
2840 * @param __x A %cauchy_distribution random number distribution.
2842 * @returns The output stream with the state of @p __x inserted or in
2845 template<typename _RealType
, typename _CharT
, typename _Traits
>
2846 std::basic_ostream
<_CharT
, _Traits
>&
2847 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2848 const std::cauchy_distribution
<_RealType
>&);
2851 * @brief Extracts a %cauchy_distribution random number distribution
2852 * @p __x from the input stream @p __is.
2854 * @param __is An input stream.
2855 * @param __x A %cauchy_distribution random number
2858 * @returns The input stream with @p __x extracted or in an error state.
2860 template<typename _RealType
, typename _CharT
, typename _Traits
>
2861 std::basic_istream
<_CharT
, _Traits
>&
2862 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2863 std::cauchy_distribution
<_RealType
>&);
2867 * @brief A fisher_f_distribution random number distribution.
2869 * The formula for the normal probability mass function is
2871 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2872 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2873 * (1 + \frac{mx}{n})^{-(m+n)/2}
2876 template<typename _RealType
= double>
2877 class fisher_f_distribution
2879 static_assert(std::is_floating_point
<_RealType
>::value
,
2880 "template argument not a floating point type");
2883 /** The type of the range of the distribution. */
2884 typedef _RealType result_type
;
2885 /** Parameter type. */
2888 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2891 param_type(_RealType __m
= _RealType(1),
2892 _RealType __n
= _RealType(1))
2893 : _M_m(__m
), _M_n(__n
)
2905 operator==(const param_type
& __p1
, const param_type
& __p2
)
2906 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
2914 fisher_f_distribution(_RealType __m
= _RealType(1),
2915 _RealType __n
= _RealType(1))
2916 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2920 fisher_f_distribution(const param_type
& __p
)
2921 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2925 * @brief Resets the distribution state.
2939 { return _M_param
.m(); }
2943 { return _M_param
.n(); }
2946 * @brief Returns the parameter set of the distribution.
2950 { return _M_param
; }
2953 * @brief Sets the parameter set of the distribution.
2954 * @param __param The new parameter set of the distribution.
2957 param(const param_type
& __param
)
2958 { _M_param
= __param
; }
2961 * @brief Returns the greatest lower bound value of the distribution.
2965 { return result_type(0); }
2968 * @brief Returns the least upper bound value of the distribution.
2972 { return std::numeric_limits
<result_type
>::max(); }
2975 * @brief Generating functions.
2977 template<typename _UniformRandomNumberGenerator
>
2979 operator()(_UniformRandomNumberGenerator
& __urng
)
2980 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
2982 template<typename _UniformRandomNumberGenerator
>
2984 operator()(_UniformRandomNumberGenerator
& __urng
,
2985 const param_type
& __p
)
2987 typedef typename
std::gamma_distribution
<result_type
>::param_type
2989 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
2990 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
2994 * @brief Return true if two Fisher f distributions have
2995 * the same parameters and the sequences that would
2996 * be generated are equal.
2998 template<typename _RealType1
>
3000 operator==(const std::fisher_f_distribution
<_RealType1
>& __d1
,
3001 const std::fisher_f_distribution
<_RealType1
>& __d2
)
3002 { return (__d1
.param() == __d2
.param()
3003 && __d1
._M_gd_x
== __d2
._M_gd_x
3004 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
3007 * @brief Inserts a %fisher_f_distribution random number distribution
3008 * @p __x into the output stream @p __os.
3010 * @param __os An output stream.
3011 * @param __x A %fisher_f_distribution random number distribution.
3013 * @returns The output stream with the state of @p __x inserted or in
3016 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3017 friend std::basic_ostream
<_CharT
, _Traits
>&
3018 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3019 const std::fisher_f_distribution
<_RealType1
>&);
3022 * @brief Extracts a %fisher_f_distribution random number distribution
3023 * @p __x from the input stream @p __is.
3025 * @param __is An input stream.
3026 * @param __x A %fisher_f_distribution random number
3029 * @returns The input stream with @p __x extracted or in an error state.
3031 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3032 friend std::basic_istream
<_CharT
, _Traits
>&
3033 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3034 std::fisher_f_distribution
<_RealType1
>&);
3037 param_type _M_param
;
3039 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3043 * @brief Return true if two Fisher f distributions are diferent.
3045 template<typename _RealType
>
3047 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3048 const std::fisher_f_distribution
<_RealType
>& __d2
)
3049 { return !(__d1
== __d2
); }
3052 * @brief A student_t_distribution random number distribution.
3054 * The formula for the normal probability mass function is:
3056 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3057 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3060 template<typename _RealType
= double>
3061 class student_t_distribution
3063 static_assert(std::is_floating_point
<_RealType
>::value
,
3064 "template argument not a floating point type");
3067 /** The type of the range of the distribution. */
3068 typedef _RealType result_type
;
3069 /** Parameter type. */
3072 typedef student_t_distribution
<_RealType
> distribution_type
;
3075 param_type(_RealType __n
= _RealType(1))
3084 operator==(const param_type
& __p1
, const param_type
& __p2
)
3085 { return __p1
._M_n
== __p2
._M_n
; }
3092 student_t_distribution(_RealType __n
= _RealType(1))
3093 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3097 student_t_distribution(const param_type
& __p
)
3098 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3102 * @brief Resets the distribution state.
3116 { return _M_param
.n(); }
3119 * @brief Returns the parameter set of the distribution.
3123 { return _M_param
; }
3126 * @brief Sets the parameter set of the distribution.
3127 * @param __param The new parameter set of the distribution.
3130 param(const param_type
& __param
)
3131 { _M_param
= __param
; }
3134 * @brief Returns the greatest lower bound value of the distribution.
3138 { return std::numeric_limits
<result_type
>::min(); }
3141 * @brief Returns the least upper bound value of the distribution.
3145 { return std::numeric_limits
<result_type
>::max(); }
3148 * @brief Generating functions.
3150 template<typename _UniformRandomNumberGenerator
>
3152 operator()(_UniformRandomNumberGenerator
& __urng
)
3153 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3155 template<typename _UniformRandomNumberGenerator
>
3157 operator()(_UniformRandomNumberGenerator
& __urng
,
3158 const param_type
& __p
)
3160 typedef typename
std::gamma_distribution
<result_type
>::param_type
3163 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3164 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3168 * @brief Return true if two Student t distributions have
3169 * the same parameters and the sequences that would
3170 * be generated are equal.
3172 template<typename _RealType1
>
3174 operator==(const std::student_t_distribution
<_RealType1
>& __d1
,
3175 const std::student_t_distribution
<_RealType1
>& __d2
)
3176 { return (__d1
.param() == __d2
.param()
3177 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3180 * @brief Inserts a %student_t_distribution random number distribution
3181 * @p __x into the output stream @p __os.
3183 * @param __os An output stream.
3184 * @param __x A %student_t_distribution random number distribution.
3186 * @returns The output stream with the state of @p __x inserted or in
3189 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3190 friend std::basic_ostream
<_CharT
, _Traits
>&
3191 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3192 const std::student_t_distribution
<_RealType1
>&);
3195 * @brief Extracts a %student_t_distribution random number distribution
3196 * @p __x from the input stream @p __is.
3198 * @param __is An input stream.
3199 * @param __x A %student_t_distribution random number
3202 * @returns The input stream with @p __x extracted or in an error state.
3204 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3205 friend std::basic_istream
<_CharT
, _Traits
>&
3206 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3207 std::student_t_distribution
<_RealType1
>&);
3210 param_type _M_param
;
3212 std::normal_distribution
<result_type
> _M_nd
;
3213 std::gamma_distribution
<result_type
> _M_gd
;
3217 * @brief Return true if two Student t distributions are different.
3219 template<typename _RealType
>
3221 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3222 const std::student_t_distribution
<_RealType
>& __d2
)
3223 { return !(__d1
== __d2
); }
3226 /* @} */ // group random_distributions_normal
3229 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3230 * @ingroup random_distributions
3235 * @brief A Bernoulli random number distribution.
3237 * Generates a sequence of true and false values with likelihood @f$p@f$
3238 * that true will come up and @f$(1 - p)@f$ that false will appear.
3240 class bernoulli_distribution
3243 /** The type of the range of the distribution. */
3244 typedef bool result_type
;
3245 /** Parameter type. */
3248 typedef bernoulli_distribution distribution_type
;
3251 param_type(double __p
= 0.5)
3254 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
3262 operator==(const param_type
& __p1
, const param_type
& __p2
)
3263 { return __p1
._M_p
== __p2
._M_p
; }
3271 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3273 * @param __p [IN] The likelihood of a true result being returned.
3274 * Must be in the interval @f$[0, 1]@f$.
3277 bernoulli_distribution(double __p
= 0.5)
3282 bernoulli_distribution(const param_type
& __p
)
3287 * @brief Resets the distribution state.
3289 * Does nothing for a Bernoulli distribution.
3295 * @brief Returns the @p p parameter of the distribution.
3299 { return _M_param
.p(); }
3302 * @brief Returns the parameter set of the distribution.
3306 { return _M_param
; }
3309 * @brief Sets the parameter set of the distribution.
3310 * @param __param The new parameter set of the distribution.
3313 param(const param_type
& __param
)
3314 { _M_param
= __param
; }
3317 * @brief Returns the greatest lower bound value of the distribution.
3321 { return std::numeric_limits
<result_type
>::min(); }
3324 * @brief Returns the least upper bound value of the distribution.
3328 { return std::numeric_limits
<result_type
>::max(); }
3331 * @brief Generating functions.
3333 template<typename _UniformRandomNumberGenerator
>
3335 operator()(_UniformRandomNumberGenerator
& __urng
)
3336 { return this->operator()(__urng
, this->param()); }
3338 template<typename _UniformRandomNumberGenerator
>
3340 operator()(_UniformRandomNumberGenerator
& __urng
,
3341 const param_type
& __p
)
3343 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3345 if ((__aurng() - __aurng
.min())
3346 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3352 param_type _M_param
;
3356 * @brief Return true if two Bernoulli distributions have
3357 * the same parameters.
3360 operator==(const std::bernoulli_distribution
& __d1
,
3361 const std::bernoulli_distribution
& __d2
)
3362 { return __d1
.param() == __d2
.param(); }
3365 * @brief Return true if two Bernoulli distributions have
3366 * different parameters.
3369 operator!=(const std::bernoulli_distribution
& __d1
,
3370 const std::bernoulli_distribution
& __d2
)
3371 { return !(__d1
== __d2
); }
3374 * @brief Inserts a %bernoulli_distribution random number distribution
3375 * @p __x into the output stream @p __os.
3377 * @param __os An output stream.
3378 * @param __x A %bernoulli_distribution random number distribution.
3380 * @returns The output stream with the state of @p __x inserted or in
3383 template<typename _CharT
, typename _Traits
>
3384 std::basic_ostream
<_CharT
, _Traits
>&
3385 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3386 const std::bernoulli_distribution
&);
3389 * @brief Extracts a %bernoulli_distribution random number distribution
3390 * @p __x from the input stream @p __is.
3392 * @param __is An input stream.
3393 * @param __x A %bernoulli_distribution random number generator engine.
3395 * @returns The input stream with @p __x extracted or in an error state.
3397 template<typename _CharT
, typename _Traits
>
3398 std::basic_istream
<_CharT
, _Traits
>&
3399 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3400 std::bernoulli_distribution
& __x
)
3404 __x
.param(bernoulli_distribution::param_type(__p
));
3410 * @brief A discrete binomial random number distribution.
3412 * The formula for the binomial probability density function is
3413 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3414 * and @f$p@f$ are the parameters of the distribution.
3416 template<typename _IntType
= int>
3417 class binomial_distribution
3419 static_assert(std::is_integral
<_IntType
>::value
,
3420 "template argument not an integral type");
3423 /** The type of the range of the distribution. */
3424 typedef _IntType result_type
;
3425 /** Parameter type. */
3428 typedef binomial_distribution
<_IntType
> distribution_type
;
3429 friend class binomial_distribution
<_IntType
>;
3432 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3433 : _M_t(__t
), _M_p(__p
)
3435 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3450 operator==(const param_type
& __p1
, const param_type
& __p2
)
3451 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3461 #if _GLIBCXX_USE_C99_MATH_TR1
3462 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3463 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3468 // constructors and member function
3470 binomial_distribution(_IntType __t
= _IntType(1),
3472 : _M_param(__t
, __p
), _M_nd()
3476 binomial_distribution(const param_type
& __p
)
3477 : _M_param(__p
), _M_nd()
3481 * @brief Resets the distribution state.
3488 * @brief Returns the distribution @p t parameter.
3492 { return _M_param
.t(); }
3495 * @brief Returns the distribution @p p parameter.
3499 { return _M_param
.p(); }
3502 * @brief Returns the parameter set of the distribution.
3506 { return _M_param
; }
3509 * @brief Sets the parameter set of the distribution.
3510 * @param __param The new parameter set of the distribution.
3513 param(const param_type
& __param
)
3514 { _M_param
= __param
; }
3517 * @brief Returns the greatest lower bound value of the distribution.
3524 * @brief Returns the least upper bound value of the distribution.
3528 { return _M_param
.t(); }
3531 * @brief Generating functions.
3533 template<typename _UniformRandomNumberGenerator
>
3535 operator()(_UniformRandomNumberGenerator
& __urng
)
3536 { return this->operator()(__urng
, this->param()); }
3538 template<typename _UniformRandomNumberGenerator
>
3540 operator()(_UniformRandomNumberGenerator
& __urng
,
3541 const param_type
& __p
);
3544 * @brief Return true if two binomial distributions have
3545 * the same parameters and the sequences that would
3546 * be generated are equal.
3548 template<typename _IntType1
>
3550 operator==(const std::binomial_distribution
<_IntType1
>& __d1
,
3551 const std::binomial_distribution
<_IntType1
>& __d2
)
3552 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3553 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
3555 { return __d1
.param() == __d2
.param(); }
3559 * @brief Inserts a %binomial_distribution random number distribution
3560 * @p __x into the output stream @p __os.
3562 * @param __os An output stream.
3563 * @param __x A %binomial_distribution random number distribution.
3565 * @returns The output stream with the state of @p __x inserted or in
3568 template<typename _IntType1
,
3569 typename _CharT
, typename _Traits
>
3570 friend std::basic_ostream
<_CharT
, _Traits
>&
3571 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3572 const std::binomial_distribution
<_IntType1
>&);
3575 * @brief Extracts a %binomial_distribution random number distribution
3576 * @p __x from the input stream @p __is.
3578 * @param __is An input stream.
3579 * @param __x A %binomial_distribution random number generator engine.
3581 * @returns The input stream with @p __x extracted or in an error
3584 template<typename _IntType1
,
3585 typename _CharT
, typename _Traits
>
3586 friend std::basic_istream
<_CharT
, _Traits
>&
3587 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3588 std::binomial_distribution
<_IntType1
>&);
3591 template<typename _UniformRandomNumberGenerator
>
3593 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3595 param_type _M_param
;
3597 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3598 std::normal_distribution
<double> _M_nd
;
3602 * @brief Return true if two binomial distributions are different.
3604 template<typename _IntType
>
3606 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
3607 const std::binomial_distribution
<_IntType
>& __d2
)
3608 { return !(__d1
== __d2
); }
3612 * @brief A discrete geometric random number distribution.
3614 * The formula for the geometric probability density function is
3615 * @f$p(i|p) = (1 - p)p^{i-1}@f$ where @f$p@f$ is the parameter of the
3618 template<typename _IntType
= int>
3619 class geometric_distribution
3621 static_assert(std::is_integral
<_IntType
>::value
,
3622 "template argument not an integral type");
3625 /** The type of the range of the distribution. */
3626 typedef _IntType result_type
;
3627 /** Parameter type. */
3630 typedef geometric_distribution
<_IntType
> distribution_type
;
3631 friend class geometric_distribution
<_IntType
>;
3634 param_type(double __p
= 0.5)
3637 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0)
3647 operator==(const param_type
& __p1
, const param_type
& __p2
)
3648 { return __p1
._M_p
== __p2
._M_p
; }
3653 { _M_log_p
= std::log(_M_p
); }
3660 // constructors and member function
3662 geometric_distribution(double __p
= 0.5)
3667 geometric_distribution(const param_type
& __p
)
3672 * @brief Resets the distribution state.
3674 * Does nothing for the geometric distribution.
3680 * @brief Returns the distribution parameter @p p.
3684 { return _M_param
.p(); }
3687 * @brief Returns the parameter set of the distribution.
3691 { return _M_param
; }
3694 * @brief Sets the parameter set of the distribution.
3695 * @param __param The new parameter set of the distribution.
3698 param(const param_type
& __param
)
3699 { _M_param
= __param
; }
3702 * @brief Returns the greatest lower bound value of the distribution.
3709 * @brief Returns the least upper bound value of the distribution.
3713 { return std::numeric_limits
<result_type
>::max(); }
3716 * @brief Generating functions.
3718 template<typename _UniformRandomNumberGenerator
>
3720 operator()(_UniformRandomNumberGenerator
& __urng
)
3721 { return this->operator()(__urng
, this->param()); }
3723 template<typename _UniformRandomNumberGenerator
>
3725 operator()(_UniformRandomNumberGenerator
& __urng
,
3726 const param_type
& __p
);
3729 param_type _M_param
;
3733 * @brief Return true if two geometric distributions have
3734 * the same parameters.
3736 template<typename _IntType
>
3738 operator==(const std::geometric_distribution
<_IntType
>& __d1
,
3739 const std::geometric_distribution
<_IntType
>& __d2
)
3740 { return __d1
.param() == __d2
.param(); }
3743 * @brief Return true if two geometric distributions have
3744 * different parameters.
3746 template<typename _IntType
>
3748 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
3749 const std::geometric_distribution
<_IntType
>& __d2
)
3750 { return !(__d1
== __d2
); }
3753 * @brief Inserts a %geometric_distribution random number distribution
3754 * @p __x into the output stream @p __os.
3756 * @param __os An output stream.
3757 * @param __x A %geometric_distribution random number distribution.
3759 * @returns The output stream with the state of @p __x inserted or in
3762 template<typename _IntType
,
3763 typename _CharT
, typename _Traits
>
3764 std::basic_ostream
<_CharT
, _Traits
>&
3765 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3766 const std::geometric_distribution
<_IntType
>&);
3769 * @brief Extracts a %geometric_distribution random number distribution
3770 * @p __x from the input stream @p __is.
3772 * @param __is An input stream.
3773 * @param __x A %geometric_distribution random number generator engine.
3775 * @returns The input stream with @p __x extracted or in an error state.
3777 template<typename _IntType
,
3778 typename _CharT
, typename _Traits
>
3779 std::basic_istream
<_CharT
, _Traits
>&
3780 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3781 std::geometric_distribution
<_IntType
>&);
3785 * @brief A negative_binomial_distribution random number distribution.
3787 * The formula for the negative binomial probability mass function is
3788 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3789 * and @f$p@f$ are the parameters of the distribution.
3791 template<typename _IntType
= int>
3792 class negative_binomial_distribution
3794 static_assert(std::is_integral
<_IntType
>::value
,
3795 "template argument not an integral type");
3798 /** The type of the range of the distribution. */
3799 typedef _IntType result_type
;
3800 /** Parameter type. */
3803 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3806 param_type(_IntType __k
= 1, double __p
= 0.5)
3807 : _M_k(__k
), _M_p(__p
)
3819 operator==(const param_type
& __p1
, const param_type
& __p2
)
3820 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
3828 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3829 : _M_param(__k
, __p
), _M_gd(__k
, __p
/ (1.0 - __p
))
3833 negative_binomial_distribution(const param_type
& __p
)
3834 : _M_param(__p
), _M_gd(__p
.k(), __p
.p() / (1.0 - __p
.p()))
3838 * @brief Resets the distribution state.
3845 * @brief Return the @f$k@f$ parameter of the distribution.
3849 { return _M_param
.k(); }
3852 * @brief Return the @f$p@f$ parameter of the distribution.
3856 { return _M_param
.p(); }
3859 * @brief Returns the parameter set of the distribution.
3863 { return _M_param
; }
3866 * @brief Sets the parameter set of the distribution.
3867 * @param __param The new parameter set of the distribution.
3870 param(const param_type
& __param
)
3871 { _M_param
= __param
; }
3874 * @brief Returns the greatest lower bound value of the distribution.
3878 { return result_type(0); }
3881 * @brief Returns the least upper bound value of the distribution.
3885 { return std::numeric_limits
<result_type
>::max(); }
3888 * @brief Generating functions.
3890 template<typename _UniformRandomNumberGenerator
>
3892 operator()(_UniformRandomNumberGenerator
& __urng
);
3894 template<typename _UniformRandomNumberGenerator
>
3896 operator()(_UniformRandomNumberGenerator
& __urng
,
3897 const param_type
& __p
);
3900 * @brief Return true if two negative binomial distributions have
3901 * the same parameters and the sequences that would be
3902 * generated are equal.
3904 template<typename _IntType1
>
3906 operator==(const std::negative_binomial_distribution
<_IntType1
>& __d1
,
3907 const std::negative_binomial_distribution
<_IntType1
>& __d2
)
3908 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
3911 * @brief Inserts a %negative_binomial_distribution random
3912 * number distribution @p __x into the output stream @p __os.
3914 * @param __os An output stream.
3915 * @param __x A %negative_binomial_distribution random number
3918 * @returns The output stream with the state of @p __x inserted or in
3921 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3922 friend std::basic_ostream
<_CharT
, _Traits
>&
3923 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3924 const std::negative_binomial_distribution
<_IntType1
>&);
3927 * @brief Extracts a %negative_binomial_distribution random number
3928 * distribution @p __x from the input stream @p __is.
3930 * @param __is An input stream.
3931 * @param __x A %negative_binomial_distribution random number
3934 * @returns The input stream with @p __x extracted or in an error state.
3936 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3937 friend std::basic_istream
<_CharT
, _Traits
>&
3938 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3939 std::negative_binomial_distribution
<_IntType1
>&);
3942 param_type _M_param
;
3944 std::gamma_distribution
<double> _M_gd
;
3948 * @brief Return true if two negative binomial distributions are different.
3950 template<typename _IntType
>
3952 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
3953 const std::negative_binomial_distribution
<_IntType
>& __d2
)
3954 { return !(__d1
== __d2
); }
3957 /* @} */ // group random_distributions_bernoulli
3960 * @addtogroup random_distributions_poisson Poisson Distributions
3961 * @ingroup random_distributions
3966 * @brief A discrete Poisson random number distribution.
3968 * The formula for the Poisson probability density function is
3969 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
3970 * parameter of the distribution.
3972 template<typename _IntType
= int>
3973 class poisson_distribution
3975 static_assert(std::is_integral
<_IntType
>::value
,
3976 "template argument not an integral type");
3979 /** The type of the range of the distribution. */
3980 typedef _IntType result_type
;
3981 /** Parameter type. */
3984 typedef poisson_distribution
<_IntType
> distribution_type
;
3985 friend class poisson_distribution
<_IntType
>;
3988 param_type(double __mean
= 1.0)
3991 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
4000 operator==(const param_type
& __p1
, const param_type
& __p2
)
4001 { return __p1
._M_mean
== __p2
._M_mean
; }
4004 // Hosts either log(mean) or the threshold of the simple method.
4011 #if _GLIBCXX_USE_C99_MATH_TR1
4012 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
4016 // constructors and member function
4018 poisson_distribution(double __mean
= 1.0)
4019 : _M_param(__mean
), _M_nd()
4023 poisson_distribution(const param_type
& __p
)
4024 : _M_param(__p
), _M_nd()
4028 * @brief Resets the distribution state.
4035 * @brief Returns the distribution parameter @p mean.
4039 { return _M_param
.mean(); }
4042 * @brief Returns the parameter set of the distribution.
4046 { return _M_param
; }
4049 * @brief Sets the parameter set of the distribution.
4050 * @param __param The new parameter set of the distribution.
4053 param(const param_type
& __param
)
4054 { _M_param
= __param
; }
4057 * @brief Returns the greatest lower bound value of the distribution.
4064 * @brief Returns the least upper bound value of the distribution.
4068 { return std::numeric_limits
<result_type
>::max(); }
4071 * @brief Generating functions.
4073 template<typename _UniformRandomNumberGenerator
>
4075 operator()(_UniformRandomNumberGenerator
& __urng
)
4076 { return this->operator()(__urng
, this->param()); }
4078 template<typename _UniformRandomNumberGenerator
>
4080 operator()(_UniformRandomNumberGenerator
& __urng
,
4081 const param_type
& __p
);
4084 * @brief Return true if two Poisson distributions have the same
4085 * parameters and the sequences that would be generated
4088 template<typename _IntType1
>
4090 operator==(const std::poisson_distribution
<_IntType1
>& __d1
,
4091 const std::poisson_distribution
<_IntType1
>& __d2
)
4092 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4093 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
4095 { return __d1
.param() == __d2
.param(); }
4099 * @brief Inserts a %poisson_distribution random number distribution
4100 * @p __x into the output stream @p __os.
4102 * @param __os An output stream.
4103 * @param __x A %poisson_distribution random number distribution.
4105 * @returns The output stream with the state of @p __x inserted or in
4108 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4109 friend std::basic_ostream
<_CharT
, _Traits
>&
4110 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4111 const std::poisson_distribution
<_IntType1
>&);
4114 * @brief Extracts a %poisson_distribution random number distribution
4115 * @p __x from the input stream @p __is.
4117 * @param __is An input stream.
4118 * @param __x A %poisson_distribution random number generator engine.
4120 * @returns The input stream with @p __x extracted or in an error
4123 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4124 friend std::basic_istream
<_CharT
, _Traits
>&
4125 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4126 std::poisson_distribution
<_IntType1
>&);
4129 param_type _M_param
;
4131 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4132 std::normal_distribution
<double> _M_nd
;
4136 * @brief Return true if two Poisson distributions are different.
4138 template<typename _IntType
>
4140 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4141 const std::poisson_distribution
<_IntType
>& __d2
)
4142 { return !(__d1
== __d2
); }
4146 * @brief An exponential continuous distribution for random numbers.
4148 * The formula for the exponential probability density function is
4149 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4151 * <table border=1 cellpadding=10 cellspacing=0>
4152 * <caption align=top>Distribution Statistics</caption>
4153 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4154 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4155 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4156 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4157 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4160 template<typename _RealType
= double>
4161 class exponential_distribution
4163 static_assert(std::is_floating_point
<_RealType
>::value
,
4164 "template argument not a floating point type");
4167 /** The type of the range of the distribution. */
4168 typedef _RealType result_type
;
4169 /** Parameter type. */
4172 typedef exponential_distribution
<_RealType
> distribution_type
;
4175 param_type(_RealType __lambda
= _RealType(1))
4176 : _M_lambda(__lambda
)
4178 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
4183 { return _M_lambda
; }
4186 operator==(const param_type
& __p1
, const param_type
& __p2
)
4187 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4190 _RealType _M_lambda
;
4195 * @brief Constructs an exponential distribution with inverse scale
4196 * parameter @f$\lambda@f$.
4199 exponential_distribution(const result_type
& __lambda
= result_type(1))
4200 : _M_param(__lambda
)
4204 exponential_distribution(const param_type
& __p
)
4209 * @brief Resets the distribution state.
4211 * Has no effect on exponential distributions.
4217 * @brief Returns the inverse scale parameter of the distribution.
4221 { return _M_param
.lambda(); }
4224 * @brief Returns the parameter set of the distribution.
4228 { return _M_param
; }
4231 * @brief Sets the parameter set of the distribution.
4232 * @param __param The new parameter set of the distribution.
4235 param(const param_type
& __param
)
4236 { _M_param
= __param
; }
4239 * @brief Returns the greatest lower bound value of the distribution.
4243 { return result_type(0); }
4246 * @brief Returns the least upper bound value of the distribution.
4250 { return std::numeric_limits
<result_type
>::max(); }
4253 * @brief Generating functions.
4255 template<typename _UniformRandomNumberGenerator
>
4257 operator()(_UniformRandomNumberGenerator
& __urng
)
4258 { return this->operator()(__urng
, this->param()); }
4260 template<typename _UniformRandomNumberGenerator
>
4262 operator()(_UniformRandomNumberGenerator
& __urng
,
4263 const param_type
& __p
)
4265 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4267 return -std::log(__aurng()) / __p
.lambda();
4271 param_type _M_param
;
4275 * @brief Return true if two exponential distributions have the same
4278 template<typename _RealType
>
4280 operator==(const std::exponential_distribution
<_RealType
>& __d1
,
4281 const std::exponential_distribution
<_RealType
>& __d2
)
4282 { return __d1
.param() == __d2
.param(); }
4285 * @brief Return true if two exponential distributions have different
4288 template<typename _RealType
>
4290 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4291 const std::exponential_distribution
<_RealType
>& __d2
)
4292 { return !(__d1
== __d2
); }
4295 * @brief Inserts a %exponential_distribution random number distribution
4296 * @p __x into the output stream @p __os.
4298 * @param __os An output stream.
4299 * @param __x A %exponential_distribution random number distribution.
4301 * @returns The output stream with the state of @p __x inserted or in
4304 template<typename _RealType
, typename _CharT
, typename _Traits
>
4305 std::basic_ostream
<_CharT
, _Traits
>&
4306 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4307 const std::exponential_distribution
<_RealType
>&);
4310 * @brief Extracts a %exponential_distribution random number distribution
4311 * @p __x from the input stream @p __is.
4313 * @param __is An input stream.
4314 * @param __x A %exponential_distribution random number
4317 * @returns The input stream with @p __x extracted or in an error state.
4319 template<typename _RealType
, typename _CharT
, typename _Traits
>
4320 std::basic_istream
<_CharT
, _Traits
>&
4321 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4322 std::exponential_distribution
<_RealType
>&);
4326 * @brief A weibull_distribution random number distribution.
4328 * The formula for the normal probability density function is:
4330 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4331 * \exp{(-(\frac{x}{\beta})^\alpha)}
4334 template<typename _RealType
= double>
4335 class weibull_distribution
4337 static_assert(std::is_floating_point
<_RealType
>::value
,
4338 "template argument not a floating point type");
4341 /** The type of the range of the distribution. */
4342 typedef _RealType result_type
;
4343 /** Parameter type. */
4346 typedef weibull_distribution
<_RealType
> distribution_type
;
4349 param_type(_RealType __a
= _RealType(1),
4350 _RealType __b
= _RealType(1))
4351 : _M_a(__a
), _M_b(__b
)
4363 operator==(const param_type
& __p1
, const param_type
& __p2
)
4364 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4372 weibull_distribution(_RealType __a
= _RealType(1),
4373 _RealType __b
= _RealType(1))
4374 : _M_param(__a
, __b
)
4378 weibull_distribution(const param_type
& __p
)
4383 * @brief Resets the distribution state.
4390 * @brief Return the @f$a@f$ parameter of the distribution.
4394 { return _M_param
.a(); }
4397 * @brief Return the @f$b@f$ parameter of the distribution.
4401 { return _M_param
.b(); }
4404 * @brief Returns the parameter set of the distribution.
4408 { return _M_param
; }
4411 * @brief Sets the parameter set of the distribution.
4412 * @param __param The new parameter set of the distribution.
4415 param(const param_type
& __param
)
4416 { _M_param
= __param
; }
4419 * @brief Returns the greatest lower bound value of the distribution.
4423 { return result_type(0); }
4426 * @brief Returns the least upper bound value of the distribution.
4430 { return std::numeric_limits
<result_type
>::max(); }
4433 * @brief Generating functions.
4435 template<typename _UniformRandomNumberGenerator
>
4437 operator()(_UniformRandomNumberGenerator
& __urng
)
4438 { return this->operator()(__urng
, this->param()); }
4440 template<typename _UniformRandomNumberGenerator
>
4442 operator()(_UniformRandomNumberGenerator
& __urng
,
4443 const param_type
& __p
);
4446 param_type _M_param
;
4450 * @brief Return true if two Weibull distributions have the same
4453 template<typename _RealType
>
4455 operator==(const std::weibull_distribution
<_RealType
>& __d1
,
4456 const std::weibull_distribution
<_RealType
>& __d2
)
4457 { return __d1
.param() == __d2
.param(); }
4460 * @brief Return true if two Weibull distributions have different
4463 template<typename _RealType
>
4465 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
4466 const std::weibull_distribution
<_RealType
>& __d2
)
4467 { return !(__d1
== __d2
); }
4470 * @brief Inserts a %weibull_distribution random number distribution
4471 * @p __x into the output stream @p __os.
4473 * @param __os An output stream.
4474 * @param __x A %weibull_distribution random number distribution.
4476 * @returns The output stream with the state of @p __x inserted or in
4479 template<typename _RealType
, typename _CharT
, typename _Traits
>
4480 std::basic_ostream
<_CharT
, _Traits
>&
4481 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4482 const std::weibull_distribution
<_RealType
>&);
4485 * @brief Extracts a %weibull_distribution random number distribution
4486 * @p __x from the input stream @p __is.
4488 * @param __is An input stream.
4489 * @param __x A %weibull_distribution random number
4492 * @returns The input stream with @p __x extracted or in an error state.
4494 template<typename _RealType
, typename _CharT
, typename _Traits
>
4495 std::basic_istream
<_CharT
, _Traits
>&
4496 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4497 std::weibull_distribution
<_RealType
>&);
4501 * @brief A extreme_value_distribution random number distribution.
4503 * The formula for the normal probability mass function is
4505 * p(x|a,b) = \frac{1}{b}
4506 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
4509 template<typename _RealType
= double>
4510 class extreme_value_distribution
4512 static_assert(std::is_floating_point
<_RealType
>::value
,
4513 "template argument not a floating point type");
4516 /** The type of the range of the distribution. */
4517 typedef _RealType result_type
;
4518 /** Parameter type. */
4521 typedef extreme_value_distribution
<_RealType
> distribution_type
;
4524 param_type(_RealType __a
= _RealType(0),
4525 _RealType __b
= _RealType(1))
4526 : _M_a(__a
), _M_b(__b
)
4538 operator==(const param_type
& __p1
, const param_type
& __p2
)
4539 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4547 extreme_value_distribution(_RealType __a
= _RealType(0),
4548 _RealType __b
= _RealType(1))
4549 : _M_param(__a
, __b
)
4553 extreme_value_distribution(const param_type
& __p
)
4558 * @brief Resets the distribution state.
4565 * @brief Return the @f$a@f$ parameter of the distribution.
4569 { return _M_param
.a(); }
4572 * @brief Return the @f$b@f$ parameter of the distribution.
4576 { return _M_param
.b(); }
4579 * @brief Returns the parameter set of the distribution.
4583 { return _M_param
; }
4586 * @brief Sets the parameter set of the distribution.
4587 * @param __param The new parameter set of the distribution.
4590 param(const param_type
& __param
)
4591 { _M_param
= __param
; }
4594 * @brief Returns the greatest lower bound value of the distribution.
4598 { return std::numeric_limits
<result_type
>::min(); }
4601 * @brief Returns the least upper bound value of the distribution.
4605 { return std::numeric_limits
<result_type
>::max(); }
4608 * @brief Generating functions.
4610 template<typename _UniformRandomNumberGenerator
>
4612 operator()(_UniformRandomNumberGenerator
& __urng
)
4613 { return this->operator()(__urng
, this->param()); }
4615 template<typename _UniformRandomNumberGenerator
>
4617 operator()(_UniformRandomNumberGenerator
& __urng
,
4618 const param_type
& __p
);
4621 param_type _M_param
;
4625 * @brief Return true if two extreme value distributions have the same
4628 template<typename _RealType
>
4630 operator==(const std::extreme_value_distribution
<_RealType
>& __d1
,
4631 const std::extreme_value_distribution
<_RealType
>& __d2
)
4632 { return __d1
.param() == __d2
.param(); }
4635 * @brief Return true if two extreme value distributions have different
4638 template<typename _RealType
>
4640 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
4641 const std::extreme_value_distribution
<_RealType
>& __d2
)
4642 { return !(__d1
== __d2
); }
4645 * @brief Inserts a %extreme_value_distribution random number distribution
4646 * @p __x into the output stream @p __os.
4648 * @param __os An output stream.
4649 * @param __x A %extreme_value_distribution random number distribution.
4651 * @returns The output stream with the state of @p __x inserted or in
4654 template<typename _RealType
, typename _CharT
, typename _Traits
>
4655 std::basic_ostream
<_CharT
, _Traits
>&
4656 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4657 const std::extreme_value_distribution
<_RealType
>&);
4660 * @brief Extracts a %extreme_value_distribution random number
4661 * distribution @p __x from the input stream @p __is.
4663 * @param __is An input stream.
4664 * @param __x A %extreme_value_distribution random number
4667 * @returns The input stream with @p __x extracted or in an error state.
4669 template<typename _RealType
, typename _CharT
, typename _Traits
>
4670 std::basic_istream
<_CharT
, _Traits
>&
4671 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4672 std::extreme_value_distribution
<_RealType
>&);
4676 * @brief A discrete_distribution random number distribution.
4678 * The formula for the discrete probability mass function is
4681 template<typename _IntType
= int>
4682 class discrete_distribution
4684 static_assert(std::is_integral
<_IntType
>::value
,
4685 "template argument not an integral type");
4688 /** The type of the range of the distribution. */
4689 typedef _IntType result_type
;
4690 /** Parameter type. */
4693 typedef discrete_distribution
<_IntType
> distribution_type
;
4694 friend class discrete_distribution
<_IntType
>;
4697 : _M_prob(), _M_cp()
4698 { _M_initialize(); }
4700 template<typename _InputIterator
>
4701 param_type(_InputIterator __wbegin
,
4702 _InputIterator __wend
)
4703 : _M_prob(__wbegin
, __wend
), _M_cp()
4704 { _M_initialize(); }
4706 param_type(initializer_list
<double> __wil
)
4707 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4708 { _M_initialize(); }
4710 template<typename _Func
>
4711 param_type(size_t __nw
, double __xmin
, double __xmax
,
4715 probabilities() const
4719 operator==(const param_type
& __p1
, const param_type
& __p2
)
4720 { return __p1
._M_prob
== __p2
._M_prob
; }
4726 std::vector
<double> _M_prob
;
4727 std::vector
<double> _M_cp
;
4730 discrete_distribution()
4734 template<typename _InputIterator
>
4735 discrete_distribution(_InputIterator __wbegin
,
4736 _InputIterator __wend
)
4737 : _M_param(__wbegin
, __wend
)
4740 discrete_distribution(initializer_list
<double> __wl
)
4744 template<typename _Func
>
4745 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4747 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4751 discrete_distribution(const param_type
& __p
)
4756 * @brief Resets the distribution state.
4763 * @brief Returns the probabilities of the distribution.
4766 probabilities() const
4767 { return _M_param
.probabilities(); }
4770 * @brief Returns the parameter set of the distribution.
4774 { return _M_param
; }
4777 * @brief Sets the parameter set of the distribution.
4778 * @param __param The new parameter set of the distribution.
4781 param(const param_type
& __param
)
4782 { _M_param
= __param
; }
4785 * @brief Returns the greatest lower bound value of the distribution.
4789 { return result_type(0); }
4792 * @brief Returns the least upper bound value of the distribution.
4796 { return this->_M_param
._M_prob
.size() - 1; }
4799 * @brief Generating functions.
4801 template<typename _UniformRandomNumberGenerator
>
4803 operator()(_UniformRandomNumberGenerator
& __urng
)
4804 { return this->operator()(__urng
, this->param()); }
4806 template<typename _UniformRandomNumberGenerator
>
4808 operator()(_UniformRandomNumberGenerator
& __urng
,
4809 const param_type
& __p
);
4812 * @brief Inserts a %discrete_distribution random number distribution
4813 * @p __x into the output stream @p __os.
4815 * @param __os An output stream.
4816 * @param __x A %discrete_distribution random number distribution.
4818 * @returns The output stream with the state of @p __x inserted or in
4821 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4822 friend std::basic_ostream
<_CharT
, _Traits
>&
4823 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4824 const std::discrete_distribution
<_IntType1
>&);
4827 * @brief Extracts a %discrete_distribution random number distribution
4828 * @p __x from the input stream @p __is.
4830 * @param __is An input stream.
4831 * @param __x A %discrete_distribution random number
4834 * @returns The input stream with @p __x extracted or in an error
4837 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4838 friend std::basic_istream
<_CharT
, _Traits
>&
4839 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4840 std::discrete_distribution
<_IntType1
>&);
4843 param_type _M_param
;
4847 * @brief Return true if two discrete distributions have the same
4850 template<typename _IntType
>
4852 operator==(const std::discrete_distribution
<_IntType
>& __d1
,
4853 const std::discrete_distribution
<_IntType
>& __d2
)
4854 { return __d1
.param() == __d2
.param(); }
4857 * @brief Return true if two discrete distributions have different
4860 template<typename _IntType
>
4862 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
4863 const std::discrete_distribution
<_IntType
>& __d2
)
4864 { return !(__d1
== __d2
); }
4868 * @brief A piecewise_constant_distribution random number distribution.
4870 * The formula for the piecewise constant probability mass function is
4873 template<typename _RealType
= double>
4874 class piecewise_constant_distribution
4876 static_assert(std::is_floating_point
<_RealType
>::value
,
4877 "template argument not a floating point type");
4880 /** The type of the range of the distribution. */
4881 typedef _RealType result_type
;
4882 /** Parameter type. */
4885 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4886 friend class piecewise_constant_distribution
<_RealType
>;
4889 : _M_int(), _M_den(), _M_cp()
4890 { _M_initialize(); }
4892 template<typename _InputIteratorB
, typename _InputIteratorW
>
4893 param_type(_InputIteratorB __bfirst
,
4894 _InputIteratorB __bend
,
4895 _InputIteratorW __wbegin
);
4897 template<typename _Func
>
4898 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
4900 template<typename _Func
>
4901 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4904 std::vector
<_RealType
>
4913 operator==(const param_type
& __p1
, const param_type
& __p2
)
4914 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
4920 std::vector
<_RealType
> _M_int
;
4921 std::vector
<double> _M_den
;
4922 std::vector
<double> _M_cp
;
4926 piecewise_constant_distribution()
4930 template<typename _InputIteratorB
, typename _InputIteratorW
>
4931 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4932 _InputIteratorB __bend
,
4933 _InputIteratorW __wbegin
)
4934 : _M_param(__bfirst
, __bend
, __wbegin
)
4937 template<typename _Func
>
4938 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
4940 : _M_param(__bl
, __fw
)
4943 template<typename _Func
>
4944 piecewise_constant_distribution(size_t __nw
,
4945 _RealType __xmin
, _RealType __xmax
,
4947 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4951 piecewise_constant_distribution(const param_type
& __p
)
4956 * @brief Resets the distribution state.
4963 * @brief Returns a vector of the intervals.
4965 std::vector
<_RealType
>
4967 { return _M_param
.intervals(); }
4970 * @brief Returns a vector of the probability densities.
4974 { return _M_param
.densities(); }
4977 * @brief Returns the parameter set of the distribution.
4981 { return _M_param
; }
4984 * @brief Sets the parameter set of the distribution.
4985 * @param __param The new parameter set of the distribution.
4988 param(const param_type
& __param
)
4989 { _M_param
= __param
; }
4992 * @brief Returns the greatest lower bound value of the distribution.
4996 { return this->_M_param
._M_int
.front(); }
4999 * @brief Returns the least upper bound value of the distribution.
5003 { return this->_M_param
._M_int
.back(); }
5006 * @brief Generating functions.
5008 template<typename _UniformRandomNumberGenerator
>
5010 operator()(_UniformRandomNumberGenerator
& __urng
)
5011 { return this->operator()(__urng
, this->param()); }
5013 template<typename _UniformRandomNumberGenerator
>
5015 operator()(_UniformRandomNumberGenerator
& __urng
,
5016 const param_type
& __p
);
5019 * @brief Inserts a %piecewise_constan_distribution random
5020 * number distribution @p __x into the output stream @p __os.
5022 * @param __os An output stream.
5023 * @param __x A %piecewise_constan_distribution random number
5026 * @returns The output stream with the state of @p __x inserted or in
5029 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5030 friend std::basic_ostream
<_CharT
, _Traits
>&
5031 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
5032 const std::piecewise_constant_distribution
<_RealType1
>&);
5035 * @brief Extracts a %piecewise_constan_distribution random
5036 * number distribution @p __x from the input stream @p __is.
5038 * @param __is An input stream.
5039 * @param __x A %piecewise_constan_distribution random number
5042 * @returns The input stream with @p __x extracted or in an error
5045 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5046 friend std::basic_istream
<_CharT
, _Traits
>&
5047 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
5048 std::piecewise_constant_distribution
<_RealType1
>&);
5051 param_type _M_param
;
5055 * @brief Return true if two piecewise constant distributions have the
5058 template<typename _RealType
>
5060 operator==(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5061 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5062 { return __d1
.param() == __d2
.param(); }
5065 * @brief Return true if two piecewise constant distributions have
5066 * different parameters.
5068 template<typename _RealType
>
5070 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5071 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5072 { return !(__d1
== __d2
); }
5076 * @brief A piecewise_linear_distribution random number distribution.
5078 * The formula for the piecewise linear probability mass function is
5081 template<typename _RealType
= double>
5082 class piecewise_linear_distribution
5084 static_assert(std::is_floating_point
<_RealType
>::value
,
5085 "template argument not a floating point type");
5088 /** The type of the range of the distribution. */
5089 typedef _RealType result_type
;
5090 /** Parameter type. */
5093 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5094 friend class piecewise_linear_distribution
<_RealType
>;
5097 : _M_int(), _M_den(), _M_cp(), _M_m()
5098 { _M_initialize(); }
5100 template<typename _InputIteratorB
, typename _InputIteratorW
>
5101 param_type(_InputIteratorB __bfirst
,
5102 _InputIteratorB __bend
,
5103 _InputIteratorW __wbegin
);
5105 template<typename _Func
>
5106 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5108 template<typename _Func
>
5109 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5112 std::vector
<_RealType
>
5121 operator==(const param_type
& __p1
, const param_type
& __p2
)
5122 { return (__p1
._M_int
== __p2
._M_int
5123 && __p1
._M_den
== __p2
._M_den
); }
5129 std::vector
<_RealType
> _M_int
;
5130 std::vector
<double> _M_den
;
5131 std::vector
<double> _M_cp
;
5132 std::vector
<double> _M_m
;
5136 piecewise_linear_distribution()
5140 template<typename _InputIteratorB
, typename _InputIteratorW
>
5141 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5142 _InputIteratorB __bend
,
5143 _InputIteratorW __wbegin
)
5144 : _M_param(__bfirst
, __bend
, __wbegin
)
5147 template<typename _Func
>
5148 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5150 : _M_param(__bl
, __fw
)
5153 template<typename _Func
>
5154 piecewise_linear_distribution(size_t __nw
,
5155 _RealType __xmin
, _RealType __xmax
,
5157 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5161 piecewise_linear_distribution(const param_type
& __p
)
5166 * Resets the distribution state.
5173 * @brief Return the intervals of the distribution.
5175 std::vector
<_RealType
>
5177 { return _M_param
.intervals(); }
5180 * @brief Return a vector of the probability densities of the
5185 { return _M_param
.densities(); }
5188 * @brief Returns the parameter set of the distribution.
5192 { return _M_param
; }
5195 * @brief Sets the parameter set of the distribution.
5196 * @param __param The new parameter set of the distribution.
5199 param(const param_type
& __param
)
5200 { _M_param
= __param
; }
5203 * @brief Returns the greatest lower bound value of the distribution.
5207 { return this->_M_param
._M_int
.front(); }
5210 * @brief Returns the least upper bound value of the distribution.
5214 { return this->_M_param
._M_int
.back(); }
5217 * @brief Generating functions.
5219 template<typename _UniformRandomNumberGenerator
>
5221 operator()(_UniformRandomNumberGenerator
& __urng
)
5222 { return this->operator()(__urng
, this->param()); }
5224 template<typename _UniformRandomNumberGenerator
>
5226 operator()(_UniformRandomNumberGenerator
& __urng
,
5227 const param_type
& __p
);
5230 * @brief Inserts a %piecewise_linear_distribution random number
5231 * distribution @p __x into the output stream @p __os.
5233 * @param __os An output stream.
5234 * @param __x A %piecewise_linear_distribution random number
5237 * @returns The output stream with the state of @p __x inserted or in
5240 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5241 friend std::basic_ostream
<_CharT
, _Traits
>&
5242 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
5243 const std::piecewise_linear_distribution
<_RealType1
>&);
5246 * @brief Extracts a %piecewise_linear_distribution random number
5247 * distribution @p __x from the input stream @p __is.
5249 * @param __is An input stream.
5250 * @param __x A %piecewise_linear_distribution random number
5253 * @returns The input stream with @p __x extracted or in an error
5256 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5257 friend std::basic_istream
<_CharT
, _Traits
>&
5258 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
5259 std::piecewise_linear_distribution
<_RealType1
>&);
5262 param_type _M_param
;
5266 * @brief Return true if two piecewise linear distributions have the
5269 template<typename _RealType
>
5271 operator==(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5272 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5273 { return __d1
.param() == __d2
.param(); }
5276 * @brief Return true if two piecewise linear distributions have
5277 * different parameters.
5279 template<typename _RealType
>
5281 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5282 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5283 { return !(__d1
== __d2
); }
5286 /* @} */ // group random_distributions_poisson
5288 /* @} */ // group random_distributions
5291 * @addtogroup random_utilities Random Number Utilities
5297 * @brief The seed_seq class generates sequences of seeds for random
5298 * number generators.
5304 /** The type of the seed vales. */
5305 typedef uint_least32_t result_type
;
5307 /** Default constructor. */
5312 template<typename _IntType
>
5313 seed_seq(std::initializer_list
<_IntType
> il
);
5315 template<typename _InputIterator
>
5316 seed_seq(_InputIterator __begin
, _InputIterator __end
);
5318 // generating functions
5319 template<typename _RandomAccessIterator
>
5321 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
5323 // property functions
5325 { return _M_v
.size(); }
5327 template<typename OutputIterator
>
5329 param(OutputIterator __dest
) const
5330 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
5334 std::vector
<result_type
> _M_v
;
5337 /* @} */ // group random_utilities
5339 /* @} */ // group random