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 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/>.
26 * @file tr1_impl/random
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
33 _GLIBCXX_BEGIN_NAMESPACE_TR1
35 // [5.1] Random number generation
38 * @defgroup tr1_random Random Number Generation
40 * A facility for generating random numbers on selected distributions.
45 * Implementation-space details.
49 template<typename _UIntType, int __w,
50 bool = __w < std::numeric_limits<_UIntType>::digits>
52 { static const _UIntType __value = 0; };
54 template<typename _UIntType, int __w>
55 struct _Shift<_UIntType, __w, true>
56 { static const _UIntType __value = _UIntType(1) << __w; };
58 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
61 // Dispatch based on modulus value to prevent divide-by-zero compile-time
62 // errors when m == 0.
63 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
66 { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
68 typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
69 unsigned, unsigned long>::__type _UInt32Type;
72 * An adaptor class for converting the output of any Generator into
73 * the input for a specific Distribution.
75 template<typename _Engine, typename _Distribution>
78 typedef typename remove_reference<_Engine>::type _BEngine;
79 typedef typename _BEngine::result_type _Engine_result_type;
80 typedef typename _Distribution::input_type result_type;
83 _Adaptor(const _Engine& __g)
89 result_type __return_value;
90 if (is_integral<_Engine_result_type>::value
91 && is_integral<result_type>::value)
92 __return_value = _M_g.min();
94 __return_value = result_type(0);
95 return __return_value;
101 result_type __return_value;
102 if (is_integral<_Engine_result_type>::value
103 && is_integral<result_type>::value)
104 __return_value = _M_g.max();
105 else if (!is_integral<result_type>::value)
106 __return_value = result_type(1);
108 __return_value = std::numeric_limits<result_type>::max() - 1;
109 return __return_value;
113 * Converts a value generated by the adapted random number generator
114 * into a value in the input domain for the dependent random number
117 * Because the type traits are compile time constants only the
118 * appropriate clause of the if statements will actually be emitted
124 result_type __return_value;
125 if (is_integral<_Engine_result_type>::value
126 && is_integral<result_type>::value)
127 __return_value = _M_g();
128 else if (!is_integral<_Engine_result_type>::value
129 && !is_integral<result_type>::value)
130 __return_value = result_type(_M_g() - _M_g.min())
131 / result_type(_M_g.max() - _M_g.min());
132 else if (is_integral<_Engine_result_type>::value
133 && !is_integral<result_type>::value)
134 __return_value = result_type(_M_g() - _M_g.min())
135 / result_type(_M_g.max() - _M_g.min() + result_type(1));
137 __return_value = (((_M_g() - _M_g.min())
138 / (_M_g.max() - _M_g.min()))
139 * std::numeric_limits<result_type>::max());
140 return __return_value;
147 // Specialization for _Engine*.
148 template<typename _Engine, typename _Distribution>
149 struct _Adaptor<_Engine*, _Distribution>
151 typedef typename _Engine::result_type _Engine_result_type;
152 typedef typename _Distribution::input_type result_type;
155 _Adaptor(_Engine* __g)
161 result_type __return_value;
162 if (is_integral<_Engine_result_type>::value
163 && is_integral<result_type>::value)
164 __return_value = _M_g->min();
166 __return_value = result_type(0);
167 return __return_value;
173 result_type __return_value;
174 if (is_integral<_Engine_result_type>::value
175 && is_integral<result_type>::value)
176 __return_value = _M_g->max();
177 else if (!is_integral<result_type>::value)
178 __return_value = result_type(1);
180 __return_value = std::numeric_limits<result_type>::max() - 1;
181 return __return_value;
187 result_type __return_value;
188 if (is_integral<_Engine_result_type>::value
189 && is_integral<result_type>::value)
190 __return_value = (*_M_g)();
191 else if (!is_integral<_Engine_result_type>::value
192 && !is_integral<result_type>::value)
193 __return_value = result_type((*_M_g)() - _M_g->min())
194 / result_type(_M_g->max() - _M_g->min());
195 else if (is_integral<_Engine_result_type>::value
196 && !is_integral<result_type>::value)
197 __return_value = result_type((*_M_g)() - _M_g->min())
198 / result_type(_M_g->max() - _M_g->min() + result_type(1));
200 __return_value = ((((*_M_g)() - _M_g->min())
201 / (_M_g->max() - _M_g->min()))
202 * std::numeric_limits<result_type>::max());
203 return __return_value;
209 } // namespace __detail
212 * Produces random numbers on a given distribution function using a
213 * non-uniform random number generation engine.
215 * @todo the engine_value_type needs to be studied more carefully.
217 template<typename _Engine, typename _Dist>
218 class variate_generator
220 // Concept requirements.
221 __glibcxx_class_requires(_Engine, _CopyConstructibleConcept)
222 // __glibcxx_class_requires(_Engine, _EngineConcept)
223 // __glibcxx_class_requires(_Dist, _EngineConcept)
226 typedef _Engine engine_type;
227 typedef __detail::_Adaptor<_Engine, _Dist> engine_value_type;
228 typedef _Dist distribution_type;
229 typedef typename _Dist::result_type result_type;
231 // tr1:5.1.1 table 5.1 requirement
232 typedef typename __gnu_cxx::__enable_if<
233 is_arithmetic<result_type>::value, result_type>::__type _IsValidType;
236 * Constructs a variate generator with the uniform random number
237 * generator @p __eng for the random distribution @p __dist.
239 * @throws Any exceptions which may thrown by the copy constructors of
240 * the @p _Engine or @p _Dist objects.
242 variate_generator(engine_type __eng, distribution_type __dist)
243 : _M_engine(__eng), _M_dist(__dist) { }
246 * Gets the next generated value on the distribution.
250 { return _M_dist(_M_engine); }
255 template<typename _Tp>
257 operator()(_Tp __value)
258 { return _M_dist(_M_engine, __value); }
261 * Gets a reference to the underlying uniform random number generator
266 { return _M_engine; }
269 * Gets a const reference to the underlying uniform random number
272 const engine_value_type&
274 { return _M_engine; }
277 * Gets a reference to the underlying random distribution.
284 * Gets a const reference to the underlying random distribution.
286 const distribution_type&
291 * Gets the closed lower bound of the distribution interval.
295 { return this->distribution().min(); }
298 * Gets the closed upper bound of the distribution interval.
302 { return this->distribution().max(); }
305 engine_value_type _M_engine;
306 distribution_type _M_dist;
311 * @defgroup tr1_random_generators Random Number Generators
312 * @ingroup tr1_random
314 * These classes define objects which provide random or pseudorandom
315 * numbers, either from a discrete or a continuous interval. The
316 * random number generator supplied as a part of this library are
317 * all uniform random number generators which provide a sequence of
318 * random number uniformly distributed over their range.
320 * A number generator is a function object with an operator() that
321 * takes zero arguments and returns a number.
323 * A compliant random number generator must satisfy the following
324 * requirements. <table border=1 cellpadding=10 cellspacing=0>
325 * <caption align=top>Random Number Generator Requirements</caption>
326 * <tr><td>To be documented.</td></tr> </table>
332 * @brief A model of a linear congruential random number generator.
334 * A random number generator that produces pseudorandom numbers using the
335 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
337 * The template parameter @p _UIntType must be an unsigned integral type
338 * large enough to store values up to (__m-1). If the template parameter
339 * @p __m is 0, the modulus @p __m used is
340 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
341 * parameters @p __a and @p __c must be less than @p __m.
343 * The size of the state is @f$ 1 @f$.
345 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
346 class linear_congruential
348 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
349 // __glibcpp_class_requires(__a < __m && __c < __m)
352 /** The type of the generated random value. */
353 typedef _UIntType result_type;
355 /** The multiplier. */
356 static const _UIntType multiplier = __a;
358 static const _UIntType increment = __c;
360 static const _UIntType modulus = __m;
363 * Constructs a %linear_congruential random number generator engine with
364 * seed @p __s. The default seed value is 1.
366 * @param __s The initial seed value.
369 linear_congruential(unsigned long __x0 = 1)
370 { this->seed(__x0); }
373 * Constructs a %linear_congruential random number generator engine
374 * seeded from the generator function @p __g.
376 * @param __g The seed generator function.
379 linear_congruential(_Gen& __g)
383 * Reseeds the %linear_congruential random number generator engine
384 * sequence to the seed @g __s.
386 * @param __s The new seed.
389 seed(unsigned long __s = 1);
392 * Reseeds the %linear_congruential random number generator engine
393 * sequence using values from the generator function @p __g.
395 * @param __g the seed generator function.
400 { seed(__g, typename is_fundamental<_Gen>::type()); }
403 * Gets the smallest possible value in the output range.
405 * The minimum depends on the @p __c parameter: if it is zero, the
406 * minimum generated must be > 0, otherwise 0 is allowed.
410 { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
413 * Gets the largest possible value in the output range.
420 * Gets the next random number in the sequence.
426 * Compares two linear congruential random number generator
427 * objects of the same type for equality.
429 * @param __lhs A linear congruential random number generator object.
430 * @param __rhs Another linear congruential random number generator obj.
432 * @returns true if the two objects are equal, false otherwise.
435 operator==(const linear_congruential& __lhs,
436 const linear_congruential& __rhs)
437 { return __lhs._M_x == __rhs._M_x; }
440 * Compares two linear congruential random number generator
441 * objects of the same type for inequality.
443 * @param __lhs A linear congruential random number generator object.
444 * @param __rhs Another linear congruential random number generator obj.
446 * @returns true if the two objects are not equal, false otherwise.
449 operator!=(const linear_congruential& __lhs,
450 const linear_congruential& __rhs)
451 { return !(__lhs == __rhs); }
454 * Writes the textual representation of the state x(i) of x to @p __os.
456 * @param __os The output stream.
457 * @param __lcr A % linear_congruential random number generator.
460 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
462 typename _CharT, typename _Traits>
463 friend std::basic_ostream<_CharT, _Traits>&
464 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
465 const linear_congruential<_UIntType1, __a1, __c1,
469 * Sets the state of the engine by reading its textual
470 * representation from @p __is.
472 * The textual representation must have been previously written using an
473 * output stream whose imbued locale and whose type's template
474 * specialization arguments _CharT and _Traits were the same as those of
477 * @param __is The input stream.
478 * @param __lcr A % linear_congruential random number generator.
481 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
483 typename _CharT, typename _Traits>
484 friend std::basic_istream<_CharT, _Traits>&
485 operator>>(std::basic_istream<_CharT, _Traits>& __is,
486 linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
491 seed(_Gen& __g, true_type)
492 { return seed(static_cast<unsigned long>(__g)); }
496 seed(_Gen& __g, false_type);
502 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
504 typedef linear_congruential<unsigned long, 16807, 0, 2147483647> minstd_rand0;
507 * An alternative LCR (Lehmer Generator function) .
509 typedef linear_congruential<unsigned long, 48271, 0, 2147483647> minstd_rand;
513 * A generalized feedback shift register discrete random number generator.
515 * This algorithm avoids multiplication and division and is designed to be
516 * friendly to a pipelined architecture. If the parameters are chosen
517 * correctly, this generator will produce numbers with a very long period and
518 * fairly good apparent entropy, although still not cryptographically strong.
520 * The best way to use this generator is with the predefined mt19937 class.
522 * This algorithm was originally invented by Makoto Matsumoto and
525 * @var word_size The number of bits in each element of the state vector.
526 * @var state_size The degree of recursion.
527 * @var shift_size The period parameter.
528 * @var mask_bits The separation point bit index.
529 * @var parameter_a The last row of the twist matrix.
530 * @var output_u The first right-shift tempering matrix parameter.
531 * @var output_s The first left-shift tempering matrix parameter.
532 * @var output_b The first left-shift tempering matrix mask.
533 * @var output_t The second left-shift tempering matrix parameter.
534 * @var output_c The second left-shift tempering matrix mask.
535 * @var output_l The second right-shift tempering matrix parameter.
537 template<class _UIntType, int __w, int __n, int __m, int __r,
538 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
539 _UIntType __c, int __l>
540 class mersenne_twister
542 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
546 typedef _UIntType result_type;
549 static const int word_size = __w;
550 static const int state_size = __n;
551 static const int shift_size = __m;
552 static const int mask_bits = __r;
553 static const _UIntType parameter_a = __a;
554 static const int output_u = __u;
555 static const int output_s = __s;
556 static const _UIntType output_b = __b;
557 static const int output_t = __t;
558 static const _UIntType output_c = __c;
559 static const int output_l = __l;
561 // constructors and member function
566 mersenne_twister(unsigned long __value)
570 mersenne_twister(_Gen& __g)
578 seed(unsigned long __value);
583 { seed(__g, typename is_fundamental<_Gen>::type()); }
591 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
597 * Compares two % mersenne_twister random number generator objects of
598 * the same type for equality.
600 * @param __lhs A % mersenne_twister random number generator object.
601 * @param __rhs Another % mersenne_twister random number generator
604 * @returns true if the two objects are equal, false otherwise.
607 operator==(const mersenne_twister& __lhs,
608 const mersenne_twister& __rhs)
609 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
612 * Compares two % mersenne_twister random number generator objects of
613 * the same type for inequality.
615 * @param __lhs A % mersenne_twister random number generator object.
616 * @param __rhs Another % mersenne_twister random number generator
619 * @returns true if the two objects are not equal, false otherwise.
622 operator!=(const mersenne_twister& __lhs,
623 const mersenne_twister& __rhs)
624 { return !(__lhs == __rhs); }
627 * Inserts the current state of a % mersenne_twister random number
628 * generator engine @p __x into the output stream @p __os.
630 * @param __os An output stream.
631 * @param __x A % mersenne_twister random number generator engine.
633 * @returns The output stream with the state of @p __x inserted or in
636 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
637 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
638 _UIntType1 __c1, int __l1,
639 typename _CharT, typename _Traits>
640 friend std::basic_ostream<_CharT, _Traits>&
641 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
642 const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
643 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
646 * Extracts the current state of a % mersenne_twister random number
647 * generator engine @p __x from the input stream @p __is.
649 * @param __is An input stream.
650 * @param __x A % mersenne_twister random number generator engine.
652 * @returns The input stream with the state of @p __x extracted or in
655 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
656 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
657 _UIntType1 __c1, int __l1,
658 typename _CharT, typename _Traits>
659 friend std::basic_istream<_CharT, _Traits>&
660 operator>>(std::basic_istream<_CharT, _Traits>& __is,
661 mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
662 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
667 seed(_Gen& __g, true_type)
668 { return seed(static_cast<unsigned long>(__g)); }
672 seed(_Gen& __g, false_type);
674 _UIntType _M_x[state_size];
679 * The classic Mersenne Twister.
682 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
683 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
684 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
686 typedef mersenne_twister<
687 unsigned long, 32, 624, 397, 31,
695 * @brief The Marsaglia-Zaman generator.
697 * This is a model of a Generalized Fibonacci discrete random number
698 * generator, sometimes referred to as the SWC generator.
700 * A discrete random number generator that produces pseudorandom
701 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
702 * carry_{i-1}) \bmod m @f$.
704 * The size of the state is @f$ r @f$
705 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
707 * N1688[4.13] says "the template parameter _IntType shall denote an integral
708 * type large enough to store values up to m."
710 * @var _M_x The state of the generator. This is a ring buffer.
711 * @var _M_carry The carry.
712 * @var _M_p Current index of x(i - r).
714 template<typename _IntType, _IntType __m, int __s, int __r>
715 class subtract_with_carry
717 __glibcxx_class_requires(_IntType, _IntegerConcept)
720 /** The type of the generated random value. */
721 typedef _IntType result_type;
724 static const _IntType modulus = __m;
725 static const int long_lag = __r;
726 static const int short_lag = __s;
729 * Constructs a default-initialized % subtract_with_carry random number
732 subtract_with_carry()
736 * Constructs an explicitly seeded % subtract_with_carry random number
740 subtract_with_carry(unsigned long __value)
741 { this->seed(__value); }
744 * Constructs a %subtract_with_carry random number generator engine
745 * seeded from the generator function @p __g.
747 * @param __g The seed generator function.
750 subtract_with_carry(_Gen& __g)
754 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
756 * N1688[4.19] modifies this as follows. If @p __value == 0,
757 * sets value to 19780503. In any case, with a linear
758 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
759 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
760 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
761 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
762 * set carry to 1, otherwise sets carry to 0.
765 seed(unsigned long __value = 19780503);
768 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
769 * random number generator.
774 { seed(__g, typename is_fundamental<_Gen>::type()); }
777 * Gets the inclusive minimum value of the range of random integers
778 * returned by this generator.
785 * Gets the inclusive maximum value of the range of random integers
786 * returned by this generator.
790 { return this->modulus - 1; }
793 * Gets the next random number in the sequence.
799 * Compares two % subtract_with_carry random number generator objects of
800 * the same type for equality.
802 * @param __lhs A % subtract_with_carry random number generator object.
803 * @param __rhs Another % subtract_with_carry random number generator
806 * @returns true if the two objects are equal, false otherwise.
809 operator==(const subtract_with_carry& __lhs,
810 const subtract_with_carry& __rhs)
811 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
814 * Compares two % subtract_with_carry random number generator objects of
815 * the same type for inequality.
817 * @param __lhs A % subtract_with_carry random number generator object.
818 * @param __rhs Another % subtract_with_carry random number generator
821 * @returns true if the two objects are not equal, false otherwise.
824 operator!=(const subtract_with_carry& __lhs,
825 const subtract_with_carry& __rhs)
826 { return !(__lhs == __rhs); }
829 * Inserts the current state of a % subtract_with_carry random number
830 * generator engine @p __x into the output stream @p __os.
832 * @param __os An output stream.
833 * @param __x A % subtract_with_carry random number generator engine.
835 * @returns The output stream with the state of @p __x inserted or in
838 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
839 typename _CharT, typename _Traits>
840 friend std::basic_ostream<_CharT, _Traits>&
841 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
842 const subtract_with_carry<_IntType1, __m1, __s1,
846 * Extracts the current state of a % subtract_with_carry random number
847 * generator engine @p __x from the input stream @p __is.
849 * @param __is An input stream.
850 * @param __x A % subtract_with_carry random number generator engine.
852 * @returns The input stream with the state of @p __x extracted or in
855 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
856 typename _CharT, typename _Traits>
857 friend std::basic_istream<_CharT, _Traits>&
858 operator>>(std::basic_istream<_CharT, _Traits>& __is,
859 subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
864 seed(_Gen& __g, true_type)
865 { return seed(static_cast<unsigned long>(__g)); }
869 seed(_Gen& __g, false_type);
871 typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
873 _UIntType _M_x[long_lag];
880 * @brief The Marsaglia-Zaman generator (floats version).
882 * @var _M_x The state of the generator. This is a ring buffer.
883 * @var _M_carry The carry.
884 * @var _M_p Current index of x(i - r).
885 * @var _M_npows Precomputed negative powers of 2.
887 template<typename _RealType, int __w, int __s, int __r>
888 class subtract_with_carry_01
891 /** The type of the generated random value. */
892 typedef _RealType result_type;
895 static const int word_size = __w;
896 static const int long_lag = __r;
897 static const int short_lag = __s;
900 * Constructs a default-initialized % subtract_with_carry_01 random
903 subtract_with_carry_01()
906 _M_initialize_npows();
910 * Constructs an explicitly seeded % subtract_with_carry_01 random number
914 subtract_with_carry_01(unsigned long __value)
917 _M_initialize_npows();
921 * Constructs a % subtract_with_carry_01 random number generator engine
922 * seeded from the generator function @p __g.
924 * @param __g The seed generator function.
927 subtract_with_carry_01(_Gen& __g)
930 _M_initialize_npows();
934 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
937 seed(unsigned long __value = 19780503);
940 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
941 * random number generator.
946 { seed(__g, typename is_fundamental<_Gen>::type()); }
949 * Gets the minimum value of the range of random floats
950 * returned by this generator.
957 * Gets the maximum value of the range of random floats
958 * returned by this generator.
965 * Gets the next random number in the sequence.
971 * Compares two % subtract_with_carry_01 random number generator objects
972 * of the same type for equality.
974 * @param __lhs A % subtract_with_carry_01 random number
976 * @param __rhs Another % subtract_with_carry_01 random number generator
979 * @returns true if the two objects are equal, false otherwise.
982 operator==(const subtract_with_carry_01& __lhs,
983 const subtract_with_carry_01& __rhs)
985 for (int __i = 0; __i < long_lag; ++__i)
986 if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
993 * Compares two % subtract_with_carry_01 random number generator objects
994 * of the same type for inequality.
996 * @param __lhs A % subtract_with_carry_01 random number
999 * @param __rhs Another % subtract_with_carry_01 random number generator
1002 * @returns true if the two objects are not equal, false otherwise.
1005 operator!=(const subtract_with_carry_01& __lhs,
1006 const subtract_with_carry_01& __rhs)
1007 { return !(__lhs == __rhs); }
1010 * Inserts the current state of a % subtract_with_carry_01 random number
1011 * generator engine @p __x into the output stream @p __os.
1013 * @param __os An output stream.
1014 * @param __x A % subtract_with_carry_01 random number generator engine.
1016 * @returns The output stream with the state of @p __x inserted or in
1019 template<typename _RealType1, int __w1, int __s1, int __r1,
1020 typename _CharT, typename _Traits>
1021 friend std::basic_ostream<_CharT, _Traits>&
1022 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1023 const subtract_with_carry_01<_RealType1, __w1, __s1,
1027 * Extracts the current state of a % subtract_with_carry_01 random number
1028 * generator engine @p __x from the input stream @p __is.
1030 * @param __is An input stream.
1031 * @param __x A % subtract_with_carry_01 random number generator engine.
1033 * @returns The input stream with the state of @p __x extracted or in
1036 template<typename _RealType1, int __w1, int __s1, int __r1,
1037 typename _CharT, typename _Traits>
1038 friend std::basic_istream<_CharT, _Traits>&
1039 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1040 subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
1043 template<class _Gen>
1045 seed(_Gen& __g, true_type)
1046 { return seed(static_cast<unsigned long>(__g)); }
1048 template<class _Gen>
1050 seed(_Gen& __g, false_type);
1053 _M_initialize_npows();
1055 static const int __n = (__w + 31) / 32;
1057 typedef __detail::_UInt32Type _UInt32Type;
1058 _UInt32Type _M_x[long_lag][__n];
1059 _RealType _M_npows[__n];
1060 _UInt32Type _M_carry;
1064 typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
1066 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1067 // 508. Bad parameters for ranlux64_base_01.
1068 typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
1072 * Produces random numbers from some base engine by discarding blocks of
1075 * 0 <= @p __r <= @p __p
1077 template<class _UniformRandomNumberGenerator, int __p, int __r>
1080 // __glibcxx_class_requires(typename base_type::result_type,
1081 // ArithmeticTypeConcept)
1084 /** The type of the underlying generator engine. */
1085 typedef _UniformRandomNumberGenerator base_type;
1086 /** The type of the generated random value. */
1087 typedef typename base_type::result_type result_type;
1090 static const int block_size = __p;
1091 static const int used_block = __r;
1094 * Constructs a default %discard_block engine.
1096 * The underlying engine is default constructed as well.
1102 * Copy constructs a %discard_block engine.
1104 * Copies an existing base class random number generator.
1105 * @param rng An existing (base class) engine object.
1108 discard_block(const base_type& __rng)
1109 : _M_b(__rng), _M_n(0) { }
1112 * Seed constructs a %discard_block engine.
1114 * Constructs the underlying generator engine seeded with @p __s.
1115 * @param __s A seed value for the base class engine.
1118 discard_block(unsigned long __s)
1119 : _M_b(__s), _M_n(0) { }
1122 * Generator construct a %discard_block engine.
1124 * @param __g A seed generator function.
1126 template<class _Gen>
1127 discard_block(_Gen& __g)
1128 : _M_b(__g), _M_n(0) { }
1131 * Reseeds the %discard_block object with the default seed for the
1132 * underlying base class generator engine.
1141 * Reseeds the %discard_block object with the given seed generator
1143 * @param __g A seed generator function.
1145 template<class _Gen>
1146 void seed(_Gen& __g)
1153 * Gets a const reference to the underlying generator engine object.
1160 * Gets the minimum value in the generated random number range.
1164 { return _M_b.min(); }
1167 * Gets the maximum value in the generated random number range.
1171 { return _M_b.max(); }
1174 * Gets the next value in the generated random number sequence.
1180 * Compares two %discard_block random number generator objects of
1181 * the same type for equality.
1183 * @param __lhs A %discard_block random number generator object.
1184 * @param __rhs Another %discard_block random number generator
1187 * @returns true if the two objects are equal, false otherwise.
1190 operator==(const discard_block& __lhs, const discard_block& __rhs)
1191 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
1194 * Compares two %discard_block random number generator objects of
1195 * the same type for inequality.
1197 * @param __lhs A %discard_block random number generator object.
1198 * @param __rhs Another %discard_block random number generator
1201 * @returns true if the two objects are not equal, false otherwise.
1204 operator!=(const discard_block& __lhs, const discard_block& __rhs)
1205 { return !(__lhs == __rhs); }
1208 * Inserts the current state of a %discard_block random number
1209 * generator engine @p __x into the output stream @p __os.
1211 * @param __os An output stream.
1212 * @param __x A %discard_block random number generator engine.
1214 * @returns The output stream with the state of @p __x inserted or in
1217 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1218 typename _CharT, typename _Traits>
1219 friend std::basic_ostream<_CharT, _Traits>&
1220 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1221 const discard_block<_UniformRandomNumberGenerator1,
1225 * Extracts the current state of a % subtract_with_carry random number
1226 * generator engine @p __x from the input stream @p __is.
1228 * @param __is An input stream.
1229 * @param __x A %discard_block random number generator engine.
1231 * @returns The input stream with the state of @p __x extracted or in
1234 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1235 typename _CharT, typename _Traits>
1236 friend std::basic_istream<_CharT, _Traits>&
1237 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1238 discard_block<_UniformRandomNumberGenerator1,
1248 * James's luxury-level-3 integer adaptation of Luescher's generator.
1250 typedef discard_block<
1251 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1257 * James's luxury-level-4 integer adaptation of Luescher's generator.
1259 typedef discard_block<
1260 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1265 typedef discard_block<
1266 subtract_with_carry_01<float, 24, 10, 24>,
1271 typedef discard_block<
1272 subtract_with_carry_01<float, 24, 10, 24>,
1279 * A random number generator adaptor class that combines two random number
1280 * generator engines into a single output sequence.
1282 template<class _UniformRandomNumberGenerator1, int __s1,
1283 class _UniformRandomNumberGenerator2, int __s2>
1286 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1287 // result_type, ArithmeticTypeConcept)
1288 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1289 // result_type, ArithmeticTypeConcept)
1292 /** The type of the first underlying generator engine. */
1293 typedef _UniformRandomNumberGenerator1 base1_type;
1294 /** The type of the second underlying generator engine. */
1295 typedef _UniformRandomNumberGenerator2 base2_type;
1298 typedef typename base1_type::result_type _Result_type1;
1299 typedef typename base2_type::result_type _Result_type2;
1302 /** The type of the generated random value. */
1303 typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1)
1304 > sizeof(_Result_type2)),
1305 _Result_type1, _Result_type2>::__type result_type;
1308 static const int shift1 = __s1;
1309 static const int shift2 = __s2;
1311 // constructors and member function
1314 { _M_initialize_max(); }
1316 xor_combine(const base1_type& __rng1, const base2_type& __rng2)
1317 : _M_b1(__rng1), _M_b2(__rng2)
1318 { _M_initialize_max(); }
1320 xor_combine(unsigned long __s)
1321 : _M_b1(__s), _M_b2(__s + 1)
1322 { _M_initialize_max(); }
1324 template<class _Gen>
1325 xor_combine(_Gen& __g)
1326 : _M_b1(__g), _M_b2(__g)
1327 { _M_initialize_max(); }
1336 template<class _Gen>
1361 * Gets the next random number in the sequence.
1363 // NB: Not exactly the TR1 formula, per N2079 instead.
1367 return ((result_type(_M_b1() - _M_b1.min()) << shift1)
1368 ^ (result_type(_M_b2() - _M_b2.min()) << shift2));
1372 * Compares two %xor_combine random number generator objects of
1373 * the same type for equality.
1375 * @param __lhs A %xor_combine random number generator object.
1376 * @param __rhs Another %xor_combine random number generator
1379 * @returns true if the two objects are equal, false otherwise.
1382 operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1384 return (__lhs.base1() == __rhs.base1())
1385 && (__lhs.base2() == __rhs.base2());
1389 * Compares two %xor_combine random number generator objects of
1390 * the same type for inequality.
1392 * @param __lhs A %xor_combine random number generator object.
1393 * @param __rhs Another %xor_combine random number generator
1396 * @returns true if the two objects are not equal, false otherwise.
1399 operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1400 { return !(__lhs == __rhs); }
1403 * Inserts the current state of a %xor_combine random number
1404 * generator engine @p __x into the output stream @p __os.
1406 * @param __os An output stream.
1407 * @param __x A %xor_combine random number generator engine.
1409 * @returns The output stream with the state of @p __x inserted or in
1412 template<class _UniformRandomNumberGenerator11, int __s11,
1413 class _UniformRandomNumberGenerator21, int __s21,
1414 typename _CharT, typename _Traits>
1415 friend std::basic_ostream<_CharT, _Traits>&
1416 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1417 const xor_combine<_UniformRandomNumberGenerator11, __s11,
1418 _UniformRandomNumberGenerator21, __s21>& __x);
1421 * Extracts the current state of a %xor_combine random number
1422 * generator engine @p __x from the input stream @p __is.
1424 * @param __is An input stream.
1425 * @param __x A %xor_combine random number generator engine.
1427 * @returns The input stream with the state of @p __x extracted or in
1430 template<class _UniformRandomNumberGenerator11, int __s11,
1431 class _UniformRandomNumberGenerator21, int __s21,
1432 typename _CharT, typename _Traits>
1433 friend std::basic_istream<_CharT, _Traits>&
1434 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1435 xor_combine<_UniformRandomNumberGenerator11, __s11,
1436 _UniformRandomNumberGenerator21, __s21>& __x);
1440 _M_initialize_max();
1443 _M_initialize_max_aux(result_type, result_type, int);
1452 * A standard interface to a platform-specific non-deterministic
1453 * random number generator (if any are available).
1459 typedef unsigned int result_type;
1461 // constructors, destructors and member functions
1463 #ifdef _GLIBCXX_USE_RANDOM_TR1
1466 random_device(const std::string& __token = "/dev/urandom")
1468 if ((__token != "/dev/urandom" && __token != "/dev/random")
1469 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1470 std::__throw_runtime_error(__N("random_device::"
1471 "random_device(const std::string&)"));
1475 { std::fclose(_M_file); }
1480 random_device(const std::string& __token = "mt19937")
1481 : _M_mt(_M_strtoul(__token)) { }
1484 static unsigned long
1485 _M_strtoul(const std::string& __str)
1487 unsigned long __ret = 5489UL;
1488 if (__str != "mt19937")
1490 const char* __nptr = __str.c_str();
1492 __ret = std::strtoul(__nptr, &__endptr, 0);
1493 if (*__nptr == '\0' || *__endptr != '\0')
1494 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1495 "(const std::string&)"));
1506 { return std::numeric_limits<result_type>::min(); }
1510 { return std::numeric_limits<result_type>::max(); }
1519 #ifdef _GLIBCXX_USE_RANDOM_TR1
1521 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1530 random_device(const random_device&);
1531 void operator=(const random_device&);
1533 #ifdef _GLIBCXX_USE_RANDOM_TR1
1540 /* @} */ // group tr1_random_generators
1543 * @defgroup tr1_random_distributions Random Number Distributions
1544 * @ingroup tr1_random
1549 * @defgroup tr1_random_distributions_discrete Discrete Distributions
1550 * @ingroup tr1_random_distributions
1555 * @brief Uniform discrete distribution for random numbers.
1556 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1557 * probability throughout the range.
1559 template<typename _IntType = int>
1562 __glibcxx_class_requires(_IntType, _IntegerConcept)
1565 /** The type of the parameters of the distribution. */
1566 typedef _IntType input_type;
1567 /** The type of the range of the distribution. */
1568 typedef _IntType result_type;
1572 * Constructs a uniform distribution object.
1575 uniform_int(_IntType __min = 0, _IntType __max = 9)
1576 : _M_min(__min), _M_max(__max)
1578 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1582 * Gets the inclusive lower bound of the distribution range.
1589 * Gets the inclusive upper bound of the distribution range.
1596 * Resets the distribution state.
1598 * Does nothing for the uniform integer distribution.
1604 * Gets a uniformly distributed random number in the range
1607 template<typename _UniformRandomNumberGenerator>
1609 operator()(_UniformRandomNumberGenerator& __urng)
1611 typedef typename _UniformRandomNumberGenerator::result_type
1613 return _M_call(__urng, _M_min, _M_max,
1614 typename is_integral<_UResult_type>::type());
1618 * Gets a uniform random number in the range @f$[0, n)@f$.
1620 * This function is aimed at use with std::random_shuffle.
1622 template<typename _UniformRandomNumberGenerator>
1624 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1626 typedef typename _UniformRandomNumberGenerator::result_type
1628 return _M_call(__urng, 0, __n - 1,
1629 typename is_integral<_UResult_type>::type());
1633 * Inserts a %uniform_int random number distribution @p __x into the
1634 * output stream @p os.
1636 * @param __os An output stream.
1637 * @param __x A %uniform_int random number distribution.
1639 * @returns The output stream with the state of @p __x inserted or in
1642 template<typename _IntType1, typename _CharT, typename _Traits>
1643 friend std::basic_ostream<_CharT, _Traits>&
1644 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1645 const uniform_int<_IntType1>& __x);
1648 * Extracts a %uniform_int random number distribution
1649 * @p __x from the input stream @p __is.
1651 * @param __is An input stream.
1652 * @param __x A %uniform_int random number generator engine.
1654 * @returns The input stream with @p __x extracted or in an error state.
1656 template<typename _IntType1, typename _CharT, typename _Traits>
1657 friend std::basic_istream<_CharT, _Traits>&
1658 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1659 uniform_int<_IntType1>& __x);
1662 template<typename _UniformRandomNumberGenerator>
1664 _M_call(_UniformRandomNumberGenerator& __urng,
1665 result_type __min, result_type __max, true_type);
1667 template<typename _UniformRandomNumberGenerator>
1669 _M_call(_UniformRandomNumberGenerator& __urng,
1670 result_type __min, result_type __max, false_type)
1672 return result_type((__urng() - __urng.min())
1673 / (__urng.max() - __urng.min())
1674 * (__max - __min + 1)) + __min;
1683 * @brief A Bernoulli random number distribution.
1685 * Generates a sequence of true and false values with likelihood @f$ p @f$
1686 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1688 class bernoulli_distribution
1691 typedef int input_type;
1692 typedef bool result_type;
1696 * Constructs a Bernoulli distribution with likelihood @p p.
1698 * @param __p [IN] The likelihood of a true result being returned. Must
1699 * be in the interval @f$ [0, 1] @f$.
1702 bernoulli_distribution(double __p = 0.5)
1705 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1709 * Gets the @p p parameter of the distribution.
1716 * Resets the distribution state.
1718 * Does nothing for a Bernoulli distribution.
1724 * Gets the next value in the Bernoullian sequence.
1726 template<class _UniformRandomNumberGenerator>
1728 operator()(_UniformRandomNumberGenerator& __urng)
1730 if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
1736 * Inserts a %bernoulli_distribution random number distribution
1737 * @p __x into the output stream @p __os.
1739 * @param __os An output stream.
1740 * @param __x A %bernoulli_distribution random number distribution.
1742 * @returns The output stream with the state of @p __x inserted or in
1745 template<typename _CharT, typename _Traits>
1746 friend std::basic_ostream<_CharT, _Traits>&
1747 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1748 const bernoulli_distribution& __x);
1751 * Extracts a %bernoulli_distribution random number distribution
1752 * @p __x from the input stream @p __is.
1754 * @param __is An input stream.
1755 * @param __x A %bernoulli_distribution random number generator engine.
1757 * @returns The input stream with @p __x extracted or in an error state.
1759 template<typename _CharT, typename _Traits>
1760 friend std::basic_istream<_CharT, _Traits>&
1761 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1762 bernoulli_distribution& __x)
1763 { return __is >> __x._M_p; }
1771 * @brief A discrete geometric random number distribution.
1773 * The formula for the geometric probability mass function is
1774 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1777 template<typename _IntType = int, typename _RealType = double>
1778 class geometric_distribution
1782 typedef _RealType input_type;
1783 typedef _IntType result_type;
1785 // constructors and member function
1787 geometric_distribution(const _RealType& __p = _RealType(0.5))
1790 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
1795 * Gets the distribution parameter @p p.
1804 template<class _UniformRandomNumberGenerator>
1806 operator()(_UniformRandomNumberGenerator& __urng);
1809 * Inserts a %geometric_distribution random number distribution
1810 * @p __x into the output stream @p __os.
1812 * @param __os An output stream.
1813 * @param __x A %geometric_distribution random number distribution.
1815 * @returns The output stream with the state of @p __x inserted or in
1818 template<typename _IntType1, typename _RealType1,
1819 typename _CharT, typename _Traits>
1820 friend std::basic_ostream<_CharT, _Traits>&
1821 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1822 const geometric_distribution<_IntType1, _RealType1>& __x);
1825 * Extracts a %geometric_distribution random number distribution
1826 * @p __x from the input stream @p __is.
1828 * @param __is An input stream.
1829 * @param __x A %geometric_distribution random number generator engine.
1831 * @returns The input stream with @p __x extracted or in an error state.
1833 template<typename _CharT, typename _Traits>
1834 friend std::basic_istream<_CharT, _Traits>&
1835 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1836 geometric_distribution& __x)
1839 __x._M_initialize();
1846 { _M_log_p = std::log(_M_p); }
1853 template<typename _RealType>
1854 class normal_distribution;
1857 * @brief A discrete Poisson random number distribution.
1859 * The formula for the Poisson probability mass function is
1860 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1861 * parameter of the distribution.
1863 template<typename _IntType = int, typename _RealType = double>
1864 class poisson_distribution
1868 typedef _RealType input_type;
1869 typedef _IntType result_type;
1871 // constructors and member function
1873 poisson_distribution(const _RealType& __mean = _RealType(1))
1874 : _M_mean(__mean), _M_nd()
1876 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
1881 * Gets the distribution parameter @p mean.
1891 template<class _UniformRandomNumberGenerator>
1893 operator()(_UniformRandomNumberGenerator& __urng);
1896 * Inserts a %poisson_distribution random number distribution
1897 * @p __x into the output stream @p __os.
1899 * @param __os An output stream.
1900 * @param __x A %poisson_distribution random number distribution.
1902 * @returns The output stream with the state of @p __x inserted or in
1905 template<typename _IntType1, typename _RealType1,
1906 typename _CharT, typename _Traits>
1907 friend std::basic_ostream<_CharT, _Traits>&
1908 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1909 const poisson_distribution<_IntType1, _RealType1>& __x);
1912 * Extracts a %poisson_distribution random number distribution
1913 * @p __x from the input stream @p __is.
1915 * @param __is An input stream.
1916 * @param __x A %poisson_distribution random number generator engine.
1918 * @returns The input stream with @p __x extracted or in an error state.
1920 template<typename _IntType1, typename _RealType1,
1921 typename _CharT, typename _Traits>
1922 friend std::basic_istream<_CharT, _Traits>&
1923 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1924 poisson_distribution<_IntType1, _RealType1>& __x);
1930 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1931 normal_distribution<_RealType> _M_nd;
1935 // Hosts either log(mean) or the threshold of the simple method.
1936 _RealType _M_lm_thr;
1937 #if _GLIBCXX_USE_C99_MATH_TR1
1938 _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
1944 * @brief A discrete binomial random number distribution.
1946 * The formula for the binomial probability mass function is
1947 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1948 * and @f$ p @f$ are the parameters of the distribution.
1950 template<typename _IntType = int, typename _RealType = double>
1951 class binomial_distribution
1955 typedef _RealType input_type;
1956 typedef _IntType result_type;
1958 // constructors and member function
1960 binomial_distribution(_IntType __t = 1,
1961 const _RealType& __p = _RealType(0.5))
1962 : _M_t(__t), _M_p(__p), _M_nd()
1964 _GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
1969 * Gets the distribution @p t parameter.
1976 * Gets the distribution @p p parameter.
1986 template<class _UniformRandomNumberGenerator>
1988 operator()(_UniformRandomNumberGenerator& __urng);
1991 * Inserts a %binomial_distribution random number distribution
1992 * @p __x into the output stream @p __os.
1994 * @param __os An output stream.
1995 * @param __x A %binomial_distribution random number distribution.
1997 * @returns The output stream with the state of @p __x inserted or in
2000 template<typename _IntType1, typename _RealType1,
2001 typename _CharT, typename _Traits>
2002 friend std::basic_ostream<_CharT, _Traits>&
2003 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2004 const binomial_distribution<_IntType1, _RealType1>& __x);
2007 * Extracts a %binomial_distribution random number distribution
2008 * @p __x from the input stream @p __is.
2010 * @param __is An input stream.
2011 * @param __x A %binomial_distribution random number generator engine.
2013 * @returns The input stream with @p __x extracted or in an error state.
2015 template<typename _IntType1, typename _RealType1,
2016 typename _CharT, typename _Traits>
2017 friend std::basic_istream<_CharT, _Traits>&
2018 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2019 binomial_distribution<_IntType1, _RealType1>& __x);
2025 template<class _UniformRandomNumberGenerator>
2027 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
2029 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
2030 normal_distribution<_RealType> _M_nd;
2033 #if _GLIBCXX_USE_C99_MATH_TR1
2034 _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
2035 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
2043 /* @} */ // group tr1_random_distributions_discrete
2046 * @defgroup tr1_random_distributions_continuous Continuous Distributions
2047 * @ingroup tr1_random_distributions
2052 * @brief Uniform continuous distribution for random numbers.
2054 * A continuous random distribution on the range [min, max) with equal
2055 * probability throughout the range. The URNG should be real-valued and
2056 * deliver number in the range [0, 1).
2058 template<typename _RealType = double>
2063 typedef _RealType input_type;
2064 typedef _RealType result_type;
2068 * Constructs a uniform_real object.
2070 * @param __min [IN] The lower bound of the distribution.
2071 * @param __max [IN] The upper bound of the distribution.
2074 uniform_real(_RealType __min = _RealType(0),
2075 _RealType __max = _RealType(1))
2076 : _M_min(__min), _M_max(__max)
2078 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
2092 template<class _UniformRandomNumberGenerator>
2094 operator()(_UniformRandomNumberGenerator& __urng)
2095 { return (__urng() * (_M_max - _M_min)) + _M_min; }
2098 * Inserts a %uniform_real random number distribution @p __x into the
2099 * output stream @p __os.
2101 * @param __os An output stream.
2102 * @param __x A %uniform_real random number distribution.
2104 * @returns The output stream with the state of @p __x inserted or in
2107 template<typename _RealType1, typename _CharT, typename _Traits>
2108 friend std::basic_ostream<_CharT, _Traits>&
2109 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2110 const uniform_real<_RealType1>& __x);
2113 * Extracts a %uniform_real random number distribution
2114 * @p __x from the input stream @p __is.
2116 * @param __is An input stream.
2117 * @param __x A %uniform_real random number generator engine.
2119 * @returns The input stream with @p __x extracted or in an error state.
2121 template<typename _RealType1, typename _CharT, typename _Traits>
2122 friend std::basic_istream<_CharT, _Traits>&
2123 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2124 uniform_real<_RealType1>& __x);
2133 * @brief An exponential continuous distribution for random numbers.
2135 * The formula for the exponential probability mass function is
2136 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2138 * <table border=1 cellpadding=10 cellspacing=0>
2139 * <caption align=top>Distribution Statistics</caption>
2140 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2141 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2142 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2143 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2144 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2147 template<typename _RealType = double>
2148 class exponential_distribution
2152 typedef _RealType input_type;
2153 typedef _RealType result_type;
2157 * Constructs an exponential distribution with inverse scale parameter
2161 exponential_distribution(const result_type& __lambda = result_type(1))
2162 : _M_lambda(__lambda)
2164 _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
2168 * Gets the inverse scale parameter of the distribution.
2172 { return _M_lambda; }
2175 * Resets the distribution.
2177 * Has no effect on exponential distributions.
2182 template<class _UniformRandomNumberGenerator>
2184 operator()(_UniformRandomNumberGenerator& __urng)
2185 { return -std::log(__urng()) / _M_lambda; }
2188 * Inserts a %exponential_distribution random number distribution
2189 * @p __x into the output stream @p __os.
2191 * @param __os An output stream.
2192 * @param __x A %exponential_distribution random number distribution.
2194 * @returns The output stream with the state of @p __x inserted or in
2197 template<typename _RealType1, typename _CharT, typename _Traits>
2198 friend std::basic_ostream<_CharT, _Traits>&
2199 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2200 const exponential_distribution<_RealType1>& __x);
2203 * Extracts a %exponential_distribution random number distribution
2204 * @p __x from the input stream @p __is.
2206 * @param __is An input stream.
2207 * @param __x A %exponential_distribution random number
2210 * @returns The input stream with @p __x extracted or in an error state.
2212 template<typename _CharT, typename _Traits>
2213 friend std::basic_istream<_CharT, _Traits>&
2214 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2215 exponential_distribution& __x)
2216 { return __is >> __x._M_lambda; }
2219 result_type _M_lambda;
2224 * @brief A normal continuous distribution for random numbers.
2226 * The formula for the normal probability mass function is
2227 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2228 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2230 template<typename _RealType = double>
2231 class normal_distribution
2235 typedef _RealType input_type;
2236 typedef _RealType result_type;
2240 * Constructs a normal distribution with parameters @f$ mean @f$ and
2244 normal_distribution(const result_type& __mean = result_type(0),
2245 const result_type& __sigma = result_type(1))
2246 : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
2248 _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
2252 * Gets the mean of the distribution.
2259 * Gets the @f$ \sigma @f$ of the distribution.
2263 { return _M_sigma; }
2266 * Resets the distribution.
2270 { _M_saved_available = false; }
2272 template<class _UniformRandomNumberGenerator>
2274 operator()(_UniformRandomNumberGenerator& __urng);
2277 * Inserts a %normal_distribution random number distribution
2278 * @p __x into the output stream @p __os.
2280 * @param __os An output stream.
2281 * @param __x A %normal_distribution random number distribution.
2283 * @returns The output stream with the state of @p __x inserted or in
2286 template<typename _RealType1, typename _CharT, typename _Traits>
2287 friend std::basic_ostream<_CharT, _Traits>&
2288 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2289 const normal_distribution<_RealType1>& __x);
2292 * Extracts a %normal_distribution random number distribution
2293 * @p __x from the input stream @p __is.
2295 * @param __is An input stream.
2296 * @param __x A %normal_distribution random number generator engine.
2298 * @returns The input stream with @p __x extracted or in an error state.
2300 template<typename _RealType1, typename _CharT, typename _Traits>
2301 friend std::basic_istream<_CharT, _Traits>&
2302 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2303 normal_distribution<_RealType1>& __x);
2306 result_type _M_mean;
2307 result_type _M_sigma;
2308 result_type _M_saved;
2309 bool _M_saved_available;
2314 * @brief A gamma continuous distribution for random numbers.
2316 * The formula for the gamma probability mass function is
2317 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2319 template<typename _RealType = double>
2320 class gamma_distribution
2324 typedef _RealType input_type;
2325 typedef _RealType result_type;
2329 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2332 gamma_distribution(const result_type& __alpha_val = result_type(1))
2333 : _M_alpha(__alpha_val)
2335 _GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
2340 * Gets the @f$ \alpha @f$ of the distribution.
2344 { return _M_alpha; }
2347 * Resets the distribution.
2352 template<class _UniformRandomNumberGenerator>
2354 operator()(_UniformRandomNumberGenerator& __urng);
2357 * Inserts a %gamma_distribution random number distribution
2358 * @p __x into the output stream @p __os.
2360 * @param __os An output stream.
2361 * @param __x A %gamma_distribution random number distribution.
2363 * @returns The output stream with the state of @p __x inserted or in
2366 template<typename _RealType1, typename _CharT, typename _Traits>
2367 friend std::basic_ostream<_CharT, _Traits>&
2368 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2369 const gamma_distribution<_RealType1>& __x);
2372 * Extracts a %gamma_distribution random number distribution
2373 * @p __x from the input stream @p __is.
2375 * @param __is An input stream.
2376 * @param __x A %gamma_distribution random number generator engine.
2378 * @returns The input stream with @p __x extracted or in an error state.
2380 template<typename _CharT, typename _Traits>
2381 friend std::basic_istream<_CharT, _Traits>&
2382 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2383 gamma_distribution& __x)
2385 __is >> __x._M_alpha;
2386 __x._M_initialize();
2394 result_type _M_alpha;
2396 // Hosts either lambda of GB or d of modified Vaduva's.
2400 /* @} */ // group tr1_random_distributions_continuous
2401 /* @} */ // group tr1_random_distributions
2402 /* @} */ // group tr1_random
2404 _GLIBCXX_END_NAMESPACE_TR1
2407 #include <tr1_impl/random.tcc>