1 // random number generation -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
41 // [26.4] Random number generation
44 * @addtogroup std_random Random Number Generation
45 * A facility for generating random numbers on selected distributions.
50 * @brief A function template for converting the output of a (integral)
51 * uniform random number generator to a floatng point result in the range
54 template<typename _RealType
, size_t __bits
,
55 typename _UniformRandomNumberGenerator
>
57 generate_canonical(_UniformRandomNumberGenerator
& __g
);
62 * Implementation-space details.
66 template<typename _UIntType
, size_t __w
,
67 bool = __w
< static_cast<size_t>(std::numeric_limits
<_UIntType
>::digits
)>
69 { static const _UIntType __value
= 0; };
71 template<typename _UIntType
, size_t __w
>
72 struct _Shift
<_UIntType
, __w
, true>
73 { static const _UIntType __value
= _UIntType(1) << __w
; };
76 template<typename _UIntType
, size_t __w
,
77 bool = __w
<static_cast<size_t>(std::numeric_limits
<_UIntType
>::digits
)>
79 { static const _UIntType __value
= __gnu_cxx::__numeric_traits
<_UIntType
>::max
; };
81 template<typename _UIntType
, size_t __w
>
82 struct _ShiftMin1
<_UIntType
, __w
, true>
83 { static const _UIntType __value
= _UIntType(1) << __w
- _UIntType(1); };
85 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
, bool>
88 // Dispatch based on modulus value to prevent divide-by-zero compile-time
89 // errors when m == 0.
90 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
>
93 { return _Mod
<_Tp
, __a
, __c
, __m
, __m
== 0>::__calc(__x
); }
95 typedef __gnu_cxx::__conditional_type
<(sizeof(unsigned) == 4),
96 unsigned, unsigned long>::__type _UInt32Type
;
99 * An adaptor class for converting the output of any Generator into
100 * the input for a specific Distribution.
102 template<typename _Engine
, typename _DInputType
>
107 _Adaptor(_Engine
& __g
)
113 if (is_integral
<_DInputType
>::value
)
116 return _DInputType(0);
122 if (is_integral
<_DInputType
>::value
)
125 return _DInputType(1);
129 * Converts a value generated by the adapted random number generator
130 * into a value in the input domain for the dependent random number
133 * Because the type traits are compile time constants only the
134 * appropriate clause of the if statements will actually be emitted
140 if (is_integral
<_DInputType
>::value
)
143 return generate_canonical
<_DInputType
,
144 numeric_limits
<_DInputType
>::digits
,
151 } // namespace __detail
154 * @addtogroup std_random_generators Random Number Generators
155 * @ingroup std_random
157 * These classes define objects which provide random or pseudorandom
158 * numbers, either from a discrete or a continuous interval. The
159 * random number generator supplied as a part of this library are
160 * all uniform random number generators which provide a sequence of
161 * random number uniformly distributed over their range.
163 * A number generator is a function object with an operator() that
164 * takes zero arguments and returns a number.
166 * A compliant random number generator must satisfy the following
167 * requirements. <table border=1 cellpadding=10 cellspacing=0>
168 * <caption align=top>Random Number Generator Requirements</caption>
169 * <tr><td>To be documented.</td></tr> </table>
175 * @brief A model of a linear congruential random number generator.
177 * A random number generator that produces pseudorandom numbers using the
178 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
180 * The template parameter @p _UIntType must be an unsigned integral type
181 * large enough to store values up to (__m-1). If the template parameter
182 * @p __m is 0, the modulus @p __m used is
183 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
184 * parameters @p __a and @p __c must be less than @p __m.
186 * The size of the state is @f$ 1 @f$.
188 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
189 class linear_congruential_engine
191 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
192 static_assert(__m
== 0 || (__a
< __m
&& __c
< __m
),
193 "template arguments out of bounds"
194 " in linear_congruential_engine");
197 /** The type of the generated random value. */
198 typedef _UIntType result_type
;
200 /** The multiplier. */
201 static const result_type multiplier
= __a
;
203 static const result_type increment
= __c
;
205 static const result_type modulus
= __m
;
206 static const result_type default_seed
= 1UL;
209 * @brief Constructs a %linear_congruential_engine random number
210 * generator engine with seed @p __s. The default seed value
213 * @param __s The initial seed value.
216 linear_congruential_engine(result_type __s
= default_seed
)
220 * @brief Constructs a %linear_congruential_engine random number
221 * generator engine seeded from the seed sequence @p __q.
223 * @param __q the seed sequence.
226 linear_congruential_engine(seed_seq
& __q
)
230 * @brief Reseeds the %linear_congruential_engine random number generator
231 * engine sequence to the seed @g __s.
233 * @param __s The new seed.
236 seed(result_type __s
= default_seed
);
239 * @brief Reseeds the %linear_congruential_engine random number generator
241 * sequence using values from the seed sequence @p __q.
243 * @param __q the seed sequence.
249 * @brief Gets the smallest possible value in the output range.
251 * The minimum depends on the @p __c parameter: if it is zero, the
252 * minimum generated must be > 0, otherwise 0 is allowed.
254 * @todo This should be constexpr.
258 { return (__detail::__mod
<_UIntType
, 1, 0, __m
>(__c
) == 0) ? 1 : 0; }
261 * @brief Gets the largest possible value in the output range.
263 * @todo This should be constexpr.
270 * @brief Discard a sequence of random numbers.
272 * @todo Look for a faster way to do discard.
275 discard(unsigned long long __z
)
277 for (; __z
!= 0ULL; --__z
)
282 * @brief Gets the next random number in the sequence.
288 * @brief Compares two linear congruential random number generator
289 * objects of the same type for equality.
291 * @param __lhs A linear congruential random number generator object.
292 * @param __rhs Another linear congruential random number generator
295 * @returns true if the two objects are equal, false otherwise.
298 operator==(const linear_congruential_engine
& __lhs
,
299 const linear_congruential_engine
& __rhs
)
300 { return __lhs
._M_x
== __rhs
._M_x
; }
303 * @brief Writes the textual representation of the state x(i) of x to
306 * @param __os The output stream.
307 * @param __lcr A % linear_congruential_engine random number generator.
310 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
312 typename _CharT
, typename _Traits
>
313 friend std::basic_ostream
<_CharT
, _Traits
>&
314 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
315 const linear_congruential_engine
<_UIntType1
, __a1
, __c1
,
319 * @brief Sets the state of the engine by reading its textual
320 * representation from @p __is.
322 * The textual representation must have been previously written using
323 * an output stream whose imbued locale and whose type's template
324 * specialization arguments _CharT and _Traits were the same as those
327 * @param __is The input stream.
328 * @param __lcr A % linear_congruential_engine random number generator.
331 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
333 typename _CharT
, typename _Traits
>
334 friend std::basic_istream
<_CharT
, _Traits
>&
335 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
336 linear_congruential_engine
<_UIntType1
,
337 __a1
, __c1
, __m1
>& __lcr
);
340 template<typename _Gen
>
342 seed(_Gen
& __g
, true_type
)
343 { return seed(static_cast<unsigned long>(__g
)); }
345 template<typename _Gen
>
347 seed(_Gen
& __g
, false_type
);
354 * A generalized feedback shift register discrete random number generator.
356 * This algorithm avoids multiplication and division and is designed to be
357 * friendly to a pipelined architecture. If the parameters are chosen
358 * correctly, this generator will produce numbers with a very long period and
359 * fairly good apparent entropy, although still not cryptographically strong.
361 * The best way to use this generator is with the predefined mt19937 class.
363 * This algorithm was originally invented by Makoto Matsumoto and
366 * @var word_size The number of bits in each element of the state vector.
367 * @var state_size The degree of recursion.
368 * @var shift_size The period parameter.
369 * @var mask_bits The separation point bit index.
370 * @var parameter_a The last row of the twist matrix.
371 * @var output_u The first right-shift tempering matrix parameter.
372 * @var output_s The first left-shift tempering matrix parameter.
373 * @var output_b The first left-shift tempering matrix mask.
374 * @var output_t The second left-shift tempering matrix parameter.
375 * @var output_c The second left-shift tempering matrix mask.
376 * @var output_l The second right-shift tempering matrix parameter.
378 template<typename _UIntType
, size_t __w
,
379 size_t __n
, size_t __m
, size_t __r
,
380 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
381 _UIntType __b
, size_t __t
,
382 _UIntType __c
, size_t __l
, _UIntType __f
>
383 class mersenne_twister_engine
385 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
387 static_assert(__m
>= 1U,
388 "mersenne_twister_engine template arguments out of bounds");
389 static_assert(__n
>= __m
,
390 "mersenne_twister_engine template arguments out of bounds");
391 static_assert(__w
>= __r
,
392 "mersenne_twister_engine template arguments out of bounds");
393 static_assert(__w
>= __u
,
394 "mersenne_twister_engine template arguments out of bounds");
395 static_assert(__w
>= __s
,
396 "mersenne_twister_engine template arguments out of bounds");
397 static_assert(__w
>= __t
,
398 "mersenne_twister_engine template arguments out of bounds");
399 static_assert(__w
>= __l
,
400 "mersenne_twister_engine template arguments out of bounds");
401 static_assert(__w
<= static_cast<size_t>(numeric_limits
<_UIntType
>::digits
),
402 "mersenne_twister_engine template arguments out of bounds");
406 static_assert(__a
<= __detail::_ShiftMin1
<_UIntType
, __w
>::__value
,
407 "mersenne_twister_engine template arguments out of bounds");
408 static_assert(__b
<= __detail::_ShiftMin1
<_UIntType
, __w
>::__value
,
409 "mersenne_twister_engine template arguments out of bounds");
410 static_assert(__c
<= __detail::_ShiftMin1
<_UIntType
, __w
>::__value
,
411 "mersenne_twister_engine template arguments out of bounds");
415 /** The type of the generated random value. */
416 typedef _UIntType result_type
;
419 static const size_t word_size
= __w
;
420 static const size_t state_size
= __n
;
421 static const size_t shift_size
= __m
;
422 static const size_t mask_bits
= __r
;
423 static const result_type xor_mask
= __a
;
424 static const size_t tempering_u
= __u
;
425 static const result_type tempering_d
= __d
;
426 static const size_t tempering_s
= __s
;
427 static const result_type tempering_b
= __b
;
428 static const size_t tempering_t
= __t
;
429 static const result_type tempering_c
= __c
;
430 static const size_t tempering_l
= __l
;
431 static const size_t initialization_multiplier
= __f
;
432 static const result_type default_seed
= 5489UL;
434 // constructors and member function
436 mersenne_twister_engine(result_type __sd
= default_seed
)
440 * @brief Constructs a %mersenne_twister_engine random number generator
441 * engine seeded from the seed sequence @p __q.
443 * @param __q the seed sequence.
446 mersenne_twister_engine(seed_seq
& __q
)
450 seed(result_type __sd
= default_seed
);
456 * @brief Gets the smallest possible value in the output range.
458 * @todo This should be constexpr.
465 * @brief Gets the largest possible value in the output range.
467 * @todo This should be constexpr.
471 { return __detail::_ShiftMin1
<_UIntType
, __w
>::__value
; }
474 * @brief Discard a sequence of random numbers.
476 * @todo Look for a faster way to do discard.
479 discard(unsigned long long __z
)
481 for (; __z
!= 0ULL; --__z
)
489 * @brief Compares two % mersenne_twister_engine random number generator
490 * objects of the same type for equality.
492 * @param __lhs A % mersenne_twister_engine random number generator
494 * @param __rhs Another % mersenne_twister_engine random number
497 * @returns true if the two objects are equal, false otherwise.
500 operator==(const mersenne_twister_engine
& __lhs
,
501 const mersenne_twister_engine
& __rhs
)
502 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
505 * @brief Inserts the current state of a % mersenne_twister_engine
506 * random number generator engine @p __x into the output stream
509 * @param __os An output stream.
510 * @param __x A % mersenne_twister_engine random number generator
513 * @returns The output stream with the state of @p __x inserted or in
516 template<typename _UIntType1
,
517 size_t __w1
, size_t __n1
,
518 size_t __m1
, size_t __r1
,
519 _UIntType1 __a1
, size_t __u1
,
520 _UIntType1 __d1
, size_t __s1
,
521 _UIntType1 __b1
, size_t __t1
,
522 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
523 typename _CharT
, typename _Traits
>
524 friend std::basic_ostream
<_CharT
, _Traits
>&
525 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
526 const mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
, __r1
,
527 __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
, __l1
, __f1
>& __x
);
530 * @brief Extracts the current state of a % mersenne_twister_engine
531 * random number generator engine @p __x from the input stream
534 * @param __is An input stream.
535 * @param __x A % mersenne_twister_engine random number generator
538 * @returns The input stream with the state of @p __x extracted or in
541 template<typename _UIntType1
,
542 size_t __w1
, size_t __n1
,
543 size_t __m1
, size_t __r1
,
544 _UIntType1 __a1
, size_t __u1
,
545 _UIntType1 __d1
, size_t __s1
,
546 _UIntType1 __b1
, size_t __t1
,
547 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
548 typename _CharT
, typename _Traits
>
549 friend std::basic_istream
<_CharT
, _Traits
>&
550 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
551 mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
, __r1
,
552 __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
, __l1
, __f1
>& __x
);
555 template<typename _Gen
>
557 seed(_Gen
& __g
, true_type
)
558 { return seed(static_cast<unsigned long>(__g
)); }
560 template<typename _Gen
>
562 seed(_Gen
& __g
, false_type
);
564 _UIntType _M_x
[state_size
];
569 * @brief The Marsaglia-Zaman generator.
571 * This is a model of a Generalized Fibonacci discrete random number
572 * generator, sometimes referred to as the SWC generator.
574 * A discrete random number generator that produces pseudorandom
575 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
576 * carry_{i-1}) \bmod m @f$.
578 * The size of the state is @f$ r @f$
579 * and the maximum period of the generator is @f$ m^r - m^s - 1 @f$.
581 * @var _M_x The state of the generator. This is a ring buffer.
582 * @var _M_carry The carry.
583 * @var _M_p Current index of x(i - r).
585 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
586 class subtract_with_carry_engine
588 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
589 static_assert(__s
> 0U && __r
> __s
591 && __w
<= static_cast<size_t>(numeric_limits
<_UIntType
>::digits
),
592 "template arguments out of bounds"
593 " in subtract_with_carry_engine");
596 /** The type of the generated random value. */
597 typedef _UIntType result_type
;
600 static const size_t word_size
= __w
;
601 static const size_t short_lag
= __s
;
602 static const size_t long_lag
= __r
;
603 static const result_type default_seed
= 19780503;
606 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
607 * random number generator.
610 subtract_with_carry_engine(result_type __sd
= default_seed
)
611 { this->seed(__sd
); }
614 * @brief Constructs a %subtract_with_carry_engine random number engine
615 * seeded from the seed sequence @p __q.
617 * @param __q the seed sequence.
620 subtract_with_carry_engine(seed_seq
& __q
)
624 * @brief Seeds the initial state @f$ x_0 @f$ of the random number
627 * N1688[4.19] modifies this as follows. If @p __value == 0,
628 * sets value to 19780503. In any case, with a linear
629 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
630 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
631 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
632 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
633 * set carry to 1, otherwise sets carry to 0.
636 seed(result_type __sd
= default_seed
);
639 * @brief Seeds the initial state @f$ x_0 @f$ of the
640 * % subtract_with_carry_engine random number generator.
646 * @brief Gets the inclusive minimum value of the range of random
647 * integers returned by this generator.
649 * @todo This should be constexpr.
656 * @brief Gets the inclusive maximum value of the range of random
657 * integers returned by this generator.
659 * @todo This should be constexpr.
663 { return _S_modulus
- 1U; }
666 * @brief Discard a sequence of random numbers.
668 * @todo Look for a faster way to do discard.
671 discard(unsigned long long __z
)
673 for (; __z
!= 0ULL; --__z
)
678 * @brief Gets the next random number in the sequence.
684 * @brief Compares two % subtract_with_carry_engine random number
685 * generator objects of the same type for equality.
687 * @param __lhs A % subtract_with_carry_engine random number generator
689 * @param __rhs Another % subtract_with_carry_engine random number
692 * @returns true if the two objects are equal, false otherwise.
695 operator==(const subtract_with_carry_engine
& __lhs
,
696 const subtract_with_carry_engine
& __rhs
)
697 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
700 * @brief Inserts the current state of a % subtract_with_carry_engine
701 * random number generator engine @p __x into the output stream
704 * @param __os An output stream.
705 * @param __x A % subtract_with_carry_engine random number generator
708 * @returns The output stream with the state of @p __x inserted or in
711 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
712 typename _CharT
, typename _Traits
>
713 friend std::basic_ostream
<_CharT
, _Traits
>&
714 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
715 const subtract_with_carry_engine
<_UIntType1
, __w1
, __s1
,
719 * @brief Extracts the current state of a % subtract_with_carry_engine
720 * random number generator engine @p __x from the input stream
723 * @param __is An input stream.
724 * @param __x A % subtract_with_carry_engine random number generator engine.
726 * @returns The input stream with the state of @p __x extracted or in
729 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
730 typename _CharT
, typename _Traits
>
731 friend std::basic_istream
<_CharT
, _Traits
>&
732 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
733 subtract_with_carry_engine
<_UIntType1
, __w1
, __s1
, __r1
>& __x
);
736 template<typename _Gen
>
738 seed(_Gen
& __g
, true_type
)
739 { return seed(static_cast<unsigned long>(__g
)); }
741 template<typename _Gen
>
743 seed(_Gen
& __g
, false_type
);
745 static const size_t _S_modulus
746 = __detail::_Shift
<_UIntType
, __w
>::__value
;
748 _UIntType _M_x
[long_lag
];
754 * Produces random numbers from some base engine by discarding blocks of
757 * 0 <= @p __r <= @p __p
759 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
760 class discard_block_engine
762 static_assert(__r
>= 1U && __p
>= __r
,
763 "template arguments out of bounds"
764 " in discard_block_engine");
767 /** The type of the generated random value. */
768 typedef typename
_RandomNumberEngine::result_type result_type
;
771 static const size_t block_size
= __p
;
772 static const size_t used_block
= __r
;
775 * @brief Constructs a default %discard_block_engine engine.
777 * The underlying engine is default constructed as well.
779 discard_block_engine()
780 : _M_b(), _M_n(0) { }
783 * @brief Copy constructs a %discard_block_engine engine.
785 * Copies an existing base class random number generator.
786 * @param rng An existing (base class) engine object.
789 discard_block_engine(const _RandomNumberEngine
& __rne
)
790 : _M_b(__rne
), _M_n(0) { }
793 * @brief Move constructs a %discard_block_engine engine.
795 * Copies an existing base class random number generator.
796 * @param rng An existing (base class) engine object.
799 discard_block_engine(_RandomNumberEngine
&& __rne
)
800 : _M_b(std::move(__rne
)), _M_n(0) { }
803 * @brief Seed constructs a %discard_block_engine engine.
805 * Constructs the underlying generator engine seeded with @p __s.
806 * @param __s A seed value for the base class engine.
809 discard_block_engine(result_type __s
)
810 : _M_b(__s
), _M_n(0) { }
813 * @brief Generator construct a %discard_block_engine engine.
815 * @param __q A seed sequence.
818 discard_block_engine(seed_seq
& __q
)
823 * @brief Reseeds the %discard_block_engine object with the default
824 * seed for the underlying base class generator engine.
834 * @brief Reseeds the %discard_block_engine object with the default
835 * seed for the underlying base class generator engine.
838 seed(result_type __s
)
845 * @brief Reseeds the %discard_block_engine object with the given seed
847 * @param __q A seed generator function.
857 * @brief Gets a const reference to the underlying generator engine
860 const _RandomNumberEngine
&
865 * @brief Gets the minimum value in the generated random number range.
867 * @todo This should be constexpr.
871 { return _M_b
.min(); }
874 * @brief Gets the maximum value in the generated random number range.
876 * @todo This should be constexpr.
880 { return _M_b
.max(); }
883 * @brief Discard a sequence of random numbers.
885 * @todo Look for a faster way to do discard.
888 discard(unsigned long long __z
)
890 for (; __z
!= 0ULL; --__z
)
895 * @brief Gets the next value in the generated random number sequence.
901 * @brief Compares two %discard_block_engine random number generator
902 * objects of the same type for equality.
904 * @param __lhs A %discard_block_engine random number generator object.
905 * @param __rhs Another %discard_block_engine random number generator
908 * @returns true if the two objects are equal, false otherwise.
911 operator==(const discard_block_engine
& __lhs
,
912 const discard_block_engine
& __rhs
)
913 { return (__lhs
._M_b
== __rhs
._M_b
) && (__lhs
._M_n
== __rhs
._M_n
); }
916 * @brief Inserts the current state of a %discard_block_engine random
917 * number generator engine @p __x into the output stream
920 * @param __os An output stream.
921 * @param __x A %discard_block_engine random number generator engine.
923 * @returns The output stream with the state of @p __x inserted or in
926 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
927 typename _CharT
, typename _Traits
>
928 friend std::basic_ostream
<_CharT
, _Traits
>&
929 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
930 const discard_block_engine
<_RandomNumberEngine1
,
934 * @brief Extracts the current state of a % subtract_with_carry_engine
935 * random number generator engine @p __x from the input stream
938 * @param __is An input stream.
939 * @param __x A %discard_block_engine random number generator engine.
941 * @returns The input stream with the state of @p __x extracted or in
944 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
945 typename _CharT
, typename _Traits
>
946 friend std::basic_istream
<_CharT
, _Traits
>&
947 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
948 discard_block_engine
<_RandomNumberEngine1
,
952 _RandomNumberEngine _M_b
;
957 * Produces random numbers by combining random numbers from some base
958 * engine to produce random numbers with a specifies number of bits @p __w.
960 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
961 class independent_bits_engine
963 static_assert(__w
> 0U
964 && __w
<= static_cast<size_t>(numeric_limits
<_UIntType
>::digits
),
965 "template arguments out of bounds"
966 " in independent_bits_engine");
969 /** The type of the generated random value. */
970 typedef _UIntType result_type
;
973 * @brief Constructs a default %independent_bits_engine engine.
975 * The underlying engine is default constructed as well.
977 independent_bits_engine()
981 * @brief Copy constructs a %independent_bits_engine engine.
983 * Copies an existing base class random number generator.
984 * @param rng An existing (base class) engine object.
987 independent_bits_engine(const _RandomNumberEngine
& __rne
)
991 * @brief Move constructs a %independent_bits_engine engine.
993 * Copies an existing base class random number generator.
994 * @param rng An existing (base class) engine object.
997 independent_bits_engine(_RandomNumberEngine
&& __rne
)
998 : _M_b(std::move(__rne
)) { }
1001 * @brief Seed constructs a %independent_bits_engine engine.
1003 * Constructs the underlying generator engine seeded with @p __s.
1004 * @param __s A seed value for the base class engine.
1007 independent_bits_engine(result_type __s
)
1011 * @brief Generator construct a %independent_bits_engine engine.
1013 * @param __q A seed sequence.
1016 independent_bits_engine(seed_seq
& __q
)
1021 * @brief Reseeds the %independent_bits_engine object with the default
1022 * seed for the underlying base class generator engine.
1029 * @brief Reseeds the %independent_bits_engine object with the default
1030 * seed for the underlying base class generator engine.
1033 seed(result_type __s
)
1037 * @brief Reseeds the %independent_bits_engine object with the given
1039 * @param __q A seed generator function.
1046 * @brief Gets a const reference to the underlying generator engine
1049 const _RandomNumberEngine
&
1054 * @brief Gets the minimum value in the generated random number range.
1056 * @todo This should be constexpr.
1063 * @brief Gets the maximum value in the generated random number range.
1065 * @todo This should be constexpr.
1069 { return __detail::_ShiftMin1
<_UIntType
, __w
>::__value
; }
1072 * @brief Discard a sequence of random numbers.
1074 * @todo Look for a faster way to do discard.
1077 discard(unsigned long long __z
)
1079 for (; __z
!= 0ULL; --__z
)
1084 * @brief Gets the next value in the generated random number sequence.
1090 * @brief Compares two %independent_bits_engine random number generator
1091 * objects of the same type for equality.
1093 * @param __lhs A %independent_bits_engine random number generator
1095 * @param __rhs Another %independent_bits_engine random number generator
1098 * @returns true if the two objects are equal, false otherwise.
1101 operator==(const independent_bits_engine
& __lhs
,
1102 const independent_bits_engine
& __rhs
)
1103 { return __lhs
._M_b
== __rhs
._M_b
; }
1106 * @brief Extracts the current state of a % subtract_with_carry_engine
1107 * random number generator engine @p __x from the input stream
1110 * @param __is An input stream.
1111 * @param __x A %independent_bits_engine random number generator
1114 * @returns The input stream with the state of @p __x extracted or in
1117 template<typename _CharT
, typename _Traits
>
1118 friend std::basic_istream
<_CharT
, _Traits
>&
1119 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1120 independent_bits_engine
<_RandomNumberEngine
,
1121 __w
, _UIntType
>& __x
)
1128 _RandomNumberEngine _M_b
;
1132 * @brief Inserts the current state of a %independent_bits_engine random
1133 * number generator engine @p __x into the output stream @p __os.
1135 * @param __os An output stream.
1136 * @param __x A %independent_bits_engine random number generator engine.
1138 * @returns The output stream with the state of @p __x inserted or in
1141 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1142 typename _CharT
, typename _Traits
>
1143 std::basic_ostream
<_CharT
, _Traits
>&
1144 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1145 const independent_bits_engine
<_RandomNumberEngine
,
1146 __w
, _UIntType
>& __x
)
1153 * @brief Produces random numbers by combining random numbers from some
1154 * base engine to produce random numbers with a specifies number of bits
1157 template<typename _RandomNumberEngine
, size_t __k
>
1158 class shuffle_order_engine
1160 static_assert(__k
>= 1U,
1161 "template arguments out of bounds"
1162 " in shuffle_order_engine");
1165 /** The type of the generated random value. */
1166 typedef typename
_RandomNumberEngine::result_type result_type
;
1168 static const size_t table_size
= __k
;
1171 * @brief Constructs a default %shuffle_order_engine engine.
1173 * The underlying engine is default constructed as well.
1175 shuffle_order_engine()
1177 { _M_initialize(); }
1180 * @brief Copy constructs a %shuffle_order_engine engine.
1182 * Copies an existing base class random number generator.
1183 * @param rng An existing (base class) engine object.
1186 shuffle_order_engine(const _RandomNumberEngine
& __rne
)
1188 { _M_initialize(); }
1191 * @brief Move constructs a %shuffle_order_engine engine.
1193 * Copies an existing base class random number generator.
1194 * @param rng An existing (base class) engine object.
1197 shuffle_order_engine(_RandomNumberEngine
&& __rne
)
1198 : _M_b(std::move(__rne
))
1199 { _M_initialize(); }
1202 * @brief Seed constructs a %shuffle_order_engine engine.
1204 * Constructs the underlying generator engine seeded with @p __s.
1205 * @param __s A seed value for the base class engine.
1208 shuffle_order_engine(result_type __s
)
1210 { _M_initialize(); }
1213 * @brief Generator construct a %shuffle_order_engine engine.
1215 * @param __q A seed sequence.
1218 shuffle_order_engine(seed_seq
& __q
)
1220 { _M_initialize(); }
1223 * @brief Reseeds the %shuffle_order_engine object with the default seed for
1224 * the underlying base class generator engine.
1234 * @brief Reseeds the %shuffle_order_engine object with the default seed
1235 * for the underlying base class generator engine.
1238 seed(result_type __s
)
1245 * @brief Reseeds the %shuffle_order_engine object with the given seed
1247 * @param __q A seed generator function.
1257 * Gets a const reference to the underlying generator engine object.
1259 const _RandomNumberEngine
&
1264 * Gets the minimum value in the generated random number range.
1266 * @todo This should be constexpr.
1270 { return _M_b
.min(); }
1273 * Gets the maximum value in the generated random number range.
1275 * @todo This should be constexpr.
1279 { return _M_b
.max(); }
1282 * Discard a sequence of random numbers.
1284 * @todo Look for a faster way to do discard.
1287 discard(unsigned long long __z
)
1289 for (; __z
!= 0ULL; --__z
)
1294 * Gets the next value in the generated random number sequence.
1300 * Compares two %shuffle_order_engine random number generator objects
1301 * of the same type for equality.
1303 * @param __lhs A %shuffle_order_engine random number generator object.
1304 * @param __rhs Another %shuffle_order_engine random number generator
1307 * @returns true if the two objects are equal, false otherwise.
1310 operator==(const shuffle_order_engine
& __lhs
,
1311 const shuffle_order_engine
& __rhs
)
1312 { return __lhs
._M_b
== __rhs
._M_b
; }
1315 * @brief Inserts the current state of a %shuffle_order_engine random
1316 * number generator engine @p __x into the output stream
1319 * @param __os An output stream.
1320 * @param __x A %shuffle_order_engine random number generator engine.
1322 * @returns The output stream with the state of @p __x inserted or in
1325 template<typename _RandomNumberEngine1
, size_t __k1
,
1326 typename _CharT
, typename _Traits
>
1327 friend std::basic_ostream
<_CharT
, _Traits
>&
1328 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1329 const shuffle_order_engine
<_RandomNumberEngine1
,
1333 * @brief Extracts the current state of a % subtract_with_carry_engine
1334 * random number generator engine @p __x from the input stream
1337 * @param __is An input stream.
1338 * @param __x A %shuffle_order_engine random number generator engine.
1340 * @returns The input stream with the state of @p __x extracted or in
1343 template<typename _RandomNumberEngine1
, size_t __k1
,
1344 typename _CharT
, typename _Traits
>
1345 friend std::basic_istream
<_CharT
, _Traits
>&
1346 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1347 shuffle_order_engine
<_RandomNumberEngine1
,
1351 void _M_initialize()
1353 for (size_t __i
= 0; __i
< __k
; ++__i
)
1358 _RandomNumberEngine _M_b
;
1359 result_type _M_v
[__k
];
1364 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1366 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1370 * An alternative LCR (Lehmer Generator function) .
1372 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1376 * The classic Mersenne Twister.
1379 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
1380 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
1381 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1383 typedef mersenne_twister_engine
<
1389 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1392 * An alternative Mersenne Twister.
1394 typedef mersenne_twister_engine
<
1397 0xb5026f5aa96619e9ULL
, 29,
1398 0x5555555555555555ULL
, 17,
1399 0x71d67fffeda60000ULL
, 37,
1400 0xfff7eee000000000ULL
, 43,
1401 6364136223846793005ULL> mt19937_64
;
1406 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1409 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1411 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1414 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1419 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1424 typedef minstd_rand0 default_random_engine
;
1427 * A standard interface to a platform-specific non-deterministic
1428 * random number generator (if any are available).
1433 /** The type of the generated random value. */
1434 typedef unsigned int result_type
;
1436 // constructors, destructors and member functions
1438 #ifdef _GLIBCXX_USE_RANDOM_TR1
1441 random_device(const std::string
& __token
= "/dev/urandom")
1443 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1444 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1445 std::__throw_runtime_error(__N("random_device::"
1446 "random_device(const std::string&)"));
1450 { std::fclose(_M_file
); }
1455 random_device(const std::string
& __token
= "mt19937")
1456 : _M_mt(_M_strtoul(__token
)) { }
1459 static unsigned long
1460 _M_strtoul(const std::string
& __str
)
1462 unsigned long __ret
= 5489UL;
1463 if (__str
!= "mt19937")
1465 const char* __nptr
= __str
.c_str();
1467 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1468 if (*__nptr
== '\0' || *__endptr
!= '\0')
1469 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1470 "(const std::string&)"));
1481 { return std::numeric_limits
<result_type
>::min(); }
1485 { return std::numeric_limits
<result_type
>::max(); }
1494 #ifdef _GLIBCXX_USE_RANDOM_TR1
1496 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1504 // No copy functions.
1505 random_device(const random_device
&) = delete;
1506 void operator=(const random_device
&) = delete;
1510 #ifdef _GLIBCXX_USE_RANDOM_TR1
1517 /* @} */ // group std_random_generators
1520 * @addtogroup std_random_distributions Random Number Distributions
1521 * @ingroup std_random
1526 * @addtogroup std_random_distributions_uniform Uniform Distributions
1527 * @ingroup std_random_distributions
1532 * @brief Uniform discrete distribution for random numbers.
1533 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1534 * probability throughout the range.
1536 template<typename _IntType
= int>
1537 class uniform_int_distribution
1539 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
1542 /** The type of the range of the distribution. */
1543 typedef _IntType result_type
;
1544 /** Parameter type. */
1547 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1550 param_type(_IntType __a
= 0, _IntType __b
= 9)
1551 : _M_a(__a
), _M_b(__b
)
1553 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1565 operator==(const param_type
& __p1
, const param_type
& __p2
)
1566 { return (__p1
._M_a
== __p2
._M_a
) && (__p1
._M_b
== __p2
._M_b
); }
1575 * @brief Constructs a uniform distribution object.
1578 uniform_int_distribution(_IntType __a
= 0, _IntType __b
= 9)
1579 : _M_param(__a
, __b
)
1583 uniform_int_distribution(const param_type
& __p
)
1588 * @brief Resets the distribution state.
1590 * Does nothing for the uniform integer distribution.
1597 { return _M_param
.a(); }
1601 { return _M_param
.b(); }
1604 * @brief Returns the inclusive lower bound of the distribution range.
1608 { return this->a(); }
1611 * @brief Returns the inclusive upper bound of the distribution range.
1615 { return this->b(); }
1618 * @brief Returns the parameter set of the distribution.
1622 { return _M_param
; }
1625 * @brief Sets the parameter set of the distribution.
1626 * @param __param The new parameter set of the distribution.
1629 param(const param_type
& __param
)
1630 { _M_param
= __param
; }
1633 * Gets a uniformly distributed random number in the range
1636 template<typename _UniformRandomNumberGenerator
>
1638 operator()(_UniformRandomNumberGenerator
& __urng
)
1640 typedef typename
_UniformRandomNumberGenerator::result_type
1642 return _M_call(__urng
, this->a(), this->b(),
1643 typename is_integral
<_UResult_type
>::type());
1647 * Gets a uniform random number in the range @f$[0, n)@f$.
1649 * This function is aimed at use with std::random_shuffle.
1651 template<typename _UniformRandomNumberGenerator
>
1653 operator()(_UniformRandomNumberGenerator
& __urng
,
1654 const param_type
& __p
)
1656 typedef typename
_UniformRandomNumberGenerator::result_type
1658 return _M_call(__urng
, __p
.a(), __p
.b(),
1659 typename is_integral
<_UResult_type
>::type());
1663 template<typename _UniformRandomNumberGenerator
>
1665 _M_call(_UniformRandomNumberGenerator
& __urng
,
1666 result_type __min
, result_type __max
, true_type
);
1668 template<typename _UniformRandomNumberGenerator
>
1670 _M_call(_UniformRandomNumberGenerator
& __urng
,
1671 result_type __min
, result_type __max
, false_type
)
1673 return result_type((__urng() - __urng
.min())
1674 / (__urng
.max() - __urng
.min())
1675 * (__max
- __min
+ 1)) + __min
;
1678 param_type _M_param
;
1682 * @brief Return true if two uniform integer distributions have
1683 * the same parameters.
1685 template<typename _IntType
>
1687 operator==(const uniform_int_distribution
<_IntType
>& __d1
,
1688 const uniform_int_distribution
<_IntType
>& __d2
)
1689 { return __d1
.param() == __d2
.param(); }
1692 * @brief Inserts a %uniform_int_distribution random number
1693 * distribution @p __x into the output stream @p os.
1695 * @param __os An output stream.
1696 * @param __x A %uniform_int_distribution random number distribution.
1698 * @returns The output stream with the state of @p __x inserted or in
1701 template<typename _IntType
, typename _CharT
, typename _Traits
>
1702 std::basic_ostream
<_CharT
, _Traits
>&
1703 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1704 const uniform_int_distribution
<_IntType
>& __x
);
1707 * @brief Extracts a %uniform_int_distribution random number distribution
1708 * @p __x from the input stream @p __is.
1710 * @param __is An input stream.
1711 * @param __x A %uniform_int_distribution random number generator engine.
1713 * @returns The input stream with @p __x extracted or in an error state.
1715 template<typename _IntType
, typename _CharT
, typename _Traits
>
1716 std::basic_istream
<_CharT
, _Traits
>&
1717 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1718 uniform_int_distribution
<_IntType
>& __x
);
1722 * @brief Uniform continuous distribution for random numbers.
1724 * A continuous random distribution on the range [min, max) with equal
1725 * probability throughout the range. The URNG should be real-valued and
1726 * deliver number in the range [0, 1).
1728 template<typename _RealType
= double>
1729 class uniform_real_distribution
1732 /** The type of the range of the distribution. */
1733 typedef _RealType result_type
;
1734 /** Parameter type. */
1737 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1740 param_type(_RealType __a
= _RealType(0),
1741 _RealType __b
= _RealType(1))
1742 : _M_a(__a
), _M_b(__b
)
1744 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1756 operator==(const param_type
& __p1
, const param_type
& __p2
)
1757 { return (__p1
._M_a
== __p2
._M_a
) && (__p1
._M_b
== __p2
._M_b
); }
1766 * @brief Constructs a uniform_real_distribution object.
1768 * @param __min [IN] The lower bound of the distribution.
1769 * @param __max [IN] The upper bound of the distribution.
1772 uniform_real_distribution(_RealType __a
= _RealType(0),
1773 _RealType __b
= _RealType(1))
1774 : _M_param(__a
, __b
)
1778 uniform_real_distribution(const param_type
& __p
)
1783 * @brief Resets the distribution state.
1785 * Does nothing for the uniform real distribution.
1792 { return _M_param
.a(); }
1796 { return _M_param
.b(); }
1799 * @brief Returns the inclusive lower bound of the distribution range.
1803 { return this->a(); }
1806 * @brief Returns the inclusive upper bound of the distribution range.
1810 { return this->b(); }
1813 * @brief Returns the parameter set of the distribution.
1817 { return _M_param
; }
1820 * @brief Sets the parameter set of the distribution.
1821 * @param __param The new parameter set of the distribution.
1824 param(const param_type
& __param
)
1825 { _M_param
= __param
; }
1827 template<typename _UniformRandomNumberGenerator
>
1829 operator()(_UniformRandomNumberGenerator
& __urng
)
1831 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1833 return (__aurng() * (this->b() - this->a())) + this->a();
1836 template<typename _UniformRandomNumberGenerator
>
1838 operator()(_UniformRandomNumberGenerator
& __urng
,
1839 const param_type
& __p
)
1841 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1843 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1847 param_type _M_param
;
1851 * @brief Return true if two uniform real distributions have
1852 * the same parameters.
1854 template<typename _IntType
>
1856 operator==(const uniform_real_distribution
<_IntType
>& __d1
,
1857 const uniform_real_distribution
<_IntType
>& __d2
)
1858 { return __d1
.param() == __d2
.param(); }
1861 * @brief Inserts a %uniform_real_distribution random number
1862 * distribution @p __x into the output stream @p __os.
1864 * @param __os An output stream.
1865 * @param __x A %uniform_real_distribution random number distribution.
1867 * @returns The output stream with the state of @p __x inserted or in
1870 template<typename _RealType
, typename _CharT
, typename _Traits
>
1871 std::basic_ostream
<_CharT
, _Traits
>&
1872 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1873 const uniform_real_distribution
<_RealType
>& __x
);
1876 * @brief Extracts a %uniform_real_distribution random number distribution
1877 * @p __x from the input stream @p __is.
1879 * @param __is An input stream.
1880 * @param __x A %uniform_real_distribution random number generator engine.
1882 * @returns The input stream with @p __x extracted or in an error state.
1884 template<typename _RealType
, typename _CharT
, typename _Traits
>
1885 std::basic_istream
<_CharT
, _Traits
>&
1886 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1887 uniform_real_distribution
<_RealType
>& __x
);
1889 /* @} */ // group std_random_distributions_uniform
1892 * @addtogroup std_random_distributions_normal Normal Distributions
1893 * @ingroup std_random_distributions
1898 * @brief A normal continuous distribution for random numbers.
1900 * The formula for the normal probability density function is
1901 * @f$ p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1902 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } @f$.
1904 template<typename _RealType
= double>
1905 class normal_distribution
1908 /** The type of the range of the distribution. */
1909 typedef _RealType result_type
;
1910 /** Parameter type. */
1913 typedef normal_distribution
<_RealType
> distribution_type
;
1916 param_type(_RealType __mean
= _RealType(0),
1917 _RealType __stddev
= _RealType(1))
1918 : _M_mean(__mean
), _M_stddev(__stddev
)
1920 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
1929 { return _M_stddev
; }
1932 operator==(const param_type
& __p1
, const param_type
& __p2
)
1933 { return (__p1
._M_mean
== __p2
._M_mean
)
1934 && (__p1
._M_stddev
== __p2
._M_stddev
); }
1938 _RealType _M_stddev
;
1943 * Constructs a normal distribution with parameters @f$ mean @f$ and
1944 * standard deviation.
1947 normal_distribution(result_type __mean
= result_type(0),
1948 result_type __stddev
= result_type(1))
1949 : _M_param(__mean
, __stddev
), _M_saved_available(false)
1953 normal_distribution(const param_type
& __p
)
1954 : _M_param(__p
), _M_saved_available(false)
1958 * @brief Resets the distribution state.
1962 { _M_saved_available
= false; }
1965 * @brief Returns the mean of the distribution.
1969 { return _M_param
.mean(); }
1972 * @brief Returns the standard deviation of the distribution.
1976 { return _M_param
.stddev(); }
1979 * @brief Returns the parameter set of the distribution.
1983 { return _M_param
; }
1986 * @brief Sets the parameter set of the distribution.
1987 * @param __param The new parameter set of the distribution.
1990 param(const param_type
& __param
)
1991 { _M_param
= __param
; }
1994 * @brief Returns the greatest lower bound value of the distribution.
1998 { return std::numeric_limits
<result_type
>::min(); }
2001 * @brief Returns the least upper bound value of the distribution.
2005 { return std::numeric_limits
<result_type
>::max(); }
2007 template<typename _UniformRandomNumberGenerator
>
2009 operator()(_UniformRandomNumberGenerator
& __urng
)
2010 { return this->operator()(__urng
, this->param()); }
2012 template<typename _UniformRandomNumberGenerator
>
2014 operator()(_UniformRandomNumberGenerator
& __urng
,
2015 const param_type
& __p
);
2018 * @brief Return true if two normal distributions have
2019 * the same parameters.
2021 template<typename _RealType1
>
2023 operator==(const normal_distribution
<_RealType1
>& __d1
,
2024 const normal_distribution
<_RealType1
>& __d2
);
2027 * @brief Inserts a %normal_distribution random number distribution
2028 * @p __x into the output stream @p __os.
2030 * @param __os An output stream.
2031 * @param __x A %normal_distribution random number distribution.
2033 * @returns The output stream with the state of @p __x inserted or in
2036 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2037 friend std::basic_ostream
<_CharT
, _Traits
>&
2038 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2039 const normal_distribution
<_RealType1
>& __x
);
2042 * @brief Extracts a %normal_distribution random number distribution
2043 * @p __x from the input stream @p __is.
2045 * @param __is An input stream.
2046 * @param __x A %normal_distribution random number generator engine.
2048 * @returns The input stream with @p __x extracted or in an error
2051 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2052 friend std::basic_istream
<_CharT
, _Traits
>&
2053 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2054 normal_distribution
<_RealType1
>& __x
);
2057 param_type _M_param
;
2058 result_type _M_saved
;
2059 bool _M_saved_available
;
2064 * @brief A lognormal_distribution random number distribution.
2066 * The formula for the normal probability mass function is
2067 * @f$ p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2068 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}} @f$
2070 template<typename _RealType
= double>
2071 class lognormal_distribution
2074 /** The type of the range of the distribution. */
2075 typedef _RealType result_type
;
2076 /** Parameter type. */
2079 typedef lognormal_distribution
<_RealType
> distribution_type
;
2082 param_type(_RealType __m
= _RealType(0),
2083 _RealType __s
= _RealType(1))
2084 : _M_m(__m
), _M_s(__s
)
2096 operator==(const param_type
& __p1
, const param_type
& __p2
)
2097 { return (__p1
._M_m
== __p2
._M_m
) && (__p1
._M_s
== __p2
._M_s
); }
2105 lognormal_distribution(_RealType __m
= _RealType(0),
2106 _RealType __s
= _RealType(1))
2107 : _M_param(__m
, __s
)
2111 lognormal_distribution(const param_type
& __p
)
2116 * Resets the distribution state.
2127 { return _M_param
.m(); }
2131 { return _M_param
.s(); }
2134 * @brief Returns the parameter set of the distribution.
2138 { return _M_param
; }
2141 * @brief Sets the parameter set of the distribution.
2142 * @param __param The new parameter set of the distribution.
2145 param(const param_type
& __param
)
2146 { _M_param
= __param
; }
2149 * @brief Returns the greatest lower bound value of the distribution.
2153 { return result_type(0); }
2156 * @brief Returns the least upper bound value of the distribution.
2160 { return std::numeric_limits
<result_type
>::max(); }
2162 template<typename _UniformRandomNumberGenerator
>
2164 operator()(_UniformRandomNumberGenerator
& __urng
)
2165 { return this->operator()(__urng
, this->param()); }
2167 template<typename _UniformRandomNumberGenerator
>
2169 operator()(_UniformRandomNumberGenerator
& __urng
,
2170 const param_type
& __p
);
2173 param_type _M_param
;
2177 * @brief Return true if two lognormal distributions have
2178 * the same parameters.
2180 template<typename _RealType
>
2182 operator==(const lognormal_distribution
<_RealType
>& __d1
,
2183 const lognormal_distribution
<_RealType
>& __d2
)
2184 { return __d1
.param() == __d2
.param(); }
2187 * @brief Inserts a %lognormal_distribution random number distribution
2188 * @p __x into the output stream @p __os.
2190 * @param __os An output stream.
2191 * @param __x A %lognormal_distribution random number distribution.
2193 * @returns The output stream with the state of @p __x inserted or in
2196 template<typename _RealType
, typename _CharT
, typename _Traits
>
2197 std::basic_ostream
<_CharT
, _Traits
>&
2198 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2199 const lognormal_distribution
<_RealType
>& __x
);
2202 * @brief Extracts a %lognormal_distribution random number distribution
2203 * @p __x from the input stream @p __is.
2205 * @param __is An input stream.
2206 * @param __x A %lognormal_distribution random number
2209 * @returns The input stream with @p __x extracted or in an error state.
2211 template<typename _RealType
, typename _CharT
, typename _Traits
>
2212 std::basic_istream
<_CharT
, _Traits
>&
2213 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2214 lognormal_distribution
<_RealType
>& __x
);
2218 * @brief A chi_squared_distribution random number distribution.
2220 * The formula for the normal probability mass function is
2221 * @f$ p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}} @f$
2223 template<typename _RealType
= double>
2224 class chi_squared_distribution
2227 /** The type of the range of the distribution. */
2228 typedef _RealType result_type
;
2229 /** Parameter type. */
2232 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2235 param_type(_RealType __n
= _RealType(1))
2244 operator==(const param_type
& __p1
, const param_type
& __p2
)
2245 { return __p1
._M_n
== __p2
._M_n
; }
2252 chi_squared_distribution(_RealType __n
= _RealType(1))
2257 chi_squared_distribution(const param_type
& __p
)
2262 * @brief Resets the distribution state.
2273 { return _M_param
.n(); }
2276 * @brief Returns the parameter set of the distribution.
2280 { return _M_param
; }
2283 * @brief Sets the parameter set of the distribution.
2284 * @param __param The new parameter set of the distribution.
2287 param(const param_type
& __param
)
2288 { _M_param
= __param
; }
2291 * @brief Returns the greatest lower bound value of the distribution.
2295 { return result_type(0); }
2298 * @brief Returns the least upper bound value of the distribution.
2302 { return std::numeric_limits
<result_type
>::max(); }
2304 template<typename _UniformRandomNumberGenerator
>
2306 operator()(_UniformRandomNumberGenerator
& __urng
)
2307 { return this->operator()(__urng
, this->param()); }
2309 template<typename _UniformRandomNumberGenerator
>
2311 operator()(_UniformRandomNumberGenerator
& __urng
,
2312 const param_type
& __p
);
2315 param_type _M_param
;
2319 * @brief Return true if two Chi-squared distributions have
2320 * the same parameters.
2322 template<typename _RealType
>
2324 operator==(const chi_squared_distribution
<_RealType
>& __d1
,
2325 const chi_squared_distribution
<_RealType
>& __d2
)
2326 { return __d1
.param() == __d2
.param(); }
2329 * @brief Inserts a %chi_squared_distribution random number distribution
2330 * @p __x into the output stream @p __os.
2332 * @param __os An output stream.
2333 * @param __x A %chi_squared_distribution random number distribution.
2335 * @returns The output stream with the state of @p __x inserted or in
2338 template<typename _RealType
, typename _CharT
, typename _Traits
>
2339 std::basic_ostream
<_CharT
, _Traits
>&
2340 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2341 const chi_squared_distribution
<_RealType
>& __x
);
2344 * @brief Extracts a %chi_squared_distribution random number distribution
2345 * @p __x from the input stream @p __is.
2347 * @param __is An input stream.
2348 * @param __x A %chi_squared_distribution random number
2351 * @returns The input stream with @p __x extracted or in an error state.
2353 template<typename _RealType
, typename _CharT
, typename _Traits
>
2354 std::basic_istream
<_CharT
, _Traits
>&
2355 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2356 chi_squared_distribution
<_RealType
>& __x
);
2360 * @brief A cauchy_distribution random number distribution.
2362 * The formula for the normal probability mass function is
2363 * @f$ p(x|a,b) = \( \pi b \( 1 + \( \frac{x-a}{b} \)^2 \) \)^{-1} @f$
2365 template<typename _RealType
= double>
2366 class cauchy_distribution
2369 /** The type of the range of the distribution. */
2370 typedef _RealType result_type
;
2371 /** Parameter type. */
2374 typedef cauchy_distribution
<_RealType
> distribution_type
;
2377 param_type(_RealType __a
= _RealType(0),
2378 _RealType __b
= _RealType(1))
2379 : _M_a(__a
), _M_b(__b
)
2391 operator==(const param_type
& __p1
, const param_type
& __p2
)
2392 { return (__p1
._M_a
== __p2
._M_a
) && (__p1
._M_b
== __p2
._M_b
); }
2400 cauchy_distribution(_RealType __a
= _RealType(0),
2401 _RealType __b
= _RealType(1))
2402 : _M_param(__a
, __b
)
2406 cauchy_distribution(const param_type
& __p
)
2411 * @brief Resets the distribution state.
2422 { return _M_param
.a(); }
2426 { return _M_param
.b(); }
2429 * @brief Returns the parameter set of the distribution.
2433 { return _M_param
; }
2436 * @brief Sets the parameter set of the distribution.
2437 * @param __param The new parameter set of the distribution.
2440 param(const param_type
& __param
)
2441 { _M_param
= __param
; }
2444 * @brief Returns the greatest lower bound value of the distribution.
2448 { return std::numeric_limits
<result_type
>::min(); }
2451 * @brief Returns the least upper bound value of the distribution.
2455 { return std::numeric_limits
<result_type
>::max(); }
2457 template<typename _UniformRandomNumberGenerator
>
2459 operator()(_UniformRandomNumberGenerator
& __urng
)
2460 { return this->operator()(__urng
, this->param()); }
2462 template<typename _UniformRandomNumberGenerator
>
2464 operator()(_UniformRandomNumberGenerator
& __urng
,
2465 const param_type
& __p
);
2468 param_type _M_param
;
2472 * @brief Return true if two Cauchy distributions have
2473 * the same parameters.
2475 template<typename _RealType
>
2477 operator==(const cauchy_distribution
<_RealType
>& __d1
,
2478 const cauchy_distribution
<_RealType
>& __d2
)
2479 { return __d1
.param() == __d2
.param(); }
2482 * @brief Inserts a %cauchy_distribution random number distribution
2483 * @p __x into the output stream @p __os.
2485 * @param __os An output stream.
2486 * @param __x A %cauchy_distribution random number distribution.
2488 * @returns The output stream with the state of @p __x inserted or in
2491 template<typename _RealType
, typename _CharT
, typename _Traits
>
2492 std::basic_ostream
<_CharT
, _Traits
>&
2493 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2494 const cauchy_distribution
<_RealType
>& __x
);
2497 * @brief Extracts a %cauchy_distribution random number distribution
2498 * @p __x from the input stream @p __is.
2500 * @param __is An input stream.
2501 * @param __x A %cauchy_distribution random number
2504 * @returns The input stream with @p __x extracted or in an error state.
2506 template<typename _RealType
, typename _CharT
, typename _Traits
>
2507 std::basic_istream
<_CharT
, _Traits
>&
2508 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2509 cauchy_distribution
<_RealType
>& __x
);
2513 * @brief A fisher_f_distribution random number distribution.
2515 * The formula for the normal probability mass function is
2516 * @f$ p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2517 * \(\frac{m}{n}\)^{m/2} x^{(m/2)-1}
2518 * \( 1 + \frac{mx}{n} \)^{-(m+n)/2} @f$
2520 template<typename _RealType
= double>
2521 class fisher_f_distribution
2524 /** The type of the range of the distribution. */
2525 typedef _RealType result_type
;
2526 /** Parameter type. */
2529 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2532 param_type(_RealType __m
= _RealType(1),
2533 _RealType __n
= _RealType(1))
2534 : _M_m(__m
), _M_n(__n
)
2546 operator==(const param_type
& __p1
, const param_type
& __p2
)
2547 { return (__p1
._M_m
== __p2
._M_m
) && (__p1
._M_n
== __p2
._M_n
); }
2555 fisher_f_distribution(_RealType __m
= _RealType(1),
2556 _RealType __n
= _RealType(1))
2557 : _M_param(__m
, __n
)
2561 fisher_f_distribution(const param_type
& __p
)
2566 * @brief Resets the distribution state.
2577 { return _M_param
.m(); }
2581 { return _M_param
.n(); }
2584 * @brief Returns the parameter set of the distribution.
2588 { return _M_param
; }
2591 * @brief Sets the parameter set of the distribution.
2592 * @param __param The new parameter set of the distribution.
2595 param(const param_type
& __param
)
2596 { _M_param
= __param
; }
2599 * @brief Returns the greatest lower bound value of the distribution.
2603 { return result_type(0); }
2606 * @brief Returns the least upper bound value of the distribution.
2610 { return std::numeric_limits
<result_type
>::max(); }
2612 template<typename _UniformRandomNumberGenerator
>
2614 operator()(_UniformRandomNumberGenerator
& __urng
)
2615 { return this->operator()(__urng
, this->param()); }
2617 template<typename _UniformRandomNumberGenerator
>
2619 operator()(_UniformRandomNumberGenerator
& __urng
,
2620 const param_type
& __p
);
2623 param_type _M_param
;
2627 * @brief Return true if two Fisher f distributions have
2628 * the same parameters.
2630 template<typename _RealType
>
2632 operator==(const fisher_f_distribution
<_RealType
>& __d1
,
2633 const fisher_f_distribution
<_RealType
>& __d2
)
2634 { return __d1
.param() == __d2
.param(); }
2637 * @brief Inserts a %fisher_f_distribution random number distribution
2638 * @p __x into the output stream @p __os.
2640 * @param __os An output stream.
2641 * @param __x A %fisher_f_distribution random number distribution.
2643 * @returns The output stream with the state of @p __x inserted or in
2646 template<typename _RealType
, typename _CharT
, typename _Traits
>
2647 std::basic_ostream
<_CharT
, _Traits
>&
2648 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2649 const fisher_f_distribution
<_RealType
>& __x
);
2652 * @brief Extracts a %fisher_f_distribution random number distribution
2653 * @p __x from the input stream @p __is.
2655 * @param __is An input stream.
2656 * @param __x A %fisher_f_distribution random number
2659 * @returns The input stream with @p __x extracted or in an error state.
2661 template<typename _RealType
, typename _CharT
, typename _Traits
>
2662 std::basic_istream
<_CharT
, _Traits
>&
2663 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2664 fisher_f_distribution
<_RealType
>& __x
);
2668 * @brief A student_t_distribution random number distribution.
2670 * The formula for the normal probability mass function is
2671 * @f$ p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2672 * \( 1 + \frac{x^2}{n} \) ^{-(n+1)/2} @f$
2674 template<typename _RealType
= double>
2675 class student_t_distribution
2678 /** The type of the range of the distribution. */
2679 typedef _RealType result_type
;
2680 /** Parameter type. */
2683 typedef student_t_distribution
<_RealType
> distribution_type
;
2686 param_type(_RealType __n
= _RealType(1))
2695 operator==(const param_type
& __p1
, const param_type
& __p2
)
2696 { return __p1
._M_n
== __p2
._M_n
; }
2703 student_t_distribution(_RealType __n
= _RealType(1))
2708 student_t_distribution(const param_type
& __p
)
2713 * @brief Resets the distribution state.
2724 { return _M_param
.n(); }
2727 * @brief Returns the parameter set of the distribution.
2731 { return _M_param
; }
2734 * @brief Sets the parameter set of the distribution.
2735 * @param __param The new parameter set of the distribution.
2738 param(const param_type
& __param
)
2739 { _M_param
= __param
; }
2742 * @brief Returns the greatest lower bound value of the distribution.
2746 { return std::numeric_limits
<result_type
>::min(); }
2749 * @brief Returns the least upper bound value of the distribution.
2753 { return std::numeric_limits
<result_type
>::max(); }
2755 template<typename _UniformRandomNumberGenerator
>
2757 operator()(_UniformRandomNumberGenerator
& __urng
)
2758 { return this->operator()(__urng
, this->param()); }
2760 template<typename _UniformRandomNumberGenerator
>
2762 operator()(_UniformRandomNumberGenerator
& __urng
,
2763 const param_type
& __p
);
2766 template<typename _UniformRandomNumberGenerator
>
2768 _M_gaussian(_UniformRandomNumberGenerator
& __urng
,
2769 const result_type __sigma
);
2771 param_type _M_param
;
2775 * @brief Return true if two Student t distributions have
2776 * the same parameters.
2778 template<typename _RealType
>
2780 operator==(const student_t_distribution
<_RealType
>& __d1
,
2781 const student_t_distribution
<_RealType
>& __d2
)
2782 { return __d1
.param() == __d2
.param(); }
2785 * @brief Inserts a %student_t_distribution random number distribution
2786 * @p __x into the output stream @p __os.
2788 * @param __os An output stream.
2789 * @param __x A %student_t_distribution random number distribution.
2791 * @returns The output stream with the state of @p __x inserted or in
2794 template<typename _RealType
, typename _CharT
, typename _Traits
>
2795 std::basic_ostream
<_CharT
, _Traits
>&
2796 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2797 const student_t_distribution
<_RealType
>& __x
);
2800 * @brief Extracts a %student_t_distribution random number distribution
2801 * @p __x from the input stream @p __is.
2803 * @param __is An input stream.
2804 * @param __x A %student_t_distribution random number
2807 * @returns The input stream with @p __x extracted or in an error state.
2809 template<typename _RealType
, typename _CharT
, typename _Traits
>
2810 std::basic_istream
<_CharT
, _Traits
>&
2811 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2812 student_t_distribution
<_RealType
>& __x
);
2814 /* @} */ // group std_random_distributions_normal
2817 * @addtogroup std_random_distributions_bernoulli Bernoulli Distributions
2818 * @ingroup std_random_distributions
2823 * @brief A Bernoulli random number distribution.
2825 * Generates a sequence of true and false values with likelihood @f$ p @f$
2826 * that true will come up and @f$ (1 - p) @f$ that false will appear.
2828 class bernoulli_distribution
2831 /** The type of the range of the distribution. */
2832 typedef bool result_type
;
2833 /** Parameter type. */
2836 typedef bernoulli_distribution distribution_type
;
2839 param_type(double __p
= 0.5)
2842 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
2850 operator==(const param_type
& __p1
, const param_type
& __p2
)
2851 { return __p1
._M_p
== __p2
._M_p
; }
2859 * @brief Constructs a Bernoulli distribution with likelihood @p p.
2861 * @param __p [IN] The likelihood of a true result being returned.
2862 * Must be in the interval @f$ [0, 1] @f$.
2865 bernoulli_distribution(double __p
= 0.5)
2870 bernoulli_distribution(const param_type
& __p
)
2875 * @brief Resets the distribution state.
2877 * Does nothing for a Bernoulli distribution.
2883 * @brief Returns the @p p parameter of the distribution.
2887 { return _M_param
.p(); }
2890 * @brief Returns the parameter set of the distribution.
2894 { return _M_param
; }
2897 * @brief Sets the parameter set of the distribution.
2898 * @param __param The new parameter set of the distribution.
2901 param(const param_type
& __param
)
2902 { _M_param
= __param
; }
2905 * @brief Returns the greatest lower bound value of the distribution.
2909 { return std::numeric_limits
<result_type
>::min(); }
2912 * @brief Returns the least upper bound value of the distribution.
2916 { return std::numeric_limits
<result_type
>::max(); }
2919 * @brief Returns the next value in the Bernoullian sequence.
2921 template<typename _UniformRandomNumberGenerator
>
2923 operator()(_UniformRandomNumberGenerator
& __urng
)
2925 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
2927 if ((__aurng() - __aurng
.min())
2928 < this->p() * (__aurng
.max() - __aurng
.min()))
2933 template<typename _UniformRandomNumberGenerator
>
2935 operator()(_UniformRandomNumberGenerator
& __urng
,
2936 const param_type
& __p
)
2938 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
2940 if ((__aurng() - __aurng
.min())
2941 < __p
.p() * (__aurng
.max() - __aurng
.min()))
2947 param_type _M_param
;
2951 * @brief Return true if two Bernoulli distributions have
2952 * the same parameters.
2955 operator==(const bernoulli_distribution
& __d1
,
2956 const bernoulli_distribution
& __d2
)
2957 { return __d1
.param() == __d2
.param(); }
2960 * @brief Inserts a %bernoulli_distribution random number distribution
2961 * @p __x into the output stream @p __os.
2963 * @param __os An output stream.
2964 * @param __x A %bernoulli_distribution random number distribution.
2966 * @returns The output stream with the state of @p __x inserted or in
2969 template<typename _CharT
, typename _Traits
>
2970 std::basic_ostream
<_CharT
, _Traits
>&
2971 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2972 const bernoulli_distribution
& __x
);
2975 * @brief Extracts a %bernoulli_distribution random number distribution
2976 * @p __x from the input stream @p __is.
2978 * @param __is An input stream.
2979 * @param __x A %bernoulli_distribution random number generator engine.
2981 * @returns The input stream with @p __x extracted or in an error state.
2983 template<typename _CharT
, typename _Traits
>
2984 std::basic_istream
<_CharT
, _Traits
>&
2985 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2986 bernoulli_distribution
& __x
)
2990 __x
.param(bernoulli_distribution::param_type(__p
));
2996 * @brief A discrete binomial random number distribution.
2998 * The formula for the binomial probability density function is
2999 * @f$ p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3000 * and @f$ p @f$ are the parameters of the distribution.
3002 template<typename _IntType
= int>
3003 class binomial_distribution
3005 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3008 /** The type of the range of the distribution. */
3009 typedef _IntType result_type
;
3010 /** Parameter type. */
3013 typedef binomial_distribution
<_IntType
> distribution_type
;
3014 friend class binomial_distribution
<_IntType
>;
3017 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3018 : _M_t(__t
), _M_p(__p
)
3020 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3035 operator==(const param_type
& __p1
, const param_type
& __p2
)
3036 { return (__p1
._M_t
== __p2
._M_t
) && (__p1
._M_p
== __p2
._M_p
); }
3046 #if _GLIBCXX_USE_C99_MATH_TR1
3047 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3048 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3053 // constructors and member function
3055 binomial_distribution(_IntType __t
= _IntType(1),
3057 : _M_param(__t
, __p
), _M_nd()
3061 binomial_distribution(const param_type
& __p
)
3062 : _M_param(__p
), _M_nd()
3066 * @brief Resets the distribution state.
3073 * @brief Returns the distribution @p t parameter.
3077 { return _M_param
.t(); }
3080 * @brief Returns the distribution @p p parameter.
3084 { return _M_param
.p(); }
3087 * @brief Returns the parameter set of the distribution.
3091 { return _M_param
; }
3094 * @brief Sets the parameter set of the distribution.
3095 * @param __param The new parameter set of the distribution.
3098 param(const param_type
& __param
)
3099 { _M_param
= __param
; }
3102 * @brief Returns the greatest lower bound value of the distribution.
3109 * @brief Returns the least upper bound value of the distribution.
3113 { return _M_param
.t(); }
3116 * @brief Return true if two binomial distributions have
3117 * the same parameters.
3119 template<typename _IntType1
>
3121 operator==(const binomial_distribution
<_IntType1
>& __d1
,
3122 const binomial_distribution
<_IntType1
>& __d2
)
3123 { return (__d1
.param() == __d2
.param())
3124 && (__d1
._M_nd
== __d2
._M_nd
); }
3126 template<typename _UniformRandomNumberGenerator
>
3128 operator()(_UniformRandomNumberGenerator
& __urng
)
3129 { return this->operator()(__urng
, this->param()); }
3131 template<typename _UniformRandomNumberGenerator
>
3133 operator()(_UniformRandomNumberGenerator
& __urng
,
3134 const param_type
& __p
);
3137 * @brief Inserts a %binomial_distribution random number distribution
3138 * @p __x into the output stream @p __os.
3140 * @param __os An output stream.
3141 * @param __x A %binomial_distribution random number distribution.
3143 * @returns The output stream with the state of @p __x inserted or in
3146 template<typename _IntType1
,
3147 typename _CharT
, typename _Traits
>
3148 friend std::basic_ostream
<_CharT
, _Traits
>&
3149 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3150 const binomial_distribution
<_IntType1
>& __x
);
3153 * @brief Extracts a %binomial_distribution random number distribution
3154 * @p __x from the input stream @p __is.
3156 * @param __is An input stream.
3157 * @param __x A %binomial_distribution random number generator engine.
3159 * @returns The input stream with @p __x extracted or in an error
3162 template<typename _IntType1
,
3163 typename _CharT
, typename _Traits
>
3164 friend std::basic_istream
<_CharT
, _Traits
>&
3165 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3166 binomial_distribution
<_IntType1
>& __x
);
3169 template<typename _UniformRandomNumberGenerator
>
3171 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3173 param_type _M_param
;
3175 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3176 normal_distribution
<double> _M_nd
;
3181 * @brief A discrete geometric random number distribution.
3183 * The formula for the geometric probability density function is
3184 * @f$ p(i|p) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
3187 template<typename _IntType
= int>
3188 class geometric_distribution
3190 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3193 /** The type of the range of the distribution. */
3194 typedef _IntType result_type
;
3195 /** Parameter type. */
3198 typedef geometric_distribution
<_IntType
> distribution_type
;
3199 friend class geometric_distribution
<_IntType
>;
3202 param_type(double __p
= 0.5)
3205 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0)
3215 operator==(const param_type
& __p1
, const param_type
& __p2
)
3216 { return __p1
._M_p
== __p2
._M_p
; }
3221 { _M_log_p
= std::log(_M_p
); }
3228 // constructors and member function
3230 geometric_distribution(double __p
= 0.5)
3235 geometric_distribution(const param_type
& __p
)
3240 * @brief Resets the distribution state.
3242 * Does nothing for the geometric distribution.
3248 * @brief Returns the distribution parameter @p p.
3252 { return _M_param
.p(); }
3255 * @brief Returns the parameter set of the distribution.
3259 { return _M_param
; }
3262 * @brief Sets the parameter set of the distribution.
3263 * @param __param The new parameter set of the distribution.
3266 param(const param_type
& __param
)
3267 { _M_param
= __param
; }
3270 * @brief Returns the greatest lower bound value of the distribution.
3277 * @brief Returns the least upper bound value of the distribution.
3281 { return std::numeric_limits
<result_type
>::max(); }
3283 template<typename _UniformRandomNumberGenerator
>
3285 operator()(_UniformRandomNumberGenerator
& __urng
)
3286 { return this->operator()(__urng
, this->param()); }
3288 template<typename _UniformRandomNumberGenerator
>
3290 operator()(_UniformRandomNumberGenerator
& __urng
,
3291 const param_type
& __p
);
3294 param_type _M_param
;
3298 * @brief Return true if two geometric distributions have
3299 * the same parameters.
3301 template<typename _IntType
>
3303 operator==(const geometric_distribution
<_IntType
>& __d1
,
3304 const geometric_distribution
<_IntType
>& __d2
)
3305 { return __d1
.param() == __d2
.param(); }
3308 * @brief Inserts a %geometric_distribution random number distribution
3309 * @p __x into the output stream @p __os.
3311 * @param __os An output stream.
3312 * @param __x A %geometric_distribution random number distribution.
3314 * @returns The output stream with the state of @p __x inserted or in
3317 template<typename _IntType
,
3318 typename _CharT
, typename _Traits
>
3319 std::basic_ostream
<_CharT
, _Traits
>&
3320 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3321 const geometric_distribution
<_IntType
>& __x
);
3324 * @brief Extracts a %geometric_distribution random number distribution
3325 * @p __x from the input stream @p __is.
3327 * @param __is An input stream.
3328 * @param __x A %geometric_distribution random number generator engine.
3330 * @returns The input stream with @p __x extracted or in an error state.
3332 template<typename _IntType
,
3333 typename _CharT
, typename _Traits
>
3334 std::basic_istream
<_CharT
, _Traits
>&
3335 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3336 geometric_distribution
<_IntType
>& __x
);
3340 * @brief A negative_binomial_distribution random number distribution.
3342 * The formula for the negative binomial probability mass function is
3343 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3344 * and @f$ p @f$ are the parameters of the distribution.
3346 template<typename _IntType
= int>
3347 class negative_binomial_distribution
3349 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3352 /** The type of the range of the distribution. */
3353 typedef _IntType result_type
;
3354 /** Parameter type. */
3357 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3360 param_type(_IntType __k
= 1, double __p
= 0.5)
3361 : _M_k(__k
), _M_p(__p
)
3373 operator==(const param_type
& __p1
, const param_type
& __p2
)
3374 { return (__p1
._M_k
== __p2
._M_k
) && (__p1
._M_p
== __p2
._M_p
); }
3382 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3383 : _M_param(__k
, __p
)
3387 negative_binomial_distribution(const param_type
& __p
)
3392 * @brief Resets the distribution state.
3399 * @brief Return the @f$ k @f$ parameter of the distribution.
3403 { return _M_param
.k(); }
3406 * @brief Return the @f$ p @f$ parameter of the distribution.
3410 { return _M_param
.p(); }
3413 * @brief Returns the parameter set of the distribution.
3417 { return _M_param
; }
3420 * @brief Sets the parameter set of the distribution.
3421 * @param __param The new parameter set of the distribution.
3424 param(const param_type
& __param
)
3425 { _M_param
= __param
; }
3428 * @brief Returns the greatest lower bound value of the distribution.
3432 { return result_type(0); }
3435 * @brief Returns the least upper bound value of the distribution.
3439 { return std::numeric_limits
<result_type
>::max(); }
3441 template<typename _UniformRandomNumberGenerator
>
3443 operator()(_UniformRandomNumberGenerator
& __urng
)
3444 { return this->operator()(__urng
, this->param()); }
3446 template<typename _UniformRandomNumberGenerator
>
3448 operator()(_UniformRandomNumberGenerator
& __urng
,
3449 const param_type
& __p
);
3452 param_type _M_param
;
3456 * @brief Return true if two negative binomial distributions have
3457 * the same parameters.
3459 template<typename _IntType
>
3461 operator==(const negative_binomial_distribution
<_IntType
>& __d1
,
3462 const negative_binomial_distribution
<_IntType
>& __d2
)
3463 { return __d1
.param() == __d2
.param(); }
3466 * @brief Inserts a %negative_binomial_distribution random
3467 * number distribution @p __x into the output stream @p __os.
3469 * @param __os An output stream.
3470 * @param __x A %negative_binomial_distribution random number
3473 * @returns The output stream with the state of @p __x inserted or in
3476 template<typename _IntType
, typename _CharT
, typename _Traits
>
3477 std::basic_ostream
<_CharT
, _Traits
>&
3478 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3479 const negative_binomial_distribution
<_IntType
>& __x
);
3482 * @brief Extracts a %negative_binomial_distribution random number
3483 * distribution @p __x from the input stream @p __is.
3485 * @param __is An input stream.
3486 * @param __x A %negative_binomial_distribution random number
3489 * @returns The input stream with @p __x extracted or in an error state.
3491 template<typename _IntType
, typename _CharT
, typename _Traits
>
3492 std::basic_istream
<_CharT
, _Traits
>&
3493 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3494 negative_binomial_distribution
<_IntType
>& __x
);
3496 /* @} */ // group std_random_distributions_bernoulli
3499 * @addtogroup std_random_distributions_poisson Poisson Distributions
3500 * @ingroup std_random_distributions
3505 * @brief A discrete Poisson random number distribution.
3507 * The formula for the Poisson probability density function is
3508 * @f$ p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu} @f$ where @f$ \mu @f$ is the
3509 * parameter of the distribution.
3511 template<typename _IntType
= int>
3512 class poisson_distribution
3514 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3517 /** The type of the range of the distribution. */
3518 typedef _IntType result_type
;
3519 /** Parameter type. */
3522 typedef poisson_distribution
<_IntType
> distribution_type
;
3523 friend class poisson_distribution
<_IntType
>;
3526 param_type(double __mean
= 1.0)
3529 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3538 operator==(const param_type
& __p1
, const param_type
& __p2
)
3539 { return __p1
._M_mean
== __p2
._M_mean
; }
3542 // Hosts either log(mean) or the threshold of the simple method.
3549 #if _GLIBCXX_USE_C99_MATH_TR1
3550 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
3554 // constructors and member function
3556 poisson_distribution(double __mean
= 1.0)
3557 : _M_param(__mean
), _M_nd()
3561 poisson_distribution(const param_type
& __p
)
3562 : _M_param(__p
), _M_nd()
3566 * @brief Resets the distribution state.
3573 * @brief Returns the distribution parameter @p mean.
3577 { return _M_param
.mean(); }
3580 * @brief Returns the parameter set of the distribution.
3584 { return _M_param
; }
3587 * @brief Sets the parameter set of the distribution.
3588 * @param __param The new parameter set of the distribution.
3591 param(const param_type
& __param
)
3592 { _M_param
= __param
; }
3595 * @brief Returns the greatest lower bound value of the distribution.
3602 * @brief Returns the least upper bound value of the distribution.
3606 { return std::numeric_limits
<result_type
>::max(); }
3608 template<typename _UniformRandomNumberGenerator
>
3610 operator()(_UniformRandomNumberGenerator
& __urng
)
3611 { return this->operator()(__urng
, this->param()); }
3613 template<typename _UniformRandomNumberGenerator
>
3615 operator()(_UniformRandomNumberGenerator
& __urng
,
3616 const param_type
& __p
);
3619 * @brief Return true if two Poisson distributions have the same
3622 template<typename _IntType1
>
3624 operator==(const poisson_distribution
<_IntType1
>& __d1
,
3625 const poisson_distribution
<_IntType1
>& __d2
)
3626 { return (__d1
.param() == __d2
.param())
3627 && (__d1
._M_nd
== __d2
._M_nd
); }
3630 * @brief Inserts a %poisson_distribution random number distribution
3631 * @p __x into the output stream @p __os.
3633 * @param __os An output stream.
3634 * @param __x A %poisson_distribution random number distribution.
3636 * @returns The output stream with the state of @p __x inserted or in
3639 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3640 friend std::basic_ostream
<_CharT
, _Traits
>&
3641 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3642 const poisson_distribution
<_IntType1
>& __x
);
3645 * @brief Extracts a %poisson_distribution random number distribution
3646 * @p __x from the input stream @p __is.
3648 * @param __is An input stream.
3649 * @param __x A %poisson_distribution random number generator engine.
3651 * @returns The input stream with @p __x extracted or in an error
3654 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3655 friend std::basic_istream
<_CharT
, _Traits
>&
3656 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3657 poisson_distribution
<_IntType1
>& __x
);
3660 param_type _M_param
;
3662 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3663 normal_distribution
<double> _M_nd
;
3667 * @brief An exponential continuous distribution for random numbers.
3669 * The formula for the exponential probability density function is
3670 * @f$ p(x|\lambda) = \lambda e^{-\lambda x} @f$.
3672 * <table border=1 cellpadding=10 cellspacing=0>
3673 * <caption align=top>Distribution Statistics</caption>
3674 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3675 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
3676 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
3677 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3678 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3681 template<typename _RealType
= double>
3682 class exponential_distribution
3685 /** The type of the range of the distribution. */
3686 typedef _RealType result_type
;
3687 /** Parameter type. */
3690 typedef exponential_distribution
<_RealType
> distribution_type
;
3693 param_type(_RealType __lambda
= _RealType(1))
3694 : _M_lambda(__lambda
)
3696 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
3701 { return _M_lambda
; }
3704 operator==(const param_type
& __p1
, const param_type
& __p2
)
3705 { return __p1
._M_lambda
== __p2
._M_lambda
; }
3708 _RealType _M_lambda
;
3713 * @brief Constructs an exponential distribution with inverse scale
3714 * parameter @f$ \lambda @f$.
3717 exponential_distribution(const result_type
& __lambda
= result_type(1))
3718 : _M_param(__lambda
)
3722 exponential_distribution(const param_type
& __p
)
3727 * @brief Resets the distribution state.
3729 * Has no effect on exponential distributions.
3735 * @brief Returns the inverse scale parameter of the distribution.
3739 { return _M_param
.lambda(); }
3742 * @brief Returns the parameter set of the distribution.
3746 { return _M_param
; }
3749 * @brief Sets the parameter set of the distribution.
3750 * @param __param The new parameter set of the distribution.
3753 param(const param_type
& __param
)
3754 { _M_param
= __param
; }
3757 * @brief Returns the greatest lower bound value of the distribution.
3761 { return result_type(0); }
3764 * @brief Returns the least upper bound value of the distribution.
3768 { return std::numeric_limits
<result_type
>::max(); }
3770 template<typename _UniformRandomNumberGenerator
>
3772 operator()(_UniformRandomNumberGenerator
& __urng
)
3774 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
3776 return -std::log(__aurng()) / this->lambda();
3779 template<typename _UniformRandomNumberGenerator
>
3781 operator()(_UniformRandomNumberGenerator
& __urng
,
3782 const param_type
& __p
)
3784 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
3786 return -std::log(__aurng()) / __p
.lambda();
3790 param_type _M_param
;
3794 * @brief Return true if two exponential distributions have the same
3797 template<typename _RealType
>
3799 operator==(const exponential_distribution
<_RealType
>& __d1
,
3800 const exponential_distribution
<_RealType
>& __d2
)
3801 { return __d1
.param() == __d2
.param(); }
3804 * @brief Inserts a %exponential_distribution random number distribution
3805 * @p __x into the output stream @p __os.
3807 * @param __os An output stream.
3808 * @param __x A %exponential_distribution random number distribution.
3810 * @returns The output stream with the state of @p __x inserted or in
3813 template<typename _RealType
, typename _CharT
, typename _Traits
>
3814 std::basic_ostream
<_CharT
, _Traits
>&
3815 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3816 const exponential_distribution
<_RealType
>& __x
);
3819 * @brief Extracts a %exponential_distribution random number distribution
3820 * @p __x from the input stream @p __is.
3822 * @param __is An input stream.
3823 * @param __x A %exponential_distribution random number
3826 * @returns The input stream with @p __x extracted or in an error state.
3828 template<typename _RealType
, typename _CharT
, typename _Traits
>
3829 std::basic_istream
<_CharT
, _Traits
>&
3830 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3831 exponential_distribution
<_RealType
>& __x
);
3835 * @brief A gamma continuous distribution for random numbers.
3837 * The formula for the gamma probability density function is
3838 * @f$ p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
3839 * (x/\beta)^{\alpha - 1} e^{-x/\beta} @f$.
3841 template<typename _RealType
= double>
3842 class gamma_distribution
3845 /** The type of the range of the distribution. */
3846 typedef _RealType result_type
;
3847 /** Parameter type. */
3850 typedef gamma_distribution
<_RealType
> distribution_type
;
3851 friend class gamma_distribution
<_RealType
>;
3854 param_type(_RealType __alpha
= _RealType(1),
3855 _RealType __beta
= _RealType(1))
3856 : _M_alpha(__alpha
), _M_beta(__beta
)
3858 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
3864 { return _M_alpha
; }
3871 operator==(const param_type
& __p1
, const param_type
& __p2
)
3872 { return (__p1
._M_alpha
== __p2
._M_alpha
)
3873 && (__p1
._M_beta
== __p2
._M_beta
); }
3882 // Hosts either lambda of GB or d of modified Vaduva's.
3888 * @brief Constructs a gamma distribution with parameters
3889 * @f$ \alpha @f$ and @f$ \beta @f$.
3892 gamma_distribution(_RealType __alpha
= _RealType(1),
3893 _RealType __beta
= _RealType(1))
3894 : _M_param(__alpha
, __beta
)
3898 gamma_distribution(const param_type
& __p
)
3903 * @brief Resets the distribution state.
3905 * Does nothing for the gamma distribution.
3911 * @brief Returns the @f$ \alpha @f$ of the distribution.
3915 { return _M_param
.alpha(); }
3918 * @brief Returns the @f$ \beta @f$ of the distribution.
3922 { return _M_param
.beta(); }
3925 * @brief Returns the parameter set of the distribution.
3929 { return _M_param
; }
3932 * @brief Sets the parameter set of the distribution.
3933 * @param __param The new parameter set of the distribution.
3936 param(const param_type
& __param
)
3937 { _M_param
= __param
; }
3940 * @brief Returns the greatest lower bound value of the distribution.
3944 { return result_type(0); }
3947 * @brief Returns the least upper bound value of the distribution.
3951 { return std::numeric_limits
<result_type
>::max(); }
3953 template<typename _UniformRandomNumberGenerator
>
3955 operator()(_UniformRandomNumberGenerator
& __urng
)
3956 { return this->operator()(__urng
, this->param()); }
3958 template<typename _UniformRandomNumberGenerator
>
3960 operator()(_UniformRandomNumberGenerator
& __urng
,
3961 const param_type
& __p
);
3964 param_type _M_param
;
3968 * @brief Return true if two gamma distributions have the same
3971 template<typename _RealType
>
3973 operator==(const gamma_distribution
<_RealType
>& __d1
,
3974 const gamma_distribution
<_RealType
>& __d2
)
3975 { return __d1
.param() == __d2
.param(); }
3978 * @brief Inserts a %gamma_distribution random number distribution
3979 * @p __x into the output stream @p __os.
3981 * @param __os An output stream.
3982 * @param __x A %gamma_distribution random number distribution.
3984 * @returns The output stream with the state of @p __x inserted or in
3987 template<typename _RealType
, typename _CharT
, typename _Traits
>
3988 std::basic_ostream
<_CharT
, _Traits
>&
3989 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
3990 const gamma_distribution
<_RealType
>& __x
);
3993 * @brief Extracts a %gamma_distribution random number distribution
3994 * @p __x from the input stream @p __is.
3996 * @param __is An input stream.
3997 * @param __x A %gamma_distribution random number generator engine.
3999 * @returns The input stream with @p __x extracted or in an error state.
4001 template<typename _RealType
, typename _CharT
, typename _Traits
>
4002 std::basic_istream
<_CharT
, _Traits
>&
4003 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4004 gamma_distribution
<_RealType
>& __x
);
4008 * @brief A weibull_distribution random number distribution.
4010 * The formula for the normal probability density function is
4011 * @f$ p(x|\alpha,\beta) = \frac{a}{b} (frac{x}{b})^{a-1}
4012 * \exp{(-(frac{x}{b})^a)} @f$.
4014 template<typename _RealType
= double>
4015 class weibull_distribution
4018 /** The type of the range of the distribution. */
4019 typedef _RealType result_type
;
4020 /** Parameter type. */
4023 typedef weibull_distribution
<_RealType
> distribution_type
;
4026 param_type(_RealType __a
= _RealType(1),
4027 _RealType __b
= _RealType(1))
4028 : _M_a(__a
), _M_b(__b
)
4040 operator==(const param_type
& __p1
, const param_type
& __p2
)
4041 { return (__p1
._M_a
== __p2
._M_a
) && (__p1
._M_b
== __p2
._M_b
); }
4049 weibull_distribution(_RealType __a
= _RealType(1),
4050 _RealType __b
= _RealType(1))
4051 : _M_param(__a
, __b
)
4055 weibull_distribution(const param_type
& __p
)
4060 * @brief Resets the distribution state.
4067 * @brief Return the @f$ a @f$ parameter of the distribution.
4071 { return _M_param
.a(); }
4074 * @brief Return the @f$ b @f$ parameter of the distribution.
4078 { return _M_param
.b(); }
4081 * @brief Returns the parameter set of the distribution.
4085 { return _M_param
; }
4088 * @brief Sets the parameter set of the distribution.
4089 * @param __param The new parameter set of the distribution.
4092 param(const param_type
& __param
)
4093 { _M_param
= __param
; }
4096 * @brief Returns the greatest lower bound value of the distribution.
4100 { return result_type(0); }
4103 * @brief Returns the least upper bound value of the distribution.
4107 { return std::numeric_limits
<result_type
>::max(); }
4109 template<typename _UniformRandomNumberGenerator
>
4111 operator()(_UniformRandomNumberGenerator
& __urng
)
4112 { return this->operator()(__urng
, this->param()); }
4114 template<typename _UniformRandomNumberGenerator
>
4116 operator()(_UniformRandomNumberGenerator
& __urng
,
4117 const param_type
& __p
)
4119 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4121 return __p
.b() * std::pow(-std::log(__aurng()),
4122 result_type(1) / __p
.a());
4126 param_type _M_param
;
4130 * @brief Return true if two Weibull distributions have the same
4133 template<typename _RealType
>
4135 operator==(const weibull_distribution
<_RealType
>& __d1
,
4136 const weibull_distribution
<_RealType
>& __d2
)
4137 { return __d1
.param() == __d2
.param(); }
4140 * @brief Inserts a %weibull_distribution random number distribution
4141 * @p __x into the output stream @p __os.
4143 * @param __os An output stream.
4144 * @param __x A %weibull_distribution random number distribution.
4146 * @returns The output stream with the state of @p __x inserted or in
4149 template<typename _RealType
, typename _CharT
, typename _Traits
>
4150 std::basic_ostream
<_CharT
, _Traits
>&
4151 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4152 const weibull_distribution
<_RealType
>& __x
);
4155 * @brief Extracts a %weibull_distribution random number distribution
4156 * @p __x from the input stream @p __is.
4158 * @param __is An input stream.
4159 * @param __x A %weibull_distribution random number
4162 * @returns The input stream with @p __x extracted or in an error state.
4164 template<typename _RealType
, typename _CharT
, typename _Traits
>
4165 std::basic_istream
<_CharT
, _Traits
>&
4166 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4167 weibull_distribution
<_RealType
>& __x
);
4171 * @brief A extreme_value_distribution random number distribution.
4173 * The formula for the normal probability mass function is
4174 * @f$ p(x|a,b) = \frac{1}{b}
4175 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) @f$
4177 template<typename _RealType
= double>
4178 class extreme_value_distribution
4181 /** The type of the range of the distribution. */
4182 typedef _RealType result_type
;
4183 /** Parameter type. */
4186 typedef extreme_value_distribution
<_RealType
> distribution_type
;
4189 param_type(_RealType __a
= _RealType(0),
4190 _RealType __b
= _RealType(1))
4191 : _M_a(__a
), _M_b(__b
)
4203 operator==(const param_type
& __p1
, const param_type
& __p2
)
4204 { return (__p1
._M_a
== __p2
._M_a
) && (__p1
._M_b
== __p2
._M_b
); }
4212 extreme_value_distribution(_RealType __a
= _RealType(0),
4213 _RealType __b
= _RealType(1))
4214 : _M_param(__a
, __b
)
4218 extreme_value_distribution(const param_type
& __p
)
4223 * @brief Resets the distribution state.
4230 * @brief Return the @f$ a @f$ parameter of the distribution.
4234 { return _M_param
.a(); }
4237 * @brief Return the @f$ b @f$ parameter of the distribution.
4241 { return _M_param
.b(); }
4244 * @brief Returns the parameter set of the distribution.
4248 { return _M_param
; }
4251 * @brief Sets the parameter set of the distribution.
4252 * @param __param The new parameter set of the distribution.
4255 param(const param_type
& __param
)
4256 { _M_param
= __param
; }
4259 * @brief Returns the greatest lower bound value of the distribution.
4263 { return std::numeric_limits
<result_type
>::min(); }
4266 * @brief Returns the least upper bound value of the distribution.
4270 { return std::numeric_limits
<result_type
>::max(); }
4272 template<typename _UniformRandomNumberGenerator
>
4274 operator()(_UniformRandomNumberGenerator
& __urng
)
4275 { return this->operator()(__urng
, this->param()); }
4277 template<typename _UniformRandomNumberGenerator
>
4279 operator()(_UniformRandomNumberGenerator
& __urng
,
4280 const param_type
& __p
);
4283 param_type _M_param
;
4289 template<typename _RealType
>
4291 operator==(const extreme_value_distribution
<_RealType
>& __d1
,
4292 const extreme_value_distribution
<_RealType
>& __d2
)
4293 { return __d1
.param() == __d2
.param(); }
4296 * @brief Inserts a %extreme_value_distribution random number distribution
4297 * @p __x into the output stream @p __os.
4299 * @param __os An output stream.
4300 * @param __x A %extreme_value_distribution random number distribution.
4302 * @returns The output stream with the state of @p __x inserted or in
4305 template<typename _RealType
, typename _CharT
, typename _Traits
>
4306 std::basic_ostream
<_CharT
, _Traits
>&
4307 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4308 const extreme_value_distribution
<_RealType
>& __x
);
4311 * @brief Extracts a %extreme_value_distribution random number
4312 * distribution @p __x from the input stream @p __is.
4314 * @param __is An input stream.
4315 * @param __x A %extreme_value_distribution random number
4318 * @returns The input stream with @p __x extracted or in an error state.
4320 template<typename _RealType
, typename _CharT
, typename _Traits
>
4321 std::basic_istream
<_CharT
, _Traits
>&
4322 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4323 extreme_value_distribution
<_RealType
>& __x
);
4327 * @brief A discrete_distribution random number distribution.
4329 * The formula for the discrete probability mass function is
4332 template<typename _IntType
= int>
4333 class discrete_distribution
4335 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
4338 /** The type of the range of the distribution. */
4339 typedef _IntType result_type
;
4340 /** Parameter type. */
4343 typedef discrete_distribution
<_IntType
> distribution_type
;
4344 friend class discrete_distribution
<_IntType
>;
4347 : _M_prob(), _M_cp()
4348 { _M_initialize(); }
4350 template<typename _InputIterator
>
4351 param_type(_InputIterator __wbegin
,
4352 _InputIterator __wend
)
4353 : _M_prob(__wbegin
, __wend
), _M_cp()
4354 { _M_initialize(); }
4356 param_type(initializer_list
<double> __wil
)
4357 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4358 { _M_initialize(); }
4360 template<typename _Func
>
4361 param_type(size_t __nw
, double __xmin
, double __xmax
,
4365 probabilities() const
4369 operator==(const param_type
& __p1
, const param_type
& __p2
)
4370 { return __p1
._M_prob
== __p2
._M_prob
; }
4376 std::vector
<double> _M_prob
;
4377 std::vector
<double> _M_cp
;
4380 discrete_distribution()
4384 template<typename _InputIterator
>
4385 discrete_distribution(_InputIterator __wbegin
,
4386 _InputIterator __wend
)
4387 : _M_param(__wbegin
, __wend
)
4390 discrete_distribution(initializer_list
<double> __wil
)
4394 template<typename _Func
>
4395 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4397 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4401 discrete_distribution(const param_type
& __p
)
4406 * @brief Resets the distribution state.
4413 * @brief Returns the probabilities of the distribution.
4416 probabilities() const
4417 { return _M_param
.probabilities(); }
4420 * @brief Returns the parameter set of the distribution.
4424 { return _M_param
; }
4427 * @brief Sets the parameter set of the distribution.
4428 * @param __param The new parameter set of the distribution.
4431 param(const param_type
& __param
)
4432 { _M_param
= __param
; }
4435 * @brief Returns the greatest lower bound value of the distribution.
4439 { return result_type(0); }
4442 * @brief Returns the least upper bound value of the distribution.
4446 { return this->_M_param
._M_prob
.size() - 1; }
4448 template<typename _UniformRandomNumberGenerator
>
4450 operator()(_UniformRandomNumberGenerator
& __urng
)
4451 { return this->operator()(__urng
, this->param()); }
4453 template<typename _UniformRandomNumberGenerator
>
4455 operator()(_UniformRandomNumberGenerator
& __urng
,
4456 const param_type
& __p
);
4459 * @brief Inserts a %discrete_distribution random number distribution
4460 * @p __x into the output stream @p __os.
4462 * @param __os An output stream.
4463 * @param __x A %discrete_distribution random number distribution.
4465 * @returns The output stream with the state of @p __x inserted or in
4468 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4469 friend std::basic_ostream
<_CharT
, _Traits
>&
4470 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4471 const discrete_distribution
<_IntType1
>& __x
);
4474 * @brief Extracts a %discrete_distribution random number distribution
4475 * @p __x from the input stream @p __is.
4477 * @param __is An input stream.
4478 * @param __x A %discrete_distribution random number
4481 * @returns The input stream with @p __x extracted or in an error
4484 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4485 friend std::basic_istream
<_CharT
, _Traits
>&
4486 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4487 discrete_distribution
<_IntType1
>& __x
);
4490 param_type _M_param
;
4496 template<typename _IntType
>
4498 operator==(const discrete_distribution
<_IntType
>& __d1
,
4499 const discrete_distribution
<_IntType
>& __d2
)
4500 { return __d1
.param() == __d2
.param(); }
4504 * @brief A piecewise_constant_distribution random number distribution.
4506 * The formula for the piecewise constant probability mass function is
4509 template<typename _RealType
= double>
4510 class piecewise_constant_distribution
4513 /** The type of the range of the distribution. */
4514 typedef _RealType result_type
;
4515 /** Parameter type. */
4518 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4519 friend class piecewise_constant_distribution
<_RealType
>;
4523 template<typename _InputIteratorB
, typename _InputIteratorW
>
4524 param_type(_InputIteratorB __bfirst
,
4525 _InputIteratorB __bend
,
4526 _InputIteratorW __wbegin
);
4528 template<typename _Func
>
4529 param_type(initializer_list
<_RealType
> __bil
, _Func __fw
);
4531 template<typename _Func
>
4532 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4535 std::vector
<_RealType
>
4544 operator==(const param_type
& __p1
, const param_type
& __p2
)
4545 { return (__p1
._M_int
== __p2
._M_int
)
4546 && (__p1
._M_den
== __p2
._M_den
); }
4552 std::vector
<_RealType
> _M_int
;
4553 std::vector
<double> _M_den
;
4554 std::vector
<double> _M_cp
;
4558 piecewise_constant_distribution()
4562 template<typename _InputIteratorB
, typename _InputIteratorW
>
4563 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4564 _InputIteratorB __bend
,
4565 _InputIteratorW __wbegin
)
4566 : _M_param(__bfirst
, __bend
, __wbegin
)
4569 template<typename _Func
>
4570 piecewise_constant_distribution(initializer_list
<_RealType
> __bil
,
4572 : _M_param(__bil
, __fw
)
4575 template<typename _Func
>
4576 piecewise_constant_distribution(size_t __nw
,
4577 _RealType __xmin
, _RealType __xmax
,
4579 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4583 piecewise_constant_distribution(const param_type
& __p
)
4588 * @brief Resets the distribution state.
4595 * @brief Returns a vector of the intervals.
4597 std::vector
<_RealType
>
4599 { return _M_param
.intervals(); }
4602 * @brief Returns a vector of the probability densities.
4606 { return _M_param
.densities(); }
4609 * @brief Returns the parameter set of the distribution.
4613 { return _M_param
; }
4616 * @brief Sets the parameter set of the distribution.
4617 * @param __param The new parameter set of the distribution.
4620 param(const param_type
& __param
)
4621 { _M_param
= __param
; }
4624 * @brief Returns the greatest lower bound value of the distribution.
4628 { return this->_M_param
._M_int
.front(); }
4631 * @brief Returns the least upper bound value of the distribution.
4635 { return this->_M_param
._M_int
.back(); }
4637 template<typename _UniformRandomNumberGenerator
>
4639 operator()(_UniformRandomNumberGenerator
& __urng
)
4640 { return this->operator()(__urng
, this->param()); }
4642 template<typename _UniformRandomNumberGenerator
>
4644 operator()(_UniformRandomNumberGenerator
& __urng
,
4645 const param_type
& __p
);
4648 * @brief Inserts a %piecewise_constan_distribution random
4649 * number distribution @p __x into the output stream @p __os.
4651 * @param __os An output stream.
4652 * @param __x A %piecewise_constan_distribution random number
4655 * @returns The output stream with the state of @p __x inserted or in
4658 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4659 friend std::basic_ostream
<_CharT
, _Traits
>&
4660 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4661 const piecewise_constant_distribution
<_RealType1
>& __x
);
4664 * @brief Extracts a %piecewise_constan_distribution random
4665 * number distribution @p __x from the input stream @p __is.
4667 * @param __is An input stream.
4668 * @param __x A %piecewise_constan_distribution random number
4671 * @returns The input stream with @p __x extracted or in an error
4674 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4675 friend std::basic_istream
<_CharT
, _Traits
>&
4676 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4677 piecewise_constant_distribution
<_RealType1
>& __x
);
4680 param_type _M_param
;
4686 template<typename _RealType
>
4688 operator==(const piecewise_constant_distribution
<_RealType
>& __d1
,
4689 const piecewise_constant_distribution
<_RealType
>& __d2
)
4690 { return __d1
.param() == __d2
.param(); }
4694 * @brief A piecewise_linear_distribution random number distribution.
4696 * The formula for the piecewise linear probability mass function is
4699 template<typename _RealType
= double>
4700 class piecewise_linear_distribution
4703 /** The type of the range of the distribution. */
4704 typedef _RealType result_type
;
4705 /** Parameter type. */
4708 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
4709 friend class piecewise_linear_distribution
<_RealType
>;
4713 template<typename _InputIteratorB
, typename _InputIteratorW
>
4714 param_type(_InputIteratorB __bfirst
,
4715 _InputIteratorB __bend
,
4716 _InputIteratorW __wbegin
);
4718 template<typename _Func
>
4719 param_type(initializer_list
<_RealType
> __bil
, _Func __fw
);
4721 template<typename _Func
>
4722 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4725 std::vector
<_RealType
>
4734 operator==(const param_type
& __p1
, const param_type
& __p2
)
4735 { return (__p1
._M_int
== __p2
._M_int
)
4736 && (__p1
._M_den
== __p2
._M_den
); }
4742 std::vector
<_RealType
> _M_int
;
4743 std::vector
<double> _M_den
;
4744 std::vector
<double> _M_cp
;
4745 std::vector
<double> _M_m
;
4749 piecewise_linear_distribution()
4753 template<typename _InputIteratorB
, typename _InputIteratorW
>
4754 piecewise_linear_distribution(_InputIteratorB __bfirst
,
4755 _InputIteratorB __bend
,
4756 _InputIteratorW __wbegin
)
4757 : _M_param(__bfirst
, __bend
, __wbegin
)
4760 template<typename _Func
>
4761 piecewise_linear_distribution(initializer_list
<_RealType
> __bil
,
4763 : _M_param(__bil
, __fw
)
4766 template<typename _Func
>
4767 piecewise_linear_distribution(size_t __nw
,
4768 _RealType __xmin
, _RealType __xmax
,
4770 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4774 piecewise_linear_distribution(const param_type
& __p
)
4779 * Resets the distribution state.
4786 * @brief Return the intervals of the distribution.
4788 std::vector
<_RealType
>
4790 { return _M_param
.intervals(); }
4793 * @brief Return a vector of the probability densities of the
4798 { return _M_param
.densities(); }
4801 * @brief Returns the parameter set of the distribution.
4805 { return _M_param
; }
4808 * @brief Sets the parameter set of the distribution.
4809 * @param __param The new parameter set of the distribution.
4812 param(const param_type
& __param
)
4813 { _M_param
= __param
; }
4816 * @brief Returns the greatest lower bound value of the distribution.
4820 { return this->_M_param
._M_int
.front(); }
4823 * @brief Returns the least upper bound value of the distribution.
4827 { return this->_M_param
._M_int
.back(); }
4829 template<typename _UniformRandomNumberGenerator
>
4831 operator()(_UniformRandomNumberGenerator
& __urng
)
4832 { return this->operator()(__urng
, this->param()); }
4834 template<typename _UniformRandomNumberGenerator
>
4836 operator()(_UniformRandomNumberGenerator
& __urng
,
4837 const param_type
& __p
);
4840 * @brief Inserts a %piecewise_linear_distribution random number
4841 * distribution @p __x into the output stream @p __os.
4843 * @param __os An output stream.
4844 * @param __x A %piecewise_linear_distribution random number
4847 * @returns The output stream with the state of @p __x inserted or in
4850 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4851 friend std::basic_ostream
<_CharT
, _Traits
>&
4852 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
4853 const piecewise_linear_distribution
<_RealType1
>& __x
);
4856 * @brief Extracts a %piecewise_linear_distribution random number
4857 * distribution @p __x from the input stream @p __is.
4859 * @param __is An input stream.
4860 * @param __x A %piecewise_linear_distribution random number
4863 * @returns The input stream with @p __x extracted or in an error
4866 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4867 friend std::basic_istream
<_CharT
, _Traits
>&
4868 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
4869 piecewise_linear_distribution
<_RealType1
>& __x
);
4872 param_type _M_param
;
4878 template<typename _RealType
>
4880 operator==(const piecewise_linear_distribution
<_RealType
>& __d1
,
4881 const piecewise_linear_distribution
<_RealType
>& __d2
)
4882 { return __d1
.param() == __d2
.param(); }
4884 /* @} */ // group std_random_distributions_poisson
4886 /* @} */ // group std_random_distributions
4889 * @addtogroup std_random_utilities Random Number Utilities
4890 * @ingroup std_random
4895 * @brief The seed_seq class generates sequences of seeds for random
4896 * number generators.
4902 /** The type of the seed vales. */
4903 typedef uint_least32_t result_type
;
4905 /** Default constructor. */
4910 template<typename _IntType
>
4911 seed_seq(std::initializer_list
<_IntType
> il
);
4913 template<typename _InputIterator
>
4914 seed_seq(_InputIterator __begin
, _InputIterator __end
);
4916 // generating functions
4917 template<typename _RandomAccessIterator
>
4919 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
4921 // property functions
4923 { return _M_v
.size(); }
4925 template<typename OutputIterator
>
4927 param(OutputIterator __dest
) const
4928 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
4932 vector
<result_type
> _M_v
;
4935 /* @} */ // group std_random_utilities
4937 /* @} */ // group std_random