OSDN Git Service

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