1 // random number generation -*- C++ -*-
3 // Copyright (C) 2006, 2007 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * This is a TR1 C++ Library header.
44 #include <tr1/type_traits>
46 #include <ext/type_traits.h>
47 #include <ext/numeric_traits.h>
48 #include <bits/concept_check.h>
49 #include <debug/debug.h>
53 _GLIBCXX_BEGIN_NAMESPACE(_GLIBCXX_TR1)
55 // [5.1] Random number generation
58 * @addtogroup tr1_random Random Number Generation
59 * A facility for generating random numbers on selected distributions.
64 * Implementation-space details.
68 template<typename _UIntType, int __w,
69 bool = __w < std::numeric_limits<_UIntType>::digits>
71 { static const _UIntType __value = 0; };
73 template<typename _UIntType, int __w>
74 struct _Shift<_UIntType, __w, true>
75 { static const _UIntType __value = _UIntType(1) << __w; };
77 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
80 // Dispatch based on modulus value to prevent divide-by-zero compile-time
81 // errors when m == 0.
82 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
85 { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
87 typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
88 unsigned, unsigned long>::__type _UInt32Type;
91 * An adaptor class for converting the output of any Generator into
92 * the input for a specific Distribution.
94 template<typename _Engine, typename _Distribution>
97 typedef typename _Engine::result_type _Engine_result_type;
98 typedef typename _Distribution::input_type result_type;
101 _Adaptor(const _Engine& __g)
107 result_type __return_value = 0;
108 if (is_integral<_Engine_result_type>::value
109 && is_integral<result_type>::value)
110 __return_value = _M_g.min();
111 else if (!is_integral<result_type>::value)
112 __return_value = result_type(0);
113 return __return_value;
119 result_type __return_value = 0;
120 if (is_integral<_Engine_result_type>::value
121 && is_integral<result_type>::value)
122 __return_value = _M_g.max();
123 else if (!is_integral<result_type>::value)
124 __return_value = result_type(1);
125 return __return_value;
136 * Converts a value generated by the adapted random number generator into a
137 * value in the input domain for the dependent random number distribution.
139 * Because the type traits are compile time constants only the appropriate
140 * clause of the if statements will actually be emitted by the compiler.
142 template<typename _Engine, typename _Distribution>
143 typename _Adaptor<_Engine, _Distribution>::result_type
144 _Adaptor<_Engine, _Distribution>::
147 result_type __return_value = 0;
148 if (is_integral<_Engine_result_type>::value
149 && is_integral<result_type>::value)
150 __return_value = _M_g();
151 else if (is_integral<_Engine_result_type>::value
152 && !is_integral<result_type>::value)
153 __return_value = result_type(_M_g() - _M_g.min())
154 / result_type(_M_g.max() - _M_g.min() + result_type(1));
155 else if (!is_integral<_Engine_result_type>::value
156 && !is_integral<result_type>::value)
157 __return_value = result_type(_M_g() - _M_g.min())
158 / result_type(_M_g.max() - _M_g.min());
159 return __return_value;
161 } // namespace __detail
164 * Produces random numbers on a given disribution function using a un uniform
165 * random number generation engine.
167 * @todo the engine_value_type needs to be studied more carefully.
169 template<typename _Engine, typename _Dist>
170 class variate_generator
172 // Concept requirements.
173 __glibcxx_class_requires(_Engine, _CopyConstructibleConcept)
174 // __glibcxx_class_requires(_Engine, _EngineConcept)
175 // __glibcxx_class_requires(_Dist, _EngineConcept)
178 typedef _Engine engine_type;
179 typedef __detail::_Adaptor<_Engine, _Dist> engine_value_type;
180 typedef _Dist distribution_type;
181 typedef typename _Dist::result_type result_type;
183 // tr1:5.1.1 table 5.1 requirement
184 typedef typename __gnu_cxx::__enable_if<
185 is_arithmetic<result_type>::value, result_type>::__type _IsValidType;
188 * Constructs a variate generator with the uniform random number
189 * generator @p __eng for the random distribution @p __dist.
191 * @throws Any exceptions which may thrown by the copy constructors of
192 * the @p _Engine or @p _Dist objects.
194 variate_generator(engine_type __eng, distribution_type __dist)
195 : _M_engine(__eng), _M_dist(__dist) { }
198 * Gets the next generated value on the distribution.
202 { return _M_dist(_M_engine); }
207 template<typename _Tp>
209 operator()(_Tp __value)
210 { return _M_dist(_M_engine, __value); }
213 * Gets a reference to the underlying uniform random number generator
218 { return _M_engine; }
221 * Gets a const reference to the underlying uniform random number
224 const engine_value_type&
226 { return _M_engine; }
229 * Gets a reference to the underlying random distribution.
236 * Gets a const reference to the underlying random distribution.
238 const distribution_type&
243 * Gets the closed lower bound of the distribution interval.
247 { return this->distribution().min(); }
250 * Gets the closed upper bound of the distribution interval.
254 { return this->distribution().max(); }
257 engine_value_type _M_engine;
258 distribution_type _M_dist;
263 * @addtogroup tr1_random_generators Random Number Generators
264 * @ingroup tr1_random
266 * These classes define objects which provide random or pseudorandom
267 * numbers, either from a discrete or a continuous interval. The
268 * random number generator supplied as a part of this library are
269 * all uniform random number generators which provide a sequence of
270 * random number uniformly distributed over their range.
272 * A number generator is a function object with an operator() that
273 * takes zero arguments and returns a number.
275 * A compliant random number generator must satisy the following
276 * requirements. <table border=1 cellpadding=10 cellspacing=0>
277 * <caption align=top>Random Number Generator Requirements</caption>
278 * <tr><td>To be documented.</td></tr> </table>
284 * @brief A model of a linear congruential random number generator.
286 * A random number generator that produces pseudorandom numbers using the
287 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
289 * The template parameter @p _UIntType must be an unsigned integral type
290 * large enough to store values up to (__m-1). If the template parameter
291 * @p __m is 0, the modulus @p __m used is
292 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
293 * parameters @p __a and @p __c must be less than @p __m.
295 * The size of the state is @f$ 1 @f$.
297 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
298 class linear_congruential
300 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
301 // __glibcpp_class_requires(__a < __m && __c < __m)
304 /** The type of the generated random value. */
305 typedef _UIntType result_type;
307 /** The multiplier. */
308 static const _UIntType multiplier = __a;
310 static const _UIntType increment = __c;
312 static const _UIntType modulus = __m;
315 * Constructs a %linear_congruential random number generator engine with
316 * seed @p __s. The default seed value is 1.
318 * @param __s The initial seed value.
321 linear_congruential(unsigned long __x0 = 1)
322 { this->seed(__x0); }
325 * Constructs a %linear_congruential random number generator engine
326 * seeded from the generator function @p __g.
328 * @param __g The seed generator function.
331 linear_congruential(_Gen& __g)
335 * Reseeds the %linear_congruential random number generator engine
336 * sequence to the seed @g __s.
338 * @param __s The new seed.
341 seed(unsigned long __s = 1);
344 * Reseeds the %linear_congruential random number generator engine
345 * sequence using values from the generator function @p __g.
347 * @param __g the seed generator function.
352 { seed(__g, typename is_fundamental<_Gen>::type()); }
355 * Gets the smallest possible value in the output range.
357 * The minumum depends on the @p __c parameter: if it is zero, the
358 * minimum generated must be > 0, otherwise 0 is allowed.
362 { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
365 * Gets the largest possible value in the output range.
372 * Gets the next random number in the sequence.
378 * Compares two linear congruential random number generator
379 * objects of the same type for equality.
381 * @param __lhs A linear congruential random number generator object.
382 * @param __rhs Another linear congruential random number generator obj.
384 * @returns true if the two objects are equal, false otherwise.
387 operator==(const linear_congruential& __lhs,
388 const linear_congruential& __rhs)
389 { return __lhs._M_x == __rhs._M_x; }
392 * Compares two linear congruential random number generator
393 * objects of the same type for inequality.
395 * @param __lhs A linear congruential random number generator object.
396 * @param __rhs Another linear congruential random number generator obj.
398 * @returns true if the two objects are not equal, false otherwise.
401 operator!=(const linear_congruential& __lhs,
402 const linear_congruential& __rhs)
403 { return !(__lhs == __rhs); }
406 * Writes the textual representation of the state x(i) of x to @p __os.
408 * @param __os The output stream.
409 * @param __lcr A % linear_congruential random number generator.
412 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
414 typename _CharT, typename _Traits>
415 friend std::basic_ostream<_CharT, _Traits>&
416 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
417 const linear_congruential<_UIntType1, __a1, __c1,
421 * Sets the state of the engine by reading its textual
422 * representation from @p __is.
424 * The textual representation must have been previously written using an
425 * output stream whose imbued locale and whose type's template
426 * specialization arguments _CharT and _Traits were the same as those of
429 * @param __is The input stream.
430 * @param __lcr A % linear_congruential random number generator.
433 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
435 typename _CharT, typename _Traits>
436 friend std::basic_istream<_CharT, _Traits>&
437 operator>>(std::basic_istream<_CharT, _Traits>& __is,
438 linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
443 seed(_Gen& __g, true_type)
444 { return seed(static_cast<unsigned long>(__g)); }
448 seed(_Gen& __g, false_type);
454 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
456 typedef linear_congruential<unsigned long, 16807, 0, 2147483647> minstd_rand0;
459 * An alternative LCR (Lehmer Generator function) .
461 typedef linear_congruential<unsigned long, 48271, 0, 2147483647> minstd_rand;
465 * A generalized feedback shift register discrete random number generator.
467 * This algorithm avoind multiplication and division and is designed to be
468 * friendly to a pipelined architecture. If the parameters are chosen
469 * correctly, this generator will produce numbers with a very long period and
470 * fairly good apparent entropy, although still not cryptographically strong.
472 * The best way to use this generator is with the predefined mt19937 class.
474 * This algorithm was originally invented by Makoto Matsumoto and
477 * @var word_size The number of bits in each element of the state vector.
478 * @var state_size The degree of recursion.
479 * @var shift_size The period parameter.
480 * @var mask_bits The separation point bit index.
481 * @var parameter_a The last row of the twist matrix.
482 * @var output_u The first right-shift tempering matrix parameter.
483 * @var output_s The first left-shift tempering matrix parameter.
484 * @var output_b The first left-shift tempering matrix mask.
485 * @var output_t The second left-shift tempering matrix parameter.
486 * @var output_c The second left-shift tempering matrix mask.
487 * @var output_l The second right-shift tempering matrix parameter.
489 template<class _UIntType, int __w, int __n, int __m, int __r,
490 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
491 _UIntType __c, int __l>
492 class mersenne_twister
494 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
498 typedef _UIntType result_type;
501 static const int word_size = __w;
502 static const int state_size = __n;
503 static const int shift_size = __m;
504 static const int mask_bits = __r;
505 static const _UIntType parameter_a = __a;
506 static const int output_u = __u;
507 static const int output_s = __s;
508 static const _UIntType output_b = __b;
509 static const int output_t = __t;
510 static const _UIntType output_c = __c;
511 static const int output_l = __l;
513 // constructors and member function
518 mersenne_twister(unsigned long __value)
522 mersenne_twister(_Gen& __g)
530 seed(unsigned long __value);
535 { seed(__g, typename is_fundamental<_Gen>::type()); }
543 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
549 * Compares two % mersenne_twister random number generator objects of
550 * the same type for equality.
552 * @param __lhs A % mersenne_twister random number generator object.
553 * @param __rhs Another % mersenne_twister random number generator
556 * @returns true if the two objects are equal, false otherwise.
559 operator==(const mersenne_twister& __lhs,
560 const mersenne_twister& __rhs)
561 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
564 * Compares two % mersenne_twister random number generator objects of
565 * the same type for inequality.
567 * @param __lhs A % mersenne_twister random number generator object.
568 * @param __rhs Another % mersenne_twister random number generator
571 * @returns true if the two objects are not equal, false otherwise.
574 operator!=(const mersenne_twister& __lhs,
575 const mersenne_twister& __rhs)
576 { return !(__lhs == __rhs); }
579 * Inserts the current state of a % mersenne_twister random number
580 * generator engine @p __x into the output stream @p __os.
582 * @param __os An output stream.
583 * @param __x A % mersenne_twister random number generator engine.
585 * @returns The output stream with the state of @p __x inserted or in
588 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
589 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
590 _UIntType1 __c1, int __l1,
591 typename _CharT, typename _Traits>
592 friend std::basic_ostream<_CharT, _Traits>&
593 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
594 const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
595 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
598 * Extracts the current state of a % mersenne_twister random number
599 * generator engine @p __x from the input stream @p __is.
601 * @param __is An input stream.
602 * @param __x A % mersenne_twister random number generator engine.
604 * @returns The input stream with the state of @p __x extracted or in
607 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
608 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
609 _UIntType1 __c1, int __l1,
610 typename _CharT, typename _Traits>
611 friend std::basic_istream<_CharT, _Traits>&
612 operator>>(std::basic_istream<_CharT, _Traits>& __is,
613 mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
614 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
619 seed(_Gen& __g, true_type)
620 { return seed(static_cast<unsigned long>(__g)); }
624 seed(_Gen& __g, false_type);
626 _UIntType _M_x[state_size];
631 * The classic Mersenne Twister.
634 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
635 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
636 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
638 typedef mersenne_twister<
639 unsigned long, 32, 624, 397, 31,
647 * @brief The Marsaglia-Zaman generator.
649 * This is a model of a Generalized Fibonacci discrete random number
650 * generator, sometimes referred to as the SWC generator.
652 * A discrete random number generator that produces pseudorandom
653 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
654 * carry_{i-1}) \bmod m @f$.
656 * The size of the state is @f$ r @f$
657 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
659 * N1688[4.13] says "the template parameter _IntType shall denote an integral
660 * type large enough to store values up to m."
663 * @var _M_x The state of the generator. This is a ring buffer.
664 * @var _M_carry The carry.
665 * @var _M_p Current index of x(i - r).
668 template<typename _IntType, _IntType __m, int __s, int __r>
669 class subtract_with_carry
671 __glibcxx_class_requires(_IntType, _IntegerConcept)
674 /** The type of the generated random value. */
675 typedef _IntType result_type;
678 static const _IntType modulus = __m;
679 static const int long_lag = __r;
680 static const int short_lag = __s;
683 * Constructs a default-initialized % subtract_with_carry random number
686 subtract_with_carry()
690 * Constructs an explicitly seeded % subtract_with_carry random number
694 subtract_with_carry(unsigned long __value)
695 { this->seed(__value); }
698 * Constructs a %subtract_with_carry random number generator engine
699 * seeded from the generator function @p __g.
701 * @param __g The seed generator function.
704 subtract_with_carry(_Gen& __g)
708 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
710 * N1688[4.19] modifies this as follows. If @p __value == 0,
711 * sets value to 19780503. In any case, with a linear
712 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
713 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
714 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
715 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
716 * set carry to 1, otherwise sets carry to 0.
719 seed(unsigned long __value = 19780503);
722 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
723 * random number generator.
728 { seed(__g, typename is_fundamental<_Gen>::type()); }
731 * Gets the inclusive minimum value of the range of random integers
732 * returned by this generator.
739 * Gets the inclusive maximum value of the range of random integers
740 * returned by this generator.
744 { return this->modulus - 1; }
747 * Gets the next random number in the sequence.
753 * Compares two % subtract_with_carry random number generator objects of
754 * the same type for equality.
756 * @param __lhs A % subtract_with_carry random number generator object.
757 * @param __rhs Another % subtract_with_carry random number generator
760 * @returns true if the two objects are equal, false otherwise.
763 operator==(const subtract_with_carry& __lhs,
764 const subtract_with_carry& __rhs)
765 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
768 * Compares two % subtract_with_carry random number generator objects of
769 * the same type for inequality.
771 * @param __lhs A % subtract_with_carry random number generator object.
772 * @param __rhs Another % subtract_with_carry random number generator
775 * @returns true if the two objects are not equal, false otherwise.
778 operator!=(const subtract_with_carry& __lhs,
779 const subtract_with_carry& __rhs)
780 { return !(__lhs == __rhs); }
783 * Inserts the current state of a % subtract_with_carry random number
784 * generator engine @p __x into the output stream @p __os.
786 * @param __os An output stream.
787 * @param __x A % subtract_with_carry random number generator engine.
789 * @returns The output stream with the state of @p __x inserted or in
792 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
793 typename _CharT, typename _Traits>
794 friend std::basic_ostream<_CharT, _Traits>&
795 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
796 const subtract_with_carry<_IntType1, __m1, __s1,
800 * Extracts the current state of a % subtract_with_carry random number
801 * generator engine @p __x from the input stream @p __is.
803 * @param __is An input stream.
804 * @param __x A % subtract_with_carry random number generator engine.
806 * @returns The input stream with the state of @p __x extracted or in
809 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
810 typename _CharT, typename _Traits>
811 friend std::basic_istream<_CharT, _Traits>&
812 operator>>(std::basic_istream<_CharT, _Traits>& __is,
813 subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
818 seed(_Gen& __g, true_type)
819 { return seed(static_cast<unsigned long>(__g)); }
823 seed(_Gen& __g, false_type);
825 typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
827 _UIntType _M_x[long_lag];
834 * @brief The Marsaglia-Zaman generator (floats version).
837 * @var _M_x The state of the generator. This is a ring buffer.
838 * @var _M_carry The carry.
839 * @var _M_p Current index of x(i - r).
840 * @var _M_npows Precomputed negative powers of 2.
843 template<typename _RealType, int __w, int __s, int __r>
844 class subtract_with_carry_01
847 /** The type of the generated random value. */
848 typedef _RealType result_type;
851 static const int word_size = __w;
852 static const int long_lag = __r;
853 static const int short_lag = __s;
856 * Constructs a default-initialized % subtract_with_carry_01 random
859 subtract_with_carry_01()
862 _M_initialize_npows();
866 * Constructs an explicitly seeded % subtract_with_carry_01 random number
870 subtract_with_carry_01(unsigned long __value)
873 _M_initialize_npows();
877 * Constructs a % subtract_with_carry_01 random number generator engine
878 * seeded from the generator function @p __g.
880 * @param __g The seed generator function.
883 subtract_with_carry_01(_Gen& __g)
886 _M_initialize_npows();
890 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
893 seed(unsigned long __value = 19780503);
896 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
897 * random number generator.
902 { seed(__g, typename is_fundamental<_Gen>::type()); }
905 * Gets the minimum value of the range of random floats
906 * returned by this generator.
913 * Gets the maximum value of the range of random floats
914 * returned by this generator.
921 * Gets the next random number in the sequence.
927 * Compares two % subtract_with_carry_01 random number generator objects
928 * of the same type for equality.
930 * @param __lhs A % subtract_with_carry_01 random number
932 * @param __rhs Another % subtract_with_carry_01 random number generator
935 * @returns true if the two objects are equal, false otherwise.
938 operator==(const subtract_with_carry_01& __lhs,
939 const subtract_with_carry_01& __rhs)
941 for (int __i = 0; __i < long_lag; ++__i)
942 if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
949 * Compares two % subtract_with_carry_01 random number generator objects
950 * of the same type for inequality.
952 * @param __lhs A % subtract_with_carry_01 random number
955 * @param __rhs Another % subtract_with_carry_01 random number generator
958 * @returns true if the two objects are not equal, false otherwise.
961 operator!=(const subtract_with_carry_01& __lhs,
962 const subtract_with_carry_01& __rhs)
963 { return !(__lhs == __rhs); }
966 * Inserts the current state of a % subtract_with_carry_01 random number
967 * generator engine @p __x into the output stream @p __os.
969 * @param __os An output stream.
970 * @param __x A % subtract_with_carry_01 random number generator engine.
972 * @returns The output stream with the state of @p __x inserted or in
975 template<typename _RealType1, int __w1, int __s1, int __r1,
976 typename _CharT, typename _Traits>
977 friend std::basic_ostream<_CharT, _Traits>&
978 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
979 const subtract_with_carry_01<_RealType1, __w1, __s1,
983 * Extracts the current state of a % subtract_with_carry_01 random number
984 * generator engine @p __x from the input stream @p __is.
986 * @param __is An input stream.
987 * @param __x A % subtract_with_carry_01 random number generator engine.
989 * @returns The input stream with the state of @p __x extracted or in
992 template<typename _RealType1, int __w1, int __s1, int __r1,
993 typename _CharT, typename _Traits>
994 friend std::basic_istream<_CharT, _Traits>&
995 operator>>(std::basic_istream<_CharT, _Traits>& __is,
996 subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
1001 seed(_Gen& __g, true_type)
1002 { return seed(static_cast<unsigned long>(__g)); }
1004 template<class _Gen>
1006 seed(_Gen& __g, false_type);
1009 _M_initialize_npows();
1011 static const int __n = (__w + 31) / 32;
1013 typedef __detail::_UInt32Type _UInt32Type;
1014 _UInt32Type _M_x[long_lag][__n];
1015 _RealType _M_npows[__n];
1016 _UInt32Type _M_carry;
1020 typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
1022 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1023 // 508. Bad parameters for ranlux64_base_01.
1024 typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
1028 * Produces random numbers from some base engine by discarding blocks of
1031 * 0 <= @p __r <= @p __p
1033 template<class _UniformRandomNumberGenerator, int __p, int __r>
1036 // __glibcxx_class_requires(typename base_type::result_type,
1037 // ArithmeticTypeConcept)
1040 /** The type of the underlying generator engine. */
1041 typedef _UniformRandomNumberGenerator base_type;
1042 /** The type of the generated random value. */
1043 typedef typename base_type::result_type result_type;
1046 static const int block_size = __p;
1047 static const int used_block = __r;
1050 * Constructs a default %discard_block engine.
1052 * The underlying engine is default constructed as well.
1058 * Copy constructs a %discard_block engine.
1060 * Copies an existing base class random number geenerator.
1061 * @param rng An existing (base class) engine object.
1064 discard_block(const base_type& __rng)
1065 : _M_b(__rng), _M_n(0) { }
1068 * Seed constructs a %discard_block engine.
1070 * Constructs the underlying generator engine seeded with @p __s.
1071 * @param __s A seed value for the base class engine.
1074 discard_block(unsigned long __s)
1075 : _M_b(__s), _M_n(0) { }
1078 * Generator construct a %discard_block engine.
1080 * @param __g A seed generator function.
1082 template<class _Gen>
1083 discard_block(_Gen& __g)
1084 : _M_b(__g), _M_n(0) { }
1087 * Reseeds the %discard_block object with the default seed for the
1088 * underlying base class generator engine.
1097 * Reseeds the %discard_block object with the given seed generator
1099 * @param __g A seed generator function.
1101 template<class _Gen>
1102 void seed(_Gen& __g)
1109 * Gets a const reference to the underlying generator engine object.
1116 * Gets the minimum value in the generated random number range.
1120 { return _M_b.min(); }
1123 * Gets the maximum value in the generated random number range.
1127 { return _M_b.max(); }
1130 * Gets the next value in the generated random number sequence.
1136 * Compares two %discard_block random number generator objects of
1137 * the same type for equality.
1139 * @param __lhs A %discard_block random number generator object.
1140 * @param __rhs Another %discard_block random number generator
1143 * @returns true if the two objects are equal, false otherwise.
1146 operator==(const discard_block& __lhs, const discard_block& __rhs)
1147 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
1150 * Compares two %discard_block random number generator objects of
1151 * the same type for inequality.
1153 * @param __lhs A %discard_block random number generator object.
1154 * @param __rhs Another %discard_block random number generator
1157 * @returns true if the two objects are not equal, false otherwise.
1160 operator!=(const discard_block& __lhs, const discard_block& __rhs)
1161 { return !(__lhs == __rhs); }
1164 * Inserts the current state of a %discard_block random number
1165 * generator engine @p __x into the output stream @p __os.
1167 * @param __os An output stream.
1168 * @param __x A %discard_block random number generator engine.
1170 * @returns The output stream with the state of @p __x inserted or in
1173 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1174 typename _CharT, typename _Traits>
1175 friend std::basic_ostream<_CharT, _Traits>&
1176 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1177 const discard_block<_UniformRandomNumberGenerator1,
1181 * Extracts the current state of a % subtract_with_carry random number
1182 * generator engine @p __x from the input stream @p __is.
1184 * @param __is An input stream.
1185 * @param __x A %discard_block random number generator engine.
1187 * @returns The input stream with the state of @p __x extracted or in
1190 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1191 typename _CharT, typename _Traits>
1192 friend std::basic_istream<_CharT, _Traits>&
1193 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1194 discard_block<_UniformRandomNumberGenerator1,
1204 * James's luxury-level-3 integer adaptation of Luescher's generator.
1206 typedef discard_block<
1207 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1213 * James's luxury-level-4 integer adaptation of Luescher's generator.
1215 typedef discard_block<
1216 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1221 typedef discard_block<
1222 subtract_with_carry_01<float, 24, 10, 24>,
1227 typedef discard_block<
1228 subtract_with_carry_01<float, 24, 10, 24>,
1235 * A random number generator adaptor class that combines two random number
1236 * generator engines into a single output sequence.
1238 template<class _UniformRandomNumberGenerator1, int __s1,
1239 class _UniformRandomNumberGenerator2, int __s2>
1242 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1243 // result_type, ArithmeticTypeConcept)
1244 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1245 // result_type, ArithmeticTypeConcept)
1248 /** The type of the the first underlying generator engine. */
1249 typedef _UniformRandomNumberGenerator1 base1_type;
1250 /** The type of the the second underlying generator engine. */
1251 typedef _UniformRandomNumberGenerator2 base2_type;
1254 typedef typename base1_type::result_type _Result_type1;
1255 typedef typename base2_type::result_type _Result_type2;
1258 /** The type of the generated random value. */
1259 typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1)
1260 > sizeof(_Result_type2)),
1261 _Result_type1, _Result_type2>::__type result_type;
1264 static const int shift1 = __s1;
1265 static const int shift2 = __s2;
1267 // constructors and member function
1270 { _M_initialize_max(); }
1272 xor_combine(const base1_type& __rng1, const base2_type& __rng2)
1273 : _M_b1(__rng1), _M_b2(__rng2)
1274 { _M_initialize_max(); }
1276 xor_combine(unsigned long __s)
1277 : _M_b1(__s), _M_b2(__s + 1)
1278 { _M_initialize_max(); }
1280 template<class _Gen>
1281 xor_combine(_Gen& __g)
1282 : _M_b1(__g), _M_b2(__g)
1283 { _M_initialize_max(); }
1292 template<class _Gen>
1317 * Gets the next random number in the sequence.
1319 // NB: Not exactly the TR1 formula, per N2079 instead.
1323 return ((result_type(_M_b1() - _M_b1.min()) << shift1)
1324 ^ (result_type(_M_b2() - _M_b2.min()) << shift2));
1328 * Compares two %xor_combine random number generator objects of
1329 * the same type for equality.
1331 * @param __lhs A %xor_combine random number generator object.
1332 * @param __rhs Another %xor_combine random number generator
1335 * @returns true if the two objects are equal, false otherwise.
1338 operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1340 return (__lhs.base1() == __rhs.base1())
1341 && (__lhs.base2() == __rhs.base2());
1345 * Compares two %xor_combine random number generator objects of
1346 * the same type for inequality.
1348 * @param __lhs A %xor_combine random number generator object.
1349 * @param __rhs Another %xor_combine random number generator
1352 * @returns true if the two objects are not equal, false otherwise.
1355 operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1356 { return !(__lhs == __rhs); }
1359 * Inserts the current state of a %xor_combine random number
1360 * generator engine @p __x into the output stream @p __os.
1362 * @param __os An output stream.
1363 * @param __x A %xor_combine random number generator engine.
1365 * @returns The output stream with the state of @p __x inserted or in
1368 template<class _UniformRandomNumberGenerator11, int __s11,
1369 class _UniformRandomNumberGenerator21, int __s21,
1370 typename _CharT, typename _Traits>
1371 friend std::basic_ostream<_CharT, _Traits>&
1372 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1373 const xor_combine<_UniformRandomNumberGenerator11, __s11,
1374 _UniformRandomNumberGenerator21, __s21>& __x);
1377 * Extracts the current state of a %xor_combine random number
1378 * generator engine @p __x from the input stream @p __is.
1380 * @param __is An input stream.
1381 * @param __x A %xor_combine random number generator engine.
1383 * @returns The input stream with the state of @p __x extracted or in
1386 template<class _UniformRandomNumberGenerator11, int __s11,
1387 class _UniformRandomNumberGenerator21, int __s21,
1388 typename _CharT, typename _Traits>
1389 friend std::basic_istream<_CharT, _Traits>&
1390 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1391 xor_combine<_UniformRandomNumberGenerator11, __s11,
1392 _UniformRandomNumberGenerator21, __s21>& __x);
1396 _M_initialize_max();
1399 _M_initialize_max_aux(result_type, result_type, int);
1408 * A standard interface to a platform-specific non-deterministic
1409 * random number generator (if any are available).
1415 typedef unsigned int result_type;
1417 // constructors, destructors and member functions
1419 #ifdef _GLIBCXX_USE_RANDOM_TR1
1422 random_device(const std::string& __token = "/dev/urandom")
1424 if ((__token != "/dev/urandom" && __token != "/dev/random")
1425 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1426 std::__throw_runtime_error(__N("random_device::"
1427 "random_device(const std::string&)"));
1431 { std::fclose(_M_file); }
1436 random_device(const std::string& __token = "mt19937")
1437 : _M_mt(_M_strtoul(__token)) { }
1440 static unsigned long
1441 _M_strtoul(const std::string& __str)
1443 unsigned long __ret = 5489UL;
1444 if (__str != "mt19937")
1446 const char* __nptr = __str.c_str();
1448 __ret = std::strtoul(__nptr, &__endptr, 0);
1449 if (*__nptr == '\0' || *__endptr != '\0')
1450 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1451 "(const std::string&)"));
1462 { return std::numeric_limits<result_type>::min(); }
1466 { return std::numeric_limits<result_type>::max(); }
1475 #ifdef _GLIBCXX_USE_RANDOM_TR1
1477 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1486 random_device(const random_device&);
1487 void operator=(const random_device&);
1489 #ifdef _GLIBCXX_USE_RANDOM_TR1
1496 /* @} */ // group tr1_random_generators
1499 * @addtogroup tr1_random_distributions Random Number Distributions
1500 * @ingroup tr1_random
1505 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1506 * @ingroup tr1_random_distributions
1511 * @brief Uniform discrete distribution for random numbers.
1512 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1513 * probability throughout the range.
1515 template<typename _IntType = int>
1518 __glibcxx_class_requires(_IntType, _IntegerConcept)
1521 /** The type of the parameters of the distribution. */
1522 typedef _IntType input_type;
1523 /** The type of the range of the distribution. */
1524 typedef _IntType result_type;
1528 * Constructs a uniform distribution object.
1531 uniform_int(_IntType __min = 0, _IntType __max = 9)
1532 : _M_min(__min), _M_max(__max)
1534 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1538 * Gets the inclusive lower bound of the distribution range.
1545 * Gets the inclusive upper bound of the distribution range.
1552 * Resets the distribution state.
1554 * Does nothing for the uniform integer distribution.
1560 * Gets a uniformly distributed random number in the range
1563 template<typename _UniformRandomNumberGenerator>
1565 operator()(_UniformRandomNumberGenerator& __urng)
1567 typedef typename _UniformRandomNumberGenerator::result_type
1569 return _M_call(__urng, _M_min, _M_max,
1570 typename is_integral<_UResult_type>::type());
1574 * Gets a uniform random number in the range @f$[0, n)@f$.
1576 * This function is aimed at use with std::random_shuffle.
1578 template<typename _UniformRandomNumberGenerator>
1580 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1582 typedef typename _UniformRandomNumberGenerator::result_type
1584 return _M_call(__urng, 0, __n - 1,
1585 typename is_integral<_UResult_type>::type());
1589 * Inserts a %uniform_int random number distribution @p __x into the
1590 * output stream @p os.
1592 * @param __os An output stream.
1593 * @param __x A %uniform_int random number distribution.
1595 * @returns The output stream with the state of @p __x inserted or in
1598 template<typename _IntType1, typename _CharT, typename _Traits>
1599 friend std::basic_ostream<_CharT, _Traits>&
1600 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1601 const uniform_int<_IntType1>& __x);
1604 * Extracts a %unform_int random number distribution
1605 * @p __x from the input stream @p __is.
1607 * @param __is An input stream.
1608 * @param __x A %uniform_int random number generator engine.
1610 * @returns The input stream with @p __x extracted or in an error state.
1612 template<typename _IntType1, typename _CharT, typename _Traits>
1613 friend std::basic_istream<_CharT, _Traits>&
1614 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1615 uniform_int<_IntType1>& __x);
1618 template<typename _UniformRandomNumberGenerator>
1620 _M_call(_UniformRandomNumberGenerator& __urng,
1621 result_type __min, result_type __max, true_type)
1622 { return result_type(__urng() % (__max - __min + 1)) + __min; }
1624 template<typename _UniformRandomNumberGenerator>
1626 _M_call(_UniformRandomNumberGenerator& __urng,
1627 result_type __min, result_type __max, false_type)
1629 return result_type((__urng() - __urng.min())
1630 / (__urng.max() - __urng.min())
1631 * (__max - __min + 1)) + __min;
1640 * @brief A Bernoulli random number distribution.
1642 * Generates a sequence of true and false values with likelihood @f$ p @f$
1643 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1645 class bernoulli_distribution
1648 typedef int input_type;
1649 typedef bool result_type;
1653 * Constructs a Bernoulli distribution with likelihood @p p.
1655 * @param __p [IN] The likelihood of a true result being returned. Must
1656 * be in the interval @f$ [0, 1] @f$.
1659 bernoulli_distribution(double __p = 0.5)
1662 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1666 * Gets the @p p parameter of the distribution.
1673 * Resets the distribution state.
1675 * Does nothing for a bernoulli distribution.
1681 * Gets the next value in the Bernoullian sequence.
1683 template<class _UniformRandomNumberGenerator>
1685 operator()(_UniformRandomNumberGenerator& __urng)
1687 if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
1693 * Inserts a %bernoulli_distribution random number distribution
1694 * @p __x into the output stream @p __os.
1696 * @param __os An output stream.
1697 * @param __x A %bernoulli_distribution random number distribution.
1699 * @returns The output stream with the state of @p __x inserted or in
1702 template<typename _CharT, typename _Traits>
1703 friend std::basic_ostream<_CharT, _Traits>&
1704 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1705 const bernoulli_distribution& __x);
1708 * Extracts a %bernoulli_distribution random number distribution
1709 * @p __x from the input stream @p __is.
1711 * @param __is An input stream.
1712 * @param __x A %bernoulli_distribution random number generator engine.
1714 * @returns The input stream with @p __x extracted or in an error state.
1716 template<typename _CharT, typename _Traits>
1717 friend std::basic_istream<_CharT, _Traits>&
1718 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1719 bernoulli_distribution& __x)
1720 { return __is >> __x._M_p; }
1728 * @brief A discrete geometric random number distribution.
1730 * The formula for the geometric probability mass function is
1731 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1734 template<typename _IntType = int, typename _RealType = double>
1735 class geometric_distribution
1739 typedef _RealType input_type;
1740 typedef _IntType result_type;
1742 // constructors and member function
1744 geometric_distribution(const _RealType& __p = _RealType(0.5))
1747 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
1752 * Gets the distribution parameter @p p.
1761 template<class _UniformRandomNumberGenerator>
1763 operator()(_UniformRandomNumberGenerator& __urng);
1766 * Inserts a %geometric_distribution random number distribution
1767 * @p __x into the output stream @p __os.
1769 * @param __os An output stream.
1770 * @param __x A %geometric_distribution random number distribution.
1772 * @returns The output stream with the state of @p __x inserted or in
1775 template<typename _IntType1, typename _RealType1,
1776 typename _CharT, typename _Traits>
1777 friend std::basic_ostream<_CharT, _Traits>&
1778 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1779 const geometric_distribution<_IntType1, _RealType1>& __x);
1782 * Extracts a %geometric_distribution random number distribution
1783 * @p __x from the input stream @p __is.
1785 * @param __is An input stream.
1786 * @param __x A %geometric_distribution random number generator engine.
1788 * @returns The input stream with @p __x extracted or in an error state.
1790 template<typename _CharT, typename _Traits>
1791 friend std::basic_istream<_CharT, _Traits>&
1792 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1793 geometric_distribution& __x)
1796 __x._M_initialize();
1803 { _M_log_p = std::log(_M_p); }
1810 template<typename _RealType>
1811 class normal_distribution;
1814 * @brief A discrete Poisson random number distribution.
1816 * The formula for the poisson probability mass function is
1817 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1818 * parameter of the distribution.
1820 template<typename _IntType = int, typename _RealType = double>
1821 class poisson_distribution
1825 typedef _RealType input_type;
1826 typedef _IntType result_type;
1828 // constructors and member function
1830 poisson_distribution(const _RealType& __mean = _RealType(1))
1831 : _M_mean(__mean), _M_nd()
1833 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
1838 * Gets the distribution parameter @p mean.
1848 template<class _UniformRandomNumberGenerator>
1850 operator()(_UniformRandomNumberGenerator& __urng);
1853 * Inserts a %poisson_distribution random number distribution
1854 * @p __x into the output stream @p __os.
1856 * @param __os An output stream.
1857 * @param __x A %poisson_distribution random number distribution.
1859 * @returns The output stream with the state of @p __x inserted or in
1862 template<typename _IntType1, typename _RealType1,
1863 typename _CharT, typename _Traits>
1864 friend std::basic_ostream<_CharT, _Traits>&
1865 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1866 const poisson_distribution<_IntType1, _RealType1>& __x);
1869 * Extracts a %poisson_distribution random number distribution
1870 * @p __x from the input stream @p __is.
1872 * @param __is An input stream.
1873 * @param __x A %poisson_distribution random number generator engine.
1875 * @returns The input stream with @p __x extracted or in an error state.
1877 template<typename _IntType1, typename _RealType1,
1878 typename _CharT, typename _Traits>
1879 friend std::basic_istream<_CharT, _Traits>&
1880 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1881 poisson_distribution<_IntType1, _RealType1>& __x);
1887 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1888 normal_distribution<_RealType> _M_nd;
1892 // Hosts either log(mean) or the threshold of the simple method.
1893 _RealType _M_lm_thr;
1894 #if _GLIBCXX_USE_C99_MATH_TR1
1895 _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
1901 * @brief A discrete binomial random number distribution.
1903 * The formula for the binomial probability mass function is
1904 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1905 * and @f$ p @f$ are the parameters of the distribution.
1907 template<typename _IntType = int, typename _RealType = double>
1908 class binomial_distribution
1912 typedef _RealType input_type;
1913 typedef _IntType result_type;
1915 // constructors and member function
1917 binomial_distribution(_IntType __t = 1,
1918 const _RealType& __p = _RealType(0.5))
1919 : _M_t(__t), _M_p(__p), _M_nd()
1921 _GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
1926 * Gets the distribution @p t parameter.
1933 * Gets the distribution @p p parameter.
1943 template<class _UniformRandomNumberGenerator>
1945 operator()(_UniformRandomNumberGenerator& __urng);
1948 * Inserts a %binomial_distribution random number distribution
1949 * @p __x into the output stream @p __os.
1951 * @param __os An output stream.
1952 * @param __x A %binomial_distribution random number distribution.
1954 * @returns The output stream with the state of @p __x inserted or in
1957 template<typename _IntType1, typename _RealType1,
1958 typename _CharT, typename _Traits>
1959 friend std::basic_ostream<_CharT, _Traits>&
1960 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1961 const binomial_distribution<_IntType1, _RealType1>& __x);
1964 * Extracts a %binomial_distribution random number distribution
1965 * @p __x from the input stream @p __is.
1967 * @param __is An input stream.
1968 * @param __x A %binomial_distribution random number generator engine.
1970 * @returns The input stream with @p __x extracted or in an error state.
1972 template<typename _IntType1, typename _RealType1,
1973 typename _CharT, typename _Traits>
1974 friend std::basic_istream<_CharT, _Traits>&
1975 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1976 binomial_distribution<_IntType1, _RealType1>& __x);
1982 template<class _UniformRandomNumberGenerator>
1984 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
1986 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1987 normal_distribution<_RealType> _M_nd;
1990 #if _GLIBCXX_USE_C99_MATH_TR1
1991 _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
1992 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
2000 /* @} */ // group tr1_random_distributions_discrete
2003 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
2004 * @ingroup tr1_random_distributions
2009 * @brief Uniform continuous distribution for random numbers.
2011 * A continuous random distribution on the range [min, max) with equal
2012 * probability throughout the range. The URNG should be real-valued and
2013 * deliver number in the range [0, 1).
2015 template<typename _RealType = double>
2020 typedef _RealType input_type;
2021 typedef _RealType result_type;
2025 * Constructs a uniform_real object.
2027 * @param __min [IN] The lower bound of the distribution.
2028 * @param __max [IN] The upper bound of the distribution.
2031 uniform_real(_RealType __min = _RealType(0),
2032 _RealType __max = _RealType(1))
2033 : _M_min(__min), _M_max(__max)
2035 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
2049 template<class _UniformRandomNumberGenerator>
2051 operator()(_UniformRandomNumberGenerator& __urng)
2052 { return (__urng() * (_M_max - _M_min)) + _M_min; }
2055 * Inserts a %uniform_real random number distribution @p __x into the
2056 * output stream @p __os.
2058 * @param __os An output stream.
2059 * @param __x A %uniform_real random number distribution.
2061 * @returns The output stream with the state of @p __x inserted or in
2064 template<typename _RealType1, typename _CharT, typename _Traits>
2065 friend std::basic_ostream<_CharT, _Traits>&
2066 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2067 const uniform_real<_RealType1>& __x);
2070 * Extracts a %unform_real random number distribution
2071 * @p __x from the input stream @p __is.
2073 * @param __is An input stream.
2074 * @param __x A %uniform_real random number generator engine.
2076 * @returns The input stream with @p __x extracted or in an error state.
2078 template<typename _RealType1, typename _CharT, typename _Traits>
2079 friend std::basic_istream<_CharT, _Traits>&
2080 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2081 uniform_real<_RealType1>& __x);
2090 * @brief An exponential continuous distribution for random numbers.
2092 * The formula for the exponential probability mass function is
2093 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2095 * <table border=1 cellpadding=10 cellspacing=0>
2096 * <caption align=top>Distribution Statistics</caption>
2097 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2098 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2099 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2100 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2101 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2104 template<typename _RealType = double>
2105 class exponential_distribution
2109 typedef _RealType input_type;
2110 typedef _RealType result_type;
2114 * Constructs an exponential distribution with inverse scale parameter
2118 exponential_distribution(const result_type& __lambda = result_type(1))
2119 : _M_lambda(__lambda)
2121 _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
2125 * Gets the inverse scale parameter of the distribution.
2129 { return _M_lambda; }
2132 * Resets the distribution.
2134 * Has no effect on exponential distributions.
2139 template<class _UniformRandomNumberGenerator>
2141 operator()(_UniformRandomNumberGenerator& __urng)
2142 { return -std::log(__urng()) / _M_lambda; }
2145 * Inserts a %exponential_distribution random number distribution
2146 * @p __x into the output stream @p __os.
2148 * @param __os An output stream.
2149 * @param __x A %exponential_distribution random number distribution.
2151 * @returns The output stream with the state of @p __x inserted or in
2154 template<typename _RealType1, typename _CharT, typename _Traits>
2155 friend std::basic_ostream<_CharT, _Traits>&
2156 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2157 const exponential_distribution<_RealType1>& __x);
2160 * Extracts a %exponential_distribution random number distribution
2161 * @p __x from the input stream @p __is.
2163 * @param __is An input stream.
2164 * @param __x A %exponential_distribution random number
2167 * @returns The input stream with @p __x extracted or in an error state.
2169 template<typename _CharT, typename _Traits>
2170 friend std::basic_istream<_CharT, _Traits>&
2171 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2172 exponential_distribution& __x)
2173 { return __is >> __x._M_lambda; }
2176 result_type _M_lambda;
2181 * @brief A normal continuous distribution for random numbers.
2183 * The formula for the normal probability mass function is
2184 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2185 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2187 template<typename _RealType = double>
2188 class normal_distribution
2192 typedef _RealType input_type;
2193 typedef _RealType result_type;
2197 * Constructs a normal distribution with parameters @f$ mean @f$ and
2201 normal_distribution(const result_type& __mean = result_type(0),
2202 const result_type& __sigma = result_type(1))
2203 : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
2205 _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
2209 * Gets the mean of the distribution.
2216 * Gets the @f$ \sigma @f$ of the distribution.
2220 { return _M_sigma; }
2223 * Resets the distribution.
2227 { _M_saved_available = false; }
2229 template<class _UniformRandomNumberGenerator>
2231 operator()(_UniformRandomNumberGenerator& __urng);
2234 * Inserts a %normal_distribution random number distribution
2235 * @p __x into the output stream @p __os.
2237 * @param __os An output stream.
2238 * @param __x A %normal_distribution random number distribution.
2240 * @returns The output stream with the state of @p __x inserted or in
2243 template<typename _RealType1, typename _CharT, typename _Traits>
2244 friend std::basic_ostream<_CharT, _Traits>&
2245 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2246 const normal_distribution<_RealType1>& __x);
2249 * Extracts a %normal_distribution random number distribution
2250 * @p __x from the input stream @p __is.
2252 * @param __is An input stream.
2253 * @param __x A %normal_distribution random number generator engine.
2255 * @returns The input stream with @p __x extracted or in an error state.
2257 template<typename _RealType1, typename _CharT, typename _Traits>
2258 friend std::basic_istream<_CharT, _Traits>&
2259 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2260 normal_distribution<_RealType1>& __x);
2263 result_type _M_mean;
2264 result_type _M_sigma;
2265 result_type _M_saved;
2266 bool _M_saved_available;
2271 * @brief A gamma continuous distribution for random numbers.
2273 * The formula for the gamma probability mass function is
2274 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2276 template<typename _RealType = double>
2277 class gamma_distribution
2281 typedef _RealType input_type;
2282 typedef _RealType result_type;
2286 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2289 gamma_distribution(const result_type& __alpha_val = result_type(1))
2290 : _M_alpha(__alpha_val)
2292 _GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
2297 * Gets the @f$ \alpha @f$ of the distribution.
2301 { return _M_alpha; }
2304 * Resets the distribution.
2309 template<class _UniformRandomNumberGenerator>
2311 operator()(_UniformRandomNumberGenerator& __urng);
2314 * Inserts a %gamma_distribution random number distribution
2315 * @p __x into the output stream @p __os.
2317 * @param __os An output stream.
2318 * @param __x A %gamma_distribution random number distribution.
2320 * @returns The output stream with the state of @p __x inserted or in
2323 template<typename _RealType1, typename _CharT, typename _Traits>
2324 friend std::basic_ostream<_CharT, _Traits>&
2325 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2326 const gamma_distribution<_RealType1>& __x);
2329 * Extracts a %gamma_distribution random number distribution
2330 * @p __x from the input stream @p __is.
2332 * @param __is An input stream.
2333 * @param __x A %gamma_distribution random number generator engine.
2335 * @returns The input stream with @p __x extracted or in an error state.
2337 template<typename _CharT, typename _Traits>
2338 friend std::basic_istream<_CharT, _Traits>&
2339 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2340 gamma_distribution& __x)
2342 __is >> __x._M_alpha;
2343 __x._M_initialize();
2351 result_type _M_alpha;
2353 // Hosts either lambda of GB or d of modified Vaduva's.
2357 /* @} */ // group tr1_random_distributions_continuous
2358 /* @} */ // group tr1_random_distributions
2359 /* @} */ // group tr1_random
2361 _GLIBCXX_END_NAMESPACE
2364 #include <tr1/random.tcc>
2366 #endif // _TR1_RANDOM