2006-07-04 Paolo Carlini <pcarlini@suse.de>
[official-gcc.git] / libstdc++-v3 / include / tr1 / random
blob5124ccb312c63ca77623484f282a8a2e1f8f3a31
1 // random number generation -*- C++ -*-
3 // Copyright (C) 2006 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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
33 /**
34  * @file 
35  * This is a TR1 C++ Library header. 
36  */
38 #include <algorithm>
39 #include <bits/concept_check.h>
40 #include <bits/cpp_type_traits.h>
41 #include <cmath>
42 #include <debug/debug.h>
43 #include <iterator>
44 #include <iosfwd>
45 #include <limits>
46 #include <tr1/type_traits>
47 #include <fstream>
49 namespace std
51 _GLIBCXX_BEGIN_NAMESPACE(tr1)
53   // [5.1] Random number generation
55   /**
56    * @addtogroup tr1_random Random Number Generation
57    * A facility for generating random numbers on selected distributions.
58    * @{
59    */
61   /*
62    * Implementation-space details.
63    */
64   namespace _Private
65   {
66     // Type selectors -- are these already implemented elsewhere?
67     template<bool, typename _TpTrue, typename _TpFalse>
68       struct _Select
69       {
70         typedef _TpTrue _Type;
71       };
73     template<typename _TpTrue, typename _TpFalse>
74       struct _Select<false, _TpTrue, _TpFalse>
75       {
76         typedef _TpFalse _Type;
77       };
79     /*
80      * An adaptor class for converting the output of any Generator into
81      * the input for a specific Distribution.
82      */
83     template<typename _Engine, typename _Distribution>
84       struct _Adaptor
85       { 
86         typedef typename _Engine::result_type        _Engine_result_type;
87         typedef typename _Distribution::input_type   result_type;
89       public:
90         _Adaptor(const _Engine& __g)
91         : _M_g(__g) { }
93         result_type
94         operator()();
96       private:
97         _Engine _M_g;
98       };
100     /*
101      * Converts a value generated by the adapted random number generator into a
102      * value in the input domain for the dependent random number distribution.
103      *
104      * Because the type traits are compile time constants only the appropriate
105      * clause of the if statements will actually be emitted by the compiler.
106      */
107     template<typename _Engine, typename _Distribution>
108       typename _Adaptor<_Engine, _Distribution>::result_type
109       _Adaptor<_Engine, _Distribution>::
110       operator()()
111       {
112         result_type __return_value = 0;
113         if (is_integral<_Engine_result_type>::value
114             && is_integral<result_type>::value)
115           __return_value = _M_g();
116         else if (is_integral<_Engine_result_type>::value
117                  && !is_integral<result_type>::value)
118           __return_value = result_type(_M_g())
119             / result_type(_M_g.max() - _M_g.min() + result_type(1));
120         else if (!is_integral<_Engine_result_type>::value
121                  && !is_integral<result_type>::value)
122           __return_value = result_type(_M_g())
123             / result_type(_M_g.max() - _M_g.min());
124         return __return_value;
125       }
127     template<typename _UIntType, int __w, bool = 
128              __w != std::numeric_limits<_UIntType>::digits>
129       struct _Shift
130       { static const _UIntType __value = 0; };
132     template<typename _UIntType, int __w>
133       struct _Shift<_UIntType, __w, true>
134       { static const _UIntType __value = _UIntType(1) << __w; };
136   } // namespace std::tr1::_Private
139   /**
140    * Produces random numbers on a given disribution function using a un uniform
141    * random number generation engine.
142    *
143    * @todo the engine_value_type needs to be studied more carefully.
144    */
145   template<typename _Engine, typename _Dist>
146     class variate_generator
147     {
148       // Concept requirements.
149       __glibcxx_class_requires(_Engine, _CopyConstructibleConcept)
150       //  __glibcxx_class_requires(_Engine, _EngineConcept)
151       //  __glibcxx_class_requires(_Dist, _EngineConcept)
153     public:
154       typedef _Engine                                engine_type;
155       typedef _Private::_Adaptor<_Engine, _Dist>     engine_value_type;
156       typedef _Dist                                  distribution_type;
157       typedef typename _Dist::result_type            result_type;
159       // tr1:5.1.1 table 5.1 requirement
160       typedef typename std::__enable_if<result_type,
161                                         is_arithmetic<result_type>::value
162         >::__type _IsValidType;
164     public:
165       /**
166        * Constructs a variate generator with the uniform random number
167        * generator @p __eng for the random distribution @p __dist.
168        *
169        * @throws Any exceptions which may thrown by the copy constructors of
170        * the @p _Engine or @p _Dist objects.
171        */
172       variate_generator(engine_type __eng, distribution_type __dist)
173       : _M_engine(__eng), _M_dist(__dist) { }
175       /**
176        * Gets the next generated value on the distribution.
177        */
178       result_type
179       operator()()
180       { return _M_dist(_M_engine); }
182       /**
183        * WTF?
184        */
185       template<typename _Tp>
186         result_type
187         operator()(_Tp __value)
188         { return _M_dist(_M_engine, __value); }
190       /**
191        * Gets a reference to the underlying uniform random number generator
192        * object.
193        */
194       engine_value_type&
195       engine()
196       { return _M_engine; }
198       /**
199        * Gets a const reference to the underlying uniform random number
200        * generator object.
201        */
202       const engine_value_type&
203       engine() const
204       { return _M_engine; }
206       /**
207        * Gets a reference to the underlying random distribution.
208        */
209       distribution_type&
210       distribution()
211       { return _M_dist; }
213       /**
214        * Gets a const reference to the underlying random distribution.
215        */
216       const distribution_type&
217       distribution() const
218       { return _M_dist; }
220       /**
221        * Gets the closed lower bound of the distribution interval.
222        */
223       result_type
224       min() const
225       { return this->distribution().min(); }
227       /**
228        * Gets the closed upper bound of the distribution interval.
229        */
230       result_type
231       max() const
232       { return this->distribution().max(); }
234     private:
235       engine_value_type _M_engine;
236       distribution_type _M_dist;
237     };
240   /**
241    * @addtogroup tr1_random_generators Random Number Generators
242    * @ingroup tr1_random
243    *
244    * These classes define objects which provide random or pseudorandom numbers,
245    * either from a discrete or a continuous interval.  The random number
246    * generator supplied as a part of this library are all uniform random number
247    * generators which provide a sequence of random number uniformly distributed
248    * over their range.
249    *
250    * A number generator is a function object with an operator() that takes zero
251    * arguments and returns a number.
252    *
253    * A compliant random number generator must satisy the following requirements.
254    * <table border=1 cellpadding=10 cellspacing=0>
255    * <caption align=top>Random Number Generator Requirements</caption>
256    * <tr><td>To be documented.</td></tr>
257    * </table>
258    * 
259    * @{
260    */
262   /**
263    * @brief A model of a linear congruential random number generator.
264    *
265    * A random number generator that produces pseudorandom numbers using the
266    * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
267    *
268    * The template parameter @p _UIntType must be an unsigned integral type
269    * large enough to store values up to (__m-1). If the template parameter
270    * @p __m is 0, the modulus @p __m used is
271    * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
272    * parameters @p __a and @p __c must be less than @p __m.
273    *
274    * The size of the state is @f$ 1 @f$.
275    */
276   template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
277     class linear_congruential;
279   template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
280            typename _CharT, typename _Traits>
281     std::basic_ostream<_CharT, _Traits>&
282     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
283                const linear_congruential<_UIntType, __a, __c, __m>& __lcr);
285   template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
286            typename _CharT, typename _Traits>
287     std::basic_istream<_CharT, _Traits>&
288     operator>>(std::basic_istream<_CharT, _Traits>& __is,
289                linear_congruential<_UIntType, __a, __c, __m>& __lcr);
291   template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
292     class linear_congruential
293     {
294       __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
295       //  __glibcpp_class_requires(__a < __m && __c < __m)
297     public:
298       /** The type of the generated random value. */
299       typedef _UIntType result_type;
301       /** The multiplier. */
302       static const _UIntType multiplier = __a;
303       /** An increment. */
304       static const _UIntType increment = __c;
305       /** The modulus. */
306       static const _UIntType modulus = __m;
308       /**
309        * Constructs a %linear_congruential random number generator engine with
310        * seed @p __s.  The default seed value is 1.
311        *
312        * @param __s The initial seed value.
313        */
314       explicit
315       linear_congruential(unsigned long __x0 = 1)
316       { this->seed(__x0); }
318       /**
319        * Constructs a %linear_congruential random number generator engine
320        * seeded from the generator function @p __g.
321        *
322        * @param __g The seed generator function.
323        */
324       template<class _Gen>
325         linear_congruential(_Gen& __g)
326         { this->seed(__g); }
328       /**
329        * Reseeds the %linear_congruential random number generator engine
330        * sequence to the seed @g __s.
331        *
332        * @param __s The new seed.
333        */
334       void
335       seed(unsigned long __s = 1);
337       /**
338        * Reseeds the %linear_congruential random number generator engine
339        * sequence using values from the generator function @p __g.
340        *
341        * @param __g the seed generator function.
342        */
343       template<class _Gen>
344         void
345         seed(_Gen& __g)
346         { seed(__g, typename is_fundamental<_Gen>::type()); }
348       /**
349        * Gets the smallest possible value in the output range.
350        */
351       result_type
352       min() const;
354       /**
355        * Gets the largest possible value in the output range.
356        */
357       result_type
358       max() const;
360       /**
361        * Gets the next random number in the sequence.
362        */
363       result_type
364       operator()();
366       /**
367        * Compares two linear congruential random number generator objects of the
368        * same type for equality.
369        *  
370        * @param __lhs A linear congruential random number generator object.
371        * @param __rhs Another linear congruential random number generator obj.
372        *
373        * @returns true if the two objects are equal, false otherwise.
374        */
375       friend bool
376       operator==(const linear_congruential& __lhs,
377                  const linear_congruential& __rhs)
378       { return __lhs._M_x == __rhs._M_x; }
380       /**
381        * Compares two linear congruential random number generator objects of the
382        * same type for inequality.
383        *
384        * @param __lhs A linear congruential random number generator object.
385        * @param __rhs Another linear congruential random number generator obj.
386        *
387        * @returns true if the two objects are not equal, false otherwise.
388        */
389       friend bool
390       operator!=(const linear_congruential& __lhs,
391                  const linear_congruential& __rhs)
392       { return !(__lhs == __rhs); }
394       /**
395        * Writes the textual representation of the state x(i) of x to @p __os.
396        *
397        * @param __os  The output stream.
398        * @param __lcr A % linear_congruential random number generator.
399        * @returns __os.
400        */
401       template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
402                _UIntType1 __m1,
403                typename _CharT, typename _Traits>
404         friend std::basic_ostream<_CharT, _Traits>&
405         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
406                    const linear_congruential<_UIntType1, __a1, __c1,
407                    __m1>& __lcr);
409       /**
410        * Sets the state of the engine by reading its textual
411        * representation from @p __is.
412        *
413        * The textual representation must have been previously written using an
414        * output stream whose imbued locale and whose type's template
415        * specialization arguments _CharT and _Traits were the same as those of
416        * @p __is.
417        *
418        * @param __is  The input stream.
419        * @param __lcr A % linear_congruential random number generator.
420        * @returns __is.
421        */
422       template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
423                _UIntType1 __m1,
424                typename _CharT, typename _Traits>
425         friend std::basic_istream<_CharT, _Traits>&
426         operator>>(std::basic_istream<_CharT, _Traits>& __is,
427                    linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
429     private:
430       template<class _Gen>
431         void
432         seed(_Gen& __g, true_type)
433         { return seed(static_cast<unsigned long>(__g)); }
435       template<class _Gen>
436         void
437         seed(_Gen& __g, false_type);
439     private:
440       _UIntType _M_x;
441     };
443   /**
444    * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
445    */
446   typedef linear_congruential<unsigned int, 16807, 0, 2147483647> minstd_rand0;
448   /**
449    * An alternative LCR (Lehmer Generator function) .
450    */
451   typedef linear_congruential<unsigned int, 48271, 0, 2147483647> minstd_rand;
454   /**
455    * A generalized feedback shift register discrete random number generator.
456    *
457    * This algorithm avoind multiplication and division and is designed to be
458    * friendly to a pipelined architecture.  If the parameters are chosen
459    * correctly, this generator will produce numbers with a very long period and
460    * fairly good apparent entropy, although still not cryptographically strong.
461    *
462    * The best way to use this generator is with the predefined mt19937 class.
463    *
464    * This algorithm was originally invented by Makoto Matsumoto and
465    * Takuji Nishimura.
466    *
467    * @var word_size   The number of bits in each element of the state vector.
468    * @var state_size  The degree of recursion.
469    * @var shift_size  The period parameter.
470    * @var mask_bits   The separation point bit index.
471    * @var parameter_a The last row of the twist matrix.
472    * @var output_u    The first right-shift tempering matrix parameter.
473    * @var output_s    The first left-shift tempering matrix parameter.
474    * @var output_b    The first left-shift tempering matrix mask.
475    * @var output_t    The second left-shift tempering matrix parameter.
476    * @var output_c    The second left-shift tempering matrix mask.
477    * @var output_l    The second right-shift tempering matrix parameter.
478    */
479   template<class _UIntType, int __w, int __n, int __m, int __r,
480            _UIntType __a, int __u, int __s, _UIntType __b, int __t,
481            _UIntType __c, int __l>
482     class mersenne_twister;
484   template<class _UIntType, int __w, int __n, int __m, int __r,
485            _UIntType __a, int __u, int __s, _UIntType __b, int __t,
486            _UIntType __c, int __l,
487            typename _CharT, typename _Traits>
488     std::basic_ostream<_CharT, _Traits>&
489     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
490                const mersenne_twister<_UIntType, __w, __n, __m,
491                __r, __a, __u, __s, __b, __t, __c, __l>& __x);
493   template<class _UIntType, int __w, int __n, int __m, int __r,
494            _UIntType __a, int __u, int __s, _UIntType __b, int __t,
495            _UIntType __c, int __l,
496            typename _CharT, typename _Traits>
497     std::basic_istream<_CharT, _Traits>&
498     operator>>(std::basic_istream<_CharT, _Traits>& __is,
499                mersenne_twister<_UIntType, __w, __n, __m,
500                __r, __a, __u, __s, __b, __t, __c, __l>& __x);
502   template<class _UIntType, int __w, int __n, int __m, int __r,
503            _UIntType __a, int __u, int __s, _UIntType __b, int __t,
504            _UIntType __c, int __l>
505     class mersenne_twister
506     {
507       __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
509     public:
510       // types
511       typedef _UIntType result_type;
513       // parameter values
514       static const int       word_size   = __w;
515       static const int       state_size  = __n;
516       static const int       shift_size  = __m;
517       static const int       mask_bits   = __r;
518       static const _UIntType parameter_a = __a;
519       static const int       output_u    = __u;
520       static const int       output_s    = __s;
521       static const _UIntType output_b    = __b;
522       static const int       output_t    = __t;
523       static const _UIntType output_c    = __c;
524       static const int       output_l    = __l;
526       // constructors and member function
527       mersenne_twister()
528       { seed(); }
530       explicit
531       mersenne_twister(unsigned long __value)
532       { seed(__value); }
534       template<class _Gen>
535         mersenne_twister(_Gen& __g)
536         { seed(__g); }
538       void
539       seed()
540       { seed(5489UL); }
542       void
543       seed(unsigned long __value);
545       template<class _Gen>
546         void
547         seed(_Gen& __g)
548         { seed(__g, typename is_fundamental<_Gen>::type()); }
550       result_type
551       min() const
552       { return 0; };
554       result_type
555       max() const
556       { return _Private::_Shift<_UIntType, __w>::__value - 1; }
558       result_type
559       operator()();
561       /**
562        * Compares two % mersenne_twister random number generator objects of
563        * the same type for equality.
564        *
565        * @param __lhs A % mersenne_twister random number generator object.
566        * @param __rhs Another % mersenne_twister random number generator
567        *              object.
568        *
569        * @returns true if the two objects are equal, false otherwise.
570        */
571       friend bool
572       operator==(const mersenne_twister& __lhs,
573                  const mersenne_twister& __rhs)
574       { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
576       /**
577        * Compares two % mersenne_twister random number generator objects of
578        * the same type for inequality.
579        *
580        * @param __lhs A % mersenne_twister random number generator object.
581        * @param __rhs Another % mersenne_twister random number generator
582        *              object.
583        *
584        * @returns true if the two objects are not equal, false otherwise.
585        */
586       friend bool
587       operator!=(const mersenne_twister& __lhs,
588                  const mersenne_twister& __rhs)
589       { return !(__lhs == __rhs); }
591       /**
592        * Inserts the current state of a % mersenne_twister random number
593        * generator engine @p __x into the output stream @p __os.
594        *
595        * @param __os An output stream.
596        * @param __x  A % mersenne_twister random number generator engine.
597        *
598        * @returns The output stream with the state of @p __x inserted or in
599        * an error state.
600        */
601       template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
602                _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
603                _UIntType1 __c1, int __l1,
604                typename _CharT, typename _Traits>
605         friend std::basic_ostream<_CharT, _Traits>&
606         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
607                    const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
608                    __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
610       /**
611        * Extracts the current state of a % mersenne_twister random number
612        * generator engine @p __x from the input stream @p __is.
613        *
614        * @param __is An input stream.
615        * @param __x  A % mersenne_twister random number generator engine.
616        *
617        * @returns The input stream with the state of @p __x extracted or in
618        * an error state.
619        */
620       template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
621                _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
622                _UIntType1 __c1, int __l1,
623                typename _CharT, typename _Traits>
624         friend std::basic_istream<_CharT, _Traits>&
625         operator>>(std::basic_istream<_CharT, _Traits>& __is,
626                    mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
627                    __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
629     private:
630       template<class _Gen>
631         void
632         seed(_Gen& __g, true_type)
633         { return seed(static_cast<unsigned long>(__g)); }
635       template<class _Gen>
636         void
637         seed(_Gen& __g, false_type);
639     private:
640       _UIntType _M_x[state_size];
641       int       _M_p;
642     };
644   /**
645    * The classic Mersenne Twister.
646    *
647    * Reference:
648    * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
649    * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
650    * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
651    */
652   typedef mersenne_twister<
653     unsigned long, 32, 624, 397, 31,
654     0x9908b0dful, 11, 7,
655     0x9d2c5680ul, 15,
656     0xefc60000ul, 18
657     > mt19937;
660   /**
661    * @brief The Marsaglia-Zaman generator.
662    * 
663    * This is a model of a Generalized Fibonacci discrete random number
664    * generator, sometimes referred to as the SWC generator.
665    *
666    * A discrete random number generator that produces pseudorandom numbers using
667    * @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m @f$.
668    *
669    * The size of the state is @f$ r @f$
670    * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
671    *
672    * N1688[4.13] says "the template parameter _IntType shall denote an integral
673    * type large enough to store values up to m."
674    *
675    * @if maint
676    * @var _M_x     The state of te generator.  This is a ring buffer.
677    * @var _M_carry The carry.
678    * @var _M_p     Current index of x(i - r).
679    * @endif
680    */
681   template<typename _IntType, _IntType __m, int __s, int __r>
682     class subtract_with_carry;
684   template<typename _IntType, _IntType __m, int __s, int __r,
685            typename _CharT, typename _Traits>
686     std::basic_ostream<_CharT, _Traits>&
687     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
688                const subtract_with_carry<_IntType, __m, __s, __r>& __x);
690   template<typename _IntType, _IntType __m, int __s, int __r,
691            typename _CharT, typename _Traits>
692     std::basic_istream<_CharT, _Traits>&
693     operator>>(std::basic_istream<_CharT, _Traits>& __is,
694                subtract_with_carry<_IntType, __m, __s, __r>& __x);
696   template<typename _IntType, _IntType __m, int __s, int __r>
697     class subtract_with_carry
698     {
699       __glibcxx_class_requires(_IntType, _IntegerConcept)
701     public:
702       /** The type of the generated random value. */
703       typedef _IntType result_type;
704       
705       // parameter values
706       static const _IntType modulus   = __m;
707       static const int      long_lag  = __r;
708       static const int      short_lag = __s;
710     public:
711       /**
712        * Constructs a default-initialized % subtract_with_carry random number
713        * generator.
714        */
715       subtract_with_carry()
716       { this->seed(); }
718       /**
719        * Constructs an explicitly seeded % subtract_with_carry random number
720        * generator.
721        */
722       explicit
723       subtract_with_carry(unsigned long __value)
724       { this->seed(__value); }
726       /**
727        * Constructs a % subtract_with_carry random number generator seeded from
728        * the PAD iterated by [__first, last).
729        */
730       template<class _Gen>
731         subtract_with_carry(_Gen& __g)
732         { this->seed(__g); }
734       /**
735        * Seeds the initial state @f$ x_0 @f$ of the random number generator.
736        *
737        * @note This implementation follows the tr1 specification but will
738        * obviously not work correctly on all platforms, since it has hardcoded
739        * values that may overflow ints on some platforms.
740        *
741        * N1688[4.19] modifies this as follows.
742        * If @p __value == 0, sets value to 19780503.  In any case, with a linear
743        * congruential generator lcg(i) having parameters @f$ m_{lcg} =
744        * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value @f$, sets
745        * @f$ x_{-r} \dots x_{-1} @f$ to
746        * @f$ lcg(1) \bmod m \dots lcg(r) \bmod m @f$ respectively.
747        * If @f$ x_{-1} = 0 @f$ set carry to 1, otherwise sets carry to 0.
748        */
749       void
750       seed(unsigned long __value = 19780503);
752       /**
753        * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
754        * random number generator.
755        */
756       template<class _Gen>
757         void
758         seed(_Gen& __g)
759         { seed(__g, typename is_fundamental<_Gen>::type()); }
761       /**
762        * Gets the inclusive minimum value of the range of random integers
763        * returned by this generator.
764        */
765       result_type
766       min() const
767       { return 0; }
769       /**
770        * Gets the inclusive maximum value of the range of random integers
771        * returned by this generator.
772        */
773       result_type
774       max() const
775       { return this->modulus - 1; }
777       /**
778        * Gets the next random number in the sequence.
779        */
780       result_type
781       operator()();
783       /**
784        * Compares two % subtract_with_carry random number generator objects of
785        * the same type for equality.
786        *
787        * @param __lhs A % subtract_with_carry random number generator object.
788        * @param __rhs Another % subtract_with_carry random number generator
789        *              object.
790        *
791        * @returns true if the two objects are equal, false otherwise.
792        */
793       friend bool
794       operator==(const subtract_with_carry& __lhs,
795                  const subtract_with_carry& __rhs)
796       { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
798       /**
799        * Compares two % subtract_with_carry random number generator objects of
800        * the same type for inequality.
801        *
802        * @param __lhs A % subtract_with_carry random number generator object.
803        * @param __rhs Another % subtract_with_carry random number generator
804        *              object.
805        *
806        * @returns true if the two objects are not equal, false otherwise.
807        */
808       friend bool
809       operator!=(const subtract_with_carry& __lhs,
810                  const subtract_with_carry& __rhs)
811       { return !(__lhs == __rhs); }
813       /**
814        * Inserts the current state of a % subtract_with_carry random number
815        * generator engine @p __x into the output stream @p __os.
816        *
817        * @param __os An output stream.
818        * @param __x  A % subtract_with_carry random number generator engine.
819        *
820        * @returns The output stream with the state of @p __x inserted or in
821        * an error state.
822        */
823       template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
824                typename _CharT, typename _Traits>
825         friend std::basic_ostream<_CharT, _Traits>&
826         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
827                    const subtract_with_carry<_IntType1, __m1, __s1,
828                    __r1>& __x);
830       /**
831        * Extracts the current state of a % subtract_with_carry random number
832        * generator engine @p __x from the input stream @p __is.
833        *
834        * @param __is An input stream.
835        * @param __x  A % subtract_with_carry random number generator engine.
836        *
837        * @returns The input stream with the state of @p __x extracted or in
838        * an error state.
839        */
840       template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
841                typename _CharT, typename _Traits>
842         friend std::basic_istream<_CharT, _Traits>&
843         operator>>(std::basic_istream<_CharT, _Traits>& __is,
844                    subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
846     private:
847       template<class _Gen>
848         void
849         seed(_Gen& __g, true_type)
850         { return seed(static_cast<unsigned long>(__g)); }
852       template<class _Gen>
853         void
854         seed(_Gen& __g, false_type);
856     private:
857       int         _M_p;
858       result_type _M_x[long_lag];
859       result_type _M_carry;
860     };
863   /**
864    * Produces random numbers from some base engine by discarding blocks of
865    * data.
866    *
867    * 0 <= @p __r <= @p __p
868    */
869   template<class _UniformRandomNumberGenerator, int __p, int __r>
870     class discard_block;
872   template<class _UniformRandomNumberGenerator, int __p, int __r,
873            typename _CharT, typename _Traits>
874     std::basic_ostream<_CharT, _Traits>&
875     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
876                const discard_block<_UniformRandomNumberGenerator,
877                __p, __r>& __x);
879   template<class _UniformRandomNumberGenerator, int __p, int __r,
880            typename _CharT, typename _Traits>
881     std::basic_istream<_CharT, _Traits>&
882     operator>>(std::basic_istream<_CharT, _Traits>& __is,
883                discard_block<_UniformRandomNumberGenerator, __p, __r>& __x);
885   template<class _UniformRandomNumberGenerator, int __p, int __r>
886     class discard_block
887     {
888       // __glibcxx_class_requires(typename base_type::result_type,
889       //                          ArithmeticTypeConcept)
891     public:
892       /** The type of the underlying generator engine. */
893       typedef _UniformRandomNumberGenerator   base_type;
894       /** The type of the generated random value. */
895       typedef typename base_type::result_type result_type;
897       // parameter values
898       static const int block_size = __p;
899       static const int used_block = __r;
901       /**
902        * Constructs a default %discard_block engine.
903        *
904        * The underlying engine is default constructed as well.
905        */
906       discard_block()
907       : _M_n(0) { }
909       /**
910        * Copy constructs a %discard_block engine.
911        *
912        * Copies an existing base class random number geenerator.
913        * @param rng An existing (base class) engine object.
914        */
915       explicit
916       discard_block(const base_type& __rng)
917       : _M_b(__rng), _M_n(0) { }
919       /**
920        * Seed constructs a %discard_block engine.
921        *
922        * Constructs the underlying generator engine seeded with @p __s.
923        * @param __s A seed value for the base class engine.
924        */
925       explicit
926       discard_block(unsigned long __s)
927       : _M_b(__s), _M_n(0) { }
929       /**
930        * Generator constructs a %discard_block engine.
931        *
932        * @param __g A seed generator function.
933        */
934       template<class _Gen>
935         discard_block(_Gen& __g)
936         : _M_b(__g), _M_n(0) { }
938       /**
939        * Reseeds the %discard_block object with the default seed for the
940        * underlying base class generator engine.
941        */
942       void seed()
943       {
944         _M_b.seed();
945         _M_n = 0;
946       }
948       /**
949        * Reseeds the %discard_block object with the given seed generator
950        * function.
951        * @param __g A seed generator function.
952        */
953       template<class _Gen>
954         void seed(_Gen& __g)
955         {
956           _M_b.seed(__g);
957           _M_n = 0;
958         }
960       /**
961        * Gets a const reference to the underlying generator engine object.
962        */
963       const base_type&
964       base() const
965       { return _M_b; }
967       /**
968        * Gets the minimum value in the generated random number range.
969        */
970       result_type
971       min() const
972       { return _M_b.min(); }
974       /**
975        * Gets the maximum value in the generated random number range.
976        */
977       result_type
978       max() const
979       { return _M_b.max(); }
981       /**
982        * Gets the next value in the generated random number sequence.
983        */
984       result_type
985       operator()();
987       /**
988        * Compares two %discard_block random number generator objects of
989        * the same type for equality.
990        *
991        * @param __lhs A %discard_block random number generator object.
992        * @param __rhs Another %discard_block random number generator
993        *              object.
994        *
995        * @returns true if the two objects are equal, false otherwise.
996        */
997       friend bool
998       operator==(const discard_block& __lhs, const discard_block& __rhs)
999       { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
1001       /**
1002        * Compares two %discard_block random number generator objects of
1003        * the same type for inequality.
1004        *
1005        * @param __lhs A %discard_block random number generator object.
1006        * @param __rhs Another %discard_block random number generator
1007        *              object.
1008        *
1009        * @returns true if the two objects are not equal, false otherwise.
1010        */
1011       friend bool
1012       operator!=(const discard_block& __lhs, const discard_block& __rhs)
1013       { return !(__lhs == __rhs); }
1015       /**
1016        * Inserts the current state of a %discard_block random number
1017        * generator engine @p __x into the output stream @p __os.
1018        *
1019        * @param __os An output stream.
1020        * @param __x  A %discard_block random number generator engine.
1021        *
1022        * @returns The output stream with the state of @p __x inserted or in
1023        * an error state.
1024        */
1025       template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1026                typename _CharT, typename _Traits>
1027         friend std::basic_ostream<_CharT, _Traits>&
1028         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1029                    const discard_block<_UniformRandomNumberGenerator1,
1030                    __p1, __r1>& __x);
1032       /**
1033        * Extracts the current state of a % subtract_with_carry random number
1034        * generator engine @p __x from the input stream @p __is.
1035        *
1036        * @param __is An input stream.
1037        * @param __x  A %discard_block random number generator engine.
1038        *
1039        * @returns The input stream with the state of @p __x extracted or in
1040        * an error state.
1041        */
1042       template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1043                typename _CharT, typename _Traits>
1044         friend std::basic_istream<_CharT, _Traits>&
1045         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1046                    discard_block<_UniformRandomNumberGenerator1,
1047                    __p1, __r1>& __x);
1049     private:
1050       base_type _M_b;
1051       int       _M_n;
1052     };
1055   /**
1056    * James's luxury-level-3 integer adaptation of Luescher's generator.
1057    */
1058   typedef discard_block<
1059     subtract_with_carry<int, (1 << 24), 10, 24>,
1060       223,
1061       24
1062       > ranlux3;
1064   /**
1065    * James's luxury-level-4 integer adaptation of Luescher's generator.
1066    */
1067   typedef discard_block<
1068     subtract_with_carry<int, (1 << 24), 10, 24>,
1069       389,
1070       24
1071       > ranlux4;
1074   /**
1075    * A random number generator adaptor class that combines two random number
1076    * generator engines into a single output sequence.
1077    */
1078   template<class _UniformRandomNumberGenerator1, int __s1,
1079            class _UniformRandomNumberGenerator2, int __s2>
1080     class xor_combine;
1082   template<class _UniformRandomNumberGenerator1, int __s1,
1083            class _UniformRandomNumberGenerator2, int __s2,
1084            typename _CharT, typename _Traits>
1085     std::basic_ostream<_CharT, _Traits>&
1086     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1087                const xor_combine<_UniformRandomNumberGenerator1, __s1,
1088                _UniformRandomNumberGenerator2, __s2>& __x);
1090   template<class _UniformRandomNumberGenerator1, int __s1,
1091            class _UniformRandomNumberGenerator2, int __s2,
1092            typename _CharT, typename _Traits>
1093     std::basic_istream<_CharT, _Traits>&
1094     operator>>(std::basic_istream<_CharT, _Traits>& __is,
1095                xor_combine<_UniformRandomNumberGenerator1, __s1,
1096                _UniformRandomNumberGenerator2, __s2>& __x);
1098   template<class _UniformRandomNumberGenerator1, int __s1,
1099            class _UniformRandomNumberGenerator2, int __s2>
1100     class xor_combine
1101     {
1102       // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1103       //                          result_type, ArithmeticTypeConcept)
1104       // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1105       //                          result_type, ArithmeticTypeConcept)
1107     public:
1108       /** The type of the the first underlying generator engine. */
1109       typedef _UniformRandomNumberGenerator1   base1_type;
1110       /** The type of the the second underlying generator engine. */
1111       typedef _UniformRandomNumberGenerator2   base2_type;
1113     private:
1114       typedef typename base1_type::result_type _Result_type1;
1115       typedef typename base2_type::result_type _Result_type2;
1117     public:
1118       /** The type of the generated random value. */
1119       typedef typename _Private::_Select<
1120         (sizeof(_Result_type1) > sizeof(_Result_type2)),
1121         _Result_type1, _Result_type2>::_Type result_type;
1123       // parameter values
1124       static const int shift1 = __s1;
1125       static const int shift2 = __s2;
1127       // constructors and member function
1128       xor_combine() { }
1130       xor_combine(const base1_type& __rng1, const base2_type& __rng2)
1131       : _M_b1(__rng1), _M_b2(__rng2) { }
1133       xor_combine(unsigned long __s)
1134       : _M_b1(__s), _M_b2(__s + 1) { }
1136       template<class _Gen>
1137         xor_combine(_Gen& __g)
1138         : _M_b1(__g), _M_b2(__g) { }
1140       void
1141       seed()
1142       {
1143         _M_b1.seed();
1144         _M_b2.seed();
1145       }
1147       template<class _Gen>
1148         void
1149         seed(_Gen& __g)
1150         {
1151           _M_b1.seed(__g);
1152           _M_b2.seed(__g);
1153         }
1155       const base1_type&
1156       base1() const
1157       { return _M_b1; }
1159       const base2_type&
1160       base2() const
1161       { return _M_b2; }
1163       result_type
1164       min() const
1165       { return _M_b1.min() ^ _M_b2.min(); }
1167       result_type
1168       max() const
1169       { return _M_b1.max() | _M_b2.max(); }
1171       /**
1172        * Gets the next random number in the sequence.
1173        */
1174       result_type
1175       operator()()
1176       { return ((_M_b1() << shift1) ^ (_M_b2() << shift2)); }
1178       /**
1179        * Compares two %xor_combine random number generator objects of
1180        * the same type for equality.
1181        *
1182        * @param __lhs A %xor_combine random number generator object.
1183        * @param __rhs Another %xor_combine random number generator
1184        *              object.
1185        *
1186        * @returns true if the two objects are equal, false otherwise.
1187        */
1188       friend bool
1189       operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1190       {
1191         return (__lhs.base1() == __rhs.base1())
1192                 && (__lhs.base2() == __rhs.base2());
1193       }
1195       /**
1196        * Compares two %xor_combine random number generator objects of
1197        * the same type for inequality.
1198        *
1199        * @param __lhs A %xor_combine random number generator object.
1200        * @param __rhs Another %xor_combine random number generator
1201        *              object.
1202        *
1203        * @returns true if the two objects are not equal, false otherwise.
1204        */
1205       friend bool
1206       operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1207       { return !(__lhs == __rhs); }
1209       /**
1210        * Inserts the current state of a %xor_combine random number
1211        * generator engine @p __x into the output stream @p __os.
1212        *
1213        * @param __os An output stream.
1214        * @param __x  A %xor_combine random number generator engine.
1215        *
1216        * @returns The output stream with the state of @p __x inserted or in
1217        * an error state.
1218        */
1219       template<class _UniformRandomNumberGenerator11, int __s11,
1220                class _UniformRandomNumberGenerator21, int __s21,
1221                typename _CharT, typename _Traits>
1222         friend std::basic_ostream<_CharT, _Traits>&
1223         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1224                    const xor_combine<_UniformRandomNumberGenerator11, __s11,
1225                    _UniformRandomNumberGenerator21, __s21>& __x);
1227       /**
1228        * Extracts the current state of a %xor_combine random number
1229        * generator engine @p __x from the input stream @p __is.
1230        *
1231        * @param __is An input stream.
1232        * @param __x  A %xor_combine random number generator engine.
1233        *
1234        * @returns The input stream with the state of @p __x extracted or in
1235        * an error state.
1236        */
1237       template<class _UniformRandomNumberGenerator11, int __s11,
1238                class _UniformRandomNumberGenerator21, int __s21,
1239                typename _CharT, typename _Traits>
1240         friend std::basic_istream<_CharT, _Traits>&
1241         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1242                    xor_combine<_UniformRandomNumberGenerator11, __s11,
1243                    _UniformRandomNumberGenerator21, __s21>& __x);
1245     private:
1246       base1_type _M_b1;
1247       base2_type _M_b2;
1248     };
1251   /**
1252    * A standard interface to a platform-specific non-deterministic random number
1253    * generator (if any are available).
1254    */
1255   class random_device
1256   {
1257   public:
1258     // types
1259     typedef unsigned int result_type;
1261     // constructors, destructors and member functions
1263 #ifdef _GLIBCXX_USE_RANDOM_TR1
1265     explicit
1266     random_device(const std::string& __token = "/dev/urandom")
1267     {
1268       if ((__token != "/dev/urandom" && __token != "/dev/random")
1269           || !_M_filebuf.open(__token.c_str(),
1270                               std::ios_base::in | std::ios_base::binary))
1271         std::__throw_runtime_error(__N("random_device::"
1272                                        "random_device(const std::string&)"));
1273     }
1275     ~random_device()
1276     { _M_filebuf.close(); }
1278 #else
1280     explicit
1281     random_device(const std::string& __token = "mt19937")
1282     : _M_mt(_M_strtoul(__token)) { }
1284   private:
1285     static unsigned long
1286     _M_strtoul(const std::string& __str)
1287     {
1288       unsigned long __ret = 5489UL;
1289       if (__str != "mt19937")
1290         {
1291           const char* __nptr = __str.c_str();
1292           char* __endptr;
1293           __ret = std::strtoul(__nptr, &__endptr, 0);
1294           if (*__nptr == '\0' || *__endptr != '\0')
1295             std::__throw_runtime_error(__N("random_device::_M_strtoul"
1296                                            "(const std::string&)"));
1297         }
1298       return __ret;
1299     }
1301   public:
1303 #endif
1305     result_type
1306     min() const
1307     { return std::numeric_limits<result_type>::min(); }
1309     result_type
1310     max() const
1311     { return std::numeric_limits<result_type>::max(); }
1313     double
1314     entropy() const
1315     { return 0.0; }
1317     result_type
1318     operator()()
1319     {
1320 #ifdef _GLIBCXX_USE_RANDOM_TR1
1321       result_type __ret;
1322       _M_filebuf.sgetn(reinterpret_cast<char*>(&__ret), sizeof(result_type));
1323       return __ret;
1324 #else
1325       return _M_mt();
1326 #endif
1327     }
1329   private:
1330     random_device(const random_device&);
1331     void operator=(const random_device&);
1333 #ifdef _GLIBCXX_USE_RANDOM_TR1
1334     std::filebuf _M_filebuf;
1335 #else
1336     mt19937      _M_mt;
1337 #endif
1338   };
1340   /* @} */ // group tr1_random_generators
1342   /**
1343    * @addtogroup tr1_random_distributions Random Number Distributions
1344    * @ingroup tr1_random
1345    * @{
1346    */
1348   /**
1349    * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1350    * @ingroup tr1_random_distributions
1351    * @{
1352    */
1354   /**
1355    * @brief Uniform discrete distribution for random numbers.
1356    * A discrete random distribution on the range @f$[min, max]@f$ with equal
1357    * probability throughout the range.
1358    */
1359   template<typename _IntType = int>
1360     class uniform_int;
1362   template<typename _IntType, typename _CharT, typename _Traits>
1363     std::basic_ostream<_CharT, _Traits>&
1364     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1365                const uniform_int<_IntType>& __x);
1367   template<typename _IntType, typename _CharT, typename _Traits>
1368     std::basic_istream<_CharT, _Traits>&
1369     operator>>(std::basic_istream<_CharT, _Traits>& __is,
1370                uniform_int<_IntType>& __x);
1372   template<typename _IntType>
1373     class uniform_int
1374     {
1375       __glibcxx_class_requires(_IntType, _IntegerConcept)
1377     public:
1378       /** The type of the parameters of the distribution. */
1379       typedef _IntType input_type;
1380       /** The type of the range of the distribution. */
1381       typedef _IntType result_type;
1383     public:
1384       /**
1385        * Constructs a uniform distribution object.
1386        */
1387       explicit
1388       uniform_int(_IntType __min = 0, _IntType __max = 9)
1389       : _M_min(__min), _M_max(__max)
1390       {
1391         _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1392       }
1394       /**
1395        * Gets the inclusive lower bound of the distribution range.
1396        */
1397       result_type
1398       min() const
1399       { return _M_min; }
1401       /**
1402        * Gets the inclusive upper bound of the distribution range.
1403        */
1404       result_type
1405       max() const
1406       { return _M_max; }
1408       /**
1409        * Resets the distribution state.
1410        *
1411        * Does nothing for the uniform integer distribution.
1412        */
1413       void
1414       reset() { }
1416       /**
1417        * Gets a uniformly distributed random number in the range
1418        * @f$(min, max)@f$.
1419        */
1420       template<typename _UniformRandomNumberGenerator>
1421         result_type
1422         operator()(_UniformRandomNumberGenerator& __urng)
1423         { return (__urng() % (_M_max - _M_min + 1)) + _M_min; }
1425       /**
1426        * Gets a uniform random number in the range @f$[0, n)@f$.
1427        *
1428        * This function is aimed at use with std::random_shuffle.
1429        */
1430       template<typename _UniformRandomNumberGenerator>
1431         result_type
1432         operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1433         { return __urng() % __n; }
1435       /**
1436        * Inserts a %uniform_int random number distribution @p __x into the
1437        * output stream @p os.
1438        *
1439        * @param __os An output stream.
1440        * @param __x  A %uniform_int random number distribution.
1441        *
1442        * @returns The output stream with the state of @p __x inserted or in
1443        * an error state.
1444        */
1445       template<typename _IntType1, typename _CharT, typename _Traits>
1446         friend std::basic_ostream<_CharT, _Traits>&
1447         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1448                    const uniform_int<_IntType1>& __x);
1450       /**
1451        * Extracts a %unform_int random number distribution
1452        * @p __x from the input stream @p __is.
1453        *
1454        * @param __is An input stream.
1455        * @param __x  A %uniform_int random number generator engine.
1456        *
1457        * @returns The input stream with @p __x extracted or in an error state.
1458        */
1459       template<typename _IntType1, typename _CharT, typename _Traits>
1460         friend std::basic_istream<_CharT, _Traits>&
1461         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1462                    uniform_int<_IntType1>& __x);
1464     private:
1465       _IntType _M_min;
1466       _IntType _M_max;
1467     };
1470   /**
1471    * @brief A Bernoulli random number distribution.
1472    *
1473    * Generates a sequence of true and false values with likelihood @f$ p @f$
1474    * that true will come up and @f$ (1 - p) @f$ that false will appear.
1475    */
1476   class bernoulli_distribution;
1478   template<typename _CharT, typename _Traits>
1479     std::basic_ostream<_CharT, _Traits>&
1480     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1481                const bernoulli_distribution& __x);
1483   class bernoulli_distribution
1484   {
1485   public:
1486     typedef int  input_type;
1487     typedef bool result_type;
1489   public:
1490     /**
1491      * Constructs a Bernoulli distribution with likelihood @p p.
1492      *
1493      * @param __p  [IN]  The likelihood of a true result being returned.  Must
1494      * be in the interval @f$ [0, 1] @f$.
1495      */
1496     explicit
1497     bernoulli_distribution(double __p = 0.5)
1498     : _M_p(__p)
1499     { 
1500       _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1501     }
1503     /**
1504      * Gets the @p p parameter of the distribution.
1505      */
1506     double
1507     p() const
1508     { return _M_p; }
1510     /**
1511      * Resets the distribution state.
1512      *
1513      * Does nothing for a bernoulli distribution.
1514      */
1515     void
1516     reset() { }
1518     /**
1519      * Gets the next value in the Bernoullian sequence.
1520      */
1521     template<class UniformRandomNumberGenerator>
1522       result_type
1523       operator()(UniformRandomNumberGenerator& __urng)
1524       {
1525         if (__urng() < _M_p)
1526           return true;
1527         return false;
1528       }
1530     /**
1531      * Inserts a %bernoulli_distribution random number distribution
1532      * @p __x into the output stream @p __os.
1533      *
1534      * @param __os An output stream.
1535      * @param __x  A %bernoulli_distribution random number distribution.
1536      *
1537      * @returns The output stream with the state of @p __x inserted or in
1538      * an error state.
1539      */
1540     template<typename _CharT, typename _Traits>
1541       friend std::basic_ostream<_CharT, _Traits>&
1542       operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1543                  const bernoulli_distribution& __x);
1545     /**
1546      * Extracts a %bernoulli_distribution random number distribution
1547      * @p __x from the input stream @p __is.
1548      *
1549      * @param __is An input stream.
1550      * @param __x  A %bernoulli_distribution random number generator engine.
1551      *
1552      * @returns The input stream with @p __x extracted or in an error state.
1553      */
1554     template<typename _CharT, typename _Traits>
1555       friend std::basic_istream<_CharT, _Traits>&
1556       operator>>(std::basic_istream<_CharT, _Traits>& __is,
1557                  bernoulli_distribution& __x)
1558       { return __is >> __x._M_p; }
1560   protected:
1561     double _M_p;
1562   };
1565   /**
1566    * @brief A discrete geometric random number distribution.
1567    *
1568    * The formula for the geometric probability mass function is 
1569    * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1570    * distribution.
1571    */
1572   template<typename _IntType = int, typename _RealType = double>
1573     class geometric_distribution;
1575   template<typename _IntType, typename _RealType,
1576            typename _CharT, typename _Traits>
1577     std::basic_ostream<_CharT, _Traits>&
1578     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1579                const geometric_distribution<_IntType, _RealType>& __x);
1581   template<typename _IntType, typename _RealType>
1582     class geometric_distribution
1583     {
1584     public:
1585       // types
1586       typedef _RealType input_type;
1587       typedef _IntType  result_type;
1589       // constructors and member function
1590       
1591       explicit
1592       geometric_distribution(const _RealType& __p = _RealType(0.5))
1593       : _M_p(__p), _M_log_p(std::log(_M_p))
1594       {
1595         _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1596       }
1598       /**
1599        * Gets the distribution parameter @p p.
1600        */
1601       _RealType
1602       p() const
1603       { return _M_p; }
1605       void
1606       reset() { }
1608       template<class _UniformRandomNumberGenerator>
1609         result_type
1610         operator()(_UniformRandomNumberGenerator& __urng)
1611         { return result_type(std::ceil(std::log(__urng()) / _M_log_p)); }
1613       /**
1614        * Inserts a %geometric_distribution random number distribution
1615        * @p __x into the output stream @p __os.
1616        *
1617        * @param __os An output stream.
1618        * @param __x  A %geometric_distribution random number distribution.
1619        *
1620        * @returns The output stream with the state of @p __x inserted or in
1621        * an error state.
1622        */
1623       template<typename _IntType1, typename _RealType1,
1624                typename _CharT, typename _Traits>
1625         friend std::basic_ostream<_CharT, _Traits>&
1626         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1627                    const geometric_distribution<_IntType1, _RealType1>& __x);
1629       /**
1630        * Extracts a %geometric_distribution random number distribution
1631        * @p __x from the input stream @p __is.
1632        *
1633        * @param __is An input stream.
1634        * @param __x  A %geometric_distribution random number generator engine.
1635        *
1636        * @returns The input stream with @p __x extracted or in an error state.
1637        */
1638       template<typename _CharT, typename _Traits>
1639         friend std::basic_istream<_CharT, _Traits>&
1640         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1641                    geometric_distribution& __x)
1642         {
1643           __is >> __x._M_p;
1644           __x._M_log_p = std::log(__x._M_p);
1645           return __is;
1646         }
1648     protected:
1649       _RealType _M_p;
1650       _RealType _M_log_p;
1651     };
1653   /* @} */ // group tr1_random_distributions_discrete
1655   /**
1656    * @addtogroup tr1_random_distributions_continuous Continuous Distributions
1657    * @ingroup tr1_random_distributions
1658    * @{
1659    */
1661   /**
1662    * @brief Uniform continuous distribution for random numbers.
1663    *
1664    * A continuous random distribution on the range [min, max) with equal
1665    * probability throughout the range.  The URNG should be real-valued and
1666    * deliver number in the range [0, 1).
1667    */
1668   template<typename _RealType = double>
1669     class uniform_real;
1670   
1671   template<typename _RealType, typename _CharT, typename _Traits>
1672     std::basic_ostream<_CharT, _Traits>&
1673     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1674                const uniform_real<_RealType>& __x);
1676   template<typename _RealType, typename _CharT, typename _Traits>
1677     std::basic_istream<_CharT, _Traits>&
1678     operator>>(std::basic_istream<_CharT, _Traits>& __is,
1679                uniform_real<_RealType>& __x);
1681   template<typename _RealType>
1682     class uniform_real
1683     {
1684     public:
1685       // types
1686       typedef _RealType input_type;
1687       typedef _RealType result_type;
1689     public:
1690       /**
1691        * Constructs a uniform_real object.
1692        *
1693        * @param __min [IN]  The lower bound of the distribution.
1694        * @param __max [IN]  The upper bound of the distribution.
1695        */
1696       explicit
1697       uniform_real(_RealType __min = _RealType(0),
1698                    _RealType __max = _RealType(1))
1699       : _M_min(__min), _M_max(__max)
1700       {
1701         _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1702       }
1704       result_type
1705       min() const
1706       { return _M_min; }
1708       result_type
1709       max() const
1710       { return _M_max; }
1712       void
1713       reset() { }
1715       template<class _UniformRandomNumberGenerator>
1716         result_type
1717         operator()(_UniformRandomNumberGenerator& __urng)
1718         { return (__urng() * (_M_max - _M_min)) + _M_min; }
1720       /**
1721        * Inserts a %uniform_real random number distribution @p __x into the
1722        * output stream @p __os.
1723        *
1724        * @param __os An output stream.
1725        * @param __x  A %uniform_real random number distribution.
1726        *
1727        * @returns The output stream with the state of @p __x inserted or in
1728        * an error state.
1729        */
1730       template<typename _RealType1, typename _CharT, typename _Traits>
1731         friend std::basic_ostream<_CharT, _Traits>&
1732         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1733                    const uniform_real<_RealType1>& __x);
1735       /**
1736        * Extracts a %unform_real random number distribution
1737        * @p __x from the input stream @p __is.
1738        *
1739        * @param __is An input stream.
1740        * @param __x  A %uniform_real random number generator engine.
1741        *
1742        * @returns The input stream with @p __x extracted or in an error state.
1743        */
1744       template<typename _RealType1, typename _CharT, typename _Traits>
1745         friend std::basic_istream<_CharT, _Traits>&
1746         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1747                    uniform_real<_RealType1>& __x);
1749     protected:
1750       _RealType _M_min;
1751       _RealType _M_max;
1752     };
1755   /**
1756    * @brief An exponential continuous distribution for random numbers.
1757    *
1758    * The formula for the exponential probability mass function is 
1759    * @f$ p(x) = \lambda e^{-\lambda x} @f$.
1760    *
1761    * <table border=1 cellpadding=10 cellspacing=0>
1762    * <caption align=top>Distribution Statistics</caption>
1763    * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
1764    * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
1765    * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
1766    * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
1767    * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
1768    * </table>
1769    */
1770   template<typename _RealType = double>
1771     class exponential_distribution;
1773   template<typename _RealType, typename _CharT, typename _Traits>
1774     std::basic_ostream<_CharT, _Traits>&
1775     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1776                const exponential_distribution<_RealType>& __x);
1778   template<typename _RealType>
1779     class exponential_distribution
1780     {
1781     public:
1782       // types
1783       typedef _RealType input_type;
1784       typedef _RealType result_type;
1786     public:
1787       /**
1788        * Constructs an exponential distribution with inverse scale parameter
1789        * @f$ \lambda @f$.
1790        */
1791       explicit
1792       exponential_distribution(const result_type& __lambda = result_type(1))
1793       : _M_lambda(__lambda)
1794       { 
1795         _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
1796       }
1798       /**
1799        * Gets the inverse scale parameter of the distribution.
1800        */
1801       _RealType
1802       lambda() const
1803       { return _M_lambda; }
1805       /**
1806        * Resets the distribution.
1807        *
1808        * Has no effect on exponential distributions.
1809        */
1810       void
1811       reset() { }
1813       template<class _UniformRandomNumberGenerator>
1814         result_type
1815         operator()(_UniformRandomNumberGenerator& __urng)
1816         { return -std::log(__urng()) / _M_lambda; }
1818       /**
1819        * Inserts a %exponential_distribution random number distribution
1820        * @p __x into the output stream @p __os.
1821        *
1822        * @param __os An output stream.
1823        * @param __x  A %exponential_distribution random number distribution.
1824        *
1825        * @returns The output stream with the state of @p __x inserted or in
1826        * an error state.
1827        */
1828       template<typename _RealType1, typename _CharT, typename _Traits>
1829         friend std::basic_ostream<_CharT, _Traits>&
1830         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1831                    const exponential_distribution<_RealType1>& __x);
1833       /**
1834        * Extracts a %exponential_distribution random number distribution
1835        * @p __x from the input stream @p __is.
1836        *
1837        * @param __is An input stream.
1838        * @param __x  A %exponential_distribution random number generator engine.
1839        *
1840        * @returns The input stream with @p __x extracted or in an error state.
1841        */
1842       template<typename _CharT, typename _Traits>
1843         friend std::basic_istream<_CharT, _Traits>&
1844         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1845                    exponential_distribution& __x)
1846         { return __is >> __x._M_lambda; }
1848     private:
1849       result_type _M_lambda;
1850     };
1853   /**
1854    * @brief A normal continuous distribution for random numbers.
1855    *
1856    * The formula for the normal probability mass function is 
1857    * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}} 
1858    *            e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
1859    */
1860   template<typename _RealType = double>
1861     class normal_distribution;
1863   template<typename _RealType, typename _CharT, typename _Traits>
1864     std::basic_ostream<_CharT, _Traits>&
1865     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1866                const normal_distribution<_RealType>& __x);
1868   template<typename _RealType, typename _CharT, typename _Traits>
1869     std::basic_istream<_CharT, _Traits>&
1870     operator>>(std::basic_istream<_CharT, _Traits>& __is,
1871                normal_distribution<_RealType>& __x);
1873   template<typename _RealType>
1874     class normal_distribution
1875     {
1876     public:
1877       // types
1878       typedef _RealType input_type;
1879       typedef _RealType result_type;
1881     public:
1882       /**
1883        * Constructs a normal distribution with parameters @f$ mean @f$ and
1884        * @f$ \sigma @f$.
1885        */
1886       explicit
1887       normal_distribution(const result_type& __mean = result_type(0),
1888                           const result_type& __sigma = result_type(1))
1889       : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
1890       { 
1891         _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
1892       }
1894       /**
1895        * Gets the mean of the distribution.
1896        */
1897       _RealType
1898       mean() const
1899       { return _M_mean; }
1901       /**
1902        * Gets the @f$ \sigma @f$ of the distribution.
1903        */
1904       _RealType
1905       sigma() const
1906       { return _M_sigma; }
1908       /**
1909        * Resets the distribution.
1910        */
1911       void
1912       reset()
1913       { _M_saved_available = false; }
1915       template<class _UniformRandomNumberGenerator>
1916         result_type
1917         operator()(_UniformRandomNumberGenerator& __urng);
1919       /**
1920        * Inserts a %normal_distribution random number distribution
1921        * @p __x into the output stream @p __os.
1922        *
1923        * @param __os An output stream.
1924        * @param __x  A %normal_distribution random number distribution.
1925        *
1926        * @returns The output stream with the state of @p __x inserted or in
1927        * an error state.
1928        */
1929       template<typename _RealType1, typename _CharT, typename _Traits>
1930         friend std::basic_ostream<_CharT, _Traits>&
1931         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1932                    const normal_distribution<_RealType1>& __x);
1934       /**
1935        * Extracts a %normal_distribution random number distribution
1936        * @p __x from the input stream @p __is.
1937        *
1938        * @param __is An input stream.
1939        * @param __x  A %normal_distribution random number generator engine.
1940        *
1941        * @returns The input stream with @p __x extracted or in an error state.
1942        */
1943       template<typename _RealType1, typename _CharT, typename _Traits>
1944         friend std::basic_istream<_CharT, _Traits>&
1945         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1946                    normal_distribution<_RealType1>& __x);
1948     private:
1949       result_type _M_mean;
1950       result_type _M_sigma;
1951       result_type _M_saved;
1952       bool        _M_saved_available;     
1953     };
1955   /* @} */ // group tr1_random_distributions_continuous
1956   /* @} */ // group tr1_random_distributions
1957   /* @} */ // group tr1_random
1959 _GLIBCXX_END_NAMESPACE
1962 #include <tr1/random.tcc>
1964 #endif // _STD_TR1_RANDOM