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.
30 #ifndef _STD_TR1_RANDOM
31 #define _STD_TR1_RANDOM 1
35 * This is a TR1 C++ Library header.
39 #include <bits/concept_check.h>
40 #include <bits/cpp_type_traits.h>
42 #include <debug/debug.h>
46 #include <tr1/type_traits>
50 _GLIBCXX_BEGIN_NAMESPACE(tr1)
52 // [5.1] Random number generation
55 * @addtogroup tr1_random Random Number Generation
56 * A facility for generating random numbers on selected distributions.
61 * Implementation-space details.
65 // Type selectors -- are these already implemented elsewhere?
66 template<bool, typename _TpTrue, typename _TpFalse>
69 typedef _TpTrue _Type;
72 template<typename _TpTrue, typename _TpFalse>
73 struct _Select<false, _TpTrue, _TpFalse>
75 typedef _TpFalse _Type;
79 * An adaptor class for converting the output of any Generator into
80 * the input for a specific Distribution.
82 template<typename _Generator, typename _Distribution>
85 typedef typename _Generator::result_type generated_type;
86 typedef typename _Distribution::input_type result_type;
89 _Adaptor(const _Generator& __g)
100 * Converts a value generated by the adapted random number generator into a
101 * value in the input domain for the dependent random number distribution.
103 * Because the type traits are compile time constants only the appropriate
104 * clause of the if statements will actually be emitted by the compiler.
106 template<typename _Generator, typename _Distribution>
107 typename _Adaptor<_Generator, _Distribution>::result_type
108 _Adaptor<_Generator, _Distribution>::
111 result_type __return_value = 0;
112 if (is_integral<generated_type>::value
113 && is_integral<result_type>::value)
114 __return_value = _M_g();
115 else if (is_integral<generated_type>::value
116 && !is_integral<result_type>::value)
117 __return_value = result_type(_M_g())
118 / result_type(_M_g.max() - _M_g.min() + 1);
119 else if (!is_integral<generated_type>::value
120 && !is_integral<result_type>::value)
121 __return_value = result_type(_M_g())
122 / result_type(_M_g.max() - _M_g.min());
123 return __return_value;
126 } // namespace std::tr1::_Private
130 * Produces random numbers on a given disribution function using a un uniform
131 * random number generation engine.
133 * @todo the engine_value_type needs to be studied more carefully.
135 template<typename _Generator, typename _Dist>
136 class variate_generator
138 // Concept requirements.
139 __glibcxx_class_requires(_Generator, _CopyConstructibleConcept)
140 // __glibcxx_class_requires(_Generator, _GeneratorConcept)
141 // __glibcxx_class_requires(_Dist, _GeneratorConcept)
144 typedef _Generator engine_type;
145 typedef _Private::_Adaptor<_Generator, _Dist> engine_value_type;
146 typedef _Dist distribution_type;
147 typedef typename _Dist::result_type result_type;
149 // tr1:5.1.1 table 5.1 requirement
150 typedef typename std::__enable_if<result_type,
151 is_arithmetic<result_type>::value
152 >::__type _IsValidType;
156 * Constructs a variate generator with the uniform random number
157 * generator @p __eng for the random distribution @p __dist.
159 * @throws Any exceptions which may thrown by the copy constructors of
160 * the @p _Generator or @p _Dist objects.
162 variate_generator(engine_type __eng, distribution_type __dist)
163 : _M_engine(__eng), _M_dist(__dist) { }
166 * Gets the next generated value on the distribution.
171 template<typename _Tp>
173 operator()(_Tp __value);
176 * Gets a reference to the underlying uniform random number generator
181 { return _M_engine; }
184 * Gets a const reference to the underlying uniform random number
187 const engine_value_type&
189 { return _M_engine; }
192 * Gets a reference to the underlying random distribution.
199 * Gets a const reference to the underlying random distribution.
201 const distribution_type&
206 * Gets the closed lower bound of the distribution interval.
210 { return this->distribution().min(); }
213 * Gets the closed upper bound of the distribution interval.
217 { return this->distribution().max(); }
220 engine_value_type _M_engine;
221 distribution_type _M_dist;
225 * Gets the next random value on the given distribution.
227 template<typename _Generator, typename _Dist>
228 typename variate_generator<_Generator, _Dist>::result_type
229 variate_generator<_Generator, _Dist>::
231 { return _M_dist(_M_engine); }
236 template<typename _Generator, typename _Dist>
237 template<typename _Tp>
238 typename variate_generator<_Generator, _Dist>::result_type
239 variate_generator<_Generator, _Dist>::
240 operator()(_Tp __value)
241 { return _M_dist(_M_engine, __value); }
245 * @addtogroup tr1_random_generators Random Number Generators
246 * @ingroup tr1_random
248 * These classes define objects which provide random or pseudorandom numbers,
249 * either from a discrete or a continuous interval. The random number
250 * generator supplied as a part of this library are all uniform random number
251 * generators which provide a sequence of random number uniformly distributed
254 * A number generator is a function object with an operator() that takes zero
255 * arguments and returns a number.
257 * A compliant random number generator must satisy the following requirements.
258 * <table border=1 cellpadding=10 cellspacing=0>
259 * <caption align=top>Random Number Generator Requirements</caption>
260 * <tr><td>To be documented.</td></tr>
267 * @brief A model of a linear congruential random number generator.
269 * A random number generator that produces pseudorandom numbers using the
270 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
272 * The template parameter @p _UIntType must be an unsigned integral type
273 * large enough to store values up to (__m-1). If the template parameter
274 * @p __m is 0, the modulus @p __m used is
275 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
276 * parameters @p __a and @p __c must be less than @p __m.
278 * The size of the state is @f$ 1 @f$.
280 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
281 class linear_congruential
283 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
284 // __glibcpp_class_requires(__a < __m && __c < __m)
287 /** The type of the generated random value. */
288 typedef _UIntType result_type;
290 /** The multiplier. */
291 static const _UIntType multiplier = __a;
293 static const _UIntType increment = __c;
295 static const _UIntType modulus = __m;
298 * Constructs a %linear_congruential random number generator engine with
299 * seed @p __s. The default seed value is 1.
301 * @param __s The initial seed value.
303 explicit linear_congruential(unsigned long __s = 1);
306 * Constructs a %linear_congruential random number generator engine
307 * seeded from the generator function @p __g.
309 * @param __g The seed generator function.
312 linear_congruential(_Gen& __g);
315 * Reseeds the %linear_congruential random number generator engine
316 * sequence to the seed @g __s.
318 * @param __s The new seed.
321 seed(unsigned long __s = 1);
324 * Reseeds the %linear_congruential random number generator engine
325 * sequence using values from the generator function @p __g.
327 * @param __g the seed generator function.
332 { seed(__g, typename is_fundamental<_Gen>::type()); }
335 * Gets the smallest possible value in the output range.
341 * Gets the largest possible value in the output range.
347 * Gets the next random number in the sequence.
353 * Compares two linear congruential random number generator objects of the
354 * same type for equality.
356 * @param __lhs A linear congruential random number generator object.
357 * @param __rhs Another linear congruential random number generator obj.
359 * @returns true if the two objects are equal, false otherwise.
362 operator==(const linear_congruential& __lhs,
363 const linear_congruential& __rhs)
364 { return __lhs._M_x == __rhs._M_x; }
367 * Compares two linear congruential random number generator objects of the
368 * same type for inequality.
370 * @param __lhs A linear congruential random number generator object.
371 * @param __rhs Another linear congruential random number generator obj.
373 * @returns true if the two objects are not equal, false otherwise.
376 operator!=(const linear_congruential& __lhs,
377 const linear_congruential& __rhs)
378 { return !(__lhs == __rhs); }
381 * Writes the textual representation of the state x(i) of x to @p __os.
383 * @param __os The output stream.
384 * @param __lcr A linear_congruential random number generator.
387 template<typename _CharT, typename _Traits>
388 friend std::basic_ostream<_CharT, _Traits>&
389 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
390 const linear_congruential& __lcr)
391 { return __os << __lcr._M_x; }
394 * Sets the state of the engine by reading its textual
395 * representation from @p __is.
397 * The textual representation must have been previously written using an
398 * output stream whose imbued locale and whose type's template
399 * specialization arguments _CharT and _Traits were the same as those of
402 * @param __is The input stream.
403 * @param __lcr A linear_congruential random number generator.
406 template<typename _CharT, typename _Traits>
407 friend std::basic_istream<_CharT, _Traits>&
408 operator>>(std::basic_istream<_CharT, _Traits>& __is,
409 linear_congruential& __lcr)
410 { return __is >> __lcr._M_x; }
415 seed(_Gen& __g, true_type)
416 { return seed(static_cast<unsigned long>(__g)); }
420 seed(_Gen& __g, false_type);
427 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
429 typedef linear_congruential<unsigned int, 16807, 0, 2147483647> minstd_rand0;
432 * An alternative LCR (Lehmer Generator function) .
434 typedef linear_congruential<unsigned int, 48271, 0, 2147483647> minstd_rand;
438 * A generalized feedback shift register discrete random number generator.
440 * This algorithm avoind multiplication and division and is designed to be
441 * friendly to a pipelined architecture. If the parameters are chosen
442 * correctly, this generator will produce numbers with a very long period and
443 * fairly good apparent entropy, although still not cryptographically strong.
445 * The best way to use this generator is with the predefined mt19937 class.
447 * This algorithm was originally invented by Makoto Matsumoto and
450 * @var word_size The number of bits in each element of the state vector.
451 * @var state_size The degree of recursion.
452 * @var shift_size The period parameter.
453 * @var mask_bits The separation point bit index.
454 * @var parameter_a The last row of the twist matrix.
455 * @var output_u The first right-shift tempering matrix parameter.
456 * @var output_s The first left-shift tempering matrix parameter.
457 * @var output_b The first left-shift tempering matrix mask.
458 * @var output_t The second left-shift tempering matrix parameter.
459 * @var output_c The second left-shift tempering matrix mask.
460 * @var output_l The second right-shift tempering matrix parameter.
462 template<class _UIntType, int __w, int __n, int __m, int __r,
463 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
464 _UIntType __c, int __l>
465 class mersenne_twister
467 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
471 typedef _UIntType result_type ;
474 static const int word_size = __w;
475 static const int state_size = __n;
476 static const int shift_size = __m;
477 static const int mask_bits = __r;
478 static const _UIntType parameter_a = __a;
479 static const int output_u = __u;
480 static const int output_s = __s;
481 static const _UIntType output_b = __b;
482 static const int output_t = __t;
483 static const _UIntType output_c = __c;
484 static const int output_l = __l;
486 // constructors and member function
491 mersenne_twister(unsigned long __value)
495 mersenne_twister(_Gen& __g)
503 seed(unsigned long __value);
508 { seed(__g, typename is_fundamental<_Gen>::type()); }
523 seed(_Gen& __g, true_type)
524 { return seed(static_cast<unsigned long>(__g)); }
528 seed(_Gen& __g, false_type);
531 _UIntType _M_x[state_size];
536 * The classic Mersenne Twister.
539 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
540 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
541 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
543 typedef mersenne_twister<
544 unsigned long, 32, 624, 397, 31,
552 * @brief The Marsaglia-Zaman generator.
554 * This is a model of a Generalized Fibonacci discrete random number
555 * generator, sometimes referred to as the SWC generator.
557 * A discrete random number generator that produces pseudorandom numbers using
558 * @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m @f$.
560 * The size of the state is @f$ r @f$
561 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
563 * N1688[4.13] says "the template parameter _IntType shall denote an integral
564 * type large enough to store values up to m."
567 * @var _M_x The state of te generator. This is a ring buffer.
568 * @var _M_carry The carry.
569 * @var _M_p Current index of x(i - r).
572 template<typename _IntType, _IntType __m, int __s, int __r>
573 class subtract_with_carry
575 __glibcxx_class_requires(_IntType, _IntegerConcept)
578 /** The type of the generated random value. */
579 typedef _IntType result_type;
582 static const _IntType modulus = __m;
583 static const int long_lag = __r;
584 static const int short_lag = __s;
588 * Constructs a default-initialized % subtract_with_carry random number
591 subtract_with_carry()
595 * Constructs an explicitly seeded % subtract_with_carry random number
599 subtract_with_carry(unsigned long __value)
600 { this->seed(__value); }
603 * Constructs a % subtract_with_carry random number generator seeded from
604 * the PAD iterated by [__first, last).
607 subtract_with_carry(_Gen& __g)
611 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
613 * @note This implementation follows the tr1 specification but will
614 * obviously not work correctly on all platforms, since it has hardcoded
615 * values that may overflow ints on some platforms.
617 * N1688[4.19] modifies this as follows.
618 * If @p __value == 0, sets value to 19780503. In any case, with a linear
619 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
620 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value @f$, sets
621 * @f$ x_{-r} \dots x_{-1} @f$ to
622 * @f$ lcg(1) \bmod m \dots lcg(r) \bmod m @f$ respectively.
623 * If @f$ x_{-1} = 0 @f$ set carry to 1, otherwise sets carry to 0.
626 seed(unsigned long __value = 19780503);
629 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
630 * random number generator.
635 { seed(__g, typename is_fundamental<_Gen>::type()); }
638 * Gets the inclusive minimum value of the range of random integers
639 * returned by this generator.
646 * Gets the inclusive maximum value of the range of random integers
647 * returned by this generator.
651 { return this->modulus - 1; }
654 * Gets the next random number in the sequence.
660 * Compares two % subtract_with_carry random number generator objects of
661 * the same type for equality.
663 * @param __lhs A % subtract_with_carry random number generator object.
664 * @param __rhs Another % subtract_with_carry random number generator
667 * @returns true if the two objects are equal, false otherwise.
670 operator==(const subtract_with_carry& __lhs,
671 const subtract_with_carry& __rhs)
673 return ((__lhs._M_x[0] == __rhs._M_x[0])
674 && (__lhs._M_x[__r - 1] == __rhs._M_x[__r - 1]));
678 * Compares two % subtract_with_carry random number generator objects of
679 * the same type for inequality.
681 * @param __lhs A % subtract_with_carry random number generator object.
682 * @param __rhs Another % subtract_with_carry random number generator
685 * @returns true if the two objects are not equal, false otherwise.
688 operator!=(const subtract_with_carry& __lhs,
689 const subtract_with_carry& __rhs)
690 { return !(__lhs == __rhs); }
693 * Inserts the current state of a % subtract_with_carry random number
694 * genator engine @p x into the output stream @p __os.
696 * @param __os An output stream.
697 * @param __x A % subtract_with_carry random number generator engine.
699 * @returns The output stream with the state of @p x inserted or in an
702 template<typename _CharT, typename _Traits>
703 friend basic_ostream<_CharT, _Traits>&
704 operator<<(basic_ostream<_CharT, _Traits>& __os,
705 const subtract_with_carry& __x)
707 std::copy(__x._M_x, __x._M_x + __r,
708 std::ostream_iterator<_IntType>(__os, " "));
709 return __os << __x._M_carry;
713 * Extracts the current state of a % subtract_with_carry random number
714 * gerator engine @p x from the input stream @p __is.
716 * @param __is An input stream.
717 * @param __x A % subtract_with_carry random number generator engine.
719 * @returns The input stream with the state of @p x extracted or in an
722 template<typename _CharT, typename _Traits>
723 friend basic_istream<_CharT, _Traits>&
724 operator>>(basic_istream<_CharT, _Traits>& __is,
725 subtract_with_carry& __x)
727 for (int __i = 0; __i < __r; ++__i)
728 __is >> __x._M_x[__i];
729 __is >> __x._M_carry;
736 seed(_Gen& __g, true_type)
737 { return seed(static_cast<unsigned long>(__g)); }
741 seed(_Gen& __g, false_type);
745 result_type _M_x[long_lag];
746 result_type _M_carry;
751 * Produces random numbers from some base engine by discarding blocks of
754 * 0 <= @p __r <= @p __p
756 template<class _UniformRandomNumberGenerator, int __p, int __r>
759 // __glibcxx_class_requires(typename base_type::result_type,
760 // ArithmeticTypeConcept);
763 /** The type of the underlying generator engine. */
764 typedef _UniformRandomNumberGenerator base_type;
765 /** The type of the generated random value. */
766 typedef typename base_type::result_type result_type;
769 static const int block_size = __p;
770 static const int used_block = __r;
773 * Constructs a default %discard_block engine.
775 * The underlying engine is default constrcuted as well.
781 * Copy constructs a %discard_block engine.
783 * Copies an existing base class random number geenerator.
784 * @param rng An existing (base class) engine object.
786 explicit discard_block(const base_type& __rng)
787 : _M_b(__rng) , _M_n(0) { }
790 * Seed constructs a %discard_block engine.
792 * Constructs the underlying generator engine seeded with @p __s.
793 * @param __s A seed value for the base class engine.
795 explicit discard_block(unsigned long __s)
796 : _M_b(__s), _M_n(0) { }
799 * Generator constructs a %discard_block engine.
801 * @param __g A seed generator function.
804 discard_block(_Gen& __g)
805 : _M_b(__g), _M_n(0) { }
808 * Reseeds the %discard_block object with the default seed for the
809 * underlying base class generator engine.
818 * Reseeds the %discard_block object with the given seed generator
820 * @param __g A seed generator function.
830 * Gets a const reference to the underlying generator engine object.
837 * Gets the minimum value in the generated random number range.
841 { return _M_b.min(); }
844 * Gets the maximum value in the generated random number range.
848 { return _M_b.max(); }
851 * Gets the next value in the generated random number sequence.
857 * Compares two %discard_block random number generator objects of
858 * the same type for equality.
860 * @param __lhs A %discard_block random number generator object.
861 * @param __rhs Another %discard_block random number generator
864 * @returns true if the two objects are equal, false otherwise.
867 operator==(const discard_block& __lhs, const discard_block& __rhs)
869 return ((__lhs._M_b == __rhs._M_b)
870 && (__lhs._M_n == __rhs._M_n));
874 * Compares two %discard_block random number generator objects of
875 * the same type for inequality.
877 * @param __lhs A %discard_block random number generator object.
878 * @param __rhs Another %discard_block random number generator
881 * @returns true if the two objects are not equal, false otherwise.
884 operator!=(const discard_block& __lhs, const discard_block& __rhs)
885 { return !(__lhs == __rhs); }
888 * Inserts the current state of a %discard_block random number
889 * genator engine @p x into the output stream @p __os.
891 * @param __os An output stream.
892 * @param __x A %discard_block random number generator engine.
894 * @returns The output stream with the state of @p x inserted or in an
897 template<typename _CharT, typename _Traits>
898 friend basic_ostream<_CharT, _Traits>&
899 operator<<(basic_ostream<_CharT, _Traits>& __os,
900 const discard_block& __x)
901 { return __os << __x._M_b << " " << __x._M_n; }
904 * Extracts the current state of a % subtract_with_carry random number
905 * gerator engine @p x from the input stream @p __is.
907 * @param __is An input stream.
908 * @param __x A %discard_block random number generator engine.
910 * @returns The input stream with the state of @p x extracted or in an
913 template<typename _CharT, typename _Traits>
914 friend basic_istream<_CharT, _Traits>&
915 operator>>(basic_istream<_CharT, _Traits>& __is,
917 { return __is >> __x._M_b >> __x._M_n; }
926 * James's luxury-level-3 integer adaptation of Luescher's generator.
928 typedef discard_block<
929 subtract_with_carry<int, (1<<24), 10, 24>,
935 * James's luxury-level-4 integer adaptation of Luescher's generator.
937 typedef discard_block<
938 subtract_with_carry<int, (1<<24), 10, 24>,
945 * A random number generator adaptor class that combines two random number
946 * generator engines into a single output sequence.
948 template<class _UniformRandomNumberGenerator1, int __s1,
949 class _UniformRandomNumberGenerator2, int __s2>
952 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
953 // result_type, ArithmeticTypeConcept);
954 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
955 // result_type, ArithmeticTypeConcept);
958 /** The type of the the first underlying generator engine. */
959 typedef _UniformRandomNumberGenerator1 base1_type;
960 /** The type of the the second underlying generator engine. */
961 typedef _UniformRandomNumberGenerator2 base2_type;
962 /** The type of the generated random value. */
963 typedef typename _Private::_Select<
964 (sizeof(base1_type) > sizeof(base2_type)),
967 >::_Type result_type;
970 static const int shift1 = __s1;
971 static const int shift2 = __s2;
973 // constructors and member function
976 xor_combine(const base1_type& __rng1, const base2_type& __rng2)
977 : _M_b1(__rng1), _M_b2(__rng2) { }
979 xor_combine(unsigned long __s)
980 : _M_b1(__s), _M_b2(__s + 1) { }
983 xor_combine(_Gen& __g)
984 : _M_b1(__g), _M_b2(__g) { }
1011 { return _M_b1.min() ^ _M_b2.min(); }
1015 { return _M_b1.max() | _M_b2.max(); }
1018 * Gets the next random number in the sequence.
1022 { return ((_M_b1() << shift1) ^ (_M_b2() << shift2)); }
1025 * Compares two %xor_combine random number generator objects of
1026 * the same type for equality.
1028 * @param __lhs A %xor_combine random number generator object.
1029 * @param __rhs Another %xor_combine random number generator
1032 * @returns true if the two objects are equal, false otherwise.
1035 operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1037 return (__lhs.base1() == __rhs.base1())
1038 && (__lhs.base2() == __rhs.base2());
1042 * Compares two %xor_combine random number generator objects of
1043 * the same type for inequality.
1045 * @param __lhs A %xor_combine random number generator object.
1046 * @param __rhs Another %xor_combine random number generator
1049 * @returns true if the two objects are not equal, false otherwise.
1052 operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1053 { return !(__lhs == __rhs); }
1056 * Inserts the current state of a %xor_combine random number
1057 * genator engine @p x into the output stream @p __os.
1059 * @param __os An output stream.
1060 * @param __x A %xor_combine random number generator engine.
1062 * @returns The output stream with the state of @p x inserted or in an
1065 template<typename _CharT, typename _Traits>
1066 friend basic_ostream<_CharT, _Traits>&
1067 operator<<(basic_ostream<_CharT, _Traits>& __os,
1068 const xor_combine& __x)
1069 { return __os << __x.base1() << " " << __x.base1(); }
1072 * Extracts the current state of a %xor_combine random number
1073 * gerator engine @p x from the input stream @p __is.
1075 * @param __is An input stream.
1076 * @param __x A %xor_combine random number generator engine.
1078 * @returns The input stream with the state of @p x extracted or in an
1081 template<typename _CharT, typename _Traits>
1082 friend basic_istream<_CharT, _Traits>&
1083 operator>>(basic_istream<_CharT, _Traits>& __is,
1085 { return __is >> __x._M_b1 >> __x._M_b2; }
1094 * A standard interface to a platform-specific non-deterministic random number
1095 * generator (if any are available).
1097 * @todo The underlying interface is system-specific and needs to be factored
1098 * into the generated configury mechs. For example, the use of "/dev/random"
1099 * under a Linux OS would be appropriate.
1105 typedef unsigned int result_type;
1107 // constructors, destructors and member functions
1108 explicit random_device(const std::string& __token = "unimplemented");
1109 result_type min() const;
1110 result_type max() const;
1111 double entropy() const;
1112 result_type operator()();
1115 random_device(const random_device&);
1116 void operator=(const random_device&);
1119 /* @} */ // group tr1_random_generators
1122 * @addtogroup tr1_random_distributions Random Number Distributions
1123 * @ingroup tr1_random
1128 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1129 * @ingroup tr1_random_distributions
1134 * @brief Uniform discrete distribution for random numbers.
1135 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1136 * probability throughout the range.
1138 template<typename _IntType = int>
1141 __glibcxx_class_requires(_IntType, _IntegerConcept)
1144 /** The type of the parameters of the distribution. */
1145 typedef _IntType input_type;
1146 /** The type of the range of the distribution. */
1147 typedef _IntType result_type;
1151 * Constructs a uniform distribution object.
1154 uniform_int(_IntType __min = 0, _IntType __max = 9)
1155 : _M_min(__min), _M_max(__max)
1157 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1161 * Gets the inclusive lower bound of the distribution range.
1168 * Gets the inclusive upper bound of the distribution range.
1175 * Resets the distribution state.
1177 * Does nothing for the uniform integer distribution.
1183 * Gets a uniformly distributed random number in the range
1186 template<typename _UniformRandomNumberGenerator>
1188 operator()(_UniformRandomNumberGenerator& __urng)
1189 { return (__urng() % (_M_max - _M_min + 1)) + _M_min; }
1192 * Gets a uniform random number in the range @f$[0, n)@f$.
1194 * This function is aimed at use with std::random_shuffle.
1196 template<typename _UniformRandomNumberGenerator>
1198 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1199 { return __urng() % __n; }
1202 * Inserts a %uniform_int random number distribution @p x into the
1203 * output stream @p os.
1205 * @param __os An output stream.
1206 * @param __x A %uniform_int random number distribution.
1208 * @returns The output stream with the state of @p x inserted or in an
1211 template<typename _CharT, typename _Traits>
1212 friend basic_ostream<_CharT, _Traits>&
1213 operator<<(basic_ostream<_CharT, _Traits>& __os,
1214 const uniform_int& __x)
1215 { return __os << __x._M_min << " " << __x._M_max; }
1218 * Extracts a %unform_int random number distribution
1219 * @p u from the input stream @p __is.
1221 * @param __is An input stream.
1222 * @param __u A %uniform_int random number generator engine.
1224 * @returns The input stream with @p u extracted or in an error state.
1226 template<typename _CharT, typename _Traits>
1227 friend basic_istream<_CharT, _Traits>&
1228 operator>>(basic_istream<_CharT, _Traits>& __is, uniform_int& __u)
1229 { return __is >> __u._M_min >> __u._M_max; }
1238 * @brief A Bernoulli random number distribution.
1240 * Generates a sequence of true and false values with likelihood @f$ p @f$
1241 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1243 class bernoulli_distribution
1246 typedef int input_type;
1247 typedef bool result_type;
1251 * Constructs a Bernoulli distribution with likelihood @p p.
1253 * @param __p [IN] The likelihood of a true result being returned. Must
1254 * be in the interval @f$ [0, 1] @f$.
1257 bernoulli_distribution(double __p = 0.5)
1260 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1264 * Gets the @p p parameter of the distribution.
1271 * Gets the inclusive lower bound of the distribution range.
1278 * Gets the inclusive upper bound of the distribution range.
1285 * Resets the distribution state.
1287 * Does nothing for a bernoulli distribution.
1293 * Gets the next value in the Bernoullian sequence.
1295 template<class UniformRandomNumberGenerator>
1297 operator()(UniformRandomNumberGenerator& __urng)
1299 if (__urng() < _M_p)
1305 * Inserts a %bernoulli_distribution random number distribution
1306 * @p x into the output stream @p __os.
1308 * @param __os An output stream.
1309 * @param __x A %bernoulli_distribution random number distribution.
1311 * @returns The output stream with the state of @p x inserted or in an
1314 template<typename _CharT, typename _Traits>
1315 friend basic_ostream<_CharT, _Traits>&
1316 operator<<(basic_ostream<_CharT, _Traits>& __os,
1317 const bernoulli_distribution& __x)
1318 { return __os << __x.p(); }
1321 * Extracts a %bernoulli_distribution random number distribution
1322 * @p u from the input stream @p __is.
1324 * @param __is An input stream.
1325 * @param __u A %bernoulli_distribution random number generator engine.
1327 * @returns The input stream with @p u extracted or in an error state.
1329 template<typename _CharT, typename _Traits>
1330 friend basic_istream<_CharT, _Traits>&
1331 operator>>(basic_istream<_CharT, _Traits>& __is,
1332 bernoulli_distribution& __u)
1333 { return __is >> __u._M_p; }
1341 * @brief A discrete geometric random number distribution.
1343 * The formula for the geometric probability mass function is
1344 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1347 template<typename _IntType = int, typename _RealType = double>
1348 class geometric_distribution
1352 typedef _RealType input_type;
1353 typedef _IntType result_type;
1355 // constructors and member function
1358 geometric_distribution(const _RealType& __p = _RealType(0.5))
1359 : _M_p(__p), _M_log_p(std::log(_M_p))
1361 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1365 * Gets the distribution parameter @p p.
1372 * Gets the inclusive lower bound of the distribution range.
1378 * Gets the inclusive upper bound of the distribution range.
1386 template<class _UniformRandomNumberGenerator>
1388 operator()(_UniformRandomNumberGenerator& __urng)
1390 return result_type(std::floor(std::log(_RealType(1.0) - __urng())
1391 / _M_log_p)) + result_type(1);
1395 * Inserts a %geometric_distribution random number distribution
1396 * @p x into the output stream @p __os.
1398 * @param __os An output stream.
1399 * @param __x A %geometric_distribution random number distribution.
1401 * @returns The output stream with the state of @p x inserted or in an
1404 template<typename _CharT, typename _Traits>
1405 friend basic_ostream<_CharT, _Traits>&
1406 operator<<(basic_ostream<_CharT, _Traits>& __os,
1407 const geometric_distribution& __x)
1408 { return __os << __x.p(); }
1411 * Extracts a %geometric_distribution random number distribution
1412 * @p u from the input stream @p __is.
1414 * @param __is An input stream.
1415 * @param __u A %geometric_distribution random number generator engine.
1417 * @returns The input stream with @p u extracted or in an error state.
1419 template<typename _CharT, typename _Traits>
1420 friend basic_istream<_CharT, _Traits>&
1421 operator>>(basic_istream<_CharT, _Traits>& __is,
1422 geometric_distribution& __u)
1425 __u._M_log_p = std::log(__u._M_p);
1434 /* @} */ // group tr1_random_distributions_discrete
1437 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
1438 * @ingroup tr1_random_distributions
1443 * @brief Uniform continuous distribution for random numbers.
1445 * A continuous random distribution on the range [min, max) with equal
1446 * probability throughout the range. The URNG should be real-valued and
1447 * deliver number in the range [0, 1).
1449 template<typename _RealType = double>
1454 typedef _RealType input_type;
1455 typedef _RealType result_type;
1459 * Constructs a uniform_real object.
1461 * @param __min [IN] The lower bound of the distribution.
1462 * @param __max [IN] The upper bound of the distribution.
1465 uniform_real(_RealType __min = _RealType(0),
1466 _RealType __max = _RealType(1));
1476 template<class _UniformRandomNumberGenerator>
1478 operator()(_UniformRandomNumberGenerator& __urng)
1479 { return (__urng() * (max() - min())) + min(); }
1482 * Inserts a %uniform_real random number distribution @p x into the
1483 * output stream @p __os.
1485 * @param __os An output stream.
1486 * @param __x A %uniform_real random number distribution.
1488 * @returns The output stream with the state of @p x inserted or in an
1491 template<typename _CharT, typename _Traits>
1492 friend basic_ostream<_CharT, _Traits>&
1493 operator<<(basic_ostream<_CharT, _Traits>& __os,
1494 const uniform_real& __x)
1495 { return __os << __x.min() << " " << __x.max(); }
1498 * Extracts a %unform_real random number distribution
1499 * @p u from the input stream @p __is.
1501 * @param __is An input stream.
1502 * @param __u A %uniform_real random number generator engine.
1504 * @returns The input stream with @p u extracted or in an error state.
1506 template<typename _CharT, typename _Traits>
1507 friend basic_istream<_CharT, _Traits>&
1508 operator>>(basic_istream<_CharT, _Traits>& __is,
1510 { return __is >> __u._M_min >> __u._M_max; }
1519 * @brief An exponential continuous distribution for random numbers.
1521 * The formula for the exponential probability mass function is
1522 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
1524 * <table border=1 cellpadding=10 cellspacing=0>
1525 * <caption align=top>Distribution Statistics</caption>
1526 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
1527 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
1528 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
1529 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
1530 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
1533 template<typename _RealType = double>
1534 class exponential_distribution
1538 typedef _RealType input_type;
1539 typedef _RealType result_type;
1543 * Constructs an exponential distribution with inverse scale parameter
1547 exponential_distribution(const result_type& __lambda = result_type(1))
1548 : _M_lambda(__lambda) { }
1551 * Gets the inverse scale parameter of the distribution.
1555 { return _M_lambda; }
1558 * Resets the distribution.
1560 * Has no effect on exponential distributions.
1565 template<class _UniformRandomNumberGenerator>
1567 operator()(_UniformRandomNumberGenerator& __urng)
1568 { return -std::log(__urng()) / _M_lambda; }
1571 * Inserts a %exponential_distribution random number distribution
1572 * @p x into the output stream @p __os.
1574 * @param __os An output stream.
1575 * @param __x A %exponential_distribution random number distribution.
1577 * @returns The output stream with the state of @p x inserted or in an
1580 template<typename _CharT, typename _Traits>
1581 friend basic_ostream<_CharT, _Traits>&
1582 operator<<(basic_ostream<_CharT, _Traits>& __os,
1583 const exponential_distribution& __x)
1584 { return __os << __x.lambda(); }
1587 * Extracts a %exponential_distribution random number distribution
1588 * @p u from the input stream @p __is.
1590 * @param __is An input stream.
1591 * @param __u A %exponential_distribution random number generator engine.
1593 * @returns The input stream with @p u extracted or in an error state.
1595 template<typename _CharT, typename _Traits>
1596 friend basic_istream<_CharT, _Traits>&
1597 operator>>(basic_istream<_CharT, _Traits>& __is,
1598 exponential_distribution& __u)
1599 { return __is >> __u._M_lambda; }
1602 result_type _M_lambda;
1605 /* @} */ // group tr1_random_distributions_continuous
1606 /* @} */ // group tr1_random_distributions
1607 /* @} */ // group tr1_random
1609 _GLIBCXX_END_NAMESPACE
1612 #include <tr1/random.tcc>
1614 #endif // _STD_TR1_RANDOM