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 inclusive lower bound of the distribution range.
1702 { return this->a(); }
1705 * @brief Returns the inclusive upper bound of the distribution range.
1709 { return this->b(); }
1712 * @brief Returns the parameter set of the distribution.
1716 { return _M_param
; }
1719 * @brief Sets the parameter set of the distribution.
1720 * @param __param The new parameter set of the distribution.
1723 param(const param_type
& __param
)
1724 { _M_param
= __param
; }
1727 * Gets a uniformly distributed random number in the range
1730 template<typename _UniformRandomNumberGenerator
>
1732 operator()(_UniformRandomNumberGenerator
& __urng
)
1733 { return this->operator()(__urng
, this->param()); }
1736 * Gets a uniform random number in the range @f$[0, n)@f$.
1738 * This function is aimed at use with std::random_shuffle.
1740 template<typename _UniformRandomNumberGenerator
>
1742 operator()(_UniformRandomNumberGenerator
& __urng
,
1743 const param_type
& __p
);
1745 param_type _M_param
;
1749 * @brief Return true if two uniform integer distributions have
1750 * the same parameters.
1752 template<typename _IntType
>
1754 operator==(const std::uniform_int_distribution
<_IntType
>& __d1
,
1755 const std::uniform_int_distribution
<_IntType
>& __d2
)
1756 { return __d1
.param() == __d2
.param(); }
1759 * @brief Return true if two uniform integer distributions have
1760 * different parameters.
1762 template<typename _IntType
>
1764 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1765 const std::uniform_int_distribution
<_IntType
>& __d2
)
1766 { return !(__d1
== __d2
); }
1769 * @brief Inserts a %uniform_int_distribution random number
1770 * distribution @p __x into the output stream @p os.
1772 * @param __os An output stream.
1773 * @param __x A %uniform_int_distribution random number distribution.
1775 * @returns The output stream with the state of @p __x inserted or in
1778 template<typename _IntType
, typename _CharT
, typename _Traits
>
1779 std::basic_ostream
<_CharT
, _Traits
>&
1780 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1781 const std::uniform_int_distribution
<_IntType
>&);
1784 * @brief Extracts a %uniform_int_distribution random number distribution
1785 * @p __x from the input stream @p __is.
1787 * @param __is An input stream.
1788 * @param __x A %uniform_int_distribution random number generator engine.
1790 * @returns The input stream with @p __x extracted or in an error state.
1792 template<typename _IntType
, typename _CharT
, typename _Traits
>
1793 std::basic_istream
<_CharT
, _Traits
>&
1794 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1795 std::uniform_int_distribution
<_IntType
>&);
1799 * @brief Uniform continuous distribution for random numbers.
1801 * A continuous random distribution on the range [min, max) with equal
1802 * probability throughout the range. The URNG should be real-valued and
1803 * deliver number in the range [0, 1).
1805 template<typename _RealType
= double>
1806 class uniform_real_distribution
1808 static_assert(std::is_floating_point
<_RealType
>::value
,
1809 "template argument not a floating point type");
1812 /** The type of the range of the distribution. */
1813 typedef _RealType result_type
;
1814 /** Parameter type. */
1817 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1820 param_type(_RealType __a
= _RealType(0),
1821 _RealType __b
= _RealType(1))
1822 : _M_a(__a
), _M_b(__b
)
1824 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1836 operator==(const param_type
& __p1
, const param_type
& __p2
)
1837 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1846 * @brief Constructs a uniform_real_distribution object.
1848 * @param __min [IN] The lower bound of the distribution.
1849 * @param __max [IN] The upper bound of the distribution.
1852 uniform_real_distribution(_RealType __a
= _RealType(0),
1853 _RealType __b
= _RealType(1))
1854 : _M_param(__a
, __b
)
1858 uniform_real_distribution(const param_type
& __p
)
1863 * @brief Resets the distribution state.
1865 * Does nothing for the uniform real distribution.
1872 { return _M_param
.a(); }
1876 { return _M_param
.b(); }
1879 * @brief Returns the inclusive lower bound of the distribution range.
1883 { return this->a(); }
1886 * @brief Returns the inclusive upper bound of the distribution range.
1890 { return this->b(); }
1893 * @brief Returns the parameter set of the distribution.
1897 { return _M_param
; }
1900 * @brief Sets the parameter set of the distribution.
1901 * @param __param The new parameter set of the distribution.
1904 param(const param_type
& __param
)
1905 { _M_param
= __param
; }
1907 template<typename _UniformRandomNumberGenerator
>
1909 operator()(_UniformRandomNumberGenerator
& __urng
)
1910 { return this->operator()(__urng
, this->param()); }
1912 template<typename _UniformRandomNumberGenerator
>
1914 operator()(_UniformRandomNumberGenerator
& __urng
,
1915 const param_type
& __p
)
1917 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1919 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1923 param_type _M_param
;
1927 * @brief Return true if two uniform real distributions have
1928 * the same parameters.
1930 template<typename _IntType
>
1932 operator==(const std::uniform_real_distribution
<_IntType
>& __d1
,
1933 const std::uniform_real_distribution
<_IntType
>& __d2
)
1934 { return __d1
.param() == __d2
.param(); }
1937 * @brief Return true if two uniform real distributions have
1938 * different parameters.
1940 template<typename _IntType
>
1942 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1943 const std::uniform_real_distribution
<_IntType
>& __d2
)
1944 { return !(__d1
== __d2
); }
1947 * @brief Inserts a %uniform_real_distribution random number
1948 * distribution @p __x into the output stream @p __os.
1950 * @param __os An output stream.
1951 * @param __x A %uniform_real_distribution random number distribution.
1953 * @returns The output stream with the state of @p __x inserted or in
1956 template<typename _RealType
, typename _CharT
, typename _Traits
>
1957 std::basic_ostream
<_CharT
, _Traits
>&
1958 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1959 const std::uniform_real_distribution
<_RealType
>&);
1962 * @brief Extracts a %uniform_real_distribution random number distribution
1963 * @p __x from the input stream @p __is.
1965 * @param __is An input stream.
1966 * @param __x A %uniform_real_distribution random number generator engine.
1968 * @returns The input stream with @p __x extracted or in an error state.
1970 template<typename _RealType
, typename _CharT
, typename _Traits
>
1971 std::basic_istream
<_CharT
, _Traits
>&
1972 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1973 std::uniform_real_distribution
<_RealType
>&);
1975 /* @} */ // group random_distributions_uniform
1978 * @addtogroup random_distributions_normal Normal Distributions
1979 * @ingroup random_distributions
1984 * @brief A normal continuous distribution for random numbers.
1986 * The formula for the normal probability density function is
1988 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1989 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1992 template<typename _RealType
= double>
1993 class normal_distribution
1995 static_assert(std::is_floating_point
<_RealType
>::value
,
1996 "template argument not a floating point type");
1999 /** The type of the range of the distribution. */
2000 typedef _RealType result_type
;
2001 /** Parameter type. */
2004 typedef normal_distribution
<_RealType
> distribution_type
;
2007 param_type(_RealType __mean
= _RealType(0),
2008 _RealType __stddev
= _RealType(1))
2009 : _M_mean(__mean
), _M_stddev(__stddev
)
2011 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
2020 { return _M_stddev
; }
2023 operator==(const param_type
& __p1
, const param_type
& __p2
)
2024 { return (__p1
._M_mean
== __p2
._M_mean
2025 && __p1
._M_stddev
== __p2
._M_stddev
); }
2029 _RealType _M_stddev
;
2034 * Constructs a normal distribution with parameters @f$mean@f$ and
2035 * standard deviation.
2038 normal_distribution(result_type __mean
= result_type(0),
2039 result_type __stddev
= result_type(1))
2040 : _M_param(__mean
, __stddev
), _M_saved_available(false)
2044 normal_distribution(const param_type
& __p
)
2045 : _M_param(__p
), _M_saved_available(false)
2049 * @brief Resets the distribution state.
2053 { _M_saved_available
= false; }
2056 * @brief Returns the mean of the distribution.
2060 { return _M_param
.mean(); }
2063 * @brief Returns the standard deviation of the distribution.
2067 { return _M_param
.stddev(); }
2070 * @brief Returns the parameter set of the distribution.
2074 { return _M_param
; }
2077 * @brief Sets the parameter set of the distribution.
2078 * @param __param The new parameter set of the distribution.
2081 param(const param_type
& __param
)
2082 { _M_param
= __param
; }
2085 * @brief Returns the greatest lower bound value of the distribution.
2089 { return std::numeric_limits
<result_type
>::min(); }
2092 * @brief Returns the least upper bound value of the distribution.
2096 { return std::numeric_limits
<result_type
>::max(); }
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(); }
2268 template<typename _UniformRandomNumberGenerator
>
2270 operator()(_UniformRandomNumberGenerator
& __urng
)
2271 { return this->operator()(__urng
, this->param()); }
2273 template<typename _UniformRandomNumberGenerator
>
2275 operator()(_UniformRandomNumberGenerator
& __urng
,
2276 const param_type
& __p
)
2277 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2280 * @brief Return true if two lognormal distributions have
2281 * the same parameters and the sequences that would
2282 * be generated are equal.
2284 template<typename _RealType1
>
2286 operator==(const std::lognormal_distribution
<_RealType1
>& __d1
,
2287 const std::lognormal_distribution
<_RealType1
>& __d2
)
2288 { return (__d1
.param() == __d2
.param()
2289 && __d1
._M_nd
== __d2
._M_nd
); }
2292 * @brief Inserts a %lognormal_distribution random number distribution
2293 * @p __x into the output stream @p __os.
2295 * @param __os An output stream.
2296 * @param __x A %lognormal_distribution random number distribution.
2298 * @returns The output stream with the state of @p __x inserted or in
2301 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2302 friend std::basic_ostream
<_CharT
, _Traits
>&
2303 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2304 const std::lognormal_distribution
<_RealType1
>&);
2307 * @brief Extracts a %lognormal_distribution random number distribution
2308 * @p __x from the input stream @p __is.
2310 * @param __is An input stream.
2311 * @param __x A %lognormal_distribution random number
2314 * @returns The input stream with @p __x extracted or in an error state.
2316 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2317 friend std::basic_istream
<_CharT
, _Traits
>&
2318 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2319 std::lognormal_distribution
<_RealType1
>&);
2322 param_type _M_param
;
2324 std::normal_distribution
<result_type
> _M_nd
;
2328 * @brief Return true if two lognormal distributions are different.
2330 template<typename _RealType
>
2332 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2333 const std::lognormal_distribution
<_RealType
>& __d2
)
2334 { return !(__d1
== __d2
); }
2338 * @brief A gamma continuous distribution for random numbers.
2340 * The formula for the gamma probability density function is:
2342 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2343 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2346 template<typename _RealType
= double>
2347 class gamma_distribution
2349 static_assert(std::is_floating_point
<_RealType
>::value
,
2350 "template argument not a floating point type");
2353 /** The type of the range of the distribution. */
2354 typedef _RealType result_type
;
2355 /** Parameter type. */
2358 typedef gamma_distribution
<_RealType
> distribution_type
;
2359 friend class gamma_distribution
<_RealType
>;
2362 param_type(_RealType __alpha_val
= _RealType(1),
2363 _RealType __beta_val
= _RealType(1))
2364 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2366 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2372 { return _M_alpha
; }
2379 operator==(const param_type
& __p1
, const param_type
& __p2
)
2380 { return (__p1
._M_alpha
== __p2
._M_alpha
2381 && __p1
._M_beta
== __p2
._M_beta
); }
2390 _RealType _M_malpha
, _M_a2
;
2395 * @brief Constructs a gamma distribution with parameters
2396 * @f$\alpha@f$ and @f$\beta@f$.
2399 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2400 _RealType __beta_val
= _RealType(1))
2401 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2405 gamma_distribution(const param_type
& __p
)
2406 : _M_param(__p
), _M_nd()
2410 * @brief Resets the distribution state.
2417 * @brief Returns the @f$\alpha@f$ of the distribution.
2421 { return _M_param
.alpha(); }
2424 * @brief Returns the @f$\beta@f$ of the distribution.
2428 { return _M_param
.beta(); }
2431 * @brief Returns the parameter set of the distribution.
2435 { return _M_param
; }
2438 * @brief Sets the parameter set of the distribution.
2439 * @param __param The new parameter set of the distribution.
2442 param(const param_type
& __param
)
2443 { _M_param
= __param
; }
2446 * @brief Returns the greatest lower bound value of the distribution.
2450 { return result_type(0); }
2453 * @brief Returns the least upper bound value of the distribution.
2457 { return std::numeric_limits
<result_type
>::max(); }
2459 template<typename _UniformRandomNumberGenerator
>
2461 operator()(_UniformRandomNumberGenerator
& __urng
)
2462 { return this->operator()(__urng
, this->param()); }
2464 template<typename _UniformRandomNumberGenerator
>
2466 operator()(_UniformRandomNumberGenerator
& __urng
,
2467 const param_type
& __p
);
2470 * @brief Return true if two gamma distributions have the same
2471 * parameters and the sequences that would be generated
2474 template<typename _RealType1
>
2476 operator==(const std::gamma_distribution
<_RealType1
>& __d1
,
2477 const std::gamma_distribution
<_RealType1
>& __d2
)
2478 { return (__d1
.param() == __d2
.param()
2479 && __d1
._M_nd
== __d2
._M_nd
); }
2482 * @brief Inserts a %gamma_distribution random number distribution
2483 * @p __x into the output stream @p __os.
2485 * @param __os An output stream.
2486 * @param __x A %gamma_distribution random number distribution.
2488 * @returns The output stream with the state of @p __x inserted or in
2491 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2492 friend std::basic_ostream
<_CharT
, _Traits
>&
2493 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2494 const std::gamma_distribution
<_RealType1
>&);
2497 * @brief Extracts a %gamma_distribution random number distribution
2498 * @p __x from the input stream @p __is.
2500 * @param __is An input stream.
2501 * @param __x A %gamma_distribution random number generator engine.
2503 * @returns The input stream with @p __x extracted or in an error state.
2505 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2506 friend std::basic_istream
<_CharT
, _Traits
>&
2507 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2508 std::gamma_distribution
<_RealType1
>&);
2511 param_type _M_param
;
2513 std::normal_distribution
<result_type
> _M_nd
;
2517 * @brief Return true if two gamma distributions are different.
2519 template<typename _RealType
>
2521 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2522 const std::gamma_distribution
<_RealType
>& __d2
)
2523 { return !(__d1
== __d2
); }
2527 * @brief A chi_squared_distribution random number distribution.
2529 * The formula for the normal probability mass function is
2530 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2532 template<typename _RealType
= double>
2533 class chi_squared_distribution
2535 static_assert(std::is_floating_point
<_RealType
>::value
,
2536 "template argument not a floating point type");
2539 /** The type of the range of the distribution. */
2540 typedef _RealType result_type
;
2541 /** Parameter type. */
2544 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2547 param_type(_RealType __n
= _RealType(1))
2556 operator==(const param_type
& __p1
, const param_type
& __p2
)
2557 { return __p1
._M_n
== __p2
._M_n
; }
2564 chi_squared_distribution(_RealType __n
= _RealType(1))
2565 : _M_param(__n
), _M_gd(__n
/ 2)
2569 chi_squared_distribution(const param_type
& __p
)
2570 : _M_param(__p
), _M_gd(__p
.n() / 2)
2574 * @brief Resets the distribution state.
2585 { return _M_param
.n(); }
2588 * @brief Returns the parameter set of the distribution.
2592 { return _M_param
; }
2595 * @brief Sets the parameter set of the distribution.
2596 * @param __param The new parameter set of the distribution.
2599 param(const param_type
& __param
)
2600 { _M_param
= __param
; }
2603 * @brief Returns the greatest lower bound value of the distribution.
2607 { return result_type(0); }
2610 * @brief Returns the least upper bound value of the distribution.
2614 { return std::numeric_limits
<result_type
>::max(); }
2616 template<typename _UniformRandomNumberGenerator
>
2618 operator()(_UniformRandomNumberGenerator
& __urng
)
2619 { return 2 * _M_gd(__urng
); }
2621 template<typename _UniformRandomNumberGenerator
>
2623 operator()(_UniformRandomNumberGenerator
& __urng
,
2624 const param_type
& __p
)
2626 typedef typename
std::gamma_distribution
<result_type
>::param_type
2628 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2632 * @brief Return true if two Chi-squared distributions have
2633 * the same parameters and the sequences that would be
2634 * generated are equal.
2636 template<typename _RealType1
>
2638 operator==(const std::chi_squared_distribution
<_RealType1
>& __d1
,
2639 const std::chi_squared_distribution
<_RealType1
>& __d2
)
2640 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
2643 * @brief Inserts a %chi_squared_distribution random number distribution
2644 * @p __x into the output stream @p __os.
2646 * @param __os An output stream.
2647 * @param __x A %chi_squared_distribution random number distribution.
2649 * @returns The output stream with the state of @p __x inserted or in
2652 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2653 friend std::basic_ostream
<_CharT
, _Traits
>&
2654 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2655 const std::chi_squared_distribution
<_RealType1
>&);
2658 * @brief Extracts a %chi_squared_distribution random number distribution
2659 * @p __x from the input stream @p __is.
2661 * @param __is An input stream.
2662 * @param __x A %chi_squared_distribution random number
2665 * @returns The input stream with @p __x extracted or in an error state.
2667 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2668 friend std::basic_istream
<_CharT
, _Traits
>&
2669 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2670 std::chi_squared_distribution
<_RealType1
>&);
2673 param_type _M_param
;
2675 std::gamma_distribution
<result_type
> _M_gd
;
2679 * @brief Return true if two Chi-squared distributions are different.
2681 template<typename _RealType
>
2683 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2684 const std::chi_squared_distribution
<_RealType
>& __d2
)
2685 { return !(__d1
== __d2
); }
2689 * @brief A cauchy_distribution random number distribution.
2691 * The formula for the normal probability mass function is
2692 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2694 template<typename _RealType
= double>
2695 class cauchy_distribution
2697 static_assert(std::is_floating_point
<_RealType
>::value
,
2698 "template argument not a floating point type");
2701 /** The type of the range of the distribution. */
2702 typedef _RealType result_type
;
2703 /** Parameter type. */
2706 typedef cauchy_distribution
<_RealType
> distribution_type
;
2709 param_type(_RealType __a
= _RealType(0),
2710 _RealType __b
= _RealType(1))
2711 : _M_a(__a
), _M_b(__b
)
2723 operator==(const param_type
& __p1
, const param_type
& __p2
)
2724 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2732 cauchy_distribution(_RealType __a
= _RealType(0),
2733 _RealType __b
= _RealType(1))
2734 : _M_param(__a
, __b
)
2738 cauchy_distribution(const param_type
& __p
)
2743 * @brief Resets the distribution state.
2754 { return _M_param
.a(); }
2758 { return _M_param
.b(); }
2761 * @brief Returns the parameter set of the distribution.
2765 { return _M_param
; }
2768 * @brief Sets the parameter set of the distribution.
2769 * @param __param The new parameter set of the distribution.
2772 param(const param_type
& __param
)
2773 { _M_param
= __param
; }
2776 * @brief Returns the greatest lower bound value of the distribution.
2780 { return std::numeric_limits
<result_type
>::min(); }
2783 * @brief Returns the least upper bound value of the distribution.
2787 { return std::numeric_limits
<result_type
>::max(); }
2789 template<typename _UniformRandomNumberGenerator
>
2791 operator()(_UniformRandomNumberGenerator
& __urng
)
2792 { return this->operator()(__urng
, this->param()); }
2794 template<typename _UniformRandomNumberGenerator
>
2796 operator()(_UniformRandomNumberGenerator
& __urng
,
2797 const param_type
& __p
);
2800 param_type _M_param
;
2804 * @brief Return true if two Cauchy distributions have
2805 * the same parameters.
2807 template<typename _RealType
>
2809 operator==(const std::cauchy_distribution
<_RealType
>& __d1
,
2810 const std::cauchy_distribution
<_RealType
>& __d2
)
2811 { return __d1
.param() == __d2
.param(); }
2814 * @brief Return true if two Cauchy distributions have
2815 * different parameters.
2817 template<typename _RealType
>
2819 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
2820 const std::cauchy_distribution
<_RealType
>& __d2
)
2821 { return !(__d1
== __d2
); }
2824 * @brief Inserts a %cauchy_distribution random number distribution
2825 * @p __x into the output stream @p __os.
2827 * @param __os An output stream.
2828 * @param __x A %cauchy_distribution random number distribution.
2830 * @returns The output stream with the state of @p __x inserted or in
2833 template<typename _RealType
, typename _CharT
, typename _Traits
>
2834 std::basic_ostream
<_CharT
, _Traits
>&
2835 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2836 const std::cauchy_distribution
<_RealType
>&);
2839 * @brief Extracts a %cauchy_distribution random number distribution
2840 * @p __x from the input stream @p __is.
2842 * @param __is An input stream.
2843 * @param __x A %cauchy_distribution random number
2846 * @returns The input stream with @p __x extracted or in an error state.
2848 template<typename _RealType
, typename _CharT
, typename _Traits
>
2849 std::basic_istream
<_CharT
, _Traits
>&
2850 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2851 std::cauchy_distribution
<_RealType
>&);
2855 * @brief A fisher_f_distribution random number distribution.
2857 * The formula for the normal probability mass function is
2859 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2860 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2861 * (1 + \frac{mx}{n})^{-(m+n)/2}
2864 template<typename _RealType
= double>
2865 class fisher_f_distribution
2867 static_assert(std::is_floating_point
<_RealType
>::value
,
2868 "template argument not a floating point type");
2871 /** The type of the range of the distribution. */
2872 typedef _RealType result_type
;
2873 /** Parameter type. */
2876 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2879 param_type(_RealType __m
= _RealType(1),
2880 _RealType __n
= _RealType(1))
2881 : _M_m(__m
), _M_n(__n
)
2893 operator==(const param_type
& __p1
, const param_type
& __p2
)
2894 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
2902 fisher_f_distribution(_RealType __m
= _RealType(1),
2903 _RealType __n
= _RealType(1))
2904 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2908 fisher_f_distribution(const param_type
& __p
)
2909 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2913 * @brief Resets the distribution state.
2927 { return _M_param
.m(); }
2931 { return _M_param
.n(); }
2934 * @brief Returns the parameter set of the distribution.
2938 { return _M_param
; }
2941 * @brief Sets the parameter set of the distribution.
2942 * @param __param The new parameter set of the distribution.
2945 param(const param_type
& __param
)
2946 { _M_param
= __param
; }
2949 * @brief Returns the greatest lower bound value of the distribution.
2953 { return result_type(0); }
2956 * @brief Returns the least upper bound value of the distribution.
2960 { return std::numeric_limits
<result_type
>::max(); }
2962 template<typename _UniformRandomNumberGenerator
>
2964 operator()(_UniformRandomNumberGenerator
& __urng
)
2965 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
2967 template<typename _UniformRandomNumberGenerator
>
2969 operator()(_UniformRandomNumberGenerator
& __urng
,
2970 const param_type
& __p
)
2972 typedef typename
std::gamma_distribution
<result_type
>::param_type
2974 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
2975 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
2979 * @brief Return true if two Fisher f distributions have
2980 * the same parameters and the sequences that would
2981 * be generated are equal.
2983 template<typename _RealType1
>
2985 operator==(const std::fisher_f_distribution
<_RealType1
>& __d1
,
2986 const std::fisher_f_distribution
<_RealType1
>& __d2
)
2987 { return (__d1
.param() == __d2
.param()
2988 && __d1
._M_gd_x
== __d2
._M_gd_x
2989 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
2992 * @brief Inserts a %fisher_f_distribution random number distribution
2993 * @p __x into the output stream @p __os.
2995 * @param __os An output stream.
2996 * @param __x A %fisher_f_distribution random number distribution.
2998 * @returns The output stream with the state of @p __x inserted or in
3001 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3002 friend std::basic_ostream
<_CharT
, _Traits
>&
3003 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3004 const std::fisher_f_distribution
<_RealType1
>&);
3007 * @brief Extracts a %fisher_f_distribution random number distribution
3008 * @p __x from the input stream @p __is.
3010 * @param __is An input stream.
3011 * @param __x A %fisher_f_distribution random number
3014 * @returns The input stream with @p __x extracted or in an error state.
3016 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3017 friend std::basic_istream
<_CharT
, _Traits
>&
3018 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3019 std::fisher_f_distribution
<_RealType1
>&);
3022 param_type _M_param
;
3024 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3028 * @brief Return true if two Fisher f distributions are diferent.
3030 template<typename _RealType
>
3032 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3033 const std::fisher_f_distribution
<_RealType
>& __d2
)
3034 { return !(__d1
== __d2
); }
3037 * @brief A student_t_distribution random number distribution.
3039 * The formula for the normal probability mass function is:
3041 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3042 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3045 template<typename _RealType
= double>
3046 class student_t_distribution
3048 static_assert(std::is_floating_point
<_RealType
>::value
,
3049 "template argument not a floating point type");
3052 /** The type of the range of the distribution. */
3053 typedef _RealType result_type
;
3054 /** Parameter type. */
3057 typedef student_t_distribution
<_RealType
> distribution_type
;
3060 param_type(_RealType __n
= _RealType(1))
3069 operator==(const param_type
& __p1
, const param_type
& __p2
)
3070 { return __p1
._M_n
== __p2
._M_n
; }
3077 student_t_distribution(_RealType __n
= _RealType(1))
3078 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3082 student_t_distribution(const param_type
& __p
)
3083 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3087 * @brief Resets the distribution state.
3101 { return _M_param
.n(); }
3104 * @brief Returns the parameter set of the distribution.
3108 { return _M_param
; }
3111 * @brief Sets the parameter set of the distribution.
3112 * @param __param The new parameter set of the distribution.
3115 param(const param_type
& __param
)
3116 { _M_param
= __param
; }
3119 * @brief Returns the greatest lower bound value of the distribution.
3123 { return std::numeric_limits
<result_type
>::min(); }
3126 * @brief Returns the least upper bound value of the distribution.
3130 { return std::numeric_limits
<result_type
>::max(); }
3132 template<typename _UniformRandomNumberGenerator
>
3134 operator()(_UniformRandomNumberGenerator
& __urng
)
3135 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3137 template<typename _UniformRandomNumberGenerator
>
3139 operator()(_UniformRandomNumberGenerator
& __urng
,
3140 const param_type
& __p
)
3142 typedef typename
std::gamma_distribution
<result_type
>::param_type
3145 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3146 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3150 * @brief Return true if two Student t distributions have
3151 * the same parameters and the sequences that would
3152 * be generated are equal.
3154 template<typename _RealType1
>
3156 operator==(const std::student_t_distribution
<_RealType1
>& __d1
,
3157 const std::student_t_distribution
<_RealType1
>& __d2
)
3158 { return (__d1
.param() == __d2
.param()
3159 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3162 * @brief Inserts a %student_t_distribution random number distribution
3163 * @p __x into the output stream @p __os.
3165 * @param __os An output stream.
3166 * @param __x A %student_t_distribution random number distribution.
3168 * @returns The output stream with the state of @p __x inserted or in
3171 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3172 friend std::basic_ostream
<_CharT
, _Traits
>&
3173 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3174 const std::student_t_distribution
<_RealType1
>&);
3177 * @brief Extracts a %student_t_distribution random number distribution
3178 * @p __x from the input stream @p __is.
3180 * @param __is An input stream.
3181 * @param __x A %student_t_distribution random number
3184 * @returns The input stream with @p __x extracted or in an error state.
3186 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3187 friend std::basic_istream
<_CharT
, _Traits
>&
3188 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3189 std::student_t_distribution
<_RealType1
>&);
3192 param_type _M_param
;
3194 std::normal_distribution
<result_type
> _M_nd
;
3195 std::gamma_distribution
<result_type
> _M_gd
;
3199 * @brief Return true if two Student t distributions are different.
3201 template<typename _RealType
>
3203 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3204 const std::student_t_distribution
<_RealType
>& __d2
)
3205 { return !(__d1
== __d2
); }
3208 /* @} */ // group random_distributions_normal
3211 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3212 * @ingroup random_distributions
3217 * @brief A Bernoulli random number distribution.
3219 * Generates a sequence of true and false values with likelihood @f$p@f$
3220 * that true will come up and @f$(1 - p)@f$ that false will appear.
3222 class bernoulli_distribution
3225 /** The type of the range of the distribution. */
3226 typedef bool result_type
;
3227 /** Parameter type. */
3230 typedef bernoulli_distribution distribution_type
;
3233 param_type(double __p
= 0.5)
3236 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
3244 operator==(const param_type
& __p1
, const param_type
& __p2
)
3245 { return __p1
._M_p
== __p2
._M_p
; }
3253 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3255 * @param __p [IN] The likelihood of a true result being returned.
3256 * Must be in the interval @f$[0, 1]@f$.
3259 bernoulli_distribution(double __p
= 0.5)
3264 bernoulli_distribution(const param_type
& __p
)
3269 * @brief Resets the distribution state.
3271 * Does nothing for a Bernoulli distribution.
3277 * @brief Returns the @p p parameter of the distribution.
3281 { return _M_param
.p(); }
3284 * @brief Returns the parameter set of the distribution.
3288 { return _M_param
; }
3291 * @brief Sets the parameter set of the distribution.
3292 * @param __param The new parameter set of the distribution.
3295 param(const param_type
& __param
)
3296 { _M_param
= __param
; }
3299 * @brief Returns the greatest lower bound value of the distribution.
3303 { return std::numeric_limits
<result_type
>::min(); }
3306 * @brief Returns the least upper bound value of the distribution.
3310 { return std::numeric_limits
<result_type
>::max(); }
3313 * @brief Returns the next value in the Bernoullian sequence.
3315 template<typename _UniformRandomNumberGenerator
>
3317 operator()(_UniformRandomNumberGenerator
& __urng
)
3318 { return this->operator()(__urng
, this->param()); }
3320 template<typename _UniformRandomNumberGenerator
>
3322 operator()(_UniformRandomNumberGenerator
& __urng
,
3323 const param_type
& __p
)
3325 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3327 if ((__aurng() - __aurng
.min())
3328 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3334 param_type _M_param
;
3338 * @brief Return true if two Bernoulli distributions have
3339 * the same parameters.
3342 operator==(const std::bernoulli_distribution
& __d1
,
3343 const std::bernoulli_distribution
& __d2
)
3344 { return __d1
.param() == __d2
.param(); }
3347 * @brief Return true if two Bernoulli distributions have
3348 * different parameters.
3351 operator!=(const std::bernoulli_distribution
& __d1
,
3352 const std::bernoulli_distribution
& __d2
)
3353 { return !(__d1
== __d2
); }
3356 * @brief Inserts a %bernoulli_distribution random number distribution
3357 * @p __x into the output stream @p __os.
3359 * @param __os An output stream.
3360 * @param __x A %bernoulli_distribution random number distribution.
3362 * @returns The output stream with the state of @p __x inserted or in
3365 template<typename _CharT
, typename _Traits
>
3366 std::basic_ostream
<_CharT
, _Traits
>&
3367 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3368 const std::bernoulli_distribution
&);
3371 * @brief Extracts a %bernoulli_distribution random number distribution
3372 * @p __x from the input stream @p __is.
3374 * @param __is An input stream.
3375 * @param __x A %bernoulli_distribution random number generator engine.
3377 * @returns The input stream with @p __x extracted or in an error state.
3379 template<typename _CharT
, typename _Traits
>
3380 std::basic_istream
<_CharT
, _Traits
>&
3381 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3382 std::bernoulli_distribution
& __x
)
3386 __x
.param(bernoulli_distribution::param_type(__p
));
3392 * @brief A discrete binomial random number distribution.
3394 * The formula for the binomial probability density function is
3395 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3396 * and @f$p@f$ are the parameters of the distribution.
3398 template<typename _IntType
= int>
3399 class binomial_distribution
3401 static_assert(std::is_integral
<_IntType
>::value
,
3402 "template argument not an integral type");
3405 /** The type of the range of the distribution. */
3406 typedef _IntType result_type
;
3407 /** Parameter type. */
3410 typedef binomial_distribution
<_IntType
> distribution_type
;
3411 friend class binomial_distribution
<_IntType
>;
3414 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3415 : _M_t(__t
), _M_p(__p
)
3417 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3432 operator==(const param_type
& __p1
, const param_type
& __p2
)
3433 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3443 #if _GLIBCXX_USE_C99_MATH_TR1
3444 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3445 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3450 // constructors and member function
3452 binomial_distribution(_IntType __t
= _IntType(1),
3454 : _M_param(__t
, __p
), _M_nd()
3458 binomial_distribution(const param_type
& __p
)
3459 : _M_param(__p
), _M_nd()
3463 * @brief Resets the distribution state.
3470 * @brief Returns the distribution @p t parameter.
3474 { return _M_param
.t(); }
3477 * @brief Returns the distribution @p p parameter.
3481 { return _M_param
.p(); }
3484 * @brief Returns the parameter set of the distribution.
3488 { return _M_param
; }
3491 * @brief Sets the parameter set of the distribution.
3492 * @param __param The new parameter set of the distribution.
3495 param(const param_type
& __param
)
3496 { _M_param
= __param
; }
3499 * @brief Returns the greatest lower bound value of the distribution.
3506 * @brief Returns the least upper bound value of the distribution.
3510 { return _M_param
.t(); }
3513 * @brief Return true if two binomial distributions have
3514 * the same parameters and the sequences that would
3515 * be generated are equal.
3517 template<typename _IntType1
>
3519 operator==(const std::binomial_distribution
<_IntType1
>& __d1
,
3520 const std::binomial_distribution
<_IntType1
>& __d2
)
3521 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3522 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
3524 { return __d1
.param() == __d2
.param(); }
3527 template<typename _UniformRandomNumberGenerator
>
3529 operator()(_UniformRandomNumberGenerator
& __urng
)
3530 { return this->operator()(__urng
, this->param()); }
3532 template<typename _UniformRandomNumberGenerator
>
3534 operator()(_UniformRandomNumberGenerator
& __urng
,
3535 const param_type
& __p
);
3538 * @brief Inserts a %binomial_distribution random number distribution
3539 * @p __x into the output stream @p __os.
3541 * @param __os An output stream.
3542 * @param __x A %binomial_distribution random number distribution.
3544 * @returns The output stream with the state of @p __x inserted or in
3547 template<typename _IntType1
,
3548 typename _CharT
, typename _Traits
>
3549 friend std::basic_ostream
<_CharT
, _Traits
>&
3550 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3551 const std::binomial_distribution
<_IntType1
>&);
3554 * @brief Extracts a %binomial_distribution random number distribution
3555 * @p __x from the input stream @p __is.
3557 * @param __is An input stream.
3558 * @param __x A %binomial_distribution random number generator engine.
3560 * @returns The input stream with @p __x extracted or in an error
3563 template<typename _IntType1
,
3564 typename _CharT
, typename _Traits
>
3565 friend std::basic_istream
<_CharT
, _Traits
>&
3566 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3567 std::binomial_distribution
<_IntType1
>&);
3570 template<typename _UniformRandomNumberGenerator
>
3572 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3574 param_type _M_param
;
3576 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3577 std::normal_distribution
<double> _M_nd
;
3581 * @brief Return true if two binomial distributions are different.
3583 template<typename _IntType
>
3585 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
3586 const std::binomial_distribution
<_IntType
>& __d2
)
3587 { return !(__d1
== __d2
); }
3591 * @brief A discrete geometric random number distribution.
3593 * The formula for the geometric probability density function is
3594 * @f$p(i|p) = (1 - p)p^{i-1}@f$ where @f$p@f$ is the parameter of the
3597 template<typename _IntType
= int>
3598 class geometric_distribution
3600 static_assert(std::is_integral
<_IntType
>::value
,
3601 "template argument not an integral type");
3604 /** The type of the range of the distribution. */
3605 typedef _IntType result_type
;
3606 /** Parameter type. */
3609 typedef geometric_distribution
<_IntType
> distribution_type
;
3610 friend class geometric_distribution
<_IntType
>;
3613 param_type(double __p
= 0.5)
3616 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0)
3626 operator==(const param_type
& __p1
, const param_type
& __p2
)
3627 { return __p1
._M_p
== __p2
._M_p
; }
3632 { _M_log_p
= std::log(_M_p
); }
3639 // constructors and member function
3641 geometric_distribution(double __p
= 0.5)
3646 geometric_distribution(const param_type
& __p
)
3651 * @brief Resets the distribution state.
3653 * Does nothing for the geometric distribution.
3659 * @brief Returns the distribution parameter @p p.
3663 { return _M_param
.p(); }
3666 * @brief Returns the parameter set of the distribution.
3670 { return _M_param
; }
3673 * @brief Sets the parameter set of the distribution.
3674 * @param __param The new parameter set of the distribution.
3677 param(const param_type
& __param
)
3678 { _M_param
= __param
; }
3681 * @brief Returns the greatest lower bound value of the distribution.
3688 * @brief Returns the least upper bound value of the distribution.
3692 { return std::numeric_limits
<result_type
>::max(); }
3694 template<typename _UniformRandomNumberGenerator
>
3696 operator()(_UniformRandomNumberGenerator
& __urng
)
3697 { return this->operator()(__urng
, this->param()); }
3699 template<typename _UniformRandomNumberGenerator
>
3701 operator()(_UniformRandomNumberGenerator
& __urng
,
3702 const param_type
& __p
);
3705 param_type _M_param
;
3709 * @brief Return true if two geometric distributions have
3710 * the same parameters.
3712 template<typename _IntType
>
3714 operator==(const std::geometric_distribution
<_IntType
>& __d1
,
3715 const std::geometric_distribution
<_IntType
>& __d2
)
3716 { return __d1
.param() == __d2
.param(); }
3719 * @brief Return true if two geometric distributions have
3720 * different parameters.
3722 template<typename _IntType
>
3724 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
3725 const std::geometric_distribution
<_IntType
>& __d2
)
3726 { return !(__d1
== __d2
); }
3729 * @brief Inserts a %geometric_distribution random number distribution
3730 * @p __x into the output stream @p __os.
3732 * @param __os An output stream.
3733 * @param __x A %geometric_distribution random number distribution.
3735 * @returns The output stream with the state of @p __x inserted or in
3738 template<typename _IntType
,
3739 typename _CharT
, typename _Traits
>
3740 std::basic_ostream
<_CharT
, _Traits
>&
3741 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3742 const std::geometric_distribution
<_IntType
>&);
3745 * @brief Extracts a %geometric_distribution random number distribution
3746 * @p __x from the input stream @p __is.
3748 * @param __is An input stream.
3749 * @param __x A %geometric_distribution random number generator engine.
3751 * @returns The input stream with @p __x extracted or in an error state.
3753 template<typename _IntType
,
3754 typename _CharT
, typename _Traits
>
3755 std::basic_istream
<_CharT
, _Traits
>&
3756 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3757 std::geometric_distribution
<_IntType
>&);
3761 * @brief A negative_binomial_distribution random number distribution.
3763 * The formula for the negative binomial probability mass function is
3764 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3765 * and @f$p@f$ are the parameters of the distribution.
3767 template<typename _IntType
= int>
3768 class negative_binomial_distribution
3770 static_assert(std::is_integral
<_IntType
>::value
,
3771 "template argument not an integral type");
3774 /** The type of the range of the distribution. */
3775 typedef _IntType result_type
;
3776 /** Parameter type. */
3779 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3782 param_type(_IntType __k
= 1, double __p
= 0.5)
3783 : _M_k(__k
), _M_p(__p
)
3795 operator==(const param_type
& __p1
, const param_type
& __p2
)
3796 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
3804 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3805 : _M_param(__k
, __p
), _M_gd(__k
, __p
/ (1.0 - __p
))
3809 negative_binomial_distribution(const param_type
& __p
)
3810 : _M_param(__p
), _M_gd(__p
.k(), __p
.p() / (1.0 - __p
.p()))
3814 * @brief Resets the distribution state.
3821 * @brief Return the @f$k@f$ parameter of the distribution.
3825 { return _M_param
.k(); }
3828 * @brief Return the @f$p@f$ parameter of the distribution.
3832 { return _M_param
.p(); }
3835 * @brief Returns the parameter set of the distribution.
3839 { return _M_param
; }
3842 * @brief Sets the parameter set of the distribution.
3843 * @param __param The new parameter set of the distribution.
3846 param(const param_type
& __param
)
3847 { _M_param
= __param
; }
3850 * @brief Returns the greatest lower bound value of the distribution.
3854 { return result_type(0); }
3857 * @brief Returns the least upper bound value of the distribution.
3861 { return std::numeric_limits
<result_type
>::max(); }
3863 template<typename _UniformRandomNumberGenerator
>
3865 operator()(_UniformRandomNumberGenerator
& __urng
);
3867 template<typename _UniformRandomNumberGenerator
>
3869 operator()(_UniformRandomNumberGenerator
& __urng
,
3870 const param_type
& __p
);
3873 * @brief Return true if two negative binomial distributions have
3874 * the same parameters and the sequences that would be
3875 * generated are equal.
3877 template<typename _IntType1
>
3879 operator==(const std::negative_binomial_distribution
<_IntType1
>& __d1
,
3880 const std::negative_binomial_distribution
<_IntType1
>& __d2
)
3881 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
3884 * @brief Inserts a %negative_binomial_distribution random
3885 * number distribution @p __x into the output stream @p __os.
3887 * @param __os An output stream.
3888 * @param __x A %negative_binomial_distribution random number
3891 * @returns The output stream with the state of @p __x inserted or in
3894 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3895 friend std::basic_ostream
<_CharT
, _Traits
>&
3896 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3897 const std::negative_binomial_distribution
<_IntType1
>&);
3900 * @brief Extracts a %negative_binomial_distribution random number
3901 * distribution @p __x from the input stream @p __is.
3903 * @param __is An input stream.
3904 * @param __x A %negative_binomial_distribution random number
3907 * @returns The input stream with @p __x extracted or in an error state.
3909 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3910 friend std::basic_istream
<_CharT
, _Traits
>&
3911 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3912 std::negative_binomial_distribution
<_IntType1
>&);
3915 param_type _M_param
;
3917 std::gamma_distribution
<double> _M_gd
;
3921 * @brief Return true if two negative binomial distributions are different.
3923 template<typename _IntType
>
3925 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
3926 const std::negative_binomial_distribution
<_IntType
>& __d2
)
3927 { return !(__d1
== __d2
); }
3930 /* @} */ // group random_distributions_bernoulli
3933 * @addtogroup random_distributions_poisson Poisson Distributions
3934 * @ingroup random_distributions
3939 * @brief A discrete Poisson random number distribution.
3941 * The formula for the Poisson probability density function is
3942 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
3943 * parameter of the distribution.
3945 template<typename _IntType
= int>
3946 class poisson_distribution
3948 static_assert(std::is_integral
<_IntType
>::value
,
3949 "template argument not an integral type");
3952 /** The type of the range of the distribution. */
3953 typedef _IntType result_type
;
3954 /** Parameter type. */
3957 typedef poisson_distribution
<_IntType
> distribution_type
;
3958 friend class poisson_distribution
<_IntType
>;
3961 param_type(double __mean
= 1.0)
3964 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3973 operator==(const param_type
& __p1
, const param_type
& __p2
)
3974 { return __p1
._M_mean
== __p2
._M_mean
; }
3977 // Hosts either log(mean) or the threshold of the simple method.
3984 #if _GLIBCXX_USE_C99_MATH_TR1
3985 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
3989 // constructors and member function
3991 poisson_distribution(double __mean
= 1.0)
3992 : _M_param(__mean
), _M_nd()
3996 poisson_distribution(const param_type
& __p
)
3997 : _M_param(__p
), _M_nd()
4001 * @brief Resets the distribution state.
4008 * @brief Returns the distribution parameter @p mean.
4012 { return _M_param
.mean(); }
4015 * @brief Returns the parameter set of the distribution.
4019 { return _M_param
; }
4022 * @brief Sets the parameter set of the distribution.
4023 * @param __param The new parameter set of the distribution.
4026 param(const param_type
& __param
)
4027 { _M_param
= __param
; }
4030 * @brief Returns the greatest lower bound value of the distribution.
4037 * @brief Returns the least upper bound value of the distribution.
4041 { return std::numeric_limits
<result_type
>::max(); }
4043 template<typename _UniformRandomNumberGenerator
>
4045 operator()(_UniformRandomNumberGenerator
& __urng
)
4046 { return this->operator()(__urng
, this->param()); }
4048 template<typename _UniformRandomNumberGenerator
>
4050 operator()(_UniformRandomNumberGenerator
& __urng
,
4051 const param_type
& __p
);
4054 * @brief Return true if two Poisson distributions have the same
4055 * parameters and the sequences that would be generated
4058 template<typename _IntType1
>
4060 operator==(const std::poisson_distribution
<_IntType1
>& __d1
,
4061 const std::poisson_distribution
<_IntType1
>& __d2
)
4062 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4063 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
4065 { return __d1
.param() == __d2
.param(); }
4069 * @brief Inserts a %poisson_distribution random number distribution
4070 * @p __x into the output stream @p __os.
4072 * @param __os An output stream.
4073 * @param __x A %poisson_distribution random number distribution.
4075 * @returns The output stream with the state of @p __x inserted or in
4078 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4079 friend std::basic_ostream
<_CharT
, _Traits
>&
4080 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4081 const std::poisson_distribution
<_IntType1
>&);
4084 * @brief Extracts a %poisson_distribution random number distribution
4085 * @p __x from the input stream @p __is.
4087 * @param __is An input stream.
4088 * @param __x A %poisson_distribution random number generator engine.
4090 * @returns The input stream with @p __x extracted or in an error
4093 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4094 friend std::basic_istream
<_CharT
, _Traits
>&
4095 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4096 std::poisson_distribution
<_IntType1
>&);
4099 param_type _M_param
;
4101 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4102 std::normal_distribution
<double> _M_nd
;
4106 * @brief Return true if two Poisson distributions are different.
4108 template<typename _IntType
>
4110 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4111 const std::poisson_distribution
<_IntType
>& __d2
)
4112 { return !(__d1
== __d2
); }
4116 * @brief An exponential continuous distribution for random numbers.
4118 * The formula for the exponential probability density function is
4119 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4121 * <table border=1 cellpadding=10 cellspacing=0>
4122 * <caption align=top>Distribution Statistics</caption>
4123 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4124 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4125 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4126 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4127 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4130 template<typename _RealType
= double>
4131 class exponential_distribution
4133 static_assert(std::is_floating_point
<_RealType
>::value
,
4134 "template argument not a floating point type");
4137 /** The type of the range of the distribution. */
4138 typedef _RealType result_type
;
4139 /** Parameter type. */
4142 typedef exponential_distribution
<_RealType
> distribution_type
;
4145 param_type(_RealType __lambda
= _RealType(1))
4146 : _M_lambda(__lambda
)
4148 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
4153 { return _M_lambda
; }
4156 operator==(const param_type
& __p1
, const param_type
& __p2
)
4157 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4160 _RealType _M_lambda
;
4165 * @brief Constructs an exponential distribution with inverse scale
4166 * parameter @f$\lambda@f$.
4169 exponential_distribution(const result_type
& __lambda
= result_type(1))
4170 : _M_param(__lambda
)
4174 exponential_distribution(const param_type
& __p
)
4179 * @brief Resets the distribution state.
4181 * Has no effect on exponential distributions.
4187 * @brief Returns the inverse scale parameter of the distribution.
4191 { return _M_param
.lambda(); }
4194 * @brief Returns the parameter set of the distribution.
4198 { return _M_param
; }
4201 * @brief Sets the parameter set of the distribution.
4202 * @param __param The new parameter set of the distribution.
4205 param(const param_type
& __param
)
4206 { _M_param
= __param
; }
4209 * @brief Returns the greatest lower bound value of the distribution.
4213 { return result_type(0); }
4216 * @brief Returns the least upper bound value of the distribution.
4220 { return std::numeric_limits
<result_type
>::max(); }
4222 template<typename _UniformRandomNumberGenerator
>
4224 operator()(_UniformRandomNumberGenerator
& __urng
)
4225 { return this->operator()(__urng
, this->param()); }
4227 template<typename _UniformRandomNumberGenerator
>
4229 operator()(_UniformRandomNumberGenerator
& __urng
,
4230 const param_type
& __p
)
4232 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4234 return -std::log(__aurng()) / __p
.lambda();
4238 param_type _M_param
;
4242 * @brief Return true if two exponential distributions have the same
4245 template<typename _RealType
>
4247 operator==(const std::exponential_distribution
<_RealType
>& __d1
,
4248 const std::exponential_distribution
<_RealType
>& __d2
)
4249 { return __d1
.param() == __d2
.param(); }
4252 * @brief Return true if two exponential distributions have different
4255 template<typename _RealType
>
4257 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4258 const std::exponential_distribution
<_RealType
>& __d2
)
4259 { return !(__d1
== __d2
); }
4262 * @brief Inserts a %exponential_distribution random number distribution
4263 * @p __x into the output stream @p __os.
4265 * @param __os An output stream.
4266 * @param __x A %exponential_distribution random number distribution.
4268 * @returns The output stream with the state of @p __x inserted or in
4271 template<typename _RealType
, typename _CharT
, typename _Traits
>
4272 std::basic_ostream
<_CharT
, _Traits
>&
4273 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4274 const std::exponential_distribution
<_RealType
>&);
4277 * @brief Extracts a %exponential_distribution random number distribution
4278 * @p __x from the input stream @p __is.
4280 * @param __is An input stream.
4281 * @param __x A %exponential_distribution random number
4284 * @returns The input stream with @p __x extracted or in an error state.
4286 template<typename _RealType
, typename _CharT
, typename _Traits
>
4287 std::basic_istream
<_CharT
, _Traits
>&
4288 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4289 std::exponential_distribution
<_RealType
>&);
4293 * @brief A weibull_distribution random number distribution.
4295 * The formula for the normal probability density function is:
4297 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4298 * \exp{(-(\frac{x}{\beta})^\alpha)}
4301 template<typename _RealType
= double>
4302 class weibull_distribution
4304 static_assert(std::is_floating_point
<_RealType
>::value
,
4305 "template argument not a floating point type");
4308 /** The type of the range of the distribution. */
4309 typedef _RealType result_type
;
4310 /** Parameter type. */
4313 typedef weibull_distribution
<_RealType
> distribution_type
;
4316 param_type(_RealType __a
= _RealType(1),
4317 _RealType __b
= _RealType(1))
4318 : _M_a(__a
), _M_b(__b
)
4330 operator==(const param_type
& __p1
, const param_type
& __p2
)
4331 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4339 weibull_distribution(_RealType __a
= _RealType(1),
4340 _RealType __b
= _RealType(1))
4341 : _M_param(__a
, __b
)
4345 weibull_distribution(const param_type
& __p
)
4350 * @brief Resets the distribution state.
4357 * @brief Return the @f$a@f$ parameter of the distribution.
4361 { return _M_param
.a(); }
4364 * @brief Return the @f$b@f$ parameter of the distribution.
4368 { return _M_param
.b(); }
4371 * @brief Returns the parameter set of the distribution.
4375 { return _M_param
; }
4378 * @brief Sets the parameter set of the distribution.
4379 * @param __param The new parameter set of the distribution.
4382 param(const param_type
& __param
)
4383 { _M_param
= __param
; }
4386 * @brief Returns the greatest lower bound value of the distribution.
4390 { return result_type(0); }
4393 * @brief Returns the least upper bound value of the distribution.
4397 { return std::numeric_limits
<result_type
>::max(); }
4399 template<typename _UniformRandomNumberGenerator
>
4401 operator()(_UniformRandomNumberGenerator
& __urng
)
4402 { return this->operator()(__urng
, this->param()); }
4404 template<typename _UniformRandomNumberGenerator
>
4406 operator()(_UniformRandomNumberGenerator
& __urng
,
4407 const param_type
& __p
);
4410 param_type _M_param
;
4414 * @brief Return true if two Weibull distributions have the same
4417 template<typename _RealType
>
4419 operator==(const std::weibull_distribution
<_RealType
>& __d1
,
4420 const std::weibull_distribution
<_RealType
>& __d2
)
4421 { return __d1
.param() == __d2
.param(); }
4424 * @brief Return true if two Weibull distributions have different
4427 template<typename _RealType
>
4429 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
4430 const std::weibull_distribution
<_RealType
>& __d2
)
4431 { return !(__d1
== __d2
); }
4434 * @brief Inserts a %weibull_distribution random number distribution
4435 * @p __x into the output stream @p __os.
4437 * @param __os An output stream.
4438 * @param __x A %weibull_distribution random number distribution.
4440 * @returns The output stream with the state of @p __x inserted or in
4443 template<typename _RealType
, typename _CharT
, typename _Traits
>
4444 std::basic_ostream
<_CharT
, _Traits
>&
4445 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4446 const std::weibull_distribution
<_RealType
>&);
4449 * @brief Extracts a %weibull_distribution random number distribution
4450 * @p __x from the input stream @p __is.
4452 * @param __is An input stream.
4453 * @param __x A %weibull_distribution random number
4456 * @returns The input stream with @p __x extracted or in an error state.
4458 template<typename _RealType
, typename _CharT
, typename _Traits
>
4459 std::basic_istream
<_CharT
, _Traits
>&
4460 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4461 std::weibull_distribution
<_RealType
>&);
4465 * @brief A extreme_value_distribution random number distribution.
4467 * The formula for the normal probability mass function is
4469 * p(x|a,b) = \frac{1}{b}
4470 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
4473 template<typename _RealType
= double>
4474 class extreme_value_distribution
4476 static_assert(std::is_floating_point
<_RealType
>::value
,
4477 "template argument not a floating point type");
4480 /** The type of the range of the distribution. */
4481 typedef _RealType result_type
;
4482 /** Parameter type. */
4485 typedef extreme_value_distribution
<_RealType
> distribution_type
;
4488 param_type(_RealType __a
= _RealType(0),
4489 _RealType __b
= _RealType(1))
4490 : _M_a(__a
), _M_b(__b
)
4502 operator==(const param_type
& __p1
, const param_type
& __p2
)
4503 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4511 extreme_value_distribution(_RealType __a
= _RealType(0),
4512 _RealType __b
= _RealType(1))
4513 : _M_param(__a
, __b
)
4517 extreme_value_distribution(const param_type
& __p
)
4522 * @brief Resets the distribution state.
4529 * @brief Return the @f$a@f$ parameter of the distribution.
4533 { return _M_param
.a(); }
4536 * @brief Return the @f$b@f$ parameter of the distribution.
4540 { return _M_param
.b(); }
4543 * @brief Returns the parameter set of the distribution.
4547 { return _M_param
; }
4550 * @brief Sets the parameter set of the distribution.
4551 * @param __param The new parameter set of the distribution.
4554 param(const param_type
& __param
)
4555 { _M_param
= __param
; }
4558 * @brief Returns the greatest lower bound value of the distribution.
4562 { return std::numeric_limits
<result_type
>::min(); }
4565 * @brief Returns the least upper bound value of the distribution.
4569 { return std::numeric_limits
<result_type
>::max(); }
4571 template<typename _UniformRandomNumberGenerator
>
4573 operator()(_UniformRandomNumberGenerator
& __urng
)
4574 { return this->operator()(__urng
, this->param()); }
4576 template<typename _UniformRandomNumberGenerator
>
4578 operator()(_UniformRandomNumberGenerator
& __urng
,
4579 const param_type
& __p
);
4582 param_type _M_param
;
4586 * @brief Return true if two extreme value distributions have the same
4589 template<typename _RealType
>
4591 operator==(const std::extreme_value_distribution
<_RealType
>& __d1
,
4592 const std::extreme_value_distribution
<_RealType
>& __d2
)
4593 { return __d1
.param() == __d2
.param(); }
4596 * @brief Return true if two extreme value distributions have different
4599 template<typename _RealType
>
4601 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
4602 const std::extreme_value_distribution
<_RealType
>& __d2
)
4603 { return !(__d1
== __d2
); }
4606 * @brief Inserts a %extreme_value_distribution random number distribution
4607 * @p __x into the output stream @p __os.
4609 * @param __os An output stream.
4610 * @param __x A %extreme_value_distribution random number distribution.
4612 * @returns The output stream with the state of @p __x inserted or in
4615 template<typename _RealType
, typename _CharT
, typename _Traits
>
4616 std::basic_ostream
<_CharT
, _Traits
>&
4617 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4618 const std::extreme_value_distribution
<_RealType
>&);
4621 * @brief Extracts a %extreme_value_distribution random number
4622 * distribution @p __x from the input stream @p __is.
4624 * @param __is An input stream.
4625 * @param __x A %extreme_value_distribution random number
4628 * @returns The input stream with @p __x extracted or in an error state.
4630 template<typename _RealType
, typename _CharT
, typename _Traits
>
4631 std::basic_istream
<_CharT
, _Traits
>&
4632 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4633 std::extreme_value_distribution
<_RealType
>&);
4637 * @brief A discrete_distribution random number distribution.
4639 * The formula for the discrete probability mass function is
4642 template<typename _IntType
= int>
4643 class discrete_distribution
4645 static_assert(std::is_integral
<_IntType
>::value
,
4646 "template argument not an integral type");
4649 /** The type of the range of the distribution. */
4650 typedef _IntType result_type
;
4651 /** Parameter type. */
4654 typedef discrete_distribution
<_IntType
> distribution_type
;
4655 friend class discrete_distribution
<_IntType
>;
4658 : _M_prob(), _M_cp()
4659 { _M_initialize(); }
4661 template<typename _InputIterator
>
4662 param_type(_InputIterator __wbegin
,
4663 _InputIterator __wend
)
4664 : _M_prob(__wbegin
, __wend
), _M_cp()
4665 { _M_initialize(); }
4667 param_type(initializer_list
<double> __wil
)
4668 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4669 { _M_initialize(); }
4671 template<typename _Func
>
4672 param_type(size_t __nw
, double __xmin
, double __xmax
,
4676 probabilities() const
4680 operator==(const param_type
& __p1
, const param_type
& __p2
)
4681 { return __p1
._M_prob
== __p2
._M_prob
; }
4687 std::vector
<double> _M_prob
;
4688 std::vector
<double> _M_cp
;
4691 discrete_distribution()
4695 template<typename _InputIterator
>
4696 discrete_distribution(_InputIterator __wbegin
,
4697 _InputIterator __wend
)
4698 : _M_param(__wbegin
, __wend
)
4701 discrete_distribution(initializer_list
<double> __wl
)
4705 template<typename _Func
>
4706 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4708 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4712 discrete_distribution(const param_type
& __p
)
4717 * @brief Resets the distribution state.
4724 * @brief Returns the probabilities of the distribution.
4727 probabilities() const
4728 { return _M_param
.probabilities(); }
4731 * @brief Returns the parameter set of the distribution.
4735 { return _M_param
; }
4738 * @brief Sets the parameter set of the distribution.
4739 * @param __param The new parameter set of the distribution.
4742 param(const param_type
& __param
)
4743 { _M_param
= __param
; }
4746 * @brief Returns the greatest lower bound value of the distribution.
4750 { return result_type(0); }
4753 * @brief Returns the least upper bound value of the distribution.
4757 { return this->_M_param
._M_prob
.size() - 1; }
4759 template<typename _UniformRandomNumberGenerator
>
4761 operator()(_UniformRandomNumberGenerator
& __urng
)
4762 { return this->operator()(__urng
, this->param()); }
4764 template<typename _UniformRandomNumberGenerator
>
4766 operator()(_UniformRandomNumberGenerator
& __urng
,
4767 const param_type
& __p
);
4770 * @brief Inserts a %discrete_distribution random number distribution
4771 * @p __x into the output stream @p __os.
4773 * @param __os An output stream.
4774 * @param __x A %discrete_distribution random number distribution.
4776 * @returns The output stream with the state of @p __x inserted or in
4779 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4780 friend std::basic_ostream
<_CharT
, _Traits
>&
4781 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4782 const std::discrete_distribution
<_IntType1
>&);
4785 * @brief Extracts a %discrete_distribution random number distribution
4786 * @p __x from the input stream @p __is.
4788 * @param __is An input stream.
4789 * @param __x A %discrete_distribution random number
4792 * @returns The input stream with @p __x extracted or in an error
4795 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4796 friend std::basic_istream
<_CharT
, _Traits
>&
4797 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4798 std::discrete_distribution
<_IntType1
>&);
4801 param_type _M_param
;
4805 * @brief Return true if two discrete distributions have the same
4808 template<typename _IntType
>
4810 operator==(const std::discrete_distribution
<_IntType
>& __d1
,
4811 const std::discrete_distribution
<_IntType
>& __d2
)
4812 { return __d1
.param() == __d2
.param(); }
4815 * @brief Return true if two discrete distributions have different
4818 template<typename _IntType
>
4820 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
4821 const std::discrete_distribution
<_IntType
>& __d2
)
4822 { return !(__d1
== __d2
); }
4826 * @brief A piecewise_constant_distribution random number distribution.
4828 * The formula for the piecewise constant probability mass function is
4831 template<typename _RealType
= double>
4832 class piecewise_constant_distribution
4834 static_assert(std::is_floating_point
<_RealType
>::value
,
4835 "template argument not a floating point type");
4838 /** The type of the range of the distribution. */
4839 typedef _RealType result_type
;
4840 /** Parameter type. */
4843 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4844 friend class piecewise_constant_distribution
<_RealType
>;
4847 : _M_int(), _M_den(), _M_cp()
4848 { _M_initialize(); }
4850 template<typename _InputIteratorB
, typename _InputIteratorW
>
4851 param_type(_InputIteratorB __bfirst
,
4852 _InputIteratorB __bend
,
4853 _InputIteratorW __wbegin
);
4855 template<typename _Func
>
4856 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
4858 template<typename _Func
>
4859 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4862 std::vector
<_RealType
>
4871 operator==(const param_type
& __p1
, const param_type
& __p2
)
4872 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
4878 std::vector
<_RealType
> _M_int
;
4879 std::vector
<double> _M_den
;
4880 std::vector
<double> _M_cp
;
4884 piecewise_constant_distribution()
4888 template<typename _InputIteratorB
, typename _InputIteratorW
>
4889 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4890 _InputIteratorB __bend
,
4891 _InputIteratorW __wbegin
)
4892 : _M_param(__bfirst
, __bend
, __wbegin
)
4895 template<typename _Func
>
4896 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
4898 : _M_param(__bl
, __fw
)
4901 template<typename _Func
>
4902 piecewise_constant_distribution(size_t __nw
,
4903 _RealType __xmin
, _RealType __xmax
,
4905 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4909 piecewise_constant_distribution(const param_type
& __p
)
4914 * @brief Resets the distribution state.
4921 * @brief Returns a vector of the intervals.
4923 std::vector
<_RealType
>
4925 { return _M_param
.intervals(); }
4928 * @brief Returns a vector of the probability densities.
4932 { return _M_param
.densities(); }
4935 * @brief Returns the parameter set of the distribution.
4939 { return _M_param
; }
4942 * @brief Sets the parameter set of the distribution.
4943 * @param __param The new parameter set of the distribution.
4946 param(const param_type
& __param
)
4947 { _M_param
= __param
; }
4950 * @brief Returns the greatest lower bound value of the distribution.
4954 { return this->_M_param
._M_int
.front(); }
4957 * @brief Returns the least upper bound value of the distribution.
4961 { return this->_M_param
._M_int
.back(); }
4963 template<typename _UniformRandomNumberGenerator
>
4965 operator()(_UniformRandomNumberGenerator
& __urng
)
4966 { return this->operator()(__urng
, this->param()); }
4968 template<typename _UniformRandomNumberGenerator
>
4970 operator()(_UniformRandomNumberGenerator
& __urng
,
4971 const param_type
& __p
);
4974 * @brief Inserts a %piecewise_constan_distribution random
4975 * number distribution @p __x into the output stream @p __os.
4977 * @param __os An output stream.
4978 * @param __x A %piecewise_constan_distribution random number
4981 * @returns The output stream with the state of @p __x inserted or in
4984 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4985 friend std::basic_ostream
<_CharT
, _Traits
>&
4986 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4987 const std::piecewise_constant_distribution
<_RealType1
>&);
4990 * @brief Extracts a %piecewise_constan_distribution random
4991 * number distribution @p __x from the input stream @p __is.
4993 * @param __is An input stream.
4994 * @param __x A %piecewise_constan_distribution random number
4997 * @returns The input stream with @p __x extracted or in an error
5000 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5001 friend std::basic_istream
<_CharT
, _Traits
>&
5002 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
5003 std::piecewise_constant_distribution
<_RealType1
>&);
5006 param_type _M_param
;
5010 * @brief Return true if two piecewise constant distributions have the
5013 template<typename _RealType
>
5015 operator==(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5016 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5017 { return __d1
.param() == __d2
.param(); }
5020 * @brief Return true if two piecewise constant distributions have
5021 * different parameters.
5023 template<typename _RealType
>
5025 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5026 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5027 { return !(__d1
== __d2
); }
5031 * @brief A piecewise_linear_distribution random number distribution.
5033 * The formula for the piecewise linear probability mass function is
5036 template<typename _RealType
= double>
5037 class piecewise_linear_distribution
5039 static_assert(std::is_floating_point
<_RealType
>::value
,
5040 "template argument not a floating point type");
5043 /** The type of the range of the distribution. */
5044 typedef _RealType result_type
;
5045 /** Parameter type. */
5048 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5049 friend class piecewise_linear_distribution
<_RealType
>;
5052 : _M_int(), _M_den(), _M_cp(), _M_m()
5053 { _M_initialize(); }
5055 template<typename _InputIteratorB
, typename _InputIteratorW
>
5056 param_type(_InputIteratorB __bfirst
,
5057 _InputIteratorB __bend
,
5058 _InputIteratorW __wbegin
);
5060 template<typename _Func
>
5061 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5063 template<typename _Func
>
5064 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5067 std::vector
<_RealType
>
5076 operator==(const param_type
& __p1
, const param_type
& __p2
)
5077 { return (__p1
._M_int
== __p2
._M_int
5078 && __p1
._M_den
== __p2
._M_den
); }
5084 std::vector
<_RealType
> _M_int
;
5085 std::vector
<double> _M_den
;
5086 std::vector
<double> _M_cp
;
5087 std::vector
<double> _M_m
;
5091 piecewise_linear_distribution()
5095 template<typename _InputIteratorB
, typename _InputIteratorW
>
5096 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5097 _InputIteratorB __bend
,
5098 _InputIteratorW __wbegin
)
5099 : _M_param(__bfirst
, __bend
, __wbegin
)
5102 template<typename _Func
>
5103 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5105 : _M_param(__bl
, __fw
)
5108 template<typename _Func
>
5109 piecewise_linear_distribution(size_t __nw
,
5110 _RealType __xmin
, _RealType __xmax
,
5112 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5116 piecewise_linear_distribution(const param_type
& __p
)
5121 * Resets the distribution state.
5128 * @brief Return the intervals of the distribution.
5130 std::vector
<_RealType
>
5132 { return _M_param
.intervals(); }
5135 * @brief Return a vector of the probability densities of the
5140 { return _M_param
.densities(); }
5143 * @brief Returns the parameter set of the distribution.
5147 { return _M_param
; }
5150 * @brief Sets the parameter set of the distribution.
5151 * @param __param The new parameter set of the distribution.
5154 param(const param_type
& __param
)
5155 { _M_param
= __param
; }
5158 * @brief Returns the greatest lower bound value of the distribution.
5162 { return this->_M_param
._M_int
.front(); }
5165 * @brief Returns the least upper bound value of the distribution.
5169 { return this->_M_param
._M_int
.back(); }
5171 template<typename _UniformRandomNumberGenerator
>
5173 operator()(_UniformRandomNumberGenerator
& __urng
)
5174 { return this->operator()(__urng
, this->param()); }
5176 template<typename _UniformRandomNumberGenerator
>
5178 operator()(_UniformRandomNumberGenerator
& __urng
,
5179 const param_type
& __p
);
5182 * @brief Inserts a %piecewise_linear_distribution random number
5183 * distribution @p __x into the output stream @p __os.
5185 * @param __os An output stream.
5186 * @param __x A %piecewise_linear_distribution random number
5189 * @returns The output stream with the state of @p __x inserted or in
5192 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5193 friend std::basic_ostream
<_CharT
, _Traits
>&
5194 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
5195 const std::piecewise_linear_distribution
<_RealType1
>&);
5198 * @brief Extracts a %piecewise_linear_distribution random number
5199 * distribution @p __x from the input stream @p __is.
5201 * @param __is An input stream.
5202 * @param __x A %piecewise_linear_distribution random number
5205 * @returns The input stream with @p __x extracted or in an error
5208 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5209 friend std::basic_istream
<_CharT
, _Traits
>&
5210 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
5211 std::piecewise_linear_distribution
<_RealType1
>&);
5214 param_type _M_param
;
5218 * @brief Return true if two piecewise linear distributions have the
5221 template<typename _RealType
>
5223 operator==(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5224 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5225 { return __d1
.param() == __d2
.param(); }
5228 * @brief Return true if two piecewise linear distributions have
5229 * different parameters.
5231 template<typename _RealType
>
5233 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5234 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5235 { return !(__d1
== __d2
); }
5238 /* @} */ // group random_distributions_poisson
5240 /* @} */ // group random_distributions
5243 * @addtogroup random_utilities Random Number Utilities
5249 * @brief The seed_seq class generates sequences of seeds for random
5250 * number generators.
5256 /** The type of the seed vales. */
5257 typedef uint_least32_t result_type
;
5259 /** Default constructor. */
5264 template<typename _IntType
>
5265 seed_seq(std::initializer_list
<_IntType
> il
);
5267 template<typename _InputIterator
>
5268 seed_seq(_InputIterator __begin
, _InputIterator __end
);
5270 // generating functions
5271 template<typename _RandomAccessIterator
>
5273 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
5275 // property functions
5277 { return _M_v
.size(); }
5279 template<typename OutputIterator
>
5281 param(OutputIterator __dest
) const
5282 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
5286 std::vector
<result_type
> _M_v
;
5289 /* @} */ // group random_utilities
5291 /* @} */ // group random