1 // random number generation -*- C++ -*-
3 // Copyright (C) 2007, 2008 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * @file tr1_impl/random
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
38 _GLIBCXX_BEGIN_NAMESPACE_TR1
40 // [5.1] Random number generation
43 * @addtogroup tr1_random Random Number Generation
44 * A facility for generating random numbers on selected distributions.
49 * Implementation-space details.
53 template<typename _UIntType, int __w,
54 bool = __w < std::numeric_limits<_UIntType>::digits>
56 { static const _UIntType __value = 0; };
58 template<typename _UIntType, int __w>
59 struct _Shift<_UIntType, __w, true>
60 { static const _UIntType __value = _UIntType(1) << __w; };
62 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
65 // Dispatch based on modulus value to prevent divide-by-zero compile-time
66 // errors when m == 0.
67 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
70 { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
72 typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
73 unsigned, unsigned long>::__type _UInt32Type;
76 * An adaptor class for converting the output of any Generator into
77 * the input for a specific Distribution.
79 template<typename _Engine, typename _Distribution>
82 typedef typename _Engine::result_type _Engine_result_type;
83 typedef typename _Distribution::input_type result_type;
86 _Adaptor(const _Engine& __g)
92 result_type __return_value;
93 if (is_integral<_Engine_result_type>::value
94 && is_integral<result_type>::value)
95 __return_value = _M_g.min();
97 __return_value = result_type(0);
98 return __return_value;
104 result_type __return_value;
105 if (is_integral<_Engine_result_type>::value
106 && is_integral<result_type>::value)
107 __return_value = _M_g.max();
108 else if (!is_integral<result_type>::value)
109 __return_value = result_type(1);
111 __return_value = std::numeric_limits<result_type>::max() - 1;
112 return __return_value;
116 * Converts a value generated by the adapted random number generator
117 * into a value in the input domain for the dependent random number
120 * Because the type traits are compile time constants only the
121 * appropriate clause of the if statements will actually be emitted
127 result_type __return_value;
128 if (is_integral<_Engine_result_type>::value
129 && is_integral<result_type>::value)
130 __return_value = _M_g();
131 else if (!is_integral<_Engine_result_type>::value
132 && !is_integral<result_type>::value)
133 __return_value = result_type(_M_g() - _M_g.min())
134 / result_type(_M_g.max() - _M_g.min());
135 else if (is_integral<_Engine_result_type>::value
136 && !is_integral<result_type>::value)
137 __return_value = result_type(_M_g() - _M_g.min())
138 / result_type(_M_g.max() - _M_g.min() + result_type(1));
140 __return_value = (((_M_g() - _M_g.min())
141 / (_M_g.max() - _M_g.min()))
142 * std::numeric_limits<result_type>::max());
143 return __return_value;
149 } // namespace __detail
152 * Produces random numbers on a given distribution function using a
153 * non-uniform random number generation engine.
155 * @todo the engine_value_type needs to be studied more carefully.
157 template<typename _Engine, typename _Dist>
158 class variate_generator
160 // Concept requirements.
161 __glibcxx_class_requires(_Engine, _CopyConstructibleConcept)
162 // __glibcxx_class_requires(_Engine, _EngineConcept)
163 // __glibcxx_class_requires(_Dist, _EngineConcept)
166 typedef _Engine engine_type;
167 typedef __detail::_Adaptor<_Engine, _Dist> engine_value_type;
168 typedef _Dist distribution_type;
169 typedef typename _Dist::result_type result_type;
171 // tr1:5.1.1 table 5.1 requirement
172 typedef typename __gnu_cxx::__enable_if<
173 is_arithmetic<result_type>::value, result_type>::__type _IsValidType;
176 * Constructs a variate generator with the uniform random number
177 * generator @p __eng for the random distribution @p __dist.
179 * @throws Any exceptions which may thrown by the copy constructors of
180 * the @p _Engine or @p _Dist objects.
182 variate_generator(engine_type __eng, distribution_type __dist)
183 : _M_engine(__eng), _M_dist(__dist) { }
186 * Gets the next generated value on the distribution.
190 { return _M_dist(_M_engine); }
195 template<typename _Tp>
197 operator()(_Tp __value)
198 { return _M_dist(_M_engine, __value); }
201 * Gets a reference to the underlying uniform random number generator
206 { return _M_engine; }
209 * Gets a const reference to the underlying uniform random number
212 const engine_value_type&
214 { return _M_engine; }
217 * Gets a reference to the underlying random distribution.
224 * Gets a const reference to the underlying random distribution.
226 const distribution_type&
231 * Gets the closed lower bound of the distribution interval.
235 { return this->distribution().min(); }
238 * Gets the closed upper bound of the distribution interval.
242 { return this->distribution().max(); }
245 engine_value_type _M_engine;
246 distribution_type _M_dist;
251 * @addtogroup tr1_random_generators Random Number Generators
252 * @ingroup tr1_random
254 * These classes define objects which provide random or pseudorandom
255 * numbers, either from a discrete or a continuous interval. The
256 * random number generator supplied as a part of this library are
257 * all uniform random number generators which provide a sequence of
258 * random number uniformly distributed over their range.
260 * A number generator is a function object with an operator() that
261 * takes zero arguments and returns a number.
263 * A compliant random number generator must satisfy the following
264 * requirements. <table border=1 cellpadding=10 cellspacing=0>
265 * <caption align=top>Random Number Generator Requirements</caption>
266 * <tr><td>To be documented.</td></tr> </table>
272 * @brief A model of a linear congruential random number generator.
274 * A random number generator that produces pseudorandom numbers using the
275 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
277 * The template parameter @p _UIntType must be an unsigned integral type
278 * large enough to store values up to (__m-1). If the template parameter
279 * @p __m is 0, the modulus @p __m used is
280 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
281 * parameters @p __a and @p __c must be less than @p __m.
283 * The size of the state is @f$ 1 @f$.
285 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
286 class linear_congruential
288 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
289 // __glibcpp_class_requires(__a < __m && __c < __m)
292 /** The type of the generated random value. */
293 typedef _UIntType result_type;
295 /** The multiplier. */
296 static const _UIntType multiplier = __a;
298 static const _UIntType increment = __c;
300 static const _UIntType modulus = __m;
303 * Constructs a %linear_congruential random number generator engine with
304 * seed @p __s. The default seed value is 1.
306 * @param __s The initial seed value.
309 linear_congruential(unsigned long __x0 = 1)
310 { this->seed(__x0); }
313 * Constructs a %linear_congruential random number generator engine
314 * seeded from the generator function @p __g.
316 * @param __g The seed generator function.
319 linear_congruential(_Gen& __g)
323 * Reseeds the %linear_congruential random number generator engine
324 * sequence to the seed @g __s.
326 * @param __s The new seed.
329 seed(unsigned long __s = 1);
332 * Reseeds the %linear_congruential random number generator engine
333 * sequence using values from the generator function @p __g.
335 * @param __g the seed generator function.
340 { seed(__g, typename is_fundamental<_Gen>::type()); }
343 * Gets the smallest possible value in the output range.
345 * The minimum depends on the @p __c parameter: if it is zero, the
346 * minimum generated must be > 0, otherwise 0 is allowed.
350 { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
353 * Gets the largest possible value in the output range.
360 * Gets the next random number in the sequence.
366 * Compares two linear congruential random number generator
367 * objects of the same type for equality.
369 * @param __lhs A linear congruential random number generator object.
370 * @param __rhs Another linear congruential random number generator obj.
372 * @returns true if the two objects are equal, false otherwise.
375 operator==(const linear_congruential& __lhs,
376 const linear_congruential& __rhs)
377 { return __lhs._M_x == __rhs._M_x; }
380 * Compares two linear congruential random number generator
381 * objects of the same type for inequality.
383 * @param __lhs A linear congruential random number generator object.
384 * @param __rhs Another linear congruential random number generator obj.
386 * @returns true if the two objects are not equal, false otherwise.
389 operator!=(const linear_congruential& __lhs,
390 const linear_congruential& __rhs)
391 { return !(__lhs == __rhs); }
394 * Writes the textual representation of the state x(i) of x to @p __os.
396 * @param __os The output stream.
397 * @param __lcr A % linear_congruential random number generator.
400 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
402 typename _CharT, typename _Traits>
403 friend std::basic_ostream<_CharT, _Traits>&
404 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
405 const linear_congruential<_UIntType1, __a1, __c1,
409 * Sets the state of the engine by reading its textual
410 * representation from @p __is.
412 * The textual representation must have been previously written using an
413 * output stream whose imbued locale and whose type's template
414 * specialization arguments _CharT and _Traits were the same as those of
417 * @param __is The input stream.
418 * @param __lcr A % linear_congruential random number generator.
421 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
423 typename _CharT, typename _Traits>
424 friend std::basic_istream<_CharT, _Traits>&
425 operator>>(std::basic_istream<_CharT, _Traits>& __is,
426 linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
431 seed(_Gen& __g, true_type)
432 { return seed(static_cast<unsigned long>(__g)); }
436 seed(_Gen& __g, false_type);
442 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
444 typedef linear_congruential<unsigned long, 16807, 0, 2147483647> minstd_rand0;
447 * An alternative LCR (Lehmer Generator function) .
449 typedef linear_congruential<unsigned long, 48271, 0, 2147483647> minstd_rand;
453 * A generalized feedback shift register discrete random number generator.
455 * This algorithm avoids multiplication and division and is designed to be
456 * friendly to a pipelined architecture. If the parameters are chosen
457 * correctly, this generator will produce numbers with a very long period and
458 * fairly good apparent entropy, although still not cryptographically strong.
460 * The best way to use this generator is with the predefined mt19937 class.
462 * This algorithm was originally invented by Makoto Matsumoto and
465 * @var word_size The number of bits in each element of the state vector.
466 * @var state_size The degree of recursion.
467 * @var shift_size The period parameter.
468 * @var mask_bits The separation point bit index.
469 * @var parameter_a The last row of the twist matrix.
470 * @var output_u The first right-shift tempering matrix parameter.
471 * @var output_s The first left-shift tempering matrix parameter.
472 * @var output_b The first left-shift tempering matrix mask.
473 * @var output_t The second left-shift tempering matrix parameter.
474 * @var output_c The second left-shift tempering matrix mask.
475 * @var output_l The second right-shift tempering matrix parameter.
477 template<class _UIntType, int __w, int __n, int __m, int __r,
478 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
479 _UIntType __c, int __l>
480 class mersenne_twister
482 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
486 typedef _UIntType result_type;
489 static const int word_size = __w;
490 static const int state_size = __n;
491 static const int shift_size = __m;
492 static const int mask_bits = __r;
493 static const _UIntType parameter_a = __a;
494 static const int output_u = __u;
495 static const int output_s = __s;
496 static const _UIntType output_b = __b;
497 static const int output_t = __t;
498 static const _UIntType output_c = __c;
499 static const int output_l = __l;
501 // constructors and member function
506 mersenne_twister(unsigned long __value)
510 mersenne_twister(_Gen& __g)
518 seed(unsigned long __value);
523 { seed(__g, typename is_fundamental<_Gen>::type()); }
531 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
537 * Compares two % mersenne_twister random number generator objects of
538 * the same type for equality.
540 * @param __lhs A % mersenne_twister random number generator object.
541 * @param __rhs Another % mersenne_twister random number generator
544 * @returns true if the two objects are equal, false otherwise.
547 operator==(const mersenne_twister& __lhs,
548 const mersenne_twister& __rhs)
549 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
552 * Compares two % mersenne_twister random number generator objects of
553 * the same type for inequality.
555 * @param __lhs A % mersenne_twister random number generator object.
556 * @param __rhs Another % mersenne_twister random number generator
559 * @returns true if the two objects are not equal, false otherwise.
562 operator!=(const mersenne_twister& __lhs,
563 const mersenne_twister& __rhs)
564 { return !(__lhs == __rhs); }
567 * Inserts the current state of a % mersenne_twister random number
568 * generator engine @p __x into the output stream @p __os.
570 * @param __os An output stream.
571 * @param __x A % mersenne_twister random number generator engine.
573 * @returns The output stream with the state of @p __x inserted or in
576 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
577 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
578 _UIntType1 __c1, int __l1,
579 typename _CharT, typename _Traits>
580 friend std::basic_ostream<_CharT, _Traits>&
581 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
582 const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
583 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
586 * Extracts the current state of a % mersenne_twister random number
587 * generator engine @p __x from the input stream @p __is.
589 * @param __is An input stream.
590 * @param __x A % mersenne_twister random number generator engine.
592 * @returns The input stream with the state of @p __x extracted or in
595 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
596 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
597 _UIntType1 __c1, int __l1,
598 typename _CharT, typename _Traits>
599 friend std::basic_istream<_CharT, _Traits>&
600 operator>>(std::basic_istream<_CharT, _Traits>& __is,
601 mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
602 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
607 seed(_Gen& __g, true_type)
608 { return seed(static_cast<unsigned long>(__g)); }
612 seed(_Gen& __g, false_type);
614 _UIntType _M_x[state_size];
619 * The classic Mersenne Twister.
622 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
623 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
624 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
626 typedef mersenne_twister<
627 unsigned long, 32, 624, 397, 31,
635 * @brief The Marsaglia-Zaman generator.
637 * This is a model of a Generalized Fibonacci discrete random number
638 * generator, sometimes referred to as the SWC generator.
640 * A discrete random number generator that produces pseudorandom
641 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
642 * carry_{i-1}) \bmod m @f$.
644 * The size of the state is @f$ r @f$
645 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
647 * N1688[4.13] says "the template parameter _IntType shall denote an integral
648 * type large enough to store values up to m."
650 * @var _M_x The state of the generator. This is a ring buffer.
651 * @var _M_carry The carry.
652 * @var _M_p Current index of x(i - r).
654 template<typename _IntType, _IntType __m, int __s, int __r>
655 class subtract_with_carry
657 __glibcxx_class_requires(_IntType, _IntegerConcept)
660 /** The type of the generated random value. */
661 typedef _IntType result_type;
664 static const _IntType modulus = __m;
665 static const int long_lag = __r;
666 static const int short_lag = __s;
669 * Constructs a default-initialized % subtract_with_carry random number
672 subtract_with_carry()
676 * Constructs an explicitly seeded % subtract_with_carry random number
680 subtract_with_carry(unsigned long __value)
681 { this->seed(__value); }
684 * Constructs a %subtract_with_carry random number generator engine
685 * seeded from the generator function @p __g.
687 * @param __g The seed generator function.
690 subtract_with_carry(_Gen& __g)
694 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
696 * N1688[4.19] modifies this as follows. If @p __value == 0,
697 * sets value to 19780503. In any case, with a linear
698 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
699 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
700 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
701 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
702 * set carry to 1, otherwise sets carry to 0.
705 seed(unsigned long __value = 19780503);
708 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
709 * random number generator.
714 { seed(__g, typename is_fundamental<_Gen>::type()); }
717 * Gets the inclusive minimum value of the range of random integers
718 * returned by this generator.
725 * Gets the inclusive maximum value of the range of random integers
726 * returned by this generator.
730 { return this->modulus - 1; }
733 * Gets the next random number in the sequence.
739 * Compares two % subtract_with_carry random number generator objects of
740 * the same type for equality.
742 * @param __lhs A % subtract_with_carry random number generator object.
743 * @param __rhs Another % subtract_with_carry random number generator
746 * @returns true if the two objects are equal, false otherwise.
749 operator==(const subtract_with_carry& __lhs,
750 const subtract_with_carry& __rhs)
751 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
754 * Compares two % subtract_with_carry random number generator objects of
755 * the same type for inequality.
757 * @param __lhs A % subtract_with_carry random number generator object.
758 * @param __rhs Another % subtract_with_carry random number generator
761 * @returns true if the two objects are not equal, false otherwise.
764 operator!=(const subtract_with_carry& __lhs,
765 const subtract_with_carry& __rhs)
766 { return !(__lhs == __rhs); }
769 * Inserts the current state of a % subtract_with_carry random number
770 * generator engine @p __x into the output stream @p __os.
772 * @param __os An output stream.
773 * @param __x A % subtract_with_carry random number generator engine.
775 * @returns The output stream with the state of @p __x inserted or in
778 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
779 typename _CharT, typename _Traits>
780 friend std::basic_ostream<_CharT, _Traits>&
781 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
782 const subtract_with_carry<_IntType1, __m1, __s1,
786 * Extracts the current state of a % subtract_with_carry random number
787 * generator engine @p __x from the input stream @p __is.
789 * @param __is An input stream.
790 * @param __x A % subtract_with_carry random number generator engine.
792 * @returns The input stream with the state of @p __x extracted or in
795 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
796 typename _CharT, typename _Traits>
797 friend std::basic_istream<_CharT, _Traits>&
798 operator>>(std::basic_istream<_CharT, _Traits>& __is,
799 subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
804 seed(_Gen& __g, true_type)
805 { return seed(static_cast<unsigned long>(__g)); }
809 seed(_Gen& __g, false_type);
811 typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
813 _UIntType _M_x[long_lag];
820 * @brief The Marsaglia-Zaman generator (floats version).
822 * @var _M_x The state of the generator. This is a ring buffer.
823 * @var _M_carry The carry.
824 * @var _M_p Current index of x(i - r).
825 * @var _M_npows Precomputed negative powers of 2.
827 template<typename _RealType, int __w, int __s, int __r>
828 class subtract_with_carry_01
831 /** The type of the generated random value. */
832 typedef _RealType result_type;
835 static const int word_size = __w;
836 static const int long_lag = __r;
837 static const int short_lag = __s;
840 * Constructs a default-initialized % subtract_with_carry_01 random
843 subtract_with_carry_01()
846 _M_initialize_npows();
850 * Constructs an explicitly seeded % subtract_with_carry_01 random number
854 subtract_with_carry_01(unsigned long __value)
857 _M_initialize_npows();
861 * Constructs a % subtract_with_carry_01 random number generator engine
862 * seeded from the generator function @p __g.
864 * @param __g The seed generator function.
867 subtract_with_carry_01(_Gen& __g)
870 _M_initialize_npows();
874 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
877 seed(unsigned long __value = 19780503);
880 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
881 * random number generator.
886 { seed(__g, typename is_fundamental<_Gen>::type()); }
889 * Gets the minimum value of the range of random floats
890 * returned by this generator.
897 * Gets the maximum value of the range of random floats
898 * returned by this generator.
905 * Gets the next random number in the sequence.
911 * Compares two % subtract_with_carry_01 random number generator objects
912 * of the same type for equality.
914 * @param __lhs A % subtract_with_carry_01 random number
916 * @param __rhs Another % subtract_with_carry_01 random number generator
919 * @returns true if the two objects are equal, false otherwise.
922 operator==(const subtract_with_carry_01& __lhs,
923 const subtract_with_carry_01& __rhs)
925 for (int __i = 0; __i < long_lag; ++__i)
926 if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
933 * Compares two % subtract_with_carry_01 random number generator objects
934 * of the same type for inequality.
936 * @param __lhs A % subtract_with_carry_01 random number
939 * @param __rhs Another % subtract_with_carry_01 random number generator
942 * @returns true if the two objects are not equal, false otherwise.
945 operator!=(const subtract_with_carry_01& __lhs,
946 const subtract_with_carry_01& __rhs)
947 { return !(__lhs == __rhs); }
950 * Inserts the current state of a % subtract_with_carry_01 random number
951 * generator engine @p __x into the output stream @p __os.
953 * @param __os An output stream.
954 * @param __x A % subtract_with_carry_01 random number generator engine.
956 * @returns The output stream with the state of @p __x inserted or in
959 template<typename _RealType1, int __w1, int __s1, int __r1,
960 typename _CharT, typename _Traits>
961 friend std::basic_ostream<_CharT, _Traits>&
962 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
963 const subtract_with_carry_01<_RealType1, __w1, __s1,
967 * Extracts the current state of a % subtract_with_carry_01 random number
968 * generator engine @p __x from the input stream @p __is.
970 * @param __is An input stream.
971 * @param __x A % subtract_with_carry_01 random number generator engine.
973 * @returns The input stream with the state of @p __x extracted or in
976 template<typename _RealType1, int __w1, int __s1, int __r1,
977 typename _CharT, typename _Traits>
978 friend std::basic_istream<_CharT, _Traits>&
979 operator>>(std::basic_istream<_CharT, _Traits>& __is,
980 subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
985 seed(_Gen& __g, true_type)
986 { return seed(static_cast<unsigned long>(__g)); }
990 seed(_Gen& __g, false_type);
993 _M_initialize_npows();
995 static const int __n = (__w + 31) / 32;
997 typedef __detail::_UInt32Type _UInt32Type;
998 _UInt32Type _M_x[long_lag][__n];
999 _RealType _M_npows[__n];
1000 _UInt32Type _M_carry;
1004 typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
1006 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1007 // 508. Bad parameters for ranlux64_base_01.
1008 typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
1012 * Produces random numbers from some base engine by discarding blocks of
1015 * 0 <= @p __r <= @p __p
1017 template<class _UniformRandomNumberGenerator, int __p, int __r>
1020 // __glibcxx_class_requires(typename base_type::result_type,
1021 // ArithmeticTypeConcept)
1024 /** The type of the underlying generator engine. */
1025 typedef _UniformRandomNumberGenerator base_type;
1026 /** The type of the generated random value. */
1027 typedef typename base_type::result_type result_type;
1030 static const int block_size = __p;
1031 static const int used_block = __r;
1034 * Constructs a default %discard_block engine.
1036 * The underlying engine is default constructed as well.
1042 * Copy constructs a %discard_block engine.
1044 * Copies an existing base class random number generator.
1045 * @param rng An existing (base class) engine object.
1048 discard_block(const base_type& __rng)
1049 : _M_b(__rng), _M_n(0) { }
1052 * Seed constructs a %discard_block engine.
1054 * Constructs the underlying generator engine seeded with @p __s.
1055 * @param __s A seed value for the base class engine.
1058 discard_block(unsigned long __s)
1059 : _M_b(__s), _M_n(0) { }
1062 * Generator construct a %discard_block engine.
1064 * @param __g A seed generator function.
1066 template<class _Gen>
1067 discard_block(_Gen& __g)
1068 : _M_b(__g), _M_n(0) { }
1071 * Reseeds the %discard_block object with the default seed for the
1072 * underlying base class generator engine.
1081 * Reseeds the %discard_block object with the given seed generator
1083 * @param __g A seed generator function.
1085 template<class _Gen>
1086 void seed(_Gen& __g)
1093 * Gets a const reference to the underlying generator engine object.
1100 * Gets the minimum value in the generated random number range.
1104 { return _M_b.min(); }
1107 * Gets the maximum value in the generated random number range.
1111 { return _M_b.max(); }
1114 * Gets the next value in the generated random number sequence.
1120 * Compares two %discard_block random number generator objects of
1121 * the same type for equality.
1123 * @param __lhs A %discard_block random number generator object.
1124 * @param __rhs Another %discard_block random number generator
1127 * @returns true if the two objects are equal, false otherwise.
1130 operator==(const discard_block& __lhs, const discard_block& __rhs)
1131 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
1134 * Compares two %discard_block random number generator objects of
1135 * the same type for inequality.
1137 * @param __lhs A %discard_block random number generator object.
1138 * @param __rhs Another %discard_block random number generator
1141 * @returns true if the two objects are not equal, false otherwise.
1144 operator!=(const discard_block& __lhs, const discard_block& __rhs)
1145 { return !(__lhs == __rhs); }
1148 * Inserts the current state of a %discard_block random number
1149 * generator engine @p __x into the output stream @p __os.
1151 * @param __os An output stream.
1152 * @param __x A %discard_block random number generator engine.
1154 * @returns The output stream with the state of @p __x inserted or in
1157 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1158 typename _CharT, typename _Traits>
1159 friend std::basic_ostream<_CharT, _Traits>&
1160 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1161 const discard_block<_UniformRandomNumberGenerator1,
1165 * Extracts the current state of a % subtract_with_carry random number
1166 * generator engine @p __x from the input stream @p __is.
1168 * @param __is An input stream.
1169 * @param __x A %discard_block random number generator engine.
1171 * @returns The input stream with the state of @p __x extracted or in
1174 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1175 typename _CharT, typename _Traits>
1176 friend std::basic_istream<_CharT, _Traits>&
1177 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1178 discard_block<_UniformRandomNumberGenerator1,
1188 * James's luxury-level-3 integer adaptation of Luescher's generator.
1190 typedef discard_block<
1191 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1197 * James's luxury-level-4 integer adaptation of Luescher's generator.
1199 typedef discard_block<
1200 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1205 typedef discard_block<
1206 subtract_with_carry_01<float, 24, 10, 24>,
1211 typedef discard_block<
1212 subtract_with_carry_01<float, 24, 10, 24>,
1219 * A random number generator adaptor class that combines two random number
1220 * generator engines into a single output sequence.
1222 template<class _UniformRandomNumberGenerator1, int __s1,
1223 class _UniformRandomNumberGenerator2, int __s2>
1226 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1227 // result_type, ArithmeticTypeConcept)
1228 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1229 // result_type, ArithmeticTypeConcept)
1232 /** The type of the first underlying generator engine. */
1233 typedef _UniformRandomNumberGenerator1 base1_type;
1234 /** The type of the second underlying generator engine. */
1235 typedef _UniformRandomNumberGenerator2 base2_type;
1238 typedef typename base1_type::result_type _Result_type1;
1239 typedef typename base2_type::result_type _Result_type2;
1242 /** The type of the generated random value. */
1243 typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1)
1244 > sizeof(_Result_type2)),
1245 _Result_type1, _Result_type2>::__type result_type;
1248 static const int shift1 = __s1;
1249 static const int shift2 = __s2;
1251 // constructors and member function
1254 { _M_initialize_max(); }
1256 xor_combine(const base1_type& __rng1, const base2_type& __rng2)
1257 : _M_b1(__rng1), _M_b2(__rng2)
1258 { _M_initialize_max(); }
1260 xor_combine(unsigned long __s)
1261 : _M_b1(__s), _M_b2(__s + 1)
1262 { _M_initialize_max(); }
1264 template<class _Gen>
1265 xor_combine(_Gen& __g)
1266 : _M_b1(__g), _M_b2(__g)
1267 { _M_initialize_max(); }
1276 template<class _Gen>
1301 * Gets the next random number in the sequence.
1303 // NB: Not exactly the TR1 formula, per N2079 instead.
1307 return ((result_type(_M_b1() - _M_b1.min()) << shift1)
1308 ^ (result_type(_M_b2() - _M_b2.min()) << shift2));
1312 * Compares two %xor_combine random number generator objects of
1313 * the same type for equality.
1315 * @param __lhs A %xor_combine random number generator object.
1316 * @param __rhs Another %xor_combine random number generator
1319 * @returns true if the two objects are equal, false otherwise.
1322 operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1324 return (__lhs.base1() == __rhs.base1())
1325 && (__lhs.base2() == __rhs.base2());
1329 * Compares two %xor_combine random number generator objects of
1330 * the same type for inequality.
1332 * @param __lhs A %xor_combine random number generator object.
1333 * @param __rhs Another %xor_combine random number generator
1336 * @returns true if the two objects are not equal, false otherwise.
1339 operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1340 { return !(__lhs == __rhs); }
1343 * Inserts the current state of a %xor_combine random number
1344 * generator engine @p __x into the output stream @p __os.
1346 * @param __os An output stream.
1347 * @param __x A %xor_combine random number generator engine.
1349 * @returns The output stream with the state of @p __x inserted or in
1352 template<class _UniformRandomNumberGenerator11, int __s11,
1353 class _UniformRandomNumberGenerator21, int __s21,
1354 typename _CharT, typename _Traits>
1355 friend std::basic_ostream<_CharT, _Traits>&
1356 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1357 const xor_combine<_UniformRandomNumberGenerator11, __s11,
1358 _UniformRandomNumberGenerator21, __s21>& __x);
1361 * Extracts the current state of a %xor_combine random number
1362 * generator engine @p __x from the input stream @p __is.
1364 * @param __is An input stream.
1365 * @param __x A %xor_combine random number generator engine.
1367 * @returns The input stream with the state of @p __x extracted or in
1370 template<class _UniformRandomNumberGenerator11, int __s11,
1371 class _UniformRandomNumberGenerator21, int __s21,
1372 typename _CharT, typename _Traits>
1373 friend std::basic_istream<_CharT, _Traits>&
1374 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1375 xor_combine<_UniformRandomNumberGenerator11, __s11,
1376 _UniformRandomNumberGenerator21, __s21>& __x);
1380 _M_initialize_max();
1383 _M_initialize_max_aux(result_type, result_type, int);
1392 * A standard interface to a platform-specific non-deterministic
1393 * random number generator (if any are available).
1399 typedef unsigned int result_type;
1401 // constructors, destructors and member functions
1403 #ifdef _GLIBCXX_USE_RANDOM_TR1
1406 random_device(const std::string& __token = "/dev/urandom")
1408 if ((__token != "/dev/urandom" && __token != "/dev/random")
1409 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1410 std::__throw_runtime_error(__N("random_device::"
1411 "random_device(const std::string&)"));
1415 { std::fclose(_M_file); }
1420 random_device(const std::string& __token = "mt19937")
1421 : _M_mt(_M_strtoul(__token)) { }
1424 static unsigned long
1425 _M_strtoul(const std::string& __str)
1427 unsigned long __ret = 5489UL;
1428 if (__str != "mt19937")
1430 const char* __nptr = __str.c_str();
1432 __ret = std::strtoul(__nptr, &__endptr, 0);
1433 if (*__nptr == '\0' || *__endptr != '\0')
1434 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1435 "(const std::string&)"));
1446 { return std::numeric_limits<result_type>::min(); }
1450 { return std::numeric_limits<result_type>::max(); }
1459 #ifdef _GLIBCXX_USE_RANDOM_TR1
1461 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1470 random_device(const random_device&);
1471 void operator=(const random_device&);
1473 #ifdef _GLIBCXX_USE_RANDOM_TR1
1480 /* @} */ // group tr1_random_generators
1483 * @addtogroup tr1_random_distributions Random Number Distributions
1484 * @ingroup tr1_random
1489 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1490 * @ingroup tr1_random_distributions
1495 * @brief Uniform discrete distribution for random numbers.
1496 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1497 * probability throughout the range.
1499 template<typename _IntType = int>
1502 __glibcxx_class_requires(_IntType, _IntegerConcept)
1505 /** The type of the parameters of the distribution. */
1506 typedef _IntType input_type;
1507 /** The type of the range of the distribution. */
1508 typedef _IntType result_type;
1512 * Constructs a uniform distribution object.
1515 uniform_int(_IntType __min = 0, _IntType __max = 9)
1516 : _M_min(__min), _M_max(__max)
1518 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1522 * Gets the inclusive lower bound of the distribution range.
1529 * Gets the inclusive upper bound of the distribution range.
1536 * Resets the distribution state.
1538 * Does nothing for the uniform integer distribution.
1544 * Gets a uniformly distributed random number in the range
1547 template<typename _UniformRandomNumberGenerator>
1549 operator()(_UniformRandomNumberGenerator& __urng)
1551 typedef typename _UniformRandomNumberGenerator::result_type
1553 return _M_call(__urng, _M_min, _M_max,
1554 typename is_integral<_UResult_type>::type());
1558 * Gets a uniform random number in the range @f$[0, n)@f$.
1560 * This function is aimed at use with std::random_shuffle.
1562 template<typename _UniformRandomNumberGenerator>
1564 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1566 typedef typename _UniformRandomNumberGenerator::result_type
1568 return _M_call(__urng, 0, __n - 1,
1569 typename is_integral<_UResult_type>::type());
1573 * Inserts a %uniform_int random number distribution @p __x into the
1574 * output stream @p os.
1576 * @param __os An output stream.
1577 * @param __x A %uniform_int random number distribution.
1579 * @returns The output stream with the state of @p __x inserted or in
1582 template<typename _IntType1, typename _CharT, typename _Traits>
1583 friend std::basic_ostream<_CharT, _Traits>&
1584 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1585 const uniform_int<_IntType1>& __x);
1588 * Extracts a %uniform_int random number distribution
1589 * @p __x from the input stream @p __is.
1591 * @param __is An input stream.
1592 * @param __x A %uniform_int random number generator engine.
1594 * @returns The input stream with @p __x extracted or in an error state.
1596 template<typename _IntType1, typename _CharT, typename _Traits>
1597 friend std::basic_istream<_CharT, _Traits>&
1598 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1599 uniform_int<_IntType1>& __x);
1602 template<typename _UniformRandomNumberGenerator>
1604 _M_call(_UniformRandomNumberGenerator& __urng,
1605 result_type __min, result_type __max, true_type);
1607 template<typename _UniformRandomNumberGenerator>
1609 _M_call(_UniformRandomNumberGenerator& __urng,
1610 result_type __min, result_type __max, false_type)
1612 return result_type((__urng() - __urng.min())
1613 / (__urng.max() - __urng.min())
1614 * (__max - __min + 1)) + __min;
1623 * @brief A Bernoulli random number distribution.
1625 * Generates a sequence of true and false values with likelihood @f$ p @f$
1626 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1628 class bernoulli_distribution
1631 typedef int input_type;
1632 typedef bool result_type;
1636 * Constructs a Bernoulli distribution with likelihood @p p.
1638 * @param __p [IN] The likelihood of a true result being returned. Must
1639 * be in the interval @f$ [0, 1] @f$.
1642 bernoulli_distribution(double __p = 0.5)
1645 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1649 * Gets the @p p parameter of the distribution.
1656 * Resets the distribution state.
1658 * Does nothing for a Bernoulli distribution.
1664 * Gets the next value in the Bernoullian sequence.
1666 template<class _UniformRandomNumberGenerator>
1668 operator()(_UniformRandomNumberGenerator& __urng)
1670 if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
1676 * Inserts a %bernoulli_distribution random number distribution
1677 * @p __x into the output stream @p __os.
1679 * @param __os An output stream.
1680 * @param __x A %bernoulli_distribution random number distribution.
1682 * @returns The output stream with the state of @p __x inserted or in
1685 template<typename _CharT, typename _Traits>
1686 friend std::basic_ostream<_CharT, _Traits>&
1687 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1688 const bernoulli_distribution& __x);
1691 * Extracts a %bernoulli_distribution random number distribution
1692 * @p __x from the input stream @p __is.
1694 * @param __is An input stream.
1695 * @param __x A %bernoulli_distribution random number generator engine.
1697 * @returns The input stream with @p __x extracted or in an error state.
1699 template<typename _CharT, typename _Traits>
1700 friend std::basic_istream<_CharT, _Traits>&
1701 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1702 bernoulli_distribution& __x)
1703 { return __is >> __x._M_p; }
1711 * @brief A discrete geometric random number distribution.
1713 * The formula for the geometric probability mass function is
1714 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1717 template<typename _IntType = int, typename _RealType = double>
1718 class geometric_distribution
1722 typedef _RealType input_type;
1723 typedef _IntType result_type;
1725 // constructors and member function
1727 geometric_distribution(const _RealType& __p = _RealType(0.5))
1730 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
1735 * Gets the distribution parameter @p p.
1744 template<class _UniformRandomNumberGenerator>
1746 operator()(_UniformRandomNumberGenerator& __urng);
1749 * Inserts a %geometric_distribution random number distribution
1750 * @p __x into the output stream @p __os.
1752 * @param __os An output stream.
1753 * @param __x A %geometric_distribution random number distribution.
1755 * @returns The output stream with the state of @p __x inserted or in
1758 template<typename _IntType1, typename _RealType1,
1759 typename _CharT, typename _Traits>
1760 friend std::basic_ostream<_CharT, _Traits>&
1761 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1762 const geometric_distribution<_IntType1, _RealType1>& __x);
1765 * Extracts a %geometric_distribution random number distribution
1766 * @p __x from the input stream @p __is.
1768 * @param __is An input stream.
1769 * @param __x A %geometric_distribution random number generator engine.
1771 * @returns The input stream with @p __x extracted or in an error state.
1773 template<typename _CharT, typename _Traits>
1774 friend std::basic_istream<_CharT, _Traits>&
1775 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1776 geometric_distribution& __x)
1779 __x._M_initialize();
1786 { _M_log_p = std::log(_M_p); }
1793 template<typename _RealType>
1794 class normal_distribution;
1797 * @brief A discrete Poisson random number distribution.
1799 * The formula for the Poisson probability mass function is
1800 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1801 * parameter of the distribution.
1803 template<typename _IntType = int, typename _RealType = double>
1804 class poisson_distribution
1808 typedef _RealType input_type;
1809 typedef _IntType result_type;
1811 // constructors and member function
1813 poisson_distribution(const _RealType& __mean = _RealType(1))
1814 : _M_mean(__mean), _M_nd()
1816 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
1821 * Gets the distribution parameter @p mean.
1831 template<class _UniformRandomNumberGenerator>
1833 operator()(_UniformRandomNumberGenerator& __urng);
1836 * Inserts a %poisson_distribution random number distribution
1837 * @p __x into the output stream @p __os.
1839 * @param __os An output stream.
1840 * @param __x A %poisson_distribution random number distribution.
1842 * @returns The output stream with the state of @p __x inserted or in
1845 template<typename _IntType1, typename _RealType1,
1846 typename _CharT, typename _Traits>
1847 friend std::basic_ostream<_CharT, _Traits>&
1848 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1849 const poisson_distribution<_IntType1, _RealType1>& __x);
1852 * Extracts a %poisson_distribution random number distribution
1853 * @p __x from the input stream @p __is.
1855 * @param __is An input stream.
1856 * @param __x A %poisson_distribution random number generator engine.
1858 * @returns The input stream with @p __x extracted or in an error state.
1860 template<typename _IntType1, typename _RealType1,
1861 typename _CharT, typename _Traits>
1862 friend std::basic_istream<_CharT, _Traits>&
1863 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1864 poisson_distribution<_IntType1, _RealType1>& __x);
1870 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1871 normal_distribution<_RealType> _M_nd;
1875 // Hosts either log(mean) or the threshold of the simple method.
1876 _RealType _M_lm_thr;
1877 #if _GLIBCXX_USE_C99_MATH_TR1
1878 _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
1884 * @brief A discrete binomial random number distribution.
1886 * The formula for the binomial probability mass function is
1887 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1888 * and @f$ p @f$ are the parameters of the distribution.
1890 template<typename _IntType = int, typename _RealType = double>
1891 class binomial_distribution
1895 typedef _RealType input_type;
1896 typedef _IntType result_type;
1898 // constructors and member function
1900 binomial_distribution(_IntType __t = 1,
1901 const _RealType& __p = _RealType(0.5))
1902 : _M_t(__t), _M_p(__p), _M_nd()
1904 _GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
1909 * Gets the distribution @p t parameter.
1916 * Gets the distribution @p p parameter.
1926 template<class _UniformRandomNumberGenerator>
1928 operator()(_UniformRandomNumberGenerator& __urng);
1931 * Inserts a %binomial_distribution random number distribution
1932 * @p __x into the output stream @p __os.
1934 * @param __os An output stream.
1935 * @param __x A %binomial_distribution random number distribution.
1937 * @returns The output stream with the state of @p __x inserted or in
1940 template<typename _IntType1, typename _RealType1,
1941 typename _CharT, typename _Traits>
1942 friend std::basic_ostream<_CharT, _Traits>&
1943 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1944 const binomial_distribution<_IntType1, _RealType1>& __x);
1947 * Extracts a %binomial_distribution random number distribution
1948 * @p __x from the input stream @p __is.
1950 * @param __is An input stream.
1951 * @param __x A %binomial_distribution random number generator engine.
1953 * @returns The input stream with @p __x extracted or in an error state.
1955 template<typename _IntType1, typename _RealType1,
1956 typename _CharT, typename _Traits>
1957 friend std::basic_istream<_CharT, _Traits>&
1958 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1959 binomial_distribution<_IntType1, _RealType1>& __x);
1965 template<class _UniformRandomNumberGenerator>
1967 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
1969 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1970 normal_distribution<_RealType> _M_nd;
1973 #if _GLIBCXX_USE_C99_MATH_TR1
1974 _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
1975 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
1983 /* @} */ // group tr1_random_distributions_discrete
1986 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
1987 * @ingroup tr1_random_distributions
1992 * @brief Uniform continuous distribution for random numbers.
1994 * A continuous random distribution on the range [min, max) with equal
1995 * probability throughout the range. The URNG should be real-valued and
1996 * deliver number in the range [0, 1).
1998 template<typename _RealType = double>
2003 typedef _RealType input_type;
2004 typedef _RealType result_type;
2008 * Constructs a uniform_real object.
2010 * @param __min [IN] The lower bound of the distribution.
2011 * @param __max [IN] The upper bound of the distribution.
2014 uniform_real(_RealType __min = _RealType(0),
2015 _RealType __max = _RealType(1))
2016 : _M_min(__min), _M_max(__max)
2018 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
2032 template<class _UniformRandomNumberGenerator>
2034 operator()(_UniformRandomNumberGenerator& __urng)
2035 { return (__urng() * (_M_max - _M_min)) + _M_min; }
2038 * Inserts a %uniform_real random number distribution @p __x into the
2039 * output stream @p __os.
2041 * @param __os An output stream.
2042 * @param __x A %uniform_real random number distribution.
2044 * @returns The output stream with the state of @p __x inserted or in
2047 template<typename _RealType1, typename _CharT, typename _Traits>
2048 friend std::basic_ostream<_CharT, _Traits>&
2049 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2050 const uniform_real<_RealType1>& __x);
2053 * Extracts a %uniform_real random number distribution
2054 * @p __x from the input stream @p __is.
2056 * @param __is An input stream.
2057 * @param __x A %uniform_real random number generator engine.
2059 * @returns The input stream with @p __x extracted or in an error state.
2061 template<typename _RealType1, typename _CharT, typename _Traits>
2062 friend std::basic_istream<_CharT, _Traits>&
2063 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2064 uniform_real<_RealType1>& __x);
2073 * @brief An exponential continuous distribution for random numbers.
2075 * The formula for the exponential probability mass function is
2076 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2078 * <table border=1 cellpadding=10 cellspacing=0>
2079 * <caption align=top>Distribution Statistics</caption>
2080 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2081 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2082 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2083 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2084 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2087 template<typename _RealType = double>
2088 class exponential_distribution
2092 typedef _RealType input_type;
2093 typedef _RealType result_type;
2097 * Constructs an exponential distribution with inverse scale parameter
2101 exponential_distribution(const result_type& __lambda = result_type(1))
2102 : _M_lambda(__lambda)
2104 _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
2108 * Gets the inverse scale parameter of the distribution.
2112 { return _M_lambda; }
2115 * Resets the distribution.
2117 * Has no effect on exponential distributions.
2122 template<class _UniformRandomNumberGenerator>
2124 operator()(_UniformRandomNumberGenerator& __urng)
2125 { return -std::log(__urng()) / _M_lambda; }
2128 * Inserts a %exponential_distribution random number distribution
2129 * @p __x into the output stream @p __os.
2131 * @param __os An output stream.
2132 * @param __x A %exponential_distribution random number distribution.
2134 * @returns The output stream with the state of @p __x inserted or in
2137 template<typename _RealType1, typename _CharT, typename _Traits>
2138 friend std::basic_ostream<_CharT, _Traits>&
2139 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2140 const exponential_distribution<_RealType1>& __x);
2143 * Extracts a %exponential_distribution random number distribution
2144 * @p __x from the input stream @p __is.
2146 * @param __is An input stream.
2147 * @param __x A %exponential_distribution random number
2150 * @returns The input stream with @p __x extracted or in an error state.
2152 template<typename _CharT, typename _Traits>
2153 friend std::basic_istream<_CharT, _Traits>&
2154 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2155 exponential_distribution& __x)
2156 { return __is >> __x._M_lambda; }
2159 result_type _M_lambda;
2164 * @brief A normal continuous distribution for random numbers.
2166 * The formula for the normal probability mass function is
2167 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2168 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2170 template<typename _RealType = double>
2171 class normal_distribution
2175 typedef _RealType input_type;
2176 typedef _RealType result_type;
2180 * Constructs a normal distribution with parameters @f$ mean @f$ and
2184 normal_distribution(const result_type& __mean = result_type(0),
2185 const result_type& __sigma = result_type(1))
2186 : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
2188 _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
2192 * Gets the mean of the distribution.
2199 * Gets the @f$ \sigma @f$ of the distribution.
2203 { return _M_sigma; }
2206 * Resets the distribution.
2210 { _M_saved_available = false; }
2212 template<class _UniformRandomNumberGenerator>
2214 operator()(_UniformRandomNumberGenerator& __urng);
2217 * Inserts a %normal_distribution random number distribution
2218 * @p __x into the output stream @p __os.
2220 * @param __os An output stream.
2221 * @param __x A %normal_distribution random number distribution.
2223 * @returns The output stream with the state of @p __x inserted or in
2226 template<typename _RealType1, typename _CharT, typename _Traits>
2227 friend std::basic_ostream<_CharT, _Traits>&
2228 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2229 const normal_distribution<_RealType1>& __x);
2232 * Extracts a %normal_distribution random number distribution
2233 * @p __x from the input stream @p __is.
2235 * @param __is An input stream.
2236 * @param __x A %normal_distribution random number generator engine.
2238 * @returns The input stream with @p __x extracted or in an error state.
2240 template<typename _RealType1, typename _CharT, typename _Traits>
2241 friend std::basic_istream<_CharT, _Traits>&
2242 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2243 normal_distribution<_RealType1>& __x);
2246 result_type _M_mean;
2247 result_type _M_sigma;
2248 result_type _M_saved;
2249 bool _M_saved_available;
2254 * @brief A gamma continuous distribution for random numbers.
2256 * The formula for the gamma probability mass function is
2257 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2259 template<typename _RealType = double>
2260 class gamma_distribution
2264 typedef _RealType input_type;
2265 typedef _RealType result_type;
2269 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2272 gamma_distribution(const result_type& __alpha_val = result_type(1))
2273 : _M_alpha(__alpha_val)
2275 _GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
2280 * Gets the @f$ \alpha @f$ of the distribution.
2284 { return _M_alpha; }
2287 * Resets the distribution.
2292 template<class _UniformRandomNumberGenerator>
2294 operator()(_UniformRandomNumberGenerator& __urng);
2297 * Inserts a %gamma_distribution random number distribution
2298 * @p __x into the output stream @p __os.
2300 * @param __os An output stream.
2301 * @param __x A %gamma_distribution random number distribution.
2303 * @returns The output stream with the state of @p __x inserted or in
2306 template<typename _RealType1, typename _CharT, typename _Traits>
2307 friend std::basic_ostream<_CharT, _Traits>&
2308 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2309 const gamma_distribution<_RealType1>& __x);
2312 * Extracts a %gamma_distribution random number distribution
2313 * @p __x from the input stream @p __is.
2315 * @param __is An input stream.
2316 * @param __x A %gamma_distribution random number generator engine.
2318 * @returns The input stream with @p __x extracted or in an error state.
2320 template<typename _CharT, typename _Traits>
2321 friend std::basic_istream<_CharT, _Traits>&
2322 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2323 gamma_distribution& __x)
2325 __is >> __x._M_alpha;
2326 __x._M_initialize();
2334 result_type _M_alpha;
2336 // Hosts either lambda of GB or d of modified Vaduva's.
2340 /* @} */ // group tr1_random_distributions_continuous
2341 /* @} */ // group tr1_random_distributions
2342 /* @} */ // group tr1_random
2344 _GLIBCXX_END_NAMESPACE_TR1
2347 #include <tr1_impl/random.tcc>