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 implemeted elsewhere?
66 template<bool, typename _TpTrue, typename _TpFalse>
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 genereator 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 d.
159 * @throws Any exceptions which may thrown by the copy constructors of the
160 * @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 large
273 * enough to store values up to (m-1). If the template parameter @p m is 0,
274 * the modulus @p m used is std::numeric_limits<UIntType>::max() plus 1.
275 * Otherwise, the template parameters @p a and @p c must be less than @p m.
277 * The size of the state is @f$ 1 @f$.
279 template<class UIntType, UIntType a, UIntType c, UIntType m>
280 class linear_congruential
282 __glibcxx_class_requires(UIntType, _UnsignedIntegerConcept)
283 // __glibcpp_class_requires(a < m && c < m)
286 /** The type of the generated random value. */
287 typedef UIntType result_type;
289 /** The multiplier. */
290 static const UIntType multiplier = a;
292 static const UIntType increment = c;
294 static const UIntType modulus = m;
297 * Constructs a %linear_congruential random number generator engine with
298 * seed @p s. The default seed value is 1.
300 * @param s The initial seed value.
302 explicit linear_congruential(unsigned long s = 1);
305 * Constructs a %linear_congruential random number generator engine
306 * seeded from the generator function @g.
308 * @param g The seed generator function.
311 linear_congruential(Gen& g);
314 * Resets the %linear_congruential random number generator engine sequence
317 * @param s The new seed.
320 seed(unsigned long s = 1);
323 * Resets the %linear_congruential random number generator engine sequence
324 * using a vlaue from the generator function @g.
326 * @param g the seed generator function.
331 { seed(g, typename is_fundamental<Gen>::type()); }
334 * Gets the smallest possible value in the output range.
340 * Gets the largest possible value in the output range.
346 * Gets the next random number in the sequence.
352 * Compares two linear congruential random number generator objects of the
353 * same type for equality.
355 * @param lhs A linear congruential random number generator object.
356 * @param rhs Another linear congruential random number generator object.
358 * @returns true if the two objects are equal, false otherwise.
361 operator==(const linear_congruential& lhs, const linear_congruential& rhs)
362 { return lhs.m_x == rhs.m_x; }
365 * Compares two linear congruential random number generator objects of the
366 * same type for inequality.
368 * @param lhs A linear congruential random number generator object.
369 * @param rhs Another linear congruential random number generator object.
371 * @returns true if the two objects are not equal, false otherwise.
374 operator!=(const linear_congruential& lhs, const linear_congruential& rhs)
375 { return !(lhs == rhs); }
378 * Writes the textual representation of the state x(i) of x to @p os.
380 * @param os The output stream.
381 * @param lcr A linear_congruential random number generator.
384 template<typename CharT, typename Traits>
385 friend std::basic_ostream<CharT, Traits>&
386 operator<<(std::basic_ostream<CharT, Traits>& os,
387 const linear_congruential& lcr)
388 { return os << lcr.m_x; }
391 * Sets the state of the engine by reading its textual
392 * representation from @p is.
394 * The textual representation must have been previously written using an
395 * output stream whose imbued locale and whose type's template
396 * specialization arguments CharT and Traits were the same as those of
399 * @param is The input stream.
400 * @param lcr A linear_congruential random number generator.
403 template<typename CharT, typename Traits>
404 friend std::basic_istream<CharT, Traits>&
405 operator>>(std::basic_istream<CharT, Traits>& is,
406 linear_congruential& lcr)
407 { return is >> lcr.m_x; }
412 seed(Gen& g, true_type)
413 { return seed(static_cast<unsigned long>(g)); }
417 seed(Gen& g, false_type);
424 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
426 typedef linear_congruential<unsigned int, 16807, 0, 2147483647> minstd_rand0;
429 * An alternative LCR (Lehmer Generator function) .
431 typedef linear_congruential<unsigned int, 48271, 0, 2147483647> minstd_rand;
435 * A generalized feedback shift register discrete random number generator.
437 * This algorithm avoind multiplication and division and is designed to be
438 * friendly to a pipelined architecture. If the parameters are chosen
439 * correctly, this generator will produce numbers with a very long period and
440 * fairly good apparent entropy, although still not cryptographically strong.
442 * The best way to use this generator is with the predefined mt19937 class.
444 * This algorithm was originally invented by Makoto Matsumoto and
447 * @var word_size The number of bits in each element of the state vector.
448 * @var state_size The degree of recursion.
449 * @var shift_size The period parameter.
450 * @var mask_bits The separation point bit index.
451 * @var parameter_a The last row of the twist matrix.
452 * @var output_u The first right-shift tempering matrix parameter.
453 * @var output_s The first left-shift tempering matrix parameter.
454 * @var output_b The first left-shift tempering matrix mask.
455 * @var output_t The second left-shift tempering matrix parameter.
456 * @var output_c The second left-shift tempering matrix mask.
457 * @var output_l The second right-shift tempering matrix parameter.
459 template<class UIntType, int w, int n, int m, int r,
460 UIntType a, int u, int s, UIntType b, int t, UIntType c, int l>
461 class mersenne_twister
463 __glibcxx_class_requires(UIntType, _UnsignedIntegerConcept)
467 typedef UIntType result_type ;
470 static const int word_size = w;
471 static const int state_size = n;
472 static const int shift_size = m;
473 static const int mask_bits = r;
474 static const UIntType parameter_a = a;
475 static const int output_u = u;
476 static const int output_s = s;
477 static const UIntType output_b = b;
478 static const int output_t = t;
479 static const UIntType output_c = c;
480 static const int output_l = l;
482 // constructors and member function
487 mersenne_twister(unsigned long value)
491 mersenne_twister(Gen& g)
499 seed(unsigned long value);
504 { seed(g, typename is_fundamental<Gen>::type()); }
519 seed(Gen& g, true_type)
520 { return seed(static_cast<unsigned long>(g)); }
524 seed(Gen& g, false_type);
527 UIntType _M_x[state_size];
532 * The classic Mersenne Twister.
535 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
536 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
537 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
539 typedef mersenne_twister<
540 unsigned long, 32, 624, 397, 31,
548 * @brief The Marsaglia-Zaman generator.
550 * This is a model of a Generalized Fibonacci discrete random number
551 * generator, sometimes referred to as the SWC generator.
553 * A discrete random number generator that produces pseudorandom numbers using
554 * @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m @f$.
556 * The size of the state is @f$ r @f$
557 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
559 * N1688[4.13] says "the template parameter _IntType shall denote an integral
560 * type large enough to store values up to m."
563 * @var _M_x The state of te generator. This is a ring buffer.
564 * @var _M_carry The carry.
565 * @var _M_p Current index of x(i - r).
568 template<typename _IntType, _IntType m, int s, int r>
569 class subtract_with_carry
571 __glibcxx_class_requires(_IntType, _IntegerConcept)
574 /** The type of the generated random value. */
575 typedef _IntType result_type;
578 static const _IntType modulus = m;
579 static const int long_lag = r;
580 static const int short_lag = s;
584 * Constructs a default-initialized % subtract_with_carry random number
587 subtract_with_carry()
591 * Constructs an explicitly seeded % subtract_with_carry random number
595 subtract_with_carry(_IntType __value)
596 { this->seed(__value); }
599 * Constructs a % subtract_with_carry random number generator seeded from
600 * the PAD iterated by [__first, last).
603 subtract_with_carry(Gen& g)
607 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
609 * @note This implementation follows the tr1 specification but will
610 * obviously not work correctly on all platforms, since it has hardcoded
611 * values that may overflow ints on some platforms.
613 * N1688[4.19] modifies this as follows.
614 * If @p __value == 0, sets value to 19780503. In any case, with a linear
615 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
616 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value @f$, sets
617 * @f$ x_{-r} \dots x_{-1} @f$ to
618 * @f$ lcg(1) \bmod m \dots lcg(r) \bmod m @f$ respectively.
619 * If @f$ x_{-1} = 0 @f$ set carry to 1, otherwise sets carry to 0.
622 seed(_IntType __value = 19780503);
625 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
626 * random number generator.
631 { seed(g, typename is_fundamental<Gen>::type()); }
634 * Gets the inclusive minimum value of the range of random integers
635 * returned by this generator.
642 * Gets the inclusive maximum value of the range of random integers
643 * returned by this generator.
647 { return this->modulus - 1; }
650 * Gets the next random number in the sequence.
656 * Compares two % subtract_with_carry random number generator objects of
657 * the same type for equality.
659 * @param __lhs A % subtract_with_carry random number generator object.
660 * @param __rhs Another % subtract_with_carry random number generator
663 * @returns true if the two objects are equal, false otherwise.
666 operator==(const subtract_with_carry& __lhs,
667 const subtract_with_carry& __rhs)
669 return ((__lhs._M_x[0] == __rhs._M_x[0])
670 && (__lhs._M_x[r-1] == __rhs._M_x[r-1]));
674 * Compares two % subtract_with_carry random number generator objects of
675 * the same type for inequality.
677 * @param __lhs A % subtract_with_carry random number generator object.
678 * @param __rhs Another % subtract_with_carry random number generator
681 * @returns true if the two objects are not equal, false otherwise.
684 operator!=(const subtract_with_carry& __lhs,
685 const subtract_with_carry& __rhs)
686 { return !(__lhs == __rhs); }
689 * Inserts the current state of a % subtract_with_carry random number
690 * genator engine @p x into the output stream @p os.
692 * @param __os An output stream.
693 * @param __x A % subtract_with_carry random number generator engine.
695 * @returns The output stream with the state of @p x inserted or in an
698 template<typename _CharT, typename _Traits>
699 friend basic_ostream<_CharT, _Traits>&
700 operator<<(basic_ostream<_CharT, _Traits>& __os,
701 const subtract_with_carry& __x)
703 std::copy(__x._M_x, __x._M_x + r,
704 std::ostream_iterator<_IntType>(__os, " "));
705 return __os << __x._M_carry;
709 * Extracts the current state of a % subtract_with_carry random number
710 * gerator engine @p x from the input stream @p is.
712 * @param __is An input stream.
713 * @param __x A % subtract_with_carry random number generator engine.
715 * @returns The input stream with the state of @p x extracted or in an
718 template<typename _CharT, typename _Traits>
719 friend basic_istream<_CharT, _Traits>&
720 operator>>(basic_istream<_CharT, _Traits>& __is,
721 subtract_with_carry& __x)
723 for (int __i = 0; __i < r; ++__i)
724 __is >> __x._M_x[__i];
725 __is >> __x._M_carry;
732 seed(Gen& g, true_type)
733 { return seed(static_cast<unsigned long>(g)); }
737 seed(Gen& g, false_type);
741 result_type _M_x[long_lag];
742 result_type _M_carry;
747 * Produces random numbers from some base engine by discarding blocks of
752 template<class UniformRandomNumberGenerator, int p, int r>
755 // __glibcxx_class_requires(typename base_type::result_type,
756 // ArithmeticTypeConcept);
759 /** The type of the underlying generator engine. */
760 typedef UniformRandomNumberGenerator base_type;
761 /** The type of the generated random value. */
762 typedef typename base_type::result_type result_type;
765 static const int block_size = p;
766 static const int used_block = r;
769 * Constructs a default %discard_block engine.
771 * The underlying engine is default constrcuted as well.
777 * Copy constructs a %discard_block engine.
779 * Copies an existing base class random number geenerator.
780 * @param rng An existing (base class) engine object.
782 explicit discard_block(const base_type& rng)
783 : _M_b(rng) , _M_n(0) { }
786 * Seed constructs a %discard_block engine.
788 * Constructs the underlying generator engine seeded with @p s.
789 * @param s A seed value for the base class engine.
791 explicit discard_block(unsigned long s)
792 : _M_b(s), _M_n(0) { }
795 * Generator constructs a %discard_block engine.
797 * @param g A seed generator function.
800 discard_block(Gen& g)
801 : _M_b(g), _M_n(0) { }
804 * Reseeds the %discard_block object with the default seed for the
805 * underlying base class generator engine.
814 * Reseeds the %discard_block object with the given seed generator
816 * @param g A seed generator function.
826 * Gets a const reference to the underlying generator engine object.
833 * Gets the minimum value in the generated random number range.
837 { return _M_b.min(); }
840 * Gets the maximum value in the generated random number range.
844 { return _M_b.max(); }
847 * Gets the next value in the generated random number sequence.
853 * Compares two %discard_block random number generator objects of
854 * the same type for equality.
856 * @param __lhs A %discard_block random number generator object.
857 * @param __rhs Another %discard_block random number generator
860 * @returns true if the two objects are equal, false otherwise.
863 operator==(const discard_block& __lhs, const discard_block& __rhs)
865 return ((__lhs._M_b == __rhs._M_b)
866 && (__lhs._M_n == __rhs._M_n));
870 * Compares two %discard_block random number generator objects of
871 * the same type for inequality.
873 * @param __lhs A %discard_block random number generator object.
874 * @param __rhs Another %discard_block random number generator
877 * @returns true if the two objects are not equal, false otherwise.
880 operator!=(const discard_block& __lhs, const discard_block& __rhs)
881 { return !(__lhs == __rhs); }
884 * Inserts the current state of a %discard_block random number
885 * genator engine @p x into the output stream @p os.
887 * @param __os An output stream.
888 * @param __x A %discard_block random number generator engine.
890 * @returns The output stream with the state of @p x inserted or in an
893 template<typename _CharT, typename _Traits>
894 friend basic_ostream<_CharT, _Traits>&
895 operator<<(basic_ostream<_CharT, _Traits>& __os,
896 const discard_block& __x)
897 { return __os << __x._M_b << " " << __x._M_n; }
900 * Extracts the current state of a % subtract_with_carry random number
901 * gerator engine @p x from the input stream @p is.
903 * @param __is An input stream.
904 * @param __x A %discard_block random number generator engine.
906 * @returns The input stream with the state of @p x extracted or in an
909 template<typename _CharT, typename _Traits>
910 friend basic_istream<_CharT, _Traits>&
911 operator>>(basic_istream<_CharT, _Traits>& __is,
913 { return __is >> __x._M_b >> __x._M_n; }
922 * James's luxury-level-3 integer adaptation of Luescher's generator.
924 typedef discard_block<
925 subtract_with_carry<int, (1<<24), 10, 24>,
931 * James's luxury-level-4 integer adaptation of Luescher's generator.
933 typedef discard_block<
934 subtract_with_carry<int, (1<<24), 10, 24>,
941 * A random number generator adaptor class that combines two random number
942 * generator engines into a single output sequence.
944 template<class UniformRandomNumberGenerator1, int s1,
945 class UniformRandomNumberGenerator2, int s2>
948 // __glibcxx_class_requires(typename UniformRandomNumberGenerator1::
949 // result_type, ArithmeticTypeConcept);
950 // __glibcxx_class_requires(typename UniformRandomNumberGenerator2::
951 // result_type, ArithmeticTypeConcept);
954 /** The type of the the first underlying generator engine. */
955 typedef UniformRandomNumberGenerator1 base1_type;
956 /** The type of the the second underlying generator engine. */
957 typedef UniformRandomNumberGenerator2 base2_type;
958 /** The type of the generated random value. */
959 typedef typename _Private::_Select<
960 (sizeof(base1_type) > sizeof(base2_type)),
966 static const int shift1 = s1;
967 static const int shift2 = s2;
969 // constructors and member function
972 xor_combine(const base1_type& rng1, const base2_type& rng2)
973 : _M_b1(rng1), _M_b2(rng2) { }
975 xor_combine(unsigned long s)
976 : _M_b1(s), _M_b2(s + 1) { }
980 : _M_b1(g), _M_b2(g) { }
1007 { return _M_b1.min() ^ _M_b2.min(); }
1011 { return _M_b1.max() | _M_b2.max(); }
1014 * Gets the next random number in the sequence.
1018 { return ((_M_b1() << shift1) ^ (_M_b2() << shift2)); }
1021 * Compares two %xor_combine random number generator objects of
1022 * the same type for equality.
1024 * @param __lhs A %xor_combine random number generator object.
1025 * @param __rhs Another %xor_combine random number generator
1028 * @returns true if the two objects are equal, false otherwise.
1031 operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1033 return (__lhs.base1() == __rhs.base1())
1034 && (__lhs.base2() == __rhs.base2());
1038 * Compares two %xor_combine random number generator objects of
1039 * the same type for inequality.
1041 * @param __lhs A %xor_combine random number generator object.
1042 * @param __rhs Another %xor_combine random number generator
1045 * @returns true if the two objects are not equal, false otherwise.
1048 operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1049 { return !(__lhs == __rhs); }
1052 * Inserts the current state of a %xor_combine random number
1053 * genator engine @p x into the output stream @p os.
1055 * @param __os An output stream.
1056 * @param __x A %xor_combine random number generator engine.
1058 * @returns The output stream with the state of @p x inserted or in an
1061 template<typename _CharT, typename _Traits>
1062 friend basic_ostream<_CharT, _Traits>&
1063 operator<<(basic_ostream<_CharT, _Traits>& __os,
1064 const xor_combine& __x)
1065 { return __os << __x.base1() << " " << __x.base1(); }
1068 * Extracts the current state of a %xor_combine random number
1069 * gerator engine @p x from the input stream @p is.
1071 * @param __is An input stream.
1072 * @param __x A %xor_combine random number generator engine.
1074 * @returns The input stream with the state of @p x extracted or in an
1077 template<typename _CharT, typename _Traits>
1078 friend basic_istream<_CharT, _Traits>&
1079 operator>>(basic_istream<_CharT, _Traits>& __is,
1081 { return __is >> __x._M_b1 >> __x._M_b2; }
1090 * A standard interface to a platform-specific non-deterministic random number
1091 * generator (if any are available).
1093 * @todo The underlying interface is system-specific and needs to be factored
1094 * into the generated configury mechs. For example, the use of "/dev/random"
1095 * under a Linux OS would be appropriate.
1101 typedef unsigned int result_type;
1103 // constructors, destructors and member functions
1104 explicit random_device(const std::string& token = "unimplemented");
1105 result_type min() const;
1106 result_type max() const;
1107 double entropy() const;
1108 result_type operator()();
1111 random_device(const random_device &);
1112 void operator=(const random_device &);
1115 /* @} */ // group tr1_random_generators
1118 * @addtogroup tr1_random_distributions Random Number Distributions
1119 * @ingroup tr1_random
1124 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1125 * @ingroup tr1_random_distributions
1130 * @brief Uniform discrete distribution for random numbers.
1131 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1132 * probability throughout the range.
1134 template<typename _IntType = int>
1137 __glibcxx_class_requires(_IntType, _IntegerConcept)
1140 /** The type of the parameters of the distribution. */
1141 typedef _IntType input_type;
1142 /** The type of the range of the distribution. */
1143 typedef _IntType result_type;
1147 * Constructs a uniform distribution object.
1150 uniform_int(_IntType __min = 0, _IntType __max = 9)
1151 : _M_min(__min), _M_max(__max)
1153 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1157 * Gets the inclusive lower bound of the distribution range.
1164 * Gets the inclusive upper bound of the distribution range.
1171 * Resets the distribution state.
1173 * Does nothing for the uniform integer distribution.
1179 * Gets a uniformly distributed random number in the range
1182 template<typename _UniformRandomNumberGenerator>
1184 operator()(_UniformRandomNumberGenerator& __urng)
1185 { return (__urng() % (_M_max - _M_min + 1)) + _M_min; }
1188 * Gets a uniform random number in the range @f$[0, n)@f$.
1190 * This function is aimed at use with std::random_shuffle.
1192 template<typename _UniformRandomNumberGenerator>
1194 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1195 { return __urng() % __n; }
1198 * Inserts a %uniform_int random number distribution @p x into the
1199 * output stream @p os.
1201 * @param __os An output stream.
1202 * @param __x A %uniform_int random number distribution.
1204 * @returns The output stream with the state of @p x inserted or in an
1207 template<typename _CharT, typename _Traits>
1208 friend basic_ostream<_CharT, _Traits>&
1209 operator<<(basic_ostream<_CharT, _Traits>& __os,
1210 const uniform_int& __x)
1211 { return __os << __x._M_min << " " << __x._M_max; }
1214 * Extracts a %unform_int random number distribution
1215 * @p u from the input stream @p is.
1217 * @param __is An input stream.
1218 * @param __u A %uniform_int random number generator engine.
1220 * @returns The input stream with @p u extracted or in an error state.
1222 template<typename _CharT, typename _Traits>
1223 friend basic_istream<_CharT, _Traits>&
1224 operator>>(basic_istream<_CharT, _Traits>& __is, uniform_int& __u)
1225 { return __is >> __u._M_min >> __u._M_max; }
1234 * @brief A Bernoulli random number distribution.
1236 * Generates a sequence of true and false values with likelihood @f$ p @f$
1237 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1239 class bernoulli_distribution
1242 typedef int input_type;
1243 typedef bool result_type;
1247 * Constructs a Bernoulli distribution with likelihood @p p.
1249 * @param p [IN] The likelihood of a true result being returned. Must
1250 * be in the interval @f$ [0, 1] @f$.
1253 bernoulli_distribution(double __p = 0.5)
1256 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1260 * Gets the @p p parameter of the distribution.
1267 * Gets the inclusive lower bound of the distribution range.
1274 * Gets the inclusive upper bound of the distribution range.
1281 * Resets the distribution state.
1283 * Does nothing for a bernoulli distribution.
1289 * Gets the next value in the Bernoullian sequence.
1291 template<class UniformRandomNumberGenerator>
1293 operator()(UniformRandomNumberGenerator& __urng)
1295 if (__urng() < _M_p)
1301 * Inserts a %bernoulli_distribution random number distribution
1302 * @p x into the output stream @p os.
1304 * @param __os An output stream.
1305 * @param __x A %bernoulli_distribution random number distribution.
1307 * @returns The output stream with the state of @p x inserted or in an
1310 template<typename _CharT, typename _Traits>
1311 friend basic_ostream<_CharT, _Traits>&
1312 operator<<(basic_ostream<_CharT, _Traits>& __os,
1313 const bernoulli_distribution& __x)
1314 { return __os << __x.p(); }
1317 * Extracts a %bernoulli_distribution random number distribution
1318 * @p u from the input stream @p is.
1320 * @param __is An input stream.
1321 * @param __u A %bernoulli_distribution random number generator engine.
1323 * @returns The input stream with @p u extracted or in an error state.
1325 template<typename _CharT, typename _Traits>
1326 friend basic_istream<_CharT, _Traits>&
1327 operator>>(basic_istream<_CharT, _Traits>& __is,
1328 bernoulli_distribution& __u)
1329 { return __is >> __u._M_p; }
1337 * @brief A discrete geometric random number distribution.
1339 * The formula for the geometric probability mass function is
1340 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1343 template<typename _IntType = int, typename _RealType = double>
1344 class geometric_distribution
1348 typedef _RealType input_type;
1349 typedef _IntType result_type;
1351 // constructors and member function
1354 geometric_distribution(const _RealType& __p = _RealType(0.5))
1355 : _M_p(__p), _M_log_p(std::log(_M_p))
1357 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1361 * Gets the distribution parameter @p.
1368 * Gets the inclusive lower bound of the distribution range.
1374 * Gets the inclusive upper bound of the distribution range.
1382 template<class _UniformRandomNumberGenerator>
1384 operator()(_UniformRandomNumberGenerator& __urng)
1386 return result_type(std::floor(std::log(_RealType(1.0) - __urng())
1387 / _M_log_p)) + result_type(1);
1391 * Inserts a %geometric_distribution random number distribution
1392 * @p x into the output stream @p os.
1394 * @param __os An output stream.
1395 * @param __x A %geometric_distribution random number distribution.
1397 * @returns The output stream with the state of @p x inserted or in an
1400 template<typename _CharT, typename _Traits>
1401 friend basic_ostream<_CharT, _Traits>&
1402 operator<<(basic_ostream<_CharT, _Traits>& __os,
1403 const geometric_distribution& __x)
1404 { return __os << __x.p(); }
1407 * Extracts a %geometric_distribution random number distribution
1408 * @p u from the input stream @p is.
1410 * @param __is An input stream.
1411 * @param __u A %geometric_distribution random number generator engine.
1413 * @returns The input stream with @p u extracted or in an error state.
1415 template<typename _CharT, typename _Traits>
1416 friend basic_istream<_CharT, _Traits>&
1417 operator>>(basic_istream<_CharT, _Traits>& __is,
1418 geometric_distribution& __u)
1421 __u._M_log_p = std::log(__u._M_p);
1430 /* @} */ // group tr1_random_distributions_discrete
1433 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
1434 * @ingroup tr1_random_distributions
1439 * @brief Uniform continuous distribution for random numbers.
1441 * A continuous random distribution on the range [min, max) with equal
1442 * probability throughout the range. The URNG should be real-valued and
1443 * deliver number in the range [0, 1).
1445 template<typename _RealType = double>
1450 typedef _RealType input_type;
1451 typedef _RealType result_type;
1455 * Constructs a uniform_real object.
1457 * @param min [IN] The lower bound of the distribution.
1458 * @param max [IN] The upper bound of the distribution.
1461 uniform_real(_RealType min = _RealType(0), _RealType max = _RealType(1));
1471 template<class _UniformRandomNumberGenerator>
1473 operator()(_UniformRandomNumberGenerator& __urng)
1474 { return (__urng() * (max() - min())) + min(); }
1477 * Inserts a %uniform_real random number distribution @p x into the
1478 * output stream @p os.
1480 * @param __os An output stream.
1481 * @param __x A %uniform_real random number distribution.
1483 * @returns The output stream with the state of @p x inserted or in an
1486 template<typename _CharT, typename _Traits>
1487 friend basic_ostream<_CharT, _Traits>&
1488 operator<<(basic_ostream<_CharT, _Traits>& __os,
1489 const uniform_real& __x)
1490 { return __os << __x.min() << " " << __x.max(); }
1493 * Extracts a %unform_real random number distribution
1494 * @p u from the input stream @p is.
1496 * @param __is An input stream.
1497 * @param __u A %uniform_real random number generator engine.
1499 * @returns The input stream with @p u extracted or in an error state.
1501 template<typename _CharT, typename _Traits>
1502 friend basic_istream<_CharT, _Traits>&
1503 operator>>(basic_istream<_CharT, _Traits>& __is,
1505 { return __is >> __u._M_min >> __u._M_max; }
1514 * @brief An exponential continuous distribution for random numbers.
1516 * The formula for the exponential probability mass function is
1517 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
1519 * <table border=1 cellpadding=10 cellspacing=0>
1520 * <caption align=top>Distribution Statistics</caption>
1521 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
1522 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
1523 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
1524 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
1525 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
1528 template<typename _RealType = double>
1529 class exponential_distribution
1533 typedef _RealType input_type;
1534 typedef _RealType result_type;
1538 * Constructs an exponential distribution with inverse scale parameter
1542 exponential_distribution(const result_type& __lambda = result_type(1))
1543 : _M_lambda(__lambda) { }
1546 * Gets the inverse scale parameter of the distribution.
1550 { return _M_lambda; }
1553 * Resets the distribution.
1555 * Has no effect on exponential distributions.
1560 template<class _UniformRandomNumberGenerator>
1562 operator()(_UniformRandomNumberGenerator& __urng)
1563 { return std::log(__urng) / _M_lambda; }
1566 * Inserts a %exponential_distribution random number distribution
1567 * @p x into the output stream @p os.
1569 * @param __os An output stream.
1570 * @param __x A %exponential_distribution random number distribution.
1572 * @returns The output stream with the state of @p x inserted or in an
1575 template<typename _CharT, typename _Traits>
1576 friend basic_ostream<_CharT, _Traits>&
1577 operator<<(basic_ostream<_CharT, _Traits>& __os,
1578 const exponential_distribution& __x)
1579 { return __os << __x.lambda(); }
1582 * Extracts a %exponential_distribution random number distribution
1583 * @p u from the input stream @p is.
1585 * @param __is An input stream.
1586 * @param __u A %exponential_distribution random number generator engine.
1588 * @returns The input stream with @p u extracted or in an error state.
1590 template<typename _CharT, typename _Traits>
1591 friend basic_istream<_CharT, _Traits>&
1592 operator>>(basic_istream<_CharT, _Traits>& __is,
1593 exponential_distribution& __u)
1594 { return __is >> __u._M_lambda; }
1597 result_type _M_lambda;
1600 /* @} */ // group tr1_random_distributions_continuous
1601 /* @} */ // group tr1_random_distributions
1602 /* @} */ // group tr1_random
1604 _GLIBCXX_END_NAMESPACE
1607 #include <tr1/random.tcc>
1609 #endif // _STD_TR1_RANDOM