1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
35 // [26.4] Random number generation
38 * @addtogroup std_random Random Number Generation
39 * A facility for generating random numbers on selected distributions.
44 * @brief A function template for converting the output of a (integral)
45 * uniform random number generator to a floatng point result in the range
48 template<typename _RealType
, size_t __bits
,
49 typename _UniformRandomNumberGenerator
>
51 generate_canonical(_UniformRandomNumberGenerator
& __g
);
54 * Implementation-space details.
58 template<typename _UIntType
, size_t __w
,
59 bool = __w
< static_cast<size_t>
60 (std::numeric_limits
<_UIntType
>::digits
)>
62 { static const _UIntType __value
= 0; };
64 template<typename _UIntType
, size_t __w
>
65 struct _Shift
<_UIntType
, __w
, true>
66 { static const _UIntType __value
= _UIntType(1) << __w
; };
68 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool>
71 // Dispatch based on modulus value to prevent divide-by-zero compile-time
72 // errors when m == 0.
73 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
76 { return _Mod
<_Tp
, __m
, __a
, __c
, __m
== 0>::__calc(__x
); }
79 * An adaptor class for converting the output of any Generator into
80 * the input for a specific Distribution.
82 template<typename _Engine
, typename _DInputType
>
87 _Adaptor(_Engine
& __g
)
92 { return _DInputType(0); }
96 { return _DInputType(1); }
99 * Converts a value generated by the adapted random number generator
100 * into a value in the input domain for the dependent random number
106 return std::generate_canonical
<_DInputType
,
107 std::numeric_limits
<_DInputType
>::digits
,
114 } // namespace __detail
117 * @addtogroup std_random_generators Random Number Generators
118 * @ingroup std_random
120 * These classes define objects which provide random or pseudorandom
121 * numbers, either from a discrete or a continuous interval. The
122 * random number generator supplied as a part of this library are
123 * all uniform random number generators which provide a sequence of
124 * random number uniformly distributed over their range.
126 * A number generator is a function object with an operator() that
127 * takes zero arguments and returns a number.
129 * A compliant random number generator must satisfy the following
130 * requirements. <table border=1 cellpadding=10 cellspacing=0>
131 * <caption align=top>Random Number Generator Requirements</caption>
132 * <tr><td>To be documented.</td></tr> </table>
138 * @brief A model of a linear congruential random number generator.
140 * A random number generator that produces pseudorandom numbers via
143 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
146 * The template parameter @p _UIntType must be an unsigned integral type
147 * large enough to store values up to (__m-1). If the template parameter
148 * @p __m is 0, the modulus @p __m used is
149 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
150 * parameters @p __a and @p __c must be less than @p __m.
152 * The size of the state is @f$1@f$.
154 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
155 class linear_congruential_engine
157 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
158 "substituting _UIntType not an unsigned integral type");
159 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
160 "template argument substituting __m out of bounds");
163 /** The type of the generated random value. */
164 typedef _UIntType result_type
;
166 /** The multiplier. */
167 static const result_type multiplier
= __a
;
169 static const result_type increment
= __c
;
171 static const result_type modulus
= __m
;
172 static const result_type default_seed
= 1u;
175 * @brief Constructs a %linear_congruential_engine random number
176 * generator engine with seed @p __s. The default seed value
179 * @param __s The initial seed value.
182 linear_congruential_engine(result_type __s
= default_seed
)
186 * @brief Constructs a %linear_congruential_engine random number
187 * generator engine seeded from the seed sequence @p __q.
189 * @param __q the seed sequence.
191 template<typename _Sseq
, typename
= typename
192 std::enable_if
<!std::is_same
<_Sseq
, linear_congruential_engine
>::value
>
195 linear_congruential_engine(_Sseq
& __q
)
199 * @brief Reseeds the %linear_congruential_engine random number generator
200 * engine sequence to the seed @p __s.
202 * @param __s The new seed.
205 seed(result_type __s
= default_seed
);
208 * @brief Reseeds the %linear_congruential_engine random number generator
210 * sequence using values from the seed sequence @p __q.
212 * @param __q the seed sequence.
214 template<typename _Sseq
>
215 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
219 * @brief Gets the smallest possible value in the output range.
221 * The minimum depends on the @p __c parameter: if it is zero, the
222 * minimum generated must be > 0, otherwise 0 is allowed.
224 * @todo This should be constexpr.
228 { return __c
== 0u ? 1u : 0u; }
231 * @brief Gets the largest possible value in the output range.
233 * @todo This should be constexpr.
240 * @brief Discard a sequence of random numbers.
242 * @todo Look for a faster way to do discard.
245 discard(unsigned long long __z
)
247 for (; __z
!= 0ULL; --__z
)
252 * @brief Gets the next random number in the sequence.
257 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
262 * @brief Compares two linear congruential random number generator
263 * objects of the same type for equality.
265 * @param __lhs A linear congruential random number generator object.
266 * @param __rhs Another linear congruential random number generator
269 * @returns true if the two objects are equal, false otherwise.
272 operator==(const linear_congruential_engine
& __lhs
,
273 const linear_congruential_engine
& __rhs
)
274 { return __lhs
._M_x
== __rhs
._M_x
; }
277 * @brief Writes the textual representation of the state x(i) of x to
280 * @param __os The output stream.
281 * @param __lcr A % linear_congruential_engine random number generator.
284 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
285 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
286 friend std::basic_ostream
<_CharT
, _Traits
>&
287 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
288 const std::linear_congruential_engine
<_UIntType1
,
292 * @brief Sets the state of the engine by reading its textual
293 * representation from @p __is.
295 * The textual representation must have been previously written using
296 * an output stream whose imbued locale and whose type's template
297 * specialization arguments _CharT and _Traits were the same as those
300 * @param __is The input stream.
301 * @param __lcr A % linear_congruential_engine random number generator.
304 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
305 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
306 friend std::basic_istream
<_CharT
, _Traits
>&
307 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
308 std::linear_congruential_engine
<_UIntType1
, __a1
,
317 * A generalized feedback shift register discrete random number generator.
319 * This algorithm avoids multiplication and division and is designed to be
320 * friendly to a pipelined architecture. If the parameters are chosen
321 * correctly, this generator will produce numbers with a very long period and
322 * fairly good apparent entropy, although still not cryptographically strong.
324 * The best way to use this generator is with the predefined mt19937 class.
326 * This algorithm was originally invented by Makoto Matsumoto and
329 * @var word_size The number of bits in each element of the state vector.
330 * @var state_size The degree of recursion.
331 * @var shift_size The period parameter.
332 * @var mask_bits The separation point bit index.
333 * @var parameter_a The last row of the twist matrix.
334 * @var output_u The first right-shift tempering matrix parameter.
335 * @var output_s The first left-shift tempering matrix parameter.
336 * @var output_b The first left-shift tempering matrix mask.
337 * @var output_t The second left-shift tempering matrix parameter.
338 * @var output_c The second left-shift tempering matrix mask.
339 * @var output_l The second right-shift tempering matrix parameter.
341 template<typename _UIntType
, size_t __w
,
342 size_t __n
, size_t __m
, size_t __r
,
343 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
344 _UIntType __b
, size_t __t
,
345 _UIntType __c
, size_t __l
, _UIntType __f
>
346 class mersenne_twister_engine
348 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
349 "substituting _UIntType not an unsigned integral type");
350 static_assert(1u <= __m
&& __m
<= __n
,
351 "template argument substituting __m out of bounds");
352 static_assert(__r
<= __w
, "template argument substituting "
354 static_assert(__u
<= __w
, "template argument substituting "
356 static_assert(__s
<= __w
, "template argument substituting "
358 static_assert(__t
<= __w
, "template argument substituting "
360 static_assert(__l
<= __w
, "template argument substituting "
362 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
363 "template argument substituting __w out of bound");
364 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
365 "template argument substituting __a out of bound");
366 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
367 "template argument substituting __b out of bound");
368 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
369 "template argument substituting __c out of bound");
370 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
371 "template argument substituting __d out of bound");
372 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
373 "template argument substituting __f out of bound");
376 /** The type of the generated random value. */
377 typedef _UIntType result_type
;
380 static const size_t word_size
= __w
;
381 static const size_t state_size
= __n
;
382 static const size_t shift_size
= __m
;
383 static const size_t mask_bits
= __r
;
384 static const result_type xor_mask
= __a
;
385 static const size_t tempering_u
= __u
;
386 static const result_type tempering_d
= __d
;
387 static const size_t tempering_s
= __s
;
388 static const result_type tempering_b
= __b
;
389 static const size_t tempering_t
= __t
;
390 static const result_type tempering_c
= __c
;
391 static const size_t tempering_l
= __l
;
392 static const result_type initialization_multiplier
= __f
;
393 static const result_type default_seed
= 5489u;
395 // constructors and member function
397 mersenne_twister_engine(result_type __sd
= default_seed
)
401 * @brief Constructs a %mersenne_twister_engine random number generator
402 * engine seeded from the seed sequence @p __q.
404 * @param __q the seed sequence.
406 template<typename _Sseq
, typename
= typename
407 std::enable_if
<!std::is_same
<_Sseq
, mersenne_twister_engine
>::value
>
410 mersenne_twister_engine(_Sseq
& __q
)
414 seed(result_type __sd
= default_seed
);
416 template<typename _Sseq
>
417 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
421 * @brief Gets the smallest possible value in the output range.
423 * @todo This should be constexpr.
430 * @brief Gets the largest possible value in the output range.
432 * @todo This should be constexpr.
436 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
439 * @brief Discard a sequence of random numbers.
441 * @todo Look for a faster way to do discard.
444 discard(unsigned long long __z
)
446 for (; __z
!= 0ULL; --__z
)
454 * @brief Compares two % mersenne_twister_engine random number generator
455 * objects of the same type for equality.
457 * @param __lhs A % mersenne_twister_engine random number generator
459 * @param __rhs Another % mersenne_twister_engine random number
462 * @returns true if the two objects are equal, false otherwise.
465 operator==(const mersenne_twister_engine
& __lhs
,
466 const mersenne_twister_engine
& __rhs
)
467 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
470 * @brief Inserts the current state of a % mersenne_twister_engine
471 * random number generator engine @p __x into the output stream
474 * @param __os An output stream.
475 * @param __x A % mersenne_twister_engine random number generator
478 * @returns The output stream with the state of @p __x inserted or in
481 template<typename _UIntType1
,
482 size_t __w1
, size_t __n1
,
483 size_t __m1
, size_t __r1
,
484 _UIntType1 __a1
, size_t __u1
,
485 _UIntType1 __d1
, size_t __s1
,
486 _UIntType1 __b1
, size_t __t1
,
487 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
488 typename _CharT
, typename _Traits
>
489 friend std::basic_ostream
<_CharT
, _Traits
>&
490 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
491 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
492 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
496 * @brief Extracts the current state of a % mersenne_twister_engine
497 * random number generator engine @p __x from the input stream
500 * @param __is An input stream.
501 * @param __x A % mersenne_twister_engine random number generator
504 * @returns The input stream with the state of @p __x extracted or in
507 template<typename _UIntType1
,
508 size_t __w1
, size_t __n1
,
509 size_t __m1
, size_t __r1
,
510 _UIntType1 __a1
, size_t __u1
,
511 _UIntType1 __d1
, size_t __s1
,
512 _UIntType1 __b1
, size_t __t1
,
513 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
514 typename _CharT
, typename _Traits
>
515 friend std::basic_istream
<_CharT
, _Traits
>&
516 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
517 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
518 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
522 _UIntType _M_x
[state_size
];
527 * @brief The Marsaglia-Zaman generator.
529 * This is a model of a Generalized Fibonacci discrete random number
530 * generator, sometimes referred to as the SWC generator.
532 * A discrete random number generator that produces pseudorandom
535 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
538 * The size of the state is @f$r@f$
539 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
541 * @var _M_x The state of the generator. This is a ring buffer.
542 * @var _M_carry The carry.
543 * @var _M_p Current index of x(i - r).
545 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
546 class subtract_with_carry_engine
548 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
549 "substituting _UIntType not an unsigned integral type");
550 static_assert(0u < __s
&& __s
< __r
,
551 "template argument substituting __s out of bounds");
552 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
553 "template argument substituting __w out of bounds");
556 /** The type of the generated random value. */
557 typedef _UIntType result_type
;
560 static const size_t word_size
= __w
;
561 static const size_t short_lag
= __s
;
562 static const size_t long_lag
= __r
;
563 static const result_type default_seed
= 19780503u;
566 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
567 * random number generator.
570 subtract_with_carry_engine(result_type __sd
= default_seed
)
574 * @brief Constructs a %subtract_with_carry_engine random number engine
575 * seeded from the seed sequence @p __q.
577 * @param __q the seed sequence.
579 template<typename _Sseq
, typename
= typename
580 std::enable_if
<!std::is_same
<_Sseq
, subtract_with_carry_engine
>::value
>
583 subtract_with_carry_engine(_Sseq
& __q
)
587 * @brief Seeds the initial state @f$x_0@f$ of the random number
590 * N1688[4.19] modifies this as follows. If @p __value == 0,
591 * sets value to 19780503. In any case, with a linear
592 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
593 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
594 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
595 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
596 * set carry to 1, otherwise sets carry to 0.
599 seed(result_type __sd
= default_seed
);
602 * @brief Seeds the initial state @f$x_0@f$ of the
603 * % subtract_with_carry_engine random number generator.
605 template<typename _Sseq
>
606 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
610 * @brief Gets the inclusive minimum value of the range of random
611 * integers returned by this generator.
613 * @todo This should be constexpr.
620 * @brief Gets the inclusive maximum value of the range of random
621 * integers returned by this generator.
623 * @todo This should be constexpr.
627 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
630 * @brief Discard a sequence of random numbers.
632 * @todo Look for a faster way to do discard.
635 discard(unsigned long long __z
)
637 for (; __z
!= 0ULL; --__z
)
642 * @brief Gets the next random number in the sequence.
648 * @brief Compares two % subtract_with_carry_engine random number
649 * generator objects of the same type for equality.
651 * @param __lhs A % subtract_with_carry_engine random number generator
653 * @param __rhs Another % subtract_with_carry_engine random number
656 * @returns true if the two objects are equal, false otherwise.
659 operator==(const subtract_with_carry_engine
& __lhs
,
660 const subtract_with_carry_engine
& __rhs
)
661 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
664 * @brief Inserts the current state of a % subtract_with_carry_engine
665 * random number generator engine @p __x into the output stream
668 * @param __os An output stream.
669 * @param __x A % subtract_with_carry_engine random number generator
672 * @returns The output stream with the state of @p __x inserted or in
675 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
676 typename _CharT
, typename _Traits
>
677 friend std::basic_ostream
<_CharT
, _Traits
>&
678 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
679 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
683 * @brief Extracts the current state of a % subtract_with_carry_engine
684 * random number generator engine @p __x from the input stream
687 * @param __is An input stream.
688 * @param __x A % subtract_with_carry_engine random number generator
691 * @returns The input stream with the state of @p __x extracted or in
694 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
695 typename _CharT
, typename _Traits
>
696 friend std::basic_istream
<_CharT
, _Traits
>&
697 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
698 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
702 _UIntType _M_x
[long_lag
];
708 * Produces random numbers from some base engine by discarding blocks of
711 * 0 <= @p __r <= @p __p
713 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
714 class discard_block_engine
716 static_assert(1 <= __r
&& __r
<= __p
,
717 "template argument substituting __r out of bounds");
720 /** The type of the generated random value. */
721 typedef typename
_RandomNumberEngine::result_type result_type
;
724 static const size_t block_size
= __p
;
725 static const size_t used_block
= __r
;
728 * @brief Constructs a default %discard_block_engine engine.
730 * The underlying engine is default constructed as well.
732 discard_block_engine()
733 : _M_b(), _M_n(0) { }
736 * @brief Copy constructs a %discard_block_engine engine.
738 * Copies an existing base class random number generator.
739 * @param rng An existing (base class) engine object.
742 discard_block_engine(const _RandomNumberEngine
& __rne
)
743 : _M_b(__rne
), _M_n(0) { }
746 * @brief Move constructs a %discard_block_engine engine.
748 * Copies an existing base class random number generator.
749 * @param rng An existing (base class) engine object.
752 discard_block_engine(_RandomNumberEngine
&& __rne
)
753 : _M_b(std::move(__rne
)), _M_n(0) { }
756 * @brief Seed constructs a %discard_block_engine engine.
758 * Constructs the underlying generator engine seeded with @p __s.
759 * @param __s A seed value for the base class engine.
762 discard_block_engine(result_type __s
)
763 : _M_b(__s
), _M_n(0) { }
766 * @brief Generator construct a %discard_block_engine engine.
768 * @param __q A seed sequence.
770 template<typename _Sseq
, typename
= typename
771 std::enable_if
<!std::is_same
<_Sseq
, discard_block_engine
>::value
772 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
775 discard_block_engine(_Sseq
& __q
)
780 * @brief Reseeds the %discard_block_engine object with the default
781 * seed for the underlying base class generator engine.
791 * @brief Reseeds the %discard_block_engine object with the default
792 * seed for the underlying base class generator engine.
795 seed(result_type __s
)
802 * @brief Reseeds the %discard_block_engine object with the given seed
804 * @param __q A seed generator function.
806 template<typename _Sseq
>
815 * @brief Gets a const reference to the underlying generator engine
818 const _RandomNumberEngine
&
823 * @brief Gets the minimum value in the generated random number range.
825 * @todo This should be constexpr.
829 { return _M_b
.min(); }
832 * @brief Gets the maximum value in the generated random number range.
834 * @todo This should be constexpr.
838 { return _M_b
.max(); }
841 * @brief Discard a sequence of random numbers.
843 * @todo Look for a faster way to do discard.
846 discard(unsigned long long __z
)
848 for (; __z
!= 0ULL; --__z
)
853 * @brief Gets the next value in the generated random number sequence.
859 * @brief Compares two %discard_block_engine random number generator
860 * objects of the same type for equality.
862 * @param __lhs A %discard_block_engine random number generator object.
863 * @param __rhs Another %discard_block_engine random number generator
866 * @returns true if the two objects are equal, false otherwise.
869 operator==(const discard_block_engine
& __lhs
,
870 const discard_block_engine
& __rhs
)
871 { return (__lhs
._M_b
== __rhs
._M_b
) && (__lhs
._M_n
== __rhs
._M_n
); }
874 * @brief Inserts the current state of a %discard_block_engine random
875 * number generator engine @p __x into the output stream
878 * @param __os An output stream.
879 * @param __x A %discard_block_engine random number generator engine.
881 * @returns The output stream with the state of @p __x inserted or in
884 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
885 typename _CharT
, typename _Traits
>
886 friend std::basic_ostream
<_CharT
, _Traits
>&
887 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
888 const std::discard_block_engine
<_RandomNumberEngine1
,
892 * @brief Extracts the current state of a % subtract_with_carry_engine
893 * random number generator engine @p __x from the input stream
896 * @param __is An input stream.
897 * @param __x A %discard_block_engine random number generator engine.
899 * @returns The input stream with the state of @p __x extracted or in
902 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
903 typename _CharT
, typename _Traits
>
904 friend std::basic_istream
<_CharT
, _Traits
>&
905 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
906 std::discard_block_engine
<_RandomNumberEngine1
,
910 _RandomNumberEngine _M_b
;
915 * Produces random numbers by combining random numbers from some base
916 * engine to produce random numbers with a specifies number of bits @p __w.
918 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
919 class independent_bits_engine
921 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
922 "substituting _UIntType not an unsigned integral type");
923 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
924 "template argument substituting __w out of bounds");
927 /** The type of the generated random value. */
928 typedef _UIntType result_type
;
931 * @brief Constructs a default %independent_bits_engine engine.
933 * The underlying engine is default constructed as well.
935 independent_bits_engine()
939 * @brief Copy constructs a %independent_bits_engine engine.
941 * Copies an existing base class random number generator.
942 * @param rng An existing (base class) engine object.
945 independent_bits_engine(const _RandomNumberEngine
& __rne
)
949 * @brief Move constructs a %independent_bits_engine engine.
951 * Copies an existing base class random number generator.
952 * @param rng An existing (base class) engine object.
955 independent_bits_engine(_RandomNumberEngine
&& __rne
)
956 : _M_b(std::move(__rne
)) { }
959 * @brief Seed constructs a %independent_bits_engine engine.
961 * Constructs the underlying generator engine seeded with @p __s.
962 * @param __s A seed value for the base class engine.
965 independent_bits_engine(result_type __s
)
969 * @brief Generator construct a %independent_bits_engine engine.
971 * @param __q A seed sequence.
973 template<typename _Sseq
, typename
= typename
974 std::enable_if
<!std::is_same
<_Sseq
, independent_bits_engine
>::value
975 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
978 independent_bits_engine(_Sseq
& __q
)
983 * @brief Reseeds the %independent_bits_engine object with the default
984 * seed for the underlying base class generator engine.
991 * @brief Reseeds the %independent_bits_engine object with the default
992 * seed for the underlying base class generator engine.
995 seed(result_type __s
)
999 * @brief Reseeds the %independent_bits_engine object with the given
1001 * @param __q A seed generator function.
1003 template<typename _Sseq
>
1009 * @brief Gets a const reference to the underlying generator engine
1012 const _RandomNumberEngine
&
1017 * @brief Gets the minimum value in the generated random number range.
1019 * @todo This should be constexpr.
1026 * @brief Gets the maximum value in the generated random number range.
1028 * @todo This should be constexpr.
1032 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1035 * @brief Discard a sequence of random numbers.
1037 * @todo Look for a faster way to do discard.
1040 discard(unsigned long long __z
)
1042 for (; __z
!= 0ULL; --__z
)
1047 * @brief Gets the next value in the generated random number sequence.
1053 * @brief Compares two %independent_bits_engine random number generator
1054 * objects of the same type for equality.
1056 * @param __lhs A %independent_bits_engine random number generator
1058 * @param __rhs Another %independent_bits_engine random number generator
1061 * @returns true if the two objects are equal, false otherwise.
1064 operator==(const independent_bits_engine
& __lhs
,
1065 const independent_bits_engine
& __rhs
)
1066 { return __lhs
._M_b
== __rhs
._M_b
; }
1069 * @brief Extracts the current state of a % subtract_with_carry_engine
1070 * random number generator engine @p __x from the input stream
1073 * @param __is An input stream.
1074 * @param __x A %independent_bits_engine random number generator
1077 * @returns The input stream with the state of @p __x extracted or in
1080 template<typename _CharT
, typename _Traits
>
1081 friend std::basic_istream
<_CharT
, _Traits
>&
1082 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1083 std::independent_bits_engine
<_RandomNumberEngine
,
1084 __w
, _UIntType
>& __x
)
1091 _RandomNumberEngine _M_b
;
1095 * @brief Inserts the current state of a %independent_bits_engine random
1096 * number generator engine @p __x into the output stream @p __os.
1098 * @param __os An output stream.
1099 * @param __x A %independent_bits_engine random number generator engine.
1101 * @returns The output stream with the state of @p __x inserted or in
1104 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1105 typename _CharT
, typename _Traits
>
1106 std::basic_ostream
<_CharT
, _Traits
>&
1107 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1108 const std::independent_bits_engine
<_RandomNumberEngine
,
1109 __w
, _UIntType
>& __x
)
1116 * @brief Produces random numbers by combining random numbers from some
1117 * base engine to produce random numbers with a specifies number of bits
1120 template<typename _RandomNumberEngine
, size_t __k
>
1121 class shuffle_order_engine
1123 static_assert(1u <= __k
, "template argument substituting "
1124 "__k out of bound");
1127 /** The type of the generated random value. */
1128 typedef typename
_RandomNumberEngine::result_type result_type
;
1130 static const size_t table_size
= __k
;
1133 * @brief Constructs a default %shuffle_order_engine engine.
1135 * The underlying engine is default constructed as well.
1137 shuffle_order_engine()
1139 { _M_initialize(); }
1142 * @brief Copy constructs a %shuffle_order_engine engine.
1144 * Copies an existing base class random number generator.
1145 * @param rng An existing (base class) engine object.
1148 shuffle_order_engine(const _RandomNumberEngine
& __rne
)
1150 { _M_initialize(); }
1153 * @brief Move constructs a %shuffle_order_engine engine.
1155 * Copies an existing base class random number generator.
1156 * @param rng An existing (base class) engine object.
1159 shuffle_order_engine(_RandomNumberEngine
&& __rne
)
1160 : _M_b(std::move(__rne
))
1161 { _M_initialize(); }
1164 * @brief Seed constructs a %shuffle_order_engine engine.
1166 * Constructs the underlying generator engine seeded with @p __s.
1167 * @param __s A seed value for the base class engine.
1170 shuffle_order_engine(result_type __s
)
1172 { _M_initialize(); }
1175 * @brief Generator construct a %shuffle_order_engine engine.
1177 * @param __q A seed sequence.
1179 template<typename _Sseq
, typename
= typename
1180 std::enable_if
<!std::is_same
<_Sseq
, shuffle_order_engine
>::value
1181 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1184 shuffle_order_engine(_Sseq
& __q
)
1186 { _M_initialize(); }
1189 * @brief Reseeds the %shuffle_order_engine object with the default seed
1190 for the underlying base class generator engine.
1200 * @brief Reseeds the %shuffle_order_engine object with the default seed
1201 * for the underlying base class generator engine.
1204 seed(result_type __s
)
1211 * @brief Reseeds the %shuffle_order_engine object with the given seed
1213 * @param __q A seed generator function.
1215 template<typename _Sseq
>
1224 * Gets a const reference to the underlying generator engine object.
1226 const _RandomNumberEngine
&
1231 * Gets the minimum value in the generated random number range.
1233 * @todo This should be constexpr.
1237 { return _M_b
.min(); }
1240 * Gets the maximum value in the generated random number range.
1242 * @todo This should be constexpr.
1246 { return _M_b
.max(); }
1249 * Discard a sequence of random numbers.
1251 * @todo Look for a faster way to do discard.
1254 discard(unsigned long long __z
)
1256 for (; __z
!= 0ULL; --__z
)
1261 * Gets the next value in the generated random number sequence.
1267 * Compares two %shuffle_order_engine random number generator objects
1268 * of the same type for equality.
1270 * @param __lhs A %shuffle_order_engine random number generator object.
1271 * @param __rhs Another %shuffle_order_engine random number generator
1274 * @returns true if the two objects are equal, false otherwise.
1277 operator==(const shuffle_order_engine
& __lhs
,
1278 const shuffle_order_engine
& __rhs
)
1279 { return __lhs
._M_b
== __rhs
._M_b
; }
1282 * @brief Inserts the current state of a %shuffle_order_engine random
1283 * number generator engine @p __x into the output stream
1286 * @param __os An output stream.
1287 * @param __x A %shuffle_order_engine random number generator engine.
1289 * @returns The output stream with the state of @p __x inserted or in
1292 template<typename _RandomNumberEngine1
, size_t __k1
,
1293 typename _CharT
, typename _Traits
>
1294 friend std::basic_ostream
<_CharT
, _Traits
>&
1295 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1296 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1300 * @brief Extracts the current state of a % subtract_with_carry_engine
1301 * random number generator engine @p __x from the input stream
1304 * @param __is An input stream.
1305 * @param __x A %shuffle_order_engine random number generator engine.
1307 * @returns The input stream with the state of @p __x extracted or in
1310 template<typename _RandomNumberEngine1
, size_t __k1
,
1311 typename _CharT
, typename _Traits
>
1312 friend std::basic_istream
<_CharT
, _Traits
>&
1313 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1314 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>&);
1317 void _M_initialize()
1319 for (size_t __i
= 0; __i
< __k
; ++__i
)
1324 _RandomNumberEngine _M_b
;
1325 result_type _M_v
[__k
];
1330 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1332 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1336 * An alternative LCR (Lehmer Generator function).
1338 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1342 * The classic Mersenne Twister.
1345 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1346 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1347 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1349 typedef mersenne_twister_engine
<
1355 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1358 * An alternative Mersenne Twister.
1360 typedef mersenne_twister_engine
<
1363 0xb5026f5aa96619e9ULL
, 29,
1364 0x5555555555555555ULL
, 17,
1365 0x71d67fffeda60000ULL
, 37,
1366 0xfff7eee000000000ULL
, 43,
1367 6364136223846793005ULL> mt19937_64
;
1369 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1372 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1375 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1377 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1379 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1381 typedef minstd_rand0 default_random_engine
;
1384 * A standard interface to a platform-specific non-deterministic
1385 * random number generator (if any are available).
1390 /** The type of the generated random value. */
1391 typedef unsigned int result_type
;
1393 // constructors, destructors and member functions
1395 #ifdef _GLIBCXX_USE_RANDOM_TR1
1398 random_device(const std::string
& __token
= "/dev/urandom")
1400 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1401 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1402 std::__throw_runtime_error(__N("random_device::"
1403 "random_device(const std::string&)"));
1407 { std::fclose(_M_file
); }
1412 random_device(const std::string
& __token
= "mt19937")
1413 : _M_mt(_M_strtoul(__token
)) { }
1416 static unsigned long
1417 _M_strtoul(const std::string
& __str
)
1419 unsigned long __ret
= 5489UL;
1420 if (__str
!= "mt19937")
1422 const char* __nptr
= __str
.c_str();
1424 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1425 if (*__nptr
== '\0' || *__endptr
!= '\0')
1426 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1427 "(const std::string&)"));
1438 { return std::numeric_limits
<result_type
>::min(); }
1442 { return std::numeric_limits
<result_type
>::max(); }
1451 #ifdef _GLIBCXX_USE_RANDOM_TR1
1453 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1461 // No copy functions.
1462 random_device(const random_device
&) = delete;
1463 void operator=(const random_device
&) = delete;
1467 #ifdef _GLIBCXX_USE_RANDOM_TR1
1474 /* @} */ // group std_random_generators
1477 * @addtogroup std_random_distributions Random Number Distributions
1478 * @ingroup std_random
1483 * @addtogroup std_random_distributions_uniform Uniform Distributions
1484 * @ingroup std_random_distributions
1489 * @brief Uniform discrete distribution for random numbers.
1490 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1491 * probability throughout the range.
1493 template<typename _IntType
= int>
1494 class uniform_int_distribution
1496 static_assert(std::is_integral
<_IntType
>::value
,
1497 "template argument not an integral type");
1500 /** The type of the range of the distribution. */
1501 typedef _IntType result_type
;
1502 /** Parameter type. */
1505 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1508 param_type(_IntType __a
= 0,
1509 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1510 : _M_a(__a
), _M_b(__b
)
1512 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1530 * @brief Constructs a uniform distribution object.
1533 uniform_int_distribution(_IntType __a
= 0,
1534 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1535 : _M_param(__a
, __b
)
1539 uniform_int_distribution(const param_type
& __p
)
1544 * @brief Resets the distribution state.
1546 * Does nothing for the uniform integer distribution.
1553 { return _M_param
.a(); }
1557 { return _M_param
.b(); }
1560 * @brief Returns the inclusive lower bound of the distribution range.
1564 { return this->a(); }
1567 * @brief Returns the inclusive upper bound of the distribution range.
1571 { return this->b(); }
1574 * @brief Returns the parameter set of the distribution.
1578 { return _M_param
; }
1581 * @brief Sets the parameter set of the distribution.
1582 * @param __param The new parameter set of the distribution.
1585 param(const param_type
& __param
)
1586 { _M_param
= __param
; }
1589 * Gets a uniformly distributed random number in the range
1592 template<typename _UniformRandomNumberGenerator
>
1594 operator()(_UniformRandomNumberGenerator
& __urng
)
1595 { return this->operator()(__urng
, this->param()); }
1598 * Gets a uniform random number in the range @f$[0, n)@f$.
1600 * This function is aimed at use with std::random_shuffle.
1602 template<typename _UniformRandomNumberGenerator
>
1604 operator()(_UniformRandomNumberGenerator
& __urng
,
1605 const param_type
& __p
);
1607 param_type _M_param
;
1611 * @brief Inserts a %uniform_int_distribution random number
1612 * distribution @p __x into the output stream @p os.
1614 * @param __os An output stream.
1615 * @param __x A %uniform_int_distribution random number distribution.
1617 * @returns The output stream with the state of @p __x inserted or in
1620 template<typename _IntType
, typename _CharT
, typename _Traits
>
1621 std::basic_ostream
<_CharT
, _Traits
>&
1622 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1623 const std::uniform_int_distribution
<_IntType
>&);
1626 * @brief Extracts a %uniform_int_distribution random number distribution
1627 * @p __x from the input stream @p __is.
1629 * @param __is An input stream.
1630 * @param __x A %uniform_int_distribution random number generator engine.
1632 * @returns The input stream with @p __x extracted or in an error state.
1634 template<typename _IntType
, typename _CharT
, typename _Traits
>
1635 std::basic_istream
<_CharT
, _Traits
>&
1636 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1637 std::uniform_int_distribution
<_IntType
>&);
1641 * @brief Uniform continuous distribution for random numbers.
1643 * A continuous random distribution on the range [min, max) with equal
1644 * probability throughout the range. The URNG should be real-valued and
1645 * deliver number in the range [0, 1).
1647 template<typename _RealType
= double>
1648 class uniform_real_distribution
1650 static_assert(std::is_floating_point
<_RealType
>::value
,
1651 "template argument not a floating point type");
1654 /** The type of the range of the distribution. */
1655 typedef _RealType result_type
;
1656 /** Parameter type. */
1659 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1662 param_type(_RealType __a
= _RealType(0),
1663 _RealType __b
= _RealType(1))
1664 : _M_a(__a
), _M_b(__b
)
1666 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1684 * @brief Constructs a uniform_real_distribution object.
1686 * @param __min [IN] The lower bound of the distribution.
1687 * @param __max [IN] The upper bound of the distribution.
1690 uniform_real_distribution(_RealType __a
= _RealType(0),
1691 _RealType __b
= _RealType(1))
1692 : _M_param(__a
, __b
)
1696 uniform_real_distribution(const param_type
& __p
)
1701 * @brief Resets the distribution state.
1703 * Does nothing for the uniform real distribution.
1710 { return _M_param
.a(); }
1714 { return _M_param
.b(); }
1717 * @brief Returns the inclusive lower bound of the distribution range.
1721 { return this->a(); }
1724 * @brief Returns the inclusive upper bound of the distribution range.
1728 { return this->b(); }
1731 * @brief Returns the parameter set of the distribution.
1735 { return _M_param
; }
1738 * @brief Sets the parameter set of the distribution.
1739 * @param __param The new parameter set of the distribution.
1742 param(const param_type
& __param
)
1743 { _M_param
= __param
; }
1745 template<typename _UniformRandomNumberGenerator
>
1747 operator()(_UniformRandomNumberGenerator
& __urng
)
1748 { return this->operator()(__urng
, this->param()); }
1750 template<typename _UniformRandomNumberGenerator
>
1752 operator()(_UniformRandomNumberGenerator
& __urng
,
1753 const param_type
& __p
)
1755 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1757 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1761 param_type _M_param
;
1765 * @brief Inserts a %uniform_real_distribution random number
1766 * distribution @p __x into the output stream @p __os.
1768 * @param __os An output stream.
1769 * @param __x A %uniform_real_distribution random number distribution.
1771 * @returns The output stream with the state of @p __x inserted or in
1774 template<typename _RealType
, typename _CharT
, typename _Traits
>
1775 std::basic_ostream
<_CharT
, _Traits
>&
1776 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1777 const std::uniform_real_distribution
<_RealType
>&);
1780 * @brief Extracts a %uniform_real_distribution random number distribution
1781 * @p __x from the input stream @p __is.
1783 * @param __is An input stream.
1784 * @param __x A %uniform_real_distribution random number generator engine.
1786 * @returns The input stream with @p __x extracted or in an error state.
1788 template<typename _RealType
, typename _CharT
, typename _Traits
>
1789 std::basic_istream
<_CharT
, _Traits
>&
1790 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1791 std::uniform_real_distribution
<_RealType
>&);
1793 /* @} */ // group std_random_distributions_uniform
1796 * @addtogroup std_random_distributions_normal Normal Distributions
1797 * @ingroup std_random_distributions
1802 * @brief A normal continuous distribution for random numbers.
1804 * The formula for the normal probability density function is
1806 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1807 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1810 template<typename _RealType
= double>
1811 class normal_distribution
1813 static_assert(std::is_floating_point
<_RealType
>::value
,
1814 "template argument not a floating point type");
1817 /** The type of the range of the distribution. */
1818 typedef _RealType result_type
;
1819 /** Parameter type. */
1822 typedef normal_distribution
<_RealType
> distribution_type
;
1825 param_type(_RealType __mean
= _RealType(0),
1826 _RealType __stddev
= _RealType(1))
1827 : _M_mean(__mean
), _M_stddev(__stddev
)
1829 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
1838 { return _M_stddev
; }
1842 _RealType _M_stddev
;
1847 * Constructs a normal distribution with parameters @f$mean@f$ and
1848 * standard deviation.
1851 normal_distribution(result_type __mean
= result_type(0),
1852 result_type __stddev
= result_type(1))
1853 : _M_param(__mean
, __stddev
), _M_saved_available(false)
1857 normal_distribution(const param_type
& __p
)
1858 : _M_param(__p
), _M_saved_available(false)
1862 * @brief Resets the distribution state.
1866 { _M_saved_available
= false; }
1869 * @brief Returns the mean of the distribution.
1873 { return _M_param
.mean(); }
1876 * @brief Returns the standard deviation of the distribution.
1880 { return _M_param
.stddev(); }
1883 * @brief Returns the parameter set of the distribution.
1887 { return _M_param
; }
1890 * @brief Sets the parameter set of the distribution.
1891 * @param __param The new parameter set of the distribution.
1894 param(const param_type
& __param
)
1895 { _M_param
= __param
; }
1898 * @brief Returns the greatest lower bound value of the distribution.
1902 { return std::numeric_limits
<result_type
>::min(); }
1905 * @brief Returns the least upper bound value of the distribution.
1909 { return std::numeric_limits
<result_type
>::max(); }
1911 template<typename _UniformRandomNumberGenerator
>
1913 operator()(_UniformRandomNumberGenerator
& __urng
)
1914 { return this->operator()(__urng
, this->param()); }
1916 template<typename _UniformRandomNumberGenerator
>
1918 operator()(_UniformRandomNumberGenerator
& __urng
,
1919 const param_type
& __p
);
1922 * @brief Inserts a %normal_distribution random number distribution
1923 * @p __x into the output stream @p __os.
1925 * @param __os An output stream.
1926 * @param __x A %normal_distribution random number distribution.
1928 * @returns The output stream with the state of @p __x inserted or in
1931 template<typename _RealType1
, typename _CharT
, typename _Traits
>
1932 friend std::basic_ostream
<_CharT
, _Traits
>&
1933 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1934 const std::normal_distribution
<_RealType1
>&);
1937 * @brief Extracts a %normal_distribution random number distribution
1938 * @p __x from the input stream @p __is.
1940 * @param __is An input stream.
1941 * @param __x A %normal_distribution random number generator engine.
1943 * @returns The input stream with @p __x extracted or in an error
1946 template<typename _RealType1
, typename _CharT
, typename _Traits
>
1947 friend std::basic_istream
<_CharT
, _Traits
>&
1948 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1949 std::normal_distribution
<_RealType1
>&);
1952 param_type _M_param
;
1953 result_type _M_saved
;
1954 bool _M_saved_available
;
1959 * @brief A lognormal_distribution random number distribution.
1961 * The formula for the normal probability mass function is
1963 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
1964 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
1967 template<typename _RealType
= double>
1968 class lognormal_distribution
1970 static_assert(std::is_floating_point
<_RealType
>::value
,
1971 "template argument not a floating point type");
1974 /** The type of the range of the distribution. */
1975 typedef _RealType result_type
;
1976 /** Parameter type. */
1979 typedef lognormal_distribution
<_RealType
> distribution_type
;
1982 param_type(_RealType __m
= _RealType(0),
1983 _RealType __s
= _RealType(1))
1984 : _M_m(__m
), _M_s(__s
)
2001 lognormal_distribution(_RealType __m
= _RealType(0),
2002 _RealType __s
= _RealType(1))
2003 : _M_param(__m
, __s
), _M_nd()
2007 lognormal_distribution(const param_type
& __p
)
2008 : _M_param(__p
), _M_nd()
2012 * Resets the distribution state.
2023 { return _M_param
.m(); }
2027 { return _M_param
.s(); }
2030 * @brief Returns the parameter set of the distribution.
2034 { return _M_param
; }
2037 * @brief Sets the parameter set of the distribution.
2038 * @param __param The new parameter set of the distribution.
2041 param(const param_type
& __param
)
2042 { _M_param
= __param
; }
2045 * @brief Returns the greatest lower bound value of the distribution.
2049 { return result_type(0); }
2052 * @brief Returns the least upper bound value of the distribution.
2056 { return std::numeric_limits
<result_type
>::max(); }
2058 template<typename _UniformRandomNumberGenerator
>
2060 operator()(_UniformRandomNumberGenerator
& __urng
)
2061 { return this->operator()(__urng
, this->param()); }
2063 template<typename _UniformRandomNumberGenerator
>
2065 operator()(_UniformRandomNumberGenerator
& __urng
,
2066 const param_type
& __p
)
2067 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2070 * @brief Inserts a %lognormal_distribution random number distribution
2071 * @p __x into the output stream @p __os.
2073 * @param __os An output stream.
2074 * @param __x A %lognormal_distribution random number distribution.
2076 * @returns The output stream with the state of @p __x inserted or in
2079 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2080 friend std::basic_ostream
<_CharT
, _Traits
>&
2081 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2082 const std::lognormal_distribution
<_RealType1
>&);
2085 * @brief Extracts a %lognormal_distribution random number distribution
2086 * @p __x from the input stream @p __is.
2088 * @param __is An input stream.
2089 * @param __x A %lognormal_distribution random number
2092 * @returns The input stream with @p __x extracted or in an error state.
2094 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2095 friend std::basic_istream
<_CharT
, _Traits
>&
2096 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2097 std::lognormal_distribution
<_RealType1
>&);
2100 param_type _M_param
;
2102 std::normal_distribution
<result_type
> _M_nd
;
2107 * @brief A gamma continuous distribution for random numbers.
2109 * The formula for the gamma probability density function is:
2111 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2112 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2115 template<typename _RealType
= double>
2116 class gamma_distribution
2118 static_assert(std::is_floating_point
<_RealType
>::value
,
2119 "template argument not a floating point type");
2122 /** The type of the range of the distribution. */
2123 typedef _RealType result_type
;
2124 /** Parameter type. */
2127 typedef gamma_distribution
<_RealType
> distribution_type
;
2128 friend class gamma_distribution
<_RealType
>;
2131 param_type(_RealType __alpha_val
= _RealType(1),
2132 _RealType __beta_val
= _RealType(1))
2133 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2135 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2141 { return _M_alpha
; }
2154 _RealType _M_malpha
, _M_a2
;
2159 * @brief Constructs a gamma distribution with parameters
2160 * @f$\alpha@f$ and @f$\beta@f$.
2163 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2164 _RealType __beta_val
= _RealType(1))
2165 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2169 gamma_distribution(const param_type
& __p
)
2170 : _M_param(__p
), _M_nd()
2174 * @brief Resets the distribution state.
2181 * @brief Returns the @f$\alpha@f$ of the distribution.
2185 { return _M_param
.alpha(); }
2188 * @brief Returns the @f$\beta@f$ of the distribution.
2192 { return _M_param
.beta(); }
2195 * @brief Returns the parameter set of the distribution.
2199 { return _M_param
; }
2202 * @brief Sets the parameter set of the distribution.
2203 * @param __param The new parameter set of the distribution.
2206 param(const param_type
& __param
)
2207 { _M_param
= __param
; }
2210 * @brief Returns the greatest lower bound value of the distribution.
2214 { return result_type(0); }
2217 * @brief Returns the least upper bound value of the distribution.
2221 { return std::numeric_limits
<result_type
>::max(); }
2223 template<typename _UniformRandomNumberGenerator
>
2225 operator()(_UniformRandomNumberGenerator
& __urng
)
2226 { return this->operator()(__urng
, this->param()); }
2228 template<typename _UniformRandomNumberGenerator
>
2230 operator()(_UniformRandomNumberGenerator
& __urng
,
2231 const param_type
& __p
);
2234 * @brief Inserts a %gamma_distribution random number distribution
2235 * @p __x into the output stream @p __os.
2237 * @param __os An output stream.
2238 * @param __x A %gamma_distribution random number distribution.
2240 * @returns The output stream with the state of @p __x inserted or in
2243 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2244 friend std::basic_ostream
<_CharT
, _Traits
>&
2245 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2246 const std::gamma_distribution
<_RealType1
>&);
2249 * @brief Extracts a %gamma_distribution random number distribution
2250 * @p __x from the input stream @p __is.
2252 * @param __is An input stream.
2253 * @param __x A %gamma_distribution random number generator engine.
2255 * @returns The input stream with @p __x extracted or in an error state.
2257 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2258 friend std::basic_istream
<_CharT
, _Traits
>&
2259 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2260 std::gamma_distribution
<_RealType1
>&);
2263 param_type _M_param
;
2265 std::normal_distribution
<result_type
> _M_nd
;
2270 * @brief A chi_squared_distribution random number distribution.
2272 * The formula for the normal probability mass function is
2273 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2275 template<typename _RealType
= double>
2276 class chi_squared_distribution
2278 static_assert(std::is_floating_point
<_RealType
>::value
,
2279 "template argument not a floating point type");
2282 /** The type of the range of the distribution. */
2283 typedef _RealType result_type
;
2284 /** Parameter type. */
2287 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2290 param_type(_RealType __n
= _RealType(1))
2303 chi_squared_distribution(_RealType __n
= _RealType(1))
2304 : _M_param(__n
), _M_gd(__n
/ 2)
2308 chi_squared_distribution(const param_type
& __p
)
2309 : _M_param(__p
), _M_gd(__p
.n() / 2)
2313 * @brief Resets the distribution state.
2324 { return _M_param
.n(); }
2327 * @brief Returns the parameter set of the distribution.
2331 { return _M_param
; }
2334 * @brief Sets the parameter set of the distribution.
2335 * @param __param The new parameter set of the distribution.
2338 param(const param_type
& __param
)
2339 { _M_param
= __param
; }
2342 * @brief Returns the greatest lower bound value of the distribution.
2346 { return result_type(0); }
2349 * @brief Returns the least upper bound value of the distribution.
2353 { return std::numeric_limits
<result_type
>::max(); }
2355 template<typename _UniformRandomNumberGenerator
>
2357 operator()(_UniformRandomNumberGenerator
& __urng
)
2358 { return 2 * _M_gd(__urng
); }
2360 template<typename _UniformRandomNumberGenerator
>
2362 operator()(_UniformRandomNumberGenerator
& __urng
,
2363 const param_type
& __p
)
2365 typedef typename
std::gamma_distribution
<result_type
>::param_type
2367 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2371 * @brief Inserts a %chi_squared_distribution random number distribution
2372 * @p __x into the output stream @p __os.
2374 * @param __os An output stream.
2375 * @param __x A %chi_squared_distribution random number distribution.
2377 * @returns The output stream with the state of @p __x inserted or in
2380 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2381 friend std::basic_ostream
<_CharT
, _Traits
>&
2382 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2383 const std::chi_squared_distribution
<_RealType1
>&);
2386 * @brief Extracts a %chi_squared_distribution random number distribution
2387 * @p __x from the input stream @p __is.
2389 * @param __is An input stream.
2390 * @param __x A %chi_squared_distribution random number
2393 * @returns The input stream with @p __x extracted or in an error state.
2395 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2396 friend std::basic_istream
<_CharT
, _Traits
>&
2397 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2398 std::chi_squared_distribution
<_RealType1
>&);
2401 param_type _M_param
;
2403 std::gamma_distribution
<result_type
> _M_gd
;
2408 * @brief A cauchy_distribution random number distribution.
2410 * The formula for the normal probability mass function is
2411 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2413 template<typename _RealType
= double>
2414 class cauchy_distribution
2416 static_assert(std::is_floating_point
<_RealType
>::value
,
2417 "template argument not a floating point type");
2420 /** The type of the range of the distribution. */
2421 typedef _RealType result_type
;
2422 /** Parameter type. */
2425 typedef cauchy_distribution
<_RealType
> distribution_type
;
2428 param_type(_RealType __a
= _RealType(0),
2429 _RealType __b
= _RealType(1))
2430 : _M_a(__a
), _M_b(__b
)
2447 cauchy_distribution(_RealType __a
= _RealType(0),
2448 _RealType __b
= _RealType(1))
2449 : _M_param(__a
, __b
)
2453 cauchy_distribution(const param_type
& __p
)
2458 * @brief Resets the distribution state.
2469 { return _M_param
.a(); }
2473 { return _M_param
.b(); }
2476 * @brief Returns the parameter set of the distribution.
2480 { return _M_param
; }
2483 * @brief Sets the parameter set of the distribution.
2484 * @param __param The new parameter set of the distribution.
2487 param(const param_type
& __param
)
2488 { _M_param
= __param
; }
2491 * @brief Returns the greatest lower bound value of the distribution.
2495 { return std::numeric_limits
<result_type
>::min(); }
2498 * @brief Returns the least upper bound value of the distribution.
2502 { return std::numeric_limits
<result_type
>::max(); }
2504 template<typename _UniformRandomNumberGenerator
>
2506 operator()(_UniformRandomNumberGenerator
& __urng
)
2507 { return this->operator()(__urng
, this->param()); }
2509 template<typename _UniformRandomNumberGenerator
>
2511 operator()(_UniformRandomNumberGenerator
& __urng
,
2512 const param_type
& __p
);
2515 param_type _M_param
;
2519 * @brief Inserts a %cauchy_distribution random number distribution
2520 * @p __x into the output stream @p __os.
2522 * @param __os An output stream.
2523 * @param __x A %cauchy_distribution random number distribution.
2525 * @returns The output stream with the state of @p __x inserted or in
2528 template<typename _RealType
, typename _CharT
, typename _Traits
>
2529 std::basic_ostream
<_CharT
, _Traits
>&
2530 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2531 const std::cauchy_distribution
<_RealType
>&);
2534 * @brief Extracts a %cauchy_distribution random number distribution
2535 * @p __x from the input stream @p __is.
2537 * @param __is An input stream.
2538 * @param __x A %cauchy_distribution random number
2541 * @returns The input stream with @p __x extracted or in an error state.
2543 template<typename _RealType
, typename _CharT
, typename _Traits
>
2544 std::basic_istream
<_CharT
, _Traits
>&
2545 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2546 std::cauchy_distribution
<_RealType
>&);
2550 * @brief A fisher_f_distribution random number distribution.
2552 * The formula for the normal probability mass function is
2554 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2555 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2556 * (1 + \frac{mx}{n})^{-(m+n)/2}
2559 template<typename _RealType
= double>
2560 class fisher_f_distribution
2562 static_assert(std::is_floating_point
<_RealType
>::value
,
2563 "template argument not a floating point type");
2566 /** The type of the range of the distribution. */
2567 typedef _RealType result_type
;
2568 /** Parameter type. */
2571 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2574 param_type(_RealType __m
= _RealType(1),
2575 _RealType __n
= _RealType(1))
2576 : _M_m(__m
), _M_n(__n
)
2593 fisher_f_distribution(_RealType __m
= _RealType(1),
2594 _RealType __n
= _RealType(1))
2595 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2599 fisher_f_distribution(const param_type
& __p
)
2600 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2604 * @brief Resets the distribution state.
2618 { return _M_param
.m(); }
2622 { return _M_param
.n(); }
2625 * @brief Returns the parameter set of the distribution.
2629 { return _M_param
; }
2632 * @brief Sets the parameter set of the distribution.
2633 * @param __param The new parameter set of the distribution.
2636 param(const param_type
& __param
)
2637 { _M_param
= __param
; }
2640 * @brief Returns the greatest lower bound value of the distribution.
2644 { return result_type(0); }
2647 * @brief Returns the least upper bound value of the distribution.
2651 { return std::numeric_limits
<result_type
>::max(); }
2653 template<typename _UniformRandomNumberGenerator
>
2655 operator()(_UniformRandomNumberGenerator
& __urng
)
2656 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
2658 template<typename _UniformRandomNumberGenerator
>
2660 operator()(_UniformRandomNumberGenerator
& __urng
,
2661 const param_type
& __p
)
2663 typedef typename
std::gamma_distribution
<result_type
>::param_type
2665 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
2666 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
2670 * @brief Inserts a %fisher_f_distribution random number distribution
2671 * @p __x into the output stream @p __os.
2673 * @param __os An output stream.
2674 * @param __x A %fisher_f_distribution random number distribution.
2676 * @returns The output stream with the state of @p __x inserted or in
2679 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2680 friend std::basic_ostream
<_CharT
, _Traits
>&
2681 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2682 const std::fisher_f_distribution
<_RealType1
>&);
2685 * @brief Extracts a %fisher_f_distribution random number distribution
2686 * @p __x from the input stream @p __is.
2688 * @param __is An input stream.
2689 * @param __x A %fisher_f_distribution random number
2692 * @returns The input stream with @p __x extracted or in an error state.
2694 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2695 friend std::basic_istream
<_CharT
, _Traits
>&
2696 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2697 std::fisher_f_distribution
<_RealType1
>&);
2700 param_type _M_param
;
2702 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
2707 * @brief A student_t_distribution random number distribution.
2709 * The formula for the normal probability mass function is:
2711 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2712 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
2715 template<typename _RealType
= double>
2716 class student_t_distribution
2718 static_assert(std::is_floating_point
<_RealType
>::value
,
2719 "template argument not a floating point type");
2722 /** The type of the range of the distribution. */
2723 typedef _RealType result_type
;
2724 /** Parameter type. */
2727 typedef student_t_distribution
<_RealType
> distribution_type
;
2730 param_type(_RealType __n
= _RealType(1))
2743 student_t_distribution(_RealType __n
= _RealType(1))
2744 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
2748 student_t_distribution(const param_type
& __p
)
2749 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
2753 * @brief Resets the distribution state.
2767 { return _M_param
.n(); }
2770 * @brief Returns the parameter set of the distribution.
2774 { return _M_param
; }
2777 * @brief Sets the parameter set of the distribution.
2778 * @param __param The new parameter set of the distribution.
2781 param(const param_type
& __param
)
2782 { _M_param
= __param
; }
2785 * @brief Returns the greatest lower bound value of the distribution.
2789 { return std::numeric_limits
<result_type
>::min(); }
2792 * @brief Returns the least upper bound value of the distribution.
2796 { return std::numeric_limits
<result_type
>::max(); }
2798 template<typename _UniformRandomNumberGenerator
>
2800 operator()(_UniformRandomNumberGenerator
& __urng
)
2801 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
2803 template<typename _UniformRandomNumberGenerator
>
2805 operator()(_UniformRandomNumberGenerator
& __urng
,
2806 const param_type
& __p
)
2808 typedef typename
std::gamma_distribution
<result_type
>::param_type
2811 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
2812 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
2816 * @brief Inserts a %student_t_distribution random number distribution
2817 * @p __x into the output stream @p __os.
2819 * @param __os An output stream.
2820 * @param __x A %student_t_distribution random number distribution.
2822 * @returns The output stream with the state of @p __x inserted or in
2825 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2826 friend std::basic_ostream
<_CharT
, _Traits
>&
2827 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2828 const std::student_t_distribution
<_RealType1
>&);
2831 * @brief Extracts a %student_t_distribution random number distribution
2832 * @p __x from the input stream @p __is.
2834 * @param __is An input stream.
2835 * @param __x A %student_t_distribution random number
2838 * @returns The input stream with @p __x extracted or in an error state.
2840 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2841 friend std::basic_istream
<_CharT
, _Traits
>&
2842 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2843 std::student_t_distribution
<_RealType1
>&);
2846 param_type _M_param
;
2848 std::normal_distribution
<result_type
> _M_nd
;
2849 std::gamma_distribution
<result_type
> _M_gd
;
2852 /* @} */ // group std_random_distributions_normal
2855 * @addtogroup std_random_distributions_bernoulli Bernoulli Distributions
2856 * @ingroup std_random_distributions
2861 * @brief A Bernoulli random number distribution.
2863 * Generates a sequence of true and false values with likelihood @f$p@f$
2864 * that true will come up and @f$(1 - p)@f$ that false will appear.
2866 class bernoulli_distribution
2869 /** The type of the range of the distribution. */
2870 typedef bool result_type
;
2871 /** Parameter type. */
2874 typedef bernoulli_distribution distribution_type
;
2877 param_type(double __p
= 0.5)
2880 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
2893 * @brief Constructs a Bernoulli distribution with likelihood @p p.
2895 * @param __p [IN] The likelihood of a true result being returned.
2896 * Must be in the interval @f$[0, 1]@f$.
2899 bernoulli_distribution(double __p
= 0.5)
2904 bernoulli_distribution(const param_type
& __p
)
2909 * @brief Resets the distribution state.
2911 * Does nothing for a Bernoulli distribution.
2917 * @brief Returns the @p p parameter of the distribution.
2921 { return _M_param
.p(); }
2924 * @brief Returns the parameter set of the distribution.
2928 { return _M_param
; }
2931 * @brief Sets the parameter set of the distribution.
2932 * @param __param The new parameter set of the distribution.
2935 param(const param_type
& __param
)
2936 { _M_param
= __param
; }
2939 * @brief Returns the greatest lower bound value of the distribution.
2943 { return std::numeric_limits
<result_type
>::min(); }
2946 * @brief Returns the least upper bound value of the distribution.
2950 { return std::numeric_limits
<result_type
>::max(); }
2953 * @brief Returns the next value in the Bernoullian sequence.
2955 template<typename _UniformRandomNumberGenerator
>
2957 operator()(_UniformRandomNumberGenerator
& __urng
)
2958 { return this->operator()(__urng
, this->param()); }
2960 template<typename _UniformRandomNumberGenerator
>
2962 operator()(_UniformRandomNumberGenerator
& __urng
,
2963 const param_type
& __p
)
2965 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
2967 if ((__aurng() - __aurng
.min())
2968 < __p
.p() * (__aurng
.max() - __aurng
.min()))
2974 param_type _M_param
;
2978 * @brief Inserts a %bernoulli_distribution random number distribution
2979 * @p __x into the output stream @p __os.
2981 * @param __os An output stream.
2982 * @param __x A %bernoulli_distribution random number distribution.
2984 * @returns The output stream with the state of @p __x inserted or in
2987 template<typename _CharT
, typename _Traits
>
2988 std::basic_ostream
<_CharT
, _Traits
>&
2989 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2990 const std::bernoulli_distribution
&);
2993 * @brief Extracts a %bernoulli_distribution random number distribution
2994 * @p __x from the input stream @p __is.
2996 * @param __is An input stream.
2997 * @param __x A %bernoulli_distribution random number generator engine.
2999 * @returns The input stream with @p __x extracted or in an error state.
3001 template<typename _CharT
, typename _Traits
>
3002 std::basic_istream
<_CharT
, _Traits
>&
3003 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3004 std::bernoulli_distribution
& __x
)
3008 __x
.param(bernoulli_distribution::param_type(__p
));
3014 * @brief A discrete binomial random number distribution.
3016 * The formula for the binomial probability density function is
3017 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3018 * and @f$p@f$ are the parameters of the distribution.
3020 template<typename _IntType
= int>
3021 class binomial_distribution
3023 static_assert(std::is_integral
<_IntType
>::value
,
3024 "template argument not an integral type");
3027 /** The type of the range of the distribution. */
3028 typedef _IntType result_type
;
3029 /** Parameter type. */
3032 typedef binomial_distribution
<_IntType
> distribution_type
;
3033 friend class binomial_distribution
<_IntType
>;
3036 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3037 : _M_t(__t
), _M_p(__p
)
3039 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3061 #if _GLIBCXX_USE_C99_MATH_TR1
3062 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3063 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3068 // constructors and member function
3070 binomial_distribution(_IntType __t
= _IntType(1),
3072 : _M_param(__t
, __p
), _M_nd()
3076 binomial_distribution(const param_type
& __p
)
3077 : _M_param(__p
), _M_nd()
3081 * @brief Resets the distribution state.
3088 * @brief Returns the distribution @p t parameter.
3092 { return _M_param
.t(); }
3095 * @brief Returns the distribution @p p parameter.
3099 { return _M_param
.p(); }
3102 * @brief Returns the parameter set of the distribution.
3106 { return _M_param
; }
3109 * @brief Sets the parameter set of the distribution.
3110 * @param __param The new parameter set of the distribution.
3113 param(const param_type
& __param
)
3114 { _M_param
= __param
; }
3117 * @brief Returns the greatest lower bound value of the distribution.
3124 * @brief Returns the least upper bound value of the distribution.
3128 { return _M_param
.t(); }
3130 template<typename _UniformRandomNumberGenerator
>
3132 operator()(_UniformRandomNumberGenerator
& __urng
)
3133 { return this->operator()(__urng
, this->param()); }
3135 template<typename _UniformRandomNumberGenerator
>
3137 operator()(_UniformRandomNumberGenerator
& __urng
,
3138 const param_type
& __p
);
3141 * @brief Inserts a %binomial_distribution random number distribution
3142 * @p __x into the output stream @p __os.
3144 * @param __os An output stream.
3145 * @param __x A %binomial_distribution random number distribution.
3147 * @returns The output stream with the state of @p __x inserted or in
3150 template<typename _IntType1
,
3151 typename _CharT
, typename _Traits
>
3152 friend std::basic_ostream
<_CharT
, _Traits
>&
3153 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3154 const std::binomial_distribution
<_IntType1
>&);
3157 * @brief Extracts a %binomial_distribution random number distribution
3158 * @p __x from the input stream @p __is.
3160 * @param __is An input stream.
3161 * @param __x A %binomial_distribution random number generator engine.
3163 * @returns The input stream with @p __x extracted or in an error
3166 template<typename _IntType1
,
3167 typename _CharT
, typename _Traits
>
3168 friend std::basic_istream
<_CharT
, _Traits
>&
3169 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3170 std::binomial_distribution
<_IntType1
>&);
3173 template<typename _UniformRandomNumberGenerator
>
3175 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3177 param_type _M_param
;
3179 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3180 std::normal_distribution
<double> _M_nd
;
3185 * @brief A discrete geometric random number distribution.
3187 * The formula for the geometric probability density function is
3188 * @f$p(i|p) = (1 - p)p^{i-1}@f$ where @f$p@f$ is the parameter of the
3191 template<typename _IntType
= int>
3192 class geometric_distribution
3194 static_assert(std::is_integral
<_IntType
>::value
,
3195 "template argument not an integral type");
3198 /** The type of the range of the distribution. */
3199 typedef _IntType result_type
;
3200 /** Parameter type. */
3203 typedef geometric_distribution
<_IntType
> distribution_type
;
3204 friend class geometric_distribution
<_IntType
>;
3207 param_type(double __p
= 0.5)
3210 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0)
3222 { _M_log_p
= std::log(_M_p
); }
3229 // constructors and member function
3231 geometric_distribution(double __p
= 0.5)
3236 geometric_distribution(const param_type
& __p
)
3241 * @brief Resets the distribution state.
3243 * Does nothing for the geometric distribution.
3249 * @brief Returns the distribution parameter @p p.
3253 { return _M_param
.p(); }
3256 * @brief Returns the parameter set of the distribution.
3260 { return _M_param
; }
3263 * @brief Sets the parameter set of the distribution.
3264 * @param __param The new parameter set of the distribution.
3267 param(const param_type
& __param
)
3268 { _M_param
= __param
; }
3271 * @brief Returns the greatest lower bound value of the distribution.
3278 * @brief Returns the least upper bound value of the distribution.
3282 { return std::numeric_limits
<result_type
>::max(); }
3284 template<typename _UniformRandomNumberGenerator
>
3286 operator()(_UniformRandomNumberGenerator
& __urng
)
3287 { return this->operator()(__urng
, this->param()); }
3289 template<typename _UniformRandomNumberGenerator
>
3291 operator()(_UniformRandomNumberGenerator
& __urng
,
3292 const param_type
& __p
);
3295 param_type _M_param
;
3299 * @brief Inserts a %geometric_distribution random number distribution
3300 * @p __x into the output stream @p __os.
3302 * @param __os An output stream.
3303 * @param __x A %geometric_distribution random number distribution.
3305 * @returns The output stream with the state of @p __x inserted or in
3308 template<typename _IntType
,
3309 typename _CharT
, typename _Traits
>
3310 std::basic_ostream
<_CharT
, _Traits
>&
3311 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3312 const std::geometric_distribution
<_IntType
>&);
3315 * @brief Extracts a %geometric_distribution random number distribution
3316 * @p __x from the input stream @p __is.
3318 * @param __is An input stream.
3319 * @param __x A %geometric_distribution random number generator engine.
3321 * @returns The input stream with @p __x extracted or in an error state.
3323 template<typename _IntType
,
3324 typename _CharT
, typename _Traits
>
3325 std::basic_istream
<_CharT
, _Traits
>&
3326 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3327 std::geometric_distribution
<_IntType
>&);
3331 * @brief A negative_binomial_distribution random number distribution.
3333 * The formula for the negative binomial probability mass function is
3334 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3335 * and @f$p@f$ are the parameters of the distribution.
3337 template<typename _IntType
= int>
3338 class negative_binomial_distribution
3340 static_assert(std::is_integral
<_IntType
>::value
,
3341 "template argument not an integral type");
3344 /** The type of the range of the distribution. */
3345 typedef _IntType result_type
;
3346 /** Parameter type. */
3349 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3352 param_type(_IntType __k
= 1, double __p
= 0.5)
3353 : _M_k(__k
), _M_p(__p
)
3370 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3371 : _M_param(__k
, __p
), _M_gd(__k
, __p
/ (1.0 - __p
))
3375 negative_binomial_distribution(const param_type
& __p
)
3376 : _M_param(__p
), _M_gd(__p
.k(), __p
.p() / (1.0 - __p
.p()))
3380 * @brief Resets the distribution state.
3387 * @brief Return the @f$k@f$ parameter of the distribution.
3391 { return _M_param
.k(); }
3394 * @brief Return the @f$p@f$ parameter of the distribution.
3398 { return _M_param
.p(); }
3401 * @brief Returns the parameter set of the distribution.
3405 { return _M_param
; }
3408 * @brief Sets the parameter set of the distribution.
3409 * @param __param The new parameter set of the distribution.
3412 param(const param_type
& __param
)
3413 { _M_param
= __param
; }
3416 * @brief Returns the greatest lower bound value of the distribution.
3420 { return result_type(0); }
3423 * @brief Returns the least upper bound value of the distribution.
3427 { return std::numeric_limits
<result_type
>::max(); }
3429 template<typename _UniformRandomNumberGenerator
>
3431 operator()(_UniformRandomNumberGenerator
& __urng
);
3433 template<typename _UniformRandomNumberGenerator
>
3435 operator()(_UniformRandomNumberGenerator
& __urng
,
3436 const param_type
& __p
);
3439 * @brief Inserts a %negative_binomial_distribution random
3440 * number distribution @p __x into the output stream @p __os.
3442 * @param __os An output stream.
3443 * @param __x A %negative_binomial_distribution random number
3446 * @returns The output stream with the state of @p __x inserted or in
3449 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3450 friend std::basic_ostream
<_CharT
, _Traits
>&
3451 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3452 const std::negative_binomial_distribution
<_IntType1
>&);
3455 * @brief Extracts a %negative_binomial_distribution random number
3456 * distribution @p __x from the input stream @p __is.
3458 * @param __is An input stream.
3459 * @param __x A %negative_binomial_distribution random number
3462 * @returns The input stream with @p __x extracted or in an error state.
3464 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3465 friend std::basic_istream
<_CharT
, _Traits
>&
3466 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3467 std::negative_binomial_distribution
<_IntType1
>&);
3470 param_type _M_param
;
3472 std::gamma_distribution
<double> _M_gd
;
3475 /* @} */ // group std_random_distributions_bernoulli
3478 * @addtogroup std_random_distributions_poisson Poisson Distributions
3479 * @ingroup std_random_distributions
3484 * @brief A discrete Poisson random number distribution.
3486 * The formula for the Poisson probability density function is
3487 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
3488 * parameter of the distribution.
3490 template<typename _IntType
= int>
3491 class poisson_distribution
3493 static_assert(std::is_integral
<_IntType
>::value
,
3494 "template argument not an integral type");
3497 /** The type of the range of the distribution. */
3498 typedef _IntType result_type
;
3499 /** Parameter type. */
3502 typedef poisson_distribution
<_IntType
> distribution_type
;
3503 friend class poisson_distribution
<_IntType
>;
3506 param_type(double __mean
= 1.0)
3509 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3518 // Hosts either log(mean) or the threshold of the simple method.
3525 #if _GLIBCXX_USE_C99_MATH_TR1
3526 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
3530 // constructors and member function
3532 poisson_distribution(double __mean
= 1.0)
3533 : _M_param(__mean
), _M_nd()
3537 poisson_distribution(const param_type
& __p
)
3538 : _M_param(__p
), _M_nd()
3542 * @brief Resets the distribution state.
3549 * @brief Returns the distribution parameter @p mean.
3553 { return _M_param
.mean(); }
3556 * @brief Returns the parameter set of the distribution.
3560 { return _M_param
; }
3563 * @brief Sets the parameter set of the distribution.
3564 * @param __param The new parameter set of the distribution.
3567 param(const param_type
& __param
)
3568 { _M_param
= __param
; }
3571 * @brief Returns the greatest lower bound value of the distribution.
3578 * @brief Returns the least upper bound value of the distribution.
3582 { return std::numeric_limits
<result_type
>::max(); }
3584 template<typename _UniformRandomNumberGenerator
>
3586 operator()(_UniformRandomNumberGenerator
& __urng
)
3587 { return this->operator()(__urng
, this->param()); }
3589 template<typename _UniformRandomNumberGenerator
>
3591 operator()(_UniformRandomNumberGenerator
& __urng
,
3592 const param_type
& __p
);
3595 * @brief Inserts a %poisson_distribution random number distribution
3596 * @p __x into the output stream @p __os.
3598 * @param __os An output stream.
3599 * @param __x A %poisson_distribution random number distribution.
3601 * @returns The output stream with the state of @p __x inserted or in
3604 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3605 friend std::basic_ostream
<_CharT
, _Traits
>&
3606 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3607 const std::poisson_distribution
<_IntType1
>&);
3610 * @brief Extracts a %poisson_distribution random number distribution
3611 * @p __x from the input stream @p __is.
3613 * @param __is An input stream.
3614 * @param __x A %poisson_distribution random number generator engine.
3616 * @returns The input stream with @p __x extracted or in an error
3619 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3620 friend std::basic_istream
<_CharT
, _Traits
>&
3621 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3622 std::poisson_distribution
<_IntType1
>&);
3625 param_type _M_param
;
3627 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3628 std::normal_distribution
<double> _M_nd
;
3632 * @brief An exponential continuous distribution for random numbers.
3634 * The formula for the exponential probability density function is
3635 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
3637 * <table border=1 cellpadding=10 cellspacing=0>
3638 * <caption align=top>Distribution Statistics</caption>
3639 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
3640 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
3641 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
3642 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3643 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
3646 template<typename _RealType
= double>
3647 class exponential_distribution
3649 static_assert(std::is_floating_point
<_RealType
>::value
,
3650 "template argument not a floating point type");
3653 /** The type of the range of the distribution. */
3654 typedef _RealType result_type
;
3655 /** Parameter type. */
3658 typedef exponential_distribution
<_RealType
> distribution_type
;
3661 param_type(_RealType __lambda
= _RealType(1))
3662 : _M_lambda(__lambda
)
3664 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
3669 { return _M_lambda
; }
3672 _RealType _M_lambda
;
3677 * @brief Constructs an exponential distribution with inverse scale
3678 * parameter @f$\lambda@f$.
3681 exponential_distribution(const result_type
& __lambda
= result_type(1))
3682 : _M_param(__lambda
)
3686 exponential_distribution(const param_type
& __p
)
3691 * @brief Resets the distribution state.
3693 * Has no effect on exponential distributions.
3699 * @brief Returns the inverse scale parameter of the distribution.
3703 { return _M_param
.lambda(); }
3706 * @brief Returns the parameter set of the distribution.
3710 { return _M_param
; }
3713 * @brief Sets the parameter set of the distribution.
3714 * @param __param The new parameter set of the distribution.
3717 param(const param_type
& __param
)
3718 { _M_param
= __param
; }
3721 * @brief Returns the greatest lower bound value of the distribution.
3725 { return result_type(0); }
3728 * @brief Returns the least upper bound value of the distribution.
3732 { return std::numeric_limits
<result_type
>::max(); }
3734 template<typename _UniformRandomNumberGenerator
>
3736 operator()(_UniformRandomNumberGenerator
& __urng
)
3737 { return this->operator()(__urng
, this->param()); }
3739 template<typename _UniformRandomNumberGenerator
>
3741 operator()(_UniformRandomNumberGenerator
& __urng
,
3742 const param_type
& __p
)
3744 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
3746 return -std::log(__aurng()) / __p
.lambda();
3750 param_type _M_param
;
3754 * @brief Inserts a %exponential_distribution random number distribution
3755 * @p __x into the output stream @p __os.
3757 * @param __os An output stream.
3758 * @param __x A %exponential_distribution random number distribution.
3760 * @returns The output stream with the state of @p __x inserted or in
3763 template<typename _RealType
, typename _CharT
, typename _Traits
>
3764 std::basic_ostream
<_CharT
, _Traits
>&
3765 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3766 const std::exponential_distribution
<_RealType
>&);
3769 * @brief Extracts a %exponential_distribution random number distribution
3770 * @p __x from the input stream @p __is.
3772 * @param __is An input stream.
3773 * @param __x A %exponential_distribution random number
3776 * @returns The input stream with @p __x extracted or in an error state.
3778 template<typename _RealType
, typename _CharT
, typename _Traits
>
3779 std::basic_istream
<_CharT
, _Traits
>&
3780 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3781 std::exponential_distribution
<_RealType
>&);
3785 * @brief A weibull_distribution random number distribution.
3787 * The formula for the normal probability density function is:
3789 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
3790 * \exp{(-(\frac{x}{\beta})^\alpha)}
3793 template<typename _RealType
= double>
3794 class weibull_distribution
3796 static_assert(std::is_floating_point
<_RealType
>::value
,
3797 "template argument not a floating point type");
3800 /** The type of the range of the distribution. */
3801 typedef _RealType result_type
;
3802 /** Parameter type. */
3805 typedef weibull_distribution
<_RealType
> distribution_type
;
3808 param_type(_RealType __a
= _RealType(1),
3809 _RealType __b
= _RealType(1))
3810 : _M_a(__a
), _M_b(__b
)
3827 weibull_distribution(_RealType __a
= _RealType(1),
3828 _RealType __b
= _RealType(1))
3829 : _M_param(__a
, __b
)
3833 weibull_distribution(const param_type
& __p
)
3838 * @brief Resets the distribution state.
3845 * @brief Return the @f$a@f$ parameter of the distribution.
3849 { return _M_param
.a(); }
3852 * @brief Return the @f$b@f$ parameter of the distribution.
3856 { return _M_param
.b(); }
3859 * @brief Returns the parameter set of the distribution.
3863 { return _M_param
; }
3866 * @brief Sets the parameter set of the distribution.
3867 * @param __param The new parameter set of the distribution.
3870 param(const param_type
& __param
)
3871 { _M_param
= __param
; }
3874 * @brief Returns the greatest lower bound value of the distribution.
3878 { return result_type(0); }
3881 * @brief Returns the least upper bound value of the distribution.
3885 { return std::numeric_limits
<result_type
>::max(); }
3887 template<typename _UniformRandomNumberGenerator
>
3889 operator()(_UniformRandomNumberGenerator
& __urng
)
3890 { return this->operator()(__urng
, this->param()); }
3892 template<typename _UniformRandomNumberGenerator
>
3894 operator()(_UniformRandomNumberGenerator
& __urng
,
3895 const param_type
& __p
);
3898 param_type _M_param
;
3902 * @brief Inserts a %weibull_distribution random number distribution
3903 * @p __x into the output stream @p __os.
3905 * @param __os An output stream.
3906 * @param __x A %weibull_distribution random number distribution.
3908 * @returns The output stream with the state of @p __x inserted or in
3911 template<typename _RealType
, typename _CharT
, typename _Traits
>
3912 std::basic_ostream
<_CharT
, _Traits
>&
3913 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3914 const std::weibull_distribution
<_RealType
>&);
3917 * @brief Extracts a %weibull_distribution random number distribution
3918 * @p __x from the input stream @p __is.
3920 * @param __is An input stream.
3921 * @param __x A %weibull_distribution random number
3924 * @returns The input stream with @p __x extracted or in an error state.
3926 template<typename _RealType
, typename _CharT
, typename _Traits
>
3927 std::basic_istream
<_CharT
, _Traits
>&
3928 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3929 std::weibull_distribution
<_RealType
>&);
3933 * @brief A extreme_value_distribution random number distribution.
3935 * The formula for the normal probability mass function is
3937 * p(x|a,b) = \frac{1}{b}
3938 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
3941 template<typename _RealType
= double>
3942 class extreme_value_distribution
3944 static_assert(std::is_floating_point
<_RealType
>::value
,
3945 "template argument not a floating point type");
3948 /** The type of the range of the distribution. */
3949 typedef _RealType result_type
;
3950 /** Parameter type. */
3953 typedef extreme_value_distribution
<_RealType
> distribution_type
;
3956 param_type(_RealType __a
= _RealType(0),
3957 _RealType __b
= _RealType(1))
3958 : _M_a(__a
), _M_b(__b
)
3975 extreme_value_distribution(_RealType __a
= _RealType(0),
3976 _RealType __b
= _RealType(1))
3977 : _M_param(__a
, __b
)
3981 extreme_value_distribution(const param_type
& __p
)
3986 * @brief Resets the distribution state.
3993 * @brief Return the @f$a@f$ parameter of the distribution.
3997 { return _M_param
.a(); }
4000 * @brief Return the @f$b@f$ parameter of the distribution.
4004 { return _M_param
.b(); }
4007 * @brief Returns the parameter set of the distribution.
4011 { return _M_param
; }
4014 * @brief Sets the parameter set of the distribution.
4015 * @param __param The new parameter set of the distribution.
4018 param(const param_type
& __param
)
4019 { _M_param
= __param
; }
4022 * @brief Returns the greatest lower bound value of the distribution.
4026 { return std::numeric_limits
<result_type
>::min(); }
4029 * @brief Returns the least upper bound value of the distribution.
4033 { return std::numeric_limits
<result_type
>::max(); }
4035 template<typename _UniformRandomNumberGenerator
>
4037 operator()(_UniformRandomNumberGenerator
& __urng
)
4038 { return this->operator()(__urng
, this->param()); }
4040 template<typename _UniformRandomNumberGenerator
>
4042 operator()(_UniformRandomNumberGenerator
& __urng
,
4043 const param_type
& __p
);
4046 param_type _M_param
;
4050 * @brief Inserts a %extreme_value_distribution random number distribution
4051 * @p __x into the output stream @p __os.
4053 * @param __os An output stream.
4054 * @param __x A %extreme_value_distribution random number distribution.
4056 * @returns The output stream with the state of @p __x inserted or in
4059 template<typename _RealType
, typename _CharT
, typename _Traits
>
4060 std::basic_ostream
<_CharT
, _Traits
>&
4061 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4062 const std::extreme_value_distribution
<_RealType
>&);
4065 * @brief Extracts a %extreme_value_distribution random number
4066 * distribution @p __x from the input stream @p __is.
4068 * @param __is An input stream.
4069 * @param __x A %extreme_value_distribution random number
4072 * @returns The input stream with @p __x extracted or in an error state.
4074 template<typename _RealType
, typename _CharT
, typename _Traits
>
4075 std::basic_istream
<_CharT
, _Traits
>&
4076 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4077 std::extreme_value_distribution
<_RealType
>&);
4081 * @brief A discrete_distribution random number distribution.
4083 * The formula for the discrete probability mass function is
4086 template<typename _IntType
= int>
4087 class discrete_distribution
4089 static_assert(std::is_integral
<_IntType
>::value
,
4090 "template argument not an integral type");
4093 /** The type of the range of the distribution. */
4094 typedef _IntType result_type
;
4095 /** Parameter type. */
4098 typedef discrete_distribution
<_IntType
> distribution_type
;
4099 friend class discrete_distribution
<_IntType
>;
4102 : _M_prob(), _M_cp()
4103 { _M_initialize(); }
4105 template<typename _InputIterator
>
4106 param_type(_InputIterator __wbegin
,
4107 _InputIterator __wend
)
4108 : _M_prob(__wbegin
, __wend
), _M_cp()
4109 { _M_initialize(); }
4111 param_type(initializer_list
<double> __wil
)
4112 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4113 { _M_initialize(); }
4115 template<typename _Func
>
4116 param_type(size_t __nw
, double __xmin
, double __xmax
,
4120 probabilities() const
4127 std::vector
<double> _M_prob
;
4128 std::vector
<double> _M_cp
;
4131 discrete_distribution()
4135 template<typename _InputIterator
>
4136 discrete_distribution(_InputIterator __wbegin
,
4137 _InputIterator __wend
)
4138 : _M_param(__wbegin
, __wend
)
4141 discrete_distribution(initializer_list
<double> __wl
)
4145 template<typename _Func
>
4146 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4148 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4152 discrete_distribution(const param_type
& __p
)
4157 * @brief Resets the distribution state.
4164 * @brief Returns the probabilities of the distribution.
4167 probabilities() const
4168 { return _M_param
.probabilities(); }
4171 * @brief Returns the parameter set of the distribution.
4175 { return _M_param
; }
4178 * @brief Sets the parameter set of the distribution.
4179 * @param __param The new parameter set of the distribution.
4182 param(const param_type
& __param
)
4183 { _M_param
= __param
; }
4186 * @brief Returns the greatest lower bound value of the distribution.
4190 { return result_type(0); }
4193 * @brief Returns the least upper bound value of the distribution.
4197 { return this->_M_param
._M_prob
.size() - 1; }
4199 template<typename _UniformRandomNumberGenerator
>
4201 operator()(_UniformRandomNumberGenerator
& __urng
)
4202 { return this->operator()(__urng
, this->param()); }
4204 template<typename _UniformRandomNumberGenerator
>
4206 operator()(_UniformRandomNumberGenerator
& __urng
,
4207 const param_type
& __p
);
4210 * @brief Inserts a %discrete_distribution random number distribution
4211 * @p __x into the output stream @p __os.
4213 * @param __os An output stream.
4214 * @param __x A %discrete_distribution random number distribution.
4216 * @returns The output stream with the state of @p __x inserted or in
4219 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4220 friend std::basic_ostream
<_CharT
, _Traits
>&
4221 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4222 const std::discrete_distribution
<_IntType1
>&);
4225 * @brief Extracts a %discrete_distribution random number distribution
4226 * @p __x from the input stream @p __is.
4228 * @param __is An input stream.
4229 * @param __x A %discrete_distribution random number
4232 * @returns The input stream with @p __x extracted or in an error
4235 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4236 friend std::basic_istream
<_CharT
, _Traits
>&
4237 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4238 std::discrete_distribution
<_IntType1
>&);
4241 param_type _M_param
;
4246 * @brief A piecewise_constant_distribution random number distribution.
4248 * The formula for the piecewise constant probability mass function is
4251 template<typename _RealType
= double>
4252 class piecewise_constant_distribution
4254 static_assert(std::is_floating_point
<_RealType
>::value
,
4255 "template argument not a floating point type");
4258 /** The type of the range of the distribution. */
4259 typedef _RealType result_type
;
4260 /** Parameter type. */
4263 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4264 friend class piecewise_constant_distribution
<_RealType
>;
4267 : _M_int(), _M_den(), _M_cp()
4268 { _M_initialize(); }
4270 template<typename _InputIteratorB
, typename _InputIteratorW
>
4271 param_type(_InputIteratorB __bfirst
,
4272 _InputIteratorB __bend
,
4273 _InputIteratorW __wbegin
);
4275 template<typename _Func
>
4276 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
4278 template<typename _Func
>
4279 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4282 std::vector
<_RealType
>
4294 std::vector
<_RealType
> _M_int
;
4295 std::vector
<double> _M_den
;
4296 std::vector
<double> _M_cp
;
4300 piecewise_constant_distribution()
4304 template<typename _InputIteratorB
, typename _InputIteratorW
>
4305 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4306 _InputIteratorB __bend
,
4307 _InputIteratorW __wbegin
)
4308 : _M_param(__bfirst
, __bend
, __wbegin
)
4311 template<typename _Func
>
4312 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
4314 : _M_param(__bl
, __fw
)
4317 template<typename _Func
>
4318 piecewise_constant_distribution(size_t __nw
,
4319 _RealType __xmin
, _RealType __xmax
,
4321 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4325 piecewise_constant_distribution(const param_type
& __p
)
4330 * @brief Resets the distribution state.
4337 * @brief Returns a vector of the intervals.
4339 std::vector
<_RealType
>
4341 { return _M_param
.intervals(); }
4344 * @brief Returns a vector of the probability densities.
4348 { return _M_param
.densities(); }
4351 * @brief Returns the parameter set of the distribution.
4355 { return _M_param
; }
4358 * @brief Sets the parameter set of the distribution.
4359 * @param __param The new parameter set of the distribution.
4362 param(const param_type
& __param
)
4363 { _M_param
= __param
; }
4366 * @brief Returns the greatest lower bound value of the distribution.
4370 { return this->_M_param
._M_int
.front(); }
4373 * @brief Returns the least upper bound value of the distribution.
4377 { return this->_M_param
._M_int
.back(); }
4379 template<typename _UniformRandomNumberGenerator
>
4381 operator()(_UniformRandomNumberGenerator
& __urng
)
4382 { return this->operator()(__urng
, this->param()); }
4384 template<typename _UniformRandomNumberGenerator
>
4386 operator()(_UniformRandomNumberGenerator
& __urng
,
4387 const param_type
& __p
);
4390 * @brief Inserts a %piecewise_constan_distribution random
4391 * number distribution @p __x into the output stream @p __os.
4393 * @param __os An output stream.
4394 * @param __x A %piecewise_constan_distribution random number
4397 * @returns The output stream with the state of @p __x inserted or in
4400 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4401 friend std::basic_ostream
<_CharT
, _Traits
>&
4402 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4403 const std::piecewise_constant_distribution
<_RealType1
>&);
4406 * @brief Extracts a %piecewise_constan_distribution random
4407 * number distribution @p __x from the input stream @p __is.
4409 * @param __is An input stream.
4410 * @param __x A %piecewise_constan_distribution random number
4413 * @returns The input stream with @p __x extracted or in an error
4416 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4417 friend std::basic_istream
<_CharT
, _Traits
>&
4418 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4419 std::piecewise_constant_distribution
<_RealType1
>&);
4422 param_type _M_param
;
4427 * @brief A piecewise_linear_distribution random number distribution.
4429 * The formula for the piecewise linear probability mass function is
4432 template<typename _RealType
= double>
4433 class piecewise_linear_distribution
4435 static_assert(std::is_floating_point
<_RealType
>::value
,
4436 "template argument not a floating point type");
4439 /** The type of the range of the distribution. */
4440 typedef _RealType result_type
;
4441 /** Parameter type. */
4444 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
4445 friend class piecewise_linear_distribution
<_RealType
>;
4448 : _M_int(), _M_den(), _M_cp(), _M_m()
4449 { _M_initialize(); }
4451 template<typename _InputIteratorB
, typename _InputIteratorW
>
4452 param_type(_InputIteratorB __bfirst
,
4453 _InputIteratorB __bend
,
4454 _InputIteratorW __wbegin
);
4456 template<typename _Func
>
4457 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
4459 template<typename _Func
>
4460 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4463 std::vector
<_RealType
>
4475 std::vector
<_RealType
> _M_int
;
4476 std::vector
<double> _M_den
;
4477 std::vector
<double> _M_cp
;
4478 std::vector
<double> _M_m
;
4482 piecewise_linear_distribution()
4486 template<typename _InputIteratorB
, typename _InputIteratorW
>
4487 piecewise_linear_distribution(_InputIteratorB __bfirst
,
4488 _InputIteratorB __bend
,
4489 _InputIteratorW __wbegin
)
4490 : _M_param(__bfirst
, __bend
, __wbegin
)
4493 template<typename _Func
>
4494 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
4496 : _M_param(__bl
, __fw
)
4499 template<typename _Func
>
4500 piecewise_linear_distribution(size_t __nw
,
4501 _RealType __xmin
, _RealType __xmax
,
4503 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4507 piecewise_linear_distribution(const param_type
& __p
)
4512 * Resets the distribution state.
4519 * @brief Return the intervals of the distribution.
4521 std::vector
<_RealType
>
4523 { return _M_param
.intervals(); }
4526 * @brief Return a vector of the probability densities of the
4531 { return _M_param
.densities(); }
4534 * @brief Returns the parameter set of the distribution.
4538 { return _M_param
; }
4541 * @brief Sets the parameter set of the distribution.
4542 * @param __param The new parameter set of the distribution.
4545 param(const param_type
& __param
)
4546 { _M_param
= __param
; }
4549 * @brief Returns the greatest lower bound value of the distribution.
4553 { return this->_M_param
._M_int
.front(); }
4556 * @brief Returns the least upper bound value of the distribution.
4560 { return this->_M_param
._M_int
.back(); }
4562 template<typename _UniformRandomNumberGenerator
>
4564 operator()(_UniformRandomNumberGenerator
& __urng
)
4565 { return this->operator()(__urng
, this->param()); }
4567 template<typename _UniformRandomNumberGenerator
>
4569 operator()(_UniformRandomNumberGenerator
& __urng
,
4570 const param_type
& __p
);
4573 * @brief Inserts a %piecewise_linear_distribution random number
4574 * distribution @p __x into the output stream @p __os.
4576 * @param __os An output stream.
4577 * @param __x A %piecewise_linear_distribution random number
4580 * @returns The output stream with the state of @p __x inserted or in
4583 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4584 friend std::basic_ostream
<_CharT
, _Traits
>&
4585 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4586 const std::piecewise_linear_distribution
<_RealType1
>&);
4589 * @brief Extracts a %piecewise_linear_distribution random number
4590 * distribution @p __x from the input stream @p __is.
4592 * @param __is An input stream.
4593 * @param __x A %piecewise_linear_distribution random number
4596 * @returns The input stream with @p __x extracted or in an error
4599 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4600 friend std::basic_istream
<_CharT
, _Traits
>&
4601 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4602 std::piecewise_linear_distribution
<_RealType1
>&);
4605 param_type _M_param
;
4609 /* @} */ // group std_random_distributions_poisson
4611 /* @} */ // group std_random_distributions
4614 * @addtogroup std_random_utilities Random Number Utilities
4615 * @ingroup std_random
4620 * @brief The seed_seq class generates sequences of seeds for random
4621 * number generators.
4627 /** The type of the seed vales. */
4628 typedef uint_least32_t result_type
;
4630 /** Default constructor. */
4635 template<typename _IntType
>
4636 seed_seq(std::initializer_list
<_IntType
> il
);
4638 template<typename _InputIterator
>
4639 seed_seq(_InputIterator __begin
, _InputIterator __end
);
4641 // generating functions
4642 template<typename _RandomAccessIterator
>
4644 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
4646 // property functions
4648 { return _M_v
.size(); }
4650 template<typename OutputIterator
>
4652 param(OutputIterator __dest
) const
4653 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
4657 std::vector
<result_type
> _M_v
;
4660 /* @} */ // group std_random_utilities
4662 /* @} */ // group std_random