OSDN Git Service

55c7b5ad05c20e69b75414282d340f5378359c16
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / random.h
1 // random number generation -*- C++ -*-
2
3 // Copyright (C) 2009 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.
10
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.
15
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.
20
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.
29
30 /**
31  * @file bits/random.h
32  *  This is an internal header file, included by other library headers.
33  *  You should not attempt to use it directly.
34  */
35
36 #include <vector>
37
38 namespace std
39 {
40
41   // [26.4] Random number generation
42
43   /**
44    * @addtogroup std_random Random Number Generation
45    * A facility for generating random numbers on selected distributions.
46    * @{
47    */
48
49   /**
50    * @brief A function template for converting the output of a (integral)
51    * uniform random number generator to a floatng point result in the range
52    * [0-1).
53    */
54   template<typename _RealType, size_t __bits,
55            typename _UniformRandomNumberGenerator>
56     _RealType
57     generate_canonical(_UniformRandomNumberGenerator& __g);
58
59   class seed_seq;
60
61   /*
62    * Implementation-space details.
63    */
64   namespace __detail
65   {
66     template<typename _UIntType, size_t __w,
67              bool = __w < static_cast<size_t>
68                           (std::numeric_limits<_UIntType>::digits)>
69       struct _Shift
70       { static const _UIntType __value = 0; };
71
72     template<typename _UIntType, size_t __w>
73       struct _Shift<_UIntType, __w, true>
74       { static const _UIntType __value = _UIntType(1) << __w; };
75
76     // XXX need constexpr
77     template<typename _UIntType, size_t __w,
78              bool = __w <static_cast<size_t>
79                          (std::numeric_limits<_UIntType>::digits)>
80       struct _ShiftMin1
81       {
82         static const _UIntType __value =
83           __gnu_cxx::__numeric_traits<_UIntType>::__max;
84       };
85
86     template<typename _UIntType, size_t __w>
87       struct _ShiftMin1<_UIntType, __w, true>
88       {
89         static const _UIntType __value =
90           (_UIntType(1) << __w) - _UIntType(1);
91       };
92
93     template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
94       struct _Mod;
95
96     // Dispatch based on modulus value to prevent divide-by-zero compile-time
97     // errors when m == 0.
98     template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
99       inline _Tp
100       __mod(_Tp __x)
101       { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
102
103     typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
104                     unsigned, unsigned long>::__type _UInt32Type;
105
106     /*
107      * An adaptor class for converting the output of any Generator into
108      * the input for a specific Distribution.
109      */
110     template<typename _Engine, typename _DInputType>
111       struct _Adaptor
112       {
113
114       public:
115         _Adaptor(_Engine& __g)
116         : _M_g(__g) { }
117
118         _DInputType
119         min() const
120         {
121           if (is_integral<_DInputType>::value)
122             return _M_g.min();
123           else
124             return _DInputType(0);
125         }
126
127         _DInputType
128         max() const
129         {
130           if (is_integral<_DInputType>::value)
131             return _M_g.max();
132           else
133             return _DInputType(1);
134         }
135
136         /*
137          * Converts a value generated by the adapted random number generator
138          * into a value in the input domain for the dependent random number
139          * distribution.
140          *
141          * Because the type traits are compile time constants only the
142          * appropriate clause of the if statements will actually be emitted
143          * by the compiler.
144          */
145         _DInputType
146         operator()()
147         {
148           if (is_integral<_DInputType>::value)
149             return _M_g();
150           else
151             return generate_canonical<_DInputType,
152                                       numeric_limits<_DInputType>::digits,
153                                       _Engine>(_M_g);
154         }
155
156       private:
157         _Engine& _M_g;
158       };
159   } // namespace __detail
160
161   /**
162    * @addtogroup std_random_generators Random Number Generators
163    * @ingroup std_random
164    *
165    * These classes define objects which provide random or pseudorandom
166    * numbers, either from a discrete or a continuous interval.  The
167    * random number generator supplied as a part of this library are
168    * all uniform random number generators which provide a sequence of
169    * random number uniformly distributed over their range.
170    *
171    * A number generator is a function object with an operator() that
172    * takes zero arguments and returns a number.
173    *
174    * A compliant random number generator must satisfy the following
175    * requirements.  <table border=1 cellpadding=10 cellspacing=0>
176    * <caption align=top>Random Number Generator Requirements</caption>
177    * <tr><td>To be documented.</td></tr> </table>
178    *
179    * @{
180    */
181
182   /**
183    * @brief A model of a linear congruential random number generator.
184    *
185    * A random number generator that produces pseudorandom numbers using the
186    * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
187    *
188    * The template parameter @p _UIntType must be an unsigned integral type
189    * large enough to store values up to (__m-1). If the template parameter
190    * @p __m is 0, the modulus @p __m used is
191    * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
192    * parameters @p __a and @p __c must be less than @p __m.
193    *
194    * The size of the state is @f$ 1 @f$.
195    */
196   template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
197     class linear_congruential_engine
198     {
199       __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
200       static_assert(__m == 0 || (__a < __m && __c < __m),
201                     "template arguments out of bounds"
202                     " in linear_congruential_engine");
203
204     public:
205       /** The type of the generated random value. */
206       typedef _UIntType result_type;
207
208       /** The multiplier. */
209       static const result_type multiplier   = __a;
210       /** An increment. */
211       static const result_type increment    = __c;
212       /** The modulus. */
213       static const result_type modulus      = __m;
214       static const result_type default_seed = 1UL;
215
216       /**
217        * @brief Constructs a %linear_congruential_engine random number
218        *        generator engine with seed @p __s.  The default seed value
219        *        is 1.
220        *
221        * @param __s The initial seed value.
222        */
223       explicit
224       linear_congruential_engine(result_type __s = default_seed)
225       { this->seed(__s); }
226
227       /**
228        * @brief Constructs a %linear_congruential_engine random number
229        *        generator engine seeded from the seed sequence @p __q.
230        *
231        * @param __q the seed sequence.
232        */
233       explicit
234       linear_congruential_engine(seed_seq& __q)
235       { this->seed(__q); }
236
237       /**
238        * @brief Reseeds the %linear_congruential_engine random number generator
239        *        engine sequence to the seed @g __s.
240        *
241        * @param __s The new seed.
242        */
243       void
244       seed(result_type __s = default_seed);
245
246       /**
247        * @brief Reseeds the %linear_congruential_engine random number generator
248        *        engine
249        * sequence using values from the seed sequence @p __q.
250        *
251        * @param __q the seed sequence.
252        */
253       void
254       seed(seed_seq& __q);
255
256       /**
257        * @brief Gets the smallest possible value in the output range.
258        *
259        * The minimum depends on the @p __c parameter: if it is zero, the
260        * minimum generated must be > 0, otherwise 0 is allowed.
261        *
262        * @todo This should be constexpr.
263        */
264       result_type
265       min() const
266       { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
267
268       /**
269        * @brief Gets the largest possible value in the output range.
270        *
271        * @todo This should be constexpr.
272        */
273       result_type
274       max() const
275       { return __m - 1; }
276
277       /**
278        * @brief Discard a sequence of random numbers.
279        *
280        * @todo Look for a faster way to do discard.
281        */
282       void
283       discard(unsigned long long __z)
284       {
285         for (; __z != 0ULL; --__z)
286           (*this)();
287       }
288
289       /**
290        * @brief Gets the next random number in the sequence.
291        */
292       result_type
293       operator()();
294
295       /**
296        * @brief Compares two linear congruential random number generator
297        * objects of the same type for equality.
298        *
299        * @param __lhs A linear congruential random number generator object.
300        * @param __rhs Another linear congruential random number generator
301        *              object.
302        *
303        * @returns true if the two objects are equal, false otherwise.
304        */
305       friend bool
306       operator==(const linear_congruential_engine& __lhs,
307                  const linear_congruential_engine& __rhs)
308       { return __lhs._M_x == __rhs._M_x; }
309
310       /**
311        * @brief Writes the textual representation of the state x(i) of x to
312        *        @p __os.
313        *
314        * @param __os  The output stream.
315        * @param __lcr A % linear_congruential_engine random number generator.
316        * @returns __os.
317        */
318       template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
319                _UIntType1 __m1,
320                typename _CharT, typename _Traits>
321         friend std::basic_ostream<_CharT, _Traits>&
322         operator<<(std::basic_ostream<_CharT, _Traits>&,
323                    const std::linear_congruential_engine<_UIntType1,
324                    __a1, __c1, __m1>&);
325
326       /**
327        * @brief Sets the state of the engine by reading its textual
328        *        representation from @p __is.
329        *
330        * The textual representation must have been previously written using
331        * an output stream whose imbued locale and whose type's template
332        * specialization arguments _CharT and _Traits were the same as those
333        * of @p __is.
334        *
335        * @param __is  The input stream.
336        * @param __lcr A % linear_congruential_engine random number generator.
337        * @returns __is.
338        */
339       template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
340                _UIntType1 __m1,
341                typename _CharT, typename _Traits>
342         friend std::basic_istream<_CharT, _Traits>&
343         operator>>(std::basic_istream<_CharT, _Traits>&,
344                    std::linear_congruential_engine<_UIntType1, __a1,
345                    __c1, __m1>&);
346
347     private:
348       template<typename _Gen>
349         void
350         seed(_Gen& __g, true_type)
351         { return seed(static_cast<unsigned long>(__g)); }
352
353       template<typename _Gen>
354         void
355         seed(_Gen& __g, false_type);
356
357       _UIntType _M_x;
358     };
359
360
361   /**
362    * A generalized feedback shift register discrete random number generator.
363    *
364    * This algorithm avoids multiplication and division and is designed to be
365    * friendly to a pipelined architecture.  If the parameters are chosen
366    * correctly, this generator will produce numbers with a very long period and
367    * fairly good apparent entropy, although still not cryptographically strong.
368    *
369    * The best way to use this generator is with the predefined mt19937 class.
370    *
371    * This algorithm was originally invented by Makoto Matsumoto and
372    * Takuji Nishimura.
373    *
374    * @var word_size   The number of bits in each element of the state vector.
375    * @var state_size  The degree of recursion.
376    * @var shift_size  The period parameter.
377    * @var mask_bits   The separation point bit index.
378    * @var parameter_a The last row of the twist matrix.
379    * @var output_u    The first right-shift tempering matrix parameter.
380    * @var output_s    The first left-shift tempering matrix parameter.
381    * @var output_b    The first left-shift tempering matrix mask.
382    * @var output_t    The second left-shift tempering matrix parameter.
383    * @var output_c    The second left-shift tempering matrix mask.
384    * @var output_l    The second right-shift tempering matrix parameter.
385    */
386   template<typename _UIntType, size_t __w,
387            size_t __n, size_t __m, size_t __r,
388            _UIntType __a, size_t __u, _UIntType __d, size_t __s,
389            _UIntType __b, size_t __t,
390            _UIntType __c, size_t __l, _UIntType __f>
391     class mersenne_twister_engine
392     {
393       __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
394
395       static_assert(__m >= 1U, 
396                     "mersenne_twister_engine template arguments out of bounds");
397       static_assert(__n >= __m,
398                     "mersenne_twister_engine template arguments out of bounds");
399       static_assert(__w >= __r,
400                     "mersenne_twister_engine template arguments out of bounds");
401       static_assert(__w >= __u,
402                     "mersenne_twister_engine template arguments out of bounds");
403       static_assert(__w >= __s,
404                     "mersenne_twister_engine template arguments out of bounds");
405       static_assert(__w >= __t,
406                     "mersenne_twister_engine template arguments out of bounds");
407       static_assert(__w >= __l,
408                     "mersenne_twister_engine template arguments out of bounds");
409       static_assert(__w <=
410                     static_cast<size_t>(numeric_limits<_UIntType>::digits),
411                     "mersenne_twister_engine template arguments out of bounds");
412       static_assert(__a <= __detail::_ShiftMin1<_UIntType, __w>::__value,
413                     "mersenne_twister_engine template arguments out of bounds");
414       static_assert(__b <= __detail::_ShiftMin1<_UIntType, __w>::__value,
415                     "mersenne_twister_engine template arguments out of bounds");
416       static_assert(__c <= __detail::_ShiftMin1<_UIntType, __w>::__value,
417                     "mersenne_twister_engine template arguments out of bounds");
418
419     public:
420       /** The type of the generated random value. */
421       typedef _UIntType result_type;
422
423       // parameter values
424       static const size_t      word_size                 = __w;
425       static const size_t      state_size                = __n;
426       static const size_t      shift_size                = __m;
427       static const size_t      mask_bits                 = __r;
428       static const result_type xor_mask                  = __a;
429       static const size_t      tempering_u               = __u;
430       static const result_type tempering_d               = __d;
431       static const size_t      tempering_s               = __s;
432       static const result_type tempering_b               = __b;
433       static const size_t      tempering_t               = __t;
434       static const result_type tempering_c               = __c;
435       static const size_t      tempering_l               = __l;
436       static const size_t      initialization_multiplier = __f;
437       static const result_type default_seed = 5489UL;
438
439       // constructors and member function
440       explicit
441       mersenne_twister_engine(result_type __sd = default_seed)
442       { seed(__sd); }
443
444       /**
445        * @brief Constructs a %mersenne_twister_engine random number generator
446        *        engine seeded from the seed sequence @p __q.
447        *
448        * @param __q the seed sequence.
449        */
450       explicit
451       mersenne_twister_engine(seed_seq& __q)
452       { seed(__q); }
453
454       void
455       seed(result_type __sd = default_seed);
456
457       void
458       seed(seed_seq& __q);
459
460       /**
461        * @brief Gets the smallest possible value in the output range.
462        *
463        * @todo This should be constexpr.
464        */
465       result_type
466       min() const
467       { return 0; };
468
469       /**
470        * @brief Gets the largest possible value in the output range.
471        *
472        * @todo This should be constexpr.
473        */
474       result_type
475       max() const
476       { return __detail::_ShiftMin1<_UIntType, __w>::__value; }
477
478       /**
479        * @brief Discard a sequence of random numbers.
480        *
481        * @todo Look for a faster way to do discard.
482        */
483       void
484       discard(unsigned long long __z)
485       {
486         for (; __z != 0ULL; --__z)
487           (*this)();
488       }
489
490       result_type
491       operator()();
492
493       /**
494        * @brief Compares two % mersenne_twister_engine random number generator
495        *        objects of the same type for equality.
496        *
497        * @param __lhs A % mersenne_twister_engine random number generator
498        *              object.
499        * @param __rhs Another % mersenne_twister_engine random number
500        *              generator object.
501        *
502        * @returns true if the two objects are equal, false otherwise.
503        */
504       friend bool
505       operator==(const mersenne_twister_engine& __lhs,
506                  const mersenne_twister_engine& __rhs)
507       { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
508
509       /**
510        * @brief Inserts the current state of a % mersenne_twister_engine
511        *        random number generator engine @p __x into the output stream
512        *        @p __os.
513        *
514        * @param __os An output stream.
515        * @param __x  A % mersenne_twister_engine random number generator
516        *             engine.
517        *
518        * @returns The output stream with the state of @p __x inserted or in
519        * an error state.
520        */
521       template<typename _UIntType1,
522                size_t __w1, size_t __n1,
523                size_t __m1, size_t __r1,
524                _UIntType1 __a1, size_t __u1,
525                _UIntType1 __d1, size_t __s1,
526                _UIntType1 __b1, size_t __t1,
527                _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
528                typename _CharT, typename _Traits>
529         friend std::basic_ostream<_CharT, _Traits>&
530         operator<<(std::basic_ostream<_CharT, _Traits>&,
531                    const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
532                    __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
533                    __l1, __f1>&);
534
535       /**
536        * @brief Extracts the current state of a % mersenne_twister_engine
537        *        random number generator engine @p __x from the input stream
538        *        @p __is.
539        *
540        * @param __is An input stream.
541        * @param __x  A % mersenne_twister_engine random number generator
542        *             engine.
543        *
544        * @returns The input stream with the state of @p __x extracted or in
545        * an error state.
546        */
547       template<typename _UIntType1,
548                size_t __w1, size_t __n1,
549                size_t __m1, size_t __r1,
550                _UIntType1 __a1, size_t __u1,
551                _UIntType1 __d1, size_t __s1,
552                _UIntType1 __b1, size_t __t1,
553                _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
554                typename _CharT, typename _Traits>
555         friend std::basic_istream<_CharT, _Traits>&
556         operator>>(std::basic_istream<_CharT, _Traits>&,
557                    std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
558                    __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
559                    __l1, __f1>&);
560
561     private:
562       template<typename _Gen>
563         void
564         seed(_Gen& __g, true_type)
565         { return seed(static_cast<unsigned long>(__g)); }
566
567       template<typename _Gen>
568         void
569         seed(_Gen& __g, false_type);
570
571       _UIntType _M_x[state_size];
572       size_t    _M_p;
573     };
574
575   /**
576    * @brief The Marsaglia-Zaman generator.
577    *
578    * This is a model of a Generalized Fibonacci discrete random number
579    * generator, sometimes referred to as the SWC generator.
580    *
581    * A discrete random number generator that produces pseudorandom
582    * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
583    * carry_{i-1}) \bmod m @f$.
584    *
585    * The size of the state is @f$ r @f$
586    * and the maximum period of the generator is @f$ m^r - m^s - 1 @f$.
587    *
588    * @var _M_x     The state of the generator.  This is a ring buffer.
589    * @var _M_carry The carry.
590    * @var _M_p     Current index of x(i - r).
591    */
592   template<typename _UIntType, size_t __w, size_t __s, size_t __r>
593     class subtract_with_carry_engine
594     {
595       __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
596       static_assert(__s > 0U && __r > __s
597                  && __w > 0U
598                  && __w <= static_cast<size_t>(numeric_limits<_UIntType>::digits),
599                     "template arguments out of bounds"
600                     " in subtract_with_carry_engine");
601
602     public:
603       /** The type of the generated random value. */
604       typedef _UIntType result_type;
605
606       // parameter values
607       static const size_t      word_size    = __w;
608       static const size_t      short_lag    = __s;
609       static const size_t      long_lag     = __r;
610       static const result_type default_seed = 19780503;
611
612       /**
613        * @brief Constructs an explicitly seeded % subtract_with_carry_engine
614        *        random number generator.
615        */
616       explicit
617       subtract_with_carry_engine(result_type __sd = default_seed)
618       { this->seed(__sd); }
619
620       /**
621        * @brief Constructs a %subtract_with_carry_engine random number engine
622        *        seeded from the seed sequence @p __q.
623        *
624        * @param __q the seed sequence.
625        */
626       explicit
627       subtract_with_carry_engine(seed_seq& __q)
628       { this->seed(__q); }
629
630       /**
631        * @brief Seeds the initial state @f$ x_0 @f$ of the random number
632        *        generator.
633        *
634        * N1688[4.19] modifies this as follows.  If @p __value == 0,
635        * sets value to 19780503.  In any case, with a linear
636        * congruential generator lcg(i) having parameters @f$ m_{lcg} =
637        * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
638        * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
639        * \dots lcg(r) \bmod m @f$ respectively.  If @f$ x_{-1} = 0 @f$
640        * set carry to 1, otherwise sets carry to 0.
641        */
642       void
643       seed(result_type __sd = default_seed);
644
645       /**
646        * @brief Seeds the initial state @f$ x_0 @f$ of the
647        * % subtract_with_carry_engine random number generator.
648        */
649       void
650       seed(seed_seq& __q);
651
652       /**
653        * @brief Gets the inclusive minimum value of the range of random
654        * integers returned by this generator.
655        *
656        * @todo This should be constexpr.
657        */
658       result_type
659       min() const
660       { return 0; }
661
662       /**
663        * @brief Gets the inclusive maximum value of the range of random
664        * integers returned by this generator.
665        *
666        * @todo This should be constexpr.
667        */
668       result_type
669       max() const
670       { return _S_modulus - 1U; }
671
672       /**
673        * @brief Discard a sequence of random numbers.
674        *
675        * @todo Look for a faster way to do discard.
676        */
677       void
678       discard(unsigned long long __z)
679       {
680         for (; __z != 0ULL; --__z)
681           (*this)();
682       }
683
684       /**
685        * @brief Gets the next random number in the sequence.
686        */
687       result_type
688       operator()();
689
690       /**
691        * @brief Compares two % subtract_with_carry_engine random number
692        *        generator objects of the same type for equality.
693        *
694        * @param __lhs A % subtract_with_carry_engine random number generator
695        *              object.
696        * @param __rhs Another % subtract_with_carry_engine random number
697        *              generator object.
698        *
699        * @returns true if the two objects are equal, false otherwise.
700        */
701       friend bool
702       operator==(const subtract_with_carry_engine& __lhs,
703                  const subtract_with_carry_engine& __rhs)
704       { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
705
706       /**
707        * @brief Inserts the current state of a % subtract_with_carry_engine
708        *        random number generator engine @p __x into the output stream
709        *        @p __os.
710        *
711        * @param __os An output stream.
712        * @param __x  A % subtract_with_carry_engine random number generator
713        *             engine.
714        *
715        * @returns The output stream with the state of @p __x inserted or in
716        * an error state.
717        */
718       template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
719                typename _CharT, typename _Traits>
720         friend std::basic_ostream<_CharT, _Traits>&
721         operator<<(std::basic_ostream<_CharT, _Traits>&,
722                    const std::subtract_with_carry_engine<_UIntType1, __w1,
723                    __s1, __r1>&);
724
725       /**
726        * @brief Extracts the current state of a % subtract_with_carry_engine
727        *        random number generator engine @p __x from the input stream
728        *        @p __is.
729        *
730        * @param __is An input stream.
731        * @param __x  A % subtract_with_carry_engine random number generator engine.
732        *
733        * @returns The input stream with the state of @p __x extracted or in
734        * an error state.
735        */
736       template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
737                typename _CharT, typename _Traits>
738         friend std::basic_istream<_CharT, _Traits>&
739         operator>>(std::basic_istream<_CharT, _Traits>&,
740                    std::subtract_with_carry_engine<_UIntType1, __w1,
741                    __s1, __r1>&);
742
743     private:
744       template<typename _Gen>
745         void
746         seed(_Gen& __g, true_type)
747         { return seed(static_cast<unsigned long>(__g)); }
748
749       template<typename _Gen>
750         void
751         seed(_Gen& __g, false_type);
752
753       static const size_t _S_modulus
754         = __detail::_Shift<_UIntType, __w>::__value;
755
756       _UIntType  _M_x[long_lag];
757       _UIntType  _M_carry;
758       size_t     _M_p;
759     };
760
761   /**
762    * Produces random numbers from some base engine by discarding blocks of
763    * data.
764    *
765    * 0 <= @p __r <= @p __p
766    */
767   template<typename _RandomNumberEngine, size_t __p, size_t __r>
768     class discard_block_engine
769     {
770       static_assert(__r >= 1U && __p >= __r,
771                     "template arguments out of bounds"
772                     " in discard_block_engine");
773
774     public:
775       /** The type of the generated random value. */
776       typedef typename _RandomNumberEngine::result_type result_type;
777
778       // parameter values
779       static const size_t block_size = __p;
780       static const size_t used_block = __r;
781
782       /**
783        * @brief Constructs a default %discard_block_engine engine.
784        *
785        * The underlying engine is default constructed as well.
786        */
787       discard_block_engine()
788       : _M_b(), _M_n(0) { }
789
790       /**
791        * @brief Copy constructs a %discard_block_engine engine.
792        *
793        * Copies an existing base class random number generator.
794        * @param rng An existing (base class) engine object.
795        */
796       explicit
797       discard_block_engine(const _RandomNumberEngine& __rne)
798       : _M_b(__rne), _M_n(0) { }
799
800       /**
801        * @brief Move constructs a %discard_block_engine engine.
802        *
803        * Copies an existing base class random number generator.
804        * @param rng An existing (base class) engine object.
805        */
806       explicit
807       discard_block_engine(_RandomNumberEngine&& __rne)
808       : _M_b(std::move(__rne)), _M_n(0) { }
809
810       /**
811        * @brief Seed constructs a %discard_block_engine engine.
812        *
813        * Constructs the underlying generator engine seeded with @p __s.
814        * @param __s A seed value for the base class engine.
815        */
816       explicit
817       discard_block_engine(result_type __s)
818       : _M_b(__s), _M_n(0) { }
819
820       /**
821        * @brief Generator construct a %discard_block_engine engine.
822        *
823        * @param __q A seed sequence.
824        */
825       explicit
826       discard_block_engine(seed_seq& __q)
827       : _M_b(__q), _M_n(0)
828       { }
829
830       /**
831        * @brief Reseeds the %discard_block_engine object with the default
832        *        seed for the underlying base class generator engine.
833        */
834       void
835       seed()
836       {
837         _M_b.seed();
838         _M_n = 0;
839       }
840
841       /**
842        * @brief Reseeds the %discard_block_engine object with the default
843        *        seed for the underlying base class generator engine.
844        */
845       void
846       seed(result_type __s)
847       {
848         _M_b.seed(__s);
849         _M_n = 0;
850       }
851
852       /**
853        * @brief Reseeds the %discard_block_engine object with the given seed
854        *        sequence.
855        * @param __q A seed generator function.
856        */
857       void
858       seed(seed_seq& __q)
859       {
860         _M_b.seed(__q);
861         _M_n = 0;
862       }
863
864       /**
865        * @brief Gets a const reference to the underlying generator engine
866        *        object.
867        */
868       const _RandomNumberEngine&
869       base() const
870       { return _M_b; }
871
872       /**
873        * @brief Gets the minimum value in the generated random number range.
874        *
875        * @todo This should be constexpr.
876        */
877       result_type
878       min() const
879       { return _M_b.min(); }
880
881       /**
882        * @brief Gets the maximum value in the generated random number range.
883        *
884        * @todo This should be constexpr.
885        */
886       result_type
887       max() const
888       { return _M_b.max(); }
889
890       /**
891        * @brief Discard a sequence of random numbers.
892        *
893        * @todo Look for a faster way to do discard.
894        */
895       void
896       discard(unsigned long long __z)
897       {
898         for (; __z != 0ULL; --__z)
899           (*this)();
900       }
901
902       /**
903        * @brief Gets the next value in the generated random number sequence.
904        */
905       result_type
906       operator()();
907
908       /**
909        * @brief Compares two %discard_block_engine random number generator
910        *        objects of the same type for equality.
911        *
912        * @param __lhs A %discard_block_engine random number generator object.
913        * @param __rhs Another %discard_block_engine random number generator
914        *              object.
915        *
916        * @returns true if the two objects are equal, false otherwise.
917        */
918       friend bool
919       operator==(const discard_block_engine& __lhs,
920                  const discard_block_engine& __rhs)
921       { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
922
923       /**
924        * @brief Inserts the current state of a %discard_block_engine random
925        *        number generator engine @p __x into the output stream
926        *        @p __os.
927        *
928        * @param __os An output stream.
929        * @param __x  A %discard_block_engine random number generator engine.
930        *
931        * @returns The output stream with the state of @p __x inserted or in
932        * an error state.
933        */
934       template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
935                typename _CharT, typename _Traits>
936         friend std::basic_ostream<_CharT, _Traits>&
937         operator<<(std::basic_ostream<_CharT, _Traits>&,
938                    const std::discard_block_engine<_RandomNumberEngine1,
939                    __p1, __r1>&);
940
941       /**
942        * @brief Extracts the current state of a % subtract_with_carry_engine
943        *        random number generator engine @p __x from the input stream
944        *        @p __is.
945        *
946        * @param __is An input stream.
947        * @param __x  A %discard_block_engine random number generator engine.
948        *
949        * @returns The input stream with the state of @p __x extracted or in
950        * an error state.
951        */
952       template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
953                typename _CharT, typename _Traits>
954         friend std::basic_istream<_CharT, _Traits>&
955         operator>>(std::basic_istream<_CharT, _Traits>&,
956                    std::discard_block_engine<_RandomNumberEngine1,
957                    __p1, __r1>&);
958
959     private:
960       _RandomNumberEngine _M_b;
961       size_t _M_n;
962     };
963
964   /**
965    * Produces random numbers by combining random numbers from some base
966    * engine to produce random numbers with a specifies number of bits @p __w.
967    */
968   template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
969     class independent_bits_engine
970     {
971       static_assert(__w > 0U
972                     && __w <=
973                     static_cast<size_t>(numeric_limits<_UIntType>::digits),
974                     "template arguments out of bounds "
975                     "in independent_bits_engine");
976
977     public:
978       /** The type of the generated random value. */
979       typedef _UIntType result_type;
980
981       /**
982        * @brief Constructs a default %independent_bits_engine engine.
983        *
984        * The underlying engine is default constructed as well.
985        */
986       independent_bits_engine()
987       : _M_b() { }
988
989       /**
990        * @brief Copy constructs a %independent_bits_engine engine.
991        *
992        * Copies an existing base class random number generator.
993        * @param rng An existing (base class) engine object.
994        */
995       explicit
996       independent_bits_engine(const _RandomNumberEngine& __rne)
997       : _M_b(__rne) { }
998
999       /**
1000        * @brief Move constructs a %independent_bits_engine engine.
1001        *
1002        * Copies an existing base class random number generator.
1003        * @param rng An existing (base class) engine object.
1004        */
1005       explicit
1006       independent_bits_engine(_RandomNumberEngine&& __rne)
1007       : _M_b(std::move(__rne)) { }
1008
1009       /**
1010        * @brief Seed constructs a %independent_bits_engine engine.
1011        *
1012        * Constructs the underlying generator engine seeded with @p __s.
1013        * @param __s A seed value for the base class engine.
1014        */
1015       explicit
1016       independent_bits_engine(result_type __s)
1017       : _M_b(__s) { }
1018
1019       /**
1020        * @brief Generator construct a %independent_bits_engine engine.
1021        *
1022        * @param __q A seed sequence.
1023        */
1024       explicit
1025       independent_bits_engine(seed_seq& __q)
1026       : _M_b(__q)
1027       { }
1028
1029       /**
1030        * @brief Reseeds the %independent_bits_engine object with the default
1031        *        seed for the underlying base class generator engine.
1032        */
1033       void
1034       seed()
1035       { _M_b.seed(); }
1036
1037       /**
1038        * @brief Reseeds the %independent_bits_engine object with the default
1039        *        seed for the underlying base class generator engine.
1040        */
1041       void
1042       seed(result_type __s)
1043       { _M_b.seed(__s); }
1044
1045       /**
1046        * @brief Reseeds the %independent_bits_engine object with the given
1047        *        seed sequence.
1048        * @param __q A seed generator function.
1049        */
1050       void
1051       seed(seed_seq& __q)
1052       { _M_b.seed(__q); }
1053
1054       /**
1055        * @brief Gets a const reference to the underlying generator engine
1056        *        object.
1057        */
1058       const _RandomNumberEngine&
1059       base() const
1060       { return _M_b; }
1061
1062       /**
1063        * @brief Gets the minimum value in the generated random number range.
1064        *
1065        * @todo This should be constexpr.
1066        */
1067       result_type
1068       min() const
1069       { return 0U; }
1070
1071       /**
1072        * @brief Gets the maximum value in the generated random number range.
1073        *
1074        * @todo This should be constexpr.
1075        */
1076       result_type
1077       max() const
1078       { return __detail::_ShiftMin1<_UIntType, __w>::__value; }
1079
1080       /**
1081        * @brief Discard a sequence of random numbers.
1082        *
1083        * @todo Look for a faster way to do discard.
1084        */
1085       void
1086       discard(unsigned long long __z)
1087       {
1088         for (; __z != 0ULL; --__z)
1089           (*this)();
1090       }
1091
1092       /**
1093        * @brief Gets the next value in the generated random number sequence.
1094        */
1095       result_type
1096       operator()();
1097
1098       /**
1099        * @brief Compares two %independent_bits_engine random number generator
1100        * objects of the same type for equality.
1101        *
1102        * @param __lhs A %independent_bits_engine random number generator
1103        *              object.
1104        * @param __rhs Another %independent_bits_engine random number generator
1105        *              object.
1106        *
1107        * @returns true if the two objects are equal, false otherwise.
1108        */
1109       friend bool
1110       operator==(const independent_bits_engine& __lhs,
1111                  const independent_bits_engine& __rhs)
1112       { return __lhs._M_b == __rhs._M_b; }
1113
1114       /**
1115        * @brief Extracts the current state of a % subtract_with_carry_engine
1116        *        random number generator engine @p __x from the input stream
1117        *        @p __is.
1118        *
1119        * @param __is An input stream.
1120        * @param __x  A %independent_bits_engine random number generator
1121        *             engine.
1122        *
1123        * @returns The input stream with the state of @p __x extracted or in
1124        *          an error state.
1125        */
1126       template<typename _CharT, typename _Traits>
1127         friend std::basic_istream<_CharT, _Traits>&
1128         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1129                    std::independent_bits_engine<_RandomNumberEngine,
1130                    __w, _UIntType>& __x)
1131         {
1132           __is >> __x._M_b;
1133           return __is;
1134         }
1135
1136     private:
1137       _RandomNumberEngine _M_b;
1138     };
1139
1140   /**
1141    * @brief Inserts the current state of a %independent_bits_engine random
1142    *        number generator engine @p __x into the output stream @p __os.
1143    *
1144    * @param __os An output stream.
1145    * @param __x  A %independent_bits_engine random number generator engine.
1146    *
1147    * @returns The output stream with the state of @p __x inserted or in
1148    *          an error state.
1149    */
1150   template<typename _RandomNumberEngine, size_t __w, typename _UIntType,
1151            typename _CharT, typename _Traits>
1152     std::basic_ostream<_CharT, _Traits>&
1153     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1154                const std::independent_bits_engine<_RandomNumberEngine,
1155                __w, _UIntType>& __x)
1156     {
1157       __os << __x.base();
1158       return __os;
1159     }
1160
1161   /**
1162    * @brief Produces random numbers by combining random numbers from some
1163    * base engine to produce random numbers with a specifies number of bits
1164    * @p __w.
1165    */
1166   template<typename _RandomNumberEngine, size_t __k>
1167     class shuffle_order_engine
1168     {
1169       static_assert(__k >= 1U,
1170                     "template arguments out of bounds"
1171                     " in shuffle_order_engine");
1172
1173     public:
1174       /** The type of the generated random value. */
1175       typedef typename _RandomNumberEngine::result_type result_type;
1176
1177       static const size_t table_size = __k;
1178
1179       /**
1180        * @brief Constructs a default %shuffle_order_engine engine.
1181        *
1182        * The underlying engine is default constructed as well.
1183        */
1184       shuffle_order_engine()
1185       : _M_b()
1186       { _M_initialize(); }
1187
1188       /**
1189        * @brief Copy constructs a %shuffle_order_engine engine.
1190        *
1191        * Copies an existing base class random number generator.
1192        * @param rng An existing (base class) engine object.
1193        */
1194       explicit
1195       shuffle_order_engine(const _RandomNumberEngine& __rne)
1196       : _M_b(__rne)
1197       { _M_initialize(); }
1198
1199       /**
1200        * @brief Move constructs a %shuffle_order_engine engine.
1201        *
1202        * Copies an existing base class random number generator.
1203        * @param rng An existing (base class) engine object.
1204        */
1205       explicit
1206       shuffle_order_engine(_RandomNumberEngine&& __rne)
1207       : _M_b(std::move(__rne))
1208       { _M_initialize(); }
1209
1210       /**
1211        * @brief Seed constructs a %shuffle_order_engine engine.
1212        *
1213        * Constructs the underlying generator engine seeded with @p __s.
1214        * @param __s A seed value for the base class engine.
1215        */
1216       explicit
1217       shuffle_order_engine(result_type __s)
1218       : _M_b(__s)
1219       { _M_initialize(); }
1220
1221       /**
1222        * @brief Generator construct a %shuffle_order_engine engine.
1223        *
1224        * @param __q A seed sequence.
1225        */
1226       explicit
1227       shuffle_order_engine(seed_seq& __q)
1228       : _M_b(__q)
1229       { _M_initialize(); }
1230
1231       /**
1232        * @brief Reseeds the %shuffle_order_engine object with the default seed
1233                 for the underlying base class generator engine.
1234        */
1235       void
1236       seed()
1237       {
1238         _M_b.seed();
1239         _M_initialize();
1240       }
1241
1242       /**
1243        * @brief Reseeds the %shuffle_order_engine object with the default seed
1244        *        for the underlying base class generator engine.
1245        */
1246       void
1247       seed(result_type __s)
1248       {
1249         _M_b.seed(__s);
1250         _M_initialize();
1251       }
1252
1253       /**
1254        * @brief Reseeds the %shuffle_order_engine object with the given seed
1255        *        sequence.
1256        * @param __q A seed generator function.
1257        */
1258       void
1259       seed(seed_seq& __q)
1260       {
1261         _M_b.seed(__q);
1262         _M_initialize();
1263       }
1264
1265       /**
1266        * Gets a const reference to the underlying generator engine object.
1267        */
1268       const _RandomNumberEngine&
1269       base() const
1270       { return _M_b; }
1271
1272       /**
1273        * Gets the minimum value in the generated random number range.
1274        *
1275        * @todo This should be constexpr.
1276        */
1277       result_type
1278       min() const
1279       { return _M_b.min(); }
1280
1281       /**
1282        * Gets the maximum value in the generated random number range.
1283        *
1284        * @todo This should be constexpr.
1285        */
1286       result_type
1287       max() const
1288       { return _M_b.max(); }
1289
1290       /**
1291        * Discard a sequence of random numbers.
1292        *
1293        * @todo Look for a faster way to do discard.
1294        */
1295       void
1296       discard(unsigned long long __z)
1297       {
1298         for (; __z != 0ULL; --__z)
1299           (*this)();
1300       }
1301
1302       /**
1303        * Gets the next value in the generated random number sequence.
1304        */
1305       result_type
1306       operator()();
1307
1308       /**
1309        * Compares two %shuffle_order_engine random number generator objects
1310        * of the same type for equality.
1311        *
1312        * @param __lhs A %shuffle_order_engine random number generator object.
1313        * @param __rhs Another %shuffle_order_engine random number generator
1314        *              object.
1315        *
1316        * @returns true if the two objects are equal, false otherwise.
1317        */
1318       friend bool
1319       operator==(const shuffle_order_engine& __lhs,
1320                  const shuffle_order_engine& __rhs)
1321       { return __lhs._M_b == __rhs._M_b; }
1322
1323       /**
1324        * @brief Inserts the current state of a %shuffle_order_engine random
1325        *        number generator engine @p __x into the output stream
1326         @p __os.
1327        *
1328        * @param __os An output stream.
1329        * @param __x  A %shuffle_order_engine random number generator engine.
1330        *
1331        * @returns The output stream with the state of @p __x inserted or in
1332        * an error state.
1333        */
1334       template<typename _RandomNumberEngine1, size_t __k1,
1335                typename _CharT, typename _Traits>
1336         friend std::basic_ostream<_CharT, _Traits>&
1337         operator<<(std::basic_ostream<_CharT, _Traits>&,
1338                    const std::shuffle_order_engine<_RandomNumberEngine1,
1339                    __k1>&);
1340
1341       /**
1342        * @brief Extracts the current state of a % subtract_with_carry_engine
1343        *        random number generator engine @p __x from the input stream
1344        *        @p __is.
1345        *
1346        * @param __is An input stream.
1347        * @param __x  A %shuffle_order_engine random number generator engine.
1348        *
1349        * @returns The input stream with the state of @p __x extracted or in
1350        * an error state.
1351        */
1352       template<typename _RandomNumberEngine1, size_t __k1,
1353                typename _CharT, typename _Traits>
1354         friend std::basic_istream<_CharT, _Traits>&
1355         operator>>(std::basic_istream<_CharT, _Traits>&,
1356                    std::shuffle_order_engine<_RandomNumberEngine1, __k1>&);
1357
1358     private:
1359       void _M_initialize()
1360       {
1361         for (size_t __i = 0; __i < __k; ++__i)
1362           _M_v[__i] = _M_b();
1363         _M_y = _M_b();
1364       }
1365
1366       _RandomNumberEngine _M_b;
1367       result_type _M_v[__k];
1368       result_type _M_y;
1369     };
1370
1371   /**
1372    * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1373    */
1374   typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1375   minstd_rand0;
1376
1377   /**
1378    * An alternative LCR (Lehmer Generator function) .
1379    */
1380   typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1381   minstd_rand;
1382
1383   /**
1384    * The classic Mersenne Twister.
1385    *
1386    * Reference:
1387    * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
1388    * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
1389    * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1390    */
1391   typedef mersenne_twister_engine<
1392     uint_fast32_t,
1393     32, 624, 397, 31,
1394     0x9908b0dfUL, 11,
1395     0xffffffffUL, 7,
1396     0x9d2c5680UL, 15,
1397     0xefc60000UL, 18, 1812433253UL> mt19937;
1398
1399   /**
1400    * An alternative Mersenne Twister.
1401    */
1402   typedef mersenne_twister_engine<
1403     uint_fast64_t,
1404     64, 312, 156, 31,
1405     0xb5026f5aa96619e9ULL, 29,
1406     0x5555555555555555ULL, 17,
1407     0x71d67fffeda60000ULL, 37,
1408     0xfff7eee000000000ULL, 43,
1409     6364136223846793005ULL> mt19937_64;
1410
1411   /**
1412    * .
1413    */
1414   typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
1415     ranlux24_base;
1416
1417   typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
1418
1419   typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
1420     ranlux48_base;
1421
1422   typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
1423
1424   /**
1425    * .
1426    */
1427   typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
1428
1429   /**
1430    * .
1431    */
1432   typedef minstd_rand0 default_random_engine;
1433
1434   /**
1435    * A standard interface to a platform-specific non-deterministic
1436    * random number generator (if any are available).
1437    */
1438   class random_device
1439   {
1440   public:
1441     /** The type of the generated random value. */
1442     typedef unsigned int result_type;
1443
1444     // constructors, destructors and member functions
1445
1446 #ifdef _GLIBCXX_USE_RANDOM_TR1
1447
1448     explicit
1449     random_device(const std::string& __token = "/dev/urandom")
1450     {
1451       if ((__token != "/dev/urandom" && __token != "/dev/random")
1452           || !(_M_file = std::fopen(__token.c_str(), "rb")))
1453         std::__throw_runtime_error(__N("random_device::"
1454                                        "random_device(const std::string&)"));
1455     }
1456
1457     ~random_device()
1458     { std::fclose(_M_file); }
1459
1460 #else
1461
1462     explicit
1463     random_device(const std::string& __token = "mt19937")
1464     : _M_mt(_M_strtoul(__token)) { }
1465
1466   private:
1467     static unsigned long
1468     _M_strtoul(const std::string& __str)
1469     {
1470       unsigned long __ret = 5489UL;
1471       if (__str != "mt19937")
1472         {
1473           const char* __nptr = __str.c_str();
1474           char* __endptr;
1475           __ret = std::strtoul(__nptr, &__endptr, 0);
1476           if (*__nptr == '\0' || *__endptr != '\0')
1477             std::__throw_runtime_error(__N("random_device::_M_strtoul"
1478                                            "(const std::string&)"));
1479         }
1480       return __ret;
1481     }
1482
1483   public:
1484
1485 #endif
1486
1487     result_type
1488     min() const
1489     { return std::numeric_limits<result_type>::min(); }
1490
1491     result_type
1492     max() const
1493     { return std::numeric_limits<result_type>::max(); }
1494
1495     double
1496     entropy() const
1497     { return 0.0; }
1498
1499     result_type
1500     operator()()
1501     {
1502 #ifdef _GLIBCXX_USE_RANDOM_TR1
1503       result_type __ret;
1504       std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1505                  1, _M_file);
1506       return __ret;
1507 #else
1508       return _M_mt();
1509 #endif
1510     }
1511
1512     // No copy functions.
1513     random_device(const random_device&) = delete;
1514     void operator=(const random_device&) = delete;
1515
1516   private:
1517
1518 #ifdef _GLIBCXX_USE_RANDOM_TR1
1519     FILE*        _M_file;
1520 #else
1521     mt19937      _M_mt;
1522 #endif
1523   };
1524
1525   /* @} */ // group std_random_generators
1526
1527   /**
1528    * @addtogroup std_random_distributions Random Number Distributions
1529    * @ingroup std_random
1530    * @{
1531    */
1532
1533   /**
1534    * @addtogroup std_random_distributions_uniform Uniform Distributions
1535    * @ingroup std_random_distributions
1536    * @{
1537    */
1538
1539   /**
1540    * @brief Uniform discrete distribution for random numbers.
1541    * A discrete random distribution on the range @f$[min, max]@f$ with equal
1542    * probability throughout the range.
1543    */
1544   template<typename _IntType = int>
1545     class uniform_int_distribution
1546     {
1547       __glibcxx_class_requires(_IntType, _IntegerConcept)
1548
1549     public:
1550       /** The type of the range of the distribution. */
1551       typedef _IntType result_type;
1552       /** Parameter type. */
1553       struct param_type
1554       {
1555         typedef uniform_int_distribution<_IntType> distribution_type;
1556
1557         explicit
1558         param_type(_IntType __a = 0, _IntType __b = 9)
1559         : _M_a(__a), _M_b(__b)
1560         {
1561           _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1562         }
1563
1564         result_type
1565         a() const
1566         { return _M_a; }
1567
1568         result_type
1569         b() const
1570         { return _M_b; }
1571
1572         friend bool
1573         operator==(const param_type& __p1, const param_type& __p2)
1574         { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
1575
1576       private:
1577         _IntType _M_a;
1578         _IntType _M_b;
1579       };
1580
1581     public:
1582       /**
1583        * @brief Constructs a uniform distribution object.
1584        */
1585       explicit
1586       uniform_int_distribution(_IntType __a = 0, _IntType __b = 9)
1587       : _M_param(__a, __b)
1588       { }
1589
1590       explicit
1591       uniform_int_distribution(const param_type& __p)
1592       : _M_param(__p)
1593       { }
1594
1595       /**
1596        * @brief Resets the distribution state.
1597        *
1598        * Does nothing for the uniform integer distribution.
1599        */
1600       void
1601       reset() { }
1602
1603       result_type
1604       a() const
1605       { return _M_param.a(); }
1606
1607       result_type
1608       b() const
1609       { return _M_param.b(); }
1610
1611       /**
1612        * @brief Returns the inclusive lower bound of the distribution range.
1613        */
1614       result_type
1615       min() const
1616       { return this->a(); }
1617
1618       /**
1619        * @brief Returns the inclusive upper bound of the distribution range.
1620        */
1621       result_type
1622       max() const
1623       { return this->b(); }
1624
1625       /**
1626        * @brief Returns the parameter set of the distribution.
1627        */
1628       param_type
1629       param() const
1630       { return _M_param; }
1631
1632       /**
1633        * @brief Sets the parameter set of the distribution.
1634        * @param __param The new parameter set of the distribution.
1635        */
1636       void
1637       param(const param_type& __param)
1638       { _M_param = __param; }
1639
1640       /**
1641        * Gets a uniformly distributed random number in the range
1642        * @f$(min, max)@f$.
1643        */
1644       template<typename _UniformRandomNumberGenerator>
1645         result_type
1646         operator()(_UniformRandomNumberGenerator& __urng)
1647         {
1648           typedef typename _UniformRandomNumberGenerator::result_type
1649             _UResult_type;
1650           return _M_call(__urng, this->a(), this->b(),
1651                          typename is_integral<_UResult_type>::type());
1652         }
1653
1654       /**
1655        * Gets a uniform random number in the range @f$[0, n)@f$.
1656        *
1657        * This function is aimed at use with std::random_shuffle.
1658        */
1659       template<typename _UniformRandomNumberGenerator>
1660         result_type
1661         operator()(_UniformRandomNumberGenerator& __urng,
1662                    const param_type& __p)
1663         {
1664           typedef typename _UniformRandomNumberGenerator::result_type
1665             _UResult_type;
1666           return _M_call(__urng, __p.a(), __p.b(),
1667                          typename is_integral<_UResult_type>::type());
1668         }
1669
1670     private:
1671       template<typename _UniformRandomNumberGenerator>
1672         result_type
1673         _M_call(_UniformRandomNumberGenerator& __urng,
1674                 result_type __min, result_type __max, true_type);
1675
1676       template<typename _UniformRandomNumberGenerator>
1677         result_type
1678         _M_call(_UniformRandomNumberGenerator& __urng,
1679                 result_type __min, result_type __max, false_type)
1680         {
1681           return result_type((__urng() - __urng.min())
1682                              / (__urng.max() - __urng.min())
1683                              * (__max - __min + 1)) + __min;
1684         }
1685
1686       param_type _M_param;
1687     };
1688
1689   /**
1690    * @brief Return true if two uniform integer distributions have
1691    *        the same parameters.
1692    */
1693   template<typename _IntType>
1694     inline bool
1695     operator==(const std::uniform_int_distribution<_IntType>& __d1,
1696                const std::uniform_int_distribution<_IntType>& __d2)
1697     { return __d1.param() == __d2.param(); }
1698
1699   /**
1700    * @brief Inserts a %uniform_int_distribution random number
1701    *        distribution @p __x into the output stream @p os.
1702    *
1703    * @param __os An output stream.
1704    * @param __x  A %uniform_int_distribution random number distribution.
1705    *
1706    * @returns The output stream with the state of @p __x inserted or in
1707    * an error state.
1708    */
1709   template<typename _IntType, typename _CharT, typename _Traits>
1710     std::basic_ostream<_CharT, _Traits>&
1711     operator<<(std::basic_ostream<_CharT, _Traits>&,
1712                const std::uniform_int_distribution<_IntType>&);
1713
1714   /**
1715    * @brief Extracts a %uniform_int_distribution random number distribution
1716    * @p __x from the input stream @p __is.
1717    *
1718    * @param __is An input stream.
1719    * @param __x  A %uniform_int_distribution random number generator engine.
1720    *
1721    * @returns The input stream with @p __x extracted or in an error state.
1722    */
1723   template<typename _IntType, typename _CharT, typename _Traits>
1724     std::basic_istream<_CharT, _Traits>&
1725     operator>>(std::basic_istream<_CharT, _Traits>&,
1726                std::uniform_int_distribution<_IntType>&);
1727
1728
1729   /**
1730    * @brief Uniform continuous distribution for random numbers.
1731    *
1732    * A continuous random distribution on the range [min, max) with equal
1733    * probability throughout the range.  The URNG should be real-valued and
1734    * deliver number in the range [0, 1).
1735    */
1736   template<typename _RealType = double>
1737     class uniform_real_distribution
1738     {
1739     public:
1740       /** The type of the range of the distribution. */
1741       typedef _RealType result_type;
1742       /** Parameter type. */
1743       struct param_type
1744       {
1745         typedef uniform_real_distribution<_RealType> distribution_type;
1746
1747         explicit
1748         param_type(_RealType __a = _RealType(0),
1749                    _RealType __b = _RealType(1))
1750         : _M_a(__a), _M_b(__b)
1751         {
1752           _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1753         }
1754
1755         result_type
1756         a() const
1757         { return _M_a; }
1758
1759         result_type
1760         b() const
1761         { return _M_b; }
1762
1763         friend bool
1764         operator==(const param_type& __p1, const param_type& __p2)
1765         { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
1766
1767       private:
1768         _RealType _M_a;
1769         _RealType _M_b;
1770       };
1771
1772     public:
1773       /**
1774        * @brief Constructs a uniform_real_distribution object.
1775        *
1776        * @param __min [IN]  The lower bound of the distribution.
1777        * @param __max [IN]  The upper bound of the distribution.
1778        */
1779       explicit
1780       uniform_real_distribution(_RealType __a = _RealType(0),
1781                                 _RealType __b = _RealType(1))
1782       : _M_param(__a, __b)
1783       { }
1784
1785       explicit
1786       uniform_real_distribution(const param_type& __p)
1787       : _M_param(__p)
1788       { }
1789
1790       /**
1791        * @brief Resets the distribution state.
1792        *
1793        * Does nothing for the uniform real distribution.
1794        */
1795       void
1796       reset() { }
1797
1798       result_type
1799       a() const
1800       { return _M_param.a(); }
1801
1802       result_type
1803       b() const
1804       { return _M_param.b(); }
1805
1806       /**
1807        * @brief Returns the inclusive lower bound of the distribution range.
1808        */
1809       result_type
1810       min() const
1811       { return this->a(); }
1812
1813       /**
1814        * @brief Returns the inclusive upper bound of the distribution range.
1815        */
1816       result_type
1817       max() const
1818       { return this->b(); }
1819
1820       /**
1821        * @brief Returns the parameter set of the distribution.
1822        */
1823       param_type
1824       param() const
1825       { return _M_param; }
1826
1827       /**
1828        * @brief Sets the parameter set of the distribution.
1829        * @param __param The new parameter set of the distribution.
1830        */
1831       void
1832       param(const param_type& __param)
1833       { _M_param = __param; }
1834
1835       template<typename _UniformRandomNumberGenerator>
1836         result_type
1837         operator()(_UniformRandomNumberGenerator& __urng)
1838         {
1839           __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1840             __aurng(__urng);
1841           return (__aurng() * (this->b() - this->a())) + this->a();
1842         }
1843
1844       template<typename _UniformRandomNumberGenerator>
1845         result_type
1846         operator()(_UniformRandomNumberGenerator& __urng,
1847                    const param_type& __p)
1848         {
1849           __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1850             __aurng(__urng);
1851           return (__aurng() * (__p.b() - __p.a())) + __p.a();
1852         }
1853
1854     private:
1855       param_type _M_param;
1856     };
1857
1858   /**
1859    * @brief Return true if two uniform real distributions have
1860    *        the same parameters.
1861    */
1862   template<typename _IntType>
1863     inline bool
1864     operator==(const std::uniform_real_distribution<_IntType>& __d1,
1865                const std::uniform_real_distribution<_IntType>& __d2)
1866     { return __d1.param() == __d2.param(); }
1867
1868   /**
1869    * @brief Inserts a %uniform_real_distribution random number
1870    *        distribution @p __x into the output stream @p __os.
1871    *
1872    * @param __os An output stream.
1873    * @param __x  A %uniform_real_distribution random number distribution.
1874    *
1875    * @returns The output stream with the state of @p __x inserted or in
1876    *          an error state.
1877    */
1878   template<typename _RealType, typename _CharT, typename _Traits>
1879     std::basic_ostream<_CharT, _Traits>&
1880     operator<<(std::basic_ostream<_CharT, _Traits>&,
1881                const std::uniform_real_distribution<_RealType>&);
1882
1883   /**
1884    * @brief Extracts a %uniform_real_distribution random number distribution
1885    * @p __x from the input stream @p __is.
1886    *
1887    * @param __is An input stream.
1888    * @param __x  A %uniform_real_distribution random number generator engine.
1889    *
1890    * @returns The input stream with @p __x extracted or in an error state.
1891    */
1892   template<typename _RealType, typename _CharT, typename _Traits>
1893     std::basic_istream<_CharT, _Traits>&
1894     operator>>(std::basic_istream<_CharT, _Traits>&,
1895                std::uniform_real_distribution<_RealType>&);
1896
1897   /* @} */ // group std_random_distributions_uniform
1898
1899   /**
1900    * @addtogroup std_random_distributions_normal Normal Distributions
1901    * @ingroup std_random_distributions
1902    * @{
1903    */
1904
1905   /**
1906    * @brief A normal continuous distribution for random numbers.
1907    *
1908    * The formula for the normal probability density function is
1909    * @f$ p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1910    *            e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } @f$.
1911    */
1912   template<typename _RealType = double>
1913     class normal_distribution
1914     {
1915     public:
1916       /** The type of the range of the distribution. */
1917       typedef _RealType result_type;
1918       /** Parameter type. */
1919       struct param_type
1920       {
1921         typedef normal_distribution<_RealType> distribution_type;
1922
1923         explicit
1924         param_type(_RealType __mean = _RealType(0),
1925                    _RealType __stddev = _RealType(1))
1926         : _M_mean(__mean), _M_stddev(__stddev)
1927         {
1928           _GLIBCXX_DEBUG_ASSERT(_M_stddev > _RealType(0));
1929         }
1930
1931         _RealType
1932         mean() const
1933         { return _M_mean; }
1934
1935         _RealType
1936         stddev() const
1937         { return _M_stddev; }
1938
1939         friend bool
1940         operator==(const param_type& __p1, const param_type& __p2)
1941         { return (__p1._M_mean == __p2._M_mean)
1942               && (__p1._M_stddev == __p2._M_stddev); }
1943
1944       private:
1945         _RealType _M_mean;
1946         _RealType _M_stddev;
1947       };
1948
1949     public:
1950       /**
1951        * Constructs a normal distribution with parameters @f$ mean @f$ and
1952        * standard deviation.
1953        */
1954       explicit
1955       normal_distribution(result_type __mean = result_type(0),
1956                           result_type __stddev = result_type(1))
1957       : _M_param(__mean, __stddev), _M_saved_available(false)
1958       { }
1959
1960       explicit
1961       normal_distribution(const param_type& __p)
1962       : _M_param(__p), _M_saved_available(false)
1963       { }
1964
1965       /**
1966        * @brief Resets the distribution state.
1967        */
1968       void
1969       reset()
1970       { _M_saved_available = false; }
1971
1972       /**
1973        * @brief Returns the mean of the distribution.
1974        */
1975       _RealType
1976       mean() const
1977       { return _M_param.mean(); }
1978
1979       /**
1980        * @brief Returns the standard deviation of the distribution.
1981        */
1982       _RealType
1983       stddev() const
1984       { return _M_param.stddev(); }
1985
1986       /**
1987        * @brief Returns the parameter set of the distribution.
1988        */
1989       param_type
1990       param() const
1991       { return _M_param; }
1992
1993       /**
1994        * @brief Sets the parameter set of the distribution.
1995        * @param __param The new parameter set of the distribution.
1996        */
1997       void
1998       param(const param_type& __param)
1999       { _M_param = __param; }
2000
2001       /**
2002        * @brief Returns the greatest lower bound value of the distribution.
2003        */
2004       result_type
2005       min() const
2006       { return std::numeric_limits<result_type>::min(); }
2007
2008       /**
2009        * @brief Returns the least upper bound value of the distribution.
2010        */
2011       result_type
2012       max() const
2013       { return std::numeric_limits<result_type>::max(); }
2014
2015       template<typename _UniformRandomNumberGenerator>
2016         result_type
2017         operator()(_UniformRandomNumberGenerator& __urng)
2018         { return this->operator()(__urng, this->param()); }
2019
2020       template<typename _UniformRandomNumberGenerator>
2021         result_type
2022         operator()(_UniformRandomNumberGenerator& __urng,
2023                    const param_type& __p);
2024
2025       /**
2026        * @brief Return true if two normal distributions have
2027        *        the same parameters.
2028        */
2029       template<typename _RealType1>
2030         friend bool
2031         operator==(const std::normal_distribution<_RealType1>& __d1,
2032                    const std::normal_distribution<_RealType1>& __d2);
2033
2034       /**
2035        * @brief Inserts a %normal_distribution random number distribution
2036        * @p __x into the output stream @p __os.
2037        *
2038        * @param __os An output stream.
2039        * @param __x  A %normal_distribution random number distribution.
2040        *
2041        * @returns The output stream with the state of @p __x inserted or in
2042        * an error state.
2043        */
2044       template<typename _RealType1, typename _CharT, typename _Traits>
2045         friend std::basic_ostream<_CharT, _Traits>&
2046         operator<<(std::basic_ostream<_CharT, _Traits>&,
2047                    const std::normal_distribution<_RealType1>&);
2048
2049       /**
2050        * @brief Extracts a %normal_distribution random number distribution
2051        * @p __x from the input stream @p __is.
2052        *
2053        * @param __is An input stream.
2054        * @param __x  A %normal_distribution random number generator engine.
2055        *
2056        * @returns The input stream with @p __x extracted or in an error
2057        *          state.
2058        */
2059       template<typename _RealType1, typename _CharT, typename _Traits>
2060         friend std::basic_istream<_CharT, _Traits>&
2061         operator>>(std::basic_istream<_CharT, _Traits>&,
2062                    std::normal_distribution<_RealType1>&);
2063
2064     private:
2065       param_type  _M_param;
2066       result_type _M_saved;
2067       bool        _M_saved_available;
2068     };
2069
2070
2071   /**
2072    * @brief A lognormal_distribution random number distribution.
2073    *
2074    * The formula for the normal probability mass function is
2075    * @f$ p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2076    *             \exp{-\frac{(\ln{x} - m)^2}{2s^2}} @f$
2077    */
2078   template<typename _RealType = double>
2079     class lognormal_distribution
2080     {
2081     public:
2082       /** The type of the range of the distribution. */
2083       typedef _RealType result_type;
2084       /** Parameter type. */
2085       struct param_type
2086       {
2087         typedef lognormal_distribution<_RealType> distribution_type;
2088
2089         explicit
2090         param_type(_RealType __m = _RealType(0),
2091                    _RealType __s = _RealType(1))
2092         : _M_m(__m), _M_s(__s)
2093         { }
2094
2095         _RealType
2096         m() const
2097         { return _M_m; }
2098
2099         _RealType
2100         s() const
2101         { return _M_s; }
2102
2103         friend bool
2104         operator==(const param_type& __p1, const param_type& __p2)
2105         { return (__p1._M_m == __p2._M_m) && (__p1._M_s == __p2._M_s); }
2106
2107       private:
2108         _RealType _M_m;
2109         _RealType _M_s;
2110       };
2111
2112       explicit
2113       lognormal_distribution(_RealType __m = _RealType(0),
2114                              _RealType __s = _RealType(1))
2115       : _M_param(__m, __s)
2116       { }
2117
2118       explicit
2119       lognormal_distribution(const param_type& __p)
2120       : _M_param(__p)
2121       { }
2122
2123       /**
2124        * Resets the distribution state.
2125        */
2126       void
2127       reset()
2128       { }
2129
2130       /**
2131        *
2132        */
2133       _RealType
2134       m() const
2135       { return _M_param.m(); }
2136
2137       _RealType
2138       s() const
2139       { return _M_param.s(); }
2140
2141       /**
2142        * @brief Returns the parameter set of the distribution.
2143        */
2144       param_type
2145       param() const
2146       { return _M_param; }
2147
2148       /**
2149        * @brief Sets the parameter set of the distribution.
2150        * @param __param The new parameter set of the distribution.
2151        */
2152       void
2153       param(const param_type& __param)
2154       { _M_param = __param; }
2155
2156       /**
2157        * @brief Returns the greatest lower bound value of the distribution.
2158        */
2159       result_type
2160       min() const
2161       { return result_type(0); }
2162
2163       /**
2164        * @brief Returns the least upper bound value of the distribution.
2165        */
2166       result_type
2167       max() const
2168       { return std::numeric_limits<result_type>::max(); }
2169
2170       template<typename _UniformRandomNumberGenerator>
2171         result_type
2172         operator()(_UniformRandomNumberGenerator& __urng)
2173         { return this->operator()(__urng, this->param()); }
2174
2175       template<typename _UniformRandomNumberGenerator>
2176         result_type
2177         operator()(_UniformRandomNumberGenerator& __urng,
2178                    const param_type& __p);
2179
2180     private:
2181       param_type _M_param;
2182     };
2183
2184   /**
2185    * @brief Return true if two lognormal distributions have
2186    *        the same parameters.
2187    */
2188   template<typename _RealType>
2189     inline bool
2190     operator==(const std::lognormal_distribution<_RealType>& __d1,
2191                const std::lognormal_distribution<_RealType>& __d2)
2192     { return __d1.param() == __d2.param(); }
2193
2194   /**
2195    * @brief Inserts a %lognormal_distribution random number distribution
2196    * @p __x into the output stream @p __os.
2197    *
2198    * @param __os An output stream.
2199    * @param __x  A %lognormal_distribution random number distribution.
2200    *
2201    * @returns The output stream with the state of @p __x inserted or in
2202    * an error state.
2203    */
2204   template<typename _RealType, typename _CharT, typename _Traits>
2205     std::basic_ostream<_CharT, _Traits>&
2206     operator<<(std::basic_ostream<_CharT, _Traits>&,
2207                const std::lognormal_distribution<_RealType>&);
2208
2209   /**
2210    * @brief Extracts a %lognormal_distribution random number distribution
2211    * @p __x from the input stream @p __is.
2212    *
2213    * @param __is An input stream.
2214    * @param __x A %lognormal_distribution random number
2215    *            generator engine.
2216    *
2217    * @returns The input stream with @p __x extracted or in an error state.
2218    */
2219   template<typename _RealType, typename _CharT, typename _Traits>
2220     std::basic_istream<_CharT, _Traits>&
2221     operator>>(std::basic_istream<_CharT, _Traits>&,
2222                std::lognormal_distribution<_RealType>&);
2223
2224
2225   /**
2226    * @brief A chi_squared_distribution random number distribution.
2227    *
2228    * The formula for the normal probability mass function is
2229    * @f$ p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}} @f$
2230    */
2231   template<typename _RealType = double>
2232     class chi_squared_distribution
2233     {
2234     public:
2235       /** The type of the range of the distribution. */
2236       typedef _RealType result_type;
2237       /** Parameter type. */
2238       struct param_type
2239       {
2240         typedef chi_squared_distribution<_RealType> distribution_type;
2241
2242         explicit
2243         param_type(_RealType __n = _RealType(1))
2244         : _M_n(__n)
2245         { }
2246
2247         _RealType
2248         n() const
2249         { return _M_n; }
2250
2251         friend bool
2252         operator==(const param_type& __p1, const param_type& __p2)
2253         { return __p1._M_n == __p2._M_n; }
2254
2255       private:
2256         _RealType _M_n;
2257       };
2258
2259       explicit
2260       chi_squared_distribution(_RealType __n = _RealType(1))
2261       : _M_param(__n)
2262       { }
2263
2264       explicit
2265       chi_squared_distribution(const param_type& __p)
2266       : _M_param(__p)
2267       { }
2268
2269       /**
2270        * @brief Resets the distribution state.
2271        */
2272       void
2273       reset()
2274       { }
2275
2276       /**
2277        *
2278        */
2279       _RealType
2280       n() const
2281       { return _M_param.n(); }
2282
2283       /**
2284        * @brief Returns the parameter set of the distribution.
2285        */
2286       param_type
2287       param() const
2288       { return _M_param; }
2289
2290       /**
2291        * @brief Sets the parameter set of the distribution.
2292        * @param __param The new parameter set of the distribution.
2293        */
2294       void
2295       param(const param_type& __param)
2296       { _M_param = __param; }
2297
2298       /**
2299        * @brief Returns the greatest lower bound value of the distribution.
2300        */
2301       result_type
2302       min() const
2303       { return result_type(0); }
2304
2305       /**
2306        * @brief Returns the least upper bound value of the distribution.
2307        */
2308       result_type
2309       max() const
2310       { return std::numeric_limits<result_type>::max(); }
2311
2312       template<typename _UniformRandomNumberGenerator>
2313         result_type
2314         operator()(_UniformRandomNumberGenerator& __urng)
2315         { return this->operator()(__urng, this->param()); }
2316
2317       template<typename _UniformRandomNumberGenerator>
2318         result_type
2319         operator()(_UniformRandomNumberGenerator& __urng,
2320                    const param_type& __p);
2321
2322     private:
2323       param_type _M_param;
2324     };
2325
2326   /**
2327    * @brief Return true if two Chi-squared distributions have
2328    *        the same parameters.
2329    */
2330   template<typename _RealType>
2331     inline bool
2332     operator==(const std::chi_squared_distribution<_RealType>& __d1,
2333                const std::chi_squared_distribution<_RealType>& __d2)
2334     { return __d1.param() == __d2.param(); }
2335
2336   /**
2337    * @brief Inserts a %chi_squared_distribution random number distribution
2338    * @p __x into the output stream @p __os.
2339    *
2340    * @param __os An output stream.
2341    * @param __x  A %chi_squared_distribution random number distribution.
2342    *
2343    * @returns The output stream with the state of @p __x inserted or in
2344    * an error state.
2345    */
2346   template<typename _RealType, typename _CharT, typename _Traits>
2347     std::basic_ostream<_CharT, _Traits>&
2348     operator<<(std::basic_ostream<_CharT, _Traits>&,
2349                const std::chi_squared_distribution<_RealType>&);
2350
2351   /**
2352    * @brief Extracts a %chi_squared_distribution random number distribution
2353    * @p __x from the input stream @p __is.
2354    *
2355    * @param __is An input stream.
2356    * @param __x A %chi_squared_distribution random number
2357    *            generator engine.
2358    *
2359    * @returns The input stream with @p __x extracted or in an error state.
2360    */
2361   template<typename _RealType, typename _CharT, typename _Traits>
2362     std::basic_istream<_CharT, _Traits>&
2363     operator>>(std::basic_istream<_CharT, _Traits>&,
2364                std::chi_squared_distribution<_RealType>&);
2365
2366
2367   /**
2368    * @brief A cauchy_distribution random number distribution.
2369    *
2370    * The formula for the normal probability mass function is
2371    * @f$ p(x|a,b) = \( \pi b \( 1 + \( \frac{x-a}{b} \)^2 \) \)^{-1} @f$
2372    */
2373   template<typename _RealType = double>
2374     class cauchy_distribution
2375     {
2376     public:
2377       /** The type of the range of the distribution. */
2378       typedef _RealType result_type;
2379       /** Parameter type. */
2380       struct param_type
2381       {
2382         typedef cauchy_distribution<_RealType> distribution_type;
2383
2384         explicit
2385         param_type(_RealType __a = _RealType(0),
2386                    _RealType __b = _RealType(1))
2387         : _M_a(__a), _M_b(__b)
2388         { }
2389
2390         _RealType
2391         a() const
2392         { return _M_a; }
2393
2394         _RealType
2395         b() const
2396         { return _M_b; }
2397
2398         friend bool
2399         operator==(const param_type& __p1, const param_type& __p2)
2400         { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
2401
2402       private:
2403         _RealType _M_a;
2404         _RealType _M_b;
2405       };
2406
2407       explicit
2408       cauchy_distribution(_RealType __a = _RealType(0),
2409                           _RealType __b = _RealType(1))
2410       : _M_param(__a, __b)
2411       { }
2412
2413       explicit
2414       cauchy_distribution(const param_type& __p)
2415       : _M_param(__p)
2416       { }
2417
2418       /**
2419        * @brief Resets the distribution state.
2420        */
2421       void
2422       reset()
2423       { }
2424
2425       /**
2426        *
2427        */
2428       _RealType
2429       a() const
2430       { return _M_param.a(); }
2431
2432       _RealType
2433       b() const
2434       { return _M_param.b(); }
2435
2436       /**
2437        * @brief Returns the parameter set of the distribution.
2438        */
2439       param_type
2440       param() const
2441       { return _M_param; }
2442
2443       /**
2444        * @brief Sets the parameter set of the distribution.
2445        * @param __param The new parameter set of the distribution.
2446        */
2447       void
2448       param(const param_type& __param)
2449       { _M_param = __param; }
2450
2451       /**
2452        * @brief Returns the greatest lower bound value of the distribution.
2453        */
2454       result_type
2455       min() const
2456       { return std::numeric_limits<result_type>::min(); }
2457
2458       /**
2459        * @brief Returns the least upper bound value of the distribution.
2460        */
2461       result_type
2462       max() const
2463       { return std::numeric_limits<result_type>::max(); }
2464
2465       template<typename _UniformRandomNumberGenerator>
2466         result_type
2467         operator()(_UniformRandomNumberGenerator& __urng)
2468         { return this->operator()(__urng, this->param()); }
2469
2470       template<typename _UniformRandomNumberGenerator>
2471         result_type
2472         operator()(_UniformRandomNumberGenerator& __urng,
2473                    const param_type& __p);
2474
2475     private:
2476       param_type _M_param;
2477     };
2478
2479   /**
2480    * @brief Return true if two Cauchy distributions have
2481    *        the same parameters.
2482    */
2483   template<typename _RealType>
2484     inline bool
2485     operator==(const std::cauchy_distribution<_RealType>& __d1,
2486                const std::cauchy_distribution<_RealType>& __d2)
2487     { return __d1.param() == __d2.param(); }
2488
2489   /**
2490    * @brief Inserts a %cauchy_distribution random number distribution
2491    * @p __x into the output stream @p __os.
2492    *
2493    * @param __os An output stream.
2494    * @param __x  A %cauchy_distribution random number distribution.
2495    *
2496    * @returns The output stream with the state of @p __x inserted or in
2497    * an error state.
2498    */
2499   template<typename _RealType, typename _CharT, typename _Traits>
2500     std::basic_ostream<_CharT, _Traits>&
2501     operator<<(std::basic_ostream<_CharT, _Traits>&,
2502                const std::cauchy_distribution<_RealType>&);
2503
2504   /**
2505    * @brief Extracts a %cauchy_distribution random number distribution
2506    * @p __x from the input stream @p __is.
2507    *
2508    * @param __is An input stream.
2509    * @param __x A %cauchy_distribution random number
2510    *            generator engine.
2511    *
2512    * @returns The input stream with @p __x extracted or in an error state.
2513    */
2514   template<typename _RealType, typename _CharT, typename _Traits>
2515     std::basic_istream<_CharT, _Traits>&
2516     operator>>(std::basic_istream<_CharT, _Traits>&,
2517                std::cauchy_distribution<_RealType>&);
2518
2519
2520   /**
2521    * @brief A fisher_f_distribution random number distribution.
2522    *
2523    * The formula for the normal probability mass function is
2524    * @f$ p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2525    *                \(\frac{m}{n}\)^{m/2} x^{(m/2)-1}
2526    *                \( 1 + \frac{mx}{n} \)^{-(m+n)/2} @f$
2527    */
2528   template<typename _RealType = double>
2529     class fisher_f_distribution
2530     {
2531     public:
2532       /** The type of the range of the distribution. */
2533       typedef _RealType result_type;
2534       /** Parameter type. */
2535       struct param_type
2536       {
2537         typedef fisher_f_distribution<_RealType> distribution_type;
2538
2539         explicit
2540         param_type(_RealType __m = _RealType(1),
2541                    _RealType __n = _RealType(1))
2542         : _M_m(__m), _M_n(__n)
2543         { }
2544
2545         _RealType
2546         m() const
2547         { return _M_m; }
2548
2549         _RealType
2550         n() const
2551         { return _M_n; }
2552
2553         friend bool
2554         operator==(const param_type& __p1, const param_type& __p2)
2555         { return (__p1._M_m == __p2._M_m) && (__p1._M_n == __p2._M_n); }
2556
2557       private:
2558         _RealType _M_m;
2559         _RealType _M_n;
2560       };
2561
2562       explicit
2563       fisher_f_distribution(_RealType __m = _RealType(1),
2564                             _RealType __n = _RealType(1))
2565       : _M_param(__m, __n)
2566       { }
2567
2568       explicit
2569       fisher_f_distribution(const param_type& __p)
2570       : _M_param(__p)
2571       { }
2572
2573       /**
2574        * @brief Resets the distribution state.
2575        */
2576       void
2577       reset()
2578       { }
2579
2580       /**
2581        *
2582        */
2583       _RealType
2584       m() const
2585       { return _M_param.m(); }
2586
2587       _RealType
2588       n() const
2589       { return _M_param.n(); }
2590
2591       /**
2592        * @brief Returns the parameter set of the distribution.
2593        */
2594       param_type
2595       param() const
2596       { return _M_param; }
2597
2598       /**
2599        * @brief Sets the parameter set of the distribution.
2600        * @param __param The new parameter set of the distribution.
2601        */
2602       void
2603       param(const param_type& __param)
2604       { _M_param = __param; }
2605
2606       /**
2607        * @brief Returns the greatest lower bound value of the distribution.
2608        */
2609       result_type
2610       min() const
2611       { return result_type(0); }
2612
2613       /**
2614        * @brief Returns the least upper bound value of the distribution.
2615        */
2616       result_type
2617       max() const
2618       { return std::numeric_limits<result_type>::max(); }
2619
2620       template<typename _UniformRandomNumberGenerator>
2621         result_type
2622         operator()(_UniformRandomNumberGenerator& __urng)
2623         { return this->operator()(__urng, this->param()); }
2624
2625       template<typename _UniformRandomNumberGenerator>
2626         result_type
2627         operator()(_UniformRandomNumberGenerator& __urng,
2628                    const param_type& __p);
2629
2630     private:
2631       param_type _M_param;
2632     };
2633
2634   /**
2635    * @brief Return true if two Fisher f distributions have
2636    *        the same parameters.
2637    */
2638   template<typename _RealType>
2639     inline bool
2640     operator==(const std::fisher_f_distribution<_RealType>& __d1,
2641                const std::fisher_f_distribution<_RealType>& __d2)
2642     { return __d1.param() == __d2.param(); }
2643
2644   /**
2645    * @brief Inserts a %fisher_f_distribution random number distribution
2646    * @p __x into the output stream @p __os.
2647    *
2648    * @param __os An output stream.
2649    * @param __x  A %fisher_f_distribution random number distribution.
2650    *
2651    * @returns The output stream with the state of @p __x inserted or in
2652    * an error state.
2653    */
2654   template<typename _RealType, typename _CharT, typename _Traits>
2655     std::basic_ostream<_CharT, _Traits>&
2656     operator<<(std::basic_ostream<_CharT, _Traits>&,
2657                const std::fisher_f_distribution<_RealType>&);
2658
2659   /**
2660    * @brief Extracts a %fisher_f_distribution random number distribution
2661    * @p __x from the input stream @p __is.
2662    *
2663    * @param __is An input stream.
2664    * @param __x A %fisher_f_distribution random number
2665    *            generator engine.
2666    *
2667    * @returns The input stream with @p __x extracted or in an error state.
2668    */
2669   template<typename _RealType, typename _CharT, typename _Traits>
2670     std::basic_istream<_CharT, _Traits>&
2671     operator>>(std::basic_istream<_CharT, _Traits>&,
2672                std::fisher_f_distribution<_RealType>&);
2673
2674
2675   /**
2676    * @brief A student_t_distribution random number distribution.
2677    *
2678    * The formula for the normal probability mass function is
2679    * @f$ p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2680    *              \( 1 + \frac{x^2}{n} \) ^{-(n+1)/2} @f$
2681    */
2682   template<typename _RealType = double>
2683     class student_t_distribution
2684     {
2685     public:
2686       /** The type of the range of the distribution. */
2687       typedef _RealType result_type;
2688       /** Parameter type. */
2689       struct param_type
2690       {
2691         typedef student_t_distribution<_RealType> distribution_type;
2692
2693         explicit
2694         param_type(_RealType __n = _RealType(1))
2695         : _M_n(__n)
2696         { }
2697
2698         _RealType
2699         n() const
2700         { return _M_n; }
2701
2702         friend bool
2703         operator==(const param_type& __p1, const param_type& __p2)
2704         { return __p1._M_n == __p2._M_n; }
2705
2706       private:
2707         _RealType _M_n;
2708       };
2709
2710       explicit
2711       student_t_distribution(_RealType __n = _RealType(1))
2712       : _M_param(__n)
2713       { }
2714
2715       explicit
2716       student_t_distribution(const param_type& __p)
2717       : _M_param(__p)
2718       { }
2719
2720       /**
2721        * @brief Resets the distribution state.
2722        */
2723       void
2724       reset()
2725       { }
2726
2727       /**
2728        *
2729        */
2730       _RealType
2731       n() const
2732       { return _M_param.n(); }
2733
2734       /**
2735        * @brief Returns the parameter set of the distribution.
2736        */
2737       param_type
2738       param() const
2739       { return _M_param; }
2740
2741       /**
2742        * @brief Sets the parameter set of the distribution.
2743        * @param __param The new parameter set of the distribution.
2744        */
2745       void
2746       param(const param_type& __param)
2747       { _M_param = __param; }
2748
2749       /**
2750        * @brief Returns the greatest lower bound value of the distribution.
2751        */
2752       result_type
2753       min() const
2754       { return std::numeric_limits<result_type>::min(); }
2755
2756       /**
2757        * @brief Returns the least upper bound value of the distribution.
2758        */
2759       result_type
2760       max() const
2761       { return std::numeric_limits<result_type>::max(); }
2762
2763       template<typename _UniformRandomNumberGenerator>
2764         result_type
2765         operator()(_UniformRandomNumberGenerator& __urng)
2766         { return this->operator()(__urng, this->param()); }
2767
2768       template<typename _UniformRandomNumberGenerator>
2769         result_type
2770         operator()(_UniformRandomNumberGenerator& __urng,
2771                    const param_type& __p);
2772
2773     private:
2774       template<typename _UniformRandomNumberGenerator>
2775         result_type
2776         _M_gaussian(_UniformRandomNumberGenerator& __urng,
2777                     const result_type __sigma);
2778
2779       param_type _M_param;
2780     };
2781
2782   /**
2783    * @brief Return true if two Student t distributions have
2784    *        the same parameters.
2785    */
2786   template<typename _RealType>
2787     inline bool
2788     operator==(const std::student_t_distribution<_RealType>& __d1,
2789                const std::student_t_distribution<_RealType>& __d2)
2790     { return __d1.param() == __d2.param(); }
2791
2792   /**
2793    * @brief Inserts a %student_t_distribution random number distribution
2794    * @p __x into the output stream @p __os.
2795    *
2796    * @param __os An output stream.
2797    * @param __x  A %student_t_distribution random number distribution.
2798    *
2799    * @returns The output stream with the state of @p __x inserted or in
2800    * an error state.
2801    */
2802   template<typename _RealType, typename _CharT, typename _Traits>
2803     std::basic_ostream<_CharT, _Traits>&
2804     operator<<(std::basic_ostream<_CharT, _Traits>&,
2805                const std::student_t_distribution<_RealType>&);
2806
2807   /**
2808    * @brief Extracts a %student_t_distribution random number distribution
2809    * @p __x from the input stream @p __is.
2810    *
2811    * @param __is An input stream.
2812    * @param __x A %student_t_distribution random number
2813    *            generator engine.
2814    *
2815    * @returns The input stream with @p __x extracted or in an error state.
2816    */
2817   template<typename _RealType, typename _CharT, typename _Traits>
2818     std::basic_istream<_CharT, _Traits>&
2819     operator>>(std::basic_istream<_CharT, _Traits>&,
2820                std::student_t_distribution<_RealType>&);
2821
2822   /* @} */ // group std_random_distributions_normal
2823
2824   /**
2825    * @addtogroup std_random_distributions_bernoulli Bernoulli Distributions
2826    * @ingroup std_random_distributions
2827    * @{
2828    */
2829
2830   /**
2831    * @brief A Bernoulli random number distribution.
2832    *
2833    * Generates a sequence of true and false values with likelihood @f$ p @f$
2834    * that true will come up and @f$ (1 - p) @f$ that false will appear.
2835    */
2836   class bernoulli_distribution
2837   {
2838   public:
2839     /** The type of the range of the distribution. */
2840     typedef bool result_type;
2841     /** Parameter type. */
2842     struct param_type
2843     {
2844       typedef bernoulli_distribution distribution_type;
2845
2846       explicit
2847       param_type(double __p = 0.5)
2848       : _M_p(__p)
2849       {
2850         _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
2851       }
2852
2853       double
2854       p() const
2855       { return _M_p; }
2856
2857       friend bool
2858       operator==(const param_type& __p1, const param_type& __p2)
2859       { return __p1._M_p == __p2._M_p; }
2860
2861     private:
2862       double _M_p;
2863     };
2864
2865   public:
2866     /**
2867      * @brief Constructs a Bernoulli distribution with likelihood @p p.
2868      *
2869      * @param __p  [IN]  The likelihood of a true result being returned.
2870      *                   Must be in the interval @f$ [0, 1] @f$.
2871      */
2872     explicit
2873     bernoulli_distribution(double __p = 0.5)
2874     : _M_param(__p)
2875     { }
2876
2877     explicit
2878     bernoulli_distribution(const param_type& __p)
2879     : _M_param(__p)
2880     { }
2881
2882     /**
2883      * @brief Resets the distribution state.
2884      *
2885      * Does nothing for a Bernoulli distribution.
2886      */
2887     void
2888     reset() { }
2889
2890     /**
2891      * @brief Returns the @p p parameter of the distribution.
2892      */
2893     double
2894     p() const
2895     { return _M_param.p(); }
2896
2897     /**
2898      * @brief Returns the parameter set of the distribution.
2899      */
2900     param_type
2901     param() const
2902     { return _M_param; }
2903
2904     /**
2905      * @brief Sets the parameter set of the distribution.
2906      * @param __param The new parameter set of the distribution.
2907      */
2908     void
2909     param(const param_type& __param)
2910     { _M_param = __param; }
2911
2912     /**
2913      * @brief Returns the greatest lower bound value of the distribution.
2914      */
2915     result_type
2916     min() const
2917     { return std::numeric_limits<result_type>::min(); }
2918
2919     /**
2920      * @brief Returns the least upper bound value of the distribution.
2921      */
2922     result_type
2923     max() const
2924     { return std::numeric_limits<result_type>::max(); }
2925
2926     /**
2927      * @brief Returns the next value in the Bernoullian sequence.
2928      */
2929     template<typename _UniformRandomNumberGenerator>
2930       result_type
2931       operator()(_UniformRandomNumberGenerator& __urng)
2932       {
2933         __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2934           __aurng(__urng);
2935         if ((__aurng() - __aurng.min())
2936              < this->p() * (__aurng.max() - __aurng.min()))
2937           return true;
2938         return false;
2939       }
2940
2941     template<typename _UniformRandomNumberGenerator>
2942       result_type
2943       operator()(_UniformRandomNumberGenerator& __urng,
2944                  const param_type& __p)
2945       {
2946         __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2947           __aurng(__urng);
2948         if ((__aurng() - __aurng.min())
2949              < __p.p() * (__aurng.max() - __aurng.min()))
2950           return true;
2951         return false;
2952       }
2953
2954   private:
2955     param_type _M_param;
2956   };
2957
2958   /**
2959    * @brief Return true if two Bernoulli distributions have
2960    *        the same parameters.
2961    */
2962   inline bool
2963   operator==(const std::bernoulli_distribution& __d1,
2964              const std::bernoulli_distribution& __d2)
2965   { return __d1.param() == __d2.param(); }
2966
2967   /**
2968    * @brief Inserts a %bernoulli_distribution random number distribution
2969    * @p __x into the output stream @p __os.
2970    *
2971    * @param __os An output stream.
2972    * @param __x  A %bernoulli_distribution random number distribution.
2973    *
2974    * @returns The output stream with the state of @p __x inserted or in
2975    * an error state.
2976    */
2977   template<typename _CharT, typename _Traits>
2978     std::basic_ostream<_CharT, _Traits>&
2979     operator<<(std::basic_ostream<_CharT, _Traits>&,
2980                const std::bernoulli_distribution&);
2981
2982   /**
2983    * @brief Extracts a %bernoulli_distribution random number distribution
2984    * @p __x from the input stream @p __is.
2985    *
2986    * @param __is An input stream.
2987    * @param __x  A %bernoulli_distribution random number generator engine.
2988    *
2989    * @returns The input stream with @p __x extracted or in an error state.
2990    */
2991   template<typename _CharT, typename _Traits>
2992     std::basic_istream<_CharT, _Traits>&
2993     operator>>(std::basic_istream<_CharT, _Traits>& __is,
2994                std::bernoulli_distribution& __x)
2995     {
2996       double __p;
2997       __is >> __p;
2998       __x.param(bernoulli_distribution::param_type(__p));
2999       return __is;
3000     }
3001
3002
3003   /**
3004    * @brief A discrete binomial random number distribution.
3005    *
3006    * The formula for the binomial probability density function is
3007    * @f$ p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3008    * and @f$ p @f$ are the parameters of the distribution.
3009    */
3010   template<typename _IntType = int>
3011     class binomial_distribution
3012     {
3013       __glibcxx_class_requires(_IntType, _IntegerConcept)
3014
3015     public:
3016       /** The type of the range of the distribution. */
3017       typedef _IntType result_type;
3018       /** Parameter type. */
3019       struct param_type
3020       {
3021         typedef binomial_distribution<_IntType> distribution_type;
3022         friend class binomial_distribution<_IntType>;
3023
3024         explicit
3025         param_type(_IntType __t = _IntType(1), double __p = 0.5)
3026         : _M_t(__t), _M_p(__p)
3027         {
3028           _GLIBCXX_DEBUG_ASSERT((_M_t >= _IntType(0))
3029                                 && (_M_p >= 0.0)
3030                                 && (_M_p <= 1.0));
3031           _M_initialize();
3032         }
3033
3034         _IntType
3035         t() const
3036         { return _M_t; }
3037
3038         double
3039         p() const
3040         { return _M_p; }
3041
3042         friend bool
3043         operator==(const param_type& __p1, const param_type& __p2)
3044         { return (__p1._M_t == __p2._M_t) && (__p1._M_p == __p2._M_p); }
3045
3046       private:
3047         void
3048         _M_initialize();
3049
3050         _IntType _M_t;
3051         double _M_p;
3052
3053         double _M_q;
3054 #if _GLIBCXX_USE_C99_MATH_TR1
3055         double _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
3056                _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
3057 #endif
3058         bool   _M_easy;
3059       };
3060
3061       // constructors and member function
3062       explicit
3063       binomial_distribution(_IntType __t = _IntType(1),
3064                             double __p = 0.5)
3065       : _M_param(__t, __p), _M_nd()
3066       { }
3067
3068       explicit
3069       binomial_distribution(const param_type& __p)
3070       : _M_param(__p), _M_nd()
3071       { }
3072
3073       /**
3074        * @brief Resets the distribution state.
3075        */
3076       void
3077       reset()
3078       { _M_nd.reset(); }
3079
3080       /**
3081        * @brief Returns the distribution @p t parameter.
3082        */
3083       _IntType
3084       t() const
3085       { return _M_param.t(); }
3086
3087       /**
3088        * @brief Returns the distribution @p p parameter.
3089        */
3090       double
3091       p() const
3092       { return _M_param.p(); }
3093
3094       /**
3095        * @brief Returns the parameter set of the distribution.
3096        */
3097       param_type
3098       param() const
3099       { return _M_param; }
3100
3101       /**
3102        * @brief Sets the parameter set of the distribution.
3103        * @param __param The new parameter set of the distribution.
3104        */
3105       void
3106       param(const param_type& __param)
3107       { _M_param = __param; }
3108
3109       /**
3110        * @brief Returns the greatest lower bound value of the distribution.
3111        */
3112       result_type
3113       min() const
3114       { return 0; }
3115
3116       /**
3117        * @brief Returns the least upper bound value of the distribution.
3118        */
3119       result_type
3120       max() const
3121       { return _M_param.t(); }
3122
3123       /**
3124        * @brief Return true if two binomial distributions have
3125        *        the same parameters.
3126        */
3127       template<typename _IntType1>
3128         friend bool
3129         operator==(const std::binomial_distribution<_IntType1>& __d1,
3130                    const std::binomial_distribution<_IntType1>& __d2)
3131         { return ((__d1.param() == __d2.param())
3132                   && (__d1._M_nd == __d2._M_nd)); }
3133
3134       template<typename _UniformRandomNumberGenerator>
3135         result_type
3136         operator()(_UniformRandomNumberGenerator& __urng)
3137         { return this->operator()(__urng, this->param()); }
3138
3139       template<typename _UniformRandomNumberGenerator>
3140         result_type
3141         operator()(_UniformRandomNumberGenerator& __urng,
3142                    const param_type& __p);
3143
3144       /**
3145        * @brief Inserts a %binomial_distribution random number distribution
3146        * @p __x into the output stream @p __os.
3147        *
3148        * @param __os An output stream.
3149        * @param __x  A %binomial_distribution random number distribution.
3150        *
3151        * @returns The output stream with the state of @p __x inserted or in
3152        * an error state.
3153        */
3154       template<typename _IntType1,
3155                typename _CharT, typename _Traits>
3156         friend std::basic_ostream<_CharT, _Traits>&
3157         operator<<(std::basic_ostream<_CharT, _Traits>&,
3158                    const std::binomial_distribution<_IntType1>&);
3159
3160       /**
3161        * @brief Extracts a %binomial_distribution random number distribution
3162        * @p __x from the input stream @p __is.
3163        *
3164        * @param __is An input stream.
3165        * @param __x  A %binomial_distribution random number generator engine.
3166        *
3167        * @returns The input stream with @p __x extracted or in an error
3168        *          state.
3169        */
3170       template<typename _IntType1,
3171                typename _CharT, typename _Traits>
3172         friend std::basic_istream<_CharT, _Traits>&
3173         operator>>(std::basic_istream<_CharT, _Traits>&,
3174                    std::binomial_distribution<_IntType1>&);
3175
3176     private:
3177       template<typename _UniformRandomNumberGenerator>
3178         result_type
3179         _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
3180
3181       param_type _M_param;
3182
3183       // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3184       normal_distribution<double> _M_nd;
3185     };
3186
3187
3188   /**
3189    * @brief A discrete geometric random number distribution.
3190    *
3191    * The formula for the geometric probability density function is
3192    * @f$ p(i|p) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
3193    * distribution.
3194    */
3195   template<typename _IntType = int>
3196     class geometric_distribution
3197     {
3198       __glibcxx_class_requires(_IntType, _IntegerConcept)
3199
3200     public:
3201       /** The type of the range of the distribution. */
3202       typedef _IntType  result_type;
3203       /** Parameter type. */
3204       struct param_type
3205       {
3206         typedef geometric_distribution<_IntType> distribution_type;
3207         friend class geometric_distribution<_IntType>;
3208
3209         explicit
3210         param_type(double __p = 0.5)
3211         : _M_p(__p)
3212         {
3213           _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0)
3214                              && (_M_p <= 1.0));
3215           _M_initialize();
3216         }
3217
3218         double
3219         p() const
3220         { return _M_p; }
3221
3222         friend bool
3223         operator==(const param_type& __p1, const param_type& __p2)
3224         { return __p1._M_p == __p2._M_p; }
3225
3226       private:
3227         void
3228         _M_initialize()
3229         { _M_log_p = std::log(_M_p); }
3230
3231         double _M_p;
3232
3233         double _M_log_p;
3234       };
3235
3236       // constructors and member function
3237       explicit
3238       geometric_distribution(double __p = 0.5)
3239       : _M_param(__p)
3240       { }
3241
3242       explicit
3243       geometric_distribution(const param_type& __p)
3244       : _M_param(__p)
3245       { }
3246
3247       /**
3248        * @brief Resets the distribution state.
3249        *
3250        * Does nothing for the geometric distribution.
3251        */
3252       void
3253       reset() { }
3254
3255       /**
3256        * @brief Returns the distribution parameter @p p.
3257        */
3258       double
3259       p() const
3260       { return _M_param.p(); }
3261
3262       /**
3263        * @brief Returns the parameter set of the distribution.
3264        */
3265       param_type
3266       param() const
3267       { return _M_param; }
3268
3269       /**
3270        * @brief Sets the parameter set of the distribution.
3271        * @param __param The new parameter set of the distribution.
3272        */
3273       void
3274       param(const param_type& __param)
3275       { _M_param = __param; }
3276
3277       /**
3278        * @brief Returns the greatest lower bound value of the distribution.
3279        */
3280       result_type
3281       min() const
3282       { return 0; }
3283
3284       /**
3285        * @brief Returns the least upper bound value of the distribution.
3286        */
3287       result_type
3288       max() const
3289       { return std::numeric_limits<result_type>::max(); }
3290
3291       template<typename _UniformRandomNumberGenerator>
3292         result_type
3293         operator()(_UniformRandomNumberGenerator& __urng)
3294         { return this->operator()(__urng, this->param()); }
3295
3296       template<typename _UniformRandomNumberGenerator>
3297         result_type
3298         operator()(_UniformRandomNumberGenerator& __urng,
3299                    const param_type& __p);
3300
3301     private:
3302       param_type _M_param;
3303     };
3304
3305   /**
3306    * @brief Return true if two geometric distributions have
3307    *        the same parameters.
3308    */
3309   template<typename _IntType>
3310     inline bool
3311     operator==(const geometric_distribution<_IntType>& __d1,
3312                const geometric_distribution<_IntType>& __d2)
3313     { return __d1.param() == __d2.param(); }
3314
3315   /**
3316    * @brief Inserts a %geometric_distribution random number distribution
3317    * @p __x into the output stream @p __os.
3318    *
3319    * @param __os An output stream.
3320    * @param __x  A %geometric_distribution random number distribution.
3321    *
3322    * @returns The output stream with the state of @p __x inserted or in
3323    * an error state.
3324    */
3325   template<typename _IntType,
3326            typename _CharT, typename _Traits>
3327     std::basic_ostream<_CharT, _Traits>&
3328     operator<<(std::basic_ostream<_CharT, _Traits>&,
3329                const std::geometric_distribution<_IntType>&);
3330
3331   /**
3332    * @brief Extracts a %geometric_distribution random number distribution
3333    * @p __x from the input stream @p __is.
3334    *
3335    * @param __is An input stream.
3336    * @param __x  A %geometric_distribution random number generator engine.
3337    *
3338    * @returns The input stream with @p __x extracted or in an error state.
3339    */
3340   template<typename _IntType,
3341            typename _CharT, typename _Traits>
3342     std::basic_istream<_CharT, _Traits>&
3343     operator>>(std::basic_istream<_CharT, _Traits>&,
3344                std::geometric_distribution<_IntType>&);
3345
3346
3347   /**
3348    * @brief A negative_binomial_distribution random number distribution.
3349    *
3350    * The formula for the negative binomial probability mass function is
3351    * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3352    * and @f$ p @f$ are the parameters of the distribution.
3353    */
3354   template<typename _IntType = int>
3355     class negative_binomial_distribution
3356     {
3357       __glibcxx_class_requires(_IntType, _IntegerConcept)
3358
3359     public:
3360       /** The type of the range of the distribution. */
3361       typedef _IntType result_type;
3362       /** Parameter type. */
3363       struct param_type
3364       {
3365         typedef negative_binomial_distribution<_IntType> distribution_type;
3366
3367         explicit
3368         param_type(_IntType __k = 1, double __p = 0.5)
3369         : _M_k(__k), _M_p(__p)
3370         { }
3371
3372         _IntType
3373         k() const
3374         { return _M_k; }
3375
3376         double
3377         p() const
3378         { return _M_p; }
3379
3380         friend bool
3381         operator==(const param_type& __p1, const param_type& __p2)
3382         { return (__p1._M_k == __p2._M_k) && (__p1._M_p == __p2._M_p); }
3383
3384       private:
3385         _IntType _M_k;
3386         double _M_p;
3387       };
3388
3389       explicit
3390       negative_binomial_distribution(_IntType __k = 1, double __p = 0.5)
3391       : _M_param(__k, __p)
3392       { }
3393
3394       explicit
3395       negative_binomial_distribution(const param_type& __p)
3396       : _M_param(__p)
3397       { }
3398
3399       /**
3400        * @brief Resets the distribution state.
3401        */
3402       void
3403       reset()
3404       { }
3405
3406       /**
3407        * @brief Return the @f$ k @f$ parameter of the distribution.
3408        */
3409       _IntType
3410       k() const
3411       { return _M_param.k(); }
3412
3413       /**
3414        * @brief Return the @f$ p @f$ parameter of the distribution.
3415        */
3416       double
3417       p() const
3418       { return _M_param.p(); }
3419
3420       /**
3421        * @brief Returns the parameter set of the distribution.
3422        */
3423       param_type
3424       param() const
3425       { return _M_param; }
3426
3427       /**
3428        * @brief Sets the parameter set of the distribution.
3429        * @param __param The new parameter set of the distribution.
3430        */
3431       void
3432       param(const param_type& __param)
3433       { _M_param = __param; }
3434
3435       /**
3436        * @brief Returns the greatest lower bound value of the distribution.
3437        */
3438       result_type
3439       min() const
3440       { return result_type(0); }
3441
3442       /**
3443        * @brief Returns the least upper bound value of the distribution.
3444        */
3445       result_type
3446       max() const
3447       { return std::numeric_limits<result_type>::max(); }
3448
3449       template<typename _UniformRandomNumberGenerator>
3450         result_type
3451         operator()(_UniformRandomNumberGenerator& __urng)
3452         { return this->operator()(__urng, this->param()); }
3453
3454       template<typename _UniformRandomNumberGenerator>
3455         result_type
3456         operator()(_UniformRandomNumberGenerator& __urng,
3457                    const param_type& __p);
3458
3459     private:
3460       param_type _M_param;
3461     };
3462
3463   /**
3464    * @brief Return true if two negative binomial distributions have
3465    *        the same parameters.
3466    */
3467   template<typename _IntType>
3468     inline bool
3469     operator==(const std::negative_binomial_distribution<_IntType>& __d1,
3470                const std::negative_binomial_distribution<_IntType>& __d2)
3471     { return __d1.param() == __d2.param(); }
3472
3473   /**
3474    * @brief Inserts a %negative_binomial_distribution random
3475    *        number distribution @p __x into the output stream @p __os.
3476    *
3477    * @param __os An output stream.
3478    * @param __x  A %negative_binomial_distribution random number
3479    *             distribution.
3480    *
3481    * @returns The output stream with the state of @p __x inserted or in
3482    *          an error state.
3483    */
3484   template<typename _IntType, typename _CharT, typename _Traits>
3485     std::basic_ostream<_CharT, _Traits>&
3486     operator<<(std::basic_ostream<_CharT, _Traits>&,
3487                const std::negative_binomial_distribution<_IntType>&);
3488
3489   /**
3490    * @brief Extracts a %negative_binomial_distribution random number
3491    *        distribution @p __x from the input stream @p __is.
3492    *
3493    * @param __is An input stream.
3494    * @param __x A %negative_binomial_distribution random number
3495    *            generator engine.
3496    *
3497    * @returns The input stream with @p __x extracted or in an error state.
3498    */
3499   template<typename _IntType, typename _CharT, typename _Traits>
3500     std::basic_istream<_CharT, _Traits>&
3501     operator>>(std::basic_istream<_CharT, _Traits>&,
3502                std::negative_binomial_distribution<_IntType>&);
3503
3504   /* @} */ // group std_random_distributions_bernoulli
3505
3506   /**
3507    * @addtogroup std_random_distributions_poisson Poisson Distributions
3508    * @ingroup std_random_distributions
3509    * @{
3510    */
3511
3512   /**
3513    * @brief A discrete Poisson random number distribution.
3514    *
3515    * The formula for the Poisson probability density function is
3516    * @f$ p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu} @f$ where @f$ \mu @f$ is the
3517    * parameter of the distribution.
3518    */
3519   template<typename _IntType = int>
3520     class poisson_distribution
3521     {
3522       __glibcxx_class_requires(_IntType, _IntegerConcept)
3523
3524     public:
3525       /** The type of the range of the distribution. */
3526       typedef _IntType  result_type;
3527       /** Parameter type. */
3528       struct param_type
3529       {
3530         typedef poisson_distribution<_IntType> distribution_type;
3531         friend class poisson_distribution<_IntType>;
3532
3533         explicit
3534         param_type(double __mean = 1.0)
3535         : _M_mean(__mean)
3536         {
3537           _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
3538           _M_initialize();
3539         }
3540
3541         double
3542         mean() const
3543         { return _M_mean; }
3544
3545         friend bool
3546         operator==(const param_type& __p1, const param_type& __p2)
3547         { return __p1._M_mean == __p2._M_mean; }
3548
3549       private:
3550         // Hosts either log(mean) or the threshold of the simple method.
3551         void
3552         _M_initialize();
3553
3554         double _M_mean;
3555
3556         double _M_lm_thr;
3557 #if _GLIBCXX_USE_C99_MATH_TR1
3558         double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
3559 #endif
3560       };
3561
3562       // constructors and member function
3563       explicit
3564       poisson_distribution(double __mean = 1.0)
3565       : _M_param(__mean), _M_nd()
3566       { }
3567
3568       explicit
3569       poisson_distribution(const param_type& __p)
3570       : _M_param(__p), _M_nd()
3571       { }
3572
3573       /**
3574        * @brief Resets the distribution state.
3575        */
3576       void
3577       reset()
3578       { _M_nd.reset(); }
3579
3580       /**
3581        * @brief Returns the distribution parameter @p mean.
3582        */
3583       double
3584       mean() const
3585       { return _M_param.mean(); }
3586
3587       /**
3588        * @brief Returns the parameter set of the distribution.
3589        */
3590       param_type
3591       param() const
3592       { return _M_param; }
3593
3594       /**
3595        * @brief Sets the parameter set of the distribution.
3596        * @param __param The new parameter set of the distribution.
3597        */
3598       void
3599       param(const param_type& __param)
3600       { _M_param = __param; }
3601
3602       /**
3603        * @brief Returns the greatest lower bound value of the distribution.
3604        */
3605       result_type
3606       min() const
3607       { return 0; }
3608
3609       /**
3610        * @brief Returns the least upper bound value of the distribution.
3611        */
3612       result_type
3613       max() const
3614       { return std::numeric_limits<result_type>::max(); }
3615
3616       template<typename _UniformRandomNumberGenerator>
3617         result_type
3618         operator()(_UniformRandomNumberGenerator& __urng)
3619         { return this->operator()(__urng, this->param()); }
3620
3621       template<typename _UniformRandomNumberGenerator>
3622         result_type
3623         operator()(_UniformRandomNumberGenerator& __urng,
3624                    const param_type& __p);
3625
3626       /**
3627        * @brief Return true if two Poisson distributions have the same
3628        *        parameters.
3629        */
3630       template<typename _IntType1>
3631         friend bool
3632         operator==(const std::poisson_distribution<_IntType1>& __d1,
3633                    const std::poisson_distribution<_IntType1>& __d2)
3634         { return ((__d1.param() == __d2.param())
3635                   && (__d1._M_nd == __d2._M_nd)); }
3636
3637       /**
3638        * @brief Inserts a %poisson_distribution random number distribution
3639        * @p __x into the output stream @p __os.
3640        *
3641        * @param __os An output stream.
3642        * @param __x  A %poisson_distribution random number distribution.
3643        *
3644        * @returns The output stream with the state of @p __x inserted or in
3645        * an error state.
3646        */
3647       template<typename _IntType1, typename _CharT, typename _Traits>
3648         friend std::basic_ostream<_CharT, _Traits>&
3649         operator<<(std::basic_ostream<_CharT, _Traits>&,
3650                    const std::poisson_distribution<_IntType1>&);
3651
3652       /**
3653        * @brief Extracts a %poisson_distribution random number distribution
3654        * @p __x from the input stream @p __is.
3655        *
3656        * @param __is An input stream.
3657        * @param __x  A %poisson_distribution random number generator engine.
3658        *
3659        * @returns The input stream with @p __x extracted or in an error
3660        *          state.
3661        */
3662       template<typename _IntType1, typename _CharT, typename _Traits>
3663         friend std::basic_istream<_CharT, _Traits>&
3664         operator>>(std::basic_istream<_CharT, _Traits>&,
3665                    std::poisson_distribution<_IntType1>&);
3666
3667     private:
3668       param_type _M_param;
3669
3670       // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3671       normal_distribution<double> _M_nd;
3672     };
3673
3674   /**
3675    * @brief An exponential continuous distribution for random numbers.
3676    *
3677    * The formula for the exponential probability density function is
3678    * @f$ p(x|\lambda) = \lambda e^{-\lambda x} @f$.
3679    *
3680    * <table border=1 cellpadding=10 cellspacing=0>
3681    * <caption align=top>Distribution Statistics</caption>
3682    * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3683    * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
3684    * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
3685    * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3686    * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3687    * </table>
3688    */
3689   template<typename _RealType = double>
3690     class exponential_distribution
3691     {
3692     public:
3693       /** The type of the range of the distribution. */
3694       typedef _RealType result_type;
3695       /** Parameter type. */
3696       struct param_type
3697       {
3698         typedef exponential_distribution<_RealType> distribution_type;
3699
3700         explicit
3701         param_type(_RealType __lambda = _RealType(1))
3702         : _M_lambda(__lambda)
3703         {
3704           _GLIBCXX_DEBUG_ASSERT(_M_lambda > _RealType(0));
3705         }
3706
3707         _RealType
3708         lambda() const
3709         { return _M_lambda; }
3710
3711         friend bool
3712         operator==(const param_type& __p1, const param_type& __p2)
3713         { return __p1._M_lambda == __p2._M_lambda; }
3714
3715       private:
3716         _RealType _M_lambda;
3717       };
3718
3719     public:
3720       /**
3721        * @brief Constructs an exponential distribution with inverse scale
3722        *        parameter @f$ \lambda @f$.
3723        */
3724       explicit
3725       exponential_distribution(const result_type& __lambda = result_type(1))
3726       : _M_param(__lambda)
3727       { }
3728
3729       explicit
3730       exponential_distribution(const param_type& __p)
3731       : _M_param(__p)
3732       { }
3733
3734       /**
3735        * @brief Resets the distribution state.
3736        *
3737        * Has no effect on exponential distributions.
3738        */
3739       void
3740       reset() { }
3741
3742       /**
3743        * @brief Returns the inverse scale parameter of the distribution.
3744        */
3745       _RealType
3746       lambda() const
3747       { return _M_param.lambda(); }
3748
3749       /**
3750        * @brief Returns the parameter set of the distribution.
3751        */
3752       param_type
3753       param() const
3754       { return _M_param; }
3755
3756       /**
3757        * @brief Sets the parameter set of the distribution.
3758        * @param __param The new parameter set of the distribution.
3759        */
3760       void
3761       param(const param_type& __param)
3762       { _M_param = __param; }
3763
3764       /**
3765        * @brief Returns the greatest lower bound value of the distribution.
3766        */
3767       result_type
3768       min() const
3769       { return result_type(0); }
3770
3771       /**
3772        * @brief Returns the least upper bound value of the distribution.
3773        */
3774       result_type
3775       max() const
3776       { return std::numeric_limits<result_type>::max(); }
3777
3778       template<typename _UniformRandomNumberGenerator>
3779         result_type
3780         operator()(_UniformRandomNumberGenerator& __urng)
3781         {
3782           __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
3783             __aurng(__urng);
3784           return -std::log(__aurng()) / this->lambda();
3785         }
3786
3787       template<typename _UniformRandomNumberGenerator>
3788         result_type
3789         operator()(_UniformRandomNumberGenerator& __urng,
3790                    const param_type& __p)
3791         {
3792           __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
3793             __aurng(__urng);
3794           return -std::log(__aurng()) / __p.lambda();
3795         }
3796
3797     private:
3798       param_type _M_param;
3799     };
3800
3801   /**
3802    * @brief Return true if two exponential distributions have the same
3803    *        parameters.
3804    */
3805   template<typename _RealType>
3806     inline bool
3807     operator==(const std::exponential_distribution<_RealType>& __d1,
3808                const std::exponential_distribution<_RealType>& __d2)
3809     { return __d1.param() == __d2.param(); }
3810
3811   /**
3812    * @brief Inserts a %exponential_distribution random number distribution
3813    * @p __x into the output stream @p __os.
3814    *
3815    * @param __os An output stream.
3816    * @param __x  A %exponential_distribution random number distribution.
3817    *
3818    * @returns The output stream with the state of @p __x inserted or in
3819    * an error state.
3820    */
3821   template<typename _RealType, typename _CharT, typename _Traits>
3822     std::basic_ostream<_CharT, _Traits>&
3823     operator<<(std::basic_ostream<_CharT, _Traits>&,
3824                const std::exponential_distribution<_RealType>&);
3825
3826   /**
3827    * @brief Extracts a %exponential_distribution random number distribution
3828    * @p __x from the input stream @p __is.
3829    *
3830    * @param __is An input stream.
3831    * @param __x A %exponential_distribution random number
3832    *            generator engine.
3833    *
3834    * @returns The input stream with @p __x extracted or in an error state.
3835    */
3836   template<typename _RealType, typename _CharT, typename _Traits>
3837     std::basic_istream<_CharT, _Traits>&
3838     operator>>(std::basic_istream<_CharT, _Traits>&,
3839                std::exponential_distribution<_RealType>&);
3840
3841
3842   /**
3843    * @brief A gamma continuous distribution for random numbers.
3844    *
3845    * The formula for the gamma probability density function is
3846    * @f$ p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
3847    *                         (x/\beta)^{\alpha - 1} e^{-x/\beta} @f$.
3848    */
3849   template<typename _RealType = double>
3850     class gamma_distribution
3851     {
3852     public:
3853       /** The type of the range of the distribution. */
3854       typedef _RealType result_type;
3855       /** Parameter type. */
3856       struct param_type
3857       {
3858         typedef gamma_distribution<_RealType> distribution_type;
3859         friend class gamma_distribution<_RealType>;
3860
3861         explicit
3862         param_type(_RealType __alpha = _RealType(1),
3863                    _RealType __beta = _RealType(1))
3864         : _M_alpha(__alpha), _M_beta(__beta)
3865         {
3866           _GLIBCXX_DEBUG_ASSERT(_M_alpha > _RealType(0));
3867           _M_initialize();
3868         }
3869
3870         _RealType
3871         alpha() const
3872         { return _M_alpha; }
3873
3874         _RealType
3875         beta() const
3876         { return _M_beta; }
3877
3878         friend bool
3879         operator==(const param_type& __p1, const param_type& __p2)
3880         { return ((__p1._M_alpha == __p2._M_alpha)
3881                   && (__p1._M_beta == __p2._M_beta)); }
3882
3883       private:
3884         void
3885         _M_initialize();
3886
3887         _RealType _M_alpha;
3888         _RealType _M_beta;
3889
3890         // Hosts either lambda of GB or d of modified Vaduva's.
3891         _RealType _M_l_d;
3892       };
3893
3894     public:
3895       /**
3896        * @brief Constructs a gamma distribution with parameters
3897        * @f$ \alpha @f$ and @f$ \beta @f$.
3898        */
3899       explicit
3900       gamma_distribution(_RealType __alpha = _RealType(1),
3901                          _RealType __beta = _RealType(1))
3902       : _M_param(__alpha, __beta)
3903       { }
3904
3905       explicit
3906       gamma_distribution(const param_type& __p)
3907       : _M_param(__p)
3908       { }
3909
3910       /**
3911        * @brief Resets the distribution state.
3912        *
3913        * Does nothing for the gamma distribution.
3914        */
3915       void
3916       reset() { }
3917
3918       /**
3919        * @brief Returns the @f$ \alpha @f$ of the distribution.
3920        */
3921       _RealType
3922       alpha() const
3923       { return _M_param.alpha(); }
3924
3925       /**
3926        * @brief Returns the @f$ \beta @f$ of the distribution.
3927        */
3928       _RealType
3929       beta() const
3930       { return _M_param.beta(); }
3931
3932       /**
3933        * @brief Returns the parameter set of the distribution.
3934        */
3935       param_type
3936       param() const
3937       { return _M_param; }
3938
3939       /**
3940        * @brief Sets the parameter set of the distribution.
3941        * @param __param The new parameter set of the distribution.
3942        */
3943       void
3944       param(const param_type& __param)
3945       { _M_param = __param; }
3946
3947       /**
3948        * @brief Returns the greatest lower bound value of the distribution.
3949        */
3950       result_type
3951       min() const
3952       { return result_type(0); }
3953
3954       /**
3955        * @brief Returns the least upper bound value of the distribution.
3956        */
3957       result_type
3958       max() const
3959       { return std::numeric_limits<result_type>::max(); }
3960
3961       template<typename _UniformRandomNumberGenerator>
3962         result_type
3963         operator()(_UniformRandomNumberGenerator& __urng)
3964         { return this->operator()(__urng, this->param()); }
3965
3966       template<typename _UniformRandomNumberGenerator>
3967         result_type
3968         operator()(_UniformRandomNumberGenerator& __urng,
3969                    const param_type& __p);
3970
3971     private:
3972       param_type _M_param;
3973     };
3974
3975   /**
3976    * @brief Return true if two gamma distributions have the same
3977    *        parameters.
3978    */
3979   template<typename _RealType>
3980     inline bool
3981     operator==(const std::gamma_distribution<_RealType>& __d1,
3982                const std::gamma_distribution<_RealType>& __d2)
3983     { return __d1.param() == __d2.param(); }
3984
3985   /**
3986    * @brief Inserts a %gamma_distribution random number distribution
3987    * @p __x into the output stream @p __os.
3988    *
3989    * @param __os An output stream.
3990    * @param __x  A %gamma_distribution random number distribution.
3991    *
3992    * @returns The output stream with the state of @p __x inserted or in
3993    * an error state.
3994    */
3995   template<typename _RealType, typename _CharT, typename _Traits>
3996     std::basic_ostream<_CharT, _Traits>&
3997     operator<<(std::basic_ostream<_CharT, _Traits>&,
3998                const std::gamma_distribution<_RealType>&);
3999
4000   /**
4001    * @brief Extracts a %gamma_distribution random number distribution
4002    * @p __x from the input stream @p __is.
4003    *
4004    * @param __is An input stream.
4005    * @param __x  A %gamma_distribution random number generator engine.
4006    *
4007    * @returns The input stream with @p __x extracted or in an error state.
4008    */
4009   template<typename _RealType, typename _CharT, typename _Traits>
4010     std::basic_istream<_CharT, _Traits>&
4011     operator>>(std::basic_istream<_CharT, _Traits>&,
4012                std::gamma_distribution<_RealType>&);
4013
4014
4015   /**
4016    * @brief A weibull_distribution random number distribution.
4017    *
4018    * The formula for the normal probability density function is
4019    * @f$ p(x|\alpha,\beta) = \frac{a}{b} (frac{x}{b})^{a-1}
4020    *                         \exp{(-(frac{x}{b})^a)} @f$.
4021    */
4022   template<typename _RealType = double>
4023     class weibull_distribution
4024     {
4025     public:
4026       /** The type of the range of the distribution. */
4027       typedef _RealType result_type;
4028       /** Parameter type. */
4029       struct param_type
4030       {
4031         typedef weibull_distribution<_RealType> distribution_type;
4032
4033         explicit
4034         param_type(_RealType __a = _RealType(1),
4035                    _RealType __b = _RealType(1))
4036         : _M_a(__a), _M_b(__b)
4037         { }
4038
4039         _RealType
4040         a() const
4041         { return _M_a; }
4042
4043         _RealType
4044         b() const
4045         { return _M_b; }
4046
4047         friend bool
4048         operator==(const param_type& __p1, const param_type& __p2)
4049         { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
4050
4051       private:
4052         _RealType _M_a;
4053         _RealType _M_b;
4054       };
4055
4056       explicit
4057       weibull_distribution(_RealType __a = _RealType(1),
4058                            _RealType __b = _RealType(1))
4059       : _M_param(__a, __b)
4060       { }
4061
4062       explicit
4063       weibull_distribution(const param_type& __p)
4064       : _M_param(__p)
4065       { }
4066
4067       /**
4068        * @brief Resets the distribution state.
4069        */
4070       void
4071       reset()
4072       { }
4073
4074       /**
4075        * @brief Return the @f$ a @f$ parameter of the distribution.
4076        */
4077       _RealType
4078       a() const
4079       { return _M_param.a(); }
4080
4081       /**
4082        * @brief Return the @f$ b @f$ parameter of the distribution.
4083        */
4084       _RealType
4085       b() const
4086       { return _M_param.b(); }
4087
4088       /**
4089        * @brief Returns the parameter set of the distribution.
4090        */
4091       param_type
4092       param() const
4093       { return _M_param; }
4094
4095       /**
4096        * @brief Sets the parameter set of the distribution.
4097        * @param __param The new parameter set of the distribution.
4098        */
4099       void
4100       param(const param_type& __param)
4101       { _M_param = __param; }
4102
4103       /**
4104        * @brief Returns the greatest lower bound value of the distribution.
4105        */
4106       result_type
4107       min() const
4108       { return result_type(0); }
4109
4110       /**
4111        * @brief Returns the least upper bound value of the distribution.
4112        */
4113       result_type
4114       max() const
4115       { return std::numeric_limits<result_type>::max(); }
4116
4117       template<typename _UniformRandomNumberGenerator>
4118         result_type
4119         operator()(_UniformRandomNumberGenerator& __urng)
4120         { return this->operator()(__urng, this->param()); }
4121
4122       template<typename _UniformRandomNumberGenerator>
4123         result_type
4124         operator()(_UniformRandomNumberGenerator& __urng,
4125                    const param_type& __p)
4126         {
4127           __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
4128             __aurng(__urng);
4129           return __p.b() * std::pow(-std::log(__aurng()),
4130                                     result_type(1) / __p.a());
4131         }
4132
4133     private:
4134       param_type _M_param;
4135     };
4136
4137   /**
4138    * @brief Return true if two Weibull distributions have the same
4139    *        parameters.
4140    */
4141   template<typename _RealType>
4142     inline bool
4143     operator==(const std::weibull_distribution<_RealType>& __d1,
4144                const std::weibull_distribution<_RealType>& __d2)
4145     { return __d1.param() == __d2.param(); }
4146
4147   /**
4148    * @brief Inserts a %weibull_distribution random number distribution
4149    * @p __x into the output stream @p __os.
4150    *
4151    * @param __os An output stream.
4152    * @param __x  A %weibull_distribution random number distribution.
4153    *
4154    * @returns The output stream with the state of @p __x inserted or in
4155    * an error state.
4156    */
4157   template<typename _RealType, typename _CharT, typename _Traits>
4158     std::basic_ostream<_CharT, _Traits>&
4159     operator<<(std::basic_ostream<_CharT, _Traits>&,
4160                const std::weibull_distribution<_RealType>&);
4161
4162   /**
4163    * @brief Extracts a %weibull_distribution random number distribution
4164    * @p __x from the input stream @p __is.
4165    *
4166    * @param __is An input stream.
4167    * @param __x A %weibull_distribution random number
4168    *            generator engine.
4169    *
4170    * @returns The input stream with @p __x extracted or in an error state.
4171    */
4172   template<typename _RealType, typename _CharT, typename _Traits>
4173     std::basic_istream<_CharT, _Traits>&
4174     operator>>(std::basic_istream<_CharT, _Traits>&,
4175                std::weibull_distribution<_RealType>&);
4176
4177
4178   /**
4179    * @brief A extreme_value_distribution random number distribution.
4180    *
4181    * The formula for the normal probability mass function is
4182    * @f$ p(x|a,b) = \frac{1}{b}
4183    *                \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) @f$
4184    */
4185   template<typename _RealType = double>
4186     class extreme_value_distribution
4187     {
4188     public:
4189       /** The type of the range of the distribution. */
4190       typedef _RealType result_type;
4191       /** Parameter type. */
4192       struct param_type
4193       {
4194         typedef extreme_value_distribution<_RealType> distribution_type;
4195
4196         explicit
4197         param_type(_RealType __a = _RealType(0),
4198                    _RealType __b = _RealType(1))
4199         : _M_a(__a), _M_b(__b)
4200         { }
4201
4202         _RealType
4203         a() const
4204         { return _M_a; }
4205
4206         _RealType
4207         b() const
4208         { return _M_b; }
4209
4210         friend bool
4211         operator==(const param_type& __p1, const param_type& __p2)
4212         { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
4213
4214       private:
4215         _RealType _M_a;
4216         _RealType _M_b;
4217       };
4218
4219       explicit
4220       extreme_value_distribution(_RealType __a = _RealType(0),
4221                                  _RealType __b = _RealType(1))
4222       : _M_param(__a, __b)
4223       { }
4224
4225       explicit
4226       extreme_value_distribution(const param_type& __p)
4227       : _M_param(__p)
4228       { }
4229
4230       /**
4231        * @brief Resets the distribution state.
4232        */
4233       void
4234       reset()
4235       { }
4236
4237       /**
4238        * @brief Return the @f$ a @f$ parameter of the distribution.
4239        */
4240       _RealType
4241       a() const
4242       { return _M_param.a(); }
4243
4244       /**
4245        * @brief Return the @f$ b @f$ parameter of the distribution.
4246        */
4247       _RealType
4248       b() const
4249       { return _M_param.b(); }
4250
4251       /**
4252        * @brief Returns the parameter set of the distribution.
4253        */
4254       param_type
4255       param() const
4256       { return _M_param; }
4257
4258       /**
4259        * @brief Sets the parameter set of the distribution.
4260        * @param __param The new parameter set of the distribution.
4261        */
4262       void
4263       param(const param_type& __param)
4264       { _M_param = __param; }
4265
4266       /**
4267        * @brief Returns the greatest lower bound value of the distribution.
4268        */
4269       result_type
4270       min() const
4271       { return std::numeric_limits<result_type>::min(); }
4272
4273       /**
4274        * @brief Returns the least upper bound value of the distribution.
4275        */
4276       result_type
4277       max() const
4278       { return std::numeric_limits<result_type>::max(); }
4279
4280       template<typename _UniformRandomNumberGenerator>
4281         result_type
4282         operator()(_UniformRandomNumberGenerator& __urng)
4283         { return this->operator()(__urng, this->param()); }
4284
4285       template<typename _UniformRandomNumberGenerator>
4286         result_type
4287         operator()(_UniformRandomNumberGenerator& __urng,
4288                    const param_type& __p);
4289
4290     private:
4291       param_type _M_param;
4292     };
4293
4294   /**
4295    *
4296    */
4297   template<typename _RealType>
4298     inline bool
4299     operator==(const std::extreme_value_distribution<_RealType>& __d1,
4300                const std::extreme_value_distribution<_RealType>& __d2)
4301     { return __d1.param() == __d2.param(); }
4302
4303   /**
4304    * @brief Inserts a %extreme_value_distribution random number distribution
4305    * @p __x into the output stream @p __os.
4306    *
4307    * @param __os An output stream.
4308    * @param __x  A %extreme_value_distribution random number distribution.
4309    *
4310    * @returns The output stream with the state of @p __x inserted or in
4311    * an error state.
4312    */
4313   template<typename _RealType, typename _CharT, typename _Traits>
4314     std::basic_ostream<_CharT, _Traits>&
4315     operator<<(std::basic_ostream<_CharT, _Traits>&,
4316                const std::extreme_value_distribution<_RealType>&);
4317
4318   /**
4319    * @brief Extracts a %extreme_value_distribution random number
4320    *        distribution @p __x from the input stream @p __is.
4321    *
4322    * @param __is An input stream.
4323    * @param __x A %extreme_value_distribution random number
4324    *            generator engine.
4325    *
4326    * @returns The input stream with @p __x extracted or in an error state.
4327    */
4328   template<typename _RealType, typename _CharT, typename _Traits>
4329     std::basic_istream<_CharT, _Traits>&
4330     operator>>(std::basic_istream<_CharT, _Traits>&,
4331                std::extreme_value_distribution<_RealType>&);
4332
4333
4334   /**
4335    * @brief A discrete_distribution random number distribution.
4336    *
4337    * The formula for the discrete probability mass function is
4338    *
4339    */
4340   template<typename _IntType = int>
4341     class discrete_distribution
4342     {
4343       __glibcxx_class_requires(_IntType, _IntegerConcept)
4344
4345     public:
4346       /** The type of the range of the distribution. */
4347       typedef _IntType result_type;
4348       /** Parameter type. */
4349       struct param_type
4350       {
4351         typedef discrete_distribution<_IntType> distribution_type;
4352         friend class discrete_distribution<_IntType>;
4353
4354         param_type()
4355         : _M_prob(), _M_cp()
4356         { _M_initialize(); }
4357
4358         template<typename _InputIterator>
4359           param_type(_InputIterator __wbegin,
4360                      _InputIterator __wend)
4361           : _M_prob(__wbegin, __wend), _M_cp()
4362           { _M_initialize(); }
4363
4364         param_type(initializer_list<double> __wil)
4365         : _M_prob(__wil.begin(), __wil.end()), _M_cp()
4366         { _M_initialize(); }
4367
4368         template<typename _Func>
4369           param_type(size_t __nw, double __xmin, double __xmax,
4370                      _Func __fw);
4371
4372         std::vector<double>
4373         probabilities() const
4374         { return _M_prob; }
4375
4376         friend bool
4377         operator==(const param_type& __p1, const param_type& __p2)
4378         { return __p1._M_prob == __p2._M_prob; }
4379
4380       private:
4381         void
4382         _M_initialize();
4383
4384         std::vector<double> _M_prob;
4385         std::vector<double> _M_cp;
4386       };
4387
4388       discrete_distribution()
4389       : _M_param()
4390       { }
4391
4392       template<typename _InputIterator>
4393         discrete_distribution(_InputIterator __wbegin,
4394                               _InputIterator __wend)
4395         : _M_param(__wbegin, __wend)
4396         { }
4397
4398       discrete_distribution(initializer_list<double> __wil)
4399       : _M_param(__wil)
4400       { }
4401
4402       template<typename _Func>
4403         discrete_distribution(size_t __nw, double __xmin, double __xmax,
4404                               _Func __fw)
4405         : _M_param(__nw, __xmin, __xmax, __fw)
4406         { }
4407
4408       explicit
4409       discrete_distribution(const param_type& __p)
4410       : _M_param(__p)
4411       { }
4412
4413       /**
4414        * @brief Resets the distribution state.
4415        */
4416       void
4417       reset()
4418       { }
4419
4420       /**
4421        * @brief Returns the probabilities of the distribution.
4422        */
4423       std::vector<double>
4424       probabilities() const
4425       { return _M_param.probabilities(); }
4426
4427       /**
4428        * @brief Returns the parameter set of the distribution.
4429        */
4430       param_type
4431       param() const
4432       { return _M_param; }
4433
4434       /**
4435        * @brief Sets the parameter set of the distribution.
4436        * @param __param The new parameter set of the distribution.
4437        */
4438       void
4439       param(const param_type& __param)
4440       { _M_param = __param; }
4441
4442       /**
4443        * @brief Returns the greatest lower bound value of the distribution.
4444        */
4445       result_type
4446       min() const
4447       { return result_type(0); }
4448
4449       /**
4450        * @brief Returns the least upper bound value of the distribution.
4451        */
4452       result_type
4453       max() const
4454       { return this->_M_param._M_prob.size() - 1; }
4455
4456       template<typename _UniformRandomNumberGenerator>
4457         result_type
4458         operator()(_UniformRandomNumberGenerator& __urng)
4459         { return this->operator()(__urng, this->param()); }
4460
4461       template<typename _UniformRandomNumberGenerator>
4462         result_type
4463         operator()(_UniformRandomNumberGenerator& __urng,
4464                    const param_type& __p);
4465
4466       /**
4467        * @brief Inserts a %discrete_distribution random number distribution
4468        * @p __x into the output stream @p __os.
4469        *
4470        * @param __os An output stream.
4471        * @param __x  A %discrete_distribution random number distribution.
4472        *
4473        * @returns The output stream with the state of @p __x inserted or in
4474        * an error state.
4475        */
4476       template<typename _IntType1, typename _CharT, typename _Traits>
4477         friend std::basic_ostream<_CharT, _Traits>&
4478         operator<<(std::basic_ostream<_CharT, _Traits>&,
4479                    const std::discrete_distribution<_IntType1>&);
4480
4481       /**
4482        * @brief Extracts a %discrete_distribution random number distribution
4483        * @p __x from the input stream @p __is.
4484        *
4485        * @param __is An input stream.
4486        * @param __x A %discrete_distribution random number
4487        *            generator engine.
4488        *
4489        * @returns The input stream with @p __x extracted or in an error
4490        *          state.
4491        */
4492       template<typename _IntType1, typename _CharT, typename _Traits>
4493         friend std::basic_istream<_CharT, _Traits>&
4494         operator>>(std::basic_istream<_CharT, _Traits>&,
4495                    std::discrete_distribution<_IntType1>&);
4496
4497     private:
4498       param_type _M_param;
4499     };
4500
4501   /**
4502    *
4503    */
4504   template<typename _IntType>
4505     inline bool
4506     operator==(const std::discrete_distribution<_IntType>& __d1,
4507                const std::discrete_distribution<_IntType>& __d2)
4508     { return __d1.param() == __d2.param(); }
4509
4510
4511   /**
4512    * @brief A piecewise_constant_distribution random number distribution.
4513    *
4514    * The formula for the piecewise constant probability mass function is
4515    *
4516    */
4517   template<typename _RealType = double>
4518     class piecewise_constant_distribution
4519     {
4520     public:
4521       /** The type of the range of the distribution. */
4522       typedef _RealType result_type;
4523       /** Parameter type. */
4524       struct param_type
4525       {
4526         typedef piecewise_constant_distribution<_RealType> distribution_type;
4527         friend class piecewise_constant_distribution<_RealType>;
4528
4529         param_type();
4530
4531         template<typename _InputIteratorB, typename _InputIteratorW>
4532           param_type(_InputIteratorB __bfirst,
4533                      _InputIteratorB __bend,
4534                      _InputIteratorW __wbegin);
4535
4536         template<typename _Func>
4537           param_type(initializer_list<_RealType> __bil, _Func __fw);
4538
4539         template<typename _Func>
4540           param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4541                      _Func __fw);
4542
4543         std::vector<_RealType>
4544         intervals() const
4545         { return _M_int; }
4546
4547         std::vector<double>
4548         densities() const
4549         { return _M_den; }
4550
4551         friend bool
4552         operator==(const param_type& __p1, const param_type& __p2)
4553         { return ((__p1._M_int == __p2._M_int)
4554                   && (__p1._M_den == __p2._M_den)); }
4555
4556       private:
4557         void
4558         _M_initialize();
4559
4560         std::vector<_RealType> _M_int;
4561         std::vector<double> _M_den;
4562         std::vector<double> _M_cp;
4563       };
4564
4565       explicit
4566       piecewise_constant_distribution()
4567       : _M_param()
4568       { }
4569
4570       template<typename _InputIteratorB, typename _InputIteratorW>
4571         piecewise_constant_distribution(_InputIteratorB __bfirst,
4572                                         _InputIteratorB __bend,
4573                                         _InputIteratorW __wbegin)
4574         : _M_param(__bfirst, __bend, __wbegin)
4575         { }
4576
4577       template<typename _Func>
4578         piecewise_constant_distribution(initializer_list<_RealType> __bil,
4579                                         _Func __fw)
4580         : _M_param(__bil, __fw)
4581         { }
4582
4583       template<typename _Func>
4584         piecewise_constant_distribution(size_t __nw,
4585                                         _RealType __xmin, _RealType __xmax,
4586                                         _Func __fw)
4587         : _M_param(__nw, __xmin, __xmax, __fw)
4588         { }
4589
4590       explicit
4591       piecewise_constant_distribution(const param_type& __p)
4592       : _M_param(__p)
4593       { }
4594
4595       /**
4596        * @brief Resets the distribution state.
4597        */
4598       void
4599       reset()
4600       { }
4601
4602       /**
4603        * @brief Returns a vector of the intervals.
4604        */
4605       std::vector<_RealType>
4606       intervals() const
4607       { return _M_param.intervals(); }
4608
4609       /**
4610        * @brief Returns a vector of the probability densities.
4611        */
4612       std::vector<double>
4613       densities() const
4614       { return _M_param.densities(); }
4615
4616       /**
4617        * @brief Returns the parameter set of the distribution.
4618        */
4619       param_type
4620       param() const
4621       { return _M_param; }
4622
4623       /**
4624        * @brief Sets the parameter set of the distribution.
4625        * @param __param The new parameter set of the distribution.
4626        */
4627       void
4628       param(const param_type& __param)
4629       { _M_param = __param; }
4630
4631       /**
4632        * @brief Returns the greatest lower bound value of the distribution.
4633        */
4634       result_type
4635       min() const
4636       { return this->_M_param._M_int.front(); }
4637
4638       /**
4639        * @brief Returns the least upper bound value of the distribution.
4640        */
4641       result_type
4642       max() const
4643       { return this->_M_param._M_int.back(); }
4644
4645       template<typename _UniformRandomNumberGenerator>
4646         result_type
4647         operator()(_UniformRandomNumberGenerator& __urng)
4648         { return this->operator()(__urng, this->param()); }
4649
4650       template<typename _UniformRandomNumberGenerator>
4651         result_type
4652         operator()(_UniformRandomNumberGenerator& __urng,
4653                    const param_type& __p);
4654
4655       /**
4656        * @brief Inserts a %piecewise_constan_distribution random
4657        *        number distribution @p __x into the output stream @p __os.
4658        *
4659        * @param __os An output stream.
4660        * @param __x  A %piecewise_constan_distribution random number
4661        *             distribution.
4662        *
4663        * @returns The output stream with the state of @p __x inserted or in
4664        * an error state.
4665        */
4666       template<typename _RealType1, typename _CharT, typename _Traits>
4667         friend std::basic_ostream<_CharT, _Traits>&
4668         operator<<(std::basic_ostream<_CharT, _Traits>&,
4669                    const std::piecewise_constant_distribution<_RealType1>&);
4670
4671       /**
4672        * @brief Extracts a %piecewise_constan_distribution random
4673        *        number distribution @p __x from the input stream @p __is.
4674        *
4675        * @param __is An input stream.
4676        * @param __x A %piecewise_constan_distribution random number
4677        *            generator engine.
4678        *
4679        * @returns The input stream with @p __x extracted or in an error
4680        *          state.
4681        */
4682       template<typename _RealType1, typename _CharT, typename _Traits>
4683         friend std::basic_istream<_CharT, _Traits>&
4684         operator>>(std::basic_istream<_CharT, _Traits>&,
4685                    std::piecewise_constant_distribution<_RealType1>&);
4686
4687     private:
4688       param_type _M_param;
4689     };
4690
4691   /**
4692    *
4693    */
4694   template<typename _RealType>
4695     inline bool
4696     operator==(const std::piecewise_constant_distribution<_RealType>& __d1,
4697                const std::piecewise_constant_distribution<_RealType>& __d2)
4698     { return __d1.param() == __d2.param(); }
4699
4700
4701   /**
4702    * @brief A piecewise_linear_distribution random number distribution.
4703    *
4704    * The formula for the piecewise linear probability mass function is
4705    *
4706    */
4707   template<typename _RealType = double>
4708     class piecewise_linear_distribution
4709     {
4710     public:
4711       /** The type of the range of the distribution. */
4712       typedef _RealType result_type;
4713       /** Parameter type. */
4714       struct param_type
4715       {
4716         typedef piecewise_linear_distribution<_RealType> distribution_type;
4717         friend class piecewise_linear_distribution<_RealType>;
4718
4719         param_type();
4720
4721         template<typename _InputIteratorB, typename _InputIteratorW>
4722           param_type(_InputIteratorB __bfirst,
4723                      _InputIteratorB __bend,
4724                      _InputIteratorW __wbegin);
4725
4726         template<typename _Func>
4727           param_type(initializer_list<_RealType> __bil, _Func __fw);
4728
4729         template<typename _Func>
4730           param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4731                      _Func __fw);
4732
4733         std::vector<_RealType>
4734         intervals() const
4735         { return _M_int; }
4736
4737         std::vector<double>
4738         densities() const
4739         { return _M_den; }
4740
4741         friend bool
4742         operator==(const param_type& __p1, const param_type& __p2)
4743         { return ((__p1._M_int == __p2._M_int)
4744                   && (__p1._M_den == __p2._M_den)); }
4745
4746       private:
4747         void
4748         _M_initialize();
4749
4750         std::vector<_RealType> _M_int;
4751         std::vector<double> _M_den;
4752         std::vector<double> _M_cp;
4753         std::vector<double> _M_m;
4754       };
4755
4756       explicit
4757       piecewise_linear_distribution()
4758       : _M_param()
4759       { }
4760
4761       template<typename _InputIteratorB, typename _InputIteratorW>
4762         piecewise_linear_distribution(_InputIteratorB __bfirst,
4763                                       _InputIteratorB __bend,
4764                                       _InputIteratorW __wbegin)
4765         : _M_param(__bfirst, __bend, __wbegin)
4766         { }
4767
4768       template<typename _Func>
4769         piecewise_linear_distribution(initializer_list<_RealType> __bil,
4770                                       _Func __fw)
4771         : _M_param(__bil, __fw)
4772         { }
4773
4774       template<typename _Func>
4775         piecewise_linear_distribution(size_t __nw,
4776                                       _RealType __xmin, _RealType __xmax,
4777                                       _Func __fw)
4778         : _M_param(__nw, __xmin, __xmax, __fw)
4779         { }
4780
4781       explicit
4782       piecewise_linear_distribution(const param_type& __p)
4783       : _M_param(__p)
4784       { }
4785
4786       /**
4787        * Resets the distribution state.
4788        */
4789       void
4790       reset()
4791       { }
4792
4793       /**
4794        * @brief Return the intervals of the distribution.
4795        */
4796       std::vector<_RealType>
4797       intervals() const
4798       { return _M_param.intervals(); }
4799
4800       /**
4801        * @brief Return a vector of the probability densities of the
4802        *        distribution.
4803        */
4804       std::vector<double>
4805       densities() const
4806       { return _M_param.densities(); }
4807
4808       /**
4809        * @brief Returns the parameter set of the distribution.
4810        */
4811       param_type
4812       param() const
4813       { return _M_param; }
4814
4815       /**
4816        * @brief Sets the parameter set of the distribution.
4817        * @param __param The new parameter set of the distribution.
4818        */
4819       void
4820       param(const param_type& __param)
4821       { _M_param = __param; }
4822
4823       /**
4824        * @brief Returns the greatest lower bound value of the distribution.
4825        */
4826       result_type
4827       min() const
4828       { return this->_M_param._M_int.front(); }
4829
4830       /**
4831        * @brief Returns the least upper bound value of the distribution.
4832        */
4833       result_type
4834       max() const
4835       { return this->_M_param._M_int.back(); }
4836
4837       template<typename _UniformRandomNumberGenerator>
4838         result_type
4839         operator()(_UniformRandomNumberGenerator& __urng)
4840         { return this->operator()(__urng, this->param()); }
4841
4842       template<typename _UniformRandomNumberGenerator>
4843         result_type
4844         operator()(_UniformRandomNumberGenerator& __urng,
4845                    const param_type& __p);
4846
4847       /**
4848        * @brief Inserts a %piecewise_linear_distribution random number
4849        *        distribution @p __x into the output stream @p __os.
4850        *
4851        * @param __os An output stream.
4852        * @param __x  A %piecewise_linear_distribution random number
4853        *             distribution.
4854        *
4855        * @returns The output stream with the state of @p __x inserted or in
4856        *          an error state.
4857        */
4858       template<typename _RealType1, typename _CharT, typename _Traits>
4859         friend std::basic_ostream<_CharT, _Traits>&
4860         operator<<(std::basic_ostream<_CharT, _Traits>&,
4861                    const std::piecewise_linear_distribution<_RealType1>&);
4862
4863       /**
4864        * @brief Extracts a %piecewise_linear_distribution random number
4865        *        distribution @p __x from the input stream @p __is.
4866        *
4867        * @param __is An input stream.
4868        * @param __x  A %piecewise_linear_distribution random number
4869        *             generator engine.
4870        *
4871        * @returns The input stream with @p __x extracted or in an error
4872        *          state.
4873        */
4874       template<typename _RealType1, typename _CharT, typename _Traits>
4875         friend std::basic_istream<_CharT, _Traits>&
4876         operator>>(std::basic_istream<_CharT, _Traits>&,
4877                    std::piecewise_linear_distribution<_RealType1>&);
4878
4879     private:
4880       param_type _M_param;
4881     };
4882
4883   /**
4884    *
4885    */
4886   template<typename _RealType>
4887     inline bool
4888     operator==(const std::piecewise_linear_distribution<_RealType>& __d1,
4889                const std::piecewise_linear_distribution<_RealType>& __d2)
4890     { return __d1.param() == __d2.param(); }
4891
4892   /* @} */ // group std_random_distributions_poisson
4893
4894   /* @} */ // group std_random_distributions
4895
4896   /**
4897    * @addtogroup std_random_utilities Random Number Utilities
4898    * @ingroup std_random
4899    * @{
4900    */
4901
4902   /**
4903    * @brief The seed_seq class generates sequences of seeds for random
4904    *        number generators.
4905    */
4906   class seed_seq
4907   {
4908
4909   public:
4910     /** The type of the seed vales. */
4911     typedef uint_least32_t result_type;
4912
4913     /** Default constructor. */
4914     seed_seq()
4915     : _M_v()
4916     { }
4917
4918     template<typename _IntType>
4919       seed_seq(std::initializer_list<_IntType> il);
4920
4921     template<typename _InputIterator>
4922       seed_seq(_InputIterator __begin, _InputIterator __end);
4923
4924     // generating functions
4925     template<typename _RandomAccessIterator>
4926       void
4927       generate(_RandomAccessIterator __begin, _RandomAccessIterator __end);
4928
4929     // property functions
4930     size_t size() const
4931     { return _M_v.size(); }
4932
4933     template<typename OutputIterator>
4934       void
4935       param(OutputIterator __dest) const
4936       { std::copy(_M_v.begin(), _M_v.end(), __dest); }
4937
4938   private:
4939     ///
4940     std::vector<result_type> _M_v;
4941   };
4942
4943   /* @} */ // group std_random_utilities
4944
4945   /* @} */ // group std_random
4946
4947 }
4948