1 // random number generation -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * @file tr1_impl/random
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
38 _GLIBCXX_BEGIN_NAMESPACE_TR1
40 // [5.1] Random number generation
43 * @defgroup tr1_random Random Number Generation
45 * A facility for generating random numbers on selected distributions.
50 * Implementation-space details.
54 template<typename _UIntType, int __w,
55 bool = __w < std::numeric_limits<_UIntType>::digits>
57 { static const _UIntType __value = 0; };
59 template<typename _UIntType, int __w>
60 struct _Shift<_UIntType, __w, true>
61 { static const _UIntType __value = _UIntType(1) << __w; };
63 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
66 // Dispatch based on modulus value to prevent divide-by-zero compile-time
67 // errors when m == 0.
68 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
71 { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
73 typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
74 unsigned, unsigned long>::__type _UInt32Type;
77 * An adaptor class for converting the output of any Generator into
78 * the input for a specific Distribution.
80 template<typename _Engine, typename _Distribution>
83 typedef typename remove_reference<_Engine>::type _BEngine;
84 typedef typename _BEngine::result_type _Engine_result_type;
85 typedef typename _Distribution::input_type result_type;
88 _Adaptor(const _Engine& __g)
94 result_type __return_value;
95 if (is_integral<_Engine_result_type>::value
96 && is_integral<result_type>::value)
97 __return_value = _M_g.min();
99 __return_value = result_type(0);
100 return __return_value;
106 result_type __return_value;
107 if (is_integral<_Engine_result_type>::value
108 && is_integral<result_type>::value)
109 __return_value = _M_g.max();
110 else if (!is_integral<result_type>::value)
111 __return_value = result_type(1);
113 __return_value = std::numeric_limits<result_type>::max() - 1;
114 return __return_value;
118 * Converts a value generated by the adapted random number generator
119 * into a value in the input domain for the dependent random number
122 * Because the type traits are compile time constants only the
123 * appropriate clause of the if statements will actually be emitted
129 result_type __return_value;
130 if (is_integral<_Engine_result_type>::value
131 && is_integral<result_type>::value)
132 __return_value = _M_g();
133 else if (!is_integral<_Engine_result_type>::value
134 && !is_integral<result_type>::value)
135 __return_value = result_type(_M_g() - _M_g.min())
136 / result_type(_M_g.max() - _M_g.min());
137 else if (is_integral<_Engine_result_type>::value
138 && !is_integral<result_type>::value)
139 __return_value = result_type(_M_g() - _M_g.min())
140 / result_type(_M_g.max() - _M_g.min() + result_type(1));
142 __return_value = (((_M_g() - _M_g.min())
143 / (_M_g.max() - _M_g.min()))
144 * std::numeric_limits<result_type>::max());
145 return __return_value;
152 // Specialization for _Engine*.
153 template<typename _Engine, typename _Distribution>
154 struct _Adaptor<_Engine*, _Distribution>
156 typedef typename _Engine::result_type _Engine_result_type;
157 typedef typename _Distribution::input_type result_type;
160 _Adaptor(_Engine* __g)
166 result_type __return_value;
167 if (is_integral<_Engine_result_type>::value
168 && is_integral<result_type>::value)
169 __return_value = _M_g->min();
171 __return_value = result_type(0);
172 return __return_value;
178 result_type __return_value;
179 if (is_integral<_Engine_result_type>::value
180 && is_integral<result_type>::value)
181 __return_value = _M_g->max();
182 else if (!is_integral<result_type>::value)
183 __return_value = result_type(1);
185 __return_value = std::numeric_limits<result_type>::max() - 1;
186 return __return_value;
192 result_type __return_value;
193 if (is_integral<_Engine_result_type>::value
194 && is_integral<result_type>::value)
195 __return_value = (*_M_g)();
196 else if (!is_integral<_Engine_result_type>::value
197 && !is_integral<result_type>::value)
198 __return_value = result_type((*_M_g)() - _M_g->min())
199 / result_type(_M_g->max() - _M_g->min());
200 else if (is_integral<_Engine_result_type>::value
201 && !is_integral<result_type>::value)
202 __return_value = result_type((*_M_g)() - _M_g->min())
203 / result_type(_M_g->max() - _M_g->min() + result_type(1));
205 __return_value = ((((*_M_g)() - _M_g->min())
206 / (_M_g->max() - _M_g->min()))
207 * std::numeric_limits<result_type>::max());
208 return __return_value;
214 } // namespace __detail
217 * Produces random numbers on a given distribution function using a
218 * non-uniform random number generation engine.
220 * @todo the engine_value_type needs to be studied more carefully.
222 template<typename _Engine, typename _Dist>
223 class variate_generator
225 // Concept requirements.
226 __glibcxx_class_requires(_Engine, _CopyConstructibleConcept)
227 // __glibcxx_class_requires(_Engine, _EngineConcept)
228 // __glibcxx_class_requires(_Dist, _EngineConcept)
231 typedef _Engine engine_type;
232 typedef __detail::_Adaptor<_Engine, _Dist> engine_value_type;
233 typedef _Dist distribution_type;
234 typedef typename _Dist::result_type result_type;
236 // tr1:5.1.1 table 5.1 requirement
237 typedef typename __gnu_cxx::__enable_if<
238 is_arithmetic<result_type>::value, result_type>::__type _IsValidType;
241 * Constructs a variate generator with the uniform random number
242 * generator @p __eng for the random distribution @p __dist.
244 * @throws Any exceptions which may thrown by the copy constructors of
245 * the @p _Engine or @p _Dist objects.
247 variate_generator(engine_type __eng, distribution_type __dist)
248 : _M_engine(__eng), _M_dist(__dist) { }
251 * Gets the next generated value on the distribution.
255 { return _M_dist(_M_engine); }
260 template<typename _Tp>
262 operator()(_Tp __value)
263 { return _M_dist(_M_engine, __value); }
266 * Gets a reference to the underlying uniform random number generator
271 { return _M_engine; }
274 * Gets a const reference to the underlying uniform random number
277 const engine_value_type&
279 { return _M_engine; }
282 * Gets a reference to the underlying random distribution.
289 * Gets a const reference to the underlying random distribution.
291 const distribution_type&
296 * Gets the closed lower bound of the distribution interval.
300 { return this->distribution().min(); }
303 * Gets the closed upper bound of the distribution interval.
307 { return this->distribution().max(); }
310 engine_value_type _M_engine;
311 distribution_type _M_dist;
316 * @defgroup tr1_random_generators Random Number Generators
317 * @ingroup tr1_random
319 * These classes define objects which provide random or pseudorandom
320 * numbers, either from a discrete or a continuous interval. The
321 * random number generator supplied as a part of this library are
322 * all uniform random number generators which provide a sequence of
323 * random number uniformly distributed over their range.
325 * A number generator is a function object with an operator() that
326 * takes zero arguments and returns a number.
328 * A compliant random number generator must satisfy the following
329 * requirements. <table border=1 cellpadding=10 cellspacing=0>
330 * <caption align=top>Random Number Generator Requirements</caption>
331 * <tr><td>To be documented.</td></tr> </table>
337 * @brief A model of a linear congruential random number generator.
339 * A random number generator that produces pseudorandom numbers using the
340 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
342 * The template parameter @p _UIntType must be an unsigned integral type
343 * large enough to store values up to (__m-1). If the template parameter
344 * @p __m is 0, the modulus @p __m used is
345 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
346 * parameters @p __a and @p __c must be less than @p __m.
348 * The size of the state is @f$ 1 @f$.
350 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
351 class linear_congruential
353 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
354 // __glibcpp_class_requires(__a < __m && __c < __m)
357 /** The type of the generated random value. */
358 typedef _UIntType result_type;
360 /** The multiplier. */
361 static const _UIntType multiplier = __a;
363 static const _UIntType increment = __c;
365 static const _UIntType modulus = __m;
368 * Constructs a %linear_congruential random number generator engine with
369 * seed @p __s. The default seed value is 1.
371 * @param __s The initial seed value.
374 linear_congruential(unsigned long __x0 = 1)
375 { this->seed(__x0); }
378 * Constructs a %linear_congruential random number generator engine
379 * seeded from the generator function @p __g.
381 * @param __g The seed generator function.
384 linear_congruential(_Gen& __g)
388 * Reseeds the %linear_congruential random number generator engine
389 * sequence to the seed @g __s.
391 * @param __s The new seed.
394 seed(unsigned long __s = 1);
397 * Reseeds the %linear_congruential random number generator engine
398 * sequence using values from the generator function @p __g.
400 * @param __g the seed generator function.
405 { seed(__g, typename is_fundamental<_Gen>::type()); }
408 * Gets the smallest possible value in the output range.
410 * The minimum depends on the @p __c parameter: if it is zero, the
411 * minimum generated must be > 0, otherwise 0 is allowed.
415 { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
418 * Gets the largest possible value in the output range.
425 * Gets the next random number in the sequence.
431 * Compares two linear congruential random number generator
432 * objects of the same type for equality.
434 * @param __lhs A linear congruential random number generator object.
435 * @param __rhs Another linear congruential random number generator obj.
437 * @returns true if the two objects are equal, false otherwise.
440 operator==(const linear_congruential& __lhs,
441 const linear_congruential& __rhs)
442 { return __lhs._M_x == __rhs._M_x; }
445 * Compares two linear congruential random number generator
446 * objects of the same type for inequality.
448 * @param __lhs A linear congruential random number generator object.
449 * @param __rhs Another linear congruential random number generator obj.
451 * @returns true if the two objects are not equal, false otherwise.
454 operator!=(const linear_congruential& __lhs,
455 const linear_congruential& __rhs)
456 { return !(__lhs == __rhs); }
459 * Writes the textual representation of the state x(i) of x to @p __os.
461 * @param __os The output stream.
462 * @param __lcr A % linear_congruential random number generator.
465 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
467 typename _CharT, typename _Traits>
468 friend std::basic_ostream<_CharT, _Traits>&
469 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
470 const linear_congruential<_UIntType1, __a1, __c1,
474 * Sets the state of the engine by reading its textual
475 * representation from @p __is.
477 * The textual representation must have been previously written using an
478 * output stream whose imbued locale and whose type's template
479 * specialization arguments _CharT and _Traits were the same as those of
482 * @param __is The input stream.
483 * @param __lcr A % linear_congruential random number generator.
486 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
488 typename _CharT, typename _Traits>
489 friend std::basic_istream<_CharT, _Traits>&
490 operator>>(std::basic_istream<_CharT, _Traits>& __is,
491 linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
496 seed(_Gen& __g, true_type)
497 { return seed(static_cast<unsigned long>(__g)); }
501 seed(_Gen& __g, false_type);
507 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
509 typedef linear_congruential<unsigned long, 16807, 0, 2147483647> minstd_rand0;
512 * An alternative LCR (Lehmer Generator function) .
514 typedef linear_congruential<unsigned long, 48271, 0, 2147483647> minstd_rand;
518 * A generalized feedback shift register discrete random number generator.
520 * This algorithm avoids multiplication and division and is designed to be
521 * friendly to a pipelined architecture. If the parameters are chosen
522 * correctly, this generator will produce numbers with a very long period and
523 * fairly good apparent entropy, although still not cryptographically strong.
525 * The best way to use this generator is with the predefined mt19937 class.
527 * This algorithm was originally invented by Makoto Matsumoto and
530 * @var word_size The number of bits in each element of the state vector.
531 * @var state_size The degree of recursion.
532 * @var shift_size The period parameter.
533 * @var mask_bits The separation point bit index.
534 * @var parameter_a The last row of the twist matrix.
535 * @var output_u The first right-shift tempering matrix parameter.
536 * @var output_s The first left-shift tempering matrix parameter.
537 * @var output_b The first left-shift tempering matrix mask.
538 * @var output_t The second left-shift tempering matrix parameter.
539 * @var output_c The second left-shift tempering matrix mask.
540 * @var output_l The second right-shift tempering matrix parameter.
542 template<class _UIntType, int __w, int __n, int __m, int __r,
543 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
544 _UIntType __c, int __l>
545 class mersenne_twister
547 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
551 typedef _UIntType result_type;
554 static const int word_size = __w;
555 static const int state_size = __n;
556 static const int shift_size = __m;
557 static const int mask_bits = __r;
558 static const _UIntType parameter_a = __a;
559 static const int output_u = __u;
560 static const int output_s = __s;
561 static const _UIntType output_b = __b;
562 static const int output_t = __t;
563 static const _UIntType output_c = __c;
564 static const int output_l = __l;
566 // constructors and member function
571 mersenne_twister(unsigned long __value)
575 mersenne_twister(_Gen& __g)
583 seed(unsigned long __value);
588 { seed(__g, typename is_fundamental<_Gen>::type()); }
596 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
602 * Compares two % mersenne_twister random number generator objects of
603 * the same type for equality.
605 * @param __lhs A % mersenne_twister random number generator object.
606 * @param __rhs Another % mersenne_twister random number generator
609 * @returns true if the two objects are equal, false otherwise.
612 operator==(const mersenne_twister& __lhs,
613 const mersenne_twister& __rhs)
614 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
617 * Compares two % mersenne_twister random number generator objects of
618 * the same type for inequality.
620 * @param __lhs A % mersenne_twister random number generator object.
621 * @param __rhs Another % mersenne_twister random number generator
624 * @returns true if the two objects are not equal, false otherwise.
627 operator!=(const mersenne_twister& __lhs,
628 const mersenne_twister& __rhs)
629 { return !(__lhs == __rhs); }
632 * Inserts the current state of a % mersenne_twister random number
633 * generator engine @p __x into the output stream @p __os.
635 * @param __os An output stream.
636 * @param __x A % mersenne_twister random number generator engine.
638 * @returns The output stream with the state of @p __x inserted or in
641 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
642 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
643 _UIntType1 __c1, int __l1,
644 typename _CharT, typename _Traits>
645 friend std::basic_ostream<_CharT, _Traits>&
646 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
647 const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
648 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
651 * Extracts the current state of a % mersenne_twister random number
652 * generator engine @p __x from the input stream @p __is.
654 * @param __is An input stream.
655 * @param __x A % mersenne_twister random number generator engine.
657 * @returns The input stream with the state of @p __x extracted or in
660 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
661 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
662 _UIntType1 __c1, int __l1,
663 typename _CharT, typename _Traits>
664 friend std::basic_istream<_CharT, _Traits>&
665 operator>>(std::basic_istream<_CharT, _Traits>& __is,
666 mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
667 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
672 seed(_Gen& __g, true_type)
673 { return seed(static_cast<unsigned long>(__g)); }
677 seed(_Gen& __g, false_type);
679 _UIntType _M_x[state_size];
684 * The classic Mersenne Twister.
687 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
688 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
689 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
691 typedef mersenne_twister<
692 unsigned long, 32, 624, 397, 31,
700 * @brief The Marsaglia-Zaman generator.
702 * This is a model of a Generalized Fibonacci discrete random number
703 * generator, sometimes referred to as the SWC generator.
705 * A discrete random number generator that produces pseudorandom
706 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
707 * carry_{i-1}) \bmod m @f$.
709 * The size of the state is @f$ r @f$
710 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
712 * N1688[4.13] says "the template parameter _IntType shall denote an integral
713 * type large enough to store values up to m."
715 * @var _M_x The state of the generator. This is a ring buffer.
716 * @var _M_carry The carry.
717 * @var _M_p Current index of x(i - r).
719 template<typename _IntType, _IntType __m, int __s, int __r>
720 class subtract_with_carry
722 __glibcxx_class_requires(_IntType, _IntegerConcept)
725 /** The type of the generated random value. */
726 typedef _IntType result_type;
729 static const _IntType modulus = __m;
730 static const int long_lag = __r;
731 static const int short_lag = __s;
734 * Constructs a default-initialized % subtract_with_carry random number
737 subtract_with_carry()
741 * Constructs an explicitly seeded % subtract_with_carry random number
745 subtract_with_carry(unsigned long __value)
746 { this->seed(__value); }
749 * Constructs a %subtract_with_carry random number generator engine
750 * seeded from the generator function @p __g.
752 * @param __g The seed generator function.
755 subtract_with_carry(_Gen& __g)
759 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
761 * N1688[4.19] modifies this as follows. If @p __value == 0,
762 * sets value to 19780503. In any case, with a linear
763 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
764 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
765 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
766 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
767 * set carry to 1, otherwise sets carry to 0.
770 seed(unsigned long __value = 19780503);
773 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
774 * random number generator.
779 { seed(__g, typename is_fundamental<_Gen>::type()); }
782 * Gets the inclusive minimum value of the range of random integers
783 * returned by this generator.
790 * Gets the inclusive maximum value of the range of random integers
791 * returned by this generator.
795 { return this->modulus - 1; }
798 * Gets the next random number in the sequence.
804 * Compares two % subtract_with_carry random number generator objects of
805 * the same type for equality.
807 * @param __lhs A % subtract_with_carry random number generator object.
808 * @param __rhs Another % subtract_with_carry random number generator
811 * @returns true if the two objects are equal, false otherwise.
814 operator==(const subtract_with_carry& __lhs,
815 const subtract_with_carry& __rhs)
816 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
819 * Compares two % subtract_with_carry random number generator objects of
820 * the same type for inequality.
822 * @param __lhs A % subtract_with_carry random number generator object.
823 * @param __rhs Another % subtract_with_carry random number generator
826 * @returns true if the two objects are not equal, false otherwise.
829 operator!=(const subtract_with_carry& __lhs,
830 const subtract_with_carry& __rhs)
831 { return !(__lhs == __rhs); }
834 * Inserts the current state of a % subtract_with_carry random number
835 * generator engine @p __x into the output stream @p __os.
837 * @param __os An output stream.
838 * @param __x A % subtract_with_carry random number generator engine.
840 * @returns The output stream with the state of @p __x inserted or in
843 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
844 typename _CharT, typename _Traits>
845 friend std::basic_ostream<_CharT, _Traits>&
846 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
847 const subtract_with_carry<_IntType1, __m1, __s1,
851 * Extracts the current state of a % subtract_with_carry random number
852 * generator engine @p __x from the input stream @p __is.
854 * @param __is An input stream.
855 * @param __x A % subtract_with_carry random number generator engine.
857 * @returns The input stream with the state of @p __x extracted or in
860 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
861 typename _CharT, typename _Traits>
862 friend std::basic_istream<_CharT, _Traits>&
863 operator>>(std::basic_istream<_CharT, _Traits>& __is,
864 subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
869 seed(_Gen& __g, true_type)
870 { return seed(static_cast<unsigned long>(__g)); }
874 seed(_Gen& __g, false_type);
876 typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
878 _UIntType _M_x[long_lag];
885 * @brief The Marsaglia-Zaman generator (floats version).
887 * @var _M_x The state of the generator. This is a ring buffer.
888 * @var _M_carry The carry.
889 * @var _M_p Current index of x(i - r).
890 * @var _M_npows Precomputed negative powers of 2.
892 template<typename _RealType, int __w, int __s, int __r>
893 class subtract_with_carry_01
896 /** The type of the generated random value. */
897 typedef _RealType result_type;
900 static const int word_size = __w;
901 static const int long_lag = __r;
902 static const int short_lag = __s;
905 * Constructs a default-initialized % subtract_with_carry_01 random
908 subtract_with_carry_01()
911 _M_initialize_npows();
915 * Constructs an explicitly seeded % subtract_with_carry_01 random number
919 subtract_with_carry_01(unsigned long __value)
922 _M_initialize_npows();
926 * Constructs a % subtract_with_carry_01 random number generator engine
927 * seeded from the generator function @p __g.
929 * @param __g The seed generator function.
932 subtract_with_carry_01(_Gen& __g)
935 _M_initialize_npows();
939 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
942 seed(unsigned long __value = 19780503);
945 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
946 * random number generator.
951 { seed(__g, typename is_fundamental<_Gen>::type()); }
954 * Gets the minimum value of the range of random floats
955 * returned by this generator.
962 * Gets the maximum value of the range of random floats
963 * returned by this generator.
970 * Gets the next random number in the sequence.
976 * Compares two % subtract_with_carry_01 random number generator objects
977 * of the same type for equality.
979 * @param __lhs A % subtract_with_carry_01 random number
981 * @param __rhs Another % subtract_with_carry_01 random number generator
984 * @returns true if the two objects are equal, false otherwise.
987 operator==(const subtract_with_carry_01& __lhs,
988 const subtract_with_carry_01& __rhs)
990 for (int __i = 0; __i < long_lag; ++__i)
991 if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
998 * Compares two % subtract_with_carry_01 random number generator objects
999 * of the same type for inequality.
1001 * @param __lhs A % subtract_with_carry_01 random number
1004 * @param __rhs Another % subtract_with_carry_01 random number generator
1007 * @returns true if the two objects are not equal, false otherwise.
1010 operator!=(const subtract_with_carry_01& __lhs,
1011 const subtract_with_carry_01& __rhs)
1012 { return !(__lhs == __rhs); }
1015 * Inserts the current state of a % subtract_with_carry_01 random number
1016 * generator engine @p __x into the output stream @p __os.
1018 * @param __os An output stream.
1019 * @param __x A % subtract_with_carry_01 random number generator engine.
1021 * @returns The output stream with the state of @p __x inserted or in
1024 template<typename _RealType1, int __w1, int __s1, int __r1,
1025 typename _CharT, typename _Traits>
1026 friend std::basic_ostream<_CharT, _Traits>&
1027 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1028 const subtract_with_carry_01<_RealType1, __w1, __s1,
1032 * Extracts the current state of a % subtract_with_carry_01 random number
1033 * generator engine @p __x from the input stream @p __is.
1035 * @param __is An input stream.
1036 * @param __x A % subtract_with_carry_01 random number generator engine.
1038 * @returns The input stream with the state of @p __x extracted or in
1041 template<typename _RealType1, int __w1, int __s1, int __r1,
1042 typename _CharT, typename _Traits>
1043 friend std::basic_istream<_CharT, _Traits>&
1044 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1045 subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
1048 template<class _Gen>
1050 seed(_Gen& __g, true_type)
1051 { return seed(static_cast<unsigned long>(__g)); }
1053 template<class _Gen>
1055 seed(_Gen& __g, false_type);
1058 _M_initialize_npows();
1060 static const int __n = (__w + 31) / 32;
1062 typedef __detail::_UInt32Type _UInt32Type;
1063 _UInt32Type _M_x[long_lag][__n];
1064 _RealType _M_npows[__n];
1065 _UInt32Type _M_carry;
1069 typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
1071 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1072 // 508. Bad parameters for ranlux64_base_01.
1073 typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
1077 * Produces random numbers from some base engine by discarding blocks of
1080 * 0 <= @p __r <= @p __p
1082 template<class _UniformRandomNumberGenerator, int __p, int __r>
1085 // __glibcxx_class_requires(typename base_type::result_type,
1086 // ArithmeticTypeConcept)
1089 /** The type of the underlying generator engine. */
1090 typedef _UniformRandomNumberGenerator base_type;
1091 /** The type of the generated random value. */
1092 typedef typename base_type::result_type result_type;
1095 static const int block_size = __p;
1096 static const int used_block = __r;
1099 * Constructs a default %discard_block engine.
1101 * The underlying engine is default constructed as well.
1107 * Copy constructs a %discard_block engine.
1109 * Copies an existing base class random number generator.
1110 * @param rng An existing (base class) engine object.
1113 discard_block(const base_type& __rng)
1114 : _M_b(__rng), _M_n(0) { }
1117 * Seed constructs a %discard_block engine.
1119 * Constructs the underlying generator engine seeded with @p __s.
1120 * @param __s A seed value for the base class engine.
1123 discard_block(unsigned long __s)
1124 : _M_b(__s), _M_n(0) { }
1127 * Generator construct a %discard_block engine.
1129 * @param __g A seed generator function.
1131 template<class _Gen>
1132 discard_block(_Gen& __g)
1133 : _M_b(__g), _M_n(0) { }
1136 * Reseeds the %discard_block object with the default seed for the
1137 * underlying base class generator engine.
1146 * Reseeds the %discard_block object with the given seed generator
1148 * @param __g A seed generator function.
1150 template<class _Gen>
1151 void seed(_Gen& __g)
1158 * Gets a const reference to the underlying generator engine object.
1165 * Gets the minimum value in the generated random number range.
1169 { return _M_b.min(); }
1172 * Gets the maximum value in the generated random number range.
1176 { return _M_b.max(); }
1179 * Gets the next value in the generated random number sequence.
1185 * Compares two %discard_block random number generator objects of
1186 * the same type for equality.
1188 * @param __lhs A %discard_block random number generator object.
1189 * @param __rhs Another %discard_block random number generator
1192 * @returns true if the two objects are equal, false otherwise.
1195 operator==(const discard_block& __lhs, const discard_block& __rhs)
1196 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
1199 * Compares two %discard_block random number generator objects of
1200 * the same type for inequality.
1202 * @param __lhs A %discard_block random number generator object.
1203 * @param __rhs Another %discard_block random number generator
1206 * @returns true if the two objects are not equal, false otherwise.
1209 operator!=(const discard_block& __lhs, const discard_block& __rhs)
1210 { return !(__lhs == __rhs); }
1213 * Inserts the current state of a %discard_block random number
1214 * generator engine @p __x into the output stream @p __os.
1216 * @param __os An output stream.
1217 * @param __x A %discard_block random number generator engine.
1219 * @returns The output stream with the state of @p __x inserted or in
1222 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1223 typename _CharT, typename _Traits>
1224 friend std::basic_ostream<_CharT, _Traits>&
1225 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1226 const discard_block<_UniformRandomNumberGenerator1,
1230 * Extracts the current state of a % subtract_with_carry random number
1231 * generator engine @p __x from the input stream @p __is.
1233 * @param __is An input stream.
1234 * @param __x A %discard_block random number generator engine.
1236 * @returns The input stream with the state of @p __x extracted or in
1239 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1240 typename _CharT, typename _Traits>
1241 friend std::basic_istream<_CharT, _Traits>&
1242 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1243 discard_block<_UniformRandomNumberGenerator1,
1253 * James's luxury-level-3 integer adaptation of Luescher's generator.
1255 typedef discard_block<
1256 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1262 * James's luxury-level-4 integer adaptation of Luescher's generator.
1264 typedef discard_block<
1265 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1270 typedef discard_block<
1271 subtract_with_carry_01<float, 24, 10, 24>,
1276 typedef discard_block<
1277 subtract_with_carry_01<float, 24, 10, 24>,
1284 * A random number generator adaptor class that combines two random number
1285 * generator engines into a single output sequence.
1287 template<class _UniformRandomNumberGenerator1, int __s1,
1288 class _UniformRandomNumberGenerator2, int __s2>
1291 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1292 // result_type, ArithmeticTypeConcept)
1293 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1294 // result_type, ArithmeticTypeConcept)
1297 /** The type of the first underlying generator engine. */
1298 typedef _UniformRandomNumberGenerator1 base1_type;
1299 /** The type of the second underlying generator engine. */
1300 typedef _UniformRandomNumberGenerator2 base2_type;
1303 typedef typename base1_type::result_type _Result_type1;
1304 typedef typename base2_type::result_type _Result_type2;
1307 /** The type of the generated random value. */
1308 typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1)
1309 > sizeof(_Result_type2)),
1310 _Result_type1, _Result_type2>::__type result_type;
1313 static const int shift1 = __s1;
1314 static const int shift2 = __s2;
1316 // constructors and member function
1319 { _M_initialize_max(); }
1321 xor_combine(const base1_type& __rng1, const base2_type& __rng2)
1322 : _M_b1(__rng1), _M_b2(__rng2)
1323 { _M_initialize_max(); }
1325 xor_combine(unsigned long __s)
1326 : _M_b1(__s), _M_b2(__s + 1)
1327 { _M_initialize_max(); }
1329 template<class _Gen>
1330 xor_combine(_Gen& __g)
1331 : _M_b1(__g), _M_b2(__g)
1332 { _M_initialize_max(); }
1341 template<class _Gen>
1366 * Gets the next random number in the sequence.
1368 // NB: Not exactly the TR1 formula, per N2079 instead.
1372 return ((result_type(_M_b1() - _M_b1.min()) << shift1)
1373 ^ (result_type(_M_b2() - _M_b2.min()) << shift2));
1377 * Compares two %xor_combine random number generator objects of
1378 * the same type for equality.
1380 * @param __lhs A %xor_combine random number generator object.
1381 * @param __rhs Another %xor_combine random number generator
1384 * @returns true if the two objects are equal, false otherwise.
1387 operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1389 return (__lhs.base1() == __rhs.base1())
1390 && (__lhs.base2() == __rhs.base2());
1394 * Compares two %xor_combine random number generator objects of
1395 * the same type for inequality.
1397 * @param __lhs A %xor_combine random number generator object.
1398 * @param __rhs Another %xor_combine random number generator
1401 * @returns true if the two objects are not equal, false otherwise.
1404 operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1405 { return !(__lhs == __rhs); }
1408 * Inserts the current state of a %xor_combine random number
1409 * generator engine @p __x into the output stream @p __os.
1411 * @param __os An output stream.
1412 * @param __x A %xor_combine random number generator engine.
1414 * @returns The output stream with the state of @p __x inserted or in
1417 template<class _UniformRandomNumberGenerator11, int __s11,
1418 class _UniformRandomNumberGenerator21, int __s21,
1419 typename _CharT, typename _Traits>
1420 friend std::basic_ostream<_CharT, _Traits>&
1421 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1422 const xor_combine<_UniformRandomNumberGenerator11, __s11,
1423 _UniformRandomNumberGenerator21, __s21>& __x);
1426 * Extracts the current state of a %xor_combine random number
1427 * generator engine @p __x from the input stream @p __is.
1429 * @param __is An input stream.
1430 * @param __x A %xor_combine random number generator engine.
1432 * @returns The input stream with the state of @p __x extracted or in
1435 template<class _UniformRandomNumberGenerator11, int __s11,
1436 class _UniformRandomNumberGenerator21, int __s21,
1437 typename _CharT, typename _Traits>
1438 friend std::basic_istream<_CharT, _Traits>&
1439 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1440 xor_combine<_UniformRandomNumberGenerator11, __s11,
1441 _UniformRandomNumberGenerator21, __s21>& __x);
1445 _M_initialize_max();
1448 _M_initialize_max_aux(result_type, result_type, int);
1457 * A standard interface to a platform-specific non-deterministic
1458 * random number generator (if any are available).
1464 typedef unsigned int result_type;
1466 // constructors, destructors and member functions
1468 #ifdef _GLIBCXX_USE_RANDOM_TR1
1471 random_device(const std::string& __token = "/dev/urandom")
1473 if ((__token != "/dev/urandom" && __token != "/dev/random")
1474 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1475 std::__throw_runtime_error(__N("random_device::"
1476 "random_device(const std::string&)"));
1480 { std::fclose(_M_file); }
1485 random_device(const std::string& __token = "mt19937")
1486 : _M_mt(_M_strtoul(__token)) { }
1489 static unsigned long
1490 _M_strtoul(const std::string& __str)
1492 unsigned long __ret = 5489UL;
1493 if (__str != "mt19937")
1495 const char* __nptr = __str.c_str();
1497 __ret = std::strtoul(__nptr, &__endptr, 0);
1498 if (*__nptr == '\0' || *__endptr != '\0')
1499 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1500 "(const std::string&)"));
1511 { return std::numeric_limits<result_type>::min(); }
1515 { return std::numeric_limits<result_type>::max(); }
1524 #ifdef _GLIBCXX_USE_RANDOM_TR1
1526 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1535 random_device(const random_device&);
1536 void operator=(const random_device&);
1538 #ifdef _GLIBCXX_USE_RANDOM_TR1
1545 /* @} */ // group tr1_random_generators
1548 * @defgroup tr1_random_distributions Random Number Distributions
1549 * @ingroup tr1_random
1554 * @defgroup tr1_random_distributions_discrete Discrete Distributions
1555 * @ingroup tr1_random_distributions
1560 * @brief Uniform discrete distribution for random numbers.
1561 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1562 * probability throughout the range.
1564 template<typename _IntType = int>
1567 __glibcxx_class_requires(_IntType, _IntegerConcept)
1570 /** The type of the parameters of the distribution. */
1571 typedef _IntType input_type;
1572 /** The type of the range of the distribution. */
1573 typedef _IntType result_type;
1577 * Constructs a uniform distribution object.
1580 uniform_int(_IntType __min = 0, _IntType __max = 9)
1581 : _M_min(__min), _M_max(__max)
1583 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1587 * Gets the inclusive lower bound of the distribution range.
1594 * Gets the inclusive upper bound of the distribution range.
1601 * Resets the distribution state.
1603 * Does nothing for the uniform integer distribution.
1609 * Gets a uniformly distributed random number in the range
1612 template<typename _UniformRandomNumberGenerator>
1614 operator()(_UniformRandomNumberGenerator& __urng)
1616 typedef typename _UniformRandomNumberGenerator::result_type
1618 return _M_call(__urng, _M_min, _M_max,
1619 typename is_integral<_UResult_type>::type());
1623 * Gets a uniform random number in the range @f$[0, n)@f$.
1625 * This function is aimed at use with std::random_shuffle.
1627 template<typename _UniformRandomNumberGenerator>
1629 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1631 typedef typename _UniformRandomNumberGenerator::result_type
1633 return _M_call(__urng, 0, __n - 1,
1634 typename is_integral<_UResult_type>::type());
1638 * Inserts a %uniform_int random number distribution @p __x into the
1639 * output stream @p os.
1641 * @param __os An output stream.
1642 * @param __x A %uniform_int random number distribution.
1644 * @returns The output stream with the state of @p __x inserted or in
1647 template<typename _IntType1, typename _CharT, typename _Traits>
1648 friend std::basic_ostream<_CharT, _Traits>&
1649 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1650 const uniform_int<_IntType1>& __x);
1653 * Extracts a %uniform_int random number distribution
1654 * @p __x from the input stream @p __is.
1656 * @param __is An input stream.
1657 * @param __x A %uniform_int random number generator engine.
1659 * @returns The input stream with @p __x extracted or in an error state.
1661 template<typename _IntType1, typename _CharT, typename _Traits>
1662 friend std::basic_istream<_CharT, _Traits>&
1663 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1664 uniform_int<_IntType1>& __x);
1667 template<typename _UniformRandomNumberGenerator>
1669 _M_call(_UniformRandomNumberGenerator& __urng,
1670 result_type __min, result_type __max, true_type);
1672 template<typename _UniformRandomNumberGenerator>
1674 _M_call(_UniformRandomNumberGenerator& __urng,
1675 result_type __min, result_type __max, false_type)
1677 return result_type((__urng() - __urng.min())
1678 / (__urng.max() - __urng.min())
1679 * (__max - __min + 1)) + __min;
1688 * @brief A Bernoulli random number distribution.
1690 * Generates a sequence of true and false values with likelihood @f$ p @f$
1691 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1693 class bernoulli_distribution
1696 typedef int input_type;
1697 typedef bool result_type;
1701 * Constructs a Bernoulli distribution with likelihood @p p.
1703 * @param __p [IN] The likelihood of a true result being returned. Must
1704 * be in the interval @f$ [0, 1] @f$.
1707 bernoulli_distribution(double __p = 0.5)
1710 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1714 * Gets the @p p parameter of the distribution.
1721 * Resets the distribution state.
1723 * Does nothing for a Bernoulli distribution.
1729 * Gets the next value in the Bernoullian sequence.
1731 template<class _UniformRandomNumberGenerator>
1733 operator()(_UniformRandomNumberGenerator& __urng)
1735 if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
1741 * Inserts a %bernoulli_distribution random number distribution
1742 * @p __x into the output stream @p __os.
1744 * @param __os An output stream.
1745 * @param __x A %bernoulli_distribution random number distribution.
1747 * @returns The output stream with the state of @p __x inserted or in
1750 template<typename _CharT, typename _Traits>
1751 friend std::basic_ostream<_CharT, _Traits>&
1752 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1753 const bernoulli_distribution& __x);
1756 * Extracts a %bernoulli_distribution random number distribution
1757 * @p __x from the input stream @p __is.
1759 * @param __is An input stream.
1760 * @param __x A %bernoulli_distribution random number generator engine.
1762 * @returns The input stream with @p __x extracted or in an error state.
1764 template<typename _CharT, typename _Traits>
1765 friend std::basic_istream<_CharT, _Traits>&
1766 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1767 bernoulli_distribution& __x)
1768 { return __is >> __x._M_p; }
1776 * @brief A discrete geometric random number distribution.
1778 * The formula for the geometric probability mass function is
1779 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1782 template<typename _IntType = int, typename _RealType = double>
1783 class geometric_distribution
1787 typedef _RealType input_type;
1788 typedef _IntType result_type;
1790 // constructors and member function
1792 geometric_distribution(const _RealType& __p = _RealType(0.5))
1795 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
1800 * Gets the distribution parameter @p p.
1809 template<class _UniformRandomNumberGenerator>
1811 operator()(_UniformRandomNumberGenerator& __urng);
1814 * Inserts a %geometric_distribution random number distribution
1815 * @p __x into the output stream @p __os.
1817 * @param __os An output stream.
1818 * @param __x A %geometric_distribution random number distribution.
1820 * @returns The output stream with the state of @p __x inserted or in
1823 template<typename _IntType1, typename _RealType1,
1824 typename _CharT, typename _Traits>
1825 friend std::basic_ostream<_CharT, _Traits>&
1826 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1827 const geometric_distribution<_IntType1, _RealType1>& __x);
1830 * Extracts a %geometric_distribution random number distribution
1831 * @p __x from the input stream @p __is.
1833 * @param __is An input stream.
1834 * @param __x A %geometric_distribution random number generator engine.
1836 * @returns The input stream with @p __x extracted or in an error state.
1838 template<typename _CharT, typename _Traits>
1839 friend std::basic_istream<_CharT, _Traits>&
1840 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1841 geometric_distribution& __x)
1844 __x._M_initialize();
1851 { _M_log_p = std::log(_M_p); }
1858 template<typename _RealType>
1859 class normal_distribution;
1862 * @brief A discrete Poisson random number distribution.
1864 * The formula for the Poisson probability mass function is
1865 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1866 * parameter of the distribution.
1868 template<typename _IntType = int, typename _RealType = double>
1869 class poisson_distribution
1873 typedef _RealType input_type;
1874 typedef _IntType result_type;
1876 // constructors and member function
1878 poisson_distribution(const _RealType& __mean = _RealType(1))
1879 : _M_mean(__mean), _M_nd()
1881 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
1886 * Gets the distribution parameter @p mean.
1896 template<class _UniformRandomNumberGenerator>
1898 operator()(_UniformRandomNumberGenerator& __urng);
1901 * Inserts a %poisson_distribution random number distribution
1902 * @p __x into the output stream @p __os.
1904 * @param __os An output stream.
1905 * @param __x A %poisson_distribution random number distribution.
1907 * @returns The output stream with the state of @p __x inserted or in
1910 template<typename _IntType1, typename _RealType1,
1911 typename _CharT, typename _Traits>
1912 friend std::basic_ostream<_CharT, _Traits>&
1913 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1914 const poisson_distribution<_IntType1, _RealType1>& __x);
1917 * Extracts a %poisson_distribution random number distribution
1918 * @p __x from the input stream @p __is.
1920 * @param __is An input stream.
1921 * @param __x A %poisson_distribution random number generator engine.
1923 * @returns The input stream with @p __x extracted or in an error state.
1925 template<typename _IntType1, typename _RealType1,
1926 typename _CharT, typename _Traits>
1927 friend std::basic_istream<_CharT, _Traits>&
1928 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1929 poisson_distribution<_IntType1, _RealType1>& __x);
1935 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1936 normal_distribution<_RealType> _M_nd;
1940 // Hosts either log(mean) or the threshold of the simple method.
1941 _RealType _M_lm_thr;
1942 #if _GLIBCXX_USE_C99_MATH_TR1
1943 _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
1949 * @brief A discrete binomial random number distribution.
1951 * The formula for the binomial probability mass function is
1952 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1953 * and @f$ p @f$ are the parameters of the distribution.
1955 template<typename _IntType = int, typename _RealType = double>
1956 class binomial_distribution
1960 typedef _RealType input_type;
1961 typedef _IntType result_type;
1963 // constructors and member function
1965 binomial_distribution(_IntType __t = 1,
1966 const _RealType& __p = _RealType(0.5))
1967 : _M_t(__t), _M_p(__p), _M_nd()
1969 _GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
1974 * Gets the distribution @p t parameter.
1981 * Gets the distribution @p p parameter.
1991 template<class _UniformRandomNumberGenerator>
1993 operator()(_UniformRandomNumberGenerator& __urng);
1996 * Inserts a %binomial_distribution random number distribution
1997 * @p __x into the output stream @p __os.
1999 * @param __os An output stream.
2000 * @param __x A %binomial_distribution random number distribution.
2002 * @returns The output stream with the state of @p __x inserted or in
2005 template<typename _IntType1, typename _RealType1,
2006 typename _CharT, typename _Traits>
2007 friend std::basic_ostream<_CharT, _Traits>&
2008 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2009 const binomial_distribution<_IntType1, _RealType1>& __x);
2012 * Extracts a %binomial_distribution random number distribution
2013 * @p __x from the input stream @p __is.
2015 * @param __is An input stream.
2016 * @param __x A %binomial_distribution random number generator engine.
2018 * @returns The input stream with @p __x extracted or in an error state.
2020 template<typename _IntType1, typename _RealType1,
2021 typename _CharT, typename _Traits>
2022 friend std::basic_istream<_CharT, _Traits>&
2023 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2024 binomial_distribution<_IntType1, _RealType1>& __x);
2030 template<class _UniformRandomNumberGenerator>
2032 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
2034 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
2035 normal_distribution<_RealType> _M_nd;
2038 #if _GLIBCXX_USE_C99_MATH_TR1
2039 _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
2040 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
2048 /* @} */ // group tr1_random_distributions_discrete
2051 * @defgroup tr1_random_distributions_continuous Continuous Distributions
2052 * @ingroup tr1_random_distributions
2057 * @brief Uniform continuous distribution for random numbers.
2059 * A continuous random distribution on the range [min, max) with equal
2060 * probability throughout the range. The URNG should be real-valued and
2061 * deliver number in the range [0, 1).
2063 template<typename _RealType = double>
2068 typedef _RealType input_type;
2069 typedef _RealType result_type;
2073 * Constructs a uniform_real object.
2075 * @param __min [IN] The lower bound of the distribution.
2076 * @param __max [IN] The upper bound of the distribution.
2079 uniform_real(_RealType __min = _RealType(0),
2080 _RealType __max = _RealType(1))
2081 : _M_min(__min), _M_max(__max)
2083 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
2097 template<class _UniformRandomNumberGenerator>
2099 operator()(_UniformRandomNumberGenerator& __urng)
2100 { return (__urng() * (_M_max - _M_min)) + _M_min; }
2103 * Inserts a %uniform_real random number distribution @p __x into the
2104 * output stream @p __os.
2106 * @param __os An output stream.
2107 * @param __x A %uniform_real random number distribution.
2109 * @returns The output stream with the state of @p __x inserted or in
2112 template<typename _RealType1, typename _CharT, typename _Traits>
2113 friend std::basic_ostream<_CharT, _Traits>&
2114 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2115 const uniform_real<_RealType1>& __x);
2118 * Extracts a %uniform_real random number distribution
2119 * @p __x from the input stream @p __is.
2121 * @param __is An input stream.
2122 * @param __x A %uniform_real random number generator engine.
2124 * @returns The input stream with @p __x extracted or in an error state.
2126 template<typename _RealType1, typename _CharT, typename _Traits>
2127 friend std::basic_istream<_CharT, _Traits>&
2128 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2129 uniform_real<_RealType1>& __x);
2138 * @brief An exponential continuous distribution for random numbers.
2140 * The formula for the exponential probability mass function is
2141 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2143 * <table border=1 cellpadding=10 cellspacing=0>
2144 * <caption align=top>Distribution Statistics</caption>
2145 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2146 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2147 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2148 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2149 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2152 template<typename _RealType = double>
2153 class exponential_distribution
2157 typedef _RealType input_type;
2158 typedef _RealType result_type;
2162 * Constructs an exponential distribution with inverse scale parameter
2166 exponential_distribution(const result_type& __lambda = result_type(1))
2167 : _M_lambda(__lambda)
2169 _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
2173 * Gets the inverse scale parameter of the distribution.
2177 { return _M_lambda; }
2180 * Resets the distribution.
2182 * Has no effect on exponential distributions.
2187 template<class _UniformRandomNumberGenerator>
2189 operator()(_UniformRandomNumberGenerator& __urng)
2190 { return -std::log(__urng()) / _M_lambda; }
2193 * Inserts a %exponential_distribution random number distribution
2194 * @p __x into the output stream @p __os.
2196 * @param __os An output stream.
2197 * @param __x A %exponential_distribution random number distribution.
2199 * @returns The output stream with the state of @p __x inserted or in
2202 template<typename _RealType1, typename _CharT, typename _Traits>
2203 friend std::basic_ostream<_CharT, _Traits>&
2204 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2205 const exponential_distribution<_RealType1>& __x);
2208 * Extracts a %exponential_distribution random number distribution
2209 * @p __x from the input stream @p __is.
2211 * @param __is An input stream.
2212 * @param __x A %exponential_distribution random number
2215 * @returns The input stream with @p __x extracted or in an error state.
2217 template<typename _CharT, typename _Traits>
2218 friend std::basic_istream<_CharT, _Traits>&
2219 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2220 exponential_distribution& __x)
2221 { return __is >> __x._M_lambda; }
2224 result_type _M_lambda;
2229 * @brief A normal continuous distribution for random numbers.
2231 * The formula for the normal probability mass function is
2232 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2233 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2235 template<typename _RealType = double>
2236 class normal_distribution
2240 typedef _RealType input_type;
2241 typedef _RealType result_type;
2245 * Constructs a normal distribution with parameters @f$ mean @f$ and
2249 normal_distribution(const result_type& __mean = result_type(0),
2250 const result_type& __sigma = result_type(1))
2251 : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
2253 _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
2257 * Gets the mean of the distribution.
2264 * Gets the @f$ \sigma @f$ of the distribution.
2268 { return _M_sigma; }
2271 * Resets the distribution.
2275 { _M_saved_available = false; }
2277 template<class _UniformRandomNumberGenerator>
2279 operator()(_UniformRandomNumberGenerator& __urng);
2282 * Inserts a %normal_distribution random number distribution
2283 * @p __x into the output stream @p __os.
2285 * @param __os An output stream.
2286 * @param __x A %normal_distribution random number distribution.
2288 * @returns The output stream with the state of @p __x inserted or in
2291 template<typename _RealType1, typename _CharT, typename _Traits>
2292 friend std::basic_ostream<_CharT, _Traits>&
2293 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2294 const normal_distribution<_RealType1>& __x);
2297 * Extracts a %normal_distribution random number distribution
2298 * @p __x from the input stream @p __is.
2300 * @param __is An input stream.
2301 * @param __x A %normal_distribution random number generator engine.
2303 * @returns The input stream with @p __x extracted or in an error state.
2305 template<typename _RealType1, typename _CharT, typename _Traits>
2306 friend std::basic_istream<_CharT, _Traits>&
2307 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2308 normal_distribution<_RealType1>& __x);
2311 result_type _M_mean;
2312 result_type _M_sigma;
2313 result_type _M_saved;
2314 bool _M_saved_available;
2319 * @brief A gamma continuous distribution for random numbers.
2321 * The formula for the gamma probability mass function is
2322 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2324 template<typename _RealType = double>
2325 class gamma_distribution
2329 typedef _RealType input_type;
2330 typedef _RealType result_type;
2334 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2337 gamma_distribution(const result_type& __alpha_val = result_type(1))
2338 : _M_alpha(__alpha_val)
2340 _GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
2345 * Gets the @f$ \alpha @f$ of the distribution.
2349 { return _M_alpha; }
2352 * Resets the distribution.
2357 template<class _UniformRandomNumberGenerator>
2359 operator()(_UniformRandomNumberGenerator& __urng);
2362 * Inserts a %gamma_distribution random number distribution
2363 * @p __x into the output stream @p __os.
2365 * @param __os An output stream.
2366 * @param __x A %gamma_distribution random number distribution.
2368 * @returns The output stream with the state of @p __x inserted or in
2371 template<typename _RealType1, typename _CharT, typename _Traits>
2372 friend std::basic_ostream<_CharT, _Traits>&
2373 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2374 const gamma_distribution<_RealType1>& __x);
2377 * Extracts a %gamma_distribution random number distribution
2378 * @p __x from the input stream @p __is.
2380 * @param __is An input stream.
2381 * @param __x A %gamma_distribution random number generator engine.
2383 * @returns The input stream with @p __x extracted or in an error state.
2385 template<typename _CharT, typename _Traits>
2386 friend std::basic_istream<_CharT, _Traits>&
2387 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2388 gamma_distribution& __x)
2390 __is >> __x._M_alpha;
2391 __x._M_initialize();
2399 result_type _M_alpha;
2401 // Hosts either lambda of GB or d of modified Vaduva's.
2405 /* @} */ // group tr1_random_distributions_continuous
2406 /* @} */ // group tr1_random_distributions
2407 /* @} */ // group tr1_random
2409 _GLIBCXX_END_NAMESPACE_TR1
2412 #include <tr1_impl/random.tcc>