1 // random number generation -*- C++ -*-
3 // Copyright (C) 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 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
36 // [26.4] Random number generation
39 * @addtogroup std_random Random Number Generation
40 * A facility for generating random numbers on selected distributions.
45 * @brief A function template for converting the output of a (integral)
46 * uniform random number generator to a floatng point result in the range
49 template<typename _RealType, size_t __bits,
50 typename _UniformRandomNumberGenerator>
52 generate_canonical(_UniformRandomNumberGenerator& __g);
57 * Implementation-space details.
61 template<typename _UIntType, size_t __w,
62 bool = __w < static_cast<size_t>
63 (std::numeric_limits<_UIntType>::digits)>
65 { static const _UIntType __value = 0; };
67 template<typename _UIntType, size_t __w>
68 struct _Shift<_UIntType, __w, true>
69 { static const _UIntType __value = _UIntType(1) << __w; };
72 template<typename _UIntType, size_t __w,
73 bool = __w <static_cast<size_t>
74 (std::numeric_limits<_UIntType>::digits)>
77 static const _UIntType __value =
78 __gnu_cxx::__numeric_traits<_UIntType>::__max;
81 template<typename _UIntType, size_t __w>
82 struct _ShiftMin1<_UIntType, __w, true>
84 static const _UIntType __value =
85 (_UIntType(1) << __w) - _UIntType(1);
88 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
91 // Dispatch based on modulus value to prevent divide-by-zero compile-time
92 // errors when m == 0.
93 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
96 { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
98 typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
99 unsigned, unsigned long>::__type _UInt32Type;
102 * An adaptor class for converting the output of any Generator into
103 * the input for a specific Distribution.
105 template<typename _Engine, typename _DInputType>
110 _Adaptor(_Engine& __g)
116 if (is_integral<_DInputType>::value)
119 return _DInputType(0);
125 if (is_integral<_DInputType>::value)
128 return _DInputType(1);
132 * Converts a value generated by the adapted random number generator
133 * into a value in the input domain for the dependent random number
136 * Because the type traits are compile time constants only the
137 * appropriate clause of the if statements will actually be emitted
143 if (is_integral<_DInputType>::value)
146 return generate_canonical<_DInputType,
147 numeric_limits<_DInputType>::digits,
154 } // namespace __detail
157 * @addtogroup std_random_generators Random Number Generators
158 * @ingroup std_random
160 * These classes define objects which provide random or pseudorandom
161 * numbers, either from a discrete or a continuous interval. The
162 * random number generator supplied as a part of this library are
163 * all uniform random number generators which provide a sequence of
164 * random number uniformly distributed over their range.
166 * A number generator is a function object with an operator() that
167 * takes zero arguments and returns a number.
169 * A compliant random number generator must satisfy the following
170 * requirements. <table border=1 cellpadding=10 cellspacing=0>
171 * <caption align=top>Random Number Generator Requirements</caption>
172 * <tr><td>To be documented.</td></tr> </table>
178 * @brief A model of a linear congruential random number generator.
180 * A random number generator that produces pseudorandom numbers using the
181 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
183 * The template parameter @p _UIntType must be an unsigned integral type
184 * large enough to store values up to (__m-1). If the template parameter
185 * @p __m is 0, the modulus @p __m used is
186 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
187 * parameters @p __a and @p __c must be less than @p __m.
189 * The size of the state is @f$ 1 @f$.
191 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
192 class linear_congruential_engine
194 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
195 static_assert(__m == 0 || (__a < __m && __c < __m),
196 "template arguments out of bounds"
197 " in linear_congruential_engine");
200 /** The type of the generated random value. */
201 typedef _UIntType result_type;
203 /** The multiplier. */
204 static const result_type multiplier = __a;
206 static const result_type increment = __c;
208 static const result_type modulus = __m;
209 static const result_type default_seed = 1UL;
212 * @brief Constructs a %linear_congruential_engine random number
213 * generator engine with seed @p __s. The default seed value
216 * @param __s The initial seed value.
219 linear_congruential_engine(result_type __s = default_seed)
223 * @brief Constructs a %linear_congruential_engine random number
224 * generator engine seeded from the seed sequence @p __q.
226 * @param __q the seed sequence.
229 linear_congruential_engine(seed_seq& __q)
233 * @brief Reseeds the %linear_congruential_engine random number generator
234 * engine sequence to the seed @p __s.
236 * @param __s The new seed.
239 seed(result_type __s = default_seed);
242 * @brief Reseeds the %linear_congruential_engine random number generator
244 * sequence using values from the seed sequence @p __q.
246 * @param __q the seed sequence.
252 * @brief Gets the smallest possible value in the output range.
254 * The minimum depends on the @p __c parameter: if it is zero, the
255 * minimum generated must be > 0, otherwise 0 is allowed.
257 * @todo This should be constexpr.
261 { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
264 * @brief Gets the largest possible value in the output range.
266 * @todo This should be constexpr.
273 * @brief Discard a sequence of random numbers.
275 * @todo Look for a faster way to do discard.
278 discard(unsigned long long __z)
280 for (; __z != 0ULL; --__z)
285 * @brief Gets the next random number in the sequence.
291 * @brief Compares two linear congruential random number generator
292 * objects of the same type for equality.
294 * @param __lhs A linear congruential random number generator object.
295 * @param __rhs Another linear congruential random number generator
298 * @returns true if the two objects are equal, false otherwise.
301 operator==(const linear_congruential_engine& __lhs,
302 const linear_congruential_engine& __rhs)
303 { return __lhs._M_x == __rhs._M_x; }
306 * @brief Writes the textual representation of the state x(i) of x to
309 * @param __os The output stream.
310 * @param __lcr A % linear_congruential_engine random number generator.
313 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
315 typename _CharT, typename _Traits>
316 friend std::basic_ostream<_CharT, _Traits>&
317 operator<<(std::basic_ostream<_CharT, _Traits>&,
318 const std::linear_congruential_engine<_UIntType1,
322 * @brief Sets the state of the engine by reading its textual
323 * representation from @p __is.
325 * The textual representation must have been previously written using
326 * an output stream whose imbued locale and whose type's template
327 * specialization arguments _CharT and _Traits were the same as those
330 * @param __is The input stream.
331 * @param __lcr A % linear_congruential_engine random number generator.
334 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
336 typename _CharT, typename _Traits>
337 friend std::basic_istream<_CharT, _Traits>&
338 operator>>(std::basic_istream<_CharT, _Traits>&,
339 std::linear_congruential_engine<_UIntType1, __a1,
343 template<typename _Gen>
345 seed(_Gen& __g, true_type)
346 { return seed(static_cast<unsigned long>(__g)); }
348 template<typename _Gen>
350 seed(_Gen& __g, false_type);
357 * A generalized feedback shift register discrete random number generator.
359 * This algorithm avoids multiplication and division and is designed to be
360 * friendly to a pipelined architecture. If the parameters are chosen
361 * correctly, this generator will produce numbers with a very long period and
362 * fairly good apparent entropy, although still not cryptographically strong.
364 * The best way to use this generator is with the predefined mt19937 class.
366 * This algorithm was originally invented by Makoto Matsumoto and
369 * @var word_size The number of bits in each element of the state vector.
370 * @var state_size The degree of recursion.
371 * @var shift_size The period parameter.
372 * @var mask_bits The separation point bit index.
373 * @var parameter_a The last row of the twist matrix.
374 * @var output_u The first right-shift tempering matrix parameter.
375 * @var output_s The first left-shift tempering matrix parameter.
376 * @var output_b The first left-shift tempering matrix mask.
377 * @var output_t The second left-shift tempering matrix parameter.
378 * @var output_c The second left-shift tempering matrix mask.
379 * @var output_l The second right-shift tempering matrix parameter.
381 template<typename _UIntType, size_t __w,
382 size_t __n, size_t __m, size_t __r,
383 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
384 _UIntType __b, size_t __t,
385 _UIntType __c, size_t __l, _UIntType __f>
386 class mersenne_twister_engine
388 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
390 static_assert(__m >= 1U,
391 "mersenne_twister_engine template arguments out of bounds");
392 static_assert(__n >= __m,
393 "mersenne_twister_engine template arguments out of bounds");
394 static_assert(__w >= __r,
395 "mersenne_twister_engine template arguments out of bounds");
396 static_assert(__w >= __u,
397 "mersenne_twister_engine template arguments out of bounds");
398 static_assert(__w >= __s,
399 "mersenne_twister_engine template arguments out of bounds");
400 static_assert(__w >= __t,
401 "mersenne_twister_engine template arguments out of bounds");
402 static_assert(__w >= __l,
403 "mersenne_twister_engine template arguments out of bounds");
405 static_cast<size_t>(numeric_limits<_UIntType>::digits),
406 "mersenne_twister_engine template arguments out of bounds");
407 static_assert(__a <= __detail::_ShiftMin1<_UIntType, __w>::__value,
408 "mersenne_twister_engine template arguments out of bounds");
409 static_assert(__b <= __detail::_ShiftMin1<_UIntType, __w>::__value,
410 "mersenne_twister_engine template arguments out of bounds");
411 static_assert(__c <= __detail::_ShiftMin1<_UIntType, __w>::__value,
412 "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>&,
526 const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
527 __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
531 * @brief Extracts the current state of a % mersenne_twister_engine
532 * random number generator engine @p __x from the input stream
535 * @param __is An input stream.
536 * @param __x A % mersenne_twister_engine random number generator
539 * @returns The input stream with the state of @p __x extracted or in
542 template<typename _UIntType1,
543 size_t __w1, size_t __n1,
544 size_t __m1, size_t __r1,
545 _UIntType1 __a1, size_t __u1,
546 _UIntType1 __d1, size_t __s1,
547 _UIntType1 __b1, size_t __t1,
548 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
549 typename _CharT, typename _Traits>
550 friend std::basic_istream<_CharT, _Traits>&
551 operator>>(std::basic_istream<_CharT, _Traits>&,
552 std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
553 __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
557 template<typename _Gen>
559 seed(_Gen& __g, true_type)
560 { return seed(static_cast<unsigned long>(__g)); }
562 template<typename _Gen>
564 seed(_Gen& __g, false_type);
566 _UIntType _M_x[state_size];
571 * @brief The Marsaglia-Zaman generator.
573 * This is a model of a Generalized Fibonacci discrete random number
574 * generator, sometimes referred to as the SWC generator.
576 * A discrete random number generator that produces pseudorandom
577 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
578 * carry_{i-1}) \bmod m @f$.
580 * The size of the state is @f$ r @f$
581 * and the maximum period of the generator is @f$ m^r - m^s - 1 @f$.
583 * @var _M_x The state of the generator. This is a ring buffer.
584 * @var _M_carry The carry.
585 * @var _M_p Current index of x(i - r).
587 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
588 class subtract_with_carry_engine
590 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
591 static_assert(__s > 0U && __r > __s
593 && __w <= static_cast<size_t>(numeric_limits<_UIntType>::digits),
594 "template arguments out of bounds"
595 " in subtract_with_carry_engine");
598 /** The type of the generated random value. */
599 typedef _UIntType result_type;
602 static const size_t word_size = __w;
603 static const size_t short_lag = __s;
604 static const size_t long_lag = __r;
605 static const result_type default_seed = 19780503;
608 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
609 * random number generator.
612 subtract_with_carry_engine(result_type __sd = default_seed)
613 { this->seed(__sd); }
616 * @brief Constructs a %subtract_with_carry_engine random number engine
617 * seeded from the seed sequence @p __q.
619 * @param __q the seed sequence.
622 subtract_with_carry_engine(seed_seq& __q)
626 * @brief Seeds the initial state @f$ x_0 @f$ of the random number
629 * N1688[4.19] modifies this as follows. If @p __value == 0,
630 * sets value to 19780503. In any case, with a linear
631 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
632 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
633 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
634 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
635 * set carry to 1, otherwise sets carry to 0.
638 seed(result_type __sd = default_seed);
641 * @brief Seeds the initial state @f$ x_0 @f$ of the
642 * % subtract_with_carry_engine random number generator.
648 * @brief Gets the inclusive minimum value of the range of random
649 * integers returned by this generator.
651 * @todo This should be constexpr.
658 * @brief Gets the inclusive maximum value of the range of random
659 * integers returned by this generator.
661 * @todo This should be constexpr.
665 { return _S_modulus - 1U; }
668 * @brief Discard a sequence of random numbers.
670 * @todo Look for a faster way to do discard.
673 discard(unsigned long long __z)
675 for (; __z != 0ULL; --__z)
680 * @brief Gets the next random number in the sequence.
686 * @brief Compares two % subtract_with_carry_engine random number
687 * generator objects of the same type for equality.
689 * @param __lhs A % subtract_with_carry_engine random number generator
691 * @param __rhs Another % subtract_with_carry_engine random number
694 * @returns true if the two objects are equal, false otherwise.
697 operator==(const subtract_with_carry_engine& __lhs,
698 const subtract_with_carry_engine& __rhs)
699 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
702 * @brief Inserts the current state of a % subtract_with_carry_engine
703 * random number generator engine @p __x into the output stream
706 * @param __os An output stream.
707 * @param __x A % subtract_with_carry_engine random number generator
710 * @returns The output stream with the state of @p __x inserted or in
713 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
714 typename _CharT, typename _Traits>
715 friend std::basic_ostream<_CharT, _Traits>&
716 operator<<(std::basic_ostream<_CharT, _Traits>&,
717 const std::subtract_with_carry_engine<_UIntType1, __w1,
721 * @brief Extracts the current state of a % subtract_with_carry_engine
722 * random number generator engine @p __x from the input stream
725 * @param __is An input stream.
726 * @param __x A % subtract_with_carry_engine random number generator engine.
728 * @returns The input stream with the state of @p __x extracted or in
731 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
732 typename _CharT, typename _Traits>
733 friend std::basic_istream<_CharT, _Traits>&
734 operator>>(std::basic_istream<_CharT, _Traits>&,
735 std::subtract_with_carry_engine<_UIntType1, __w1,
739 template<typename _Gen>
741 seed(_Gen& __g, true_type)
742 { return seed(static_cast<unsigned long>(__g)); }
744 template<typename _Gen>
746 seed(_Gen& __g, false_type);
748 static const size_t _S_modulus
749 = __detail::_Shift<_UIntType, __w>::__value;
751 _UIntType _M_x[long_lag];
757 * Produces random numbers from some base engine by discarding blocks of
760 * 0 <= @p __r <= @p __p
762 template<typename _RandomNumberEngine, size_t __p, size_t __r>
763 class discard_block_engine
765 static_assert(__r >= 1U && __p >= __r,
766 "template arguments out of bounds"
767 " in discard_block_engine");
770 /** The type of the generated random value. */
771 typedef typename _RandomNumberEngine::result_type result_type;
774 static const size_t block_size = __p;
775 static const size_t used_block = __r;
778 * @brief Constructs a default %discard_block_engine engine.
780 * The underlying engine is default constructed as well.
782 discard_block_engine()
783 : _M_b(), _M_n(0) { }
786 * @brief Copy constructs a %discard_block_engine engine.
788 * Copies an existing base class random number generator.
789 * @param rng An existing (base class) engine object.
792 discard_block_engine(const _RandomNumberEngine& __rne)
793 : _M_b(__rne), _M_n(0) { }
796 * @brief Move constructs a %discard_block_engine engine.
798 * Copies an existing base class random number generator.
799 * @param rng An existing (base class) engine object.
802 discard_block_engine(_RandomNumberEngine&& __rne)
803 : _M_b(std::move(__rne)), _M_n(0) { }
806 * @brief Seed constructs a %discard_block_engine engine.
808 * Constructs the underlying generator engine seeded with @p __s.
809 * @param __s A seed value for the base class engine.
812 discard_block_engine(result_type __s)
813 : _M_b(__s), _M_n(0) { }
816 * @brief Generator construct a %discard_block_engine engine.
818 * @param __q A seed sequence.
821 discard_block_engine(seed_seq& __q)
826 * @brief Reseeds the %discard_block_engine object with the default
827 * seed for the underlying base class generator engine.
837 * @brief Reseeds the %discard_block_engine object with the default
838 * seed for the underlying base class generator engine.
841 seed(result_type __s)
848 * @brief Reseeds the %discard_block_engine object with the given seed
850 * @param __q A seed generator function.
860 * @brief Gets a const reference to the underlying generator engine
863 const _RandomNumberEngine&
868 * @brief Gets the minimum value in the generated random number range.
870 * @todo This should be constexpr.
874 { return _M_b.min(); }
877 * @brief Gets the maximum value in the generated random number range.
879 * @todo This should be constexpr.
883 { return _M_b.max(); }
886 * @brief Discard a sequence of random numbers.
888 * @todo Look for a faster way to do discard.
891 discard(unsigned long long __z)
893 for (; __z != 0ULL; --__z)
898 * @brief Gets the next value in the generated random number sequence.
904 * @brief Compares two %discard_block_engine random number generator
905 * objects of the same type for equality.
907 * @param __lhs A %discard_block_engine random number generator object.
908 * @param __rhs Another %discard_block_engine random number generator
911 * @returns true if the two objects are equal, false otherwise.
914 operator==(const discard_block_engine& __lhs,
915 const discard_block_engine& __rhs)
916 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
919 * @brief Inserts the current state of a %discard_block_engine random
920 * number generator engine @p __x into the output stream
923 * @param __os An output stream.
924 * @param __x A %discard_block_engine random number generator engine.
926 * @returns The output stream with the state of @p __x inserted or in
929 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
930 typename _CharT, typename _Traits>
931 friend std::basic_ostream<_CharT, _Traits>&
932 operator<<(std::basic_ostream<_CharT, _Traits>&,
933 const std::discard_block_engine<_RandomNumberEngine1,
937 * @brief Extracts the current state of a % subtract_with_carry_engine
938 * random number generator engine @p __x from the input stream
941 * @param __is An input stream.
942 * @param __x A %discard_block_engine random number generator engine.
944 * @returns The input stream with the state of @p __x extracted or in
947 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
948 typename _CharT, typename _Traits>
949 friend std::basic_istream<_CharT, _Traits>&
950 operator>>(std::basic_istream<_CharT, _Traits>&,
951 std::discard_block_engine<_RandomNumberEngine1,
955 _RandomNumberEngine _M_b;
960 * Produces random numbers by combining random numbers from some base
961 * engine to produce random numbers with a specifies number of bits @p __w.
963 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
964 class independent_bits_engine
966 static_assert(__w > 0U
968 static_cast<size_t>(numeric_limits<_UIntType>::digits),
969 "template arguments out of bounds "
970 "in independent_bits_engine");
973 /** The type of the generated random value. */
974 typedef _UIntType result_type;
977 * @brief Constructs a default %independent_bits_engine engine.
979 * The underlying engine is default constructed as well.
981 independent_bits_engine()
985 * @brief Copy constructs a %independent_bits_engine engine.
987 * Copies an existing base class random number generator.
988 * @param rng An existing (base class) engine object.
991 independent_bits_engine(const _RandomNumberEngine& __rne)
995 * @brief Move constructs a %independent_bits_engine engine.
997 * Copies an existing base class random number generator.
998 * @param rng An existing (base class) engine object.
1001 independent_bits_engine(_RandomNumberEngine&& __rne)
1002 : _M_b(std::move(__rne)) { }
1005 * @brief Seed constructs a %independent_bits_engine engine.
1007 * Constructs the underlying generator engine seeded with @p __s.
1008 * @param __s A seed value for the base class engine.
1011 independent_bits_engine(result_type __s)
1015 * @brief Generator construct a %independent_bits_engine engine.
1017 * @param __q A seed sequence.
1020 independent_bits_engine(seed_seq& __q)
1025 * @brief Reseeds the %independent_bits_engine object with the default
1026 * seed for the underlying base class generator engine.
1033 * @brief Reseeds the %independent_bits_engine object with the default
1034 * seed for the underlying base class generator engine.
1037 seed(result_type __s)
1041 * @brief Reseeds the %independent_bits_engine object with the given
1043 * @param __q A seed generator function.
1050 * @brief Gets a const reference to the underlying generator engine
1053 const _RandomNumberEngine&
1058 * @brief Gets the minimum value in the generated random number range.
1060 * @todo This should be constexpr.
1067 * @brief Gets the maximum value in the generated random number range.
1069 * @todo This should be constexpr.
1073 { return __detail::_ShiftMin1<_UIntType, __w>::__value; }
1076 * @brief Discard a sequence of random numbers.
1078 * @todo Look for a faster way to do discard.
1081 discard(unsigned long long __z)
1083 for (; __z != 0ULL; --__z)
1088 * @brief Gets the next value in the generated random number sequence.
1094 * @brief Compares two %independent_bits_engine random number generator
1095 * objects of the same type for equality.
1097 * @param __lhs A %independent_bits_engine random number generator
1099 * @param __rhs Another %independent_bits_engine random number generator
1102 * @returns true if the two objects are equal, false otherwise.
1105 operator==(const independent_bits_engine& __lhs,
1106 const independent_bits_engine& __rhs)
1107 { return __lhs._M_b == __rhs._M_b; }
1110 * @brief Extracts the current state of a % subtract_with_carry_engine
1111 * random number generator engine @p __x from the input stream
1114 * @param __is An input stream.
1115 * @param __x A %independent_bits_engine random number generator
1118 * @returns The input stream with the state of @p __x extracted or in
1121 template<typename _CharT, typename _Traits>
1122 friend std::basic_istream<_CharT, _Traits>&
1123 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1124 std::independent_bits_engine<_RandomNumberEngine,
1125 __w, _UIntType>& __x)
1132 _RandomNumberEngine _M_b;
1136 * @brief Inserts the current state of a %independent_bits_engine random
1137 * number generator engine @p __x into the output stream @p __os.
1139 * @param __os An output stream.
1140 * @param __x A %independent_bits_engine random number generator engine.
1142 * @returns The output stream with the state of @p __x inserted or in
1145 template<typename _RandomNumberEngine, size_t __w, typename _UIntType,
1146 typename _CharT, typename _Traits>
1147 std::basic_ostream<_CharT, _Traits>&
1148 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1149 const std::independent_bits_engine<_RandomNumberEngine,
1150 __w, _UIntType>& __x)
1157 * @brief Produces random numbers by combining random numbers from some
1158 * base engine to produce random numbers with a specifies number of bits
1161 template<typename _RandomNumberEngine, size_t __k>
1162 class shuffle_order_engine
1164 static_assert(__k >= 1U,
1165 "template arguments out of bounds"
1166 " in shuffle_order_engine");
1169 /** The type of the generated random value. */
1170 typedef typename _RandomNumberEngine::result_type result_type;
1172 static const size_t table_size = __k;
1175 * @brief Constructs a default %shuffle_order_engine engine.
1177 * The underlying engine is default constructed as well.
1179 shuffle_order_engine()
1181 { _M_initialize(); }
1184 * @brief Copy constructs a %shuffle_order_engine engine.
1186 * Copies an existing base class random number generator.
1187 * @param rng An existing (base class) engine object.
1190 shuffle_order_engine(const _RandomNumberEngine& __rne)
1192 { _M_initialize(); }
1195 * @brief Move constructs a %shuffle_order_engine engine.
1197 * Copies an existing base class random number generator.
1198 * @param rng An existing (base class) engine object.
1201 shuffle_order_engine(_RandomNumberEngine&& __rne)
1202 : _M_b(std::move(__rne))
1203 { _M_initialize(); }
1206 * @brief Seed constructs a %shuffle_order_engine engine.
1208 * Constructs the underlying generator engine seeded with @p __s.
1209 * @param __s A seed value for the base class engine.
1212 shuffle_order_engine(result_type __s)
1214 { _M_initialize(); }
1217 * @brief Generator construct a %shuffle_order_engine engine.
1219 * @param __q A seed sequence.
1222 shuffle_order_engine(seed_seq& __q)
1224 { _M_initialize(); }
1227 * @brief Reseeds the %shuffle_order_engine object with the default seed
1228 for the underlying base class generator engine.
1238 * @brief Reseeds the %shuffle_order_engine object with the default seed
1239 * for the underlying base class generator engine.
1242 seed(result_type __s)
1249 * @brief Reseeds the %shuffle_order_engine object with the given seed
1251 * @param __q A seed generator function.
1261 * Gets a const reference to the underlying generator engine object.
1263 const _RandomNumberEngine&
1268 * Gets the minimum value in the generated random number range.
1270 * @todo This should be constexpr.
1274 { return _M_b.min(); }
1277 * Gets the maximum value in the generated random number range.
1279 * @todo This should be constexpr.
1283 { return _M_b.max(); }
1286 * Discard a sequence of random numbers.
1288 * @todo Look for a faster way to do discard.
1291 discard(unsigned long long __z)
1293 for (; __z != 0ULL; --__z)
1298 * Gets the next value in the generated random number sequence.
1304 * Compares two %shuffle_order_engine random number generator objects
1305 * of the same type for equality.
1307 * @param __lhs A %shuffle_order_engine random number generator object.
1308 * @param __rhs Another %shuffle_order_engine random number generator
1311 * @returns true if the two objects are equal, false otherwise.
1314 operator==(const shuffle_order_engine& __lhs,
1315 const shuffle_order_engine& __rhs)
1316 { return __lhs._M_b == __rhs._M_b; }
1319 * @brief Inserts the current state of a %shuffle_order_engine random
1320 * number generator engine @p __x into the output stream
1323 * @param __os An output stream.
1324 * @param __x A %shuffle_order_engine random number generator engine.
1326 * @returns The output stream with the state of @p __x inserted or in
1329 template<typename _RandomNumberEngine1, size_t __k1,
1330 typename _CharT, typename _Traits>
1331 friend std::basic_ostream<_CharT, _Traits>&
1332 operator<<(std::basic_ostream<_CharT, _Traits>&,
1333 const std::shuffle_order_engine<_RandomNumberEngine1,
1337 * @brief Extracts the current state of a % subtract_with_carry_engine
1338 * random number generator engine @p __x from the input stream
1341 * @param __is An input stream.
1342 * @param __x A %shuffle_order_engine random number generator engine.
1344 * @returns The input stream with the state of @p __x extracted or in
1347 template<typename _RandomNumberEngine1, size_t __k1,
1348 typename _CharT, typename _Traits>
1349 friend std::basic_istream<_CharT, _Traits>&
1350 operator>>(std::basic_istream<_CharT, _Traits>&,
1351 std::shuffle_order_engine<_RandomNumberEngine1, __k1>&);
1354 void _M_initialize()
1356 for (size_t __i = 0; __i < __k; ++__i)
1361 _RandomNumberEngine _M_b;
1362 result_type _M_v[__k];
1367 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1369 typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1373 * An alternative LCR (Lehmer Generator function) .
1375 typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1379 * The classic Mersenne Twister.
1382 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
1383 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
1384 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1386 typedef mersenne_twister_engine<
1392 0xefc60000UL, 18, 1812433253UL> mt19937;
1395 * An alternative Mersenne Twister.
1397 typedef mersenne_twister_engine<
1400 0xb5026f5aa96619e9ULL, 29,
1401 0x5555555555555555ULL, 17,
1402 0x71d67fffeda60000ULL, 37,
1403 0xfff7eee000000000ULL, 43,
1404 6364136223846793005ULL> mt19937_64;
1409 typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
1412 typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
1414 typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
1417 typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
1422 typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
1427 typedef minstd_rand0 default_random_engine;
1430 * A standard interface to a platform-specific non-deterministic
1431 * random number generator (if any are available).
1436 /** The type of the generated random value. */
1437 typedef unsigned int result_type;
1439 // constructors, destructors and member functions
1441 #ifdef _GLIBCXX_USE_RANDOM_TR1
1444 random_device(const std::string& __token = "/dev/urandom")
1446 if ((__token != "/dev/urandom" && __token != "/dev/random")
1447 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1448 std::__throw_runtime_error(__N("random_device::"
1449 "random_device(const std::string&)"));
1453 { std::fclose(_M_file); }
1458 random_device(const std::string& __token = "mt19937")
1459 : _M_mt(_M_strtoul(__token)) { }
1462 static unsigned long
1463 _M_strtoul(const std::string& __str)
1465 unsigned long __ret = 5489UL;
1466 if (__str != "mt19937")
1468 const char* __nptr = __str.c_str();
1470 __ret = std::strtoul(__nptr, &__endptr, 0);
1471 if (*__nptr == '\0' || *__endptr != '\0')
1472 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1473 "(const std::string&)"));
1484 { return std::numeric_limits<result_type>::min(); }
1488 { return std::numeric_limits<result_type>::max(); }
1497 #ifdef _GLIBCXX_USE_RANDOM_TR1
1499 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1507 // No copy functions.
1508 random_device(const random_device&) = delete;
1509 void operator=(const random_device&) = delete;
1513 #ifdef _GLIBCXX_USE_RANDOM_TR1
1520 /* @} */ // group std_random_generators
1523 * @addtogroup std_random_distributions Random Number Distributions
1524 * @ingroup std_random
1529 * @addtogroup std_random_distributions_uniform Uniform Distributions
1530 * @ingroup std_random_distributions
1535 * @brief Uniform discrete distribution for random numbers.
1536 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1537 * probability throughout the range.
1539 template<typename _IntType = int>
1540 class uniform_int_distribution
1542 __glibcxx_class_requires(_IntType, _IntegerConcept)
1545 /** The type of the range of the distribution. */
1546 typedef _IntType result_type;
1547 /** Parameter type. */
1550 typedef uniform_int_distribution<_IntType> distribution_type;
1553 param_type(_IntType __a = 0, _IntType __b = 9)
1554 : _M_a(__a), _M_b(__b)
1556 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1568 operator==(const param_type& __p1, const param_type& __p2)
1569 { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
1578 * @brief Constructs a uniform distribution object.
1581 uniform_int_distribution(_IntType __a = 0, _IntType __b = 9)
1582 : _M_param(__a, __b)
1586 uniform_int_distribution(const param_type& __p)
1591 * @brief Resets the distribution state.
1593 * Does nothing for the uniform integer distribution.
1600 { return _M_param.a(); }
1604 { return _M_param.b(); }
1607 * @brief Returns the inclusive lower bound of the distribution range.
1611 { return this->a(); }
1614 * @brief Returns the inclusive upper bound of the distribution range.
1618 { return this->b(); }
1621 * @brief Returns the parameter set of the distribution.
1625 { return _M_param; }
1628 * @brief Sets the parameter set of the distribution.
1629 * @param __param The new parameter set of the distribution.
1632 param(const param_type& __param)
1633 { _M_param = __param; }
1636 * Gets a uniformly distributed random number in the range
1639 template<typename _UniformRandomNumberGenerator>
1641 operator()(_UniformRandomNumberGenerator& __urng)
1643 typedef typename _UniformRandomNumberGenerator::result_type
1645 return _M_call(__urng, this->a(), this->b(),
1646 typename is_integral<_UResult_type>::type());
1650 * Gets a uniform random number in the range @f$[0, n)@f$.
1652 * This function is aimed at use with std::random_shuffle.
1654 template<typename _UniformRandomNumberGenerator>
1656 operator()(_UniformRandomNumberGenerator& __urng,
1657 const param_type& __p)
1659 typedef typename _UniformRandomNumberGenerator::result_type
1661 return _M_call(__urng, __p.a(), __p.b(),
1662 typename is_integral<_UResult_type>::type());
1666 template<typename _UniformRandomNumberGenerator>
1668 _M_call(_UniformRandomNumberGenerator& __urng,
1669 result_type __min, result_type __max, true_type);
1671 template<typename _UniformRandomNumberGenerator>
1673 _M_call(_UniformRandomNumberGenerator& __urng,
1674 result_type __min, result_type __max, false_type)
1676 return result_type((__urng() - __urng.min())
1677 / (__urng.max() - __urng.min())
1678 * (__max - __min + 1)) + __min;
1681 param_type _M_param;
1685 * @brief Return true if two uniform integer distributions have
1686 * the same parameters.
1688 template<typename _IntType>
1690 operator==(const std::uniform_int_distribution<_IntType>& __d1,
1691 const std::uniform_int_distribution<_IntType>& __d2)
1692 { return __d1.param() == __d2.param(); }
1695 * @brief Inserts a %uniform_int_distribution random number
1696 * distribution @p __x into the output stream @p os.
1698 * @param __os An output stream.
1699 * @param __x A %uniform_int_distribution random number distribution.
1701 * @returns The output stream with the state of @p __x inserted or in
1704 template<typename _IntType, typename _CharT, typename _Traits>
1705 std::basic_ostream<_CharT, _Traits>&
1706 operator<<(std::basic_ostream<_CharT, _Traits>&,
1707 const std::uniform_int_distribution<_IntType>&);
1710 * @brief Extracts a %uniform_int_distribution random number distribution
1711 * @p __x from the input stream @p __is.
1713 * @param __is An input stream.
1714 * @param __x A %uniform_int_distribution random number generator engine.
1716 * @returns The input stream with @p __x extracted or in an error state.
1718 template<typename _IntType, typename _CharT, typename _Traits>
1719 std::basic_istream<_CharT, _Traits>&
1720 operator>>(std::basic_istream<_CharT, _Traits>&,
1721 std::uniform_int_distribution<_IntType>&);
1725 * @brief Uniform continuous distribution for random numbers.
1727 * A continuous random distribution on the range [min, max) with equal
1728 * probability throughout the range. The URNG should be real-valued and
1729 * deliver number in the range [0, 1).
1731 template<typename _RealType = double>
1732 class uniform_real_distribution
1735 /** The type of the range of the distribution. */
1736 typedef _RealType result_type;
1737 /** Parameter type. */
1740 typedef uniform_real_distribution<_RealType> distribution_type;
1743 param_type(_RealType __a = _RealType(0),
1744 _RealType __b = _RealType(1))
1745 : _M_a(__a), _M_b(__b)
1747 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1759 operator==(const param_type& __p1, const param_type& __p2)
1760 { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
1769 * @brief Constructs a uniform_real_distribution object.
1771 * @param __min [IN] The lower bound of the distribution.
1772 * @param __max [IN] The upper bound of the distribution.
1775 uniform_real_distribution(_RealType __a = _RealType(0),
1776 _RealType __b = _RealType(1))
1777 : _M_param(__a, __b)
1781 uniform_real_distribution(const param_type& __p)
1786 * @brief Resets the distribution state.
1788 * Does nothing for the uniform real distribution.
1795 { return _M_param.a(); }
1799 { return _M_param.b(); }
1802 * @brief Returns the inclusive lower bound of the distribution range.
1806 { return this->a(); }
1809 * @brief Returns the inclusive upper bound of the distribution range.
1813 { return this->b(); }
1816 * @brief Returns the parameter set of the distribution.
1820 { return _M_param; }
1823 * @brief Sets the parameter set of the distribution.
1824 * @param __param The new parameter set of the distribution.
1827 param(const param_type& __param)
1828 { _M_param = __param; }
1830 template<typename _UniformRandomNumberGenerator>
1832 operator()(_UniformRandomNumberGenerator& __urng)
1834 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1836 return (__aurng() * (this->b() - this->a())) + this->a();
1839 template<typename _UniformRandomNumberGenerator>
1841 operator()(_UniformRandomNumberGenerator& __urng,
1842 const param_type& __p)
1844 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1846 return (__aurng() * (__p.b() - __p.a())) + __p.a();
1850 param_type _M_param;
1854 * @brief Return true if two uniform real distributions have
1855 * the same parameters.
1857 template<typename _IntType>
1859 operator==(const std::uniform_real_distribution<_IntType>& __d1,
1860 const std::uniform_real_distribution<_IntType>& __d2)
1861 { return __d1.param() == __d2.param(); }
1864 * @brief Inserts a %uniform_real_distribution random number
1865 * distribution @p __x into the output stream @p __os.
1867 * @param __os An output stream.
1868 * @param __x A %uniform_real_distribution random number distribution.
1870 * @returns The output stream with the state of @p __x inserted or in
1873 template<typename _RealType, typename _CharT, typename _Traits>
1874 std::basic_ostream<_CharT, _Traits>&
1875 operator<<(std::basic_ostream<_CharT, _Traits>&,
1876 const std::uniform_real_distribution<_RealType>&);
1879 * @brief Extracts a %uniform_real_distribution random number distribution
1880 * @p __x from the input stream @p __is.
1882 * @param __is An input stream.
1883 * @param __x A %uniform_real_distribution random number generator engine.
1885 * @returns The input stream with @p __x extracted or in an error state.
1887 template<typename _RealType, typename _CharT, typename _Traits>
1888 std::basic_istream<_CharT, _Traits>&
1889 operator>>(std::basic_istream<_CharT, _Traits>&,
1890 std::uniform_real_distribution<_RealType>&);
1892 /* @} */ // group std_random_distributions_uniform
1895 * @addtogroup std_random_distributions_normal Normal Distributions
1896 * @ingroup std_random_distributions
1901 * @brief A normal continuous distribution for random numbers.
1903 * The formula for the normal probability density function is
1904 * @f$ p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1905 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } @f$.
1907 template<typename _RealType = double>
1908 class normal_distribution
1911 /** The type of the range of the distribution. */
1912 typedef _RealType result_type;
1913 /** Parameter type. */
1916 typedef normal_distribution<_RealType> distribution_type;
1919 param_type(_RealType __mean = _RealType(0),
1920 _RealType __stddev = _RealType(1))
1921 : _M_mean(__mean), _M_stddev(__stddev)
1923 _GLIBCXX_DEBUG_ASSERT(_M_stddev > _RealType(0));
1932 { return _M_stddev; }
1935 operator==(const param_type& __p1, const param_type& __p2)
1936 { return (__p1._M_mean == __p2._M_mean)
1937 && (__p1._M_stddev == __p2._M_stddev); }
1941 _RealType _M_stddev;
1946 * Constructs a normal distribution with parameters @f$ mean @f$ and
1947 * standard deviation.
1950 normal_distribution(result_type __mean = result_type(0),
1951 result_type __stddev = result_type(1))
1952 : _M_param(__mean, __stddev), _M_saved_available(false)
1956 normal_distribution(const param_type& __p)
1957 : _M_param(__p), _M_saved_available(false)
1961 * @brief Resets the distribution state.
1965 { _M_saved_available = false; }
1968 * @brief Returns the mean of the distribution.
1972 { return _M_param.mean(); }
1975 * @brief Returns the standard deviation of the distribution.
1979 { return _M_param.stddev(); }
1982 * @brief Returns the parameter set of the distribution.
1986 { return _M_param; }
1989 * @brief Sets the parameter set of the distribution.
1990 * @param __param The new parameter set of the distribution.
1993 param(const param_type& __param)
1994 { _M_param = __param; }
1997 * @brief Returns the greatest lower bound value of the distribution.
2001 { return std::numeric_limits<result_type>::min(); }
2004 * @brief Returns the least upper bound value of the distribution.
2008 { return std::numeric_limits<result_type>::max(); }
2010 template<typename _UniformRandomNumberGenerator>
2012 operator()(_UniformRandomNumberGenerator& __urng)
2013 { return this->operator()(__urng, this->param()); }
2015 template<typename _UniformRandomNumberGenerator>
2017 operator()(_UniformRandomNumberGenerator& __urng,
2018 const param_type& __p);
2021 * @brief Return true if two normal distributions have
2022 * the same parameters.
2024 template<typename _RealType1>
2026 operator==(const std::normal_distribution<_RealType1>& __d1,
2027 const std::normal_distribution<_RealType1>& __d2);
2030 * @brief Inserts a %normal_distribution random number distribution
2031 * @p __x into the output stream @p __os.
2033 * @param __os An output stream.
2034 * @param __x A %normal_distribution random number distribution.
2036 * @returns The output stream with the state of @p __x inserted or in
2039 template<typename _RealType1, typename _CharT, typename _Traits>
2040 friend std::basic_ostream<_CharT, _Traits>&
2041 operator<<(std::basic_ostream<_CharT, _Traits>&,
2042 const std::normal_distribution<_RealType1>&);
2045 * @brief Extracts a %normal_distribution random number distribution
2046 * @p __x from the input stream @p __is.
2048 * @param __is An input stream.
2049 * @param __x A %normal_distribution random number generator engine.
2051 * @returns The input stream with @p __x extracted or in an error
2054 template<typename _RealType1, typename _CharT, typename _Traits>
2055 friend std::basic_istream<_CharT, _Traits>&
2056 operator>>(std::basic_istream<_CharT, _Traits>&,
2057 std::normal_distribution<_RealType1>&);
2060 param_type _M_param;
2061 result_type _M_saved;
2062 bool _M_saved_available;
2067 * @brief A lognormal_distribution random number distribution.
2069 * The formula for the normal probability mass function is
2070 * @f$ p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2071 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}} @f$
2073 template<typename _RealType = double>
2074 class lognormal_distribution
2077 /** The type of the range of the distribution. */
2078 typedef _RealType result_type;
2079 /** Parameter type. */
2082 typedef lognormal_distribution<_RealType> distribution_type;
2085 param_type(_RealType __m = _RealType(0),
2086 _RealType __s = _RealType(1))
2087 : _M_m(__m), _M_s(__s)
2099 operator==(const param_type& __p1, const param_type& __p2)
2100 { return (__p1._M_m == __p2._M_m) && (__p1._M_s == __p2._M_s); }
2108 lognormal_distribution(_RealType __m = _RealType(0),
2109 _RealType __s = _RealType(1))
2110 : _M_param(__m, __s)
2114 lognormal_distribution(const param_type& __p)
2119 * Resets the distribution state.
2130 { return _M_param.m(); }
2134 { return _M_param.s(); }
2137 * @brief Returns the parameter set of the distribution.
2141 { return _M_param; }
2144 * @brief Sets the parameter set of the distribution.
2145 * @param __param The new parameter set of the distribution.
2148 param(const param_type& __param)
2149 { _M_param = __param; }
2152 * @brief Returns the greatest lower bound value of the distribution.
2156 { return result_type(0); }
2159 * @brief Returns the least upper bound value of the distribution.
2163 { return std::numeric_limits<result_type>::max(); }
2165 template<typename _UniformRandomNumberGenerator>
2167 operator()(_UniformRandomNumberGenerator& __urng)
2168 { return this->operator()(__urng, this->param()); }
2170 template<typename _UniformRandomNumberGenerator>
2172 operator()(_UniformRandomNumberGenerator& __urng,
2173 const param_type& __p);
2176 param_type _M_param;
2180 * @brief Return true if two lognormal distributions have
2181 * the same parameters.
2183 template<typename _RealType>
2185 operator==(const std::lognormal_distribution<_RealType>& __d1,
2186 const std::lognormal_distribution<_RealType>& __d2)
2187 { return __d1.param() == __d2.param(); }
2190 * @brief Inserts a %lognormal_distribution random number distribution
2191 * @p __x into the output stream @p __os.
2193 * @param __os An output stream.
2194 * @param __x A %lognormal_distribution random number distribution.
2196 * @returns The output stream with the state of @p __x inserted or in
2199 template<typename _RealType, typename _CharT, typename _Traits>
2200 std::basic_ostream<_CharT, _Traits>&
2201 operator<<(std::basic_ostream<_CharT, _Traits>&,
2202 const std::lognormal_distribution<_RealType>&);
2205 * @brief Extracts a %lognormal_distribution random number distribution
2206 * @p __x from the input stream @p __is.
2208 * @param __is An input stream.
2209 * @param __x A %lognormal_distribution random number
2212 * @returns The input stream with @p __x extracted or in an error state.
2214 template<typename _RealType, typename _CharT, typename _Traits>
2215 std::basic_istream<_CharT, _Traits>&
2216 operator>>(std::basic_istream<_CharT, _Traits>&,
2217 std::lognormal_distribution<_RealType>&);
2221 * @brief A chi_squared_distribution random number distribution.
2223 * The formula for the normal probability mass function is
2224 * @f$ p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}} @f$
2226 template<typename _RealType = double>
2227 class chi_squared_distribution
2230 /** The type of the range of the distribution. */
2231 typedef _RealType result_type;
2232 /** Parameter type. */
2235 typedef chi_squared_distribution<_RealType> distribution_type;
2238 param_type(_RealType __n = _RealType(1))
2247 operator==(const param_type& __p1, const param_type& __p2)
2248 { return __p1._M_n == __p2._M_n; }
2255 chi_squared_distribution(_RealType __n = _RealType(1))
2260 chi_squared_distribution(const param_type& __p)
2265 * @brief Resets the distribution state.
2276 { return _M_param.n(); }
2279 * @brief Returns the parameter set of the distribution.
2283 { return _M_param; }
2286 * @brief Sets the parameter set of the distribution.
2287 * @param __param The new parameter set of the distribution.
2290 param(const param_type& __param)
2291 { _M_param = __param; }
2294 * @brief Returns the greatest lower bound value of the distribution.
2298 { return result_type(0); }
2301 * @brief Returns the least upper bound value of the distribution.
2305 { return std::numeric_limits<result_type>::max(); }
2307 template<typename _UniformRandomNumberGenerator>
2309 operator()(_UniformRandomNumberGenerator& __urng)
2310 { return this->operator()(__urng, this->param()); }
2312 template<typename _UniformRandomNumberGenerator>
2314 operator()(_UniformRandomNumberGenerator& __urng,
2315 const param_type& __p);
2318 param_type _M_param;
2322 * @brief Return true if two Chi-squared distributions have
2323 * the same parameters.
2325 template<typename _RealType>
2327 operator==(const std::chi_squared_distribution<_RealType>& __d1,
2328 const std::chi_squared_distribution<_RealType>& __d2)
2329 { return __d1.param() == __d2.param(); }
2332 * @brief Inserts a %chi_squared_distribution random number distribution
2333 * @p __x into the output stream @p __os.
2335 * @param __os An output stream.
2336 * @param __x A %chi_squared_distribution random number distribution.
2338 * @returns The output stream with the state of @p __x inserted or in
2341 template<typename _RealType, typename _CharT, typename _Traits>
2342 std::basic_ostream<_CharT, _Traits>&
2343 operator<<(std::basic_ostream<_CharT, _Traits>&,
2344 const std::chi_squared_distribution<_RealType>&);
2347 * @brief Extracts a %chi_squared_distribution random number distribution
2348 * @p __x from the input stream @p __is.
2350 * @param __is An input stream.
2351 * @param __x A %chi_squared_distribution random number
2354 * @returns The input stream with @p __x extracted or in an error state.
2356 template<typename _RealType, typename _CharT, typename _Traits>
2357 std::basic_istream<_CharT, _Traits>&
2358 operator>>(std::basic_istream<_CharT, _Traits>&,
2359 std::chi_squared_distribution<_RealType>&);
2363 * @brief A cauchy_distribution random number distribution.
2365 * The formula for the normal probability mass function is
2366 * @f$ p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1} @f$
2368 template<typename _RealType = double>
2369 class cauchy_distribution
2372 /** The type of the range of the distribution. */
2373 typedef _RealType result_type;
2374 /** Parameter type. */
2377 typedef cauchy_distribution<_RealType> distribution_type;
2380 param_type(_RealType __a = _RealType(0),
2381 _RealType __b = _RealType(1))
2382 : _M_a(__a), _M_b(__b)
2394 operator==(const param_type& __p1, const param_type& __p2)
2395 { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
2403 cauchy_distribution(_RealType __a = _RealType(0),
2404 _RealType __b = _RealType(1))
2405 : _M_param(__a, __b)
2409 cauchy_distribution(const param_type& __p)
2414 * @brief Resets the distribution state.
2425 { return _M_param.a(); }
2429 { return _M_param.b(); }
2432 * @brief Returns the parameter set of the distribution.
2436 { return _M_param; }
2439 * @brief Sets the parameter set of the distribution.
2440 * @param __param The new parameter set of the distribution.
2443 param(const param_type& __param)
2444 { _M_param = __param; }
2447 * @brief Returns the greatest lower bound value of the distribution.
2451 { return std::numeric_limits<result_type>::min(); }
2454 * @brief Returns the least upper bound value of the distribution.
2458 { return std::numeric_limits<result_type>::max(); }
2460 template<typename _UniformRandomNumberGenerator>
2462 operator()(_UniformRandomNumberGenerator& __urng)
2463 { return this->operator()(__urng, this->param()); }
2465 template<typename _UniformRandomNumberGenerator>
2467 operator()(_UniformRandomNumberGenerator& __urng,
2468 const param_type& __p);
2471 param_type _M_param;
2475 * @brief Return true if two Cauchy distributions have
2476 * the same parameters.
2478 template<typename _RealType>
2480 operator==(const std::cauchy_distribution<_RealType>& __d1,
2481 const std::cauchy_distribution<_RealType>& __d2)
2482 { return __d1.param() == __d2.param(); }
2485 * @brief Inserts a %cauchy_distribution random number distribution
2486 * @p __x into the output stream @p __os.
2488 * @param __os An output stream.
2489 * @param __x A %cauchy_distribution random number distribution.
2491 * @returns The output stream with the state of @p __x inserted or in
2494 template<typename _RealType, typename _CharT, typename _Traits>
2495 std::basic_ostream<_CharT, _Traits>&
2496 operator<<(std::basic_ostream<_CharT, _Traits>&,
2497 const std::cauchy_distribution<_RealType>&);
2500 * @brief Extracts a %cauchy_distribution random number distribution
2501 * @p __x from the input stream @p __is.
2503 * @param __is An input stream.
2504 * @param __x A %cauchy_distribution random number
2507 * @returns The input stream with @p __x extracted or in an error state.
2509 template<typename _RealType, typename _CharT, typename _Traits>
2510 std::basic_istream<_CharT, _Traits>&
2511 operator>>(std::basic_istream<_CharT, _Traits>&,
2512 std::cauchy_distribution<_RealType>&);
2516 * @brief A fisher_f_distribution random number distribution.
2518 * The formula for the normal probability mass function is
2519 * @f$ p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2520 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2521 * (1 + \frac{mx}{n})^{-(m+n)/2} @f$
2523 template<typename _RealType = double>
2524 class fisher_f_distribution
2527 /** The type of the range of the distribution. */
2528 typedef _RealType result_type;
2529 /** Parameter type. */
2532 typedef fisher_f_distribution<_RealType> distribution_type;
2535 param_type(_RealType __m = _RealType(1),
2536 _RealType __n = _RealType(1))
2537 : _M_m(__m), _M_n(__n)
2549 operator==(const param_type& __p1, const param_type& __p2)
2550 { return (__p1._M_m == __p2._M_m) && (__p1._M_n == __p2._M_n); }
2558 fisher_f_distribution(_RealType __m = _RealType(1),
2559 _RealType __n = _RealType(1))
2560 : _M_param(__m, __n)
2564 fisher_f_distribution(const param_type& __p)
2569 * @brief Resets the distribution state.
2580 { return _M_param.m(); }
2584 { return _M_param.n(); }
2587 * @brief Returns the parameter set of the distribution.
2591 { return _M_param; }
2594 * @brief Sets the parameter set of the distribution.
2595 * @param __param The new parameter set of the distribution.
2598 param(const param_type& __param)
2599 { _M_param = __param; }
2602 * @brief Returns the greatest lower bound value of the distribution.
2606 { return result_type(0); }
2609 * @brief Returns the least upper bound value of the distribution.
2613 { return std::numeric_limits<result_type>::max(); }
2615 template<typename _UniformRandomNumberGenerator>
2617 operator()(_UniformRandomNumberGenerator& __urng)
2618 { return this->operator()(__urng, this->param()); }
2620 template<typename _UniformRandomNumberGenerator>
2622 operator()(_UniformRandomNumberGenerator& __urng,
2623 const param_type& __p);
2626 param_type _M_param;
2630 * @brief Return true if two Fisher f distributions have
2631 * the same parameters.
2633 template<typename _RealType>
2635 operator==(const std::fisher_f_distribution<_RealType>& __d1,
2636 const std::fisher_f_distribution<_RealType>& __d2)
2637 { return __d1.param() == __d2.param(); }
2640 * @brief Inserts a %fisher_f_distribution random number distribution
2641 * @p __x into the output stream @p __os.
2643 * @param __os An output stream.
2644 * @param __x A %fisher_f_distribution random number distribution.
2646 * @returns The output stream with the state of @p __x inserted or in
2649 template<typename _RealType, typename _CharT, typename _Traits>
2650 std::basic_ostream<_CharT, _Traits>&
2651 operator<<(std::basic_ostream<_CharT, _Traits>&,
2652 const std::fisher_f_distribution<_RealType>&);
2655 * @brief Extracts a %fisher_f_distribution random number distribution
2656 * @p __x from the input stream @p __is.
2658 * @param __is An input stream.
2659 * @param __x A %fisher_f_distribution random number
2662 * @returns The input stream with @p __x extracted or in an error state.
2664 template<typename _RealType, typename _CharT, typename _Traits>
2665 std::basic_istream<_CharT, _Traits>&
2666 operator>>(std::basic_istream<_CharT, _Traits>&,
2667 std::fisher_f_distribution<_RealType>&);
2671 * @brief A student_t_distribution random number distribution.
2673 * The formula for the normal probability mass function is
2674 * @f$ p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2675 * (1 + \frac{x^2}{n}) ^{-(n+1)/2} @f$
2677 template<typename _RealType = double>
2678 class student_t_distribution
2681 /** The type of the range of the distribution. */
2682 typedef _RealType result_type;
2683 /** Parameter type. */
2686 typedef student_t_distribution<_RealType> distribution_type;
2689 param_type(_RealType __n = _RealType(1))
2698 operator==(const param_type& __p1, const param_type& __p2)
2699 { return __p1._M_n == __p2._M_n; }
2706 student_t_distribution(_RealType __n = _RealType(1))
2711 student_t_distribution(const param_type& __p)
2716 * @brief Resets the distribution state.
2727 { return _M_param.n(); }
2730 * @brief Returns the parameter set of the distribution.
2734 { return _M_param; }
2737 * @brief Sets the parameter set of the distribution.
2738 * @param __param The new parameter set of the distribution.
2741 param(const param_type& __param)
2742 { _M_param = __param; }
2745 * @brief Returns the greatest lower bound value of the distribution.
2749 { return std::numeric_limits<result_type>::min(); }
2752 * @brief Returns the least upper bound value of the distribution.
2756 { return std::numeric_limits<result_type>::max(); }
2758 template<typename _UniformRandomNumberGenerator>
2760 operator()(_UniformRandomNumberGenerator& __urng)
2761 { return this->operator()(__urng, this->param()); }
2763 template<typename _UniformRandomNumberGenerator>
2765 operator()(_UniformRandomNumberGenerator& __urng,
2766 const param_type& __p);
2769 template<typename _UniformRandomNumberGenerator>
2771 _M_gaussian(_UniformRandomNumberGenerator& __urng,
2772 const result_type __sigma);
2774 param_type _M_param;
2778 * @brief Return true if two Student t distributions have
2779 * the same parameters.
2781 template<typename _RealType>
2783 operator==(const std::student_t_distribution<_RealType>& __d1,
2784 const std::student_t_distribution<_RealType>& __d2)
2785 { return __d1.param() == __d2.param(); }
2788 * @brief Inserts a %student_t_distribution random number distribution
2789 * @p __x into the output stream @p __os.
2791 * @param __os An output stream.
2792 * @param __x A %student_t_distribution random number distribution.
2794 * @returns The output stream with the state of @p __x inserted or in
2797 template<typename _RealType, typename _CharT, typename _Traits>
2798 std::basic_ostream<_CharT, _Traits>&
2799 operator<<(std::basic_ostream<_CharT, _Traits>&,
2800 const std::student_t_distribution<_RealType>&);
2803 * @brief Extracts a %student_t_distribution random number distribution
2804 * @p __x from the input stream @p __is.
2806 * @param __is An input stream.
2807 * @param __x A %student_t_distribution random number
2810 * @returns The input stream with @p __x extracted or in an error state.
2812 template<typename _RealType, typename _CharT, typename _Traits>
2813 std::basic_istream<_CharT, _Traits>&
2814 operator>>(std::basic_istream<_CharT, _Traits>&,
2815 std::student_t_distribution<_RealType>&);
2817 /* @} */ // group std_random_distributions_normal
2820 * @addtogroup std_random_distributions_bernoulli Bernoulli Distributions
2821 * @ingroup std_random_distributions
2826 * @brief A Bernoulli random number distribution.
2828 * Generates a sequence of true and false values with likelihood @f$ p @f$
2829 * that true will come up and @f$ (1 - p) @f$ that false will appear.
2831 class bernoulli_distribution
2834 /** The type of the range of the distribution. */
2835 typedef bool result_type;
2836 /** Parameter type. */
2839 typedef bernoulli_distribution distribution_type;
2842 param_type(double __p = 0.5)
2845 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
2853 operator==(const param_type& __p1, const param_type& __p2)
2854 { return __p1._M_p == __p2._M_p; }
2862 * @brief Constructs a Bernoulli distribution with likelihood @p p.
2864 * @param __p [IN] The likelihood of a true result being returned.
2865 * Must be in the interval @f$ [0, 1] @f$.
2868 bernoulli_distribution(double __p = 0.5)
2873 bernoulli_distribution(const param_type& __p)
2878 * @brief Resets the distribution state.
2880 * Does nothing for a Bernoulli distribution.
2886 * @brief Returns the @p p parameter of the distribution.
2890 { return _M_param.p(); }
2893 * @brief Returns the parameter set of the distribution.
2897 { return _M_param; }
2900 * @brief Sets the parameter set of the distribution.
2901 * @param __param The new parameter set of the distribution.
2904 param(const param_type& __param)
2905 { _M_param = __param; }
2908 * @brief Returns the greatest lower bound value of the distribution.
2912 { return std::numeric_limits<result_type>::min(); }
2915 * @brief Returns the least upper bound value of the distribution.
2919 { return std::numeric_limits<result_type>::max(); }
2922 * @brief Returns the next value in the Bernoullian sequence.
2924 template<typename _UniformRandomNumberGenerator>
2926 operator()(_UniformRandomNumberGenerator& __urng)
2928 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2930 if ((__aurng() - __aurng.min())
2931 < this->p() * (__aurng.max() - __aurng.min()))
2936 template<typename _UniformRandomNumberGenerator>
2938 operator()(_UniformRandomNumberGenerator& __urng,
2939 const param_type& __p)
2941 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2943 if ((__aurng() - __aurng.min())
2944 < __p.p() * (__aurng.max() - __aurng.min()))
2950 param_type _M_param;
2954 * @brief Return true if two Bernoulli distributions have
2955 * the same parameters.
2958 operator==(const std::bernoulli_distribution& __d1,
2959 const std::bernoulli_distribution& __d2)
2960 { return __d1.param() == __d2.param(); }
2963 * @brief Inserts a %bernoulli_distribution random number distribution
2964 * @p __x into the output stream @p __os.
2966 * @param __os An output stream.
2967 * @param __x A %bernoulli_distribution random number distribution.
2969 * @returns The output stream with the state of @p __x inserted or in
2972 template<typename _CharT, typename _Traits>
2973 std::basic_ostream<_CharT, _Traits>&
2974 operator<<(std::basic_ostream<_CharT, _Traits>&,
2975 const std::bernoulli_distribution&);
2978 * @brief Extracts a %bernoulli_distribution random number distribution
2979 * @p __x from the input stream @p __is.
2981 * @param __is An input stream.
2982 * @param __x A %bernoulli_distribution random number generator engine.
2984 * @returns The input stream with @p __x extracted or in an error state.
2986 template<typename _CharT, typename _Traits>
2987 std::basic_istream<_CharT, _Traits>&
2988 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2989 std::bernoulli_distribution& __x)
2993 __x.param(bernoulli_distribution::param_type(__p));
2999 * @brief A discrete binomial random number distribution.
3001 * The formula for the binomial probability density function is
3002 * @f$ p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3003 * and @f$ p @f$ are the parameters of the distribution.
3005 template<typename _IntType = int>
3006 class binomial_distribution
3008 __glibcxx_class_requires(_IntType, _IntegerConcept)
3011 /** The type of the range of the distribution. */
3012 typedef _IntType result_type;
3013 /** Parameter type. */
3016 typedef binomial_distribution<_IntType> distribution_type;
3017 friend class binomial_distribution<_IntType>;
3020 param_type(_IntType __t = _IntType(1), double __p = 0.5)
3021 : _M_t(__t), _M_p(__p)
3023 _GLIBCXX_DEBUG_ASSERT((_M_t >= _IntType(0))
3038 operator==(const param_type& __p1, const param_type& __p2)
3039 { return (__p1._M_t == __p2._M_t) && (__p1._M_p == __p2._M_p); }
3049 #if _GLIBCXX_USE_C99_MATH_TR1
3050 double _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
3051 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
3056 // constructors and member function
3058 binomial_distribution(_IntType __t = _IntType(1),
3060 : _M_param(__t, __p), _M_nd()
3064 binomial_distribution(const param_type& __p)
3065 : _M_param(__p), _M_nd()
3069 * @brief Resets the distribution state.
3076 * @brief Returns the distribution @p t parameter.
3080 { return _M_param.t(); }
3083 * @brief Returns the distribution @p p parameter.
3087 { return _M_param.p(); }
3090 * @brief Returns the parameter set of the distribution.
3094 { return _M_param; }
3097 * @brief Sets the parameter set of the distribution.
3098 * @param __param The new parameter set of the distribution.
3101 param(const param_type& __param)
3102 { _M_param = __param; }
3105 * @brief Returns the greatest lower bound value of the distribution.
3112 * @brief Returns the least upper bound value of the distribution.
3116 { return _M_param.t(); }
3119 * @brief Return true if two binomial distributions have
3120 * the same parameters.
3122 template<typename _IntType1>
3124 operator==(const std::binomial_distribution<_IntType1>& __d1,
3125 const std::binomial_distribution<_IntType1>& __d2)
3126 { return ((__d1.param() == __d2.param())
3127 && (__d1._M_nd == __d2._M_nd)); }
3129 template<typename _UniformRandomNumberGenerator>
3131 operator()(_UniformRandomNumberGenerator& __urng)
3132 { return this->operator()(__urng, this->param()); }
3134 template<typename _UniformRandomNumberGenerator>
3136 operator()(_UniformRandomNumberGenerator& __urng,
3137 const param_type& __p);
3140 * @brief Inserts a %binomial_distribution random number distribution
3141 * @p __x into the output stream @p __os.
3143 * @param __os An output stream.
3144 * @param __x A %binomial_distribution random number distribution.
3146 * @returns The output stream with the state of @p __x inserted or in
3149 template<typename _IntType1,
3150 typename _CharT, typename _Traits>
3151 friend std::basic_ostream<_CharT, _Traits>&
3152 operator<<(std::basic_ostream<_CharT, _Traits>&,
3153 const std::binomial_distribution<_IntType1>&);
3156 * @brief Extracts a %binomial_distribution random number distribution
3157 * @p __x from the input stream @p __is.
3159 * @param __is An input stream.
3160 * @param __x A %binomial_distribution random number generator engine.
3162 * @returns The input stream with @p __x extracted or in an error
3165 template<typename _IntType1,
3166 typename _CharT, typename _Traits>
3167 friend std::basic_istream<_CharT, _Traits>&
3168 operator>>(std::basic_istream<_CharT, _Traits>&,
3169 std::binomial_distribution<_IntType1>&);
3172 template<typename _UniformRandomNumberGenerator>
3174 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
3176 param_type _M_param;
3178 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3179 normal_distribution<double> _M_nd;
3184 * @brief A discrete geometric random number distribution.
3186 * The formula for the geometric probability density function is
3187 * @f$ p(i|p) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
3190 template<typename _IntType = int>
3191 class geometric_distribution
3193 __glibcxx_class_requires(_IntType, _IntegerConcept)
3196 /** The type of the range of the distribution. */
3197 typedef _IntType result_type;
3198 /** Parameter type. */
3201 typedef geometric_distribution<_IntType> distribution_type;
3202 friend class geometric_distribution<_IntType>;
3205 param_type(double __p = 0.5)
3208 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0)
3218 operator==(const param_type& __p1, const param_type& __p2)
3219 { return __p1._M_p == __p2._M_p; }
3224 { _M_log_p = std::log(_M_p); }
3231 // constructors and member function
3233 geometric_distribution(double __p = 0.5)
3238 geometric_distribution(const param_type& __p)
3243 * @brief Resets the distribution state.
3245 * Does nothing for the geometric distribution.
3251 * @brief Returns the distribution parameter @p p.
3255 { return _M_param.p(); }
3258 * @brief Returns the parameter set of the distribution.
3262 { return _M_param; }
3265 * @brief Sets the parameter set of the distribution.
3266 * @param __param The new parameter set of the distribution.
3269 param(const param_type& __param)
3270 { _M_param = __param; }
3273 * @brief Returns the greatest lower bound value of the distribution.
3280 * @brief Returns the least upper bound value of the distribution.
3284 { return std::numeric_limits<result_type>::max(); }
3286 template<typename _UniformRandomNumberGenerator>
3288 operator()(_UniformRandomNumberGenerator& __urng)
3289 { return this->operator()(__urng, this->param()); }
3291 template<typename _UniformRandomNumberGenerator>
3293 operator()(_UniformRandomNumberGenerator& __urng,
3294 const param_type& __p);
3297 param_type _M_param;
3301 * @brief Return true if two geometric distributions have
3302 * the same parameters.
3304 template<typename _IntType>
3306 operator==(const geometric_distribution<_IntType>& __d1,
3307 const geometric_distribution<_IntType>& __d2)
3308 { return __d1.param() == __d2.param(); }
3311 * @brief Inserts a %geometric_distribution random number distribution
3312 * @p __x into the output stream @p __os.
3314 * @param __os An output stream.
3315 * @param __x A %geometric_distribution random number distribution.
3317 * @returns The output stream with the state of @p __x inserted or in
3320 template<typename _IntType,
3321 typename _CharT, typename _Traits>
3322 std::basic_ostream<_CharT, _Traits>&
3323 operator<<(std::basic_ostream<_CharT, _Traits>&,
3324 const std::geometric_distribution<_IntType>&);
3327 * @brief Extracts a %geometric_distribution random number distribution
3328 * @p __x from the input stream @p __is.
3330 * @param __is An input stream.
3331 * @param __x A %geometric_distribution random number generator engine.
3333 * @returns The input stream with @p __x extracted or in an error state.
3335 template<typename _IntType,
3336 typename _CharT, typename _Traits>
3337 std::basic_istream<_CharT, _Traits>&
3338 operator>>(std::basic_istream<_CharT, _Traits>&,
3339 std::geometric_distribution<_IntType>&);
3343 * @brief A negative_binomial_distribution random number distribution.
3345 * The formula for the negative binomial probability mass function is
3346 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3347 * and @f$ p @f$ are the parameters of the distribution.
3349 template<typename _IntType = int>
3350 class negative_binomial_distribution
3352 __glibcxx_class_requires(_IntType, _IntegerConcept)
3355 /** The type of the range of the distribution. */
3356 typedef _IntType result_type;
3357 /** Parameter type. */
3360 typedef negative_binomial_distribution<_IntType> distribution_type;
3363 param_type(_IntType __k = 1, double __p = 0.5)
3364 : _M_k(__k), _M_p(__p)
3376 operator==(const param_type& __p1, const param_type& __p2)
3377 { return (__p1._M_k == __p2._M_k) && (__p1._M_p == __p2._M_p); }
3385 negative_binomial_distribution(_IntType __k = 1, double __p = 0.5)
3386 : _M_param(__k, __p)
3390 negative_binomial_distribution(const param_type& __p)
3395 * @brief Resets the distribution state.
3402 * @brief Return the @f$ k @f$ parameter of the distribution.
3406 { return _M_param.k(); }
3409 * @brief Return the @f$ p @f$ parameter of the distribution.
3413 { return _M_param.p(); }
3416 * @brief Returns the parameter set of the distribution.
3420 { return _M_param; }
3423 * @brief Sets the parameter set of the distribution.
3424 * @param __param The new parameter set of the distribution.
3427 param(const param_type& __param)
3428 { _M_param = __param; }
3431 * @brief Returns the greatest lower bound value of the distribution.
3435 { return result_type(0); }
3438 * @brief Returns the least upper bound value of the distribution.
3442 { return std::numeric_limits<result_type>::max(); }
3444 template<typename _UniformRandomNumberGenerator>
3446 operator()(_UniformRandomNumberGenerator& __urng)
3447 { return this->operator()(__urng, this->param()); }
3449 template<typename _UniformRandomNumberGenerator>
3451 operator()(_UniformRandomNumberGenerator& __urng,
3452 const param_type& __p);
3455 param_type _M_param;
3459 * @brief Return true if two negative binomial distributions have
3460 * the same parameters.
3462 template<typename _IntType>
3464 operator==(const std::negative_binomial_distribution<_IntType>& __d1,
3465 const std::negative_binomial_distribution<_IntType>& __d2)
3466 { return __d1.param() == __d2.param(); }
3469 * @brief Inserts a %negative_binomial_distribution random
3470 * number distribution @p __x into the output stream @p __os.
3472 * @param __os An output stream.
3473 * @param __x A %negative_binomial_distribution random number
3476 * @returns The output stream with the state of @p __x inserted or in
3479 template<typename _IntType, typename _CharT, typename _Traits>
3480 std::basic_ostream<_CharT, _Traits>&
3481 operator<<(std::basic_ostream<_CharT, _Traits>&,
3482 const std::negative_binomial_distribution<_IntType>&);
3485 * @brief Extracts a %negative_binomial_distribution random number
3486 * distribution @p __x from the input stream @p __is.
3488 * @param __is An input stream.
3489 * @param __x A %negative_binomial_distribution random number
3492 * @returns The input stream with @p __x extracted or in an error state.
3494 template<typename _IntType, typename _CharT, typename _Traits>
3495 std::basic_istream<_CharT, _Traits>&
3496 operator>>(std::basic_istream<_CharT, _Traits>&,
3497 std::negative_binomial_distribution<_IntType>&);
3499 /* @} */ // group std_random_distributions_bernoulli
3502 * @addtogroup std_random_distributions_poisson Poisson Distributions
3503 * @ingroup std_random_distributions
3508 * @brief A discrete Poisson random number distribution.
3510 * The formula for the Poisson probability density function is
3511 * @f$ p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu} @f$ where @f$ \mu @f$ is the
3512 * parameter of the distribution.
3514 template<typename _IntType = int>
3515 class poisson_distribution
3517 __glibcxx_class_requires(_IntType, _IntegerConcept)
3520 /** The type of the range of the distribution. */
3521 typedef _IntType result_type;
3522 /** Parameter type. */
3525 typedef poisson_distribution<_IntType> distribution_type;
3526 friend class poisson_distribution<_IntType>;
3529 param_type(double __mean = 1.0)
3532 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
3541 operator==(const param_type& __p1, const param_type& __p2)
3542 { return __p1._M_mean == __p2._M_mean; }
3545 // Hosts either log(mean) or the threshold of the simple method.
3552 #if _GLIBCXX_USE_C99_MATH_TR1
3553 double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
3557 // constructors and member function
3559 poisson_distribution(double __mean = 1.0)
3560 : _M_param(__mean), _M_nd()
3564 poisson_distribution(const param_type& __p)
3565 : _M_param(__p), _M_nd()
3569 * @brief Resets the distribution state.
3576 * @brief Returns the distribution parameter @p mean.
3580 { return _M_param.mean(); }
3583 * @brief Returns the parameter set of the distribution.
3587 { return _M_param; }
3590 * @brief Sets the parameter set of the distribution.
3591 * @param __param The new parameter set of the distribution.
3594 param(const param_type& __param)
3595 { _M_param = __param; }
3598 * @brief Returns the greatest lower bound value of the distribution.
3605 * @brief Returns the least upper bound value of the distribution.
3609 { return std::numeric_limits<result_type>::max(); }
3611 template<typename _UniformRandomNumberGenerator>
3613 operator()(_UniformRandomNumberGenerator& __urng)
3614 { return this->operator()(__urng, this->param()); }
3616 template<typename _UniformRandomNumberGenerator>
3618 operator()(_UniformRandomNumberGenerator& __urng,
3619 const param_type& __p);
3622 * @brief Return true if two Poisson distributions have the same
3625 template<typename _IntType1>
3627 operator==(const std::poisson_distribution<_IntType1>& __d1,
3628 const std::poisson_distribution<_IntType1>& __d2)
3629 { return ((__d1.param() == __d2.param())
3630 && (__d1._M_nd == __d2._M_nd)); }
3633 * @brief Inserts a %poisson_distribution random number distribution
3634 * @p __x into the output stream @p __os.
3636 * @param __os An output stream.
3637 * @param __x A %poisson_distribution random number distribution.
3639 * @returns The output stream with the state of @p __x inserted or in
3642 template<typename _IntType1, typename _CharT, typename _Traits>
3643 friend std::basic_ostream<_CharT, _Traits>&
3644 operator<<(std::basic_ostream<_CharT, _Traits>&,
3645 const std::poisson_distribution<_IntType1>&);
3648 * @brief Extracts a %poisson_distribution random number distribution
3649 * @p __x from the input stream @p __is.
3651 * @param __is An input stream.
3652 * @param __x A %poisson_distribution random number generator engine.
3654 * @returns The input stream with @p __x extracted or in an error
3657 template<typename _IntType1, typename _CharT, typename _Traits>
3658 friend std::basic_istream<_CharT, _Traits>&
3659 operator>>(std::basic_istream<_CharT, _Traits>&,
3660 std::poisson_distribution<_IntType1>&);
3663 param_type _M_param;
3665 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3666 normal_distribution<double> _M_nd;
3670 * @brief An exponential continuous distribution for random numbers.
3672 * The formula for the exponential probability density function is
3673 * @f$ p(x|\lambda) = \lambda e^{-\lambda x} @f$.
3675 * <table border=1 cellpadding=10 cellspacing=0>
3676 * <caption align=top>Distribution Statistics</caption>
3677 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3678 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
3679 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
3680 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3681 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3684 template<typename _RealType = double>
3685 class exponential_distribution
3688 /** The type of the range of the distribution. */
3689 typedef _RealType result_type;
3690 /** Parameter type. */
3693 typedef exponential_distribution<_RealType> distribution_type;
3696 param_type(_RealType __lambda = _RealType(1))
3697 : _M_lambda(__lambda)
3699 _GLIBCXX_DEBUG_ASSERT(_M_lambda > _RealType(0));
3704 { return _M_lambda; }
3707 operator==(const param_type& __p1, const param_type& __p2)
3708 { return __p1._M_lambda == __p2._M_lambda; }
3711 _RealType _M_lambda;
3716 * @brief Constructs an exponential distribution with inverse scale
3717 * parameter @f$ \lambda @f$.
3720 exponential_distribution(const result_type& __lambda = result_type(1))
3721 : _M_param(__lambda)
3725 exponential_distribution(const param_type& __p)
3730 * @brief Resets the distribution state.
3732 * Has no effect on exponential distributions.
3738 * @brief Returns the inverse scale parameter of the distribution.
3742 { return _M_param.lambda(); }
3745 * @brief Returns the parameter set of the distribution.
3749 { return _M_param; }
3752 * @brief Sets the parameter set of the distribution.
3753 * @param __param The new parameter set of the distribution.
3756 param(const param_type& __param)
3757 { _M_param = __param; }
3760 * @brief Returns the greatest lower bound value of the distribution.
3764 { return result_type(0); }
3767 * @brief Returns the least upper bound value of the distribution.
3771 { return std::numeric_limits<result_type>::max(); }
3773 template<typename _UniformRandomNumberGenerator>
3775 operator()(_UniformRandomNumberGenerator& __urng)
3777 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
3779 return -std::log(__aurng()) / this->lambda();
3782 template<typename _UniformRandomNumberGenerator>
3784 operator()(_UniformRandomNumberGenerator& __urng,
3785 const param_type& __p)
3787 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
3789 return -std::log(__aurng()) / __p.lambda();
3793 param_type _M_param;
3797 * @brief Return true if two exponential distributions have the same
3800 template<typename _RealType>
3802 operator==(const std::exponential_distribution<_RealType>& __d1,
3803 const std::exponential_distribution<_RealType>& __d2)
3804 { return __d1.param() == __d2.param(); }
3807 * @brief Inserts a %exponential_distribution random number distribution
3808 * @p __x into the output stream @p __os.
3810 * @param __os An output stream.
3811 * @param __x A %exponential_distribution random number distribution.
3813 * @returns The output stream with the state of @p __x inserted or in
3816 template<typename _RealType, typename _CharT, typename _Traits>
3817 std::basic_ostream<_CharT, _Traits>&
3818 operator<<(std::basic_ostream<_CharT, _Traits>&,
3819 const std::exponential_distribution<_RealType>&);
3822 * @brief Extracts a %exponential_distribution random number distribution
3823 * @p __x from the input stream @p __is.
3825 * @param __is An input stream.
3826 * @param __x A %exponential_distribution random number
3829 * @returns The input stream with @p __x extracted or in an error state.
3831 template<typename _RealType, typename _CharT, typename _Traits>
3832 std::basic_istream<_CharT, _Traits>&
3833 operator>>(std::basic_istream<_CharT, _Traits>&,
3834 std::exponential_distribution<_RealType>&);
3838 * @brief A gamma continuous distribution for random numbers.
3840 * The formula for the gamma probability density function is
3841 * @f$ p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
3842 * (x/\beta)^{\alpha - 1} e^{-x/\beta} @f$.
3844 template<typename _RealType = double>
3845 class gamma_distribution
3848 /** The type of the range of the distribution. */
3849 typedef _RealType result_type;
3850 /** Parameter type. */
3853 typedef gamma_distribution<_RealType> distribution_type;
3854 friend class gamma_distribution<_RealType>;
3857 param_type(_RealType __alpha = _RealType(1),
3858 _RealType __beta = _RealType(1))
3859 : _M_alpha(__alpha), _M_beta(__beta)
3861 _GLIBCXX_DEBUG_ASSERT(_M_alpha > _RealType(0));
3867 { return _M_alpha; }
3874 operator==(const param_type& __p1, const param_type& __p2)
3875 { return ((__p1._M_alpha == __p2._M_alpha)
3876 && (__p1._M_beta == __p2._M_beta)); }
3885 // Hosts either lambda of GB or d of modified Vaduva's.
3891 * @brief Constructs a gamma distribution with parameters
3892 * @f$ \alpha @f$ and @f$ \beta @f$.
3895 gamma_distribution(_RealType __alpha = _RealType(1),
3896 _RealType __beta = _RealType(1))
3897 : _M_param(__alpha, __beta)
3901 gamma_distribution(const param_type& __p)
3906 * @brief Resets the distribution state.
3908 * Does nothing for the gamma distribution.
3914 * @brief Returns the @f$ \alpha @f$ of the distribution.
3918 { return _M_param.alpha(); }
3921 * @brief Returns the @f$ \beta @f$ of the distribution.
3925 { return _M_param.beta(); }
3928 * @brief Returns the parameter set of the distribution.
3932 { return _M_param; }
3935 * @brief Sets the parameter set of the distribution.
3936 * @param __param The new parameter set of the distribution.
3939 param(const param_type& __param)
3940 { _M_param = __param; }
3943 * @brief Returns the greatest lower bound value of the distribution.
3947 { return result_type(0); }
3950 * @brief Returns the least upper bound value of the distribution.
3954 { return std::numeric_limits<result_type>::max(); }
3956 template<typename _UniformRandomNumberGenerator>
3958 operator()(_UniformRandomNumberGenerator& __urng)
3959 { return this->operator()(__urng, this->param()); }
3961 template<typename _UniformRandomNumberGenerator>
3963 operator()(_UniformRandomNumberGenerator& __urng,
3964 const param_type& __p);
3967 param_type _M_param;
3971 * @brief Return true if two gamma distributions have the same
3974 template<typename _RealType>
3976 operator==(const std::gamma_distribution<_RealType>& __d1,
3977 const std::gamma_distribution<_RealType>& __d2)
3978 { return __d1.param() == __d2.param(); }
3981 * @brief Inserts a %gamma_distribution random number distribution
3982 * @p __x into the output stream @p __os.
3984 * @param __os An output stream.
3985 * @param __x A %gamma_distribution random number distribution.
3987 * @returns The output stream with the state of @p __x inserted or in
3990 template<typename _RealType, typename _CharT, typename _Traits>
3991 std::basic_ostream<_CharT, _Traits>&
3992 operator<<(std::basic_ostream<_CharT, _Traits>&,
3993 const std::gamma_distribution<_RealType>&);
3996 * @brief Extracts a %gamma_distribution random number distribution
3997 * @p __x from the input stream @p __is.
3999 * @param __is An input stream.
4000 * @param __x A %gamma_distribution random number generator engine.
4002 * @returns The input stream with @p __x extracted or in an error state.
4004 template<typename _RealType, typename _CharT, typename _Traits>
4005 std::basic_istream<_CharT, _Traits>&
4006 operator>>(std::basic_istream<_CharT, _Traits>&,
4007 std::gamma_distribution<_RealType>&);
4011 * @brief A weibull_distribution random number distribution.
4013 * The formula for the normal probability density function is
4014 * @f$ p(x|\alpha,\beta) = \frac{a}{b} (frac{x}{b})^{a-1}
4015 * \exp{(-(frac{x}{b})^a)} @f$.
4017 template<typename _RealType = double>
4018 class weibull_distribution
4021 /** The type of the range of the distribution. */
4022 typedef _RealType result_type;
4023 /** Parameter type. */
4026 typedef weibull_distribution<_RealType> distribution_type;
4029 param_type(_RealType __a = _RealType(1),
4030 _RealType __b = _RealType(1))
4031 : _M_a(__a), _M_b(__b)
4043 operator==(const param_type& __p1, const param_type& __p2)
4044 { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
4052 weibull_distribution(_RealType __a = _RealType(1),
4053 _RealType __b = _RealType(1))
4054 : _M_param(__a, __b)
4058 weibull_distribution(const param_type& __p)
4063 * @brief Resets the distribution state.
4070 * @brief Return the @f$ a @f$ parameter of the distribution.
4074 { return _M_param.a(); }
4077 * @brief Return the @f$ b @f$ parameter of the distribution.
4081 { return _M_param.b(); }
4084 * @brief Returns the parameter set of the distribution.
4088 { return _M_param; }
4091 * @brief Sets the parameter set of the distribution.
4092 * @param __param The new parameter set of the distribution.
4095 param(const param_type& __param)
4096 { _M_param = __param; }
4099 * @brief Returns the greatest lower bound value of the distribution.
4103 { return result_type(0); }
4106 * @brief Returns the least upper bound value of the distribution.
4110 { return std::numeric_limits<result_type>::max(); }
4112 template<typename _UniformRandomNumberGenerator>
4114 operator()(_UniformRandomNumberGenerator& __urng)
4115 { return this->operator()(__urng, this->param()); }
4117 template<typename _UniformRandomNumberGenerator>
4119 operator()(_UniformRandomNumberGenerator& __urng,
4120 const param_type& __p)
4122 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
4124 return __p.b() * std::pow(-std::log(__aurng()),
4125 result_type(1) / __p.a());
4129 param_type _M_param;
4133 * @brief Return true if two Weibull distributions have the same
4136 template<typename _RealType>
4138 operator==(const std::weibull_distribution<_RealType>& __d1,
4139 const std::weibull_distribution<_RealType>& __d2)
4140 { return __d1.param() == __d2.param(); }
4143 * @brief Inserts a %weibull_distribution random number distribution
4144 * @p __x into the output stream @p __os.
4146 * @param __os An output stream.
4147 * @param __x A %weibull_distribution random number distribution.
4149 * @returns The output stream with the state of @p __x inserted or in
4152 template<typename _RealType, typename _CharT, typename _Traits>
4153 std::basic_ostream<_CharT, _Traits>&
4154 operator<<(std::basic_ostream<_CharT, _Traits>&,
4155 const std::weibull_distribution<_RealType>&);
4158 * @brief Extracts a %weibull_distribution random number distribution
4159 * @p __x from the input stream @p __is.
4161 * @param __is An input stream.
4162 * @param __x A %weibull_distribution random number
4165 * @returns The input stream with @p __x extracted or in an error state.
4167 template<typename _RealType, typename _CharT, typename _Traits>
4168 std::basic_istream<_CharT, _Traits>&
4169 operator>>(std::basic_istream<_CharT, _Traits>&,
4170 std::weibull_distribution<_RealType>&);
4174 * @brief A extreme_value_distribution random number distribution.
4176 * The formula for the normal probability mass function is
4177 * @f$ p(x|a,b) = \frac{1}{b}
4178 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) @f$
4180 template<typename _RealType = double>
4181 class extreme_value_distribution
4184 /** The type of the range of the distribution. */
4185 typedef _RealType result_type;
4186 /** Parameter type. */
4189 typedef extreme_value_distribution<_RealType> distribution_type;
4192 param_type(_RealType __a = _RealType(0),
4193 _RealType __b = _RealType(1))
4194 : _M_a(__a), _M_b(__b)
4206 operator==(const param_type& __p1, const param_type& __p2)
4207 { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
4215 extreme_value_distribution(_RealType __a = _RealType(0),
4216 _RealType __b = _RealType(1))
4217 : _M_param(__a, __b)
4221 extreme_value_distribution(const param_type& __p)
4226 * @brief Resets the distribution state.
4233 * @brief Return the @f$ a @f$ parameter of the distribution.
4237 { return _M_param.a(); }
4240 * @brief Return the @f$ b @f$ parameter of the distribution.
4244 { return _M_param.b(); }
4247 * @brief Returns the parameter set of the distribution.
4251 { return _M_param; }
4254 * @brief Sets the parameter set of the distribution.
4255 * @param __param The new parameter set of the distribution.
4258 param(const param_type& __param)
4259 { _M_param = __param; }
4262 * @brief Returns the greatest lower bound value of the distribution.
4266 { return std::numeric_limits<result_type>::min(); }
4269 * @brief Returns the least upper bound value of the distribution.
4273 { return std::numeric_limits<result_type>::max(); }
4275 template<typename _UniformRandomNumberGenerator>
4277 operator()(_UniformRandomNumberGenerator& __urng)
4278 { return this->operator()(__urng, this->param()); }
4280 template<typename _UniformRandomNumberGenerator>
4282 operator()(_UniformRandomNumberGenerator& __urng,
4283 const param_type& __p);
4286 param_type _M_param;
4292 template<typename _RealType>
4294 operator==(const std::extreme_value_distribution<_RealType>& __d1,
4295 const std::extreme_value_distribution<_RealType>& __d2)
4296 { return __d1.param() == __d2.param(); }
4299 * @brief Inserts a %extreme_value_distribution random number distribution
4300 * @p __x into the output stream @p __os.
4302 * @param __os An output stream.
4303 * @param __x A %extreme_value_distribution random number distribution.
4305 * @returns The output stream with the state of @p __x inserted or in
4308 template<typename _RealType, typename _CharT, typename _Traits>
4309 std::basic_ostream<_CharT, _Traits>&
4310 operator<<(std::basic_ostream<_CharT, _Traits>&,
4311 const std::extreme_value_distribution<_RealType>&);
4314 * @brief Extracts a %extreme_value_distribution random number
4315 * distribution @p __x from the input stream @p __is.
4317 * @param __is An input stream.
4318 * @param __x A %extreme_value_distribution random number
4321 * @returns The input stream with @p __x extracted or in an error state.
4323 template<typename _RealType, typename _CharT, typename _Traits>
4324 std::basic_istream<_CharT, _Traits>&
4325 operator>>(std::basic_istream<_CharT, _Traits>&,
4326 std::extreme_value_distribution<_RealType>&);
4330 * @brief A discrete_distribution random number distribution.
4332 * The formula for the discrete probability mass function is
4335 template<typename _IntType = int>
4336 class discrete_distribution
4338 __glibcxx_class_requires(_IntType, _IntegerConcept)
4341 /** The type of the range of the distribution. */
4342 typedef _IntType result_type;
4343 /** Parameter type. */
4346 typedef discrete_distribution<_IntType> distribution_type;
4347 friend class discrete_distribution<_IntType>;
4350 : _M_prob(), _M_cp()
4351 { _M_initialize(); }
4353 template<typename _InputIterator>
4354 param_type(_InputIterator __wbegin,
4355 _InputIterator __wend)
4356 : _M_prob(__wbegin, __wend), _M_cp()
4357 { _M_initialize(); }
4359 param_type(initializer_list<double> __wil)
4360 : _M_prob(__wil.begin(), __wil.end()), _M_cp()
4361 { _M_initialize(); }
4363 template<typename _Func>
4364 param_type(size_t __nw, double __xmin, double __xmax,
4368 probabilities() const
4372 operator==(const param_type& __p1, const param_type& __p2)
4373 { return __p1._M_prob == __p2._M_prob; }
4379 std::vector<double> _M_prob;
4380 std::vector<double> _M_cp;
4383 discrete_distribution()
4387 template<typename _InputIterator>
4388 discrete_distribution(_InputIterator __wbegin,
4389 _InputIterator __wend)
4390 : _M_param(__wbegin, __wend)
4393 discrete_distribution(initializer_list<double> __wil)
4397 template<typename _Func>
4398 discrete_distribution(size_t __nw, double __xmin, double __xmax,
4400 : _M_param(__nw, __xmin, __xmax, __fw)
4404 discrete_distribution(const param_type& __p)
4409 * @brief Resets the distribution state.
4416 * @brief Returns the probabilities of the distribution.
4419 probabilities() const
4420 { return _M_param.probabilities(); }
4423 * @brief Returns the parameter set of the distribution.
4427 { return _M_param; }
4430 * @brief Sets the parameter set of the distribution.
4431 * @param __param The new parameter set of the distribution.
4434 param(const param_type& __param)
4435 { _M_param = __param; }
4438 * @brief Returns the greatest lower bound value of the distribution.
4442 { return result_type(0); }
4445 * @brief Returns the least upper bound value of the distribution.
4449 { return this->_M_param._M_prob.size() - 1; }
4451 template<typename _UniformRandomNumberGenerator>
4453 operator()(_UniformRandomNumberGenerator& __urng)
4454 { return this->operator()(__urng, this->param()); }
4456 template<typename _UniformRandomNumberGenerator>
4458 operator()(_UniformRandomNumberGenerator& __urng,
4459 const param_type& __p);
4462 * @brief Inserts a %discrete_distribution random number distribution
4463 * @p __x into the output stream @p __os.
4465 * @param __os An output stream.
4466 * @param __x A %discrete_distribution random number distribution.
4468 * @returns The output stream with the state of @p __x inserted or in
4471 template<typename _IntType1, typename _CharT, typename _Traits>
4472 friend std::basic_ostream<_CharT, _Traits>&
4473 operator<<(std::basic_ostream<_CharT, _Traits>&,
4474 const std::discrete_distribution<_IntType1>&);
4477 * @brief Extracts a %discrete_distribution random number distribution
4478 * @p __x from the input stream @p __is.
4480 * @param __is An input stream.
4481 * @param __x A %discrete_distribution random number
4484 * @returns The input stream with @p __x extracted or in an error
4487 template<typename _IntType1, typename _CharT, typename _Traits>
4488 friend std::basic_istream<_CharT, _Traits>&
4489 operator>>(std::basic_istream<_CharT, _Traits>&,
4490 std::discrete_distribution<_IntType1>&);
4493 param_type _M_param;
4499 template<typename _IntType>
4501 operator==(const std::discrete_distribution<_IntType>& __d1,
4502 const std::discrete_distribution<_IntType>& __d2)
4503 { return __d1.param() == __d2.param(); }
4507 * @brief A piecewise_constant_distribution random number distribution.
4509 * The formula for the piecewise constant probability mass function is
4512 template<typename _RealType = double>
4513 class piecewise_constant_distribution
4516 /** The type of the range of the distribution. */
4517 typedef _RealType result_type;
4518 /** Parameter type. */
4521 typedef piecewise_constant_distribution<_RealType> distribution_type;
4522 friend class piecewise_constant_distribution<_RealType>;
4526 template<typename _InputIteratorB, typename _InputIteratorW>
4527 param_type(_InputIteratorB __bfirst,
4528 _InputIteratorB __bend,
4529 _InputIteratorW __wbegin);
4531 template<typename _Func>
4532 param_type(initializer_list<_RealType> __bil, _Func __fw);
4534 template<typename _Func>
4535 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4538 std::vector<_RealType>
4547 operator==(const param_type& __p1, const param_type& __p2)
4548 { return ((__p1._M_int == __p2._M_int)
4549 && (__p1._M_den == __p2._M_den)); }
4555 std::vector<_RealType> _M_int;
4556 std::vector<double> _M_den;
4557 std::vector<double> _M_cp;
4561 piecewise_constant_distribution()
4565 template<typename _InputIteratorB, typename _InputIteratorW>
4566 piecewise_constant_distribution(_InputIteratorB __bfirst,
4567 _InputIteratorB __bend,
4568 _InputIteratorW __wbegin)
4569 : _M_param(__bfirst, __bend, __wbegin)
4572 template<typename _Func>
4573 piecewise_constant_distribution(initializer_list<_RealType> __bil,
4575 : _M_param(__bil, __fw)
4578 template<typename _Func>
4579 piecewise_constant_distribution(size_t __nw,
4580 _RealType __xmin, _RealType __xmax,
4582 : _M_param(__nw, __xmin, __xmax, __fw)
4586 piecewise_constant_distribution(const param_type& __p)
4591 * @brief Resets the distribution state.
4598 * @brief Returns a vector of the intervals.
4600 std::vector<_RealType>
4602 { return _M_param.intervals(); }
4605 * @brief Returns a vector of the probability densities.
4609 { return _M_param.densities(); }
4612 * @brief Returns the parameter set of the distribution.
4616 { return _M_param; }
4619 * @brief Sets the parameter set of the distribution.
4620 * @param __param The new parameter set of the distribution.
4623 param(const param_type& __param)
4624 { _M_param = __param; }
4627 * @brief Returns the greatest lower bound value of the distribution.
4631 { return this->_M_param._M_int.front(); }
4634 * @brief Returns the least upper bound value of the distribution.
4638 { return this->_M_param._M_int.back(); }
4640 template<typename _UniformRandomNumberGenerator>
4642 operator()(_UniformRandomNumberGenerator& __urng)
4643 { return this->operator()(__urng, this->param()); }
4645 template<typename _UniformRandomNumberGenerator>
4647 operator()(_UniformRandomNumberGenerator& __urng,
4648 const param_type& __p);
4651 * @brief Inserts a %piecewise_constan_distribution random
4652 * number distribution @p __x into the output stream @p __os.
4654 * @param __os An output stream.
4655 * @param __x A %piecewise_constan_distribution random number
4658 * @returns The output stream with the state of @p __x inserted or in
4661 template<typename _RealType1, typename _CharT, typename _Traits>
4662 friend std::basic_ostream<_CharT, _Traits>&
4663 operator<<(std::basic_ostream<_CharT, _Traits>&,
4664 const std::piecewise_constant_distribution<_RealType1>&);
4667 * @brief Extracts a %piecewise_constan_distribution random
4668 * number distribution @p __x from the input stream @p __is.
4670 * @param __is An input stream.
4671 * @param __x A %piecewise_constan_distribution random number
4674 * @returns The input stream with @p __x extracted or in an error
4677 template<typename _RealType1, typename _CharT, typename _Traits>
4678 friend std::basic_istream<_CharT, _Traits>&
4679 operator>>(std::basic_istream<_CharT, _Traits>&,
4680 std::piecewise_constant_distribution<_RealType1>&);
4683 param_type _M_param;
4689 template<typename _RealType>
4691 operator==(const std::piecewise_constant_distribution<_RealType>& __d1,
4692 const std::piecewise_constant_distribution<_RealType>& __d2)
4693 { return __d1.param() == __d2.param(); }
4697 * @brief A piecewise_linear_distribution random number distribution.
4699 * The formula for the piecewise linear probability mass function is
4702 template<typename _RealType = double>
4703 class piecewise_linear_distribution
4706 /** The type of the range of the distribution. */
4707 typedef _RealType result_type;
4708 /** Parameter type. */
4711 typedef piecewise_linear_distribution<_RealType> distribution_type;
4712 friend class piecewise_linear_distribution<_RealType>;
4716 template<typename _InputIteratorB, typename _InputIteratorW>
4717 param_type(_InputIteratorB __bfirst,
4718 _InputIteratorB __bend,
4719 _InputIteratorW __wbegin);
4721 template<typename _Func>
4722 param_type(initializer_list<_RealType> __bil, _Func __fw);
4724 template<typename _Func>
4725 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4728 std::vector<_RealType>
4737 operator==(const param_type& __p1, const param_type& __p2)
4738 { return ((__p1._M_int == __p2._M_int)
4739 && (__p1._M_den == __p2._M_den)); }
4745 std::vector<_RealType> _M_int;
4746 std::vector<double> _M_den;
4747 std::vector<double> _M_cp;
4748 std::vector<double> _M_m;
4752 piecewise_linear_distribution()
4756 template<typename _InputIteratorB, typename _InputIteratorW>
4757 piecewise_linear_distribution(_InputIteratorB __bfirst,
4758 _InputIteratorB __bend,
4759 _InputIteratorW __wbegin)
4760 : _M_param(__bfirst, __bend, __wbegin)
4763 template<typename _Func>
4764 piecewise_linear_distribution(initializer_list<_RealType> __bil,
4766 : _M_param(__bil, __fw)
4769 template<typename _Func>
4770 piecewise_linear_distribution(size_t __nw,
4771 _RealType __xmin, _RealType __xmax,
4773 : _M_param(__nw, __xmin, __xmax, __fw)
4777 piecewise_linear_distribution(const param_type& __p)
4782 * Resets the distribution state.
4789 * @brief Return the intervals of the distribution.
4791 std::vector<_RealType>
4793 { return _M_param.intervals(); }
4796 * @brief Return a vector of the probability densities of the
4801 { return _M_param.densities(); }
4804 * @brief Returns the parameter set of the distribution.
4808 { return _M_param; }
4811 * @brief Sets the parameter set of the distribution.
4812 * @param __param The new parameter set of the distribution.
4815 param(const param_type& __param)
4816 { _M_param = __param; }
4819 * @brief Returns the greatest lower bound value of the distribution.
4823 { return this->_M_param._M_int.front(); }
4826 * @brief Returns the least upper bound value of the distribution.
4830 { return this->_M_param._M_int.back(); }
4832 template<typename _UniformRandomNumberGenerator>
4834 operator()(_UniformRandomNumberGenerator& __urng)
4835 { return this->operator()(__urng, this->param()); }
4837 template<typename _UniformRandomNumberGenerator>
4839 operator()(_UniformRandomNumberGenerator& __urng,
4840 const param_type& __p);
4843 * @brief Inserts a %piecewise_linear_distribution random number
4844 * distribution @p __x into the output stream @p __os.
4846 * @param __os An output stream.
4847 * @param __x A %piecewise_linear_distribution random number
4850 * @returns The output stream with the state of @p __x inserted or in
4853 template<typename _RealType1, typename _CharT, typename _Traits>
4854 friend std::basic_ostream<_CharT, _Traits>&
4855 operator<<(std::basic_ostream<_CharT, _Traits>&,
4856 const std::piecewise_linear_distribution<_RealType1>&);
4859 * @brief Extracts a %piecewise_linear_distribution random number
4860 * distribution @p __x from the input stream @p __is.
4862 * @param __is An input stream.
4863 * @param __x A %piecewise_linear_distribution random number
4866 * @returns The input stream with @p __x extracted or in an error
4869 template<typename _RealType1, typename _CharT, typename _Traits>
4870 friend std::basic_istream<_CharT, _Traits>&
4871 operator>>(std::basic_istream<_CharT, _Traits>&,
4872 std::piecewise_linear_distribution<_RealType1>&);
4875 param_type _M_param;
4881 template<typename _RealType>
4883 operator==(const std::piecewise_linear_distribution<_RealType>& __d1,
4884 const std::piecewise_linear_distribution<_RealType>& __d2)
4885 { return __d1.param() == __d2.param(); }
4887 /* @} */ // group std_random_distributions_poisson
4889 /* @} */ // group std_random_distributions
4892 * @addtogroup std_random_utilities Random Number Utilities
4893 * @ingroup std_random
4898 * @brief The seed_seq class generates sequences of seeds for random
4899 * number generators.
4905 /** The type of the seed vales. */
4906 typedef uint_least32_t result_type;
4908 /** Default constructor. */
4913 template<typename _IntType>
4914 seed_seq(std::initializer_list<_IntType> il);
4916 template<typename _InputIterator>
4917 seed_seq(_InputIterator __begin, _InputIterator __end);
4919 // generating functions
4920 template<typename _RandomAccessIterator>
4922 generate(_RandomAccessIterator __begin, _RandomAccessIterator __end);
4924 // property functions
4926 { return _M_v.size(); }
4928 template<typename OutputIterator>
4930 param(OutputIterator __dest) const
4931 { std::copy(_M_v.begin(), _M_v.end(), __dest); }
4935 std::vector<result_type> _M_v;
4938 /* @} */ // group std_random_utilities
4940 /* @} */ // group std_random