1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{random}
36 namespace std
_GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 // [26.4] Random number generation
43 * @defgroup random Random Number Generation
46 * A facility for generating random numbers on selected distributions.
51 * @brief A function template for converting the output of a (integral)
52 * uniform random number generator to a floatng point result in the range
55 template<typename _RealType
, size_t __bits
,
56 typename _UniformRandomNumberGenerator
>
58 generate_canonical(_UniformRandomNumberGenerator
& __g
);
60 _GLIBCXX_END_NAMESPACE_VERSION
63 * Implementation-space details.
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
69 template<typename _UIntType
, size_t __w
,
70 bool = __w
< static_cast<size_t>
71 (std::numeric_limits
<_UIntType
>::digits
)>
73 { static const _UIntType __value
= 0; };
75 template<typename _UIntType
, size_t __w
>
76 struct _Shift
<_UIntType
, __w
, true>
77 { static const _UIntType __value
= _UIntType(1) << __w
; };
79 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool>
82 // Dispatch based on modulus value to prevent divide-by-zero compile-time
83 // errors when m == 0.
84 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
87 { return _Mod
<_Tp
, __m
, __a
, __c
, __m
== 0>::__calc(__x
); }
90 * An adaptor class for converting the output of any Generator into
91 * the input for a specific Distribution.
93 template<typename _Engine
, typename _DInputType
>
98 _Adaptor(_Engine
& __g
)
103 { return _DInputType(0); }
107 { return _DInputType(1); }
110 * Converts a value generated by the adapted random number generator
111 * into a value in the input domain for the dependent random number
117 return std::generate_canonical
<_DInputType
,
118 std::numeric_limits
<_DInputType
>::digits
,
126 _GLIBCXX_END_NAMESPACE_VERSION
127 } // namespace __detail
129 _GLIBCXX_BEGIN_NAMESPACE_VERSION
132 * @addtogroup random_generators Random Number Generators
135 * These classes define objects which provide random or pseudorandom
136 * numbers, either from a discrete or a continuous interval. The
137 * random number generator supplied as a part of this library are
138 * all uniform random number generators which provide a sequence of
139 * random number uniformly distributed over their range.
141 * A number generator is a function object with an operator() that
142 * takes zero arguments and returns a number.
144 * A compliant random number generator must satisfy the following
145 * requirements. <table border=1 cellpadding=10 cellspacing=0>
146 * <caption align=top>Random Number Generator Requirements</caption>
147 * <tr><td>To be documented.</td></tr> </table>
153 * @brief A model of a linear congruential random number generator.
155 * A random number generator that produces pseudorandom numbers via
158 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
161 * The template parameter @p _UIntType must be an unsigned integral type
162 * large enough to store values up to (__m-1). If the template parameter
163 * @p __m is 0, the modulus @p __m used is
164 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
165 * parameters @p __a and @p __c must be less than @p __m.
167 * The size of the state is @f$1@f$.
169 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
170 class linear_congruential_engine
172 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
173 "substituting _UIntType not an unsigned integral type");
174 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
175 "template argument substituting __m out of bounds");
178 /** The type of the generated random value. */
179 typedef _UIntType result_type
;
181 /** The multiplier. */
182 static constexpr result_type multiplier
= __a
;
184 static constexpr result_type increment
= __c
;
186 static constexpr result_type modulus
= __m
;
187 static constexpr result_type default_seed
= 1u;
190 * @brief Constructs a %linear_congruential_engine random number
191 * generator engine with seed @p __s. The default seed value
194 * @param __s The initial seed value.
197 linear_congruential_engine(result_type __s
= default_seed
)
201 * @brief Constructs a %linear_congruential_engine random number
202 * generator engine seeded from the seed sequence @p __q.
204 * @param __q the seed sequence.
206 template<typename _Sseq
, typename
= typename
207 std::enable_if
<!std::is_same
<_Sseq
, linear_congruential_engine
>::value
>
210 linear_congruential_engine(_Sseq
& __q
)
214 * @brief Reseeds the %linear_congruential_engine random number generator
215 * engine sequence to the seed @p __s.
217 * @param __s The new seed.
220 seed(result_type __s
= default_seed
);
223 * @brief Reseeds the %linear_congruential_engine random number generator
225 * sequence using values from the seed sequence @p __q.
227 * @param __q the seed sequence.
229 template<typename _Sseq
>
230 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
234 * @brief Gets the smallest possible value in the output range.
236 * The minimum depends on the @p __c parameter: if it is zero, the
237 * minimum generated must be > 0, otherwise 0 is allowed.
239 static constexpr result_type
241 { return __c
== 0u ? 1u : 0u; }
244 * @brief Gets the largest possible value in the output range.
246 static constexpr result_type
251 * @brief Discard a sequence of random numbers.
254 discard(unsigned long long __z
)
256 for (; __z
!= 0ULL; --__z
)
261 * @brief Gets the next random number in the sequence.
266 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
271 * @brief Compares two linear congruential random number generator
272 * objects of the same type for equality.
274 * @param __lhs A linear congruential random number generator object.
275 * @param __rhs Another linear congruential random number generator
278 * @returns true if the infinite sequences of generated values
279 * would be equal, false otherwise.
282 operator==(const linear_congruential_engine
& __lhs
,
283 const linear_congruential_engine
& __rhs
)
284 { return __lhs
._M_x
== __rhs
._M_x
; }
287 * @brief Writes the textual representation of the state x(i) of x to
290 * @param __os The output stream.
291 * @param __lcr A % linear_congruential_engine random number generator.
294 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
295 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
296 friend std::basic_ostream
<_CharT
, _Traits
>&
297 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
298 const std::linear_congruential_engine
<_UIntType1
,
299 __a1
, __c1
, __m1
>& __lcr
);
302 * @brief Sets the state of the engine by reading its textual
303 * representation from @p __is.
305 * The textual representation must have been previously written using
306 * an output stream whose imbued locale and whose type's template
307 * specialization arguments _CharT and _Traits were the same as those
310 * @param __is The input stream.
311 * @param __lcr A % linear_congruential_engine random number generator.
314 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
315 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
316 friend std::basic_istream
<_CharT
, _Traits
>&
317 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
318 std::linear_congruential_engine
<_UIntType1
, __a1
,
326 * @brief Compares two linear congruential random number generator
327 * objects of the same type for inequality.
329 * @param __lhs A linear congruential random number generator object.
330 * @param __rhs Another linear congruential random number generator
333 * @returns true if the infinite sequences of generated values
334 * would be different, false otherwise.
336 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
338 operator!=(const std::linear_congruential_engine
<_UIntType
, __a
,
340 const std::linear_congruential_engine
<_UIntType
, __a
,
342 { return !(__lhs
== __rhs
); }
346 * A generalized feedback shift register discrete random number generator.
348 * This algorithm avoids multiplication and division and is designed to be
349 * friendly to a pipelined architecture. If the parameters are chosen
350 * correctly, this generator will produce numbers with a very long period and
351 * fairly good apparent entropy, although still not cryptographically strong.
353 * The best way to use this generator is with the predefined mt19937 class.
355 * This algorithm was originally invented by Makoto Matsumoto and
358 * @tparam __w Word size, the number of bits in each element of
360 * @tparam __n The degree of recursion.
361 * @tparam __m The period parameter.
362 * @tparam __r The separation point bit index.
363 * @tparam __a The last row of the twist matrix.
364 * @tparam __u The first right-shift tempering matrix parameter.
365 * @tparam __d The first right-shift tempering matrix mask.
366 * @tparam __s The first left-shift tempering matrix parameter.
367 * @tparam __b The first left-shift tempering matrix mask.
368 * @tparam __t The second left-shift tempering matrix parameter.
369 * @tparam __c The second left-shift tempering matrix mask.
370 * @tparam __l The second right-shift tempering matrix parameter.
371 * @tparam __f Initialization multiplier.
373 template<typename _UIntType
, size_t __w
,
374 size_t __n
, size_t __m
, size_t __r
,
375 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
376 _UIntType __b
, size_t __t
,
377 _UIntType __c
, size_t __l
, _UIntType __f
>
378 class mersenne_twister_engine
380 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
381 "substituting _UIntType not an unsigned integral type");
382 static_assert(1u <= __m
&& __m
<= __n
,
383 "template argument substituting __m out of bounds");
384 static_assert(__r
<= __w
, "template argument substituting "
386 static_assert(__u
<= __w
, "template argument substituting "
388 static_assert(__s
<= __w
, "template argument substituting "
390 static_assert(__t
<= __w
, "template argument substituting "
392 static_assert(__l
<= __w
, "template argument substituting "
394 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
395 "template argument substituting __w out of bound");
396 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
397 "template argument substituting __a out of bound");
398 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
399 "template argument substituting __b out of bound");
400 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
401 "template argument substituting __c out of bound");
402 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
403 "template argument substituting __d out of bound");
404 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
405 "template argument substituting __f out of bound");
408 /** The type of the generated random value. */
409 typedef _UIntType result_type
;
412 static constexpr size_t word_size
= __w
;
413 static constexpr size_t state_size
= __n
;
414 static constexpr size_t shift_size
= __m
;
415 static constexpr size_t mask_bits
= __r
;
416 static constexpr result_type xor_mask
= __a
;
417 static constexpr size_t tempering_u
= __u
;
418 static constexpr result_type tempering_d
= __d
;
419 static constexpr size_t tempering_s
= __s
;
420 static constexpr result_type tempering_b
= __b
;
421 static constexpr size_t tempering_t
= __t
;
422 static constexpr result_type tempering_c
= __c
;
423 static constexpr size_t tempering_l
= __l
;
424 static constexpr result_type initialization_multiplier
= __f
;
425 static constexpr result_type default_seed
= 5489u;
427 // constructors and member function
429 mersenne_twister_engine(result_type __sd
= default_seed
)
433 * @brief Constructs a %mersenne_twister_engine random number generator
434 * engine seeded from the seed sequence @p __q.
436 * @param __q the seed sequence.
438 template<typename _Sseq
, typename
= typename
439 std::enable_if
<!std::is_same
<_Sseq
, mersenne_twister_engine
>::value
>
442 mersenne_twister_engine(_Sseq
& __q
)
446 seed(result_type __sd
= default_seed
);
448 template<typename _Sseq
>
449 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
453 * @brief Gets the smallest possible value in the output range.
455 static constexpr result_type
460 * @brief Gets the largest possible value in the output range.
462 static constexpr result_type
464 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
467 * @brief Discard a sequence of random numbers.
470 discard(unsigned long long __z
)
472 for (; __z
!= 0ULL; --__z
)
480 * @brief Compares two % mersenne_twister_engine random number generator
481 * objects of the same type for equality.
483 * @param __lhs A % mersenne_twister_engine random number generator
485 * @param __rhs Another % mersenne_twister_engine random number
488 * @returns true if the infinite sequences of generated values
489 * would be equal, false otherwise.
492 operator==(const mersenne_twister_engine
& __lhs
,
493 const mersenne_twister_engine
& __rhs
)
494 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
497 * @brief Inserts the current state of a % mersenne_twister_engine
498 * random number generator engine @p __x into the output stream
501 * @param __os An output stream.
502 * @param __x A % mersenne_twister_engine random number generator
505 * @returns The output stream with the state of @p __x inserted or in
508 template<typename _UIntType1
,
509 size_t __w1
, size_t __n1
,
510 size_t __m1
, size_t __r1
,
511 _UIntType1 __a1
, size_t __u1
,
512 _UIntType1 __d1
, size_t __s1
,
513 _UIntType1 __b1
, size_t __t1
,
514 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
515 typename _CharT
, typename _Traits
>
516 friend std::basic_ostream
<_CharT
, _Traits
>&
517 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
518 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
519 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
523 * @brief Extracts the current state of a % mersenne_twister_engine
524 * random number generator engine @p __x from the input stream
527 * @param __is An input stream.
528 * @param __x A % mersenne_twister_engine random number generator
531 * @returns The input stream with the state of @p __x extracted or in
534 template<typename _UIntType1
,
535 size_t __w1
, size_t __n1
,
536 size_t __m1
, size_t __r1
,
537 _UIntType1 __a1
, size_t __u1
,
538 _UIntType1 __d1
, size_t __s1
,
539 _UIntType1 __b1
, size_t __t1
,
540 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
541 typename _CharT
, typename _Traits
>
542 friend std::basic_istream
<_CharT
, _Traits
>&
543 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
544 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
545 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
549 _UIntType _M_x
[state_size
];
554 * @brief Compares two % mersenne_twister_engine random number generator
555 * objects of the same type for inequality.
557 * @param __lhs A % mersenne_twister_engine random number generator
559 * @param __rhs Another % mersenne_twister_engine random number
562 * @returns true if the infinite sequences of generated values
563 * would be different, false otherwise.
565 template<typename _UIntType
, size_t __w
,
566 size_t __n
, size_t __m
, size_t __r
,
567 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
568 _UIntType __b
, size_t __t
,
569 _UIntType __c
, size_t __l
, _UIntType __f
>
571 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
572 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
573 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
574 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
575 { return !(__lhs
== __rhs
); }
579 * @brief The Marsaglia-Zaman generator.
581 * This is a model of a Generalized Fibonacci discrete random number
582 * generator, sometimes referred to as the SWC generator.
584 * A discrete random number generator that produces pseudorandom
587 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
590 * The size of the state is @f$r@f$
591 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
593 * @var _M_x The state of the generator. This is a ring buffer.
594 * @var _M_carry The carry.
595 * @var _M_p Current index of x(i - r).
597 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
598 class subtract_with_carry_engine
600 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
601 "substituting _UIntType not an unsigned integral type");
602 static_assert(0u < __s
&& __s
< __r
,
603 "template argument substituting __s out of bounds");
604 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
605 "template argument substituting __w out of bounds");
608 /** The type of the generated random value. */
609 typedef _UIntType result_type
;
612 static constexpr size_t word_size
= __w
;
613 static constexpr size_t short_lag
= __s
;
614 static constexpr size_t long_lag
= __r
;
615 static constexpr result_type default_seed
= 19780503u;
618 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
619 * random number generator.
622 subtract_with_carry_engine(result_type __sd
= default_seed
)
626 * @brief Constructs a %subtract_with_carry_engine random number engine
627 * seeded from the seed sequence @p __q.
629 * @param __q the seed sequence.
631 template<typename _Sseq
, typename
= typename
632 std::enable_if
<!std::is_same
<_Sseq
, subtract_with_carry_engine
>::value
>
635 subtract_with_carry_engine(_Sseq
& __q
)
639 * @brief Seeds the initial state @f$x_0@f$ of the random number
642 * N1688[4.19] modifies this as follows. If @p __value == 0,
643 * sets value to 19780503. In any case, with a linear
644 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
645 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
646 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
647 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
648 * set carry to 1, otherwise sets carry to 0.
651 seed(result_type __sd
= default_seed
);
654 * @brief Seeds the initial state @f$x_0@f$ of the
655 * % subtract_with_carry_engine random number generator.
657 template<typename _Sseq
>
658 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
662 * @brief Gets the inclusive minimum value of the range of random
663 * integers returned by this generator.
665 static constexpr result_type
670 * @brief Gets the inclusive maximum value of the range of random
671 * integers returned by this generator.
673 static constexpr result_type
675 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
678 * @brief Discard a sequence of random numbers.
681 discard(unsigned long long __z
)
683 for (; __z
!= 0ULL; --__z
)
688 * @brief Gets the next random number in the sequence.
694 * @brief Compares two % subtract_with_carry_engine random number
695 * generator objects of the same type for equality.
697 * @param __lhs A % subtract_with_carry_engine random number generator
699 * @param __rhs Another % subtract_with_carry_engine random number
702 * @returns true if the infinite sequences of generated values
703 * would be equal, false otherwise.
706 operator==(const subtract_with_carry_engine
& __lhs
,
707 const subtract_with_carry_engine
& __rhs
)
708 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
711 * @brief Inserts the current state of a % subtract_with_carry_engine
712 * random number generator engine @p __x into the output stream
715 * @param __os An output stream.
716 * @param __x A % subtract_with_carry_engine random number generator
719 * @returns The output stream with the state of @p __x inserted or in
722 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
723 typename _CharT
, typename _Traits
>
724 friend std::basic_ostream
<_CharT
, _Traits
>&
725 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
726 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
730 * @brief Extracts the current state of a % subtract_with_carry_engine
731 * random number generator engine @p __x from the input stream
734 * @param __is An input stream.
735 * @param __x A % subtract_with_carry_engine random number generator
738 * @returns The input stream with the state of @p __x extracted or in
741 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
742 typename _CharT
, typename _Traits
>
743 friend std::basic_istream
<_CharT
, _Traits
>&
744 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
745 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
749 _UIntType _M_x
[long_lag
];
755 * @brief Compares two % subtract_with_carry_engine random number
756 * generator objects of the same type for inequality.
758 * @param __lhs A % subtract_with_carry_engine random number generator
760 * @param __rhs Another % subtract_with_carry_engine random number
763 * @returns true if the infinite sequences of generated values
764 * would be different, false otherwise.
766 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
768 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
770 const std::subtract_with_carry_engine
<_UIntType
, __w
,
772 { return !(__lhs
== __rhs
); }
776 * Produces random numbers from some base engine by discarding blocks of
779 * 0 <= @p __r <= @p __p
781 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
782 class discard_block_engine
784 static_assert(1 <= __r
&& __r
<= __p
,
785 "template argument substituting __r out of bounds");
788 /** The type of the generated random value. */
789 typedef typename
_RandomNumberEngine::result_type result_type
;
792 static constexpr size_t block_size
= __p
;
793 static constexpr size_t used_block
= __r
;
796 * @brief Constructs a default %discard_block_engine engine.
798 * The underlying engine is default constructed as well.
800 discard_block_engine()
801 : _M_b(), _M_n(0) { }
804 * @brief Copy constructs a %discard_block_engine engine.
806 * Copies an existing base class random number generator.
807 * @param __rng An existing (base class) engine object.
810 discard_block_engine(const _RandomNumberEngine
& __rng
)
811 : _M_b(__rng
), _M_n(0) { }
814 * @brief Move constructs a %discard_block_engine engine.
816 * Copies an existing base class random number generator.
817 * @param __rng An existing (base class) engine object.
820 discard_block_engine(_RandomNumberEngine
&& __rng
)
821 : _M_b(std::move(__rng
)), _M_n(0) { }
824 * @brief Seed constructs a %discard_block_engine engine.
826 * Constructs the underlying generator engine seeded with @p __s.
827 * @param __s A seed value for the base class engine.
830 discard_block_engine(result_type __s
)
831 : _M_b(__s
), _M_n(0) { }
834 * @brief Generator construct a %discard_block_engine engine.
836 * @param __q A seed sequence.
838 template<typename _Sseq
, typename
= typename
839 std::enable_if
<!std::is_same
<_Sseq
, discard_block_engine
>::value
840 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
843 discard_block_engine(_Sseq
& __q
)
848 * @brief Reseeds the %discard_block_engine object with the default
849 * seed for the underlying base class generator engine.
859 * @brief Reseeds the %discard_block_engine object with the default
860 * seed for the underlying base class generator engine.
863 seed(result_type __s
)
870 * @brief Reseeds the %discard_block_engine object with the given seed
872 * @param __q A seed generator function.
874 template<typename _Sseq
>
883 * @brief Gets a const reference to the underlying generator engine
886 const _RandomNumberEngine
&
887 base() const noexcept
891 * @brief Gets the minimum value in the generated random number range.
893 static constexpr result_type
895 { return _RandomNumberEngine::min(); }
898 * @brief Gets the maximum value in the generated random number range.
900 static constexpr result_type
902 { return _RandomNumberEngine::max(); }
905 * @brief Discard a sequence of random numbers.
908 discard(unsigned long long __z
)
910 for (; __z
!= 0ULL; --__z
)
915 * @brief Gets the next value in the generated random number sequence.
921 * @brief Compares two %discard_block_engine random number generator
922 * objects of the same type for equality.
924 * @param __lhs A %discard_block_engine random number generator object.
925 * @param __rhs Another %discard_block_engine random number generator
928 * @returns true if the infinite sequences of generated values
929 * would be equal, false otherwise.
932 operator==(const discard_block_engine
& __lhs
,
933 const discard_block_engine
& __rhs
)
934 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
937 * @brief Inserts the current state of a %discard_block_engine random
938 * number generator engine @p __x into the output stream
941 * @param __os An output stream.
942 * @param __x A %discard_block_engine random number generator engine.
944 * @returns The output stream with the state of @p __x inserted or in
947 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
948 typename _CharT
, typename _Traits
>
949 friend std::basic_ostream
<_CharT
, _Traits
>&
950 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
951 const std::discard_block_engine
<_RandomNumberEngine1
,
955 * @brief Extracts the current state of a % subtract_with_carry_engine
956 * random number generator engine @p __x from the input stream
959 * @param __is An input stream.
960 * @param __x A %discard_block_engine random number generator engine.
962 * @returns The input stream with the state of @p __x extracted or in
965 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
966 typename _CharT
, typename _Traits
>
967 friend std::basic_istream
<_CharT
, _Traits
>&
968 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
969 std::discard_block_engine
<_RandomNumberEngine1
,
973 _RandomNumberEngine _M_b
;
978 * @brief Compares two %discard_block_engine random number generator
979 * objects of the same type for inequality.
981 * @param __lhs A %discard_block_engine random number generator object.
982 * @param __rhs Another %discard_block_engine random number generator
985 * @returns true if the infinite sequences of generated values
986 * would be different, false otherwise.
988 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
990 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
992 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
994 { return !(__lhs
== __rhs
); }
998 * Produces random numbers by combining random numbers from some base
999 * engine to produce random numbers with a specifies number of bits @p __w.
1001 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1002 class independent_bits_engine
1004 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
1005 "substituting _UIntType not an unsigned integral type");
1006 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
1007 "template argument substituting __w out of bounds");
1010 /** The type of the generated random value. */
1011 typedef _UIntType result_type
;
1014 * @brief Constructs a default %independent_bits_engine engine.
1016 * The underlying engine is default constructed as well.
1018 independent_bits_engine()
1022 * @brief Copy constructs a %independent_bits_engine engine.
1024 * Copies an existing base class random number generator.
1025 * @param __rng An existing (base class) engine object.
1028 independent_bits_engine(const _RandomNumberEngine
& __rng
)
1032 * @brief Move constructs a %independent_bits_engine engine.
1034 * Copies an existing base class random number generator.
1035 * @param __rng An existing (base class) engine object.
1038 independent_bits_engine(_RandomNumberEngine
&& __rng
)
1039 : _M_b(std::move(__rng
)) { }
1042 * @brief Seed constructs a %independent_bits_engine engine.
1044 * Constructs the underlying generator engine seeded with @p __s.
1045 * @param __s A seed value for the base class engine.
1048 independent_bits_engine(result_type __s
)
1052 * @brief Generator construct a %independent_bits_engine engine.
1054 * @param __q A seed sequence.
1056 template<typename _Sseq
, typename
= typename
1057 std::enable_if
<!std::is_same
<_Sseq
, independent_bits_engine
>::value
1058 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1061 independent_bits_engine(_Sseq
& __q
)
1066 * @brief Reseeds the %independent_bits_engine object with the default
1067 * seed for the underlying base class generator engine.
1074 * @brief Reseeds the %independent_bits_engine object with the default
1075 * seed for the underlying base class generator engine.
1078 seed(result_type __s
)
1082 * @brief Reseeds the %independent_bits_engine object with the given
1084 * @param __q A seed generator function.
1086 template<typename _Sseq
>
1092 * @brief Gets a const reference to the underlying generator engine
1095 const _RandomNumberEngine
&
1096 base() const noexcept
1100 * @brief Gets the minimum value in the generated random number range.
1102 static constexpr result_type
1107 * @brief Gets the maximum value in the generated random number range.
1109 static constexpr result_type
1111 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1114 * @brief Discard a sequence of random numbers.
1117 discard(unsigned long long __z
)
1119 for (; __z
!= 0ULL; --__z
)
1124 * @brief Gets the next value in the generated random number sequence.
1130 * @brief Compares two %independent_bits_engine random number generator
1131 * objects of the same type for equality.
1133 * @param __lhs A %independent_bits_engine random number generator
1135 * @param __rhs Another %independent_bits_engine random number generator
1138 * @returns true if the infinite sequences of generated values
1139 * would be equal, false otherwise.
1142 operator==(const independent_bits_engine
& __lhs
,
1143 const independent_bits_engine
& __rhs
)
1144 { return __lhs
._M_b
== __rhs
._M_b
; }
1147 * @brief Extracts the current state of a % subtract_with_carry_engine
1148 * random number generator engine @p __x from the input stream
1151 * @param __is An input stream.
1152 * @param __x A %independent_bits_engine random number generator
1155 * @returns The input stream with the state of @p __x extracted or in
1158 template<typename _CharT
, typename _Traits
>
1159 friend std::basic_istream
<_CharT
, _Traits
>&
1160 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1161 std::independent_bits_engine
<_RandomNumberEngine
,
1162 __w
, _UIntType
>& __x
)
1169 _RandomNumberEngine _M_b
;
1173 * @brief Compares two %independent_bits_engine random number generator
1174 * objects of the same type for inequality.
1176 * @param __lhs A %independent_bits_engine random number generator
1178 * @param __rhs Another %independent_bits_engine random number generator
1181 * @returns true if the infinite sequences of generated values
1182 * would be different, false otherwise.
1184 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1186 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1188 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1190 { return !(__lhs
== __rhs
); }
1193 * @brief Inserts the current state of a %independent_bits_engine random
1194 * number generator engine @p __x into the output stream @p __os.
1196 * @param __os An output stream.
1197 * @param __x A %independent_bits_engine random number generator engine.
1199 * @returns The output stream with the state of @p __x inserted or in
1202 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1203 typename _CharT
, typename _Traits
>
1204 std::basic_ostream
<_CharT
, _Traits
>&
1205 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1206 const std::independent_bits_engine
<_RandomNumberEngine
,
1207 __w
, _UIntType
>& __x
)
1215 * @brief Produces random numbers by combining random numbers from some
1216 * base engine to produce random numbers with a specifies number of bits
1219 template<typename _RandomNumberEngine
, size_t __k
>
1220 class shuffle_order_engine
1222 static_assert(1u <= __k
, "template argument substituting "
1223 "__k out of bound");
1226 /** The type of the generated random value. */
1227 typedef typename
_RandomNumberEngine::result_type result_type
;
1229 static constexpr size_t table_size
= __k
;
1232 * @brief Constructs a default %shuffle_order_engine engine.
1234 * The underlying engine is default constructed as well.
1236 shuffle_order_engine()
1238 { _M_initialize(); }
1241 * @brief Copy constructs a %shuffle_order_engine engine.
1243 * Copies an existing base class random number generator.
1244 * @param __rng An existing (base class) engine object.
1247 shuffle_order_engine(const _RandomNumberEngine
& __rng
)
1249 { _M_initialize(); }
1252 * @brief Move constructs a %shuffle_order_engine engine.
1254 * Copies an existing base class random number generator.
1255 * @param __rng An existing (base class) engine object.
1258 shuffle_order_engine(_RandomNumberEngine
&& __rng
)
1259 : _M_b(std::move(__rng
))
1260 { _M_initialize(); }
1263 * @brief Seed constructs a %shuffle_order_engine engine.
1265 * Constructs the underlying generator engine seeded with @p __s.
1266 * @param __s A seed value for the base class engine.
1269 shuffle_order_engine(result_type __s
)
1271 { _M_initialize(); }
1274 * @brief Generator construct a %shuffle_order_engine engine.
1276 * @param __q A seed sequence.
1278 template<typename _Sseq
, typename
= typename
1279 std::enable_if
<!std::is_same
<_Sseq
, shuffle_order_engine
>::value
1280 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1283 shuffle_order_engine(_Sseq
& __q
)
1285 { _M_initialize(); }
1288 * @brief Reseeds the %shuffle_order_engine object with the default seed
1289 for the underlying base class generator engine.
1299 * @brief Reseeds the %shuffle_order_engine object with the default seed
1300 * for the underlying base class generator engine.
1303 seed(result_type __s
)
1310 * @brief Reseeds the %shuffle_order_engine object with the given seed
1312 * @param __q A seed generator function.
1314 template<typename _Sseq
>
1323 * Gets a const reference to the underlying generator engine object.
1325 const _RandomNumberEngine
&
1326 base() const noexcept
1330 * Gets the minimum value in the generated random number range.
1332 static constexpr result_type
1334 { return _RandomNumberEngine::min(); }
1337 * Gets the maximum value in the generated random number range.
1339 static constexpr result_type
1341 { return _RandomNumberEngine::max(); }
1344 * Discard a sequence of random numbers.
1347 discard(unsigned long long __z
)
1349 for (; __z
!= 0ULL; --__z
)
1354 * Gets the next value in the generated random number sequence.
1360 * Compares two %shuffle_order_engine random number generator objects
1361 * of the same type for equality.
1363 * @param __lhs A %shuffle_order_engine random number generator object.
1364 * @param __rhs Another %shuffle_order_engine random number generator
1367 * @returns true if the infinite sequences of generated values
1368 * would be equal, false otherwise.
1371 operator==(const shuffle_order_engine
& __lhs
,
1372 const shuffle_order_engine
& __rhs
)
1373 { return __lhs
._M_b
== __rhs
._M_b
; }
1376 * @brief Inserts the current state of a %shuffle_order_engine random
1377 * number generator engine @p __x into the output stream
1380 * @param __os An output stream.
1381 * @param __x A %shuffle_order_engine random number generator engine.
1383 * @returns The output stream with the state of @p __x inserted or in
1386 template<typename _RandomNumberEngine1
, size_t __k1
,
1387 typename _CharT
, typename _Traits
>
1388 friend std::basic_ostream
<_CharT
, _Traits
>&
1389 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1390 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1394 * @brief Extracts the current state of a % subtract_with_carry_engine
1395 * random number generator engine @p __x from the input stream
1398 * @param __is An input stream.
1399 * @param __x A %shuffle_order_engine random number generator engine.
1401 * @returns The input stream with the state of @p __x extracted or in
1404 template<typename _RandomNumberEngine1
, size_t __k1
,
1405 typename _CharT
, typename _Traits
>
1406 friend std::basic_istream
<_CharT
, _Traits
>&
1407 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1408 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>& __x
);
1411 void _M_initialize()
1413 for (size_t __i
= 0; __i
< __k
; ++__i
)
1418 _RandomNumberEngine _M_b
;
1419 result_type _M_v
[__k
];
1424 * Compares two %shuffle_order_engine random number generator objects
1425 * of the same type for inequality.
1427 * @param __lhs A %shuffle_order_engine random number generator object.
1428 * @param __rhs Another %shuffle_order_engine random number generator
1431 * @returns true if the infinite sequences of generated values
1432 * would be different, false otherwise.
1434 template<typename _RandomNumberEngine
, size_t __k
>
1436 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1438 const std::shuffle_order_engine
<_RandomNumberEngine
,
1440 { return !(__lhs
== __rhs
); }
1444 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1446 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1450 * An alternative LCR (Lehmer Generator function).
1452 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1456 * The classic Mersenne Twister.
1459 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1460 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1461 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1463 typedef mersenne_twister_engine
<
1469 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1472 * An alternative Mersenne Twister.
1474 typedef mersenne_twister_engine
<
1477 0xb5026f5aa96619e9ULL
, 29,
1478 0x5555555555555555ULL
, 17,
1479 0x71d67fffeda60000ULL
, 37,
1480 0xfff7eee000000000ULL
, 43,
1481 6364136223846793005ULL> mt19937_64
;
1483 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1486 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1489 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1491 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1493 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1495 typedef minstd_rand0 default_random_engine
;
1498 * A standard interface to a platform-specific non-deterministic
1499 * random number generator (if any are available).
1504 /** The type of the generated random value. */
1505 typedef unsigned int result_type
;
1507 // constructors, destructors and member functions
1509 #ifdef _GLIBCXX_USE_RANDOM_TR1
1512 random_device(const std::string
& __token
= "/dev/urandom")
1514 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1515 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1516 std::__throw_runtime_error(__N("random_device::"
1517 "random_device(const std::string&)"));
1521 { std::fclose(_M_file
); }
1526 random_device(const std::string
& __token
= "mt19937")
1527 : _M_mt(_M_strtoul(__token
)) { }
1530 static unsigned long
1531 _M_strtoul(const std::string
& __str
)
1533 unsigned long __ret
= 5489UL;
1534 if (__str
!= "mt19937")
1536 const char* __nptr
= __str
.c_str();
1538 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1539 if (*__nptr
== '\0' || *__endptr
!= '\0')
1540 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1541 "(const std::string&)"));
1550 static constexpr result_type
1552 { return std::numeric_limits
<result_type
>::min(); }
1554 static constexpr result_type
1556 { return std::numeric_limits
<result_type
>::max(); }
1559 entropy() const noexcept
1565 #ifdef _GLIBCXX_USE_RANDOM_TR1
1567 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1575 // No copy functions.
1576 random_device(const random_device
&) = delete;
1577 void operator=(const random_device
&) = delete;
1581 #ifdef _GLIBCXX_USE_RANDOM_TR1
1588 /* @} */ // group random_generators
1591 * @addtogroup random_distributions Random Number Distributions
1597 * @addtogroup random_distributions_uniform Uniform Distributions
1598 * @ingroup random_distributions
1603 * @brief Uniform discrete distribution for random numbers.
1604 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1605 * probability throughout the range.
1607 template<typename _IntType
= int>
1608 class uniform_int_distribution
1610 static_assert(std::is_integral
<_IntType
>::value
,
1611 "template argument not an integral type");
1614 /** The type of the range of the distribution. */
1615 typedef _IntType result_type
;
1616 /** Parameter type. */
1619 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1622 param_type(_IntType __a
= 0,
1623 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1624 : _M_a(__a
), _M_b(__b
)
1626 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1638 operator==(const param_type
& __p1
, const param_type
& __p2
)
1639 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1648 * @brief Constructs a uniform distribution object.
1651 uniform_int_distribution(_IntType __a
= 0,
1652 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1653 : _M_param(__a
, __b
)
1657 uniform_int_distribution(const param_type
& __p
)
1662 * @brief Resets the distribution state.
1664 * Does nothing for the uniform integer distribution.
1671 { return _M_param
.a(); }
1675 { return _M_param
.b(); }
1678 * @brief Returns the parameter set of the distribution.
1682 { return _M_param
; }
1685 * @brief Sets the parameter set of the distribution.
1686 * @param __param The new parameter set of the distribution.
1689 param(const param_type
& __param
)
1690 { _M_param
= __param
; }
1693 * @brief Returns the inclusive lower bound of the distribution range.
1697 { return this->a(); }
1700 * @brief Returns the inclusive upper bound of the distribution range.
1704 { return this->b(); }
1707 * @brief Generating functions.
1709 template<typename _UniformRandomNumberGenerator
>
1711 operator()(_UniformRandomNumberGenerator
& __urng
)
1712 { return this->operator()(__urng
, this->param()); }
1714 template<typename _UniformRandomNumberGenerator
>
1716 operator()(_UniformRandomNumberGenerator
& __urng
,
1717 const param_type
& __p
);
1719 param_type _M_param
;
1723 * @brief Return true if two uniform integer distributions have
1724 * the same parameters.
1726 template<typename _IntType
>
1728 operator==(const std::uniform_int_distribution
<_IntType
>& __d1
,
1729 const std::uniform_int_distribution
<_IntType
>& __d2
)
1730 { return __d1
.param() == __d2
.param(); }
1733 * @brief Return true if two uniform integer distributions have
1734 * different parameters.
1736 template<typename _IntType
>
1738 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1739 const std::uniform_int_distribution
<_IntType
>& __d2
)
1740 { return !(__d1
== __d2
); }
1743 * @brief Inserts a %uniform_int_distribution random number
1744 * distribution @p __x into the output stream @p os.
1746 * @param __os An output stream.
1747 * @param __x A %uniform_int_distribution random number distribution.
1749 * @returns The output stream with the state of @p __x inserted or in
1752 template<typename _IntType
, typename _CharT
, typename _Traits
>
1753 std::basic_ostream
<_CharT
, _Traits
>&
1754 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1755 const std::uniform_int_distribution
<_IntType
>&);
1758 * @brief Extracts a %uniform_int_distribution random number distribution
1759 * @p __x from the input stream @p __is.
1761 * @param __is An input stream.
1762 * @param __x A %uniform_int_distribution random number generator engine.
1764 * @returns The input stream with @p __x extracted or in an error state.
1766 template<typename _IntType
, typename _CharT
, typename _Traits
>
1767 std::basic_istream
<_CharT
, _Traits
>&
1768 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1769 std::uniform_int_distribution
<_IntType
>&);
1773 * @brief Uniform continuous distribution for random numbers.
1775 * A continuous random distribution on the range [min, max) with equal
1776 * probability throughout the range. The URNG should be real-valued and
1777 * deliver number in the range [0, 1).
1779 template<typename _RealType
= double>
1780 class uniform_real_distribution
1782 static_assert(std::is_floating_point
<_RealType
>::value
,
1783 "template argument not a floating point type");
1786 /** The type of the range of the distribution. */
1787 typedef _RealType result_type
;
1788 /** Parameter type. */
1791 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1794 param_type(_RealType __a
= _RealType(0),
1795 _RealType __b
= _RealType(1))
1796 : _M_a(__a
), _M_b(__b
)
1798 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1810 operator==(const param_type
& __p1
, const param_type
& __p2
)
1811 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1820 * @brief Constructs a uniform_real_distribution object.
1822 * @param __a [IN] The lower bound of the distribution.
1823 * @param __b [IN] The upper bound of the distribution.
1826 uniform_real_distribution(_RealType __a
= _RealType(0),
1827 _RealType __b
= _RealType(1))
1828 : _M_param(__a
, __b
)
1832 uniform_real_distribution(const param_type
& __p
)
1837 * @brief Resets the distribution state.
1839 * Does nothing for the uniform real distribution.
1846 { return _M_param
.a(); }
1850 { return _M_param
.b(); }
1853 * @brief Returns the parameter set of the distribution.
1857 { return _M_param
; }
1860 * @brief Sets the parameter set of the distribution.
1861 * @param __param The new parameter set of the distribution.
1864 param(const param_type
& __param
)
1865 { _M_param
= __param
; }
1868 * @brief Returns the inclusive lower bound of the distribution range.
1872 { return this->a(); }
1875 * @brief Returns the inclusive upper bound of the distribution range.
1879 { return this->b(); }
1882 * @brief Generating functions.
1884 template<typename _UniformRandomNumberGenerator
>
1886 operator()(_UniformRandomNumberGenerator
& __urng
)
1887 { return this->operator()(__urng
, this->param()); }
1889 template<typename _UniformRandomNumberGenerator
>
1891 operator()(_UniformRandomNumberGenerator
& __urng
,
1892 const param_type
& __p
)
1894 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1896 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1900 param_type _M_param
;
1904 * @brief Return true if two uniform real distributions have
1905 * the same parameters.
1907 template<typename _IntType
>
1909 operator==(const std::uniform_real_distribution
<_IntType
>& __d1
,
1910 const std::uniform_real_distribution
<_IntType
>& __d2
)
1911 { return __d1
.param() == __d2
.param(); }
1914 * @brief Return true if two uniform real distributions have
1915 * different parameters.
1917 template<typename _IntType
>
1919 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1920 const std::uniform_real_distribution
<_IntType
>& __d2
)
1921 { return !(__d1
== __d2
); }
1924 * @brief Inserts a %uniform_real_distribution random number
1925 * distribution @p __x into the output stream @p __os.
1927 * @param __os An output stream.
1928 * @param __x A %uniform_real_distribution random number distribution.
1930 * @returns The output stream with the state of @p __x inserted or in
1933 template<typename _RealType
, typename _CharT
, typename _Traits
>
1934 std::basic_ostream
<_CharT
, _Traits
>&
1935 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1936 const std::uniform_real_distribution
<_RealType
>&);
1939 * @brief Extracts a %uniform_real_distribution random number distribution
1940 * @p __x from the input stream @p __is.
1942 * @param __is An input stream.
1943 * @param __x A %uniform_real_distribution random number generator engine.
1945 * @returns The input stream with @p __x extracted or in an error state.
1947 template<typename _RealType
, typename _CharT
, typename _Traits
>
1948 std::basic_istream
<_CharT
, _Traits
>&
1949 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1950 std::uniform_real_distribution
<_RealType
>&);
1952 /* @} */ // group random_distributions_uniform
1955 * @addtogroup random_distributions_normal Normal Distributions
1956 * @ingroup random_distributions
1961 * @brief A normal continuous distribution for random numbers.
1963 * The formula for the normal probability density function is
1965 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1966 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1969 template<typename _RealType
= double>
1970 class normal_distribution
1972 static_assert(std::is_floating_point
<_RealType
>::value
,
1973 "template argument not a floating point type");
1976 /** The type of the range of the distribution. */
1977 typedef _RealType result_type
;
1978 /** Parameter type. */
1981 typedef normal_distribution
<_RealType
> distribution_type
;
1984 param_type(_RealType __mean
= _RealType(0),
1985 _RealType __stddev
= _RealType(1))
1986 : _M_mean(__mean
), _M_stddev(__stddev
)
1988 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
1997 { return _M_stddev
; }
2000 operator==(const param_type
& __p1
, const param_type
& __p2
)
2001 { return (__p1
._M_mean
== __p2
._M_mean
2002 && __p1
._M_stddev
== __p2
._M_stddev
); }
2006 _RealType _M_stddev
;
2011 * Constructs a normal distribution with parameters @f$mean@f$ and
2012 * standard deviation.
2015 normal_distribution(result_type __mean
= result_type(0),
2016 result_type __stddev
= result_type(1))
2017 : _M_param(__mean
, __stddev
), _M_saved_available(false)
2021 normal_distribution(const param_type
& __p
)
2022 : _M_param(__p
), _M_saved_available(false)
2026 * @brief Resets the distribution state.
2030 { _M_saved_available
= false; }
2033 * @brief Returns the mean of the distribution.
2037 { return _M_param
.mean(); }
2040 * @brief Returns the standard deviation of the distribution.
2044 { return _M_param
.stddev(); }
2047 * @brief Returns the parameter set of the distribution.
2051 { return _M_param
; }
2054 * @brief Sets the parameter set of the distribution.
2055 * @param __param The new parameter set of the distribution.
2058 param(const param_type
& __param
)
2059 { _M_param
= __param
; }
2062 * @brief Returns the greatest lower bound value of the distribution.
2066 { return std::numeric_limits
<result_type
>::min(); }
2069 * @brief Returns the least upper bound value of the distribution.
2073 { return std::numeric_limits
<result_type
>::max(); }
2076 * @brief Generating functions.
2078 template<typename _UniformRandomNumberGenerator
>
2080 operator()(_UniformRandomNumberGenerator
& __urng
)
2081 { return this->operator()(__urng
, this->param()); }
2083 template<typename _UniformRandomNumberGenerator
>
2085 operator()(_UniformRandomNumberGenerator
& __urng
,
2086 const param_type
& __p
);
2089 * @brief Return true if two normal distributions have
2090 * the same parameters and the sequences that would
2091 * be generated are equal.
2093 template<typename _RealType1
>
2095 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2096 const std::normal_distribution
<_RealType1
>& __d2
);
2099 * @brief Inserts a %normal_distribution random number distribution
2100 * @p __x into the output stream @p __os.
2102 * @param __os An output stream.
2103 * @param __x A %normal_distribution random number distribution.
2105 * @returns The output stream with the state of @p __x inserted or in
2108 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2109 friend std::basic_ostream
<_CharT
, _Traits
>&
2110 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2111 const std::normal_distribution
<_RealType1
>& __x
);
2114 * @brief Extracts a %normal_distribution random number distribution
2115 * @p __x from the input stream @p __is.
2117 * @param __is An input stream.
2118 * @param __x A %normal_distribution random number generator engine.
2120 * @returns The input stream with @p __x extracted or in an error
2123 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2124 friend std::basic_istream
<_CharT
, _Traits
>&
2125 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2126 std::normal_distribution
<_RealType1
>& __x
);
2129 param_type _M_param
;
2130 result_type _M_saved
;
2131 bool _M_saved_available
;
2135 * @brief Return true if two normal distributions are different.
2137 template<typename _RealType
>
2139 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2140 const std::normal_distribution
<_RealType
>& __d2
)
2141 { return !(__d1
== __d2
); }
2145 * @brief A lognormal_distribution random number distribution.
2147 * The formula for the normal probability mass function is
2149 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2150 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2153 template<typename _RealType
= double>
2154 class lognormal_distribution
2156 static_assert(std::is_floating_point
<_RealType
>::value
,
2157 "template argument not a floating point type");
2160 /** The type of the range of the distribution. */
2161 typedef _RealType result_type
;
2162 /** Parameter type. */
2165 typedef lognormal_distribution
<_RealType
> distribution_type
;
2168 param_type(_RealType __m
= _RealType(0),
2169 _RealType __s
= _RealType(1))
2170 : _M_m(__m
), _M_s(__s
)
2182 operator==(const param_type
& __p1
, const param_type
& __p2
)
2183 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2191 lognormal_distribution(_RealType __m
= _RealType(0),
2192 _RealType __s
= _RealType(1))
2193 : _M_param(__m
, __s
), _M_nd()
2197 lognormal_distribution(const param_type
& __p
)
2198 : _M_param(__p
), _M_nd()
2202 * Resets the distribution state.
2213 { return _M_param
.m(); }
2217 { return _M_param
.s(); }
2220 * @brief Returns the parameter set of the distribution.
2224 { return _M_param
; }
2227 * @brief Sets the parameter set of the distribution.
2228 * @param __param The new parameter set of the distribution.
2231 param(const param_type
& __param
)
2232 { _M_param
= __param
; }
2235 * @brief Returns the greatest lower bound value of the distribution.
2239 { return result_type(0); }
2242 * @brief Returns the least upper bound value of the distribution.
2246 { return std::numeric_limits
<result_type
>::max(); }
2249 * @brief Generating functions.
2251 template<typename _UniformRandomNumberGenerator
>
2253 operator()(_UniformRandomNumberGenerator
& __urng
)
2254 { return this->operator()(__urng
, this->param()); }
2256 template<typename _UniformRandomNumberGenerator
>
2258 operator()(_UniformRandomNumberGenerator
& __urng
,
2259 const param_type
& __p
)
2260 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2263 * @brief Return true if two lognormal distributions have
2264 * the same parameters and the sequences that would
2265 * be generated are equal.
2267 template<typename _RealType1
>
2269 operator==(const std::lognormal_distribution
<_RealType1
>& __d1
,
2270 const std::lognormal_distribution
<_RealType1
>& __d2
)
2271 { return (__d1
.param() == __d2
.param()
2272 && __d1
._M_nd
== __d2
._M_nd
); }
2275 * @brief Inserts a %lognormal_distribution random number distribution
2276 * @p __x into the output stream @p __os.
2278 * @param __os An output stream.
2279 * @param __x A %lognormal_distribution random number distribution.
2281 * @returns The output stream with the state of @p __x inserted or in
2284 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2285 friend std::basic_ostream
<_CharT
, _Traits
>&
2286 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2287 const std::lognormal_distribution
<_RealType1
>& __x
);
2290 * @brief Extracts a %lognormal_distribution random number distribution
2291 * @p __x from the input stream @p __is.
2293 * @param __is An input stream.
2294 * @param __x A %lognormal_distribution random number
2297 * @returns The input stream with @p __x extracted or in an error state.
2299 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2300 friend std::basic_istream
<_CharT
, _Traits
>&
2301 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2302 std::lognormal_distribution
<_RealType1
>& __x
);
2305 param_type _M_param
;
2307 std::normal_distribution
<result_type
> _M_nd
;
2311 * @brief Return true if two lognormal distributions are different.
2313 template<typename _RealType
>
2315 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2316 const std::lognormal_distribution
<_RealType
>& __d2
)
2317 { return !(__d1
== __d2
); }
2321 * @brief A gamma continuous distribution for random numbers.
2323 * The formula for the gamma probability density function is:
2325 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2326 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2329 template<typename _RealType
= double>
2330 class gamma_distribution
2332 static_assert(std::is_floating_point
<_RealType
>::value
,
2333 "template argument not a floating point type");
2336 /** The type of the range of the distribution. */
2337 typedef _RealType result_type
;
2338 /** Parameter type. */
2341 typedef gamma_distribution
<_RealType
> distribution_type
;
2342 friend class gamma_distribution
<_RealType
>;
2345 param_type(_RealType __alpha_val
= _RealType(1),
2346 _RealType __beta_val
= _RealType(1))
2347 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2349 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2355 { return _M_alpha
; }
2362 operator==(const param_type
& __p1
, const param_type
& __p2
)
2363 { return (__p1
._M_alpha
== __p2
._M_alpha
2364 && __p1
._M_beta
== __p2
._M_beta
); }
2373 _RealType _M_malpha
, _M_a2
;
2378 * @brief Constructs a gamma distribution with parameters
2379 * @f$\alpha@f$ and @f$\beta@f$.
2382 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2383 _RealType __beta_val
= _RealType(1))
2384 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2388 gamma_distribution(const param_type
& __p
)
2389 : _M_param(__p
), _M_nd()
2393 * @brief Resets the distribution state.
2400 * @brief Returns the @f$\alpha@f$ of the distribution.
2404 { return _M_param
.alpha(); }
2407 * @brief Returns the @f$\beta@f$ of the distribution.
2411 { return _M_param
.beta(); }
2414 * @brief Returns the parameter set of the distribution.
2418 { return _M_param
; }
2421 * @brief Sets the parameter set of the distribution.
2422 * @param __param The new parameter set of the distribution.
2425 param(const param_type
& __param
)
2426 { _M_param
= __param
; }
2429 * @brief Returns the greatest lower bound value of the distribution.
2433 { return result_type(0); }
2436 * @brief Returns the least upper bound value of the distribution.
2440 { return std::numeric_limits
<result_type
>::max(); }
2443 * @brief Generating functions.
2445 template<typename _UniformRandomNumberGenerator
>
2447 operator()(_UniformRandomNumberGenerator
& __urng
)
2448 { return this->operator()(__urng
, this->param()); }
2450 template<typename _UniformRandomNumberGenerator
>
2452 operator()(_UniformRandomNumberGenerator
& __urng
,
2453 const param_type
& __p
);
2456 * @brief Return true if two gamma distributions have the same
2457 * parameters and the sequences that would be generated
2460 template<typename _RealType1
>
2462 operator==(const std::gamma_distribution
<_RealType1
>& __d1
,
2463 const std::gamma_distribution
<_RealType1
>& __d2
)
2464 { return (__d1
.param() == __d2
.param()
2465 && __d1
._M_nd
== __d2
._M_nd
); }
2468 * @brief Inserts a %gamma_distribution random number distribution
2469 * @p __x into the output stream @p __os.
2471 * @param __os An output stream.
2472 * @param __x A %gamma_distribution random number distribution.
2474 * @returns The output stream with the state of @p __x inserted or in
2477 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2478 friend std::basic_ostream
<_CharT
, _Traits
>&
2479 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2480 const std::gamma_distribution
<_RealType1
>& __x
);
2483 * @brief Extracts a %gamma_distribution random number distribution
2484 * @p __x from the input stream @p __is.
2486 * @param __is An input stream.
2487 * @param __x A %gamma_distribution random number generator engine.
2489 * @returns The input stream with @p __x extracted or in an error state.
2491 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2492 friend std::basic_istream
<_CharT
, _Traits
>&
2493 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2494 std::gamma_distribution
<_RealType1
>& __x
);
2497 param_type _M_param
;
2499 std::normal_distribution
<result_type
> _M_nd
;
2503 * @brief Return true if two gamma distributions are different.
2505 template<typename _RealType
>
2507 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2508 const std::gamma_distribution
<_RealType
>& __d2
)
2509 { return !(__d1
== __d2
); }
2513 * @brief A chi_squared_distribution random number distribution.
2515 * The formula for the normal probability mass function is
2516 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2518 template<typename _RealType
= double>
2519 class chi_squared_distribution
2521 static_assert(std::is_floating_point
<_RealType
>::value
,
2522 "template argument not a floating point type");
2525 /** The type of the range of the distribution. */
2526 typedef _RealType result_type
;
2527 /** Parameter type. */
2530 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2533 param_type(_RealType __n
= _RealType(1))
2542 operator==(const param_type
& __p1
, const param_type
& __p2
)
2543 { return __p1
._M_n
== __p2
._M_n
; }
2550 chi_squared_distribution(_RealType __n
= _RealType(1))
2551 : _M_param(__n
), _M_gd(__n
/ 2)
2555 chi_squared_distribution(const param_type
& __p
)
2556 : _M_param(__p
), _M_gd(__p
.n() / 2)
2560 * @brief Resets the distribution state.
2571 { return _M_param
.n(); }
2574 * @brief Returns the parameter set of the distribution.
2578 { return _M_param
; }
2581 * @brief Sets the parameter set of the distribution.
2582 * @param __param The new parameter set of the distribution.
2585 param(const param_type
& __param
)
2586 { _M_param
= __param
; }
2589 * @brief Returns the greatest lower bound value of the distribution.
2593 { return result_type(0); }
2596 * @brief Returns the least upper bound value of the distribution.
2600 { return std::numeric_limits
<result_type
>::max(); }
2603 * @brief Generating functions.
2605 template<typename _UniformRandomNumberGenerator
>
2607 operator()(_UniformRandomNumberGenerator
& __urng
)
2608 { return 2 * _M_gd(__urng
); }
2610 template<typename _UniformRandomNumberGenerator
>
2612 operator()(_UniformRandomNumberGenerator
& __urng
,
2613 const param_type
& __p
)
2615 typedef typename
std::gamma_distribution
<result_type
>::param_type
2617 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2621 * @brief Return true if two Chi-squared distributions have
2622 * the same parameters and the sequences that would be
2623 * generated are equal.
2625 template<typename _RealType1
>
2627 operator==(const std::chi_squared_distribution
<_RealType1
>& __d1
,
2628 const std::chi_squared_distribution
<_RealType1
>& __d2
)
2629 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
2632 * @brief Inserts a %chi_squared_distribution random number distribution
2633 * @p __x into the output stream @p __os.
2635 * @param __os An output stream.
2636 * @param __x A %chi_squared_distribution random number distribution.
2638 * @returns The output stream with the state of @p __x inserted or in
2641 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2642 friend std::basic_ostream
<_CharT
, _Traits
>&
2643 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2644 const std::chi_squared_distribution
<_RealType1
>& __x
);
2647 * @brief Extracts a %chi_squared_distribution random number distribution
2648 * @p __x from the input stream @p __is.
2650 * @param __is An input stream.
2651 * @param __x A %chi_squared_distribution random number
2654 * @returns The input stream with @p __x extracted or in an error state.
2656 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2657 friend std::basic_istream
<_CharT
, _Traits
>&
2658 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2659 std::chi_squared_distribution
<_RealType1
>& __x
);
2662 param_type _M_param
;
2664 std::gamma_distribution
<result_type
> _M_gd
;
2668 * @brief Return true if two Chi-squared distributions are different.
2670 template<typename _RealType
>
2672 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2673 const std::chi_squared_distribution
<_RealType
>& __d2
)
2674 { return !(__d1
== __d2
); }
2678 * @brief A cauchy_distribution random number distribution.
2680 * The formula for the normal probability mass function is
2681 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2683 template<typename _RealType
= double>
2684 class cauchy_distribution
2686 static_assert(std::is_floating_point
<_RealType
>::value
,
2687 "template argument not a floating point type");
2690 /** The type of the range of the distribution. */
2691 typedef _RealType result_type
;
2692 /** Parameter type. */
2695 typedef cauchy_distribution
<_RealType
> distribution_type
;
2698 param_type(_RealType __a
= _RealType(0),
2699 _RealType __b
= _RealType(1))
2700 : _M_a(__a
), _M_b(__b
)
2712 operator==(const param_type
& __p1
, const param_type
& __p2
)
2713 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2721 cauchy_distribution(_RealType __a
= _RealType(0),
2722 _RealType __b
= _RealType(1))
2723 : _M_param(__a
, __b
)
2727 cauchy_distribution(const param_type
& __p
)
2732 * @brief Resets the distribution state.
2743 { return _M_param
.a(); }
2747 { return _M_param
.b(); }
2750 * @brief Returns the parameter set of the distribution.
2754 { return _M_param
; }
2757 * @brief Sets the parameter set of the distribution.
2758 * @param __param The new parameter set of the distribution.
2761 param(const param_type
& __param
)
2762 { _M_param
= __param
; }
2765 * @brief Returns the greatest lower bound value of the distribution.
2769 { return std::numeric_limits
<result_type
>::min(); }
2772 * @brief Returns the least upper bound value of the distribution.
2776 { return std::numeric_limits
<result_type
>::max(); }
2779 * @brief Generating functions.
2781 template<typename _UniformRandomNumberGenerator
>
2783 operator()(_UniformRandomNumberGenerator
& __urng
)
2784 { return this->operator()(__urng
, this->param()); }
2786 template<typename _UniformRandomNumberGenerator
>
2788 operator()(_UniformRandomNumberGenerator
& __urng
,
2789 const param_type
& __p
);
2792 param_type _M_param
;
2796 * @brief Return true if two Cauchy distributions have
2797 * the same parameters.
2799 template<typename _RealType
>
2801 operator==(const std::cauchy_distribution
<_RealType
>& __d1
,
2802 const std::cauchy_distribution
<_RealType
>& __d2
)
2803 { return __d1
.param() == __d2
.param(); }
2806 * @brief Return true if two Cauchy distributions have
2807 * different parameters.
2809 template<typename _RealType
>
2811 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
2812 const std::cauchy_distribution
<_RealType
>& __d2
)
2813 { return !(__d1
== __d2
); }
2816 * @brief Inserts a %cauchy_distribution random number distribution
2817 * @p __x into the output stream @p __os.
2819 * @param __os An output stream.
2820 * @param __x A %cauchy_distribution random number distribution.
2822 * @returns The output stream with the state of @p __x inserted or in
2825 template<typename _RealType
, typename _CharT
, typename _Traits
>
2826 std::basic_ostream
<_CharT
, _Traits
>&
2827 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2828 const std::cauchy_distribution
<_RealType
>& __x
);
2831 * @brief Extracts a %cauchy_distribution random number distribution
2832 * @p __x from the input stream @p __is.
2834 * @param __is An input stream.
2835 * @param __x A %cauchy_distribution random number
2838 * @returns The input stream with @p __x extracted or in an error state.
2840 template<typename _RealType
, typename _CharT
, typename _Traits
>
2841 std::basic_istream
<_CharT
, _Traits
>&
2842 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2843 std::cauchy_distribution
<_RealType
>& __x
);
2847 * @brief A fisher_f_distribution random number distribution.
2849 * The formula for the normal probability mass function is
2851 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2852 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2853 * (1 + \frac{mx}{n})^{-(m+n)/2}
2856 template<typename _RealType
= double>
2857 class fisher_f_distribution
2859 static_assert(std::is_floating_point
<_RealType
>::value
,
2860 "template argument not a floating point type");
2863 /** The type of the range of the distribution. */
2864 typedef _RealType result_type
;
2865 /** Parameter type. */
2868 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2871 param_type(_RealType __m
= _RealType(1),
2872 _RealType __n
= _RealType(1))
2873 : _M_m(__m
), _M_n(__n
)
2885 operator==(const param_type
& __p1
, const param_type
& __p2
)
2886 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
2894 fisher_f_distribution(_RealType __m
= _RealType(1),
2895 _RealType __n
= _RealType(1))
2896 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2900 fisher_f_distribution(const param_type
& __p
)
2901 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2905 * @brief Resets the distribution state.
2919 { return _M_param
.m(); }
2923 { return _M_param
.n(); }
2926 * @brief Returns the parameter set of the distribution.
2930 { return _M_param
; }
2933 * @brief Sets the parameter set of the distribution.
2934 * @param __param The new parameter set of the distribution.
2937 param(const param_type
& __param
)
2938 { _M_param
= __param
; }
2941 * @brief Returns the greatest lower bound value of the distribution.
2945 { return result_type(0); }
2948 * @brief Returns the least upper bound value of the distribution.
2952 { return std::numeric_limits
<result_type
>::max(); }
2955 * @brief Generating functions.
2957 template<typename _UniformRandomNumberGenerator
>
2959 operator()(_UniformRandomNumberGenerator
& __urng
)
2960 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
2962 template<typename _UniformRandomNumberGenerator
>
2964 operator()(_UniformRandomNumberGenerator
& __urng
,
2965 const param_type
& __p
)
2967 typedef typename
std::gamma_distribution
<result_type
>::param_type
2969 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
2970 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
2974 * @brief Return true if two Fisher f distributions have
2975 * the same parameters and the sequences that would
2976 * be generated are equal.
2978 template<typename _RealType1
>
2980 operator==(const std::fisher_f_distribution
<_RealType1
>& __d1
,
2981 const std::fisher_f_distribution
<_RealType1
>& __d2
)
2982 { return (__d1
.param() == __d2
.param()
2983 && __d1
._M_gd_x
== __d2
._M_gd_x
2984 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
2987 * @brief Inserts a %fisher_f_distribution random number distribution
2988 * @p __x into the output stream @p __os.
2990 * @param __os An output stream.
2991 * @param __x A %fisher_f_distribution random number distribution.
2993 * @returns The output stream with the state of @p __x inserted or in
2996 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2997 friend std::basic_ostream
<_CharT
, _Traits
>&
2998 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2999 const std::fisher_f_distribution
<_RealType1
>& __x
);
3002 * @brief Extracts a %fisher_f_distribution random number distribution
3003 * @p __x from the input stream @p __is.
3005 * @param __is An input stream.
3006 * @param __x A %fisher_f_distribution random number
3009 * @returns The input stream with @p __x extracted or in an error state.
3011 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3012 friend std::basic_istream
<_CharT
, _Traits
>&
3013 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3014 std::fisher_f_distribution
<_RealType1
>& __x
);
3017 param_type _M_param
;
3019 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3023 * @brief Return true if two Fisher f distributions are diferent.
3025 template<typename _RealType
>
3027 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3028 const std::fisher_f_distribution
<_RealType
>& __d2
)
3029 { return !(__d1
== __d2
); }
3032 * @brief A student_t_distribution random number distribution.
3034 * The formula for the normal probability mass function is:
3036 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3037 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3040 template<typename _RealType
= double>
3041 class student_t_distribution
3043 static_assert(std::is_floating_point
<_RealType
>::value
,
3044 "template argument not a floating point type");
3047 /** The type of the range of the distribution. */
3048 typedef _RealType result_type
;
3049 /** Parameter type. */
3052 typedef student_t_distribution
<_RealType
> distribution_type
;
3055 param_type(_RealType __n
= _RealType(1))
3064 operator==(const param_type
& __p1
, const param_type
& __p2
)
3065 { return __p1
._M_n
== __p2
._M_n
; }
3072 student_t_distribution(_RealType __n
= _RealType(1))
3073 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3077 student_t_distribution(const param_type
& __p
)
3078 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3082 * @brief Resets the distribution state.
3096 { return _M_param
.n(); }
3099 * @brief Returns the parameter set of the distribution.
3103 { return _M_param
; }
3106 * @brief Sets the parameter set of the distribution.
3107 * @param __param The new parameter set of the distribution.
3110 param(const param_type
& __param
)
3111 { _M_param
= __param
; }
3114 * @brief Returns the greatest lower bound value of the distribution.
3118 { return std::numeric_limits
<result_type
>::min(); }
3121 * @brief Returns the least upper bound value of the distribution.
3125 { return std::numeric_limits
<result_type
>::max(); }
3128 * @brief Generating functions.
3130 template<typename _UniformRandomNumberGenerator
>
3132 operator()(_UniformRandomNumberGenerator
& __urng
)
3133 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3135 template<typename _UniformRandomNumberGenerator
>
3137 operator()(_UniformRandomNumberGenerator
& __urng
,
3138 const param_type
& __p
)
3140 typedef typename
std::gamma_distribution
<result_type
>::param_type
3143 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3144 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3148 * @brief Return true if two Student t distributions have
3149 * the same parameters and the sequences that would
3150 * be generated are equal.
3152 template<typename _RealType1
>
3154 operator==(const std::student_t_distribution
<_RealType1
>& __d1
,
3155 const std::student_t_distribution
<_RealType1
>& __d2
)
3156 { return (__d1
.param() == __d2
.param()
3157 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3160 * @brief Inserts a %student_t_distribution random number distribution
3161 * @p __x into the output stream @p __os.
3163 * @param __os An output stream.
3164 * @param __x A %student_t_distribution random number distribution.
3166 * @returns The output stream with the state of @p __x inserted or in
3169 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3170 friend std::basic_ostream
<_CharT
, _Traits
>&
3171 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3172 const std::student_t_distribution
<_RealType1
>& __x
);
3175 * @brief Extracts a %student_t_distribution random number distribution
3176 * @p __x from the input stream @p __is.
3178 * @param __is An input stream.
3179 * @param __x A %student_t_distribution random number
3182 * @returns The input stream with @p __x extracted or in an error state.
3184 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3185 friend std::basic_istream
<_CharT
, _Traits
>&
3186 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3187 std::student_t_distribution
<_RealType1
>& __x
);
3190 param_type _M_param
;
3192 std::normal_distribution
<result_type
> _M_nd
;
3193 std::gamma_distribution
<result_type
> _M_gd
;
3197 * @brief Return true if two Student t distributions are different.
3199 template<typename _RealType
>
3201 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3202 const std::student_t_distribution
<_RealType
>& __d2
)
3203 { return !(__d1
== __d2
); }
3206 /* @} */ // group random_distributions_normal
3209 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3210 * @ingroup random_distributions
3215 * @brief A Bernoulli random number distribution.
3217 * Generates a sequence of true and false values with likelihood @f$p@f$
3218 * that true will come up and @f$(1 - p)@f$ that false will appear.
3220 class bernoulli_distribution
3223 /** The type of the range of the distribution. */
3224 typedef bool result_type
;
3225 /** Parameter type. */
3228 typedef bernoulli_distribution distribution_type
;
3231 param_type(double __p
= 0.5)
3234 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
3242 operator==(const param_type
& __p1
, const param_type
& __p2
)
3243 { return __p1
._M_p
== __p2
._M_p
; }
3251 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3253 * @param __p [IN] The likelihood of a true result being returned.
3254 * Must be in the interval @f$[0, 1]@f$.
3257 bernoulli_distribution(double __p
= 0.5)
3262 bernoulli_distribution(const param_type
& __p
)
3267 * @brief Resets the distribution state.
3269 * Does nothing for a Bernoulli distribution.
3275 * @brief Returns the @p p parameter of the distribution.
3279 { return _M_param
.p(); }
3282 * @brief Returns the parameter set of the distribution.
3286 { return _M_param
; }
3289 * @brief Sets the parameter set of the distribution.
3290 * @param __param The new parameter set of the distribution.
3293 param(const param_type
& __param
)
3294 { _M_param
= __param
; }
3297 * @brief Returns the greatest lower bound value of the distribution.
3301 { return std::numeric_limits
<result_type
>::min(); }
3304 * @brief Returns the least upper bound value of the distribution.
3308 { return std::numeric_limits
<result_type
>::max(); }
3311 * @brief Generating functions.
3313 template<typename _UniformRandomNumberGenerator
>
3315 operator()(_UniformRandomNumberGenerator
& __urng
)
3316 { return this->operator()(__urng
, this->param()); }
3318 template<typename _UniformRandomNumberGenerator
>
3320 operator()(_UniformRandomNumberGenerator
& __urng
,
3321 const param_type
& __p
)
3323 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3325 if ((__aurng() - __aurng
.min())
3326 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3332 param_type _M_param
;
3336 * @brief Return true if two Bernoulli distributions have
3337 * the same parameters.
3340 operator==(const std::bernoulli_distribution
& __d1
,
3341 const std::bernoulli_distribution
& __d2
)
3342 { return __d1
.param() == __d2
.param(); }
3345 * @brief Return true if two Bernoulli distributions have
3346 * different parameters.
3349 operator!=(const std::bernoulli_distribution
& __d1
,
3350 const std::bernoulli_distribution
& __d2
)
3351 { return !(__d1
== __d2
); }
3354 * @brief Inserts a %bernoulli_distribution random number distribution
3355 * @p __x into the output stream @p __os.
3357 * @param __os An output stream.
3358 * @param __x A %bernoulli_distribution random number distribution.
3360 * @returns The output stream with the state of @p __x inserted or in
3363 template<typename _CharT
, typename _Traits
>
3364 std::basic_ostream
<_CharT
, _Traits
>&
3365 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3366 const std::bernoulli_distribution
& __x
);
3369 * @brief Extracts a %bernoulli_distribution random number distribution
3370 * @p __x from the input stream @p __is.
3372 * @param __is An input stream.
3373 * @param __x A %bernoulli_distribution random number generator engine.
3375 * @returns The input stream with @p __x extracted or in an error state.
3377 template<typename _CharT
, typename _Traits
>
3378 std::basic_istream
<_CharT
, _Traits
>&
3379 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3380 std::bernoulli_distribution
& __x
)
3384 __x
.param(bernoulli_distribution::param_type(__p
));
3390 * @brief A discrete binomial random number distribution.
3392 * The formula for the binomial probability density function is
3393 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3394 * and @f$p@f$ are the parameters of the distribution.
3396 template<typename _IntType
= int>
3397 class binomial_distribution
3399 static_assert(std::is_integral
<_IntType
>::value
,
3400 "template argument not an integral type");
3403 /** The type of the range of the distribution. */
3404 typedef _IntType result_type
;
3405 /** Parameter type. */
3408 typedef binomial_distribution
<_IntType
> distribution_type
;
3409 friend class binomial_distribution
<_IntType
>;
3412 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3413 : _M_t(__t
), _M_p(__p
)
3415 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3430 operator==(const param_type
& __p1
, const param_type
& __p2
)
3431 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3441 #if _GLIBCXX_USE_C99_MATH_TR1
3442 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3443 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3448 // constructors and member function
3450 binomial_distribution(_IntType __t
= _IntType(1),
3452 : _M_param(__t
, __p
), _M_nd()
3456 binomial_distribution(const param_type
& __p
)
3457 : _M_param(__p
), _M_nd()
3461 * @brief Resets the distribution state.
3468 * @brief Returns the distribution @p t parameter.
3472 { return _M_param
.t(); }
3475 * @brief Returns the distribution @p p parameter.
3479 { return _M_param
.p(); }
3482 * @brief Returns the parameter set of the distribution.
3486 { return _M_param
; }
3489 * @brief Sets the parameter set of the distribution.
3490 * @param __param The new parameter set of the distribution.
3493 param(const param_type
& __param
)
3494 { _M_param
= __param
; }
3497 * @brief Returns the greatest lower bound value of the distribution.
3504 * @brief Returns the least upper bound value of the distribution.
3508 { return _M_param
.t(); }
3511 * @brief Generating functions.
3513 template<typename _UniformRandomNumberGenerator
>
3515 operator()(_UniformRandomNumberGenerator
& __urng
)
3516 { return this->operator()(__urng
, this->param()); }
3518 template<typename _UniformRandomNumberGenerator
>
3520 operator()(_UniformRandomNumberGenerator
& __urng
,
3521 const param_type
& __p
);
3524 * @brief Return true if two binomial distributions have
3525 * the same parameters and the sequences that would
3526 * be generated are equal.
3528 template<typename _IntType1
>
3530 operator==(const std::binomial_distribution
<_IntType1
>& __d1
,
3531 const std::binomial_distribution
<_IntType1
>& __d2
)
3532 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3533 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
3535 { return __d1
.param() == __d2
.param(); }
3539 * @brief Inserts a %binomial_distribution random number distribution
3540 * @p __x into the output stream @p __os.
3542 * @param __os An output stream.
3543 * @param __x A %binomial_distribution random number distribution.
3545 * @returns The output stream with the state of @p __x inserted or in
3548 template<typename _IntType1
,
3549 typename _CharT
, typename _Traits
>
3550 friend std::basic_ostream
<_CharT
, _Traits
>&
3551 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3552 const std::binomial_distribution
<_IntType1
>& __x
);
3555 * @brief Extracts a %binomial_distribution random number distribution
3556 * @p __x from the input stream @p __is.
3558 * @param __is An input stream.
3559 * @param __x A %binomial_distribution random number generator engine.
3561 * @returns The input stream with @p __x extracted or in an error
3564 template<typename _IntType1
,
3565 typename _CharT
, typename _Traits
>
3566 friend std::basic_istream
<_CharT
, _Traits
>&
3567 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3568 std::binomial_distribution
<_IntType1
>& __x
);
3571 template<typename _UniformRandomNumberGenerator
>
3573 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3575 param_type _M_param
;
3577 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3578 std::normal_distribution
<double> _M_nd
;
3582 * @brief Return true if two binomial distributions are different.
3584 template<typename _IntType
>
3586 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
3587 const std::binomial_distribution
<_IntType
>& __d2
)
3588 { return !(__d1
== __d2
); }
3592 * @brief A discrete geometric random number distribution.
3594 * The formula for the geometric probability density function is
3595 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
3598 template<typename _IntType
= int>
3599 class geometric_distribution
3601 static_assert(std::is_integral
<_IntType
>::value
,
3602 "template argument not an integral type");
3605 /** The type of the range of the distribution. */
3606 typedef _IntType result_type
;
3607 /** Parameter type. */
3610 typedef geometric_distribution
<_IntType
> distribution_type
;
3611 friend class geometric_distribution
<_IntType
>;
3614 param_type(double __p
= 0.5)
3617 _GLIBCXX_DEBUG_ASSERT((_M_p
> 0.0) && (_M_p
< 1.0));
3626 operator==(const param_type
& __p1
, const param_type
& __p2
)
3627 { return __p1
._M_p
== __p2
._M_p
; }
3632 { _M_log_1_p
= std::log(1.0 - _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(); }
3695 * @brief Generating functions.
3697 template<typename _UniformRandomNumberGenerator
>
3699 operator()(_UniformRandomNumberGenerator
& __urng
)
3700 { return this->operator()(__urng
, this->param()); }
3702 template<typename _UniformRandomNumberGenerator
>
3704 operator()(_UniformRandomNumberGenerator
& __urng
,
3705 const param_type
& __p
);
3708 param_type _M_param
;
3712 * @brief Return true if two geometric distributions have
3713 * the same parameters.
3715 template<typename _IntType
>
3717 operator==(const std::geometric_distribution
<_IntType
>& __d1
,
3718 const std::geometric_distribution
<_IntType
>& __d2
)
3719 { return __d1
.param() == __d2
.param(); }
3722 * @brief Return true if two geometric distributions have
3723 * different parameters.
3725 template<typename _IntType
>
3727 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
3728 const std::geometric_distribution
<_IntType
>& __d2
)
3729 { return !(__d1
== __d2
); }
3732 * @brief Inserts a %geometric_distribution random number distribution
3733 * @p __x into the output stream @p __os.
3735 * @param __os An output stream.
3736 * @param __x A %geometric_distribution random number distribution.
3738 * @returns The output stream with the state of @p __x inserted or in
3741 template<typename _IntType
,
3742 typename _CharT
, typename _Traits
>
3743 std::basic_ostream
<_CharT
, _Traits
>&
3744 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3745 const std::geometric_distribution
<_IntType
>& __x
);
3748 * @brief Extracts a %geometric_distribution random number distribution
3749 * @p __x from the input stream @p __is.
3751 * @param __is An input stream.
3752 * @param __x A %geometric_distribution random number generator engine.
3754 * @returns The input stream with @p __x extracted or in an error state.
3756 template<typename _IntType
,
3757 typename _CharT
, typename _Traits
>
3758 std::basic_istream
<_CharT
, _Traits
>&
3759 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3760 std::geometric_distribution
<_IntType
>& __x
);
3764 * @brief A negative_binomial_distribution random number distribution.
3766 * The formula for the negative binomial probability mass function is
3767 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3768 * and @f$p@f$ are the parameters of the distribution.
3770 template<typename _IntType
= int>
3771 class negative_binomial_distribution
3773 static_assert(std::is_integral
<_IntType
>::value
,
3774 "template argument not an integral type");
3777 /** The type of the range of the distribution. */
3778 typedef _IntType result_type
;
3779 /** Parameter type. */
3782 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3785 param_type(_IntType __k
= 1, double __p
= 0.5)
3786 : _M_k(__k
), _M_p(__p
)
3788 _GLIBCXX_DEBUG_ASSERT((_M_k
> 0) && (_M_p
> 0.0) && (_M_p
<= 1.0));
3800 operator==(const param_type
& __p1
, const param_type
& __p2
)
3801 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
3809 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3810 : _M_param(__k
, __p
), _M_gd(__k
, (1.0 - __p
) / __p
)
3814 negative_binomial_distribution(const param_type
& __p
)
3815 : _M_param(__p
), _M_gd(__p
.k(), (1.0 - __p
.p()) / __p
.p())
3819 * @brief Resets the distribution state.
3826 * @brief Return the @f$k@f$ parameter of the distribution.
3830 { return _M_param
.k(); }
3833 * @brief Return the @f$p@f$ parameter of the distribution.
3837 { return _M_param
.p(); }
3840 * @brief Returns the parameter set of the distribution.
3844 { return _M_param
; }
3847 * @brief Sets the parameter set of the distribution.
3848 * @param __param The new parameter set of the distribution.
3851 param(const param_type
& __param
)
3852 { _M_param
= __param
; }
3855 * @brief Returns the greatest lower bound value of the distribution.
3859 { return result_type(0); }
3862 * @brief Returns the least upper bound value of the distribution.
3866 { return std::numeric_limits
<result_type
>::max(); }
3869 * @brief Generating functions.
3871 template<typename _UniformRandomNumberGenerator
>
3873 operator()(_UniformRandomNumberGenerator
& __urng
);
3875 template<typename _UniformRandomNumberGenerator
>
3877 operator()(_UniformRandomNumberGenerator
& __urng
,
3878 const param_type
& __p
);
3881 * @brief Return true if two negative binomial distributions have
3882 * the same parameters and the sequences that would be
3883 * generated are equal.
3885 template<typename _IntType1
>
3887 operator==(const std::negative_binomial_distribution
<_IntType1
>& __d1
,
3888 const std::negative_binomial_distribution
<_IntType1
>& __d2
)
3889 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
3892 * @brief Inserts a %negative_binomial_distribution random
3893 * number distribution @p __x into the output stream @p __os.
3895 * @param __os An output stream.
3896 * @param __x A %negative_binomial_distribution random number
3899 * @returns The output stream with the state of @p __x inserted or in
3902 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3903 friend std::basic_ostream
<_CharT
, _Traits
>&
3904 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3905 const std::negative_binomial_distribution
<_IntType1
>& __x
);
3908 * @brief Extracts a %negative_binomial_distribution random number
3909 * distribution @p __x from the input stream @p __is.
3911 * @param __is An input stream.
3912 * @param __x A %negative_binomial_distribution random number
3915 * @returns The input stream with @p __x extracted or in an error state.
3917 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3918 friend std::basic_istream
<_CharT
, _Traits
>&
3919 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3920 std::negative_binomial_distribution
<_IntType1
>& __x
);
3923 param_type _M_param
;
3925 std::gamma_distribution
<double> _M_gd
;
3929 * @brief Return true if two negative binomial distributions are different.
3931 template<typename _IntType
>
3933 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
3934 const std::negative_binomial_distribution
<_IntType
>& __d2
)
3935 { return !(__d1
== __d2
); }
3938 /* @} */ // group random_distributions_bernoulli
3941 * @addtogroup random_distributions_poisson Poisson Distributions
3942 * @ingroup random_distributions
3947 * @brief A discrete Poisson random number distribution.
3949 * The formula for the Poisson probability density function is
3950 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
3951 * parameter of the distribution.
3953 template<typename _IntType
= int>
3954 class poisson_distribution
3956 static_assert(std::is_integral
<_IntType
>::value
,
3957 "template argument not an integral type");
3960 /** The type of the range of the distribution. */
3961 typedef _IntType result_type
;
3962 /** Parameter type. */
3965 typedef poisson_distribution
<_IntType
> distribution_type
;
3966 friend class poisson_distribution
<_IntType
>;
3969 param_type(double __mean
= 1.0)
3972 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3981 operator==(const param_type
& __p1
, const param_type
& __p2
)
3982 { return __p1
._M_mean
== __p2
._M_mean
; }
3985 // Hosts either log(mean) or the threshold of the simple method.
3992 #if _GLIBCXX_USE_C99_MATH_TR1
3993 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
3997 // constructors and member function
3999 poisson_distribution(double __mean
= 1.0)
4000 : _M_param(__mean
), _M_nd()
4004 poisson_distribution(const param_type
& __p
)
4005 : _M_param(__p
), _M_nd()
4009 * @brief Resets the distribution state.
4016 * @brief Returns the distribution parameter @p mean.
4020 { return _M_param
.mean(); }
4023 * @brief Returns the parameter set of the distribution.
4027 { return _M_param
; }
4030 * @brief Sets the parameter set of the distribution.
4031 * @param __param The new parameter set of the distribution.
4034 param(const param_type
& __param
)
4035 { _M_param
= __param
; }
4038 * @brief Returns the greatest lower bound value of the distribution.
4045 * @brief Returns the least upper bound value of the distribution.
4049 { return std::numeric_limits
<result_type
>::max(); }
4052 * @brief Generating functions.
4054 template<typename _UniformRandomNumberGenerator
>
4056 operator()(_UniformRandomNumberGenerator
& __urng
)
4057 { return this->operator()(__urng
, this->param()); }
4059 template<typename _UniformRandomNumberGenerator
>
4061 operator()(_UniformRandomNumberGenerator
& __urng
,
4062 const param_type
& __p
);
4065 * @brief Return true if two Poisson distributions have the same
4066 * parameters and the sequences that would be generated
4069 template<typename _IntType1
>
4071 operator==(const std::poisson_distribution
<_IntType1
>& __d1
,
4072 const std::poisson_distribution
<_IntType1
>& __d2
)
4073 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4074 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
4076 { return __d1
.param() == __d2
.param(); }
4080 * @brief Inserts a %poisson_distribution random number distribution
4081 * @p __x into the output stream @p __os.
4083 * @param __os An output stream.
4084 * @param __x A %poisson_distribution random number distribution.
4086 * @returns The output stream with the state of @p __x inserted or in
4089 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4090 friend std::basic_ostream
<_CharT
, _Traits
>&
4091 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4092 const std::poisson_distribution
<_IntType1
>& __x
);
4095 * @brief Extracts a %poisson_distribution random number distribution
4096 * @p __x from the input stream @p __is.
4098 * @param __is An input stream.
4099 * @param __x A %poisson_distribution random number generator engine.
4101 * @returns The input stream with @p __x extracted or in an error
4104 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4105 friend std::basic_istream
<_CharT
, _Traits
>&
4106 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4107 std::poisson_distribution
<_IntType1
>& __x
);
4110 param_type _M_param
;
4112 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4113 std::normal_distribution
<double> _M_nd
;
4117 * @brief Return true if two Poisson distributions are different.
4119 template<typename _IntType
>
4121 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4122 const std::poisson_distribution
<_IntType
>& __d2
)
4123 { return !(__d1
== __d2
); }
4127 * @brief An exponential continuous distribution for random numbers.
4129 * The formula for the exponential probability density function is
4130 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4132 * <table border=1 cellpadding=10 cellspacing=0>
4133 * <caption align=top>Distribution Statistics</caption>
4134 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4135 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4136 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4137 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4138 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4141 template<typename _RealType
= double>
4142 class exponential_distribution
4144 static_assert(std::is_floating_point
<_RealType
>::value
,
4145 "template argument not a floating point type");
4148 /** The type of the range of the distribution. */
4149 typedef _RealType result_type
;
4150 /** Parameter type. */
4153 typedef exponential_distribution
<_RealType
> distribution_type
;
4156 param_type(_RealType __lambda
= _RealType(1))
4157 : _M_lambda(__lambda
)
4159 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
4164 { return _M_lambda
; }
4167 operator==(const param_type
& __p1
, const param_type
& __p2
)
4168 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4171 _RealType _M_lambda
;
4176 * @brief Constructs an exponential distribution with inverse scale
4177 * parameter @f$\lambda@f$.
4180 exponential_distribution(const result_type
& __lambda
= result_type(1))
4181 : _M_param(__lambda
)
4185 exponential_distribution(const param_type
& __p
)
4190 * @brief Resets the distribution state.
4192 * Has no effect on exponential distributions.
4198 * @brief Returns the inverse scale parameter of the distribution.
4202 { return _M_param
.lambda(); }
4205 * @brief Returns the parameter set of the distribution.
4209 { return _M_param
; }
4212 * @brief Sets the parameter set of the distribution.
4213 * @param __param The new parameter set of the distribution.
4216 param(const param_type
& __param
)
4217 { _M_param
= __param
; }
4220 * @brief Returns the greatest lower bound value of the distribution.
4224 { return result_type(0); }
4227 * @brief Returns the least upper bound value of the distribution.
4231 { return std::numeric_limits
<result_type
>::max(); }
4234 * @brief Generating functions.
4236 template<typename _UniformRandomNumberGenerator
>
4238 operator()(_UniformRandomNumberGenerator
& __urng
)
4239 { return this->operator()(__urng
, this->param()); }
4241 template<typename _UniformRandomNumberGenerator
>
4243 operator()(_UniformRandomNumberGenerator
& __urng
,
4244 const param_type
& __p
)
4246 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4248 return -std::log(__aurng()) / __p
.lambda();
4252 param_type _M_param
;
4256 * @brief Return true if two exponential distributions have the same
4259 template<typename _RealType
>
4261 operator==(const std::exponential_distribution
<_RealType
>& __d1
,
4262 const std::exponential_distribution
<_RealType
>& __d2
)
4263 { return __d1
.param() == __d2
.param(); }
4266 * @brief Return true if two exponential distributions have different
4269 template<typename _RealType
>
4271 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4272 const std::exponential_distribution
<_RealType
>& __d2
)
4273 { return !(__d1
== __d2
); }
4276 * @brief Inserts a %exponential_distribution random number distribution
4277 * @p __x into the output stream @p __os.
4279 * @param __os An output stream.
4280 * @param __x A %exponential_distribution random number distribution.
4282 * @returns The output stream with the state of @p __x inserted or in
4285 template<typename _RealType
, typename _CharT
, typename _Traits
>
4286 std::basic_ostream
<_CharT
, _Traits
>&
4287 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4288 const std::exponential_distribution
<_RealType
>& __x
);
4291 * @brief Extracts a %exponential_distribution random number distribution
4292 * @p __x from the input stream @p __is.
4294 * @param __is An input stream.
4295 * @param __x A %exponential_distribution random number
4298 * @returns The input stream with @p __x extracted or in an error state.
4300 template<typename _RealType
, typename _CharT
, typename _Traits
>
4301 std::basic_istream
<_CharT
, _Traits
>&
4302 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4303 std::exponential_distribution
<_RealType
>& __x
);
4307 * @brief A weibull_distribution random number distribution.
4309 * The formula for the normal probability density function is:
4311 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4312 * \exp{(-(\frac{x}{\beta})^\alpha)}
4315 template<typename _RealType
= double>
4316 class weibull_distribution
4318 static_assert(std::is_floating_point
<_RealType
>::value
,
4319 "template argument not a floating point type");
4322 /** The type of the range of the distribution. */
4323 typedef _RealType result_type
;
4324 /** Parameter type. */
4327 typedef weibull_distribution
<_RealType
> distribution_type
;
4330 param_type(_RealType __a
= _RealType(1),
4331 _RealType __b
= _RealType(1))
4332 : _M_a(__a
), _M_b(__b
)
4344 operator==(const param_type
& __p1
, const param_type
& __p2
)
4345 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4353 weibull_distribution(_RealType __a
= _RealType(1),
4354 _RealType __b
= _RealType(1))
4355 : _M_param(__a
, __b
)
4359 weibull_distribution(const param_type
& __p
)
4364 * @brief Resets the distribution state.
4371 * @brief Return the @f$a@f$ parameter of the distribution.
4375 { return _M_param
.a(); }
4378 * @brief Return the @f$b@f$ parameter of the distribution.
4382 { return _M_param
.b(); }
4385 * @brief Returns the parameter set of the distribution.
4389 { return _M_param
; }
4392 * @brief Sets the parameter set of the distribution.
4393 * @param __param The new parameter set of the distribution.
4396 param(const param_type
& __param
)
4397 { _M_param
= __param
; }
4400 * @brief Returns the greatest lower bound value of the distribution.
4404 { return result_type(0); }
4407 * @brief Returns the least upper bound value of the distribution.
4411 { return std::numeric_limits
<result_type
>::max(); }
4414 * @brief Generating functions.
4416 template<typename _UniformRandomNumberGenerator
>
4418 operator()(_UniformRandomNumberGenerator
& __urng
)
4419 { return this->operator()(__urng
, this->param()); }
4421 template<typename _UniformRandomNumberGenerator
>
4423 operator()(_UniformRandomNumberGenerator
& __urng
,
4424 const param_type
& __p
);
4427 param_type _M_param
;
4431 * @brief Return true if two Weibull distributions have the same
4434 template<typename _RealType
>
4436 operator==(const std::weibull_distribution
<_RealType
>& __d1
,
4437 const std::weibull_distribution
<_RealType
>& __d2
)
4438 { return __d1
.param() == __d2
.param(); }
4441 * @brief Return true if two Weibull distributions have different
4444 template<typename _RealType
>
4446 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
4447 const std::weibull_distribution
<_RealType
>& __d2
)
4448 { return !(__d1
== __d2
); }
4451 * @brief Inserts a %weibull_distribution random number distribution
4452 * @p __x into the output stream @p __os.
4454 * @param __os An output stream.
4455 * @param __x A %weibull_distribution random number distribution.
4457 * @returns The output stream with the state of @p __x inserted or in
4460 template<typename _RealType
, typename _CharT
, typename _Traits
>
4461 std::basic_ostream
<_CharT
, _Traits
>&
4462 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4463 const std::weibull_distribution
<_RealType
>& __x
);
4466 * @brief Extracts a %weibull_distribution random number distribution
4467 * @p __x from the input stream @p __is.
4469 * @param __is An input stream.
4470 * @param __x A %weibull_distribution random number
4473 * @returns The input stream with @p __x extracted or in an error state.
4475 template<typename _RealType
, typename _CharT
, typename _Traits
>
4476 std::basic_istream
<_CharT
, _Traits
>&
4477 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4478 std::weibull_distribution
<_RealType
>& __x
);
4482 * @brief A extreme_value_distribution random number distribution.
4484 * The formula for the normal probability mass function is
4486 * p(x|a,b) = \frac{1}{b}
4487 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
4490 template<typename _RealType
= double>
4491 class extreme_value_distribution
4493 static_assert(std::is_floating_point
<_RealType
>::value
,
4494 "template argument not a floating point type");
4497 /** The type of the range of the distribution. */
4498 typedef _RealType result_type
;
4499 /** Parameter type. */
4502 typedef extreme_value_distribution
<_RealType
> distribution_type
;
4505 param_type(_RealType __a
= _RealType(0),
4506 _RealType __b
= _RealType(1))
4507 : _M_a(__a
), _M_b(__b
)
4519 operator==(const param_type
& __p1
, const param_type
& __p2
)
4520 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4528 extreme_value_distribution(_RealType __a
= _RealType(0),
4529 _RealType __b
= _RealType(1))
4530 : _M_param(__a
, __b
)
4534 extreme_value_distribution(const param_type
& __p
)
4539 * @brief Resets the distribution state.
4546 * @brief Return the @f$a@f$ parameter of the distribution.
4550 { return _M_param
.a(); }
4553 * @brief Return the @f$b@f$ parameter of the distribution.
4557 { return _M_param
.b(); }
4560 * @brief Returns the parameter set of the distribution.
4564 { return _M_param
; }
4567 * @brief Sets the parameter set of the distribution.
4568 * @param __param The new parameter set of the distribution.
4571 param(const param_type
& __param
)
4572 { _M_param
= __param
; }
4575 * @brief Returns the greatest lower bound value of the distribution.
4579 { return std::numeric_limits
<result_type
>::min(); }
4582 * @brief Returns the least upper bound value of the distribution.
4586 { return std::numeric_limits
<result_type
>::max(); }
4589 * @brief Generating functions.
4591 template<typename _UniformRandomNumberGenerator
>
4593 operator()(_UniformRandomNumberGenerator
& __urng
)
4594 { return this->operator()(__urng
, this->param()); }
4596 template<typename _UniformRandomNumberGenerator
>
4598 operator()(_UniformRandomNumberGenerator
& __urng
,
4599 const param_type
& __p
);
4602 param_type _M_param
;
4606 * @brief Return true if two extreme value distributions have the same
4609 template<typename _RealType
>
4611 operator==(const std::extreme_value_distribution
<_RealType
>& __d1
,
4612 const std::extreme_value_distribution
<_RealType
>& __d2
)
4613 { return __d1
.param() == __d2
.param(); }
4616 * @brief Return true if two extreme value distributions have different
4619 template<typename _RealType
>
4621 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
4622 const std::extreme_value_distribution
<_RealType
>& __d2
)
4623 { return !(__d1
== __d2
); }
4626 * @brief Inserts a %extreme_value_distribution random number distribution
4627 * @p __x into the output stream @p __os.
4629 * @param __os An output stream.
4630 * @param __x A %extreme_value_distribution random number distribution.
4632 * @returns The output stream with the state of @p __x inserted or in
4635 template<typename _RealType
, typename _CharT
, typename _Traits
>
4636 std::basic_ostream
<_CharT
, _Traits
>&
4637 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4638 const std::extreme_value_distribution
<_RealType
>& __x
);
4641 * @brief Extracts a %extreme_value_distribution random number
4642 * distribution @p __x from the input stream @p __is.
4644 * @param __is An input stream.
4645 * @param __x A %extreme_value_distribution random number
4648 * @returns The input stream with @p __x extracted or in an error state.
4650 template<typename _RealType
, typename _CharT
, typename _Traits
>
4651 std::basic_istream
<_CharT
, _Traits
>&
4652 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4653 std::extreme_value_distribution
<_RealType
>& __x
);
4657 * @brief A discrete_distribution random number distribution.
4659 * The formula for the discrete probability mass function is
4662 template<typename _IntType
= int>
4663 class discrete_distribution
4665 static_assert(std::is_integral
<_IntType
>::value
,
4666 "template argument not an integral type");
4669 /** The type of the range of the distribution. */
4670 typedef _IntType result_type
;
4671 /** Parameter type. */
4674 typedef discrete_distribution
<_IntType
> distribution_type
;
4675 friend class discrete_distribution
<_IntType
>;
4678 : _M_prob(), _M_cp()
4681 template<typename _InputIterator
>
4682 param_type(_InputIterator __wbegin
,
4683 _InputIterator __wend
)
4684 : _M_prob(__wbegin
, __wend
), _M_cp()
4685 { _M_initialize(); }
4687 param_type(initializer_list
<double> __wil
)
4688 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4689 { _M_initialize(); }
4691 template<typename _Func
>
4692 param_type(size_t __nw
, double __xmin
, double __xmax
,
4695 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4696 param_type(const param_type
&) = default;
4697 param_type
& operator=(const param_type
&) = default;
4700 probabilities() const
4701 { return _M_prob
.empty() ? std::vector
<double>(1, 1.0) : _M_prob
; }
4704 operator==(const param_type
& __p1
, const param_type
& __p2
)
4705 { return __p1
._M_prob
== __p2
._M_prob
; }
4711 std::vector
<double> _M_prob
;
4712 std::vector
<double> _M_cp
;
4715 discrete_distribution()
4719 template<typename _InputIterator
>
4720 discrete_distribution(_InputIterator __wbegin
,
4721 _InputIterator __wend
)
4722 : _M_param(__wbegin
, __wend
)
4725 discrete_distribution(initializer_list
<double> __wl
)
4729 template<typename _Func
>
4730 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4732 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4736 discrete_distribution(const param_type
& __p
)
4741 * @brief Resets the distribution state.
4748 * @brief Returns the probabilities of the distribution.
4751 probabilities() const
4753 return _M_param
._M_prob
.empty()
4754 ? std::vector
<double>(1, 1.0) : _M_param
._M_prob
;
4758 * @brief Returns the parameter set of the distribution.
4762 { return _M_param
; }
4765 * @brief Sets the parameter set of the distribution.
4766 * @param __param The new parameter set of the distribution.
4769 param(const param_type
& __param
)
4770 { _M_param
= __param
; }
4773 * @brief Returns the greatest lower bound value of the distribution.
4777 { return result_type(0); }
4780 * @brief Returns the least upper bound value of the distribution.
4785 return _M_param
._M_prob
.empty()
4786 ? result_type(0) : result_type(_M_param
._M_prob
.size() - 1);
4790 * @brief Generating functions.
4792 template<typename _UniformRandomNumberGenerator
>
4794 operator()(_UniformRandomNumberGenerator
& __urng
)
4795 { return this->operator()(__urng
, this->param()); }
4797 template<typename _UniformRandomNumberGenerator
>
4799 operator()(_UniformRandomNumberGenerator
& __urng
,
4800 const param_type
& __p
);
4803 * @brief Inserts a %discrete_distribution random number distribution
4804 * @p __x into the output stream @p __os.
4806 * @param __os An output stream.
4807 * @param __x A %discrete_distribution random number distribution.
4809 * @returns The output stream with the state of @p __x inserted or in
4812 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4813 friend std::basic_ostream
<_CharT
, _Traits
>&
4814 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4815 const std::discrete_distribution
<_IntType1
>& __x
);
4818 * @brief Extracts a %discrete_distribution random number distribution
4819 * @p __x from the input stream @p __is.
4821 * @param __is An input stream.
4822 * @param __x A %discrete_distribution random number
4825 * @returns The input stream with @p __x extracted or in an error
4828 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4829 friend std::basic_istream
<_CharT
, _Traits
>&
4830 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4831 std::discrete_distribution
<_IntType1
>& __x
);
4834 param_type _M_param
;
4838 * @brief Return true if two discrete distributions have the same
4841 template<typename _IntType
>
4843 operator==(const std::discrete_distribution
<_IntType
>& __d1
,
4844 const std::discrete_distribution
<_IntType
>& __d2
)
4845 { return __d1
.param() == __d2
.param(); }
4848 * @brief Return true if two discrete distributions have different
4851 template<typename _IntType
>
4853 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
4854 const std::discrete_distribution
<_IntType
>& __d2
)
4855 { return !(__d1
== __d2
); }
4859 * @brief A piecewise_constant_distribution random number distribution.
4861 * The formula for the piecewise constant probability mass function is
4864 template<typename _RealType
= double>
4865 class piecewise_constant_distribution
4867 static_assert(std::is_floating_point
<_RealType
>::value
,
4868 "template argument not a floating point type");
4871 /** The type of the range of the distribution. */
4872 typedef _RealType result_type
;
4873 /** Parameter type. */
4876 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4877 friend class piecewise_constant_distribution
<_RealType
>;
4880 : _M_int(), _M_den(), _M_cp()
4883 template<typename _InputIteratorB
, typename _InputIteratorW
>
4884 param_type(_InputIteratorB __bfirst
,
4885 _InputIteratorB __bend
,
4886 _InputIteratorW __wbegin
);
4888 template<typename _Func
>
4889 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
4891 template<typename _Func
>
4892 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4895 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4896 param_type(const param_type
&) = default;
4897 param_type
& operator=(const param_type
&) = default;
4899 std::vector
<_RealType
>
4904 std::vector
<_RealType
> __tmp(2);
4905 __tmp
[1] = _RealType(1);
4914 { return _M_den
.empty() ? std::vector
<double>(1, 1.0) : _M_den
; }
4917 operator==(const param_type
& __p1
, const param_type
& __p2
)
4918 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
4924 std::vector
<_RealType
> _M_int
;
4925 std::vector
<double> _M_den
;
4926 std::vector
<double> _M_cp
;
4930 piecewise_constant_distribution()
4934 template<typename _InputIteratorB
, typename _InputIteratorW
>
4935 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4936 _InputIteratorB __bend
,
4937 _InputIteratorW __wbegin
)
4938 : _M_param(__bfirst
, __bend
, __wbegin
)
4941 template<typename _Func
>
4942 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
4944 : _M_param(__bl
, __fw
)
4947 template<typename _Func
>
4948 piecewise_constant_distribution(size_t __nw
,
4949 _RealType __xmin
, _RealType __xmax
,
4951 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4955 piecewise_constant_distribution(const param_type
& __p
)
4960 * @brief Resets the distribution state.
4967 * @brief Returns a vector of the intervals.
4969 std::vector
<_RealType
>
4972 if (_M_param
._M_int
.empty())
4974 std::vector
<_RealType
> __tmp(2);
4975 __tmp
[1] = _RealType(1);
4979 return _M_param
._M_int
;
4983 * @brief Returns a vector of the probability densities.
4988 return _M_param
._M_den
.empty()
4989 ? std::vector
<double>(1, 1.0) : _M_param
._M_den
;
4993 * @brief Returns the parameter set of the distribution.
4997 { return _M_param
; }
5000 * @brief Sets the parameter set of the distribution.
5001 * @param __param The new parameter set of the distribution.
5004 param(const param_type
& __param
)
5005 { _M_param
= __param
; }
5008 * @brief Returns the greatest lower bound value of the distribution.
5013 return _M_param
._M_int
.empty()
5014 ? result_type(0) : _M_param
._M_int
.front();
5018 * @brief Returns the least upper bound value of the distribution.
5023 return _M_param
._M_int
.empty()
5024 ? result_type(1) : _M_param
._M_int
.back();
5028 * @brief Generating functions.
5030 template<typename _UniformRandomNumberGenerator
>
5032 operator()(_UniformRandomNumberGenerator
& __urng
)
5033 { return this->operator()(__urng
, this->param()); }
5035 template<typename _UniformRandomNumberGenerator
>
5037 operator()(_UniformRandomNumberGenerator
& __urng
,
5038 const param_type
& __p
);
5041 * @brief Inserts a %piecewise_constan_distribution random
5042 * number distribution @p __x into the output stream @p __os.
5044 * @param __os An output stream.
5045 * @param __x A %piecewise_constan_distribution random number
5048 * @returns The output stream with the state of @p __x inserted or in
5051 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5052 friend std::basic_ostream
<_CharT
, _Traits
>&
5053 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5054 const std::piecewise_constant_distribution
<_RealType1
>& __x
);
5057 * @brief Extracts a %piecewise_constan_distribution random
5058 * number distribution @p __x from the input stream @p __is.
5060 * @param __is An input stream.
5061 * @param __x A %piecewise_constan_distribution random number
5064 * @returns The input stream with @p __x extracted or in an error
5067 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5068 friend std::basic_istream
<_CharT
, _Traits
>&
5069 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5070 std::piecewise_constant_distribution
<_RealType1
>& __x
);
5073 param_type _M_param
;
5077 * @brief Return true if two piecewise constant distributions have the
5080 template<typename _RealType
>
5082 operator==(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5083 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5084 { return __d1
.param() == __d2
.param(); }
5087 * @brief Return true if two piecewise constant distributions have
5088 * different parameters.
5090 template<typename _RealType
>
5092 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5093 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5094 { return !(__d1
== __d2
); }
5098 * @brief A piecewise_linear_distribution random number distribution.
5100 * The formula for the piecewise linear probability mass function is
5103 template<typename _RealType
= double>
5104 class piecewise_linear_distribution
5106 static_assert(std::is_floating_point
<_RealType
>::value
,
5107 "template argument not a floating point type");
5110 /** The type of the range of the distribution. */
5111 typedef _RealType result_type
;
5112 /** Parameter type. */
5115 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5116 friend class piecewise_linear_distribution
<_RealType
>;
5119 : _M_int(), _M_den(), _M_cp(), _M_m()
5122 template<typename _InputIteratorB
, typename _InputIteratorW
>
5123 param_type(_InputIteratorB __bfirst
,
5124 _InputIteratorB __bend
,
5125 _InputIteratorW __wbegin
);
5127 template<typename _Func
>
5128 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5130 template<typename _Func
>
5131 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5134 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5135 param_type(const param_type
&) = default;
5136 param_type
& operator=(const param_type
&) = default;
5138 std::vector
<_RealType
>
5143 std::vector
<_RealType
> __tmp(2);
5144 __tmp
[1] = _RealType(1);
5153 { return _M_den
.empty() ? std::vector
<double>(2, 1.0) : _M_den
; }
5156 operator==(const param_type
& __p1
, const param_type
& __p2
)
5157 { return (__p1
._M_int
== __p2
._M_int
5158 && __p1
._M_den
== __p2
._M_den
); }
5164 std::vector
<_RealType
> _M_int
;
5165 std::vector
<double> _M_den
;
5166 std::vector
<double> _M_cp
;
5167 std::vector
<double> _M_m
;
5171 piecewise_linear_distribution()
5175 template<typename _InputIteratorB
, typename _InputIteratorW
>
5176 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5177 _InputIteratorB __bend
,
5178 _InputIteratorW __wbegin
)
5179 : _M_param(__bfirst
, __bend
, __wbegin
)
5182 template<typename _Func
>
5183 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5185 : _M_param(__bl
, __fw
)
5188 template<typename _Func
>
5189 piecewise_linear_distribution(size_t __nw
,
5190 _RealType __xmin
, _RealType __xmax
,
5192 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5196 piecewise_linear_distribution(const param_type
& __p
)
5201 * Resets the distribution state.
5208 * @brief Return the intervals of the distribution.
5210 std::vector
<_RealType
>
5213 if (_M_param
._M_int
.empty())
5215 std::vector
<_RealType
> __tmp(2);
5216 __tmp
[1] = _RealType(1);
5220 return _M_param
._M_int
;
5224 * @brief Return a vector of the probability densities of the
5230 return _M_param
._M_den
.empty()
5231 ? std::vector
<double>(2, 1.0) : _M_param
._M_den
;
5235 * @brief Returns the parameter set of the distribution.
5239 { return _M_param
; }
5242 * @brief Sets the parameter set of the distribution.
5243 * @param __param The new parameter set of the distribution.
5246 param(const param_type
& __param
)
5247 { _M_param
= __param
; }
5250 * @brief Returns the greatest lower bound value of the distribution.
5255 return _M_param
._M_int
.empty()
5256 ? result_type(0) : _M_param
._M_int
.front();
5260 * @brief Returns the least upper bound value of the distribution.
5265 return _M_param
._M_int
.empty()
5266 ? result_type(1) : _M_param
._M_int
.back();
5270 * @brief Generating functions.
5272 template<typename _UniformRandomNumberGenerator
>
5274 operator()(_UniformRandomNumberGenerator
& __urng
)
5275 { return this->operator()(__urng
, this->param()); }
5277 template<typename _UniformRandomNumberGenerator
>
5279 operator()(_UniformRandomNumberGenerator
& __urng
,
5280 const param_type
& __p
);
5283 * @brief Inserts a %piecewise_linear_distribution random number
5284 * distribution @p __x into the output stream @p __os.
5286 * @param __os An output stream.
5287 * @param __x A %piecewise_linear_distribution random number
5290 * @returns The output stream with the state of @p __x inserted or in
5293 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5294 friend std::basic_ostream
<_CharT
, _Traits
>&
5295 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5296 const std::piecewise_linear_distribution
<_RealType1
>& __x
);
5299 * @brief Extracts a %piecewise_linear_distribution random number
5300 * distribution @p __x from the input stream @p __is.
5302 * @param __is An input stream.
5303 * @param __x A %piecewise_linear_distribution random number
5306 * @returns The input stream with @p __x extracted or in an error
5309 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5310 friend std::basic_istream
<_CharT
, _Traits
>&
5311 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5312 std::piecewise_linear_distribution
<_RealType1
>& __x
);
5315 param_type _M_param
;
5319 * @brief Return true if two piecewise linear distributions have the
5322 template<typename _RealType
>
5324 operator==(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5325 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5326 { return __d1
.param() == __d2
.param(); }
5329 * @brief Return true if two piecewise linear distributions have
5330 * different parameters.
5332 template<typename _RealType
>
5334 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5335 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5336 { return !(__d1
== __d2
); }
5339 /* @} */ // group random_distributions_poisson
5341 /* @} */ // group random_distributions
5344 * @addtogroup random_utilities Random Number Utilities
5350 * @brief The seed_seq class generates sequences of seeds for random
5351 * number generators.
5357 /** The type of the seed vales. */
5358 typedef uint_least32_t result_type
;
5360 /** Default constructor. */
5365 template<typename _IntType
>
5366 seed_seq(std::initializer_list
<_IntType
> il
);
5368 template<typename _InputIterator
>
5369 seed_seq(_InputIterator __begin
, _InputIterator __end
);
5371 // generating functions
5372 template<typename _RandomAccessIterator
>
5374 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
5376 // property functions
5378 { return _M_v
.size(); }
5380 template<typename OutputIterator
>
5382 param(OutputIterator __dest
) const
5383 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
5387 std::vector
<result_type
> _M_v
;
5390 /* @} */ // group random_utilities
5392 /* @} */ // group random
5394 _GLIBCXX_END_NAMESPACE_VERSION