OSDN Git Service

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