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
)
495 && __lhs
._M_p
== __rhs
._M_p
); }
498 * @brief Inserts the current state of a % mersenne_twister_engine
499 * random number generator engine @p __x into the output stream
502 * @param __os An output stream.
503 * @param __x A % mersenne_twister_engine random number generator
506 * @returns The output stream with the state of @p __x inserted or in
509 template<typename _UIntType1
,
510 size_t __w1
, size_t __n1
,
511 size_t __m1
, size_t __r1
,
512 _UIntType1 __a1
, size_t __u1
,
513 _UIntType1 __d1
, size_t __s1
,
514 _UIntType1 __b1
, size_t __t1
,
515 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
516 typename _CharT
, typename _Traits
>
517 friend std::basic_ostream
<_CharT
, _Traits
>&
518 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
519 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
520 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
524 * @brief Extracts the current state of a % mersenne_twister_engine
525 * random number generator engine @p __x from the input stream
528 * @param __is An input stream.
529 * @param __x A % mersenne_twister_engine random number generator
532 * @returns The input stream with the state of @p __x extracted or in
535 template<typename _UIntType1
,
536 size_t __w1
, size_t __n1
,
537 size_t __m1
, size_t __r1
,
538 _UIntType1 __a1
, size_t __u1
,
539 _UIntType1 __d1
, size_t __s1
,
540 _UIntType1 __b1
, size_t __t1
,
541 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
542 typename _CharT
, typename _Traits
>
543 friend std::basic_istream
<_CharT
, _Traits
>&
544 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
545 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
546 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
550 _UIntType _M_x
[state_size
];
555 * @brief Compares two % mersenne_twister_engine random number generator
556 * objects of the same type for inequality.
558 * @param __lhs A % mersenne_twister_engine random number generator
560 * @param __rhs Another % mersenne_twister_engine random number
563 * @returns true if the infinite sequences of generated values
564 * would be different, false otherwise.
566 template<typename _UIntType
, size_t __w
,
567 size_t __n
, size_t __m
, size_t __r
,
568 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
569 _UIntType __b
, size_t __t
,
570 _UIntType __c
, size_t __l
, _UIntType __f
>
572 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
573 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
574 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
575 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
576 { return !(__lhs
== __rhs
); }
580 * @brief The Marsaglia-Zaman generator.
582 * This is a model of a Generalized Fibonacci discrete random number
583 * generator, sometimes referred to as the SWC generator.
585 * A discrete random number generator that produces pseudorandom
588 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
591 * The size of the state is @f$r@f$
592 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
594 * @var _M_x The state of the generator. This is a ring buffer.
595 * @var _M_carry The carry.
596 * @var _M_p Current index of x(i - r).
598 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
599 class subtract_with_carry_engine
601 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
602 "substituting _UIntType not an unsigned integral type");
603 static_assert(0u < __s
&& __s
< __r
,
604 "template argument substituting __s out of bounds");
605 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
606 "template argument substituting __w out of bounds");
609 /** The type of the generated random value. */
610 typedef _UIntType result_type
;
613 static constexpr size_t word_size
= __w
;
614 static constexpr size_t short_lag
= __s
;
615 static constexpr size_t long_lag
= __r
;
616 static constexpr result_type default_seed
= 19780503u;
619 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
620 * random number generator.
623 subtract_with_carry_engine(result_type __sd
= default_seed
)
627 * @brief Constructs a %subtract_with_carry_engine random number engine
628 * seeded from the seed sequence @p __q.
630 * @param __q the seed sequence.
632 template<typename _Sseq
, typename
= typename
633 std::enable_if
<!std::is_same
<_Sseq
, subtract_with_carry_engine
>::value
>
636 subtract_with_carry_engine(_Sseq
& __q
)
640 * @brief Seeds the initial state @f$x_0@f$ of the random number
643 * N1688[4.19] modifies this as follows. If @p __value == 0,
644 * sets value to 19780503. In any case, with a linear
645 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
646 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
647 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
648 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
649 * set carry to 1, otherwise sets carry to 0.
652 seed(result_type __sd
= default_seed
);
655 * @brief Seeds the initial state @f$x_0@f$ of the
656 * % subtract_with_carry_engine random number generator.
658 template<typename _Sseq
>
659 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
663 * @brief Gets the inclusive minimum value of the range of random
664 * integers returned by this generator.
666 static constexpr result_type
671 * @brief Gets the inclusive maximum value of the range of random
672 * integers returned by this generator.
674 static constexpr result_type
676 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
679 * @brief Discard a sequence of random numbers.
682 discard(unsigned long long __z
)
684 for (; __z
!= 0ULL; --__z
)
689 * @brief Gets the next random number in the sequence.
695 * @brief Compares two % subtract_with_carry_engine random number
696 * generator objects of the same type for equality.
698 * @param __lhs A % subtract_with_carry_engine random number generator
700 * @param __rhs Another % subtract_with_carry_engine random number
703 * @returns true if the infinite sequences of generated values
704 * would be equal, false otherwise.
707 operator==(const subtract_with_carry_engine
& __lhs
,
708 const subtract_with_carry_engine
& __rhs
)
709 { return (std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
)
710 && __lhs
._M_carry
== __rhs
._M_carry
711 && __lhs
._M_p
== __rhs
._M_p
); }
714 * @brief Inserts the current state of a % subtract_with_carry_engine
715 * random number generator engine @p __x into the output stream
718 * @param __os An output stream.
719 * @param __x A % subtract_with_carry_engine random number generator
722 * @returns The output stream with the state of @p __x inserted or in
725 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
726 typename _CharT
, typename _Traits
>
727 friend std::basic_ostream
<_CharT
, _Traits
>&
728 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
729 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
733 * @brief Extracts the current state of a % subtract_with_carry_engine
734 * random number generator engine @p __x from the input stream
737 * @param __is An input stream.
738 * @param __x A % subtract_with_carry_engine random number generator
741 * @returns The input stream with the state of @p __x extracted or in
744 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
745 typename _CharT
, typename _Traits
>
746 friend std::basic_istream
<_CharT
, _Traits
>&
747 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
748 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
752 _UIntType _M_x
[long_lag
];
758 * @brief Compares two % subtract_with_carry_engine random number
759 * generator objects of the same type for inequality.
761 * @param __lhs A % subtract_with_carry_engine random number generator
763 * @param __rhs Another % subtract_with_carry_engine random number
766 * @returns true if the infinite sequences of generated values
767 * would be different, false otherwise.
769 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
771 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
773 const std::subtract_with_carry_engine
<_UIntType
, __w
,
775 { return !(__lhs
== __rhs
); }
779 * Produces random numbers from some base engine by discarding blocks of
782 * 0 <= @p __r <= @p __p
784 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
785 class discard_block_engine
787 static_assert(1 <= __r
&& __r
<= __p
,
788 "template argument substituting __r out of bounds");
791 /** The type of the generated random value. */
792 typedef typename
_RandomNumberEngine::result_type result_type
;
795 static constexpr size_t block_size
= __p
;
796 static constexpr size_t used_block
= __r
;
799 * @brief Constructs a default %discard_block_engine engine.
801 * The underlying engine is default constructed as well.
803 discard_block_engine()
804 : _M_b(), _M_n(0) { }
807 * @brief Copy constructs a %discard_block_engine engine.
809 * Copies an existing base class random number generator.
810 * @param __rng An existing (base class) engine object.
813 discard_block_engine(const _RandomNumberEngine
& __rng
)
814 : _M_b(__rng
), _M_n(0) { }
817 * @brief Move constructs a %discard_block_engine engine.
819 * Copies an existing base class random number generator.
820 * @param __rng An existing (base class) engine object.
823 discard_block_engine(_RandomNumberEngine
&& __rng
)
824 : _M_b(std::move(__rng
)), _M_n(0) { }
827 * @brief Seed constructs a %discard_block_engine engine.
829 * Constructs the underlying generator engine seeded with @p __s.
830 * @param __s A seed value for the base class engine.
833 discard_block_engine(result_type __s
)
834 : _M_b(__s
), _M_n(0) { }
837 * @brief Generator construct a %discard_block_engine engine.
839 * @param __q A seed sequence.
841 template<typename _Sseq
, typename
= typename
842 std::enable_if
<!std::is_same
<_Sseq
, discard_block_engine
>::value
843 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
846 discard_block_engine(_Sseq
& __q
)
851 * @brief Reseeds the %discard_block_engine object with the default
852 * seed for the underlying base class generator engine.
862 * @brief Reseeds the %discard_block_engine object with the default
863 * seed for the underlying base class generator engine.
866 seed(result_type __s
)
873 * @brief Reseeds the %discard_block_engine object with the given seed
875 * @param __q A seed generator function.
877 template<typename _Sseq
>
886 * @brief Gets a const reference to the underlying generator engine
889 const _RandomNumberEngine
&
890 base() const noexcept
894 * @brief Gets the minimum value in the generated random number range.
896 static constexpr result_type
898 { return _RandomNumberEngine::min(); }
901 * @brief Gets the maximum value in the generated random number range.
903 static constexpr result_type
905 { return _RandomNumberEngine::max(); }
908 * @brief Discard a sequence of random numbers.
911 discard(unsigned long long __z
)
913 for (; __z
!= 0ULL; --__z
)
918 * @brief Gets the next value in the generated random number sequence.
924 * @brief Compares two %discard_block_engine random number generator
925 * objects of the same type for equality.
927 * @param __lhs A %discard_block_engine random number generator object.
928 * @param __rhs Another %discard_block_engine random number generator
931 * @returns true if the infinite sequences of generated values
932 * would be equal, false otherwise.
935 operator==(const discard_block_engine
& __lhs
,
936 const discard_block_engine
& __rhs
)
937 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
940 * @brief Inserts the current state of a %discard_block_engine random
941 * number generator engine @p __x into the output stream
944 * @param __os An output stream.
945 * @param __x A %discard_block_engine random number generator engine.
947 * @returns The output stream with the state of @p __x inserted or in
950 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
951 typename _CharT
, typename _Traits
>
952 friend std::basic_ostream
<_CharT
, _Traits
>&
953 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
954 const std::discard_block_engine
<_RandomNumberEngine1
,
958 * @brief Extracts the current state of a % subtract_with_carry_engine
959 * random number generator engine @p __x from the input stream
962 * @param __is An input stream.
963 * @param __x A %discard_block_engine random number generator engine.
965 * @returns The input stream with the state of @p __x extracted or in
968 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
969 typename _CharT
, typename _Traits
>
970 friend std::basic_istream
<_CharT
, _Traits
>&
971 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
972 std::discard_block_engine
<_RandomNumberEngine1
,
976 _RandomNumberEngine _M_b
;
981 * @brief Compares two %discard_block_engine random number generator
982 * objects of the same type for inequality.
984 * @param __lhs A %discard_block_engine random number generator object.
985 * @param __rhs Another %discard_block_engine random number generator
988 * @returns true if the infinite sequences of generated values
989 * would be different, false otherwise.
991 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
993 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
995 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
997 { return !(__lhs
== __rhs
); }
1001 * Produces random numbers by combining random numbers from some base
1002 * engine to produce random numbers with a specifies number of bits @p __w.
1004 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1005 class independent_bits_engine
1007 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
1008 "substituting _UIntType not an unsigned integral type");
1009 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
1010 "template argument substituting __w out of bounds");
1013 /** The type of the generated random value. */
1014 typedef _UIntType result_type
;
1017 * @brief Constructs a default %independent_bits_engine engine.
1019 * The underlying engine is default constructed as well.
1021 independent_bits_engine()
1025 * @brief Copy constructs a %independent_bits_engine engine.
1027 * Copies an existing base class random number generator.
1028 * @param __rng An existing (base class) engine object.
1031 independent_bits_engine(const _RandomNumberEngine
& __rng
)
1035 * @brief Move constructs a %independent_bits_engine engine.
1037 * Copies an existing base class random number generator.
1038 * @param __rng An existing (base class) engine object.
1041 independent_bits_engine(_RandomNumberEngine
&& __rng
)
1042 : _M_b(std::move(__rng
)) { }
1045 * @brief Seed constructs a %independent_bits_engine engine.
1047 * Constructs the underlying generator engine seeded with @p __s.
1048 * @param __s A seed value for the base class engine.
1051 independent_bits_engine(result_type __s
)
1055 * @brief Generator construct a %independent_bits_engine engine.
1057 * @param __q A seed sequence.
1059 template<typename _Sseq
, typename
= typename
1060 std::enable_if
<!std::is_same
<_Sseq
, independent_bits_engine
>::value
1061 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1064 independent_bits_engine(_Sseq
& __q
)
1069 * @brief Reseeds the %independent_bits_engine object with the default
1070 * seed for the underlying base class generator engine.
1077 * @brief Reseeds the %independent_bits_engine object with the default
1078 * seed for the underlying base class generator engine.
1081 seed(result_type __s
)
1085 * @brief Reseeds the %independent_bits_engine object with the given
1087 * @param __q A seed generator function.
1089 template<typename _Sseq
>
1095 * @brief Gets a const reference to the underlying generator engine
1098 const _RandomNumberEngine
&
1099 base() const noexcept
1103 * @brief Gets the minimum value in the generated random number range.
1105 static constexpr result_type
1110 * @brief Gets the maximum value in the generated random number range.
1112 static constexpr result_type
1114 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1117 * @brief Discard a sequence of random numbers.
1120 discard(unsigned long long __z
)
1122 for (; __z
!= 0ULL; --__z
)
1127 * @brief Gets the next value in the generated random number sequence.
1133 * @brief Compares two %independent_bits_engine random number generator
1134 * objects of the same type for equality.
1136 * @param __lhs A %independent_bits_engine random number generator
1138 * @param __rhs Another %independent_bits_engine random number generator
1141 * @returns true if the infinite sequences of generated values
1142 * would be equal, false otherwise.
1145 operator==(const independent_bits_engine
& __lhs
,
1146 const independent_bits_engine
& __rhs
)
1147 { return __lhs
._M_b
== __rhs
._M_b
; }
1150 * @brief Extracts the current state of a % subtract_with_carry_engine
1151 * random number generator engine @p __x from the input stream
1154 * @param __is An input stream.
1155 * @param __x A %independent_bits_engine random number generator
1158 * @returns The input stream with the state of @p __x extracted or in
1161 template<typename _CharT
, typename _Traits
>
1162 friend std::basic_istream
<_CharT
, _Traits
>&
1163 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1164 std::independent_bits_engine
<_RandomNumberEngine
,
1165 __w
, _UIntType
>& __x
)
1172 _RandomNumberEngine _M_b
;
1176 * @brief Compares two %independent_bits_engine random number generator
1177 * objects of the same type for inequality.
1179 * @param __lhs A %independent_bits_engine random number generator
1181 * @param __rhs Another %independent_bits_engine random number generator
1184 * @returns true if the infinite sequences of generated values
1185 * would be different, false otherwise.
1187 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1189 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1191 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1193 { return !(__lhs
== __rhs
); }
1196 * @brief Inserts the current state of a %independent_bits_engine random
1197 * number generator engine @p __x into the output stream @p __os.
1199 * @param __os An output stream.
1200 * @param __x A %independent_bits_engine random number generator engine.
1202 * @returns The output stream with the state of @p __x inserted or in
1205 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1206 typename _CharT
, typename _Traits
>
1207 std::basic_ostream
<_CharT
, _Traits
>&
1208 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1209 const std::independent_bits_engine
<_RandomNumberEngine
,
1210 __w
, _UIntType
>& __x
)
1218 * @brief Produces random numbers by combining random numbers from some
1219 * base engine to produce random numbers with a specifies number of bits
1222 template<typename _RandomNumberEngine
, size_t __k
>
1223 class shuffle_order_engine
1225 static_assert(1u <= __k
, "template argument substituting "
1226 "__k out of bound");
1229 /** The type of the generated random value. */
1230 typedef typename
_RandomNumberEngine::result_type result_type
;
1232 static constexpr size_t table_size
= __k
;
1235 * @brief Constructs a default %shuffle_order_engine engine.
1237 * The underlying engine is default constructed as well.
1239 shuffle_order_engine()
1241 { _M_initialize(); }
1244 * @brief Copy constructs a %shuffle_order_engine engine.
1246 * Copies an existing base class random number generator.
1247 * @param __rng An existing (base class) engine object.
1250 shuffle_order_engine(const _RandomNumberEngine
& __rng
)
1252 { _M_initialize(); }
1255 * @brief Move constructs a %shuffle_order_engine engine.
1257 * Copies an existing base class random number generator.
1258 * @param __rng An existing (base class) engine object.
1261 shuffle_order_engine(_RandomNumberEngine
&& __rng
)
1262 : _M_b(std::move(__rng
))
1263 { _M_initialize(); }
1266 * @brief Seed constructs a %shuffle_order_engine engine.
1268 * Constructs the underlying generator engine seeded with @p __s.
1269 * @param __s A seed value for the base class engine.
1272 shuffle_order_engine(result_type __s
)
1274 { _M_initialize(); }
1277 * @brief Generator construct a %shuffle_order_engine engine.
1279 * @param __q A seed sequence.
1281 template<typename _Sseq
, typename
= typename
1282 std::enable_if
<!std::is_same
<_Sseq
, shuffle_order_engine
>::value
1283 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1286 shuffle_order_engine(_Sseq
& __q
)
1288 { _M_initialize(); }
1291 * @brief Reseeds the %shuffle_order_engine object with the default seed
1292 for the underlying base class generator engine.
1302 * @brief Reseeds the %shuffle_order_engine object with the default seed
1303 * for the underlying base class generator engine.
1306 seed(result_type __s
)
1313 * @brief Reseeds the %shuffle_order_engine object with the given seed
1315 * @param __q A seed generator function.
1317 template<typename _Sseq
>
1326 * Gets a const reference to the underlying generator engine object.
1328 const _RandomNumberEngine
&
1329 base() const noexcept
1333 * Gets the minimum value in the generated random number range.
1335 static constexpr result_type
1337 { return _RandomNumberEngine::min(); }
1340 * Gets the maximum value in the generated random number range.
1342 static constexpr result_type
1344 { return _RandomNumberEngine::max(); }
1347 * Discard a sequence of random numbers.
1350 discard(unsigned long long __z
)
1352 for (; __z
!= 0ULL; --__z
)
1357 * Gets the next value in the generated random number sequence.
1363 * Compares two %shuffle_order_engine random number generator objects
1364 * of the same type for equality.
1366 * @param __lhs A %shuffle_order_engine random number generator object.
1367 * @param __rhs Another %shuffle_order_engine random number generator
1370 * @returns true if the infinite sequences of generated values
1371 * would be equal, false otherwise.
1374 operator==(const shuffle_order_engine
& __lhs
,
1375 const shuffle_order_engine
& __rhs
)
1376 { return (__lhs
._M_b
== __rhs
._M_b
1377 && std::equal(__lhs
._M_v
, __lhs
._M_v
+ __k
, __rhs
._M_v
)
1378 && __lhs
._M_y
== __rhs
._M_y
); }
1381 * @brief Inserts the current state of a %shuffle_order_engine random
1382 * number generator engine @p __x into the output stream
1385 * @param __os An output stream.
1386 * @param __x A %shuffle_order_engine random number generator engine.
1388 * @returns The output stream with the state of @p __x inserted or in
1391 template<typename _RandomNumberEngine1
, size_t __k1
,
1392 typename _CharT
, typename _Traits
>
1393 friend std::basic_ostream
<_CharT
, _Traits
>&
1394 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1395 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1399 * @brief Extracts the current state of a % subtract_with_carry_engine
1400 * random number generator engine @p __x from the input stream
1403 * @param __is An input stream.
1404 * @param __x A %shuffle_order_engine random number generator engine.
1406 * @returns The input stream with the state of @p __x extracted or in
1409 template<typename _RandomNumberEngine1
, size_t __k1
,
1410 typename _CharT
, typename _Traits
>
1411 friend std::basic_istream
<_CharT
, _Traits
>&
1412 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1413 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>& __x
);
1416 void _M_initialize()
1418 for (size_t __i
= 0; __i
< __k
; ++__i
)
1423 _RandomNumberEngine _M_b
;
1424 result_type _M_v
[__k
];
1429 * Compares two %shuffle_order_engine random number generator objects
1430 * of the same type for inequality.
1432 * @param __lhs A %shuffle_order_engine random number generator object.
1433 * @param __rhs Another %shuffle_order_engine random number generator
1436 * @returns true if the infinite sequences of generated values
1437 * would be different, false otherwise.
1439 template<typename _RandomNumberEngine
, size_t __k
>
1441 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1443 const std::shuffle_order_engine
<_RandomNumberEngine
,
1445 { return !(__lhs
== __rhs
); }
1449 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1451 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1455 * An alternative LCR (Lehmer Generator function).
1457 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1461 * The classic Mersenne Twister.
1464 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1465 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1466 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1468 typedef mersenne_twister_engine
<
1474 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1477 * An alternative Mersenne Twister.
1479 typedef mersenne_twister_engine
<
1482 0xb5026f5aa96619e9ULL
, 29,
1483 0x5555555555555555ULL
, 17,
1484 0x71d67fffeda60000ULL
, 37,
1485 0xfff7eee000000000ULL
, 43,
1486 6364136223846793005ULL> mt19937_64
;
1488 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1491 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1494 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1496 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1498 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1500 typedef minstd_rand0 default_random_engine
;
1503 * A standard interface to a platform-specific non-deterministic
1504 * random number generator (if any are available).
1509 /** The type of the generated random value. */
1510 typedef unsigned int result_type
;
1512 // constructors, destructors and member functions
1514 #ifdef _GLIBCXX_USE_RANDOM_TR1
1517 random_device(const std::string
& __token
= "/dev/urandom")
1519 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1520 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1521 std::__throw_runtime_error(__N("random_device::"
1522 "random_device(const std::string&)"));
1526 { std::fclose(_M_file
); }
1531 random_device(const std::string
& __token
= "mt19937")
1532 : _M_mt(_M_strtoul(__token
)) { }
1535 static unsigned long
1536 _M_strtoul(const std::string
& __str
)
1538 unsigned long __ret
= 5489UL;
1539 if (__str
!= "mt19937")
1541 const char* __nptr
= __str
.c_str();
1543 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1544 if (*__nptr
== '\0' || *__endptr
!= '\0')
1545 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1546 "(const std::string&)"));
1555 static constexpr result_type
1557 { return std::numeric_limits
<result_type
>::min(); }
1559 static constexpr result_type
1561 { return std::numeric_limits
<result_type
>::max(); }
1564 entropy() const noexcept
1570 #ifdef _GLIBCXX_USE_RANDOM_TR1
1572 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1580 // No copy functions.
1581 random_device(const random_device
&) = delete;
1582 void operator=(const random_device
&) = delete;
1586 #ifdef _GLIBCXX_USE_RANDOM_TR1
1593 /* @} */ // group random_generators
1596 * @addtogroup random_distributions Random Number Distributions
1602 * @addtogroup random_distributions_uniform Uniform Distributions
1603 * @ingroup random_distributions
1608 * @brief Uniform discrete distribution for random numbers.
1609 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1610 * probability throughout the range.
1612 template<typename _IntType
= int>
1613 class uniform_int_distribution
1615 static_assert(std::is_integral
<_IntType
>::value
,
1616 "template argument not an integral type");
1619 /** The type of the range of the distribution. */
1620 typedef _IntType result_type
;
1621 /** Parameter type. */
1624 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1627 param_type(_IntType __a
= 0,
1628 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1629 : _M_a(__a
), _M_b(__b
)
1631 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1643 operator==(const param_type
& __p1
, const param_type
& __p2
)
1644 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1653 * @brief Constructs a uniform distribution object.
1656 uniform_int_distribution(_IntType __a
= 0,
1657 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1658 : _M_param(__a
, __b
)
1662 uniform_int_distribution(const param_type
& __p
)
1667 * @brief Resets the distribution state.
1669 * Does nothing for the uniform integer distribution.
1676 { return _M_param
.a(); }
1680 { return _M_param
.b(); }
1683 * @brief Returns the parameter set of the distribution.
1687 { return _M_param
; }
1690 * @brief Sets the parameter set of the distribution.
1691 * @param __param The new parameter set of the distribution.
1694 param(const param_type
& __param
)
1695 { _M_param
= __param
; }
1698 * @brief Returns the inclusive lower bound of the distribution range.
1702 { return this->a(); }
1705 * @brief Returns the inclusive upper bound of the distribution range.
1709 { return this->b(); }
1712 * @brief Generating functions.
1714 template<typename _UniformRandomNumberGenerator
>
1716 operator()(_UniformRandomNumberGenerator
& __urng
)
1717 { return this->operator()(__urng
, this->param()); }
1719 template<typename _UniformRandomNumberGenerator
>
1721 operator()(_UniformRandomNumberGenerator
& __urng
,
1722 const param_type
& __p
);
1724 param_type _M_param
;
1728 * @brief Return true if two uniform integer distributions have
1729 * the same parameters.
1731 template<typename _IntType
>
1733 operator==(const std::uniform_int_distribution
<_IntType
>& __d1
,
1734 const std::uniform_int_distribution
<_IntType
>& __d2
)
1735 { return __d1
.param() == __d2
.param(); }
1738 * @brief Return true if two uniform integer distributions have
1739 * different parameters.
1741 template<typename _IntType
>
1743 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1744 const std::uniform_int_distribution
<_IntType
>& __d2
)
1745 { return !(__d1
== __d2
); }
1748 * @brief Inserts a %uniform_int_distribution random number
1749 * distribution @p __x into the output stream @p os.
1751 * @param __os An output stream.
1752 * @param __x A %uniform_int_distribution random number distribution.
1754 * @returns The output stream with the state of @p __x inserted or in
1757 template<typename _IntType
, typename _CharT
, typename _Traits
>
1758 std::basic_ostream
<_CharT
, _Traits
>&
1759 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1760 const std::uniform_int_distribution
<_IntType
>&);
1763 * @brief Extracts a %uniform_int_distribution random number distribution
1764 * @p __x from the input stream @p __is.
1766 * @param __is An input stream.
1767 * @param __x A %uniform_int_distribution random number generator engine.
1769 * @returns The input stream with @p __x extracted or in an error state.
1771 template<typename _IntType
, typename _CharT
, typename _Traits
>
1772 std::basic_istream
<_CharT
, _Traits
>&
1773 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1774 std::uniform_int_distribution
<_IntType
>&);
1778 * @brief Uniform continuous distribution for random numbers.
1780 * A continuous random distribution on the range [min, max) with equal
1781 * probability throughout the range. The URNG should be real-valued and
1782 * deliver number in the range [0, 1).
1784 template<typename _RealType
= double>
1785 class uniform_real_distribution
1787 static_assert(std::is_floating_point
<_RealType
>::value
,
1788 "template argument not a floating point type");
1791 /** The type of the range of the distribution. */
1792 typedef _RealType result_type
;
1793 /** Parameter type. */
1796 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1799 param_type(_RealType __a
= _RealType(0),
1800 _RealType __b
= _RealType(1))
1801 : _M_a(__a
), _M_b(__b
)
1803 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1815 operator==(const param_type
& __p1
, const param_type
& __p2
)
1816 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1825 * @brief Constructs a uniform_real_distribution object.
1827 * @param __a [IN] The lower bound of the distribution.
1828 * @param __b [IN] The upper bound of the distribution.
1831 uniform_real_distribution(_RealType __a
= _RealType(0),
1832 _RealType __b
= _RealType(1))
1833 : _M_param(__a
, __b
)
1837 uniform_real_distribution(const param_type
& __p
)
1842 * @brief Resets the distribution state.
1844 * Does nothing for the uniform real distribution.
1851 { return _M_param
.a(); }
1855 { return _M_param
.b(); }
1858 * @brief Returns the parameter set of the distribution.
1862 { return _M_param
; }
1865 * @brief Sets the parameter set of the distribution.
1866 * @param __param The new parameter set of the distribution.
1869 param(const param_type
& __param
)
1870 { _M_param
= __param
; }
1873 * @brief Returns the inclusive lower bound of the distribution range.
1877 { return this->a(); }
1880 * @brief Returns the inclusive upper bound of the distribution range.
1884 { return this->b(); }
1887 * @brief Generating functions.
1889 template<typename _UniformRandomNumberGenerator
>
1891 operator()(_UniformRandomNumberGenerator
& __urng
)
1892 { return this->operator()(__urng
, this->param()); }
1894 template<typename _UniformRandomNumberGenerator
>
1896 operator()(_UniformRandomNumberGenerator
& __urng
,
1897 const param_type
& __p
)
1899 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1901 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1905 param_type _M_param
;
1909 * @brief Return true if two uniform real distributions have
1910 * the same parameters.
1912 template<typename _IntType
>
1914 operator==(const std::uniform_real_distribution
<_IntType
>& __d1
,
1915 const std::uniform_real_distribution
<_IntType
>& __d2
)
1916 { return __d1
.param() == __d2
.param(); }
1919 * @brief Return true if two uniform real distributions have
1920 * different parameters.
1922 template<typename _IntType
>
1924 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1925 const std::uniform_real_distribution
<_IntType
>& __d2
)
1926 { return !(__d1
== __d2
); }
1929 * @brief Inserts a %uniform_real_distribution random number
1930 * distribution @p __x into the output stream @p __os.
1932 * @param __os An output stream.
1933 * @param __x A %uniform_real_distribution random number distribution.
1935 * @returns The output stream with the state of @p __x inserted or in
1938 template<typename _RealType
, typename _CharT
, typename _Traits
>
1939 std::basic_ostream
<_CharT
, _Traits
>&
1940 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1941 const std::uniform_real_distribution
<_RealType
>&);
1944 * @brief Extracts a %uniform_real_distribution random number distribution
1945 * @p __x from the input stream @p __is.
1947 * @param __is An input stream.
1948 * @param __x A %uniform_real_distribution random number generator engine.
1950 * @returns The input stream with @p __x extracted or in an error state.
1952 template<typename _RealType
, typename _CharT
, typename _Traits
>
1953 std::basic_istream
<_CharT
, _Traits
>&
1954 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1955 std::uniform_real_distribution
<_RealType
>&);
1957 /* @} */ // group random_distributions_uniform
1960 * @addtogroup random_distributions_normal Normal Distributions
1961 * @ingroup random_distributions
1966 * @brief A normal continuous distribution for random numbers.
1968 * The formula for the normal probability density function is
1970 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1971 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1974 template<typename _RealType
= double>
1975 class normal_distribution
1977 static_assert(std::is_floating_point
<_RealType
>::value
,
1978 "template argument not a floating point type");
1981 /** The type of the range of the distribution. */
1982 typedef _RealType result_type
;
1983 /** Parameter type. */
1986 typedef normal_distribution
<_RealType
> distribution_type
;
1989 param_type(_RealType __mean
= _RealType(0),
1990 _RealType __stddev
= _RealType(1))
1991 : _M_mean(__mean
), _M_stddev(__stddev
)
1993 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
2002 { return _M_stddev
; }
2005 operator==(const param_type
& __p1
, const param_type
& __p2
)
2006 { return (__p1
._M_mean
== __p2
._M_mean
2007 && __p1
._M_stddev
== __p2
._M_stddev
); }
2011 _RealType _M_stddev
;
2016 * Constructs a normal distribution with parameters @f$mean@f$ and
2017 * standard deviation.
2020 normal_distribution(result_type __mean
= result_type(0),
2021 result_type __stddev
= result_type(1))
2022 : _M_param(__mean
, __stddev
), _M_saved_available(false)
2026 normal_distribution(const param_type
& __p
)
2027 : _M_param(__p
), _M_saved_available(false)
2031 * @brief Resets the distribution state.
2035 { _M_saved_available
= false; }
2038 * @brief Returns the mean of the distribution.
2042 { return _M_param
.mean(); }
2045 * @brief Returns the standard deviation of the distribution.
2049 { return _M_param
.stddev(); }
2052 * @brief Returns the parameter set of the distribution.
2056 { return _M_param
; }
2059 * @brief Sets the parameter set of the distribution.
2060 * @param __param The new parameter set of the distribution.
2063 param(const param_type
& __param
)
2064 { _M_param
= __param
; }
2067 * @brief Returns the greatest lower bound value of the distribution.
2071 { return std::numeric_limits
<result_type
>::min(); }
2074 * @brief Returns the least upper bound value of the distribution.
2078 { return std::numeric_limits
<result_type
>::max(); }
2081 * @brief Generating functions.
2083 template<typename _UniformRandomNumberGenerator
>
2085 operator()(_UniformRandomNumberGenerator
& __urng
)
2086 { return this->operator()(__urng
, this->param()); }
2088 template<typename _UniformRandomNumberGenerator
>
2090 operator()(_UniformRandomNumberGenerator
& __urng
,
2091 const param_type
& __p
);
2094 * @brief Return true if two normal distributions have
2095 * the same parameters and the sequences that would
2096 * be generated are equal.
2098 template<typename _RealType1
>
2100 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2101 const std::normal_distribution
<_RealType1
>& __d2
);
2104 * @brief Inserts a %normal_distribution random number distribution
2105 * @p __x into the output stream @p __os.
2107 * @param __os An output stream.
2108 * @param __x A %normal_distribution random number distribution.
2110 * @returns The output stream with the state of @p __x inserted or in
2113 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2114 friend std::basic_ostream
<_CharT
, _Traits
>&
2115 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2116 const std::normal_distribution
<_RealType1
>& __x
);
2119 * @brief Extracts a %normal_distribution random number distribution
2120 * @p __x from the input stream @p __is.
2122 * @param __is An input stream.
2123 * @param __x A %normal_distribution random number generator engine.
2125 * @returns The input stream with @p __x extracted or in an error
2128 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2129 friend std::basic_istream
<_CharT
, _Traits
>&
2130 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2131 std::normal_distribution
<_RealType1
>& __x
);
2134 param_type _M_param
;
2135 result_type _M_saved
;
2136 bool _M_saved_available
;
2140 * @brief Return true if two normal distributions are different.
2142 template<typename _RealType
>
2144 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2145 const std::normal_distribution
<_RealType
>& __d2
)
2146 { return !(__d1
== __d2
); }
2150 * @brief A lognormal_distribution random number distribution.
2152 * The formula for the normal probability mass function is
2154 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2155 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2158 template<typename _RealType
= double>
2159 class lognormal_distribution
2161 static_assert(std::is_floating_point
<_RealType
>::value
,
2162 "template argument not a floating point type");
2165 /** The type of the range of the distribution. */
2166 typedef _RealType result_type
;
2167 /** Parameter type. */
2170 typedef lognormal_distribution
<_RealType
> distribution_type
;
2173 param_type(_RealType __m
= _RealType(0),
2174 _RealType __s
= _RealType(1))
2175 : _M_m(__m
), _M_s(__s
)
2187 operator==(const param_type
& __p1
, const param_type
& __p2
)
2188 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2196 lognormal_distribution(_RealType __m
= _RealType(0),
2197 _RealType __s
= _RealType(1))
2198 : _M_param(__m
, __s
), _M_nd()
2202 lognormal_distribution(const param_type
& __p
)
2203 : _M_param(__p
), _M_nd()
2207 * Resets the distribution state.
2218 { return _M_param
.m(); }
2222 { return _M_param
.s(); }
2225 * @brief Returns the parameter set of the distribution.
2229 { return _M_param
; }
2232 * @brief Sets the parameter set of the distribution.
2233 * @param __param The new parameter set of the distribution.
2236 param(const param_type
& __param
)
2237 { _M_param
= __param
; }
2240 * @brief Returns the greatest lower bound value of the distribution.
2244 { return result_type(0); }
2247 * @brief Returns the least upper bound value of the distribution.
2251 { return std::numeric_limits
<result_type
>::max(); }
2254 * @brief Generating functions.
2256 template<typename _UniformRandomNumberGenerator
>
2258 operator()(_UniformRandomNumberGenerator
& __urng
)
2259 { return this->operator()(__urng
, this->param()); }
2261 template<typename _UniformRandomNumberGenerator
>
2263 operator()(_UniformRandomNumberGenerator
& __urng
,
2264 const param_type
& __p
)
2265 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2268 * @brief Return true if two lognormal distributions have
2269 * the same parameters and the sequences that would
2270 * be generated are equal.
2272 template<typename _RealType1
>
2274 operator==(const std::lognormal_distribution
<_RealType1
>& __d1
,
2275 const std::lognormal_distribution
<_RealType1
>& __d2
)
2276 { return (__d1
.param() == __d2
.param()
2277 && __d1
._M_nd
== __d2
._M_nd
); }
2280 * @brief Inserts a %lognormal_distribution random number distribution
2281 * @p __x into the output stream @p __os.
2283 * @param __os An output stream.
2284 * @param __x A %lognormal_distribution random number distribution.
2286 * @returns The output stream with the state of @p __x inserted or in
2289 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2290 friend std::basic_ostream
<_CharT
, _Traits
>&
2291 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2292 const std::lognormal_distribution
<_RealType1
>& __x
);
2295 * @brief Extracts a %lognormal_distribution random number distribution
2296 * @p __x from the input stream @p __is.
2298 * @param __is An input stream.
2299 * @param __x A %lognormal_distribution random number
2302 * @returns The input stream with @p __x extracted or in an error state.
2304 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2305 friend std::basic_istream
<_CharT
, _Traits
>&
2306 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2307 std::lognormal_distribution
<_RealType1
>& __x
);
2310 param_type _M_param
;
2312 std::normal_distribution
<result_type
> _M_nd
;
2316 * @brief Return true if two lognormal distributions are different.
2318 template<typename _RealType
>
2320 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2321 const std::lognormal_distribution
<_RealType
>& __d2
)
2322 { return !(__d1
== __d2
); }
2326 * @brief A gamma continuous distribution for random numbers.
2328 * The formula for the gamma probability density function is:
2330 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2331 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2334 template<typename _RealType
= double>
2335 class gamma_distribution
2337 static_assert(std::is_floating_point
<_RealType
>::value
,
2338 "template argument not a floating point type");
2341 /** The type of the range of the distribution. */
2342 typedef _RealType result_type
;
2343 /** Parameter type. */
2346 typedef gamma_distribution
<_RealType
> distribution_type
;
2347 friend class gamma_distribution
<_RealType
>;
2350 param_type(_RealType __alpha_val
= _RealType(1),
2351 _RealType __beta_val
= _RealType(1))
2352 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2354 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2360 { return _M_alpha
; }
2367 operator==(const param_type
& __p1
, const param_type
& __p2
)
2368 { return (__p1
._M_alpha
== __p2
._M_alpha
2369 && __p1
._M_beta
== __p2
._M_beta
); }
2378 _RealType _M_malpha
, _M_a2
;
2383 * @brief Constructs a gamma distribution with parameters
2384 * @f$\alpha@f$ and @f$\beta@f$.
2387 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2388 _RealType __beta_val
= _RealType(1))
2389 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2393 gamma_distribution(const param_type
& __p
)
2394 : _M_param(__p
), _M_nd()
2398 * @brief Resets the distribution state.
2405 * @brief Returns the @f$\alpha@f$ of the distribution.
2409 { return _M_param
.alpha(); }
2412 * @brief Returns the @f$\beta@f$ of the distribution.
2416 { return _M_param
.beta(); }
2419 * @brief Returns the parameter set of the distribution.
2423 { return _M_param
; }
2426 * @brief Sets the parameter set of the distribution.
2427 * @param __param The new parameter set of the distribution.
2430 param(const param_type
& __param
)
2431 { _M_param
= __param
; }
2434 * @brief Returns the greatest lower bound value of the distribution.
2438 { return result_type(0); }
2441 * @brief Returns the least upper bound value of the distribution.
2445 { return std::numeric_limits
<result_type
>::max(); }
2448 * @brief Generating functions.
2450 template<typename _UniformRandomNumberGenerator
>
2452 operator()(_UniformRandomNumberGenerator
& __urng
)
2453 { return this->operator()(__urng
, this->param()); }
2455 template<typename _UniformRandomNumberGenerator
>
2457 operator()(_UniformRandomNumberGenerator
& __urng
,
2458 const param_type
& __p
);
2461 * @brief Return true if two gamma distributions have the same
2462 * parameters and the sequences that would be generated
2465 template<typename _RealType1
>
2467 operator==(const std::gamma_distribution
<_RealType1
>& __d1
,
2468 const std::gamma_distribution
<_RealType1
>& __d2
)
2469 { return (__d1
.param() == __d2
.param()
2470 && __d1
._M_nd
== __d2
._M_nd
); }
2473 * @brief Inserts a %gamma_distribution random number distribution
2474 * @p __x into the output stream @p __os.
2476 * @param __os An output stream.
2477 * @param __x A %gamma_distribution random number distribution.
2479 * @returns The output stream with the state of @p __x inserted or in
2482 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2483 friend std::basic_ostream
<_CharT
, _Traits
>&
2484 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2485 const std::gamma_distribution
<_RealType1
>& __x
);
2488 * @brief Extracts a %gamma_distribution random number distribution
2489 * @p __x from the input stream @p __is.
2491 * @param __is An input stream.
2492 * @param __x A %gamma_distribution random number generator engine.
2494 * @returns The input stream with @p __x extracted or in an error state.
2496 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2497 friend std::basic_istream
<_CharT
, _Traits
>&
2498 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2499 std::gamma_distribution
<_RealType1
>& __x
);
2502 param_type _M_param
;
2504 std::normal_distribution
<result_type
> _M_nd
;
2508 * @brief Return true if two gamma distributions are different.
2510 template<typename _RealType
>
2512 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2513 const std::gamma_distribution
<_RealType
>& __d2
)
2514 { return !(__d1
== __d2
); }
2518 * @brief A chi_squared_distribution random number distribution.
2520 * The formula for the normal probability mass function is
2521 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2523 template<typename _RealType
= double>
2524 class chi_squared_distribution
2526 static_assert(std::is_floating_point
<_RealType
>::value
,
2527 "template argument not a floating point type");
2530 /** The type of the range of the distribution. */
2531 typedef _RealType result_type
;
2532 /** Parameter type. */
2535 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2538 param_type(_RealType __n
= _RealType(1))
2547 operator==(const param_type
& __p1
, const param_type
& __p2
)
2548 { return __p1
._M_n
== __p2
._M_n
; }
2555 chi_squared_distribution(_RealType __n
= _RealType(1))
2556 : _M_param(__n
), _M_gd(__n
/ 2)
2560 chi_squared_distribution(const param_type
& __p
)
2561 : _M_param(__p
), _M_gd(__p
.n() / 2)
2565 * @brief Resets the distribution state.
2576 { return _M_param
.n(); }
2579 * @brief Returns the parameter set of the distribution.
2583 { return _M_param
; }
2586 * @brief Sets the parameter set of the distribution.
2587 * @param __param The new parameter set of the distribution.
2590 param(const param_type
& __param
)
2591 { _M_param
= __param
; }
2594 * @brief Returns the greatest lower bound value of the distribution.
2598 { return result_type(0); }
2601 * @brief Returns the least upper bound value of the distribution.
2605 { return std::numeric_limits
<result_type
>::max(); }
2608 * @brief Generating functions.
2610 template<typename _UniformRandomNumberGenerator
>
2612 operator()(_UniformRandomNumberGenerator
& __urng
)
2613 { return 2 * _M_gd(__urng
); }
2615 template<typename _UniformRandomNumberGenerator
>
2617 operator()(_UniformRandomNumberGenerator
& __urng
,
2618 const param_type
& __p
)
2620 typedef typename
std::gamma_distribution
<result_type
>::param_type
2622 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2626 * @brief Return true if two Chi-squared distributions have
2627 * the same parameters and the sequences that would be
2628 * generated are equal.
2630 template<typename _RealType1
>
2632 operator==(const std::chi_squared_distribution
<_RealType1
>& __d1
,
2633 const std::chi_squared_distribution
<_RealType1
>& __d2
)
2634 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
2637 * @brief Inserts a %chi_squared_distribution random number distribution
2638 * @p __x into the output stream @p __os.
2640 * @param __os An output stream.
2641 * @param __x A %chi_squared_distribution random number distribution.
2643 * @returns The output stream with the state of @p __x inserted or in
2646 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2647 friend std::basic_ostream
<_CharT
, _Traits
>&
2648 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2649 const std::chi_squared_distribution
<_RealType1
>& __x
);
2652 * @brief Extracts a %chi_squared_distribution random number distribution
2653 * @p __x from the input stream @p __is.
2655 * @param __is An input stream.
2656 * @param __x A %chi_squared_distribution random number
2659 * @returns The input stream with @p __x extracted or in an error state.
2661 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2662 friend std::basic_istream
<_CharT
, _Traits
>&
2663 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2664 std::chi_squared_distribution
<_RealType1
>& __x
);
2667 param_type _M_param
;
2669 std::gamma_distribution
<result_type
> _M_gd
;
2673 * @brief Return true if two Chi-squared distributions are different.
2675 template<typename _RealType
>
2677 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2678 const std::chi_squared_distribution
<_RealType
>& __d2
)
2679 { return !(__d1
== __d2
); }
2683 * @brief A cauchy_distribution random number distribution.
2685 * The formula for the normal probability mass function is
2686 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2688 template<typename _RealType
= double>
2689 class cauchy_distribution
2691 static_assert(std::is_floating_point
<_RealType
>::value
,
2692 "template argument not a floating point type");
2695 /** The type of the range of the distribution. */
2696 typedef _RealType result_type
;
2697 /** Parameter type. */
2700 typedef cauchy_distribution
<_RealType
> distribution_type
;
2703 param_type(_RealType __a
= _RealType(0),
2704 _RealType __b
= _RealType(1))
2705 : _M_a(__a
), _M_b(__b
)
2717 operator==(const param_type
& __p1
, const param_type
& __p2
)
2718 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2726 cauchy_distribution(_RealType __a
= _RealType(0),
2727 _RealType __b
= _RealType(1))
2728 : _M_param(__a
, __b
)
2732 cauchy_distribution(const param_type
& __p
)
2737 * @brief Resets the distribution state.
2748 { return _M_param
.a(); }
2752 { return _M_param
.b(); }
2755 * @brief Returns the parameter set of the distribution.
2759 { return _M_param
; }
2762 * @brief Sets the parameter set of the distribution.
2763 * @param __param The new parameter set of the distribution.
2766 param(const param_type
& __param
)
2767 { _M_param
= __param
; }
2770 * @brief Returns the greatest lower bound value of the distribution.
2774 { return std::numeric_limits
<result_type
>::min(); }
2777 * @brief Returns the least upper bound value of the distribution.
2781 { return std::numeric_limits
<result_type
>::max(); }
2784 * @brief Generating functions.
2786 template<typename _UniformRandomNumberGenerator
>
2788 operator()(_UniformRandomNumberGenerator
& __urng
)
2789 { return this->operator()(__urng
, this->param()); }
2791 template<typename _UniformRandomNumberGenerator
>
2793 operator()(_UniformRandomNumberGenerator
& __urng
,
2794 const param_type
& __p
);
2797 param_type _M_param
;
2801 * @brief Return true if two Cauchy distributions have
2802 * the same parameters.
2804 template<typename _RealType
>
2806 operator==(const std::cauchy_distribution
<_RealType
>& __d1
,
2807 const std::cauchy_distribution
<_RealType
>& __d2
)
2808 { return __d1
.param() == __d2
.param(); }
2811 * @brief Return true if two Cauchy distributions have
2812 * different parameters.
2814 template<typename _RealType
>
2816 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
2817 const std::cauchy_distribution
<_RealType
>& __d2
)
2818 { return !(__d1
== __d2
); }
2821 * @brief Inserts a %cauchy_distribution random number distribution
2822 * @p __x into the output stream @p __os.
2824 * @param __os An output stream.
2825 * @param __x A %cauchy_distribution random number distribution.
2827 * @returns The output stream with the state of @p __x inserted or in
2830 template<typename _RealType
, typename _CharT
, typename _Traits
>
2831 std::basic_ostream
<_CharT
, _Traits
>&
2832 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2833 const std::cauchy_distribution
<_RealType
>& __x
);
2836 * @brief Extracts a %cauchy_distribution random number distribution
2837 * @p __x from the input stream @p __is.
2839 * @param __is An input stream.
2840 * @param __x A %cauchy_distribution random number
2843 * @returns The input stream with @p __x extracted or in an error state.
2845 template<typename _RealType
, typename _CharT
, typename _Traits
>
2846 std::basic_istream
<_CharT
, _Traits
>&
2847 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2848 std::cauchy_distribution
<_RealType
>& __x
);
2852 * @brief A fisher_f_distribution random number distribution.
2854 * The formula for the normal probability mass function is
2856 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2857 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2858 * (1 + \frac{mx}{n})^{-(m+n)/2}
2861 template<typename _RealType
= double>
2862 class fisher_f_distribution
2864 static_assert(std::is_floating_point
<_RealType
>::value
,
2865 "template argument not a floating point type");
2868 /** The type of the range of the distribution. */
2869 typedef _RealType result_type
;
2870 /** Parameter type. */
2873 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2876 param_type(_RealType __m
= _RealType(1),
2877 _RealType __n
= _RealType(1))
2878 : _M_m(__m
), _M_n(__n
)
2890 operator==(const param_type
& __p1
, const param_type
& __p2
)
2891 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
2899 fisher_f_distribution(_RealType __m
= _RealType(1),
2900 _RealType __n
= _RealType(1))
2901 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2905 fisher_f_distribution(const param_type
& __p
)
2906 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2910 * @brief Resets the distribution state.
2924 { return _M_param
.m(); }
2928 { return _M_param
.n(); }
2931 * @brief Returns the parameter set of the distribution.
2935 { return _M_param
; }
2938 * @brief Sets the parameter set of the distribution.
2939 * @param __param The new parameter set of the distribution.
2942 param(const param_type
& __param
)
2943 { _M_param
= __param
; }
2946 * @brief Returns the greatest lower bound value of the distribution.
2950 { return result_type(0); }
2953 * @brief Returns the least upper bound value of the distribution.
2957 { return std::numeric_limits
<result_type
>::max(); }
2960 * @brief Generating functions.
2962 template<typename _UniformRandomNumberGenerator
>
2964 operator()(_UniformRandomNumberGenerator
& __urng
)
2965 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
2967 template<typename _UniformRandomNumberGenerator
>
2969 operator()(_UniformRandomNumberGenerator
& __urng
,
2970 const param_type
& __p
)
2972 typedef typename
std::gamma_distribution
<result_type
>::param_type
2974 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
2975 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
2979 * @brief Return true if two Fisher f distributions have
2980 * the same parameters and the sequences that would
2981 * be generated are equal.
2983 template<typename _RealType1
>
2985 operator==(const std::fisher_f_distribution
<_RealType1
>& __d1
,
2986 const std::fisher_f_distribution
<_RealType1
>& __d2
)
2987 { return (__d1
.param() == __d2
.param()
2988 && __d1
._M_gd_x
== __d2
._M_gd_x
2989 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
2992 * @brief Inserts a %fisher_f_distribution random number distribution
2993 * @p __x into the output stream @p __os.
2995 * @param __os An output stream.
2996 * @param __x A %fisher_f_distribution random number distribution.
2998 * @returns The output stream with the state of @p __x inserted or in
3001 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3002 friend std::basic_ostream
<_CharT
, _Traits
>&
3003 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3004 const std::fisher_f_distribution
<_RealType1
>& __x
);
3007 * @brief Extracts a %fisher_f_distribution random number distribution
3008 * @p __x from the input stream @p __is.
3010 * @param __is An input stream.
3011 * @param __x A %fisher_f_distribution random number
3014 * @returns The input stream with @p __x extracted or in an error state.
3016 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3017 friend std::basic_istream
<_CharT
, _Traits
>&
3018 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3019 std::fisher_f_distribution
<_RealType1
>& __x
);
3022 param_type _M_param
;
3024 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3028 * @brief Return true if two Fisher f distributions are diferent.
3030 template<typename _RealType
>
3032 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3033 const std::fisher_f_distribution
<_RealType
>& __d2
)
3034 { return !(__d1
== __d2
); }
3037 * @brief A student_t_distribution random number distribution.
3039 * The formula for the normal probability mass function is:
3041 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3042 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3045 template<typename _RealType
= double>
3046 class student_t_distribution
3048 static_assert(std::is_floating_point
<_RealType
>::value
,
3049 "template argument not a floating point type");
3052 /** The type of the range of the distribution. */
3053 typedef _RealType result_type
;
3054 /** Parameter type. */
3057 typedef student_t_distribution
<_RealType
> distribution_type
;
3060 param_type(_RealType __n
= _RealType(1))
3069 operator==(const param_type
& __p1
, const param_type
& __p2
)
3070 { return __p1
._M_n
== __p2
._M_n
; }
3077 student_t_distribution(_RealType __n
= _RealType(1))
3078 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3082 student_t_distribution(const param_type
& __p
)
3083 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3087 * @brief Resets the distribution state.
3101 { return _M_param
.n(); }
3104 * @brief Returns the parameter set of the distribution.
3108 { return _M_param
; }
3111 * @brief Sets the parameter set of the distribution.
3112 * @param __param The new parameter set of the distribution.
3115 param(const param_type
& __param
)
3116 { _M_param
= __param
; }
3119 * @brief Returns the greatest lower bound value of the distribution.
3123 { return std::numeric_limits
<result_type
>::min(); }
3126 * @brief Returns the least upper bound value of the distribution.
3130 { return std::numeric_limits
<result_type
>::max(); }
3133 * @brief Generating functions.
3135 template<typename _UniformRandomNumberGenerator
>
3137 operator()(_UniformRandomNumberGenerator
& __urng
)
3138 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3140 template<typename _UniformRandomNumberGenerator
>
3142 operator()(_UniformRandomNumberGenerator
& __urng
,
3143 const param_type
& __p
)
3145 typedef typename
std::gamma_distribution
<result_type
>::param_type
3148 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3149 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3153 * @brief Return true if two Student t distributions have
3154 * the same parameters and the sequences that would
3155 * be generated are equal.
3157 template<typename _RealType1
>
3159 operator==(const std::student_t_distribution
<_RealType1
>& __d1
,
3160 const std::student_t_distribution
<_RealType1
>& __d2
)
3161 { return (__d1
.param() == __d2
.param()
3162 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3165 * @brief Inserts a %student_t_distribution random number distribution
3166 * @p __x into the output stream @p __os.
3168 * @param __os An output stream.
3169 * @param __x A %student_t_distribution random number distribution.
3171 * @returns The output stream with the state of @p __x inserted or in
3174 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3175 friend std::basic_ostream
<_CharT
, _Traits
>&
3176 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3177 const std::student_t_distribution
<_RealType1
>& __x
);
3180 * @brief Extracts a %student_t_distribution random number distribution
3181 * @p __x from the input stream @p __is.
3183 * @param __is An input stream.
3184 * @param __x A %student_t_distribution random number
3187 * @returns The input stream with @p __x extracted or in an error state.
3189 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3190 friend std::basic_istream
<_CharT
, _Traits
>&
3191 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3192 std::student_t_distribution
<_RealType1
>& __x
);
3195 param_type _M_param
;
3197 std::normal_distribution
<result_type
> _M_nd
;
3198 std::gamma_distribution
<result_type
> _M_gd
;
3202 * @brief Return true if two Student t distributions are different.
3204 template<typename _RealType
>
3206 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3207 const std::student_t_distribution
<_RealType
>& __d2
)
3208 { return !(__d1
== __d2
); }
3211 /* @} */ // group random_distributions_normal
3214 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3215 * @ingroup random_distributions
3220 * @brief A Bernoulli random number distribution.
3222 * Generates a sequence of true and false values with likelihood @f$p@f$
3223 * that true will come up and @f$(1 - p)@f$ that false will appear.
3225 class bernoulli_distribution
3228 /** The type of the range of the distribution. */
3229 typedef bool result_type
;
3230 /** Parameter type. */
3233 typedef bernoulli_distribution distribution_type
;
3236 param_type(double __p
= 0.5)
3239 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
3247 operator==(const param_type
& __p1
, const param_type
& __p2
)
3248 { return __p1
._M_p
== __p2
._M_p
; }
3256 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3258 * @param __p [IN] The likelihood of a true result being returned.
3259 * Must be in the interval @f$[0, 1]@f$.
3262 bernoulli_distribution(double __p
= 0.5)
3267 bernoulli_distribution(const param_type
& __p
)
3272 * @brief Resets the distribution state.
3274 * Does nothing for a Bernoulli distribution.
3280 * @brief Returns the @p p parameter of the distribution.
3284 { return _M_param
.p(); }
3287 * @brief Returns the parameter set of the distribution.
3291 { return _M_param
; }
3294 * @brief Sets the parameter set of the distribution.
3295 * @param __param The new parameter set of the distribution.
3298 param(const param_type
& __param
)
3299 { _M_param
= __param
; }
3302 * @brief Returns the greatest lower bound value of the distribution.
3306 { return std::numeric_limits
<result_type
>::min(); }
3309 * @brief Returns the least upper bound value of the distribution.
3313 { return std::numeric_limits
<result_type
>::max(); }
3316 * @brief Generating functions.
3318 template<typename _UniformRandomNumberGenerator
>
3320 operator()(_UniformRandomNumberGenerator
& __urng
)
3321 { return this->operator()(__urng
, this->param()); }
3323 template<typename _UniformRandomNumberGenerator
>
3325 operator()(_UniformRandomNumberGenerator
& __urng
,
3326 const param_type
& __p
)
3328 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3330 if ((__aurng() - __aurng
.min())
3331 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3337 param_type _M_param
;
3341 * @brief Return true if two Bernoulli distributions have
3342 * the same parameters.
3345 operator==(const std::bernoulli_distribution
& __d1
,
3346 const std::bernoulli_distribution
& __d2
)
3347 { return __d1
.param() == __d2
.param(); }
3350 * @brief Return true if two Bernoulli distributions have
3351 * different parameters.
3354 operator!=(const std::bernoulli_distribution
& __d1
,
3355 const std::bernoulli_distribution
& __d2
)
3356 { return !(__d1
== __d2
); }
3359 * @brief Inserts a %bernoulli_distribution random number distribution
3360 * @p __x into the output stream @p __os.
3362 * @param __os An output stream.
3363 * @param __x A %bernoulli_distribution random number distribution.
3365 * @returns The output stream with the state of @p __x inserted or in
3368 template<typename _CharT
, typename _Traits
>
3369 std::basic_ostream
<_CharT
, _Traits
>&
3370 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3371 const std::bernoulli_distribution
& __x
);
3374 * @brief Extracts a %bernoulli_distribution random number distribution
3375 * @p __x from the input stream @p __is.
3377 * @param __is An input stream.
3378 * @param __x A %bernoulli_distribution random number generator engine.
3380 * @returns The input stream with @p __x extracted or in an error state.
3382 template<typename _CharT
, typename _Traits
>
3383 std::basic_istream
<_CharT
, _Traits
>&
3384 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3385 std::bernoulli_distribution
& __x
)
3389 __x
.param(bernoulli_distribution::param_type(__p
));
3395 * @brief A discrete binomial random number distribution.
3397 * The formula for the binomial probability density function is
3398 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3399 * and @f$p@f$ are the parameters of the distribution.
3401 template<typename _IntType
= int>
3402 class binomial_distribution
3404 static_assert(std::is_integral
<_IntType
>::value
,
3405 "template argument not an integral type");
3408 /** The type of the range of the distribution. */
3409 typedef _IntType result_type
;
3410 /** Parameter type. */
3413 typedef binomial_distribution
<_IntType
> distribution_type
;
3414 friend class binomial_distribution
<_IntType
>;
3417 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3418 : _M_t(__t
), _M_p(__p
)
3420 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3435 operator==(const param_type
& __p1
, const param_type
& __p2
)
3436 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3446 #if _GLIBCXX_USE_C99_MATH_TR1
3447 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3448 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3453 // constructors and member function
3455 binomial_distribution(_IntType __t
= _IntType(1),
3457 : _M_param(__t
, __p
), _M_nd()
3461 binomial_distribution(const param_type
& __p
)
3462 : _M_param(__p
), _M_nd()
3466 * @brief Resets the distribution state.
3473 * @brief Returns the distribution @p t parameter.
3477 { return _M_param
.t(); }
3480 * @brief Returns the distribution @p p parameter.
3484 { return _M_param
.p(); }
3487 * @brief Returns the parameter set of the distribution.
3491 { return _M_param
; }
3494 * @brief Sets the parameter set of the distribution.
3495 * @param __param The new parameter set of the distribution.
3498 param(const param_type
& __param
)
3499 { _M_param
= __param
; }
3502 * @brief Returns the greatest lower bound value of the distribution.
3509 * @brief Returns the least upper bound value of the distribution.
3513 { return _M_param
.t(); }
3516 * @brief Generating functions.
3518 template<typename _UniformRandomNumberGenerator
>
3520 operator()(_UniformRandomNumberGenerator
& __urng
)
3521 { return this->operator()(__urng
, this->param()); }
3523 template<typename _UniformRandomNumberGenerator
>
3525 operator()(_UniformRandomNumberGenerator
& __urng
,
3526 const param_type
& __p
);
3529 * @brief Return true if two binomial distributions have
3530 * the same parameters and the sequences that would
3531 * be generated are equal.
3533 template<typename _IntType1
>
3535 operator==(const std::binomial_distribution
<_IntType1
>& __d1
,
3536 const std::binomial_distribution
<_IntType1
>& __d2
)
3537 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3538 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
3540 { return __d1
.param() == __d2
.param(); }
3544 * @brief Inserts a %binomial_distribution random number distribution
3545 * @p __x into the output stream @p __os.
3547 * @param __os An output stream.
3548 * @param __x A %binomial_distribution random number distribution.
3550 * @returns The output stream with the state of @p __x inserted or in
3553 template<typename _IntType1
,
3554 typename _CharT
, typename _Traits
>
3555 friend std::basic_ostream
<_CharT
, _Traits
>&
3556 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3557 const std::binomial_distribution
<_IntType1
>& __x
);
3560 * @brief Extracts a %binomial_distribution random number distribution
3561 * @p __x from the input stream @p __is.
3563 * @param __is An input stream.
3564 * @param __x A %binomial_distribution random number generator engine.
3566 * @returns The input stream with @p __x extracted or in an error
3569 template<typename _IntType1
,
3570 typename _CharT
, typename _Traits
>
3571 friend std::basic_istream
<_CharT
, _Traits
>&
3572 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3573 std::binomial_distribution
<_IntType1
>& __x
);
3576 template<typename _UniformRandomNumberGenerator
>
3578 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3580 param_type _M_param
;
3582 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3583 std::normal_distribution
<double> _M_nd
;
3587 * @brief Return true if two binomial distributions are different.
3589 template<typename _IntType
>
3591 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
3592 const std::binomial_distribution
<_IntType
>& __d2
)
3593 { return !(__d1
== __d2
); }
3597 * @brief A discrete geometric random number distribution.
3599 * The formula for the geometric probability density function is
3600 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
3603 template<typename _IntType
= int>
3604 class geometric_distribution
3606 static_assert(std::is_integral
<_IntType
>::value
,
3607 "template argument not an integral type");
3610 /** The type of the range of the distribution. */
3611 typedef _IntType result_type
;
3612 /** Parameter type. */
3615 typedef geometric_distribution
<_IntType
> distribution_type
;
3616 friend class geometric_distribution
<_IntType
>;
3619 param_type(double __p
= 0.5)
3622 _GLIBCXX_DEBUG_ASSERT((_M_p
> 0.0) && (_M_p
< 1.0));
3631 operator==(const param_type
& __p1
, const param_type
& __p2
)
3632 { return __p1
._M_p
== __p2
._M_p
; }
3637 { _M_log_1_p
= std::log(1.0 - _M_p
); }
3644 // constructors and member function
3646 geometric_distribution(double __p
= 0.5)
3651 geometric_distribution(const param_type
& __p
)
3656 * @brief Resets the distribution state.
3658 * Does nothing for the geometric distribution.
3664 * @brief Returns the distribution parameter @p p.
3668 { return _M_param
.p(); }
3671 * @brief Returns the parameter set of the distribution.
3675 { return _M_param
; }
3678 * @brief Sets the parameter set of the distribution.
3679 * @param __param The new parameter set of the distribution.
3682 param(const param_type
& __param
)
3683 { _M_param
= __param
; }
3686 * @brief Returns the greatest lower bound value of the distribution.
3693 * @brief Returns the least upper bound value of the distribution.
3697 { return std::numeric_limits
<result_type
>::max(); }
3700 * @brief Generating functions.
3702 template<typename _UniformRandomNumberGenerator
>
3704 operator()(_UniformRandomNumberGenerator
& __urng
)
3705 { return this->operator()(__urng
, this->param()); }
3707 template<typename _UniformRandomNumberGenerator
>
3709 operator()(_UniformRandomNumberGenerator
& __urng
,
3710 const param_type
& __p
);
3713 param_type _M_param
;
3717 * @brief Return true if two geometric distributions have
3718 * the same parameters.
3720 template<typename _IntType
>
3722 operator==(const std::geometric_distribution
<_IntType
>& __d1
,
3723 const std::geometric_distribution
<_IntType
>& __d2
)
3724 { return __d1
.param() == __d2
.param(); }
3727 * @brief Return true if two geometric distributions have
3728 * different parameters.
3730 template<typename _IntType
>
3732 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
3733 const std::geometric_distribution
<_IntType
>& __d2
)
3734 { return !(__d1
== __d2
); }
3737 * @brief Inserts a %geometric_distribution random number distribution
3738 * @p __x into the output stream @p __os.
3740 * @param __os An output stream.
3741 * @param __x A %geometric_distribution random number distribution.
3743 * @returns The output stream with the state of @p __x inserted or in
3746 template<typename _IntType
,
3747 typename _CharT
, typename _Traits
>
3748 std::basic_ostream
<_CharT
, _Traits
>&
3749 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3750 const std::geometric_distribution
<_IntType
>& __x
);
3753 * @brief Extracts a %geometric_distribution random number distribution
3754 * @p __x from the input stream @p __is.
3756 * @param __is An input stream.
3757 * @param __x A %geometric_distribution random number generator engine.
3759 * @returns The input stream with @p __x extracted or in an error state.
3761 template<typename _IntType
,
3762 typename _CharT
, typename _Traits
>
3763 std::basic_istream
<_CharT
, _Traits
>&
3764 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3765 std::geometric_distribution
<_IntType
>& __x
);
3769 * @brief A negative_binomial_distribution random number distribution.
3771 * The formula for the negative binomial probability mass function is
3772 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3773 * and @f$p@f$ are the parameters of the distribution.
3775 template<typename _IntType
= int>
3776 class negative_binomial_distribution
3778 static_assert(std::is_integral
<_IntType
>::value
,
3779 "template argument not an integral type");
3782 /** The type of the range of the distribution. */
3783 typedef _IntType result_type
;
3784 /** Parameter type. */
3787 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3790 param_type(_IntType __k
= 1, double __p
= 0.5)
3791 : _M_k(__k
), _M_p(__p
)
3793 _GLIBCXX_DEBUG_ASSERT((_M_k
> 0) && (_M_p
> 0.0) && (_M_p
<= 1.0));
3805 operator==(const param_type
& __p1
, const param_type
& __p2
)
3806 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
3814 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3815 : _M_param(__k
, __p
), _M_gd(__k
, (1.0 - __p
) / __p
)
3819 negative_binomial_distribution(const param_type
& __p
)
3820 : _M_param(__p
), _M_gd(__p
.k(), (1.0 - __p
.p()) / __p
.p())
3824 * @brief Resets the distribution state.
3831 * @brief Return the @f$k@f$ parameter of the distribution.
3835 { return _M_param
.k(); }
3838 * @brief Return the @f$p@f$ parameter of the distribution.
3842 { return _M_param
.p(); }
3845 * @brief Returns the parameter set of the distribution.
3849 { return _M_param
; }
3852 * @brief Sets the parameter set of the distribution.
3853 * @param __param The new parameter set of the distribution.
3856 param(const param_type
& __param
)
3857 { _M_param
= __param
; }
3860 * @brief Returns the greatest lower bound value of the distribution.
3864 { return result_type(0); }
3867 * @brief Returns the least upper bound value of the distribution.
3871 { return std::numeric_limits
<result_type
>::max(); }
3874 * @brief Generating functions.
3876 template<typename _UniformRandomNumberGenerator
>
3878 operator()(_UniformRandomNumberGenerator
& __urng
);
3880 template<typename _UniformRandomNumberGenerator
>
3882 operator()(_UniformRandomNumberGenerator
& __urng
,
3883 const param_type
& __p
);
3886 * @brief Return true if two negative binomial distributions have
3887 * the same parameters and the sequences that would be
3888 * generated are equal.
3890 template<typename _IntType1
>
3892 operator==(const std::negative_binomial_distribution
<_IntType1
>& __d1
,
3893 const std::negative_binomial_distribution
<_IntType1
>& __d2
)
3894 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
3897 * @brief Inserts a %negative_binomial_distribution random
3898 * number distribution @p __x into the output stream @p __os.
3900 * @param __os An output stream.
3901 * @param __x A %negative_binomial_distribution random number
3904 * @returns The output stream with the state of @p __x inserted or in
3907 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3908 friend std::basic_ostream
<_CharT
, _Traits
>&
3909 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3910 const std::negative_binomial_distribution
<_IntType1
>& __x
);
3913 * @brief Extracts a %negative_binomial_distribution random number
3914 * distribution @p __x from the input stream @p __is.
3916 * @param __is An input stream.
3917 * @param __x A %negative_binomial_distribution random number
3920 * @returns The input stream with @p __x extracted or in an error state.
3922 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3923 friend std::basic_istream
<_CharT
, _Traits
>&
3924 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3925 std::negative_binomial_distribution
<_IntType1
>& __x
);
3928 param_type _M_param
;
3930 std::gamma_distribution
<double> _M_gd
;
3934 * @brief Return true if two negative binomial distributions are different.
3936 template<typename _IntType
>
3938 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
3939 const std::negative_binomial_distribution
<_IntType
>& __d2
)
3940 { return !(__d1
== __d2
); }
3943 /* @} */ // group random_distributions_bernoulli
3946 * @addtogroup random_distributions_poisson Poisson Distributions
3947 * @ingroup random_distributions
3952 * @brief A discrete Poisson random number distribution.
3954 * The formula for the Poisson probability density function is
3955 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
3956 * parameter of the distribution.
3958 template<typename _IntType
= int>
3959 class poisson_distribution
3961 static_assert(std::is_integral
<_IntType
>::value
,
3962 "template argument not an integral type");
3965 /** The type of the range of the distribution. */
3966 typedef _IntType result_type
;
3967 /** Parameter type. */
3970 typedef poisson_distribution
<_IntType
> distribution_type
;
3971 friend class poisson_distribution
<_IntType
>;
3974 param_type(double __mean
= 1.0)
3977 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3986 operator==(const param_type
& __p1
, const param_type
& __p2
)
3987 { return __p1
._M_mean
== __p2
._M_mean
; }
3990 // Hosts either log(mean) or the threshold of the simple method.
3997 #if _GLIBCXX_USE_C99_MATH_TR1
3998 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
4002 // constructors and member function
4004 poisson_distribution(double __mean
= 1.0)
4005 : _M_param(__mean
), _M_nd()
4009 poisson_distribution(const param_type
& __p
)
4010 : _M_param(__p
), _M_nd()
4014 * @brief Resets the distribution state.
4021 * @brief Returns the distribution parameter @p mean.
4025 { return _M_param
.mean(); }
4028 * @brief Returns the parameter set of the distribution.
4032 { return _M_param
; }
4035 * @brief Sets the parameter set of the distribution.
4036 * @param __param The new parameter set of the distribution.
4039 param(const param_type
& __param
)
4040 { _M_param
= __param
; }
4043 * @brief Returns the greatest lower bound value of the distribution.
4050 * @brief Returns the least upper bound value of the distribution.
4054 { return std::numeric_limits
<result_type
>::max(); }
4057 * @brief Generating functions.
4059 template<typename _UniformRandomNumberGenerator
>
4061 operator()(_UniformRandomNumberGenerator
& __urng
)
4062 { return this->operator()(__urng
, this->param()); }
4064 template<typename _UniformRandomNumberGenerator
>
4066 operator()(_UniformRandomNumberGenerator
& __urng
,
4067 const param_type
& __p
);
4070 * @brief Return true if two Poisson distributions have the same
4071 * parameters and the sequences that would be generated
4074 template<typename _IntType1
>
4076 operator==(const std::poisson_distribution
<_IntType1
>& __d1
,
4077 const std::poisson_distribution
<_IntType1
>& __d2
)
4078 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4079 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
4081 { return __d1
.param() == __d2
.param(); }
4085 * @brief Inserts a %poisson_distribution random number distribution
4086 * @p __x into the output stream @p __os.
4088 * @param __os An output stream.
4089 * @param __x A %poisson_distribution random number distribution.
4091 * @returns The output stream with the state of @p __x inserted or in
4094 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4095 friend std::basic_ostream
<_CharT
, _Traits
>&
4096 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4097 const std::poisson_distribution
<_IntType1
>& __x
);
4100 * @brief Extracts a %poisson_distribution random number distribution
4101 * @p __x from the input stream @p __is.
4103 * @param __is An input stream.
4104 * @param __x A %poisson_distribution random number generator engine.
4106 * @returns The input stream with @p __x extracted or in an error
4109 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4110 friend std::basic_istream
<_CharT
, _Traits
>&
4111 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4112 std::poisson_distribution
<_IntType1
>& __x
);
4115 param_type _M_param
;
4117 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4118 std::normal_distribution
<double> _M_nd
;
4122 * @brief Return true if two Poisson distributions are different.
4124 template<typename _IntType
>
4126 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4127 const std::poisson_distribution
<_IntType
>& __d2
)
4128 { return !(__d1
== __d2
); }
4132 * @brief An exponential continuous distribution for random numbers.
4134 * The formula for the exponential probability density function is
4135 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4137 * <table border=1 cellpadding=10 cellspacing=0>
4138 * <caption align=top>Distribution Statistics</caption>
4139 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4140 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4141 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4142 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4143 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4146 template<typename _RealType
= double>
4147 class exponential_distribution
4149 static_assert(std::is_floating_point
<_RealType
>::value
,
4150 "template argument not a floating point type");
4153 /** The type of the range of the distribution. */
4154 typedef _RealType result_type
;
4155 /** Parameter type. */
4158 typedef exponential_distribution
<_RealType
> distribution_type
;
4161 param_type(_RealType __lambda
= _RealType(1))
4162 : _M_lambda(__lambda
)
4164 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
4169 { return _M_lambda
; }
4172 operator==(const param_type
& __p1
, const param_type
& __p2
)
4173 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4176 _RealType _M_lambda
;
4181 * @brief Constructs an exponential distribution with inverse scale
4182 * parameter @f$\lambda@f$.
4185 exponential_distribution(const result_type
& __lambda
= result_type(1))
4186 : _M_param(__lambda
)
4190 exponential_distribution(const param_type
& __p
)
4195 * @brief Resets the distribution state.
4197 * Has no effect on exponential distributions.
4203 * @brief Returns the inverse scale parameter of the distribution.
4207 { return _M_param
.lambda(); }
4210 * @brief Returns the parameter set of the distribution.
4214 { return _M_param
; }
4217 * @brief Sets the parameter set of the distribution.
4218 * @param __param The new parameter set of the distribution.
4221 param(const param_type
& __param
)
4222 { _M_param
= __param
; }
4225 * @brief Returns the greatest lower bound value of the distribution.
4229 { return result_type(0); }
4232 * @brief Returns the least upper bound value of the distribution.
4236 { return std::numeric_limits
<result_type
>::max(); }
4239 * @brief Generating functions.
4241 template<typename _UniformRandomNumberGenerator
>
4243 operator()(_UniformRandomNumberGenerator
& __urng
)
4244 { return this->operator()(__urng
, this->param()); }
4246 template<typename _UniformRandomNumberGenerator
>
4248 operator()(_UniformRandomNumberGenerator
& __urng
,
4249 const param_type
& __p
)
4251 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4253 return -std::log(__aurng()) / __p
.lambda();
4257 param_type _M_param
;
4261 * @brief Return true if two exponential distributions have the same
4264 template<typename _RealType
>
4266 operator==(const std::exponential_distribution
<_RealType
>& __d1
,
4267 const std::exponential_distribution
<_RealType
>& __d2
)
4268 { return __d1
.param() == __d2
.param(); }
4271 * @brief Return true if two exponential distributions have different
4274 template<typename _RealType
>
4276 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4277 const std::exponential_distribution
<_RealType
>& __d2
)
4278 { return !(__d1
== __d2
); }
4281 * @brief Inserts a %exponential_distribution random number distribution
4282 * @p __x into the output stream @p __os.
4284 * @param __os An output stream.
4285 * @param __x A %exponential_distribution random number distribution.
4287 * @returns The output stream with the state of @p __x inserted or in
4290 template<typename _RealType
, typename _CharT
, typename _Traits
>
4291 std::basic_ostream
<_CharT
, _Traits
>&
4292 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4293 const std::exponential_distribution
<_RealType
>& __x
);
4296 * @brief Extracts a %exponential_distribution random number distribution
4297 * @p __x from the input stream @p __is.
4299 * @param __is An input stream.
4300 * @param __x A %exponential_distribution random number
4303 * @returns The input stream with @p __x extracted or in an error state.
4305 template<typename _RealType
, typename _CharT
, typename _Traits
>
4306 std::basic_istream
<_CharT
, _Traits
>&
4307 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4308 std::exponential_distribution
<_RealType
>& __x
);
4312 * @brief A weibull_distribution random number distribution.
4314 * The formula for the normal probability density function is:
4316 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4317 * \exp{(-(\frac{x}{\beta})^\alpha)}
4320 template<typename _RealType
= double>
4321 class weibull_distribution
4323 static_assert(std::is_floating_point
<_RealType
>::value
,
4324 "template argument not a floating point type");
4327 /** The type of the range of the distribution. */
4328 typedef _RealType result_type
;
4329 /** Parameter type. */
4332 typedef weibull_distribution
<_RealType
> distribution_type
;
4335 param_type(_RealType __a
= _RealType(1),
4336 _RealType __b
= _RealType(1))
4337 : _M_a(__a
), _M_b(__b
)
4349 operator==(const param_type
& __p1
, const param_type
& __p2
)
4350 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4358 weibull_distribution(_RealType __a
= _RealType(1),
4359 _RealType __b
= _RealType(1))
4360 : _M_param(__a
, __b
)
4364 weibull_distribution(const param_type
& __p
)
4369 * @brief Resets the distribution state.
4376 * @brief Return the @f$a@f$ parameter of the distribution.
4380 { return _M_param
.a(); }
4383 * @brief Return the @f$b@f$ parameter of the distribution.
4387 { return _M_param
.b(); }
4390 * @brief Returns the parameter set of the distribution.
4394 { return _M_param
; }
4397 * @brief Sets the parameter set of the distribution.
4398 * @param __param The new parameter set of the distribution.
4401 param(const param_type
& __param
)
4402 { _M_param
= __param
; }
4405 * @brief Returns the greatest lower bound value of the distribution.
4409 { return result_type(0); }
4412 * @brief Returns the least upper bound value of the distribution.
4416 { return std::numeric_limits
<result_type
>::max(); }
4419 * @brief Generating functions.
4421 template<typename _UniformRandomNumberGenerator
>
4423 operator()(_UniformRandomNumberGenerator
& __urng
)
4424 { return this->operator()(__urng
, this->param()); }
4426 template<typename _UniformRandomNumberGenerator
>
4428 operator()(_UniformRandomNumberGenerator
& __urng
,
4429 const param_type
& __p
);
4432 param_type _M_param
;
4436 * @brief Return true if two Weibull distributions have the same
4439 template<typename _RealType
>
4441 operator==(const std::weibull_distribution
<_RealType
>& __d1
,
4442 const std::weibull_distribution
<_RealType
>& __d2
)
4443 { return __d1
.param() == __d2
.param(); }
4446 * @brief Return true if two Weibull distributions have different
4449 template<typename _RealType
>
4451 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
4452 const std::weibull_distribution
<_RealType
>& __d2
)
4453 { return !(__d1
== __d2
); }
4456 * @brief Inserts a %weibull_distribution random number distribution
4457 * @p __x into the output stream @p __os.
4459 * @param __os An output stream.
4460 * @param __x A %weibull_distribution random number distribution.
4462 * @returns The output stream with the state of @p __x inserted or in
4465 template<typename _RealType
, typename _CharT
, typename _Traits
>
4466 std::basic_ostream
<_CharT
, _Traits
>&
4467 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4468 const std::weibull_distribution
<_RealType
>& __x
);
4471 * @brief Extracts a %weibull_distribution random number distribution
4472 * @p __x from the input stream @p __is.
4474 * @param __is An input stream.
4475 * @param __x A %weibull_distribution random number
4478 * @returns The input stream with @p __x extracted or in an error state.
4480 template<typename _RealType
, typename _CharT
, typename _Traits
>
4481 std::basic_istream
<_CharT
, _Traits
>&
4482 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4483 std::weibull_distribution
<_RealType
>& __x
);
4487 * @brief A extreme_value_distribution random number distribution.
4489 * The formula for the normal probability mass function is
4491 * p(x|a,b) = \frac{1}{b}
4492 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
4495 template<typename _RealType
= double>
4496 class extreme_value_distribution
4498 static_assert(std::is_floating_point
<_RealType
>::value
,
4499 "template argument not a floating point type");
4502 /** The type of the range of the distribution. */
4503 typedef _RealType result_type
;
4504 /** Parameter type. */
4507 typedef extreme_value_distribution
<_RealType
> distribution_type
;
4510 param_type(_RealType __a
= _RealType(0),
4511 _RealType __b
= _RealType(1))
4512 : _M_a(__a
), _M_b(__b
)
4524 operator==(const param_type
& __p1
, const param_type
& __p2
)
4525 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4533 extreme_value_distribution(_RealType __a
= _RealType(0),
4534 _RealType __b
= _RealType(1))
4535 : _M_param(__a
, __b
)
4539 extreme_value_distribution(const param_type
& __p
)
4544 * @brief Resets the distribution state.
4551 * @brief Return the @f$a@f$ parameter of the distribution.
4555 { return _M_param
.a(); }
4558 * @brief Return the @f$b@f$ parameter of the distribution.
4562 { return _M_param
.b(); }
4565 * @brief Returns the parameter set of the distribution.
4569 { return _M_param
; }
4572 * @brief Sets the parameter set of the distribution.
4573 * @param __param The new parameter set of the distribution.
4576 param(const param_type
& __param
)
4577 { _M_param
= __param
; }
4580 * @brief Returns the greatest lower bound value of the distribution.
4584 { return std::numeric_limits
<result_type
>::min(); }
4587 * @brief Returns the least upper bound value of the distribution.
4591 { return std::numeric_limits
<result_type
>::max(); }
4594 * @brief Generating functions.
4596 template<typename _UniformRandomNumberGenerator
>
4598 operator()(_UniformRandomNumberGenerator
& __urng
)
4599 { return this->operator()(__urng
, this->param()); }
4601 template<typename _UniformRandomNumberGenerator
>
4603 operator()(_UniformRandomNumberGenerator
& __urng
,
4604 const param_type
& __p
);
4607 param_type _M_param
;
4611 * @brief Return true if two extreme value distributions have the same
4614 template<typename _RealType
>
4616 operator==(const std::extreme_value_distribution
<_RealType
>& __d1
,
4617 const std::extreme_value_distribution
<_RealType
>& __d2
)
4618 { return __d1
.param() == __d2
.param(); }
4621 * @brief Return true if two extreme value distributions have different
4624 template<typename _RealType
>
4626 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
4627 const std::extreme_value_distribution
<_RealType
>& __d2
)
4628 { return !(__d1
== __d2
); }
4631 * @brief Inserts a %extreme_value_distribution random number distribution
4632 * @p __x into the output stream @p __os.
4634 * @param __os An output stream.
4635 * @param __x A %extreme_value_distribution random number distribution.
4637 * @returns The output stream with the state of @p __x inserted or in
4640 template<typename _RealType
, typename _CharT
, typename _Traits
>
4641 std::basic_ostream
<_CharT
, _Traits
>&
4642 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4643 const std::extreme_value_distribution
<_RealType
>& __x
);
4646 * @brief Extracts a %extreme_value_distribution random number
4647 * distribution @p __x from the input stream @p __is.
4649 * @param __is An input stream.
4650 * @param __x A %extreme_value_distribution random number
4653 * @returns The input stream with @p __x extracted or in an error state.
4655 template<typename _RealType
, typename _CharT
, typename _Traits
>
4656 std::basic_istream
<_CharT
, _Traits
>&
4657 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4658 std::extreme_value_distribution
<_RealType
>& __x
);
4662 * @brief A discrete_distribution random number distribution.
4664 * The formula for the discrete probability mass function is
4667 template<typename _IntType
= int>
4668 class discrete_distribution
4670 static_assert(std::is_integral
<_IntType
>::value
,
4671 "template argument not an integral type");
4674 /** The type of the range of the distribution. */
4675 typedef _IntType result_type
;
4676 /** Parameter type. */
4679 typedef discrete_distribution
<_IntType
> distribution_type
;
4680 friend class discrete_distribution
<_IntType
>;
4683 : _M_prob(), _M_cp()
4686 template<typename _InputIterator
>
4687 param_type(_InputIterator __wbegin
,
4688 _InputIterator __wend
)
4689 : _M_prob(__wbegin
, __wend
), _M_cp()
4690 { _M_initialize(); }
4692 param_type(initializer_list
<double> __wil
)
4693 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4694 { _M_initialize(); }
4696 template<typename _Func
>
4697 param_type(size_t __nw
, double __xmin
, double __xmax
,
4700 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4701 param_type(const param_type
&) = default;
4702 param_type
& operator=(const param_type
&) = default;
4705 probabilities() const
4706 { return _M_prob
.empty() ? std::vector
<double>(1, 1.0) : _M_prob
; }
4709 operator==(const param_type
& __p1
, const param_type
& __p2
)
4710 { return __p1
._M_prob
== __p2
._M_prob
; }
4716 std::vector
<double> _M_prob
;
4717 std::vector
<double> _M_cp
;
4720 discrete_distribution()
4724 template<typename _InputIterator
>
4725 discrete_distribution(_InputIterator __wbegin
,
4726 _InputIterator __wend
)
4727 : _M_param(__wbegin
, __wend
)
4730 discrete_distribution(initializer_list
<double> __wl
)
4734 template<typename _Func
>
4735 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4737 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4741 discrete_distribution(const param_type
& __p
)
4746 * @brief Resets the distribution state.
4753 * @brief Returns the probabilities of the distribution.
4756 probabilities() const
4758 return _M_param
._M_prob
.empty()
4759 ? std::vector
<double>(1, 1.0) : _M_param
._M_prob
;
4763 * @brief Returns the parameter set of the distribution.
4767 { return _M_param
; }
4770 * @brief Sets the parameter set of the distribution.
4771 * @param __param The new parameter set of the distribution.
4774 param(const param_type
& __param
)
4775 { _M_param
= __param
; }
4778 * @brief Returns the greatest lower bound value of the distribution.
4782 { return result_type(0); }
4785 * @brief Returns the least upper bound value of the distribution.
4790 return _M_param
._M_prob
.empty()
4791 ? result_type(0) : result_type(_M_param
._M_prob
.size() - 1);
4795 * @brief Generating functions.
4797 template<typename _UniformRandomNumberGenerator
>
4799 operator()(_UniformRandomNumberGenerator
& __urng
)
4800 { return this->operator()(__urng
, this->param()); }
4802 template<typename _UniformRandomNumberGenerator
>
4804 operator()(_UniformRandomNumberGenerator
& __urng
,
4805 const param_type
& __p
);
4808 * @brief Inserts a %discrete_distribution random number distribution
4809 * @p __x into the output stream @p __os.
4811 * @param __os An output stream.
4812 * @param __x A %discrete_distribution random number distribution.
4814 * @returns The output stream with the state of @p __x inserted or in
4817 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4818 friend std::basic_ostream
<_CharT
, _Traits
>&
4819 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4820 const std::discrete_distribution
<_IntType1
>& __x
);
4823 * @brief Extracts a %discrete_distribution random number distribution
4824 * @p __x from the input stream @p __is.
4826 * @param __is An input stream.
4827 * @param __x A %discrete_distribution random number
4830 * @returns The input stream with @p __x extracted or in an error
4833 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4834 friend std::basic_istream
<_CharT
, _Traits
>&
4835 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4836 std::discrete_distribution
<_IntType1
>& __x
);
4839 param_type _M_param
;
4843 * @brief Return true if two discrete distributions have the same
4846 template<typename _IntType
>
4848 operator==(const std::discrete_distribution
<_IntType
>& __d1
,
4849 const std::discrete_distribution
<_IntType
>& __d2
)
4850 { return __d1
.param() == __d2
.param(); }
4853 * @brief Return true if two discrete distributions have different
4856 template<typename _IntType
>
4858 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
4859 const std::discrete_distribution
<_IntType
>& __d2
)
4860 { return !(__d1
== __d2
); }
4864 * @brief A piecewise_constant_distribution random number distribution.
4866 * The formula for the piecewise constant probability mass function is
4869 template<typename _RealType
= double>
4870 class piecewise_constant_distribution
4872 static_assert(std::is_floating_point
<_RealType
>::value
,
4873 "template argument not a floating point type");
4876 /** The type of the range of the distribution. */
4877 typedef _RealType result_type
;
4878 /** Parameter type. */
4881 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4882 friend class piecewise_constant_distribution
<_RealType
>;
4885 : _M_int(), _M_den(), _M_cp()
4888 template<typename _InputIteratorB
, typename _InputIteratorW
>
4889 param_type(_InputIteratorB __bfirst
,
4890 _InputIteratorB __bend
,
4891 _InputIteratorW __wbegin
);
4893 template<typename _Func
>
4894 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
4896 template<typename _Func
>
4897 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4900 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4901 param_type(const param_type
&) = default;
4902 param_type
& operator=(const param_type
&) = default;
4904 std::vector
<_RealType
>
4909 std::vector
<_RealType
> __tmp(2);
4910 __tmp
[1] = _RealType(1);
4919 { return _M_den
.empty() ? std::vector
<double>(1, 1.0) : _M_den
; }
4922 operator==(const param_type
& __p1
, const param_type
& __p2
)
4923 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
4929 std::vector
<_RealType
> _M_int
;
4930 std::vector
<double> _M_den
;
4931 std::vector
<double> _M_cp
;
4935 piecewise_constant_distribution()
4939 template<typename _InputIteratorB
, typename _InputIteratorW
>
4940 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4941 _InputIteratorB __bend
,
4942 _InputIteratorW __wbegin
)
4943 : _M_param(__bfirst
, __bend
, __wbegin
)
4946 template<typename _Func
>
4947 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
4949 : _M_param(__bl
, __fw
)
4952 template<typename _Func
>
4953 piecewise_constant_distribution(size_t __nw
,
4954 _RealType __xmin
, _RealType __xmax
,
4956 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4960 piecewise_constant_distribution(const param_type
& __p
)
4965 * @brief Resets the distribution state.
4972 * @brief Returns a vector of the intervals.
4974 std::vector
<_RealType
>
4977 if (_M_param
._M_int
.empty())
4979 std::vector
<_RealType
> __tmp(2);
4980 __tmp
[1] = _RealType(1);
4984 return _M_param
._M_int
;
4988 * @brief Returns a vector of the probability densities.
4993 return _M_param
._M_den
.empty()
4994 ? std::vector
<double>(1, 1.0) : _M_param
._M_den
;
4998 * @brief Returns the parameter set of the distribution.
5002 { return _M_param
; }
5005 * @brief Sets the parameter set of the distribution.
5006 * @param __param The new parameter set of the distribution.
5009 param(const param_type
& __param
)
5010 { _M_param
= __param
; }
5013 * @brief Returns the greatest lower bound value of the distribution.
5018 return _M_param
._M_int
.empty()
5019 ? result_type(0) : _M_param
._M_int
.front();
5023 * @brief Returns the least upper bound value of the distribution.
5028 return _M_param
._M_int
.empty()
5029 ? result_type(1) : _M_param
._M_int
.back();
5033 * @brief Generating functions.
5035 template<typename _UniformRandomNumberGenerator
>
5037 operator()(_UniformRandomNumberGenerator
& __urng
)
5038 { return this->operator()(__urng
, this->param()); }
5040 template<typename _UniformRandomNumberGenerator
>
5042 operator()(_UniformRandomNumberGenerator
& __urng
,
5043 const param_type
& __p
);
5046 * @brief Inserts a %piecewise_constan_distribution random
5047 * number distribution @p __x into the output stream @p __os.
5049 * @param __os An output stream.
5050 * @param __x A %piecewise_constan_distribution random number
5053 * @returns The output stream with the state of @p __x inserted or in
5056 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5057 friend std::basic_ostream
<_CharT
, _Traits
>&
5058 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5059 const std::piecewise_constant_distribution
<_RealType1
>& __x
);
5062 * @brief Extracts a %piecewise_constan_distribution random
5063 * number distribution @p __x from the input stream @p __is.
5065 * @param __is An input stream.
5066 * @param __x A %piecewise_constan_distribution random number
5069 * @returns The input stream with @p __x extracted or in an error
5072 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5073 friend std::basic_istream
<_CharT
, _Traits
>&
5074 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5075 std::piecewise_constant_distribution
<_RealType1
>& __x
);
5078 param_type _M_param
;
5082 * @brief Return true if two piecewise constant distributions have the
5085 template<typename _RealType
>
5087 operator==(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5088 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5089 { return __d1
.param() == __d2
.param(); }
5092 * @brief Return true if two piecewise constant distributions have
5093 * different parameters.
5095 template<typename _RealType
>
5097 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5098 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5099 { return !(__d1
== __d2
); }
5103 * @brief A piecewise_linear_distribution random number distribution.
5105 * The formula for the piecewise linear probability mass function is
5108 template<typename _RealType
= double>
5109 class piecewise_linear_distribution
5111 static_assert(std::is_floating_point
<_RealType
>::value
,
5112 "template argument not a floating point type");
5115 /** The type of the range of the distribution. */
5116 typedef _RealType result_type
;
5117 /** Parameter type. */
5120 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5121 friend class piecewise_linear_distribution
<_RealType
>;
5124 : _M_int(), _M_den(), _M_cp(), _M_m()
5127 template<typename _InputIteratorB
, typename _InputIteratorW
>
5128 param_type(_InputIteratorB __bfirst
,
5129 _InputIteratorB __bend
,
5130 _InputIteratorW __wbegin
);
5132 template<typename _Func
>
5133 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5135 template<typename _Func
>
5136 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5139 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5140 param_type(const param_type
&) = default;
5141 param_type
& operator=(const param_type
&) = default;
5143 std::vector
<_RealType
>
5148 std::vector
<_RealType
> __tmp(2);
5149 __tmp
[1] = _RealType(1);
5158 { return _M_den
.empty() ? std::vector
<double>(2, 1.0) : _M_den
; }
5161 operator==(const param_type
& __p1
, const param_type
& __p2
)
5162 { return (__p1
._M_int
== __p2
._M_int
5163 && __p1
._M_den
== __p2
._M_den
); }
5169 std::vector
<_RealType
> _M_int
;
5170 std::vector
<double> _M_den
;
5171 std::vector
<double> _M_cp
;
5172 std::vector
<double> _M_m
;
5176 piecewise_linear_distribution()
5180 template<typename _InputIteratorB
, typename _InputIteratorW
>
5181 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5182 _InputIteratorB __bend
,
5183 _InputIteratorW __wbegin
)
5184 : _M_param(__bfirst
, __bend
, __wbegin
)
5187 template<typename _Func
>
5188 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5190 : _M_param(__bl
, __fw
)
5193 template<typename _Func
>
5194 piecewise_linear_distribution(size_t __nw
,
5195 _RealType __xmin
, _RealType __xmax
,
5197 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5201 piecewise_linear_distribution(const param_type
& __p
)
5206 * Resets the distribution state.
5213 * @brief Return the intervals of the distribution.
5215 std::vector
<_RealType
>
5218 if (_M_param
._M_int
.empty())
5220 std::vector
<_RealType
> __tmp(2);
5221 __tmp
[1] = _RealType(1);
5225 return _M_param
._M_int
;
5229 * @brief Return a vector of the probability densities of the
5235 return _M_param
._M_den
.empty()
5236 ? std::vector
<double>(2, 1.0) : _M_param
._M_den
;
5240 * @brief Returns the parameter set of the distribution.
5244 { return _M_param
; }
5247 * @brief Sets the parameter set of the distribution.
5248 * @param __param The new parameter set of the distribution.
5251 param(const param_type
& __param
)
5252 { _M_param
= __param
; }
5255 * @brief Returns the greatest lower bound value of the distribution.
5260 return _M_param
._M_int
.empty()
5261 ? result_type(0) : _M_param
._M_int
.front();
5265 * @brief Returns the least upper bound value of the distribution.
5270 return _M_param
._M_int
.empty()
5271 ? result_type(1) : _M_param
._M_int
.back();
5275 * @brief Generating functions.
5277 template<typename _UniformRandomNumberGenerator
>
5279 operator()(_UniformRandomNumberGenerator
& __urng
)
5280 { return this->operator()(__urng
, this->param()); }
5282 template<typename _UniformRandomNumberGenerator
>
5284 operator()(_UniformRandomNumberGenerator
& __urng
,
5285 const param_type
& __p
);
5288 * @brief Inserts a %piecewise_linear_distribution random number
5289 * distribution @p __x into the output stream @p __os.
5291 * @param __os An output stream.
5292 * @param __x A %piecewise_linear_distribution random number
5295 * @returns The output stream with the state of @p __x inserted or in
5298 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5299 friend std::basic_ostream
<_CharT
, _Traits
>&
5300 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
5301 const std::piecewise_linear_distribution
<_RealType1
>& __x
);
5304 * @brief Extracts a %piecewise_linear_distribution random number
5305 * distribution @p __x from the input stream @p __is.
5307 * @param __is An input stream.
5308 * @param __x A %piecewise_linear_distribution random number
5311 * @returns The input stream with @p __x extracted or in an error
5314 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5315 friend std::basic_istream
<_CharT
, _Traits
>&
5316 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
5317 std::piecewise_linear_distribution
<_RealType1
>& __x
);
5320 param_type _M_param
;
5324 * @brief Return true if two piecewise linear distributions have the
5327 template<typename _RealType
>
5329 operator==(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5330 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5331 { return __d1
.param() == __d2
.param(); }
5334 * @brief Return true if two piecewise linear distributions have
5335 * different parameters.
5337 template<typename _RealType
>
5339 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5340 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5341 { return !(__d1
== __d2
); }
5344 /* @} */ // group random_distributions_poisson
5346 /* @} */ // group random_distributions
5349 * @addtogroup random_utilities Random Number Utilities
5355 * @brief The seed_seq class generates sequences of seeds for random
5356 * number generators.
5362 /** The type of the seed vales. */
5363 typedef uint_least32_t result_type
;
5365 /** Default constructor. */
5370 template<typename _IntType
>
5371 seed_seq(std::initializer_list
<_IntType
> il
);
5373 template<typename _InputIterator
>
5374 seed_seq(_InputIterator __begin
, _InputIterator __end
);
5376 // generating functions
5377 template<typename _RandomAccessIterator
>
5379 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
5381 // property functions
5383 { return _M_v
.size(); }
5385 template<typename OutputIterator
>
5387 param(OutputIterator __dest
) const
5388 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
5392 std::vector
<result_type
> _M_v
;
5395 /* @} */ // group random_utilities
5397 /* @} */ // group random
5399 _GLIBCXX_END_NAMESPACE_VERSION