1 // random number generation -*- C++ -*-
3 // Copyright (C) 2006 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.
43 #include <tr1/type_traits>
45 #include <ext/type_traits.h>
46 #include <ext/numeric_traits.h>
47 #include <bits/concept_check.h>
48 #include <debug/debug.h>
52 _GLIBCXX_BEGIN_NAMESPACE(tr1)
54 // [5.1] Random number generation
57 * @addtogroup tr1_random Random Number Generation
58 * A facility for generating random numbers on selected distributions.
63 * Implementation-space details.
67 template<typename _UIntType, int __w,
68 bool = __w < std::numeric_limits<_UIntType>::digits>
70 { static const _UIntType __value = 0; };
72 template<typename _UIntType, int __w>
73 struct _Shift<_UIntType, __w, true>
74 { static const _UIntType __value = _UIntType(1) << __w; };
76 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
79 // Dispatch based on modulus value to prevent divide-by-zero compile-time
80 // errors when m == 0.
81 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
84 { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
86 typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
87 unsigned, unsigned long>::__type _UInt32Type;
90 * An adaptor class for converting the output of any Generator into
91 * the input for a specific Distribution.
93 template<typename _Engine, typename _Distribution>
96 typedef typename _Engine::result_type _Engine_result_type;
97 typedef typename _Distribution::input_type result_type;
100 _Adaptor(const _Engine& __g)
106 result_type __return_value = 0;
107 if (is_integral<_Engine_result_type>::value
108 && is_integral<result_type>::value)
109 __return_value = _M_g.min();
110 else if (!is_integral<result_type>::value)
111 __return_value = result_type(0);
112 return __return_value;
118 result_type __return_value = 0;
119 if (is_integral<_Engine_result_type>::value
120 && is_integral<result_type>::value)
121 __return_value = _M_g.max();
122 else if (!is_integral<result_type>::value)
123 __return_value = result_type(1);
124 return __return_value;
135 * Converts a value generated by the adapted random number generator into a
136 * value in the input domain for the dependent random number distribution.
138 * Because the type traits are compile time constants only the appropriate
139 * clause of the if statements will actually be emitted by the compiler.
141 template<typename _Engine, typename _Distribution>
142 typename _Adaptor<_Engine, _Distribution>::result_type
143 _Adaptor<_Engine, _Distribution>::
146 result_type __return_value = 0;
147 if (is_integral<_Engine_result_type>::value
148 && is_integral<result_type>::value)
149 __return_value = _M_g();
150 else if (is_integral<_Engine_result_type>::value
151 && !is_integral<result_type>::value)
152 __return_value = result_type(_M_g() - _M_g.min())
153 / result_type(_M_g.max() - _M_g.min() + result_type(1));
154 else if (!is_integral<_Engine_result_type>::value
155 && !is_integral<result_type>::value)
156 __return_value = result_type(_M_g() - _M_g.min())
157 / result_type(_M_g.max() - _M_g.min());
158 return __return_value;
160 } // namespace __detail
163 * Produces random numbers on a given disribution function using a un uniform
164 * random number generation engine.
166 * @todo the engine_value_type needs to be studied more carefully.
168 template<typename _Engine, typename _Dist>
169 class variate_generator
171 // Concept requirements.
172 __glibcxx_class_requires(_Engine, _CopyConstructibleConcept)
173 // __glibcxx_class_requires(_Engine, _EngineConcept)
174 // __glibcxx_class_requires(_Dist, _EngineConcept)
177 typedef _Engine engine_type;
178 typedef __detail::_Adaptor<_Engine, _Dist> engine_value_type;
179 typedef _Dist distribution_type;
180 typedef typename _Dist::result_type result_type;
182 // tr1:5.1.1 table 5.1 requirement
183 typedef typename __gnu_cxx::__enable_if<
184 is_arithmetic<result_type>::value, result_type>::__type _IsValidType;
187 * Constructs a variate generator with the uniform random number
188 * generator @p __eng for the random distribution @p __dist.
190 * @throws Any exceptions which may thrown by the copy constructors of
191 * the @p _Engine or @p _Dist objects.
193 variate_generator(engine_type __eng, distribution_type __dist)
194 : _M_engine(__eng), _M_dist(__dist) { }
197 * Gets the next generated value on the distribution.
201 { return _M_dist(_M_engine); }
206 template<typename _Tp>
208 operator()(_Tp __value)
209 { return _M_dist(_M_engine, __value); }
212 * Gets a reference to the underlying uniform random number generator
217 { return _M_engine; }
220 * Gets a const reference to the underlying uniform random number
223 const engine_value_type&
225 { return _M_engine; }
228 * Gets a reference to the underlying random distribution.
235 * Gets a const reference to the underlying random distribution.
237 const distribution_type&
242 * Gets the closed lower bound of the distribution interval.
246 { return this->distribution().min(); }
249 * Gets the closed upper bound of the distribution interval.
253 { return this->distribution().max(); }
256 engine_value_type _M_engine;
257 distribution_type _M_dist;
262 * @addtogroup tr1_random_generators Random Number Generators
263 * @ingroup tr1_random
265 * These classes define objects which provide random or pseudorandom
266 * numbers, either from a discrete or a continuous interval. The
267 * random number generator supplied as a part of this library are
268 * all uniform random number generators which provide a sequence of
269 * random number uniformly distributed over their range.
271 * A number generator is a function object with an operator() that
272 * takes zero arguments and returns a number.
274 * A compliant random number generator must satisy the following
275 * requirements. <table border=1 cellpadding=10 cellspacing=0>
276 * <caption align=top>Random Number Generator Requirements</caption>
277 * <tr><td>To be documented.</td></tr> </table>
283 * @brief A model of a linear congruential random number generator.
285 * A random number generator that produces pseudorandom numbers using the
286 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
288 * The template parameter @p _UIntType must be an unsigned integral type
289 * large enough to store values up to (__m-1). If the template parameter
290 * @p __m is 0, the modulus @p __m used is
291 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
292 * parameters @p __a and @p __c must be less than @p __m.
294 * The size of the state is @f$ 1 @f$.
296 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
297 class linear_congruential
299 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
300 // __glibcpp_class_requires(__a < __m && __c < __m)
303 /** The type of the generated random value. */
304 typedef _UIntType result_type;
306 /** The multiplier. */
307 static const _UIntType multiplier = __a;
309 static const _UIntType increment = __c;
311 static const _UIntType modulus = __m;
314 * Constructs a %linear_congruential random number generator engine with
315 * seed @p __s. The default seed value is 1.
317 * @param __s The initial seed value.
320 linear_congruential(unsigned long __x0 = 1)
321 { this->seed(__x0); }
324 * Constructs a %linear_congruential random number generator engine
325 * seeded from the generator function @p __g.
327 * @param __g The seed generator function.
330 linear_congruential(_Gen& __g)
334 * Reseeds the %linear_congruential random number generator engine
335 * sequence to the seed @g __s.
337 * @param __s The new seed.
340 seed(unsigned long __s = 1);
343 * Reseeds the %linear_congruential random number generator engine
344 * sequence using values from the generator function @p __g.
346 * @param __g the seed generator function.
351 { seed(__g, typename is_fundamental<_Gen>::type()); }
354 * Gets the smallest possible value in the output range.
356 * The minumum depends on the @p __c parameter: if it is zero, the
357 * minimum generated must be > 0, otherwise 0 is allowed.
361 { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
364 * Gets the largest possible value in the output range.
371 * Gets the next random number in the sequence.
377 * Compares two linear congruential random number generator
378 * objects of the same type for equality.
380 * @param __lhs A linear congruential random number generator object.
381 * @param __rhs Another linear congruential random number generator obj.
383 * @returns true if the two objects are equal, false otherwise.
386 operator==(const linear_congruential& __lhs,
387 const linear_congruential& __rhs)
388 { return __lhs._M_x == __rhs._M_x; }
391 * Compares two linear congruential random number generator
392 * objects of the same type for inequality.
394 * @param __lhs A linear congruential random number generator object.
395 * @param __rhs Another linear congruential random number generator obj.
397 * @returns true if the two objects are not equal, false otherwise.
400 operator!=(const linear_congruential& __lhs,
401 const linear_congruential& __rhs)
402 { return !(__lhs == __rhs); }
405 * Writes the textual representation of the state x(i) of x to @p __os.
407 * @param __os The output stream.
408 * @param __lcr A % linear_congruential random number generator.
411 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
413 typename _CharT, typename _Traits>
414 friend std::basic_ostream<_CharT, _Traits>&
415 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
416 const linear_congruential<_UIntType1, __a1, __c1,
420 * Sets the state of the engine by reading its textual
421 * representation from @p __is.
423 * The textual representation must have been previously written using an
424 * output stream whose imbued locale and whose type's template
425 * specialization arguments _CharT and _Traits were the same as those of
428 * @param __is The input stream.
429 * @param __lcr A % linear_congruential random number generator.
432 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
434 typename _CharT, typename _Traits>
435 friend std::basic_istream<_CharT, _Traits>&
436 operator>>(std::basic_istream<_CharT, _Traits>& __is,
437 linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
442 seed(_Gen& __g, true_type)
443 { return seed(static_cast<unsigned long>(__g)); }
447 seed(_Gen& __g, false_type);
453 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
455 typedef linear_congruential<unsigned long, 16807, 0, 2147483647> minstd_rand0;
458 * An alternative LCR (Lehmer Generator function) .
460 typedef linear_congruential<unsigned long, 48271, 0, 2147483647> minstd_rand;
464 * A generalized feedback shift register discrete random number generator.
466 * This algorithm avoind multiplication and division and is designed to be
467 * friendly to a pipelined architecture. If the parameters are chosen
468 * correctly, this generator will produce numbers with a very long period and
469 * fairly good apparent entropy, although still not cryptographically strong.
471 * The best way to use this generator is with the predefined mt19937 class.
473 * This algorithm was originally invented by Makoto Matsumoto and
476 * @var word_size The number of bits in each element of the state vector.
477 * @var state_size The degree of recursion.
478 * @var shift_size The period parameter.
479 * @var mask_bits The separation point bit index.
480 * @var parameter_a The last row of the twist matrix.
481 * @var output_u The first right-shift tempering matrix parameter.
482 * @var output_s The first left-shift tempering matrix parameter.
483 * @var output_b The first left-shift tempering matrix mask.
484 * @var output_t The second left-shift tempering matrix parameter.
485 * @var output_c The second left-shift tempering matrix mask.
486 * @var output_l The second right-shift tempering matrix parameter.
488 template<class _UIntType, int __w, int __n, int __m, int __r,
489 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
490 _UIntType __c, int __l>
491 class mersenne_twister
493 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
497 typedef _UIntType result_type;
500 static const int word_size = __w;
501 static const int state_size = __n;
502 static const int shift_size = __m;
503 static const int mask_bits = __r;
504 static const _UIntType parameter_a = __a;
505 static const int output_u = __u;
506 static const int output_s = __s;
507 static const _UIntType output_b = __b;
508 static const int output_t = __t;
509 static const _UIntType output_c = __c;
510 static const int output_l = __l;
512 // constructors and member function
517 mersenne_twister(unsigned long __value)
521 mersenne_twister(_Gen& __g)
529 seed(unsigned long __value);
534 { seed(__g, typename is_fundamental<_Gen>::type()); }
542 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
548 * Compares two % mersenne_twister random number generator objects of
549 * the same type for equality.
551 * @param __lhs A % mersenne_twister random number generator object.
552 * @param __rhs Another % mersenne_twister random number generator
555 * @returns true if the two objects are equal, false otherwise.
558 operator==(const mersenne_twister& __lhs,
559 const mersenne_twister& __rhs)
560 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
563 * Compares two % mersenne_twister random number generator objects of
564 * the same type for inequality.
566 * @param __lhs A % mersenne_twister random number generator object.
567 * @param __rhs Another % mersenne_twister random number generator
570 * @returns true if the two objects are not equal, false otherwise.
573 operator!=(const mersenne_twister& __lhs,
574 const mersenne_twister& __rhs)
575 { return !(__lhs == __rhs); }
578 * Inserts the current state of a % mersenne_twister random number
579 * generator engine @p __x into the output stream @p __os.
581 * @param __os An output stream.
582 * @param __x A % mersenne_twister random number generator engine.
584 * @returns The output stream with the state of @p __x inserted or in
587 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
588 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
589 _UIntType1 __c1, int __l1,
590 typename _CharT, typename _Traits>
591 friend std::basic_ostream<_CharT, _Traits>&
592 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
593 const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
594 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
597 * Extracts the current state of a % mersenne_twister random number
598 * generator engine @p __x from the input stream @p __is.
600 * @param __is An input stream.
601 * @param __x A % mersenne_twister random number generator engine.
603 * @returns The input stream with the state of @p __x extracted or in
606 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
607 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
608 _UIntType1 __c1, int __l1,
609 typename _CharT, typename _Traits>
610 friend std::basic_istream<_CharT, _Traits>&
611 operator>>(std::basic_istream<_CharT, _Traits>& __is,
612 mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
613 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
618 seed(_Gen& __g, true_type)
619 { return seed(static_cast<unsigned long>(__g)); }
623 seed(_Gen& __g, false_type);
625 _UIntType _M_x[state_size];
630 * The classic Mersenne Twister.
633 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
634 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
635 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
637 typedef mersenne_twister<
638 unsigned long, 32, 624, 397, 31,
646 * @brief The Marsaglia-Zaman generator.
648 * This is a model of a Generalized Fibonacci discrete random number
649 * generator, sometimes referred to as the SWC generator.
651 * A discrete random number generator that produces pseudorandom
652 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
653 * carry_{i-1}) \bmod m @f$.
655 * The size of the state is @f$ r @f$
656 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
658 * N1688[4.13] says "the template parameter _IntType shall denote an integral
659 * type large enough to store values up to m."
662 * @var _M_x The state of the generator. This is a ring buffer.
663 * @var _M_carry The carry.
664 * @var _M_p Current index of x(i - r).
667 template<typename _IntType, _IntType __m, int __s, int __r>
668 class subtract_with_carry
670 __glibcxx_class_requires(_IntType, _IntegerConcept)
673 /** The type of the generated random value. */
674 typedef _IntType result_type;
677 static const _IntType modulus = __m;
678 static const int long_lag = __r;
679 static const int short_lag = __s;
682 * Constructs a default-initialized % subtract_with_carry random number
685 subtract_with_carry()
689 * Constructs an explicitly seeded % subtract_with_carry random number
693 subtract_with_carry(unsigned long __value)
694 { this->seed(__value); }
697 * Constructs a %subtract_with_carry random number generator engine
698 * seeded from the generator function @p __g.
700 * @param __g The seed generator function.
703 subtract_with_carry(_Gen& __g)
707 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
709 * N1688[4.19] modifies this as follows. If @p __value == 0,
710 * sets value to 19780503. In any case, with a linear
711 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
712 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
713 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
714 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
715 * set carry to 1, otherwise sets carry to 0.
718 seed(unsigned long __value = 19780503);
721 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
722 * random number generator.
727 { seed(__g, typename is_fundamental<_Gen>::type()); }
730 * Gets the inclusive minimum value of the range of random integers
731 * returned by this generator.
738 * Gets the inclusive maximum value of the range of random integers
739 * returned by this generator.
743 { return this->modulus - 1; }
746 * Gets the next random number in the sequence.
752 * Compares two % subtract_with_carry random number generator objects of
753 * the same type for equality.
755 * @param __lhs A % subtract_with_carry random number generator object.
756 * @param __rhs Another % subtract_with_carry random number generator
759 * @returns true if the two objects are equal, false otherwise.
762 operator==(const subtract_with_carry& __lhs,
763 const subtract_with_carry& __rhs)
764 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
767 * Compares two % subtract_with_carry random number generator objects of
768 * the same type for inequality.
770 * @param __lhs A % subtract_with_carry random number generator object.
771 * @param __rhs Another % subtract_with_carry random number generator
774 * @returns true if the two objects are not equal, false otherwise.
777 operator!=(const subtract_with_carry& __lhs,
778 const subtract_with_carry& __rhs)
779 { return !(__lhs == __rhs); }
782 * Inserts the current state of a % subtract_with_carry random number
783 * generator engine @p __x into the output stream @p __os.
785 * @param __os An output stream.
786 * @param __x A % subtract_with_carry random number generator engine.
788 * @returns The output stream with the state of @p __x inserted or in
791 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
792 typename _CharT, typename _Traits>
793 friend std::basic_ostream<_CharT, _Traits>&
794 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
795 const subtract_with_carry<_IntType1, __m1, __s1,
799 * Extracts the current state of a % subtract_with_carry random number
800 * generator engine @p __x from the input stream @p __is.
802 * @param __is An input stream.
803 * @param __x A % subtract_with_carry random number generator engine.
805 * @returns The input stream with the state of @p __x extracted or in
808 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
809 typename _CharT, typename _Traits>
810 friend std::basic_istream<_CharT, _Traits>&
811 operator>>(std::basic_istream<_CharT, _Traits>& __is,
812 subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
817 seed(_Gen& __g, true_type)
818 { return seed(static_cast<unsigned long>(__g)); }
822 seed(_Gen& __g, false_type);
824 typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
826 _UIntType _M_x[long_lag];
833 * @brief The Marsaglia-Zaman generator (floats version).
836 * @var _M_x The state of the generator. This is a ring buffer.
837 * @var _M_carry The carry.
838 * @var _M_p Current index of x(i - r).
839 * @var _M_npows Precomputed negative powers of 2.
842 template<typename _RealType, int __w, int __s, int __r>
843 class subtract_with_carry_01
846 /** The type of the generated random value. */
847 typedef _RealType result_type;
850 static const int word_size = __w;
851 static const int long_lag = __r;
852 static const int short_lag = __s;
855 * Constructs a default-initialized % subtract_with_carry_01 random
858 subtract_with_carry_01()
861 _M_initialize_npows();
865 * Constructs an explicitly seeded % subtract_with_carry_01 random number
869 subtract_with_carry_01(unsigned long __value)
872 _M_initialize_npows();
876 * Constructs a % subtract_with_carry_01 random number generator engine
877 * seeded from the generator function @p __g.
879 * @param __g The seed generator function.
882 subtract_with_carry_01(_Gen& __g)
885 _M_initialize_npows();
889 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
892 seed(unsigned long __value = 19780503);
895 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
896 * random number generator.
901 { seed(__g, typename is_fundamental<_Gen>::type()); }
904 * Gets the minimum value of the range of random floats
905 * returned by this generator.
912 * Gets the maximum value of the range of random floats
913 * returned by this generator.
920 * Gets the next random number in the sequence.
926 * Compares two % subtract_with_carry_01 random number generator objects
927 * of the same type for equality.
929 * @param __lhs A % subtract_with_carry_01 random number
931 * @param __rhs Another % subtract_with_carry_01 random number generator
934 * @returns true if the two objects are equal, false otherwise.
937 operator==(const subtract_with_carry_01& __lhs,
938 const subtract_with_carry_01& __rhs)
940 for (int __i = 0; __i < long_lag; ++__i)
941 if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
948 * Compares two % subtract_with_carry_01 random number generator objects
949 * of the same type for inequality.
951 * @param __lhs A % subtract_with_carry_01 random number
954 * @param __rhs Another % subtract_with_carry_01 random number generator
957 * @returns true if the two objects are not equal, false otherwise.
960 operator!=(const subtract_with_carry_01& __lhs,
961 const subtract_with_carry_01& __rhs)
962 { return !(__lhs == __rhs); }
965 * Inserts the current state of a % subtract_with_carry_01 random number
966 * generator engine @p __x into the output stream @p __os.
968 * @param __os An output stream.
969 * @param __x A % subtract_with_carry_01 random number generator engine.
971 * @returns The output stream with the state of @p __x inserted or in
974 template<typename _RealType1, int __w1, int __s1, int __r1,
975 typename _CharT, typename _Traits>
976 friend std::basic_ostream<_CharT, _Traits>&
977 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
978 const subtract_with_carry_01<_RealType1, __w1, __s1,
982 * Extracts the current state of a % subtract_with_carry_01 random number
983 * generator engine @p __x from the input stream @p __is.
985 * @param __is An input stream.
986 * @param __x A % subtract_with_carry_01 random number generator engine.
988 * @returns The input stream with the state of @p __x extracted or in
991 template<typename _RealType1, int __w1, int __s1, int __r1,
992 typename _CharT, typename _Traits>
993 friend std::basic_istream<_CharT, _Traits>&
994 operator>>(std::basic_istream<_CharT, _Traits>& __is,
995 subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
1000 seed(_Gen& __g, true_type)
1001 { return seed(static_cast<unsigned long>(__g)); }
1003 template<class _Gen>
1005 seed(_Gen& __g, false_type);
1008 _M_initialize_npows();
1010 static const int __n = (__w + 31) / 32;
1012 typedef __detail::_UInt32Type _UInt32Type;
1013 _UInt32Type _M_x[long_lag][__n];
1014 _RealType _M_npows[__n];
1015 _UInt32Type _M_carry;
1019 typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
1021 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1022 // 508. Bad parameters for ranlux64_base_01.
1023 typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
1027 * Produces random numbers from some base engine by discarding blocks of
1030 * 0 <= @p __r <= @p __p
1032 template<class _UniformRandomNumberGenerator, int __p, int __r>
1035 // __glibcxx_class_requires(typename base_type::result_type,
1036 // ArithmeticTypeConcept)
1039 /** The type of the underlying generator engine. */
1040 typedef _UniformRandomNumberGenerator base_type;
1041 /** The type of the generated random value. */
1042 typedef typename base_type::result_type result_type;
1045 static const int block_size = __p;
1046 static const int used_block = __r;
1049 * Constructs a default %discard_block engine.
1051 * The underlying engine is default constructed as well.
1057 * Copy constructs a %discard_block engine.
1059 * Copies an existing base class random number geenerator.
1060 * @param rng An existing (base class) engine object.
1063 discard_block(const base_type& __rng)
1064 : _M_b(__rng), _M_n(0) { }
1067 * Seed constructs a %discard_block engine.
1069 * Constructs the underlying generator engine seeded with @p __s.
1070 * @param __s A seed value for the base class engine.
1073 discard_block(unsigned long __s)
1074 : _M_b(__s), _M_n(0) { }
1077 * Generator construct a %discard_block engine.
1079 * @param __g A seed generator function.
1081 template<class _Gen>
1082 discard_block(_Gen& __g)
1083 : _M_b(__g), _M_n(0) { }
1086 * Reseeds the %discard_block object with the default seed for the
1087 * underlying base class generator engine.
1096 * Reseeds the %discard_block object with the given seed generator
1098 * @param __g A seed generator function.
1100 template<class _Gen>
1101 void seed(_Gen& __g)
1108 * Gets a const reference to the underlying generator engine object.
1115 * Gets the minimum value in the generated random number range.
1119 { return _M_b.min(); }
1122 * Gets the maximum value in the generated random number range.
1126 { return _M_b.max(); }
1129 * Gets the next value in the generated random number sequence.
1135 * Compares two %discard_block random number generator objects of
1136 * the same type for equality.
1138 * @param __lhs A %discard_block random number generator object.
1139 * @param __rhs Another %discard_block random number generator
1142 * @returns true if the two objects are equal, false otherwise.
1145 operator==(const discard_block& __lhs, const discard_block& __rhs)
1146 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
1149 * Compares two %discard_block random number generator objects of
1150 * the same type for inequality.
1152 * @param __lhs A %discard_block random number generator object.
1153 * @param __rhs Another %discard_block random number generator
1156 * @returns true if the two objects are not equal, false otherwise.
1159 operator!=(const discard_block& __lhs, const discard_block& __rhs)
1160 { return !(__lhs == __rhs); }
1163 * Inserts the current state of a %discard_block random number
1164 * generator engine @p __x into the output stream @p __os.
1166 * @param __os An output stream.
1167 * @param __x A %discard_block random number generator engine.
1169 * @returns The output stream with the state of @p __x inserted or in
1172 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1173 typename _CharT, typename _Traits>
1174 friend std::basic_ostream<_CharT, _Traits>&
1175 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1176 const discard_block<_UniformRandomNumberGenerator1,
1180 * Extracts the current state of a % subtract_with_carry random number
1181 * generator engine @p __x from the input stream @p __is.
1183 * @param __is An input stream.
1184 * @param __x A %discard_block random number generator engine.
1186 * @returns The input stream with the state of @p __x extracted or in
1189 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1190 typename _CharT, typename _Traits>
1191 friend std::basic_istream<_CharT, _Traits>&
1192 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1193 discard_block<_UniformRandomNumberGenerator1,
1203 * James's luxury-level-3 integer adaptation of Luescher's generator.
1205 typedef discard_block<
1206 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1212 * James's luxury-level-4 integer adaptation of Luescher's generator.
1214 typedef discard_block<
1215 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1220 typedef discard_block<
1221 subtract_with_carry_01<float, 24, 10, 24>,
1226 typedef discard_block<
1227 subtract_with_carry_01<float, 24, 10, 24>,
1234 * A random number generator adaptor class that combines two random number
1235 * generator engines into a single output sequence.
1237 template<class _UniformRandomNumberGenerator1, int __s1,
1238 class _UniformRandomNumberGenerator2, int __s2>
1241 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1242 // result_type, ArithmeticTypeConcept)
1243 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1244 // result_type, ArithmeticTypeConcept)
1247 /** The type of the the first underlying generator engine. */
1248 typedef _UniformRandomNumberGenerator1 base1_type;
1249 /** The type of the the second underlying generator engine. */
1250 typedef _UniformRandomNumberGenerator2 base2_type;
1253 typedef typename base1_type::result_type _Result_type1;
1254 typedef typename base2_type::result_type _Result_type2;
1257 /** The type of the generated random value. */
1258 typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1)
1259 > sizeof(_Result_type2)),
1260 _Result_type1, _Result_type2>::__type result_type;
1263 static const int shift1 = __s1;
1264 static const int shift2 = __s2;
1266 // constructors and member function
1269 { _M_initialize_max(); }
1271 xor_combine(const base1_type& __rng1, const base2_type& __rng2)
1272 : _M_b1(__rng1), _M_b2(__rng2)
1273 { _M_initialize_max(); }
1275 xor_combine(unsigned long __s)
1276 : _M_b1(__s), _M_b2(__s + 1)
1277 { _M_initialize_max(); }
1279 template<class _Gen>
1280 xor_combine(_Gen& __g)
1281 : _M_b1(__g), _M_b2(__g)
1282 { _M_initialize_max(); }
1291 template<class _Gen>
1316 * Gets the next random number in the sequence.
1318 // NB: Not exactly the TR1 formula, per N2079 instead.
1322 return ((result_type(_M_b1() - _M_b1.min()) << shift1)
1323 ^ (result_type(_M_b2() - _M_b2.min()) << shift2));
1327 * Compares two %xor_combine random number generator objects of
1328 * the same type for equality.
1330 * @param __lhs A %xor_combine random number generator object.
1331 * @param __rhs Another %xor_combine random number generator
1334 * @returns true if the two objects are equal, false otherwise.
1337 operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1339 return (__lhs.base1() == __rhs.base1())
1340 && (__lhs.base2() == __rhs.base2());
1344 * Compares two %xor_combine random number generator objects of
1345 * the same type for inequality.
1347 * @param __lhs A %xor_combine random number generator object.
1348 * @param __rhs Another %xor_combine random number generator
1351 * @returns true if the two objects are not equal, false otherwise.
1354 operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1355 { return !(__lhs == __rhs); }
1358 * Inserts the current state of a %xor_combine random number
1359 * generator engine @p __x into the output stream @p __os.
1361 * @param __os An output stream.
1362 * @param __x A %xor_combine random number generator engine.
1364 * @returns The output stream with the state of @p __x inserted or in
1367 template<class _UniformRandomNumberGenerator11, int __s11,
1368 class _UniformRandomNumberGenerator21, int __s21,
1369 typename _CharT, typename _Traits>
1370 friend std::basic_ostream<_CharT, _Traits>&
1371 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1372 const xor_combine<_UniformRandomNumberGenerator11, __s11,
1373 _UniformRandomNumberGenerator21, __s21>& __x);
1376 * Extracts the current state of a %xor_combine random number
1377 * generator engine @p __x from the input stream @p __is.
1379 * @param __is An input stream.
1380 * @param __x A %xor_combine random number generator engine.
1382 * @returns The input stream with the state of @p __x extracted or in
1385 template<class _UniformRandomNumberGenerator11, int __s11,
1386 class _UniformRandomNumberGenerator21, int __s21,
1387 typename _CharT, typename _Traits>
1388 friend std::basic_istream<_CharT, _Traits>&
1389 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1390 xor_combine<_UniformRandomNumberGenerator11, __s11,
1391 _UniformRandomNumberGenerator21, __s21>& __x);
1395 _M_initialize_max();
1398 _M_initialize_max_aux(result_type, result_type, int);
1407 * A standard interface to a platform-specific non-deterministic
1408 * random number generator (if any are available).
1414 typedef unsigned int result_type;
1416 // constructors, destructors and member functions
1418 #ifdef _GLIBCXX_USE_RANDOM_TR1
1421 random_device(const std::string& __token = "/dev/urandom")
1423 if ((__token != "/dev/urandom" && __token != "/dev/random")
1424 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1425 std::__throw_runtime_error(__N("random_device::"
1426 "random_device(const std::string&)"));
1430 { std::fclose(_M_file); }
1435 random_device(const std::string& __token = "mt19937")
1436 : _M_mt(_M_strtoul(__token)) { }
1439 static unsigned long
1440 _M_strtoul(const std::string& __str)
1442 unsigned long __ret = 5489UL;
1443 if (__str != "mt19937")
1445 const char* __nptr = __str.c_str();
1447 __ret = std::strtoul(__nptr, &__endptr, 0);
1448 if (*__nptr == '\0' || *__endptr != '\0')
1449 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1450 "(const std::string&)"));
1461 { return std::numeric_limits<result_type>::min(); }
1465 { return std::numeric_limits<result_type>::max(); }
1474 #ifdef _GLIBCXX_USE_RANDOM_TR1
1476 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1485 random_device(const random_device&);
1486 void operator=(const random_device&);
1488 #ifdef _GLIBCXX_USE_RANDOM_TR1
1495 /* @} */ // group tr1_random_generators
1498 * @addtogroup tr1_random_distributions Random Number Distributions
1499 * @ingroup tr1_random
1504 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1505 * @ingroup tr1_random_distributions
1510 * @brief Uniform discrete distribution for random numbers.
1511 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1512 * probability throughout the range.
1514 template<typename _IntType = int>
1517 __glibcxx_class_requires(_IntType, _IntegerConcept)
1520 /** The type of the parameters of the distribution. */
1521 typedef _IntType input_type;
1522 /** The type of the range of the distribution. */
1523 typedef _IntType result_type;
1527 * Constructs a uniform distribution object.
1530 uniform_int(_IntType __min = 0, _IntType __max = 9)
1531 : _M_min(__min), _M_max(__max)
1533 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1537 * Gets the inclusive lower bound of the distribution range.
1544 * Gets the inclusive upper bound of the distribution range.
1551 * Resets the distribution state.
1553 * Does nothing for the uniform integer distribution.
1559 * Gets a uniformly distributed random number in the range
1562 template<typename _UniformRandomNumberGenerator>
1564 operator()(_UniformRandomNumberGenerator& __urng)
1566 typedef typename _UniformRandomNumberGenerator::result_type
1568 return _M_call(__urng, _M_min, _M_max,
1569 typename is_integral<_UResult_type>::type());
1573 * Gets a uniform random number in the range @f$[0, n)@f$.
1575 * This function is aimed at use with std::random_shuffle.
1577 template<typename _UniformRandomNumberGenerator>
1579 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1581 typedef typename _UniformRandomNumberGenerator::result_type
1583 return _M_call(__urng, 0, __n - 1,
1584 typename is_integral<_UResult_type>::type());
1588 * Inserts a %uniform_int random number distribution @p __x into the
1589 * output stream @p os.
1591 * @param __os An output stream.
1592 * @param __x A %uniform_int random number distribution.
1594 * @returns The output stream with the state of @p __x inserted or in
1597 template<typename _IntType1, typename _CharT, typename _Traits>
1598 friend std::basic_ostream<_CharT, _Traits>&
1599 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1600 const uniform_int<_IntType1>& __x);
1603 * Extracts a %unform_int random number distribution
1604 * @p __x from the input stream @p __is.
1606 * @param __is An input stream.
1607 * @param __x A %uniform_int random number generator engine.
1609 * @returns The input stream with @p __x extracted or in an error state.
1611 template<typename _IntType1, typename _CharT, typename _Traits>
1612 friend std::basic_istream<_CharT, _Traits>&
1613 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1614 uniform_int<_IntType1>& __x);
1617 template<typename _UniformRandomNumberGenerator>
1619 _M_call(_UniformRandomNumberGenerator& __urng,
1620 result_type __min, result_type __max, true_type)
1622 // XXX Must be fixed to also work when __urng.max() - __urng.min()
1623 // is smaller than __max - __min.
1624 typedef typename __gnu_cxx::__add_unsigned<typename
1625 _UniformRandomNumberGenerator::result_type>::__type __utype;
1626 return result_type((__max - __min + 1.0L)
1627 * (__utype(__urng()) - __utype(__urng.min()))
1628 / (__utype(__urng.max())
1629 - __utype(__urng.min()) + 1.0L)) + __min;
1632 template<typename _UniformRandomNumberGenerator>
1634 _M_call(_UniformRandomNumberGenerator& __urng,
1635 result_type __min, result_type __max, false_type)
1637 return result_type((__urng() - __urng.min())
1638 / (__urng.max() - __urng.min())
1639 * (__max - __min + 1)) + __min;
1648 * @brief A Bernoulli random number distribution.
1650 * Generates a sequence of true and false values with likelihood @f$ p @f$
1651 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1653 class bernoulli_distribution
1656 typedef int input_type;
1657 typedef bool result_type;
1661 * Constructs a Bernoulli distribution with likelihood @p p.
1663 * @param __p [IN] The likelihood of a true result being returned. Must
1664 * be in the interval @f$ [0, 1] @f$.
1667 bernoulli_distribution(double __p = 0.5)
1670 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1674 * Gets the @p p parameter of the distribution.
1681 * Resets the distribution state.
1683 * Does nothing for a bernoulli distribution.
1689 * Gets the next value in the Bernoullian sequence.
1691 template<class _UniformRandomNumberGenerator>
1693 operator()(_UniformRandomNumberGenerator& __urng)
1695 if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
1701 * Inserts a %bernoulli_distribution random number distribution
1702 * @p __x into the output stream @p __os.
1704 * @param __os An output stream.
1705 * @param __x A %bernoulli_distribution random number distribution.
1707 * @returns The output stream with the state of @p __x inserted or in
1710 template<typename _CharT, typename _Traits>
1711 friend std::basic_ostream<_CharT, _Traits>&
1712 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1713 const bernoulli_distribution& __x);
1716 * Extracts a %bernoulli_distribution random number distribution
1717 * @p __x from the input stream @p __is.
1719 * @param __is An input stream.
1720 * @param __x A %bernoulli_distribution random number generator engine.
1722 * @returns The input stream with @p __x extracted or in an error state.
1724 template<typename _CharT, typename _Traits>
1725 friend std::basic_istream<_CharT, _Traits>&
1726 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1727 bernoulli_distribution& __x)
1728 { return __is >> __x._M_p; }
1736 * @brief A discrete geometric random number distribution.
1738 * The formula for the geometric probability mass function is
1739 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1742 template<typename _IntType = int, typename _RealType = double>
1743 class geometric_distribution
1747 typedef _RealType input_type;
1748 typedef _IntType result_type;
1750 // constructors and member function
1752 geometric_distribution(const _RealType& __p = _RealType(0.5))
1755 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
1760 * Gets the distribution parameter @p p.
1769 template<class _UniformRandomNumberGenerator>
1771 operator()(_UniformRandomNumberGenerator& __urng);
1774 * Inserts a %geometric_distribution random number distribution
1775 * @p __x into the output stream @p __os.
1777 * @param __os An output stream.
1778 * @param __x A %geometric_distribution random number distribution.
1780 * @returns The output stream with the state of @p __x inserted or in
1783 template<typename _IntType1, typename _RealType1,
1784 typename _CharT, typename _Traits>
1785 friend std::basic_ostream<_CharT, _Traits>&
1786 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1787 const geometric_distribution<_IntType1, _RealType1>& __x);
1790 * Extracts a %geometric_distribution random number distribution
1791 * @p __x from the input stream @p __is.
1793 * @param __is An input stream.
1794 * @param __x A %geometric_distribution random number generator engine.
1796 * @returns The input stream with @p __x extracted or in an error state.
1798 template<typename _CharT, typename _Traits>
1799 friend std::basic_istream<_CharT, _Traits>&
1800 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1801 geometric_distribution& __x)
1804 __x._M_initialize();
1811 { _M_log_p = std::log(_M_p); }
1818 template<typename _RealType>
1819 class normal_distribution;
1822 * @brief A discrete Poisson random number distribution.
1824 * The formula for the poisson probability mass function is
1825 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1826 * parameter of the distribution.
1828 template<typename _IntType = int, typename _RealType = double>
1829 class poisson_distribution
1833 typedef _RealType input_type;
1834 typedef _IntType result_type;
1836 // constructors and member function
1838 poisson_distribution(const _RealType& __mean = _RealType(1))
1839 : _M_mean(__mean), _M_nd()
1841 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
1846 * Gets the distribution parameter @p mean.
1856 template<class _UniformRandomNumberGenerator>
1858 operator()(_UniformRandomNumberGenerator& __urng);
1861 * Inserts a %poisson_distribution random number distribution
1862 * @p __x into the output stream @p __os.
1864 * @param __os An output stream.
1865 * @param __x A %poisson_distribution random number distribution.
1867 * @returns The output stream with the state of @p __x inserted or in
1870 template<typename _IntType1, typename _RealType1,
1871 typename _CharT, typename _Traits>
1872 friend std::basic_ostream<_CharT, _Traits>&
1873 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1874 const poisson_distribution<_IntType1, _RealType1>& __x);
1877 * Extracts a %poisson_distribution random number distribution
1878 * @p __x from the input stream @p __is.
1880 * @param __is An input stream.
1881 * @param __x A %poisson_distribution random number generator engine.
1883 * @returns The input stream with @p __x extracted or in an error state.
1885 template<typename _IntType1, typename _RealType1,
1886 typename _CharT, typename _Traits>
1887 friend std::basic_istream<_CharT, _Traits>&
1888 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1889 poisson_distribution<_IntType1, _RealType1>& __x);
1895 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1896 normal_distribution<_RealType> _M_nd;
1900 // Hosts either log(mean) or the threshold of the simple method.
1901 _RealType _M_lm_thr;
1902 #if _GLIBCXX_USE_C99_MATH_TR1
1903 _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
1909 * @brief A discrete binomial random number distribution.
1911 * The formula for the binomial probability mass function is
1912 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1913 * and @f$ p @f$ are the parameters of the distribution.
1915 template<typename _IntType = int, typename _RealType = double>
1916 class binomial_distribution
1920 typedef _RealType input_type;
1921 typedef _IntType result_type;
1923 // constructors and member function
1925 binomial_distribution(_IntType __t = 1,
1926 const _RealType& __p = _RealType(0.5))
1927 : _M_t(__t), _M_p(__p), _M_nd()
1929 _GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
1934 * Gets the distribution @p t parameter.
1941 * Gets the distribution @p p parameter.
1951 template<class _UniformRandomNumberGenerator>
1953 operator()(_UniformRandomNumberGenerator& __urng);
1956 * Inserts a %binomial_distribution random number distribution
1957 * @p __x into the output stream @p __os.
1959 * @param __os An output stream.
1960 * @param __x A %binomial_distribution random number distribution.
1962 * @returns The output stream with the state of @p __x inserted or in
1965 template<typename _IntType1, typename _RealType1,
1966 typename _CharT, typename _Traits>
1967 friend std::basic_ostream<_CharT, _Traits>&
1968 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1969 const binomial_distribution<_IntType1, _RealType1>& __x);
1972 * Extracts a %binomial_distribution random number distribution
1973 * @p __x from the input stream @p __is.
1975 * @param __is An input stream.
1976 * @param __x A %binomial_distribution random number generator engine.
1978 * @returns The input stream with @p __x extracted or in an error state.
1980 template<typename _IntType1, typename _RealType1,
1981 typename _CharT, typename _Traits>
1982 friend std::basic_istream<_CharT, _Traits>&
1983 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1984 binomial_distribution<_IntType1, _RealType1>& __x);
1990 template<class _UniformRandomNumberGenerator>
1992 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
1994 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1995 normal_distribution<_RealType> _M_nd;
1998 #if _GLIBCXX_USE_C99_MATH_TR1
1999 _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
2000 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
2008 /* @} */ // group tr1_random_distributions_discrete
2011 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
2012 * @ingroup tr1_random_distributions
2017 * @brief Uniform continuous distribution for random numbers.
2019 * A continuous random distribution on the range [min, max) with equal
2020 * probability throughout the range. The URNG should be real-valued and
2021 * deliver number in the range [0, 1).
2023 template<typename _RealType = double>
2028 typedef _RealType input_type;
2029 typedef _RealType result_type;
2033 * Constructs a uniform_real object.
2035 * @param __min [IN] The lower bound of the distribution.
2036 * @param __max [IN] The upper bound of the distribution.
2039 uniform_real(_RealType __min = _RealType(0),
2040 _RealType __max = _RealType(1))
2041 : _M_min(__min), _M_max(__max)
2043 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
2057 template<class _UniformRandomNumberGenerator>
2059 operator()(_UniformRandomNumberGenerator& __urng)
2060 { return (__urng() * (_M_max - _M_min)) + _M_min; }
2063 * Inserts a %uniform_real random number distribution @p __x into the
2064 * output stream @p __os.
2066 * @param __os An output stream.
2067 * @param __x A %uniform_real random number distribution.
2069 * @returns The output stream with the state of @p __x inserted or in
2072 template<typename _RealType1, typename _CharT, typename _Traits>
2073 friend std::basic_ostream<_CharT, _Traits>&
2074 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2075 const uniform_real<_RealType1>& __x);
2078 * Extracts a %unform_real random number distribution
2079 * @p __x from the input stream @p __is.
2081 * @param __is An input stream.
2082 * @param __x A %uniform_real random number generator engine.
2084 * @returns The input stream with @p __x extracted or in an error state.
2086 template<typename _RealType1, typename _CharT, typename _Traits>
2087 friend std::basic_istream<_CharT, _Traits>&
2088 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2089 uniform_real<_RealType1>& __x);
2098 * @brief An exponential continuous distribution for random numbers.
2100 * The formula for the exponential probability mass function is
2101 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2103 * <table border=1 cellpadding=10 cellspacing=0>
2104 * <caption align=top>Distribution Statistics</caption>
2105 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2106 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2107 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2108 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2109 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2112 template<typename _RealType = double>
2113 class exponential_distribution
2117 typedef _RealType input_type;
2118 typedef _RealType result_type;
2122 * Constructs an exponential distribution with inverse scale parameter
2126 exponential_distribution(const result_type& __lambda = result_type(1))
2127 : _M_lambda(__lambda)
2129 _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
2133 * Gets the inverse scale parameter of the distribution.
2137 { return _M_lambda; }
2140 * Resets the distribution.
2142 * Has no effect on exponential distributions.
2147 template<class _UniformRandomNumberGenerator>
2149 operator()(_UniformRandomNumberGenerator& __urng)
2150 { return -std::log(__urng()) / _M_lambda; }
2153 * Inserts a %exponential_distribution random number distribution
2154 * @p __x into the output stream @p __os.
2156 * @param __os An output stream.
2157 * @param __x A %exponential_distribution random number distribution.
2159 * @returns The output stream with the state of @p __x inserted or in
2162 template<typename _RealType1, typename _CharT, typename _Traits>
2163 friend std::basic_ostream<_CharT, _Traits>&
2164 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2165 const exponential_distribution<_RealType1>& __x);
2168 * Extracts a %exponential_distribution random number distribution
2169 * @p __x from the input stream @p __is.
2171 * @param __is An input stream.
2172 * @param __x A %exponential_distribution random number
2175 * @returns The input stream with @p __x extracted or in an error state.
2177 template<typename _CharT, typename _Traits>
2178 friend std::basic_istream<_CharT, _Traits>&
2179 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2180 exponential_distribution& __x)
2181 { return __is >> __x._M_lambda; }
2184 result_type _M_lambda;
2189 * @brief A normal continuous distribution for random numbers.
2191 * The formula for the normal probability mass function is
2192 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2193 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2195 template<typename _RealType = double>
2196 class normal_distribution
2200 typedef _RealType input_type;
2201 typedef _RealType result_type;
2205 * Constructs a normal distribution with parameters @f$ mean @f$ and
2209 normal_distribution(const result_type& __mean = result_type(0),
2210 const result_type& __sigma = result_type(1))
2211 : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
2213 _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
2217 * Gets the mean of the distribution.
2224 * Gets the @f$ \sigma @f$ of the distribution.
2228 { return _M_sigma; }
2231 * Resets the distribution.
2235 { _M_saved_available = false; }
2237 template<class _UniformRandomNumberGenerator>
2239 operator()(_UniformRandomNumberGenerator& __urng);
2242 * Inserts a %normal_distribution random number distribution
2243 * @p __x into the output stream @p __os.
2245 * @param __os An output stream.
2246 * @param __x A %normal_distribution random number distribution.
2248 * @returns The output stream with the state of @p __x inserted or in
2251 template<typename _RealType1, typename _CharT, typename _Traits>
2252 friend std::basic_ostream<_CharT, _Traits>&
2253 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2254 const normal_distribution<_RealType1>& __x);
2257 * Extracts a %normal_distribution random number distribution
2258 * @p __x from the input stream @p __is.
2260 * @param __is An input stream.
2261 * @param __x A %normal_distribution random number generator engine.
2263 * @returns The input stream with @p __x extracted or in an error state.
2265 template<typename _RealType1, typename _CharT, typename _Traits>
2266 friend std::basic_istream<_CharT, _Traits>&
2267 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2268 normal_distribution<_RealType1>& __x);
2271 result_type _M_mean;
2272 result_type _M_sigma;
2273 result_type _M_saved;
2274 bool _M_saved_available;
2279 * @brief A gamma continuous distribution for random numbers.
2281 * The formula for the gamma probability mass function is
2282 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2284 template<typename _RealType = double>
2285 class gamma_distribution
2289 typedef _RealType input_type;
2290 typedef _RealType result_type;
2294 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2297 gamma_distribution(const result_type& __alpha_val = result_type(1))
2298 : _M_alpha(__alpha_val)
2300 _GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
2305 * Gets the @f$ \alpha @f$ of the distribution.
2309 { return _M_alpha; }
2312 * Resets the distribution.
2317 template<class _UniformRandomNumberGenerator>
2319 operator()(_UniformRandomNumberGenerator& __urng);
2322 * Inserts a %gamma_distribution random number distribution
2323 * @p __x into the output stream @p __os.
2325 * @param __os An output stream.
2326 * @param __x A %gamma_distribution random number distribution.
2328 * @returns The output stream with the state of @p __x inserted or in
2331 template<typename _RealType1, typename _CharT, typename _Traits>
2332 friend std::basic_ostream<_CharT, _Traits>&
2333 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2334 const gamma_distribution<_RealType1>& __x);
2337 * Extracts a %gamma_distribution random number distribution
2338 * @p __x from the input stream @p __is.
2340 * @param __is An input stream.
2341 * @param __x A %gamma_distribution random number generator engine.
2343 * @returns The input stream with @p __x extracted or in an error state.
2345 template<typename _CharT, typename _Traits>
2346 friend std::basic_istream<_CharT, _Traits>&
2347 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2348 gamma_distribution& __x)
2350 __is >> __x._M_alpha;
2351 __x._M_initialize();
2359 result_type _M_alpha;
2361 // Hosts either lambda of GB or d of modified Vaduva's.
2365 /* @} */ // group tr1_random_distributions_continuous
2366 /* @} */ // group tr1_random_distributions
2367 /* @} */ // group tr1_random
2369 _GLIBCXX_END_NAMESPACE
2372 #include <tr1/random.tcc>
2374 #endif // _TR1_RANDOM