xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/tr1/random.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // random number generation -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 2009-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj 
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj 
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj 
25*38fd1498Szrj /**
26*38fd1498Szrj  * @file tr1/random.h
27*38fd1498Szrj  *  This is an internal header file, included by other library headers.
28*38fd1498Szrj  *  Do not attempt to use it directly. @headername{tr1/random}
29*38fd1498Szrj  */
30*38fd1498Szrj 
31*38fd1498Szrj #ifndef _GLIBCXX_TR1_RANDOM_H
32*38fd1498Szrj #define _GLIBCXX_TR1_RANDOM_H 1
33*38fd1498Szrj 
34*38fd1498Szrj #pragma GCC system_header
35*38fd1498Szrj 
_GLIBCXX_VISIBILITY(default)36*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
37*38fd1498Szrj {
38*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
39*38fd1498Szrj 
40*38fd1498Szrj namespace tr1
41*38fd1498Szrj {
42*38fd1498Szrj   // [5.1] Random number generation
43*38fd1498Szrj 
44*38fd1498Szrj   /**
45*38fd1498Szrj    * @addtogroup tr1_random Random Number Generation
46*38fd1498Szrj    * A facility for generating random numbers on selected distributions.
47*38fd1498Szrj    * @{
48*38fd1498Szrj    */
49*38fd1498Szrj 
50*38fd1498Szrj   /*
51*38fd1498Szrj    * Implementation-space details.
52*38fd1498Szrj    */
53*38fd1498Szrj   namespace __detail
54*38fd1498Szrj   {
55*38fd1498Szrj     template<typename _UIntType, int __w,
56*38fd1498Szrj 	     bool = __w < std::numeric_limits<_UIntType>::digits>
57*38fd1498Szrj       struct _Shift
58*38fd1498Szrj       { static const _UIntType __value = 0; };
59*38fd1498Szrj 
60*38fd1498Szrj     template<typename _UIntType, int __w>
61*38fd1498Szrj       struct _Shift<_UIntType, __w, true>
62*38fd1498Szrj       { static const _UIntType __value = _UIntType(1) << __w; };
63*38fd1498Szrj 
64*38fd1498Szrj     template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
65*38fd1498Szrj       struct _Mod;
66*38fd1498Szrj 
67*38fd1498Szrj     // Dispatch based on modulus value to prevent divide-by-zero compile-time
68*38fd1498Szrj     // errors when m == 0.
69*38fd1498Szrj     template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
70*38fd1498Szrj       inline _Tp
71*38fd1498Szrj       __mod(_Tp __x)
72*38fd1498Szrj       { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
73*38fd1498Szrj 
74*38fd1498Szrj     typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
75*38fd1498Szrj 		    unsigned, unsigned long>::__type _UInt32Type;
76*38fd1498Szrj 
77*38fd1498Szrj     /*
78*38fd1498Szrj      * An adaptor class for converting the output of any Generator into
79*38fd1498Szrj      * the input for a specific Distribution.
80*38fd1498Szrj      */
81*38fd1498Szrj     template<typename _Engine, typename _Distribution>
82*38fd1498Szrj       struct _Adaptor
83*38fd1498Szrj       {
84*38fd1498Szrj 	typedef typename remove_reference<_Engine>::type _BEngine;
85*38fd1498Szrj 	typedef typename _BEngine::result_type           _Engine_result_type;
86*38fd1498Szrj 	typedef typename _Distribution::input_type       result_type;
87*38fd1498Szrj 
88*38fd1498Szrj       public:
89*38fd1498Szrj 	_Adaptor(const _Engine& __g)
90*38fd1498Szrj 	: _M_g(__g) { }
91*38fd1498Szrj 
92*38fd1498Szrj 	result_type
93*38fd1498Szrj 	min() const
94*38fd1498Szrj 	{
95*38fd1498Szrj 	  result_type __return_value;
96*38fd1498Szrj 	  if (is_integral<_Engine_result_type>::value
97*38fd1498Szrj 	      && is_integral<result_type>::value)
98*38fd1498Szrj 	    __return_value = _M_g.min();
99*38fd1498Szrj 	  else
100*38fd1498Szrj 	    __return_value = result_type(0);
101*38fd1498Szrj 	  return __return_value;
102*38fd1498Szrj 	}
103*38fd1498Szrj 
104*38fd1498Szrj 	result_type
105*38fd1498Szrj 	max() const
106*38fd1498Szrj 	{
107*38fd1498Szrj 	  result_type __return_value;
108*38fd1498Szrj 	  if (is_integral<_Engine_result_type>::value
109*38fd1498Szrj 	      && is_integral<result_type>::value)
110*38fd1498Szrj 	    __return_value = _M_g.max();
111*38fd1498Szrj 	  else if (!is_integral<result_type>::value)
112*38fd1498Szrj 	    __return_value = result_type(1);
113*38fd1498Szrj 	  else
114*38fd1498Szrj 	    __return_value = std::numeric_limits<result_type>::max() - 1;
115*38fd1498Szrj 	  return __return_value;
116*38fd1498Szrj 	}
117*38fd1498Szrj 
118*38fd1498Szrj 	/*
119*38fd1498Szrj 	 * Converts a value generated by the adapted random number generator
120*38fd1498Szrj 	 * into a value in the input domain for the dependent random number
121*38fd1498Szrj 	 * distribution.
122*38fd1498Szrj 	 *
123*38fd1498Szrj 	 * Because the type traits are compile time constants only the
124*38fd1498Szrj 	 * appropriate clause of the if statements will actually be emitted
125*38fd1498Szrj 	 * by the compiler.
126*38fd1498Szrj 	 */
127*38fd1498Szrj 	result_type
128*38fd1498Szrj 	operator()()
129*38fd1498Szrj 	{
130*38fd1498Szrj 	  result_type __return_value;
131*38fd1498Szrj 	  if (is_integral<_Engine_result_type>::value
132*38fd1498Szrj 	      && is_integral<result_type>::value)
133*38fd1498Szrj 	    __return_value = _M_g();
134*38fd1498Szrj 	  else if (!is_integral<_Engine_result_type>::value
135*38fd1498Szrj 		   && !is_integral<result_type>::value)
136*38fd1498Szrj 	    __return_value = result_type(_M_g() - _M_g.min())
137*38fd1498Szrj 	      / result_type(_M_g.max() - _M_g.min());
138*38fd1498Szrj 	  else if (is_integral<_Engine_result_type>::value
139*38fd1498Szrj 		   && !is_integral<result_type>::value)
140*38fd1498Szrj 	    __return_value = result_type(_M_g() - _M_g.min())
141*38fd1498Szrj 	      / result_type(_M_g.max() - _M_g.min() + result_type(1));
142*38fd1498Szrj 	  else
143*38fd1498Szrj 	    __return_value = (((_M_g() - _M_g.min())
144*38fd1498Szrj 			       / (_M_g.max() - _M_g.min()))
145*38fd1498Szrj 			      * std::numeric_limits<result_type>::max());
146*38fd1498Szrj 	  return __return_value;
147*38fd1498Szrj 	}
148*38fd1498Szrj 
149*38fd1498Szrj       private:
150*38fd1498Szrj 	_Engine _M_g;
151*38fd1498Szrj       };
152*38fd1498Szrj 
153*38fd1498Szrj     // Specialization for _Engine*.
154*38fd1498Szrj     template<typename _Engine, typename _Distribution>
155*38fd1498Szrj       struct _Adaptor<_Engine*, _Distribution>
156*38fd1498Szrj       {
157*38fd1498Szrj 	typedef typename _Engine::result_type      _Engine_result_type;
158*38fd1498Szrj 	typedef typename _Distribution::input_type result_type;
159*38fd1498Szrj 
160*38fd1498Szrj       public:
161*38fd1498Szrj 	_Adaptor(_Engine* __g)
162*38fd1498Szrj 	: _M_g(__g) { }
163*38fd1498Szrj 
164*38fd1498Szrj 	result_type
165*38fd1498Szrj 	min() const
166*38fd1498Szrj 	{
167*38fd1498Szrj 	  result_type __return_value;
168*38fd1498Szrj 	  if (is_integral<_Engine_result_type>::value
169*38fd1498Szrj 	      && is_integral<result_type>::value)
170*38fd1498Szrj 	    __return_value = _M_g->min();
171*38fd1498Szrj 	  else
172*38fd1498Szrj 	    __return_value = result_type(0);
173*38fd1498Szrj 	  return __return_value;
174*38fd1498Szrj 	}
175*38fd1498Szrj 
176*38fd1498Szrj 	result_type
177*38fd1498Szrj 	max() const
178*38fd1498Szrj 	{
179*38fd1498Szrj 	  result_type __return_value;
180*38fd1498Szrj 	  if (is_integral<_Engine_result_type>::value
181*38fd1498Szrj 	      && is_integral<result_type>::value)
182*38fd1498Szrj 	    __return_value = _M_g->max();
183*38fd1498Szrj 	  else if (!is_integral<result_type>::value)
184*38fd1498Szrj 	    __return_value = result_type(1);
185*38fd1498Szrj 	  else
186*38fd1498Szrj 	    __return_value = std::numeric_limits<result_type>::max() - 1;
187*38fd1498Szrj 	  return __return_value;
188*38fd1498Szrj 	}
189*38fd1498Szrj 
190*38fd1498Szrj 	result_type
191*38fd1498Szrj 	operator()()
192*38fd1498Szrj 	{
193*38fd1498Szrj 	  result_type __return_value;
194*38fd1498Szrj 	  if (is_integral<_Engine_result_type>::value
195*38fd1498Szrj 	      && is_integral<result_type>::value)
196*38fd1498Szrj 	    __return_value = (*_M_g)();
197*38fd1498Szrj 	  else if (!is_integral<_Engine_result_type>::value
198*38fd1498Szrj 		   && !is_integral<result_type>::value)
199*38fd1498Szrj 	    __return_value = result_type((*_M_g)() - _M_g->min())
200*38fd1498Szrj 	      / result_type(_M_g->max() - _M_g->min());
201*38fd1498Szrj 	  else if (is_integral<_Engine_result_type>::value
202*38fd1498Szrj 		   && !is_integral<result_type>::value)
203*38fd1498Szrj 	    __return_value = result_type((*_M_g)() - _M_g->min())
204*38fd1498Szrj 	      / result_type(_M_g->max() - _M_g->min() + result_type(1));
205*38fd1498Szrj 	  else
206*38fd1498Szrj 	    __return_value = ((((*_M_g)() - _M_g->min())
207*38fd1498Szrj 			       / (_M_g->max() - _M_g->min()))
208*38fd1498Szrj 			      * std::numeric_limits<result_type>::max());
209*38fd1498Szrj 	  return __return_value;
210*38fd1498Szrj 	}
211*38fd1498Szrj 
212*38fd1498Szrj       private:
213*38fd1498Szrj 	_Engine* _M_g;
214*38fd1498Szrj       };
215*38fd1498Szrj   } // namespace __detail
216*38fd1498Szrj 
217*38fd1498Szrj   /**
218*38fd1498Szrj    * Produces random numbers on a given distribution function using a
219*38fd1498Szrj    * non-uniform random number generation engine.
220*38fd1498Szrj    *
221*38fd1498Szrj    * @todo the engine_value_type needs to be studied more carefully.
222*38fd1498Szrj    */
223*38fd1498Szrj   template<typename _Engine, typename _Dist>
224*38fd1498Szrj     class variate_generator
225*38fd1498Szrj     {
226*38fd1498Szrj       // Concept requirements.
227*38fd1498Szrj       __glibcxx_class_requires(_Engine, _CopyConstructibleConcept)
228*38fd1498Szrj       //  __glibcxx_class_requires(_Engine, _EngineConcept)
229*38fd1498Szrj       //  __glibcxx_class_requires(_Dist, _EngineConcept)
230*38fd1498Szrj 
231*38fd1498Szrj     public:
232*38fd1498Szrj       typedef _Engine                                engine_type;
233*38fd1498Szrj       typedef __detail::_Adaptor<_Engine, _Dist>     engine_value_type;
234*38fd1498Szrj       typedef _Dist                                  distribution_type;
235*38fd1498Szrj       typedef typename _Dist::result_type            result_type;
236*38fd1498Szrj 
237*38fd1498Szrj       // tr1:5.1.1 table 5.1 requirement
238*38fd1498Szrj       typedef typename __gnu_cxx::__enable_if<
239*38fd1498Szrj 	is_arithmetic<result_type>::value, result_type>::__type _IsValidType;
240*38fd1498Szrj 
241*38fd1498Szrj       /**
242*38fd1498Szrj        * Constructs a variate generator with the uniform random number
243*38fd1498Szrj        * generator @p __eng for the random distribution @p __dist.
244*38fd1498Szrj        *
245*38fd1498Szrj        * @throws Any exceptions which may thrown by the copy constructors of
246*38fd1498Szrj        * the @p _Engine or @p _Dist objects.
247*38fd1498Szrj        */
248*38fd1498Szrj       variate_generator(engine_type __eng, distribution_type __dist)
249*38fd1498Szrj       : _M_engine(__eng), _M_dist(__dist) { }
250*38fd1498Szrj 
251*38fd1498Szrj       /**
252*38fd1498Szrj        * Gets the next generated value on the distribution.
253*38fd1498Szrj        */
254*38fd1498Szrj       result_type
255*38fd1498Szrj       operator()()
256*38fd1498Szrj       { return _M_dist(_M_engine); }
257*38fd1498Szrj 
258*38fd1498Szrj       /**
259*38fd1498Szrj        * WTF?
260*38fd1498Szrj        */
261*38fd1498Szrj       template<typename _Tp>
262*38fd1498Szrj         result_type
263*38fd1498Szrj         operator()(_Tp __value)
264*38fd1498Szrj         { return _M_dist(_M_engine, __value); }
265*38fd1498Szrj 
266*38fd1498Szrj       /**
267*38fd1498Szrj        * Gets a reference to the underlying uniform random number generator
268*38fd1498Szrj        * object.
269*38fd1498Szrj        */
270*38fd1498Szrj       engine_value_type&
271*38fd1498Szrj       engine()
272*38fd1498Szrj       { return _M_engine; }
273*38fd1498Szrj 
274*38fd1498Szrj       /**
275*38fd1498Szrj        * Gets a const reference to the underlying uniform random number
276*38fd1498Szrj        * generator object.
277*38fd1498Szrj        */
278*38fd1498Szrj       const engine_value_type&
279*38fd1498Szrj       engine() const
280*38fd1498Szrj       { return _M_engine; }
281*38fd1498Szrj 
282*38fd1498Szrj       /**
283*38fd1498Szrj        * Gets a reference to the underlying random distribution.
284*38fd1498Szrj        */
285*38fd1498Szrj       distribution_type&
286*38fd1498Szrj       distribution()
287*38fd1498Szrj       { return _M_dist; }
288*38fd1498Szrj 
289*38fd1498Szrj       /**
290*38fd1498Szrj        * Gets a const reference to the underlying random distribution.
291*38fd1498Szrj        */
292*38fd1498Szrj       const distribution_type&
293*38fd1498Szrj       distribution() const
294*38fd1498Szrj       { return _M_dist; }
295*38fd1498Szrj 
296*38fd1498Szrj       /**
297*38fd1498Szrj        * Gets the closed lower bound of the distribution interval.
298*38fd1498Szrj        */
299*38fd1498Szrj       result_type
300*38fd1498Szrj       min() const
301*38fd1498Szrj       { return this->distribution().min(); }
302*38fd1498Szrj 
303*38fd1498Szrj       /**
304*38fd1498Szrj        * Gets the closed upper bound of the distribution interval.
305*38fd1498Szrj        */
306*38fd1498Szrj       result_type
307*38fd1498Szrj       max() const
308*38fd1498Szrj       { return this->distribution().max(); }
309*38fd1498Szrj 
310*38fd1498Szrj     private:
311*38fd1498Szrj       engine_value_type _M_engine;
312*38fd1498Szrj       distribution_type _M_dist;
313*38fd1498Szrj     };
314*38fd1498Szrj 
315*38fd1498Szrj 
316*38fd1498Szrj   /**
317*38fd1498Szrj    * @addtogroup tr1_random_generators Random Number Generators
318*38fd1498Szrj    * @ingroup tr1_random
319*38fd1498Szrj    *
320*38fd1498Szrj    * These classes define objects which provide random or pseudorandom
321*38fd1498Szrj    * numbers, either from a discrete or a continuous interval.  The
322*38fd1498Szrj    * random number generator supplied as a part of this library are
323*38fd1498Szrj    * all uniform random number generators which provide a sequence of
324*38fd1498Szrj    * random number uniformly distributed over their range.
325*38fd1498Szrj    *
326*38fd1498Szrj    * A number generator is a function object with an operator() that
327*38fd1498Szrj    * takes zero arguments and returns a number.
328*38fd1498Szrj    *
329*38fd1498Szrj    * A compliant random number generator must satisfy the following
330*38fd1498Szrj    * requirements.  <table border=1 cellpadding=10 cellspacing=0>
331*38fd1498Szrj    * <caption align=top>Random Number Generator Requirements</caption>
332*38fd1498Szrj    * <tr><td>To be documented.</td></tr> </table>
333*38fd1498Szrj    *
334*38fd1498Szrj    * @{
335*38fd1498Szrj    */
336*38fd1498Szrj 
337*38fd1498Szrj   /**
338*38fd1498Szrj    * @brief A model of a linear congruential random number generator.
339*38fd1498Szrj    *
340*38fd1498Szrj    * A random number generator that produces pseudorandom numbers using the
341*38fd1498Szrj    * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
342*38fd1498Szrj    *
343*38fd1498Szrj    * The template parameter @p _UIntType must be an unsigned integral type
344*38fd1498Szrj    * large enough to store values up to (__m-1). If the template parameter
345*38fd1498Szrj    * @p __m is 0, the modulus @p __m used is
346*38fd1498Szrj    * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
347*38fd1498Szrj    * parameters @p __a and @p __c must be less than @p __m.
348*38fd1498Szrj    *
349*38fd1498Szrj    * The size of the state is @f$ 1 @f$.
350*38fd1498Szrj    */
351*38fd1498Szrj   template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
352*38fd1498Szrj     class linear_congruential
353*38fd1498Szrj     {
354*38fd1498Szrj       __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
355*38fd1498Szrj       //  __glibcpp_class_requires(__a < __m && __c < __m)
356*38fd1498Szrj 
357*38fd1498Szrj     public:
358*38fd1498Szrj       /** The type of the generated random value. */
359*38fd1498Szrj       typedef _UIntType result_type;
360*38fd1498Szrj 
361*38fd1498Szrj       /** The multiplier. */
362*38fd1498Szrj       static const _UIntType multiplier = __a;
363*38fd1498Szrj       /** An increment. */
364*38fd1498Szrj       static const _UIntType increment = __c;
365*38fd1498Szrj       /** The modulus. */
366*38fd1498Szrj       static const _UIntType modulus = __m;
367*38fd1498Szrj 
368*38fd1498Szrj       /**
369*38fd1498Szrj        * Constructs a %linear_congruential random number generator engine with
370*38fd1498Szrj        * seed @p __s.  The default seed value is 1.
371*38fd1498Szrj        *
372*38fd1498Szrj        * @param __s The initial seed value.
373*38fd1498Szrj        */
374*38fd1498Szrj       explicit
375*38fd1498Szrj       linear_congruential(unsigned long __x0 = 1)
376*38fd1498Szrj       { this->seed(__x0); }
377*38fd1498Szrj 
378*38fd1498Szrj       /**
379*38fd1498Szrj        * Constructs a %linear_congruential random number generator engine
380*38fd1498Szrj        * seeded from the generator function @p __g.
381*38fd1498Szrj        *
382*38fd1498Szrj        * @param __g The seed generator function.
383*38fd1498Szrj        */
384*38fd1498Szrj       template<class _Gen>
385*38fd1498Szrj         linear_congruential(_Gen& __g)
386*38fd1498Szrj         { this->seed(__g); }
387*38fd1498Szrj 
388*38fd1498Szrj       /**
389*38fd1498Szrj        * Reseeds the %linear_congruential random number generator engine
390*38fd1498Szrj        * sequence to the seed @g __s.
391*38fd1498Szrj        *
392*38fd1498Szrj        * @param __s The new seed.
393*38fd1498Szrj        */
394*38fd1498Szrj       void
395*38fd1498Szrj       seed(unsigned long __s = 1);
396*38fd1498Szrj 
397*38fd1498Szrj       /**
398*38fd1498Szrj        * Reseeds the %linear_congruential random number generator engine
399*38fd1498Szrj        * sequence using values from the generator function @p __g.
400*38fd1498Szrj        *
401*38fd1498Szrj        * @param __g the seed generator function.
402*38fd1498Szrj        */
403*38fd1498Szrj       template<class _Gen>
404*38fd1498Szrj         void
405*38fd1498Szrj         seed(_Gen& __g)
406*38fd1498Szrj         { seed(__g, typename is_fundamental<_Gen>::type()); }
407*38fd1498Szrj 
408*38fd1498Szrj       /**
409*38fd1498Szrj        * Gets the smallest possible value in the output range.
410*38fd1498Szrj        *
411*38fd1498Szrj        * The minimum depends on the @p __c parameter: if it is zero, the
412*38fd1498Szrj        * minimum generated must be > 0, otherwise 0 is allowed.
413*38fd1498Szrj        */
414*38fd1498Szrj       result_type
415*38fd1498Szrj       min() const
416*38fd1498Szrj       { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
417*38fd1498Szrj 
418*38fd1498Szrj       /**
419*38fd1498Szrj        * Gets the largest possible value in the output range.
420*38fd1498Szrj        */
421*38fd1498Szrj       result_type
422*38fd1498Szrj       max() const
423*38fd1498Szrj       { return __m - 1; }
424*38fd1498Szrj 
425*38fd1498Szrj       /**
426*38fd1498Szrj        * Gets the next random number in the sequence.
427*38fd1498Szrj        */
428*38fd1498Szrj       result_type
429*38fd1498Szrj       operator()();
430*38fd1498Szrj 
431*38fd1498Szrj       /**
432*38fd1498Szrj        * Compares two linear congruential random number generator
433*38fd1498Szrj        * objects of the same type for equality.
434*38fd1498Szrj        *
435*38fd1498Szrj        * @param __lhs A linear congruential random number generator object.
436*38fd1498Szrj        * @param __rhs Another linear congruential random number generator obj.
437*38fd1498Szrj        *
438*38fd1498Szrj        * @returns true if the two objects are equal, false otherwise.
439*38fd1498Szrj        */
440*38fd1498Szrj       friend bool
441*38fd1498Szrj       operator==(const linear_congruential& __lhs,
442*38fd1498Szrj 		 const linear_congruential& __rhs)
443*38fd1498Szrj       { return __lhs._M_x == __rhs._M_x; }
444*38fd1498Szrj 
445*38fd1498Szrj       /**
446*38fd1498Szrj        * Compares two linear congruential random number generator
447*38fd1498Szrj        * objects of the same type for inequality.
448*38fd1498Szrj        *
449*38fd1498Szrj        * @param __lhs A linear congruential random number generator object.
450*38fd1498Szrj        * @param __rhs Another linear congruential random number generator obj.
451*38fd1498Szrj        *
452*38fd1498Szrj        * @returns true if the two objects are not equal, false otherwise.
453*38fd1498Szrj        */
454*38fd1498Szrj       friend bool
455*38fd1498Szrj       operator!=(const linear_congruential& __lhs,
456*38fd1498Szrj 		 const linear_congruential& __rhs)
457*38fd1498Szrj       { return !(__lhs == __rhs); }
458*38fd1498Szrj 
459*38fd1498Szrj       /**
460*38fd1498Szrj        * Writes the textual representation of the state x(i) of x to @p __os.
461*38fd1498Szrj        *
462*38fd1498Szrj        * @param __os  The output stream.
463*38fd1498Szrj        * @param __lcr A % linear_congruential random number generator.
464*38fd1498Szrj        * @returns __os.
465*38fd1498Szrj        */
466*38fd1498Szrj       template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
467*38fd1498Szrj 	       _UIntType1 __m1,
468*38fd1498Szrj 	       typename _CharT, typename _Traits>
469*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
470*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
471*38fd1498Szrj 		   const linear_congruential<_UIntType1, __a1, __c1,
472*38fd1498Szrj 		   __m1>& __lcr);
473*38fd1498Szrj 
474*38fd1498Szrj       /**
475*38fd1498Szrj        * Sets the state of the engine by reading its textual
476*38fd1498Szrj        * representation from @p __is.
477*38fd1498Szrj        *
478*38fd1498Szrj        * The textual representation must have been previously written using an
479*38fd1498Szrj        * output stream whose imbued locale and whose type's template
480*38fd1498Szrj        * specialization arguments _CharT and _Traits were the same as those of
481*38fd1498Szrj        * @p __is.
482*38fd1498Szrj        *
483*38fd1498Szrj        * @param __is  The input stream.
484*38fd1498Szrj        * @param __lcr A % linear_congruential random number generator.
485*38fd1498Szrj        * @returns __is.
486*38fd1498Szrj        */
487*38fd1498Szrj       template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
488*38fd1498Szrj 	       _UIntType1 __m1,
489*38fd1498Szrj 	       typename _CharT, typename _Traits>
490*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
491*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
492*38fd1498Szrj 		   linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
493*38fd1498Szrj 
494*38fd1498Szrj     private:
495*38fd1498Szrj       template<class _Gen>
496*38fd1498Szrj         void
497*38fd1498Szrj         seed(_Gen& __g, true_type)
498*38fd1498Szrj         { return seed(static_cast<unsigned long>(__g)); }
499*38fd1498Szrj 
500*38fd1498Szrj       template<class _Gen>
501*38fd1498Szrj         void
502*38fd1498Szrj         seed(_Gen& __g, false_type);
503*38fd1498Szrj 
504*38fd1498Szrj       _UIntType _M_x;
505*38fd1498Szrj     };
506*38fd1498Szrj 
507*38fd1498Szrj   /**
508*38fd1498Szrj    * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
509*38fd1498Szrj    */
510*38fd1498Szrj   typedef linear_congruential<unsigned long, 16807, 0, 2147483647> minstd_rand0;
511*38fd1498Szrj 
512*38fd1498Szrj   /**
513*38fd1498Szrj    * An alternative LCR (Lehmer Generator function) .
514*38fd1498Szrj    */
515*38fd1498Szrj   typedef linear_congruential<unsigned long, 48271, 0, 2147483647> minstd_rand;
516*38fd1498Szrj 
517*38fd1498Szrj 
518*38fd1498Szrj   /**
519*38fd1498Szrj    * A generalized feedback shift register discrete random number generator.
520*38fd1498Szrj    *
521*38fd1498Szrj    * This algorithm avoids multiplication and division and is designed to be
522*38fd1498Szrj    * friendly to a pipelined architecture.  If the parameters are chosen
523*38fd1498Szrj    * correctly, this generator will produce numbers with a very long period and
524*38fd1498Szrj    * fairly good apparent entropy, although still not cryptographically strong.
525*38fd1498Szrj    *
526*38fd1498Szrj    * The best way to use this generator is with the predefined mt19937 class.
527*38fd1498Szrj    *
528*38fd1498Szrj    * This algorithm was originally invented by Makoto Matsumoto and
529*38fd1498Szrj    * Takuji Nishimura.
530*38fd1498Szrj    *
531*38fd1498Szrj    * @var word_size   The number of bits in each element of the state vector.
532*38fd1498Szrj    * @var state_size  The degree of recursion.
533*38fd1498Szrj    * @var shift_size  The period parameter.
534*38fd1498Szrj    * @var mask_bits   The separation point bit index.
535*38fd1498Szrj    * @var parameter_a The last row of the twist matrix.
536*38fd1498Szrj    * @var output_u    The first right-shift tempering matrix parameter.
537*38fd1498Szrj    * @var output_s    The first left-shift tempering matrix parameter.
538*38fd1498Szrj    * @var output_b    The first left-shift tempering matrix mask.
539*38fd1498Szrj    * @var output_t    The second left-shift tempering matrix parameter.
540*38fd1498Szrj    * @var output_c    The second left-shift tempering matrix mask.
541*38fd1498Szrj    * @var output_l    The second right-shift tempering matrix parameter.
542*38fd1498Szrj    */
543*38fd1498Szrj   template<class _UIntType, int __w, int __n, int __m, int __r,
544*38fd1498Szrj 	   _UIntType __a, int __u, int __s, _UIntType __b, int __t,
545*38fd1498Szrj 	   _UIntType __c, int __l>
546*38fd1498Szrj     class mersenne_twister
547*38fd1498Szrj     {
548*38fd1498Szrj       __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
549*38fd1498Szrj 
550*38fd1498Szrj     public:
551*38fd1498Szrj       // types
552*38fd1498Szrj       typedef _UIntType result_type;
553*38fd1498Szrj 
554*38fd1498Szrj       // parameter values
555*38fd1498Szrj       static const int       word_size   = __w;
556*38fd1498Szrj       static const int       state_size  = __n;
557*38fd1498Szrj       static const int       shift_size  = __m;
558*38fd1498Szrj       static const int       mask_bits   = __r;
559*38fd1498Szrj       static const _UIntType parameter_a = __a;
560*38fd1498Szrj       static const int       output_u    = __u;
561*38fd1498Szrj       static const int       output_s    = __s;
562*38fd1498Szrj       static const _UIntType output_b    = __b;
563*38fd1498Szrj       static const int       output_t    = __t;
564*38fd1498Szrj       static const _UIntType output_c    = __c;
565*38fd1498Szrj       static const int       output_l    = __l;
566*38fd1498Szrj 
567*38fd1498Szrj       // constructors and member function
568*38fd1498Szrj       mersenne_twister()
569*38fd1498Szrj       { seed(); }
570*38fd1498Szrj 
571*38fd1498Szrj       explicit
572*38fd1498Szrj       mersenne_twister(unsigned long __value)
573*38fd1498Szrj       { seed(__value); }
574*38fd1498Szrj 
575*38fd1498Szrj       template<class _Gen>
576*38fd1498Szrj         mersenne_twister(_Gen& __g)
577*38fd1498Szrj         { seed(__g); }
578*38fd1498Szrj 
579*38fd1498Szrj       void
580*38fd1498Szrj       seed()
581*38fd1498Szrj       { seed(5489UL); }
582*38fd1498Szrj 
583*38fd1498Szrj       void
584*38fd1498Szrj       seed(unsigned long __value);
585*38fd1498Szrj 
586*38fd1498Szrj       template<class _Gen>
587*38fd1498Szrj         void
588*38fd1498Szrj         seed(_Gen& __g)
589*38fd1498Szrj         { seed(__g, typename is_fundamental<_Gen>::type()); }
590*38fd1498Szrj 
591*38fd1498Szrj       result_type
592*38fd1498Szrj       min() const
593*38fd1498Szrj       { return 0; }
594*38fd1498Szrj 
595*38fd1498Szrj       result_type
596*38fd1498Szrj       max() const
597*38fd1498Szrj       { return __detail::_Shift<_UIntType, __w>::__value - 1; }
598*38fd1498Szrj 
599*38fd1498Szrj       result_type
600*38fd1498Szrj       operator()();
601*38fd1498Szrj 
602*38fd1498Szrj       /**
603*38fd1498Szrj        * Compares two % mersenne_twister random number generator objects of
604*38fd1498Szrj        * the same type for equality.
605*38fd1498Szrj        *
606*38fd1498Szrj        * @param __lhs A % mersenne_twister random number generator object.
607*38fd1498Szrj        * @param __rhs Another % mersenne_twister random number generator
608*38fd1498Szrj        *              object.
609*38fd1498Szrj        *
610*38fd1498Szrj        * @returns true if the two objects are equal, false otherwise.
611*38fd1498Szrj        */
612*38fd1498Szrj       friend bool
613*38fd1498Szrj       operator==(const mersenne_twister& __lhs,
614*38fd1498Szrj 		 const mersenne_twister& __rhs)
615*38fd1498Szrj       { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
616*38fd1498Szrj 
617*38fd1498Szrj       /**
618*38fd1498Szrj        * Compares two % mersenne_twister random number generator objects of
619*38fd1498Szrj        * the same type for inequality.
620*38fd1498Szrj        *
621*38fd1498Szrj        * @param __lhs A % mersenne_twister random number generator object.
622*38fd1498Szrj        * @param __rhs Another % mersenne_twister random number generator
623*38fd1498Szrj        *              object.
624*38fd1498Szrj        *
625*38fd1498Szrj        * @returns true if the two objects are not equal, false otherwise.
626*38fd1498Szrj        */
627*38fd1498Szrj       friend bool
628*38fd1498Szrj       operator!=(const mersenne_twister& __lhs,
629*38fd1498Szrj 		 const mersenne_twister& __rhs)
630*38fd1498Szrj       { return !(__lhs == __rhs); }
631*38fd1498Szrj 
632*38fd1498Szrj       /**
633*38fd1498Szrj        * Inserts the current state of a % mersenne_twister random number
634*38fd1498Szrj        * generator engine @p __x into the output stream @p __os.
635*38fd1498Szrj        *
636*38fd1498Szrj        * @param __os An output stream.
637*38fd1498Szrj        * @param __x  A % mersenne_twister random number generator engine.
638*38fd1498Szrj        *
639*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
640*38fd1498Szrj        * an error state.
641*38fd1498Szrj        */
642*38fd1498Szrj       template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
643*38fd1498Szrj 	       _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
644*38fd1498Szrj 	       _UIntType1 __c1, int __l1,
645*38fd1498Szrj 	       typename _CharT, typename _Traits>
646*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
647*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
648*38fd1498Szrj 		   const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
649*38fd1498Szrj 		   __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
650*38fd1498Szrj 
651*38fd1498Szrj       /**
652*38fd1498Szrj        * Extracts the current state of a % mersenne_twister random number
653*38fd1498Szrj        * generator engine @p __x from the input stream @p __is.
654*38fd1498Szrj        *
655*38fd1498Szrj        * @param __is An input stream.
656*38fd1498Szrj        * @param __x  A % mersenne_twister random number generator engine.
657*38fd1498Szrj        *
658*38fd1498Szrj        * @returns The input stream with the state of @p __x extracted or in
659*38fd1498Szrj        * an error state.
660*38fd1498Szrj        */
661*38fd1498Szrj       template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
662*38fd1498Szrj 	       _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
663*38fd1498Szrj 	       _UIntType1 __c1, int __l1,
664*38fd1498Szrj 	       typename _CharT, typename _Traits>
665*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
666*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
667*38fd1498Szrj 		   mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
668*38fd1498Szrj 		   __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
669*38fd1498Szrj 
670*38fd1498Szrj     private:
671*38fd1498Szrj       template<class _Gen>
672*38fd1498Szrj         void
673*38fd1498Szrj         seed(_Gen& __g, true_type)
674*38fd1498Szrj         { return seed(static_cast<unsigned long>(__g)); }
675*38fd1498Szrj 
676*38fd1498Szrj       template<class _Gen>
677*38fd1498Szrj         void
678*38fd1498Szrj         seed(_Gen& __g, false_type);
679*38fd1498Szrj 
680*38fd1498Szrj       _UIntType _M_x[state_size];
681*38fd1498Szrj       int       _M_p;
682*38fd1498Szrj     };
683*38fd1498Szrj 
684*38fd1498Szrj   /**
685*38fd1498Szrj    * The classic Mersenne Twister.
686*38fd1498Szrj    *
687*38fd1498Szrj    * Reference:
688*38fd1498Szrj    * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
689*38fd1498Szrj    * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
690*38fd1498Szrj    * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
691*38fd1498Szrj    */
692*38fd1498Szrj   typedef mersenne_twister<
693*38fd1498Szrj     unsigned long, 32, 624, 397, 31,
694*38fd1498Szrj     0x9908b0dful, 11, 7,
695*38fd1498Szrj     0x9d2c5680ul, 15,
696*38fd1498Szrj     0xefc60000ul, 18
697*38fd1498Szrj     > mt19937;
698*38fd1498Szrj 
699*38fd1498Szrj 
700*38fd1498Szrj   /**
701*38fd1498Szrj    * @brief The Marsaglia-Zaman generator.
702*38fd1498Szrj    *
703*38fd1498Szrj    * This is a model of a Generalized Fibonacci discrete random number
704*38fd1498Szrj    * generator, sometimes referred to as the SWC generator.
705*38fd1498Szrj    *
706*38fd1498Szrj    * A discrete random number generator that produces pseudorandom
707*38fd1498Szrj    * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
708*38fd1498Szrj    * carry_{i-1}) \bmod m @f$.
709*38fd1498Szrj    *
710*38fd1498Szrj    * The size of the state is @f$ r @f$
711*38fd1498Szrj    * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
712*38fd1498Szrj    *
713*38fd1498Szrj    * N1688[4.13] says <em>the template parameter _IntType shall denote
714*38fd1498Szrj    * an integral type large enough to store values up to m</em>.
715*38fd1498Szrj    *
716*38fd1498Szrj    * @var _M_x     The state of the generator.  This is a ring buffer.
717*38fd1498Szrj    * @var _M_carry The carry.
718*38fd1498Szrj    * @var _M_p     Current index of x(i - r).
719*38fd1498Szrj    */
720*38fd1498Szrj   template<typename _IntType, _IntType __m, int __s, int __r>
721*38fd1498Szrj     class subtract_with_carry
722*38fd1498Szrj     {
723*38fd1498Szrj       __glibcxx_class_requires(_IntType, _IntegerConcept)
724*38fd1498Szrj 
725*38fd1498Szrj     public:
726*38fd1498Szrj       /** The type of the generated random value. */
727*38fd1498Szrj       typedef _IntType result_type;
728*38fd1498Szrj 
729*38fd1498Szrj       // parameter values
730*38fd1498Szrj       static const _IntType modulus   = __m;
731*38fd1498Szrj       static const int      long_lag  = __r;
732*38fd1498Szrj       static const int      short_lag = __s;
733*38fd1498Szrj 
734*38fd1498Szrj       /**
735*38fd1498Szrj        * Constructs a default-initialized % subtract_with_carry random number
736*38fd1498Szrj        * generator.
737*38fd1498Szrj        */
738*38fd1498Szrj       subtract_with_carry()
739*38fd1498Szrj       { this->seed(); }
740*38fd1498Szrj 
741*38fd1498Szrj       /**
742*38fd1498Szrj        * Constructs an explicitly seeded % subtract_with_carry random number
743*38fd1498Szrj        * generator.
744*38fd1498Szrj        */
745*38fd1498Szrj       explicit
746*38fd1498Szrj       subtract_with_carry(unsigned long __value)
747*38fd1498Szrj       { this->seed(__value); }
748*38fd1498Szrj 
749*38fd1498Szrj       /**
750*38fd1498Szrj        * Constructs a %subtract_with_carry random number generator engine
751*38fd1498Szrj        * seeded from the generator function @p __g.
752*38fd1498Szrj        *
753*38fd1498Szrj        * @param __g The seed generator function.
754*38fd1498Szrj        */
755*38fd1498Szrj       template<class _Gen>
756*38fd1498Szrj         subtract_with_carry(_Gen& __g)
757*38fd1498Szrj         { this->seed(__g); }
758*38fd1498Szrj 
759*38fd1498Szrj       /**
760*38fd1498Szrj        * Seeds the initial state @f$ x_0 @f$ of the random number generator.
761*38fd1498Szrj        *
762*38fd1498Szrj        * N1688[4.19] modifies this as follows.  If @p __value == 0,
763*38fd1498Szrj        * sets value to 19780503.  In any case, with a linear
764*38fd1498Szrj        * congruential generator lcg(i) having parameters @f$ m_{lcg} =
765*38fd1498Szrj        * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
766*38fd1498Szrj        * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
767*38fd1498Szrj        * \dots lcg(r) \bmod m @f$ respectively.  If @f$ x_{-1} = 0 @f$
768*38fd1498Szrj        * set carry to 1, otherwise sets carry to 0.
769*38fd1498Szrj        */
770*38fd1498Szrj       void
771*38fd1498Szrj       seed(unsigned long __value = 19780503);
772*38fd1498Szrj 
773*38fd1498Szrj       /**
774*38fd1498Szrj        * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
775*38fd1498Szrj        * random number generator.
776*38fd1498Szrj        */
777*38fd1498Szrj       template<class _Gen>
778*38fd1498Szrj         void
779*38fd1498Szrj         seed(_Gen& __g)
780*38fd1498Szrj         { seed(__g, typename is_fundamental<_Gen>::type()); }
781*38fd1498Szrj 
782*38fd1498Szrj       /**
783*38fd1498Szrj        * Gets the inclusive minimum value of the range of random integers
784*38fd1498Szrj        * returned by this generator.
785*38fd1498Szrj        */
786*38fd1498Szrj       result_type
787*38fd1498Szrj       min() const
788*38fd1498Szrj       { return 0; }
789*38fd1498Szrj 
790*38fd1498Szrj       /**
791*38fd1498Szrj        * Gets the inclusive maximum value of the range of random integers
792*38fd1498Szrj        * returned by this generator.
793*38fd1498Szrj        */
794*38fd1498Szrj       result_type
795*38fd1498Szrj       max() const
796*38fd1498Szrj       { return this->modulus - 1; }
797*38fd1498Szrj 
798*38fd1498Szrj       /**
799*38fd1498Szrj        * Gets the next random number in the sequence.
800*38fd1498Szrj        */
801*38fd1498Szrj       result_type
802*38fd1498Szrj       operator()();
803*38fd1498Szrj 
804*38fd1498Szrj       /**
805*38fd1498Szrj        * Compares two % subtract_with_carry random number generator objects of
806*38fd1498Szrj        * the same type for equality.
807*38fd1498Szrj        *
808*38fd1498Szrj        * @param __lhs A % subtract_with_carry random number generator object.
809*38fd1498Szrj        * @param __rhs Another % subtract_with_carry random number generator
810*38fd1498Szrj        *              object.
811*38fd1498Szrj        *
812*38fd1498Szrj        * @returns true if the two objects are equal, false otherwise.
813*38fd1498Szrj        */
814*38fd1498Szrj       friend bool
815*38fd1498Szrj       operator==(const subtract_with_carry& __lhs,
816*38fd1498Szrj 		 const subtract_with_carry& __rhs)
817*38fd1498Szrj       { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
818*38fd1498Szrj 
819*38fd1498Szrj       /**
820*38fd1498Szrj        * Compares two % subtract_with_carry random number generator objects of
821*38fd1498Szrj        * the same type for inequality.
822*38fd1498Szrj        *
823*38fd1498Szrj        * @param __lhs A % subtract_with_carry random number generator object.
824*38fd1498Szrj        * @param __rhs Another % subtract_with_carry random number generator
825*38fd1498Szrj        *              object.
826*38fd1498Szrj        *
827*38fd1498Szrj        * @returns true if the two objects are not equal, false otherwise.
828*38fd1498Szrj        */
829*38fd1498Szrj       friend bool
830*38fd1498Szrj       operator!=(const subtract_with_carry& __lhs,
831*38fd1498Szrj 		 const subtract_with_carry& __rhs)
832*38fd1498Szrj       { return !(__lhs == __rhs); }
833*38fd1498Szrj 
834*38fd1498Szrj       /**
835*38fd1498Szrj        * Inserts the current state of a % subtract_with_carry random number
836*38fd1498Szrj        * generator engine @p __x into the output stream @p __os.
837*38fd1498Szrj        *
838*38fd1498Szrj        * @param __os An output stream.
839*38fd1498Szrj        * @param __x  A % subtract_with_carry random number generator engine.
840*38fd1498Szrj        *
841*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
842*38fd1498Szrj        * an error state.
843*38fd1498Szrj        */
844*38fd1498Szrj       template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
845*38fd1498Szrj 	       typename _CharT, typename _Traits>
846*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
847*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
848*38fd1498Szrj 		   const subtract_with_carry<_IntType1, __m1, __s1,
849*38fd1498Szrj 		   __r1>& __x);
850*38fd1498Szrj 
851*38fd1498Szrj       /**
852*38fd1498Szrj        * Extracts the current state of a % subtract_with_carry random number
853*38fd1498Szrj        * generator engine @p __x from the input stream @p __is.
854*38fd1498Szrj        *
855*38fd1498Szrj        * @param __is An input stream.
856*38fd1498Szrj        * @param __x  A % subtract_with_carry random number generator engine.
857*38fd1498Szrj        *
858*38fd1498Szrj        * @returns The input stream with the state of @p __x extracted or in
859*38fd1498Szrj        * an error state.
860*38fd1498Szrj        */
861*38fd1498Szrj       template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
862*38fd1498Szrj 	       typename _CharT, typename _Traits>
863*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
864*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
865*38fd1498Szrj 		   subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
866*38fd1498Szrj 
867*38fd1498Szrj     private:
868*38fd1498Szrj       template<class _Gen>
869*38fd1498Szrj         void
870*38fd1498Szrj         seed(_Gen& __g, true_type)
871*38fd1498Szrj         { return seed(static_cast<unsigned long>(__g)); }
872*38fd1498Szrj 
873*38fd1498Szrj       template<class _Gen>
874*38fd1498Szrj         void
875*38fd1498Szrj         seed(_Gen& __g, false_type);
876*38fd1498Szrj 
877*38fd1498Szrj       typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
878*38fd1498Szrj 
879*38fd1498Szrj       _UIntType  _M_x[long_lag];
880*38fd1498Szrj       _UIntType  _M_carry;
881*38fd1498Szrj       int        _M_p;
882*38fd1498Szrj     };
883*38fd1498Szrj 
884*38fd1498Szrj 
885*38fd1498Szrj   /**
886*38fd1498Szrj    * @brief The Marsaglia-Zaman generator (floats version).
887*38fd1498Szrj    *
888*38fd1498Szrj    * @var _M_x     The state of the generator.  This is a ring buffer.
889*38fd1498Szrj    * @var _M_carry The carry.
890*38fd1498Szrj    * @var _M_p     Current index of x(i - r).
891*38fd1498Szrj    * @var _M_npows Precomputed negative powers of 2.
892*38fd1498Szrj    */
893*38fd1498Szrj   template<typename _RealType, int __w, int __s, int __r>
894*38fd1498Szrj     class subtract_with_carry_01
895*38fd1498Szrj     {
896*38fd1498Szrj     public:
897*38fd1498Szrj       /** The type of the generated random value. */
898*38fd1498Szrj       typedef _RealType result_type;
899*38fd1498Szrj 
900*38fd1498Szrj       // parameter values
901*38fd1498Szrj       static const int      word_size = __w;
902*38fd1498Szrj       static const int      long_lag  = __r;
903*38fd1498Szrj       static const int      short_lag = __s;
904*38fd1498Szrj 
905*38fd1498Szrj       /**
906*38fd1498Szrj        * Constructs a default-initialized % subtract_with_carry_01 random
907*38fd1498Szrj        * number generator.
908*38fd1498Szrj        */
909*38fd1498Szrj       subtract_with_carry_01()
910*38fd1498Szrj       {
911*38fd1498Szrj 	this->seed();
912*38fd1498Szrj 	_M_initialize_npows();
913*38fd1498Szrj       }
914*38fd1498Szrj 
915*38fd1498Szrj       /**
916*38fd1498Szrj        * Constructs an explicitly seeded % subtract_with_carry_01 random number
917*38fd1498Szrj        * generator.
918*38fd1498Szrj        */
919*38fd1498Szrj       explicit
920*38fd1498Szrj       subtract_with_carry_01(unsigned long __value)
921*38fd1498Szrj       {
922*38fd1498Szrj 	this->seed(__value);
923*38fd1498Szrj 	_M_initialize_npows();
924*38fd1498Szrj       }
925*38fd1498Szrj 
926*38fd1498Szrj       /**
927*38fd1498Szrj        * Constructs a % subtract_with_carry_01 random number generator engine
928*38fd1498Szrj        * seeded from the generator function @p __g.
929*38fd1498Szrj        *
930*38fd1498Szrj        * @param __g The seed generator function.
931*38fd1498Szrj        */
932*38fd1498Szrj       template<class _Gen>
933*38fd1498Szrj         subtract_with_carry_01(_Gen& __g)
934*38fd1498Szrj         {
935*38fd1498Szrj 	  this->seed(__g);
936*38fd1498Szrj 	  _M_initialize_npows();
937*38fd1498Szrj 	}
938*38fd1498Szrj 
939*38fd1498Szrj       /**
940*38fd1498Szrj        * Seeds the initial state @f$ x_0 @f$ of the random number generator.
941*38fd1498Szrj        */
942*38fd1498Szrj       void
943*38fd1498Szrj       seed(unsigned long __value = 19780503);
944*38fd1498Szrj 
945*38fd1498Szrj       /**
946*38fd1498Szrj        * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
947*38fd1498Szrj        * random number generator.
948*38fd1498Szrj        */
949*38fd1498Szrj       template<class _Gen>
950*38fd1498Szrj         void
951*38fd1498Szrj         seed(_Gen& __g)
952*38fd1498Szrj         { seed(__g, typename is_fundamental<_Gen>::type()); }
953*38fd1498Szrj 
954*38fd1498Szrj       /**
955*38fd1498Szrj        * Gets the minimum value of the range of random floats
956*38fd1498Szrj        * returned by this generator.
957*38fd1498Szrj        */
958*38fd1498Szrj       result_type
959*38fd1498Szrj       min() const
960*38fd1498Szrj       { return 0.0; }
961*38fd1498Szrj 
962*38fd1498Szrj       /**
963*38fd1498Szrj        * Gets the maximum value of the range of random floats
964*38fd1498Szrj        * returned by this generator.
965*38fd1498Szrj        */
966*38fd1498Szrj       result_type
967*38fd1498Szrj       max() const
968*38fd1498Szrj       { return 1.0; }
969*38fd1498Szrj 
970*38fd1498Szrj       /**
971*38fd1498Szrj        * Gets the next random number in the sequence.
972*38fd1498Szrj        */
973*38fd1498Szrj       result_type
974*38fd1498Szrj       operator()();
975*38fd1498Szrj 
976*38fd1498Szrj       /**
977*38fd1498Szrj        * Compares two % subtract_with_carry_01 random number generator objects
978*38fd1498Szrj        * of the same type for equality.
979*38fd1498Szrj        *
980*38fd1498Szrj        * @param __lhs A % subtract_with_carry_01 random number
981*38fd1498Szrj        *              generator object.
982*38fd1498Szrj        * @param __rhs Another % subtract_with_carry_01 random number generator
983*38fd1498Szrj        *              object.
984*38fd1498Szrj        *
985*38fd1498Szrj        * @returns true if the two objects are equal, false otherwise.
986*38fd1498Szrj        */
987*38fd1498Szrj       friend bool
988*38fd1498Szrj       operator==(const subtract_with_carry_01& __lhs,
989*38fd1498Szrj 		 const subtract_with_carry_01& __rhs)
990*38fd1498Szrj       {
991*38fd1498Szrj 	for (int __i = 0; __i < long_lag; ++__i)
992*38fd1498Szrj 	  if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
993*38fd1498Szrj 			  __rhs._M_x[__i]))
994*38fd1498Szrj 	    return false;
995*38fd1498Szrj 	return true;
996*38fd1498Szrj       }
997*38fd1498Szrj 
998*38fd1498Szrj       /**
999*38fd1498Szrj        * Compares two % subtract_with_carry_01 random number generator objects
1000*38fd1498Szrj        * of the same type for inequality.
1001*38fd1498Szrj        *
1002*38fd1498Szrj        * @param __lhs A % subtract_with_carry_01 random number
1003*38fd1498Szrj        *              generator object.
1004*38fd1498Szrj        *
1005*38fd1498Szrj        * @param __rhs Another % subtract_with_carry_01 random number generator
1006*38fd1498Szrj        *              object.
1007*38fd1498Szrj        *
1008*38fd1498Szrj        * @returns true if the two objects are not equal, false otherwise.
1009*38fd1498Szrj        */
1010*38fd1498Szrj       friend bool
1011*38fd1498Szrj       operator!=(const subtract_with_carry_01& __lhs,
1012*38fd1498Szrj 		 const subtract_with_carry_01& __rhs)
1013*38fd1498Szrj       { return !(__lhs == __rhs); }
1014*38fd1498Szrj 
1015*38fd1498Szrj       /**
1016*38fd1498Szrj        * Inserts the current state of a % subtract_with_carry_01 random number
1017*38fd1498Szrj        * generator engine @p __x into the output stream @p __os.
1018*38fd1498Szrj        *
1019*38fd1498Szrj        * @param __os An output stream.
1020*38fd1498Szrj        * @param __x  A % subtract_with_carry_01 random number generator engine.
1021*38fd1498Szrj        *
1022*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
1023*38fd1498Szrj        * an error state.
1024*38fd1498Szrj        */
1025*38fd1498Szrj       template<typename _RealType1, int __w1, int __s1, int __r1,
1026*38fd1498Szrj 	       typename _CharT, typename _Traits>
1027*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
1028*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1029*38fd1498Szrj 		   const subtract_with_carry_01<_RealType1, __w1, __s1,
1030*38fd1498Szrj 		   __r1>& __x);
1031*38fd1498Szrj 
1032*38fd1498Szrj       /**
1033*38fd1498Szrj        * Extracts the current state of a % subtract_with_carry_01 random number
1034*38fd1498Szrj        * generator engine @p __x from the input stream @p __is.
1035*38fd1498Szrj        *
1036*38fd1498Szrj        * @param __is An input stream.
1037*38fd1498Szrj        * @param __x  A % subtract_with_carry_01 random number generator engine.
1038*38fd1498Szrj        *
1039*38fd1498Szrj        * @returns The input stream with the state of @p __x extracted or in
1040*38fd1498Szrj        * an error state.
1041*38fd1498Szrj        */
1042*38fd1498Szrj       template<typename _RealType1, int __w1, int __s1, int __r1,
1043*38fd1498Szrj 	       typename _CharT, typename _Traits>
1044*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
1045*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1046*38fd1498Szrj 		   subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
1047*38fd1498Szrj 
1048*38fd1498Szrj     private:
1049*38fd1498Szrj       template<class _Gen>
1050*38fd1498Szrj         void
1051*38fd1498Szrj         seed(_Gen& __g, true_type)
1052*38fd1498Szrj         { return seed(static_cast<unsigned long>(__g)); }
1053*38fd1498Szrj 
1054*38fd1498Szrj       template<class _Gen>
1055*38fd1498Szrj         void
1056*38fd1498Szrj         seed(_Gen& __g, false_type);
1057*38fd1498Szrj 
1058*38fd1498Szrj       void
1059*38fd1498Szrj       _M_initialize_npows();
1060*38fd1498Szrj 
1061*38fd1498Szrj       static const int __n = (__w + 31) / 32;
1062*38fd1498Szrj 
1063*38fd1498Szrj       typedef __detail::_UInt32Type _UInt32Type;
1064*38fd1498Szrj       _UInt32Type  _M_x[long_lag][__n];
1065*38fd1498Szrj       _RealType    _M_npows[__n];
1066*38fd1498Szrj       _UInt32Type  _M_carry;
1067*38fd1498Szrj       int          _M_p;
1068*38fd1498Szrj     };
1069*38fd1498Szrj 
1070*38fd1498Szrj   typedef subtract_with_carry_01<float, 24, 10, 24>   ranlux_base_01;
1071*38fd1498Szrj 
1072*38fd1498Szrj   // _GLIBCXX_RESOLVE_LIB_DEFECTS
1073*38fd1498Szrj   // 508. Bad parameters for ranlux64_base_01.
1074*38fd1498Szrj   typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
1075*38fd1498Szrj 
1076*38fd1498Szrj 
1077*38fd1498Szrj   /**
1078*38fd1498Szrj    * Produces random numbers from some base engine by discarding blocks of
1079*38fd1498Szrj    * data.
1080*38fd1498Szrj    *
1081*38fd1498Szrj    * 0 <= @p __r <= @p __p
1082*38fd1498Szrj    */
1083*38fd1498Szrj   template<class _UniformRandomNumberGenerator, int __p, int __r>
1084*38fd1498Szrj     class discard_block
1085*38fd1498Szrj     {
1086*38fd1498Szrj       // __glibcxx_class_requires(typename base_type::result_type,
1087*38fd1498Szrj       //                          ArithmeticTypeConcept)
1088*38fd1498Szrj 
1089*38fd1498Szrj     public:
1090*38fd1498Szrj       /** The type of the underlying generator engine. */
1091*38fd1498Szrj       typedef _UniformRandomNumberGenerator   base_type;
1092*38fd1498Szrj       /** The type of the generated random value. */
1093*38fd1498Szrj       typedef typename base_type::result_type result_type;
1094*38fd1498Szrj 
1095*38fd1498Szrj       // parameter values
1096*38fd1498Szrj       static const int block_size = __p;
1097*38fd1498Szrj       static const int used_block = __r;
1098*38fd1498Szrj 
1099*38fd1498Szrj       /**
1100*38fd1498Szrj        * Constructs a default %discard_block engine.
1101*38fd1498Szrj        *
1102*38fd1498Szrj        * The underlying engine is default constructed as well.
1103*38fd1498Szrj        */
1104*38fd1498Szrj       discard_block()
1105*38fd1498Szrj       : _M_n(0) { }
1106*38fd1498Szrj 
1107*38fd1498Szrj       /**
1108*38fd1498Szrj        * Copy constructs a %discard_block engine.
1109*38fd1498Szrj        *
1110*38fd1498Szrj        * Copies an existing base class random number generator.
1111*38fd1498Szrj        * @param rng An existing (base class) engine object.
1112*38fd1498Szrj        */
1113*38fd1498Szrj       explicit
1114*38fd1498Szrj       discard_block(const base_type& __rng)
1115*38fd1498Szrj       : _M_b(__rng), _M_n(0) { }
1116*38fd1498Szrj 
1117*38fd1498Szrj       /**
1118*38fd1498Szrj        * Seed constructs a %discard_block engine.
1119*38fd1498Szrj        *
1120*38fd1498Szrj        * Constructs the underlying generator engine seeded with @p __s.
1121*38fd1498Szrj        * @param __s A seed value for the base class engine.
1122*38fd1498Szrj        */
1123*38fd1498Szrj       explicit
1124*38fd1498Szrj       discard_block(unsigned long __s)
1125*38fd1498Szrj       : _M_b(__s), _M_n(0) { }
1126*38fd1498Szrj 
1127*38fd1498Szrj       /**
1128*38fd1498Szrj        * Generator construct a %discard_block engine.
1129*38fd1498Szrj        *
1130*38fd1498Szrj        * @param __g A seed generator function.
1131*38fd1498Szrj        */
1132*38fd1498Szrj       template<class _Gen>
1133*38fd1498Szrj         discard_block(_Gen& __g)
1134*38fd1498Szrj 	: _M_b(__g), _M_n(0) { }
1135*38fd1498Szrj 
1136*38fd1498Szrj       /**
1137*38fd1498Szrj        * Reseeds the %discard_block object with the default seed for the
1138*38fd1498Szrj        * underlying base class generator engine.
1139*38fd1498Szrj        */
1140*38fd1498Szrj       void seed()
1141*38fd1498Szrj       {
1142*38fd1498Szrj 	_M_b.seed();
1143*38fd1498Szrj 	_M_n = 0;
1144*38fd1498Szrj       }
1145*38fd1498Szrj 
1146*38fd1498Szrj       /**
1147*38fd1498Szrj        * Reseeds the %discard_block object with the given seed generator
1148*38fd1498Szrj        * function.
1149*38fd1498Szrj        * @param __g A seed generator function.
1150*38fd1498Szrj        */
1151*38fd1498Szrj       template<class _Gen>
1152*38fd1498Szrj         void seed(_Gen& __g)
1153*38fd1498Szrj         {
1154*38fd1498Szrj 	  _M_b.seed(__g);
1155*38fd1498Szrj 	  _M_n = 0;
1156*38fd1498Szrj 	}
1157*38fd1498Szrj 
1158*38fd1498Szrj       /**
1159*38fd1498Szrj        * Gets a const reference to the underlying generator engine object.
1160*38fd1498Szrj        */
1161*38fd1498Szrj       const base_type&
1162*38fd1498Szrj       base() const
1163*38fd1498Szrj       { return _M_b; }
1164*38fd1498Szrj 
1165*38fd1498Szrj       /**
1166*38fd1498Szrj        * Gets the minimum value in the generated random number range.
1167*38fd1498Szrj        */
1168*38fd1498Szrj       result_type
1169*38fd1498Szrj       min() const
1170*38fd1498Szrj       { return _M_b.min(); }
1171*38fd1498Szrj 
1172*38fd1498Szrj       /**
1173*38fd1498Szrj        * Gets the maximum value in the generated random number range.
1174*38fd1498Szrj        */
1175*38fd1498Szrj       result_type
1176*38fd1498Szrj       max() const
1177*38fd1498Szrj       { return _M_b.max(); }
1178*38fd1498Szrj 
1179*38fd1498Szrj       /**
1180*38fd1498Szrj        * Gets the next value in the generated random number sequence.
1181*38fd1498Szrj        */
1182*38fd1498Szrj       result_type
1183*38fd1498Szrj       operator()();
1184*38fd1498Szrj 
1185*38fd1498Szrj       /**
1186*38fd1498Szrj        * Compares two %discard_block random number generator objects of
1187*38fd1498Szrj        * the same type for equality.
1188*38fd1498Szrj        *
1189*38fd1498Szrj        * @param __lhs A %discard_block random number generator object.
1190*38fd1498Szrj        * @param __rhs Another %discard_block random number generator
1191*38fd1498Szrj        *              object.
1192*38fd1498Szrj        *
1193*38fd1498Szrj        * @returns true if the two objects are equal, false otherwise.
1194*38fd1498Szrj        */
1195*38fd1498Szrj       friend bool
1196*38fd1498Szrj       operator==(const discard_block& __lhs, const discard_block& __rhs)
1197*38fd1498Szrj       { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
1198*38fd1498Szrj 
1199*38fd1498Szrj       /**
1200*38fd1498Szrj        * Compares two %discard_block random number generator objects of
1201*38fd1498Szrj        * the same type for inequality.
1202*38fd1498Szrj        *
1203*38fd1498Szrj        * @param __lhs A %discard_block random number generator object.
1204*38fd1498Szrj        * @param __rhs Another %discard_block random number generator
1205*38fd1498Szrj        *              object.
1206*38fd1498Szrj        *
1207*38fd1498Szrj        * @returns true if the two objects are not equal, false otherwise.
1208*38fd1498Szrj        */
1209*38fd1498Szrj       friend bool
1210*38fd1498Szrj       operator!=(const discard_block& __lhs, const discard_block& __rhs)
1211*38fd1498Szrj       { return !(__lhs == __rhs); }
1212*38fd1498Szrj 
1213*38fd1498Szrj       /**
1214*38fd1498Szrj        * Inserts the current state of a %discard_block random number
1215*38fd1498Szrj        * generator engine @p __x into the output stream @p __os.
1216*38fd1498Szrj        *
1217*38fd1498Szrj        * @param __os An output stream.
1218*38fd1498Szrj        * @param __x  A %discard_block random number generator engine.
1219*38fd1498Szrj        *
1220*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
1221*38fd1498Szrj        * an error state.
1222*38fd1498Szrj        */
1223*38fd1498Szrj       template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1224*38fd1498Szrj 	       typename _CharT, typename _Traits>
1225*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
1226*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1227*38fd1498Szrj 		   const discard_block<_UniformRandomNumberGenerator1,
1228*38fd1498Szrj 		   __p1, __r1>& __x);
1229*38fd1498Szrj 
1230*38fd1498Szrj       /**
1231*38fd1498Szrj        * Extracts the current state of a % subtract_with_carry random number
1232*38fd1498Szrj        * generator engine @p __x from the input stream @p __is.
1233*38fd1498Szrj        *
1234*38fd1498Szrj        * @param __is An input stream.
1235*38fd1498Szrj        * @param __x  A %discard_block random number generator engine.
1236*38fd1498Szrj        *
1237*38fd1498Szrj        * @returns The input stream with the state of @p __x extracted or in
1238*38fd1498Szrj        * an error state.
1239*38fd1498Szrj        */
1240*38fd1498Szrj       template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1241*38fd1498Szrj 	       typename _CharT, typename _Traits>
1242*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
1243*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1244*38fd1498Szrj 		   discard_block<_UniformRandomNumberGenerator1,
1245*38fd1498Szrj 		   __p1, __r1>& __x);
1246*38fd1498Szrj 
1247*38fd1498Szrj     private:
1248*38fd1498Szrj       base_type _M_b;
1249*38fd1498Szrj       int       _M_n;
1250*38fd1498Szrj     };
1251*38fd1498Szrj 
1252*38fd1498Szrj 
1253*38fd1498Szrj   /**
1254*38fd1498Szrj    * James's luxury-level-3 integer adaptation of Luescher's generator.
1255*38fd1498Szrj    */
1256*38fd1498Szrj   typedef discard_block<
1257*38fd1498Szrj     subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1258*38fd1498Szrj       223,
1259*38fd1498Szrj       24
1260*38fd1498Szrj       > ranlux3;
1261*38fd1498Szrj 
1262*38fd1498Szrj   /**
1263*38fd1498Szrj    * James's luxury-level-4 integer adaptation of Luescher's generator.
1264*38fd1498Szrj    */
1265*38fd1498Szrj   typedef discard_block<
1266*38fd1498Szrj     subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1267*38fd1498Szrj       389,
1268*38fd1498Szrj       24
1269*38fd1498Szrj       > ranlux4;
1270*38fd1498Szrj 
1271*38fd1498Szrj   typedef discard_block<
1272*38fd1498Szrj     subtract_with_carry_01<float, 24, 10, 24>,
1273*38fd1498Szrj       223,
1274*38fd1498Szrj       24
1275*38fd1498Szrj       > ranlux3_01;
1276*38fd1498Szrj 
1277*38fd1498Szrj   typedef discard_block<
1278*38fd1498Szrj     subtract_with_carry_01<float, 24, 10, 24>,
1279*38fd1498Szrj       389,
1280*38fd1498Szrj       24
1281*38fd1498Szrj       > ranlux4_01;
1282*38fd1498Szrj 
1283*38fd1498Szrj 
1284*38fd1498Szrj   /**
1285*38fd1498Szrj    * A random number generator adaptor class that combines two random number
1286*38fd1498Szrj    * generator engines into a single output sequence.
1287*38fd1498Szrj    */
1288*38fd1498Szrj   template<class _UniformRandomNumberGenerator1, int __s1,
1289*38fd1498Szrj 	   class _UniformRandomNumberGenerator2, int __s2>
1290*38fd1498Szrj     class xor_combine
1291*38fd1498Szrj     {
1292*38fd1498Szrj       // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1293*38fd1498Szrj       //                          result_type, ArithmeticTypeConcept)
1294*38fd1498Szrj       // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1295*38fd1498Szrj       //                          result_type, ArithmeticTypeConcept)
1296*38fd1498Szrj 
1297*38fd1498Szrj     public:
1298*38fd1498Szrj       /** The type of the first underlying generator engine. */
1299*38fd1498Szrj       typedef _UniformRandomNumberGenerator1   base1_type;
1300*38fd1498Szrj       /** The type of the second underlying generator engine. */
1301*38fd1498Szrj       typedef _UniformRandomNumberGenerator2   base2_type;
1302*38fd1498Szrj 
1303*38fd1498Szrj     private:
1304*38fd1498Szrj       typedef typename base1_type::result_type _Result_type1;
1305*38fd1498Szrj       typedef typename base2_type::result_type _Result_type2;
1306*38fd1498Szrj 
1307*38fd1498Szrj     public:
1308*38fd1498Szrj       /** The type of the generated random value. */
1309*38fd1498Szrj       typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1)
1310*38fd1498Szrj 						      > sizeof(_Result_type2)),
1311*38fd1498Szrj 	_Result_type1, _Result_type2>::__type result_type;
1312*38fd1498Szrj 
1313*38fd1498Szrj       // parameter values
1314*38fd1498Szrj       static const int shift1 = __s1;
1315*38fd1498Szrj       static const int shift2 = __s2;
1316*38fd1498Szrj 
1317*38fd1498Szrj       // constructors and member function
1318*38fd1498Szrj       xor_combine()
1319*38fd1498Szrj       : _M_b1(), _M_b2()
1320*38fd1498Szrj       { _M_initialize_max(); }
1321*38fd1498Szrj 
1322*38fd1498Szrj       xor_combine(const base1_type& __rng1, const base2_type& __rng2)
1323*38fd1498Szrj       : _M_b1(__rng1), _M_b2(__rng2)
1324*38fd1498Szrj       { _M_initialize_max(); }
1325*38fd1498Szrj 
1326*38fd1498Szrj       xor_combine(unsigned long __s)
1327*38fd1498Szrj       : _M_b1(__s), _M_b2(__s + 1)
1328*38fd1498Szrj       { _M_initialize_max(); }
1329*38fd1498Szrj 
1330*38fd1498Szrj       template<class _Gen>
1331*38fd1498Szrj         xor_combine(_Gen& __g)
1332*38fd1498Szrj 	: _M_b1(__g), _M_b2(__g)
1333*38fd1498Szrj         { _M_initialize_max(); }
1334*38fd1498Szrj 
1335*38fd1498Szrj       void
1336*38fd1498Szrj       seed()
1337*38fd1498Szrj       {
1338*38fd1498Szrj 	_M_b1.seed();
1339*38fd1498Szrj 	_M_b2.seed();
1340*38fd1498Szrj       }
1341*38fd1498Szrj 
1342*38fd1498Szrj       template<class _Gen>
1343*38fd1498Szrj         void
1344*38fd1498Szrj         seed(_Gen& __g)
1345*38fd1498Szrj         {
1346*38fd1498Szrj 	  _M_b1.seed(__g);
1347*38fd1498Szrj 	  _M_b2.seed(__g);
1348*38fd1498Szrj 	}
1349*38fd1498Szrj 
1350*38fd1498Szrj       const base1_type&
1351*38fd1498Szrj       base1() const
1352*38fd1498Szrj       { return _M_b1; }
1353*38fd1498Szrj 
1354*38fd1498Szrj       const base2_type&
1355*38fd1498Szrj       base2() const
1356*38fd1498Szrj       { return _M_b2; }
1357*38fd1498Szrj 
1358*38fd1498Szrj       result_type
1359*38fd1498Szrj       min() const
1360*38fd1498Szrj       { return 0; }
1361*38fd1498Szrj 
1362*38fd1498Szrj       result_type
1363*38fd1498Szrj       max() const
1364*38fd1498Szrj       { return _M_max; }
1365*38fd1498Szrj 
1366*38fd1498Szrj       /**
1367*38fd1498Szrj        * Gets the next random number in the sequence.
1368*38fd1498Szrj        */
1369*38fd1498Szrj       // NB: Not exactly the TR1 formula, per N2079 instead.
1370*38fd1498Szrj       result_type
1371*38fd1498Szrj       operator()()
1372*38fd1498Szrj       {
1373*38fd1498Szrj 	return ((result_type(_M_b1() - _M_b1.min()) << shift1)
1374*38fd1498Szrj 		^ (result_type(_M_b2() - _M_b2.min()) << shift2));
1375*38fd1498Szrj       }
1376*38fd1498Szrj 
1377*38fd1498Szrj       /**
1378*38fd1498Szrj        * Compares two %xor_combine random number generator objects of
1379*38fd1498Szrj        * the same type for equality.
1380*38fd1498Szrj        *
1381*38fd1498Szrj        * @param __lhs A %xor_combine random number generator object.
1382*38fd1498Szrj        * @param __rhs Another %xor_combine random number generator
1383*38fd1498Szrj        *              object.
1384*38fd1498Szrj        *
1385*38fd1498Szrj        * @returns true if the two objects are equal, false otherwise.
1386*38fd1498Szrj        */
1387*38fd1498Szrj       friend bool
1388*38fd1498Szrj       operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1389*38fd1498Szrj       {
1390*38fd1498Szrj 	return (__lhs.base1() == __rhs.base1())
1391*38fd1498Szrj 	        && (__lhs.base2() == __rhs.base2());
1392*38fd1498Szrj       }
1393*38fd1498Szrj 
1394*38fd1498Szrj       /**
1395*38fd1498Szrj        * Compares two %xor_combine random number generator objects of
1396*38fd1498Szrj        * the same type for inequality.
1397*38fd1498Szrj        *
1398*38fd1498Szrj        * @param __lhs A %xor_combine random number generator object.
1399*38fd1498Szrj        * @param __rhs Another %xor_combine random number generator
1400*38fd1498Szrj        *              object.
1401*38fd1498Szrj        *
1402*38fd1498Szrj        * @returns true if the two objects are not equal, false otherwise.
1403*38fd1498Szrj        */
1404*38fd1498Szrj       friend bool
1405*38fd1498Szrj       operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1406*38fd1498Szrj       { return !(__lhs == __rhs); }
1407*38fd1498Szrj 
1408*38fd1498Szrj       /**
1409*38fd1498Szrj        * Inserts the current state of a %xor_combine random number
1410*38fd1498Szrj        * generator engine @p __x into the output stream @p __os.
1411*38fd1498Szrj        *
1412*38fd1498Szrj        * @param __os An output stream.
1413*38fd1498Szrj        * @param __x  A %xor_combine random number generator engine.
1414*38fd1498Szrj        *
1415*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
1416*38fd1498Szrj        * an error state.
1417*38fd1498Szrj        */
1418*38fd1498Szrj       template<class _UniformRandomNumberGenerator11, int __s11,
1419*38fd1498Szrj 	       class _UniformRandomNumberGenerator21, int __s21,
1420*38fd1498Szrj 	       typename _CharT, typename _Traits>
1421*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
1422*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1423*38fd1498Szrj 		   const xor_combine<_UniformRandomNumberGenerator11, __s11,
1424*38fd1498Szrj 		   _UniformRandomNumberGenerator21, __s21>& __x);
1425*38fd1498Szrj 
1426*38fd1498Szrj       /**
1427*38fd1498Szrj        * Extracts the current state of a %xor_combine random number
1428*38fd1498Szrj        * generator engine @p __x from the input stream @p __is.
1429*38fd1498Szrj        *
1430*38fd1498Szrj        * @param __is An input stream.
1431*38fd1498Szrj        * @param __x  A %xor_combine random number generator engine.
1432*38fd1498Szrj        *
1433*38fd1498Szrj        * @returns The input stream with the state of @p __x extracted or in
1434*38fd1498Szrj        * an error state.
1435*38fd1498Szrj        */
1436*38fd1498Szrj       template<class _UniformRandomNumberGenerator11, int __s11,
1437*38fd1498Szrj 	       class _UniformRandomNumberGenerator21, int __s21,
1438*38fd1498Szrj 	       typename _CharT, typename _Traits>
1439*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
1440*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1441*38fd1498Szrj 		   xor_combine<_UniformRandomNumberGenerator11, __s11,
1442*38fd1498Szrj 		   _UniformRandomNumberGenerator21, __s21>& __x);
1443*38fd1498Szrj 
1444*38fd1498Szrj     private:
1445*38fd1498Szrj       void
1446*38fd1498Szrj       _M_initialize_max();
1447*38fd1498Szrj 
1448*38fd1498Szrj       result_type
1449*38fd1498Szrj       _M_initialize_max_aux(result_type, result_type, int);
1450*38fd1498Szrj 
1451*38fd1498Szrj       base1_type  _M_b1;
1452*38fd1498Szrj       base2_type  _M_b2;
1453*38fd1498Szrj       result_type _M_max;
1454*38fd1498Szrj     };
1455*38fd1498Szrj 
1456*38fd1498Szrj 
1457*38fd1498Szrj   /**
1458*38fd1498Szrj    * A standard interface to a platform-specific non-deterministic
1459*38fd1498Szrj    * random number generator (if any are available).
1460*38fd1498Szrj    */
1461*38fd1498Szrj   class random_device
1462*38fd1498Szrj   {
1463*38fd1498Szrj   public:
1464*38fd1498Szrj     // types
1465*38fd1498Szrj     typedef unsigned int result_type;
1466*38fd1498Szrj 
1467*38fd1498Szrj     // constructors, destructors and member functions
1468*38fd1498Szrj 
1469*38fd1498Szrj #ifdef _GLIBCXX_USE_RANDOM_TR1
1470*38fd1498Szrj 
1471*38fd1498Szrj     explicit
1472*38fd1498Szrj     random_device(const std::string& __token = "/dev/urandom")
1473*38fd1498Szrj     {
1474*38fd1498Szrj       if ((__token != "/dev/urandom" && __token != "/dev/random")
1475*38fd1498Szrj 	  || !(_M_file = std::fopen(__token.c_str(), "rb")))
1476*38fd1498Szrj 	std::__throw_runtime_error(__N("random_device::"
1477*38fd1498Szrj 				       "random_device(const std::string&)"));
1478*38fd1498Szrj     }
1479*38fd1498Szrj 
1480*38fd1498Szrj     ~random_device()
1481*38fd1498Szrj     { std::fclose(_M_file); }
1482*38fd1498Szrj 
1483*38fd1498Szrj #else
1484*38fd1498Szrj 
1485*38fd1498Szrj     explicit
1486*38fd1498Szrj     random_device(const std::string& __token = "mt19937")
1487*38fd1498Szrj     : _M_mt(_M_strtoul(__token)) { }
1488*38fd1498Szrj 
1489*38fd1498Szrj   private:
1490*38fd1498Szrj     static unsigned long
1491*38fd1498Szrj     _M_strtoul(const std::string& __str)
1492*38fd1498Szrj     {
1493*38fd1498Szrj       unsigned long __ret = 5489UL;
1494*38fd1498Szrj       if (__str != "mt19937")
1495*38fd1498Szrj 	{
1496*38fd1498Szrj 	  const char* __nptr = __str.c_str();
1497*38fd1498Szrj 	  char* __endptr;
1498*38fd1498Szrj 	  __ret = std::strtoul(__nptr, &__endptr, 0);
1499*38fd1498Szrj 	  if (*__nptr == '\0' || *__endptr != '\0')
1500*38fd1498Szrj 	    std::__throw_runtime_error(__N("random_device::_M_strtoul"
1501*38fd1498Szrj 					   "(const std::string&)"));
1502*38fd1498Szrj 	}
1503*38fd1498Szrj       return __ret;
1504*38fd1498Szrj     }
1505*38fd1498Szrj 
1506*38fd1498Szrj   public:
1507*38fd1498Szrj 
1508*38fd1498Szrj #endif
1509*38fd1498Szrj 
1510*38fd1498Szrj     result_type
1511*38fd1498Szrj     min() const
1512*38fd1498Szrj     { return std::numeric_limits<result_type>::min(); }
1513*38fd1498Szrj 
1514*38fd1498Szrj     result_type
1515*38fd1498Szrj     max() const
1516*38fd1498Szrj     { return std::numeric_limits<result_type>::max(); }
1517*38fd1498Szrj 
1518*38fd1498Szrj     double
1519*38fd1498Szrj     entropy() const
1520*38fd1498Szrj     { return 0.0; }
1521*38fd1498Szrj 
1522*38fd1498Szrj     result_type
1523*38fd1498Szrj     operator()()
1524*38fd1498Szrj     {
1525*38fd1498Szrj #ifdef _GLIBCXX_USE_RANDOM_TR1
1526*38fd1498Szrj       result_type __ret;
1527*38fd1498Szrj       std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1528*38fd1498Szrj 		 1, _M_file);
1529*38fd1498Szrj       return __ret;
1530*38fd1498Szrj #else
1531*38fd1498Szrj       return _M_mt();
1532*38fd1498Szrj #endif
1533*38fd1498Szrj     }
1534*38fd1498Szrj 
1535*38fd1498Szrj   private:
1536*38fd1498Szrj     random_device(const random_device&);
1537*38fd1498Szrj     void operator=(const random_device&);
1538*38fd1498Szrj 
1539*38fd1498Szrj #ifdef _GLIBCXX_USE_RANDOM_TR1
1540*38fd1498Szrj     FILE*        _M_file;
1541*38fd1498Szrj #else
1542*38fd1498Szrj     mt19937      _M_mt;
1543*38fd1498Szrj #endif
1544*38fd1498Szrj   };
1545*38fd1498Szrj 
1546*38fd1498Szrj   /* @} */ // group tr1_random_generators
1547*38fd1498Szrj 
1548*38fd1498Szrj   /**
1549*38fd1498Szrj    * @addtogroup tr1_random_distributions Random Number Distributions
1550*38fd1498Szrj    * @ingroup tr1_random
1551*38fd1498Szrj    * @{
1552*38fd1498Szrj    */
1553*38fd1498Szrj 
1554*38fd1498Szrj   /**
1555*38fd1498Szrj    * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1556*38fd1498Szrj    * @ingroup tr1_random_distributions
1557*38fd1498Szrj    * @{
1558*38fd1498Szrj    */
1559*38fd1498Szrj 
1560*38fd1498Szrj   /**
1561*38fd1498Szrj    * @brief Uniform discrete distribution for random numbers.
1562*38fd1498Szrj    * A discrete random distribution on the range @f$[min, max]@f$ with equal
1563*38fd1498Szrj    * probability throughout the range.
1564*38fd1498Szrj    */
1565*38fd1498Szrj   template<typename _IntType = int>
1566*38fd1498Szrj     class uniform_int
1567*38fd1498Szrj     {
1568*38fd1498Szrj       __glibcxx_class_requires(_IntType, _IntegerConcept)
1569*38fd1498Szrj 
1570*38fd1498Szrj     public:
1571*38fd1498Szrj       /** The type of the parameters of the distribution. */
1572*38fd1498Szrj       typedef _IntType input_type;
1573*38fd1498Szrj       /** The type of the range of the distribution. */
1574*38fd1498Szrj       typedef _IntType result_type;
1575*38fd1498Szrj 
1576*38fd1498Szrj     public:
1577*38fd1498Szrj       /**
1578*38fd1498Szrj        * Constructs a uniform distribution object.
1579*38fd1498Szrj        */
1580*38fd1498Szrj       explicit
1581*38fd1498Szrj       uniform_int(_IntType __min = 0, _IntType __max = 9)
1582*38fd1498Szrj       : _M_min(__min), _M_max(__max)
1583*38fd1498Szrj       {
1584*38fd1498Szrj 	_GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1585*38fd1498Szrj       }
1586*38fd1498Szrj 
1587*38fd1498Szrj       /**
1588*38fd1498Szrj        * Gets the inclusive lower bound of the distribution range.
1589*38fd1498Szrj        */
1590*38fd1498Szrj       result_type
1591*38fd1498Szrj       min() const
1592*38fd1498Szrj       { return _M_min; }
1593*38fd1498Szrj 
1594*38fd1498Szrj       /**
1595*38fd1498Szrj        * Gets the inclusive upper bound of the distribution range.
1596*38fd1498Szrj        */
1597*38fd1498Szrj       result_type
1598*38fd1498Szrj       max() const
1599*38fd1498Szrj       { return _M_max; }
1600*38fd1498Szrj 
1601*38fd1498Szrj       /**
1602*38fd1498Szrj        * Resets the distribution state.
1603*38fd1498Szrj        *
1604*38fd1498Szrj        * Does nothing for the uniform integer distribution.
1605*38fd1498Szrj        */
1606*38fd1498Szrj       void
1607*38fd1498Szrj       reset() { }
1608*38fd1498Szrj 
1609*38fd1498Szrj       /**
1610*38fd1498Szrj        * Gets a uniformly distributed random number in the range
1611*38fd1498Szrj        * @f$(min, max)@f$.
1612*38fd1498Szrj        */
1613*38fd1498Szrj       template<typename _UniformRandomNumberGenerator>
1614*38fd1498Szrj         result_type
1615*38fd1498Szrj         operator()(_UniformRandomNumberGenerator& __urng)
1616*38fd1498Szrj         {
1617*38fd1498Szrj 	  typedef typename _UniformRandomNumberGenerator::result_type
1618*38fd1498Szrj 	    _UResult_type;
1619*38fd1498Szrj 	  return _M_call(__urng, _M_min, _M_max,
1620*38fd1498Szrj 			 typename is_integral<_UResult_type>::type());
1621*38fd1498Szrj 	}
1622*38fd1498Szrj 
1623*38fd1498Szrj       /**
1624*38fd1498Szrj        * Gets a uniform random number in the range @f$[0, n)@f$.
1625*38fd1498Szrj        *
1626*38fd1498Szrj        * This function is aimed at use with std::random_shuffle.
1627*38fd1498Szrj        */
1628*38fd1498Szrj       template<typename _UniformRandomNumberGenerator>
1629*38fd1498Szrj         result_type
1630*38fd1498Szrj         operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1631*38fd1498Szrj         {
1632*38fd1498Szrj 	  typedef typename _UniformRandomNumberGenerator::result_type
1633*38fd1498Szrj 	    _UResult_type;
1634*38fd1498Szrj 	  return _M_call(__urng, 0, __n - 1,
1635*38fd1498Szrj 			 typename is_integral<_UResult_type>::type());
1636*38fd1498Szrj 	}
1637*38fd1498Szrj 
1638*38fd1498Szrj       /**
1639*38fd1498Szrj        * Inserts a %uniform_int random number distribution @p __x into the
1640*38fd1498Szrj        * output stream @p os.
1641*38fd1498Szrj        *
1642*38fd1498Szrj        * @param __os An output stream.
1643*38fd1498Szrj        * @param __x  A %uniform_int random number distribution.
1644*38fd1498Szrj        *
1645*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
1646*38fd1498Szrj        * an error state.
1647*38fd1498Szrj        */
1648*38fd1498Szrj       template<typename _IntType1, typename _CharT, typename _Traits>
1649*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
1650*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1651*38fd1498Szrj 		   const uniform_int<_IntType1>& __x);
1652*38fd1498Szrj 
1653*38fd1498Szrj       /**
1654*38fd1498Szrj        * Extracts a %uniform_int random number distribution
1655*38fd1498Szrj        * @p __x from the input stream @p __is.
1656*38fd1498Szrj        *
1657*38fd1498Szrj        * @param __is An input stream.
1658*38fd1498Szrj        * @param __x  A %uniform_int random number generator engine.
1659*38fd1498Szrj        *
1660*38fd1498Szrj        * @returns The input stream with @p __x extracted or in an error state.
1661*38fd1498Szrj        */
1662*38fd1498Szrj       template<typename _IntType1, typename _CharT, typename _Traits>
1663*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
1664*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1665*38fd1498Szrj 		   uniform_int<_IntType1>& __x);
1666*38fd1498Szrj 
1667*38fd1498Szrj     private:
1668*38fd1498Szrj       template<typename _UniformRandomNumberGenerator>
1669*38fd1498Szrj         result_type
1670*38fd1498Szrj         _M_call(_UniformRandomNumberGenerator& __urng,
1671*38fd1498Szrj 		result_type __min, result_type __max, true_type);
1672*38fd1498Szrj 
1673*38fd1498Szrj       template<typename _UniformRandomNumberGenerator>
1674*38fd1498Szrj         result_type
1675*38fd1498Szrj         _M_call(_UniformRandomNumberGenerator& __urng,
1676*38fd1498Szrj 		result_type __min, result_type __max, false_type)
1677*38fd1498Szrj         {
1678*38fd1498Szrj 	  return result_type((__urng() - __urng.min())
1679*38fd1498Szrj 			     / (__urng.max() - __urng.min())
1680*38fd1498Szrj 			     * (__max - __min + 1)) + __min;
1681*38fd1498Szrj 	}
1682*38fd1498Szrj 
1683*38fd1498Szrj       _IntType _M_min;
1684*38fd1498Szrj       _IntType _M_max;
1685*38fd1498Szrj     };
1686*38fd1498Szrj 
1687*38fd1498Szrj 
1688*38fd1498Szrj   /**
1689*38fd1498Szrj    * @brief A Bernoulli random number distribution.
1690*38fd1498Szrj    *
1691*38fd1498Szrj    * Generates a sequence of true and false values with likelihood @f$ p @f$
1692*38fd1498Szrj    * that true will come up and @f$ (1 - p) @f$ that false will appear.
1693*38fd1498Szrj    */
1694*38fd1498Szrj   class bernoulli_distribution
1695*38fd1498Szrj   {
1696*38fd1498Szrj   public:
1697*38fd1498Szrj     typedef int  input_type;
1698*38fd1498Szrj     typedef bool result_type;
1699*38fd1498Szrj 
1700*38fd1498Szrj   public:
1701*38fd1498Szrj     /**
1702*38fd1498Szrj      * Constructs a Bernoulli distribution with likelihood @p p.
1703*38fd1498Szrj      *
1704*38fd1498Szrj      * @param __p  [IN]  The likelihood of a true result being returned.  Must
1705*38fd1498Szrj      * be in the interval @f$ [0, 1] @f$.
1706*38fd1498Szrj      */
1707*38fd1498Szrj     explicit
1708*38fd1498Szrj     bernoulli_distribution(double __p = 0.5)
1709*38fd1498Szrj     : _M_p(__p)
1710*38fd1498Szrj     {
1711*38fd1498Szrj       _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1712*38fd1498Szrj     }
1713*38fd1498Szrj 
1714*38fd1498Szrj     /**
1715*38fd1498Szrj      * Gets the @p p parameter of the distribution.
1716*38fd1498Szrj      */
1717*38fd1498Szrj     double
1718*38fd1498Szrj     p() const
1719*38fd1498Szrj     { return _M_p; }
1720*38fd1498Szrj 
1721*38fd1498Szrj     /**
1722*38fd1498Szrj      * Resets the distribution state.
1723*38fd1498Szrj      *
1724*38fd1498Szrj      * Does nothing for a Bernoulli distribution.
1725*38fd1498Szrj      */
1726*38fd1498Szrj     void
1727*38fd1498Szrj     reset() { }
1728*38fd1498Szrj 
1729*38fd1498Szrj     /**
1730*38fd1498Szrj      * Gets the next value in the Bernoullian sequence.
1731*38fd1498Szrj      */
1732*38fd1498Szrj     template<class _UniformRandomNumberGenerator>
1733*38fd1498Szrj       result_type
1734*38fd1498Szrj       operator()(_UniformRandomNumberGenerator& __urng)
1735*38fd1498Szrj       {
1736*38fd1498Szrj 	if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
1737*38fd1498Szrj 	  return true;
1738*38fd1498Szrj 	return false;
1739*38fd1498Szrj       }
1740*38fd1498Szrj 
1741*38fd1498Szrj     /**
1742*38fd1498Szrj      * Inserts a %bernoulli_distribution random number distribution
1743*38fd1498Szrj      * @p __x into the output stream @p __os.
1744*38fd1498Szrj      *
1745*38fd1498Szrj      * @param __os An output stream.
1746*38fd1498Szrj      * @param __x  A %bernoulli_distribution random number distribution.
1747*38fd1498Szrj      *
1748*38fd1498Szrj      * @returns The output stream with the state of @p __x inserted or in
1749*38fd1498Szrj      * an error state.
1750*38fd1498Szrj      */
1751*38fd1498Szrj     template<typename _CharT, typename _Traits>
1752*38fd1498Szrj       friend std::basic_ostream<_CharT, _Traits>&
1753*38fd1498Szrj       operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1754*38fd1498Szrj 		 const bernoulli_distribution& __x);
1755*38fd1498Szrj 
1756*38fd1498Szrj     /**
1757*38fd1498Szrj      * Extracts a %bernoulli_distribution random number distribution
1758*38fd1498Szrj      * @p __x from the input stream @p __is.
1759*38fd1498Szrj      *
1760*38fd1498Szrj      * @param __is An input stream.
1761*38fd1498Szrj      * @param __x  A %bernoulli_distribution random number generator engine.
1762*38fd1498Szrj      *
1763*38fd1498Szrj      * @returns The input stream with @p __x extracted or in an error state.
1764*38fd1498Szrj      */
1765*38fd1498Szrj     template<typename _CharT, typename _Traits>
1766*38fd1498Szrj       friend std::basic_istream<_CharT, _Traits>&
1767*38fd1498Szrj       operator>>(std::basic_istream<_CharT, _Traits>& __is,
1768*38fd1498Szrj 		 bernoulli_distribution& __x)
1769*38fd1498Szrj       { return __is >> __x._M_p; }
1770*38fd1498Szrj 
1771*38fd1498Szrj   private:
1772*38fd1498Szrj     double _M_p;
1773*38fd1498Szrj   };
1774*38fd1498Szrj 
1775*38fd1498Szrj 
1776*38fd1498Szrj   /**
1777*38fd1498Szrj    * @brief A discrete geometric random number distribution.
1778*38fd1498Szrj    *
1779*38fd1498Szrj    * The formula for the geometric probability mass function is
1780*38fd1498Szrj    * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1781*38fd1498Szrj    * distribution.
1782*38fd1498Szrj    */
1783*38fd1498Szrj   template<typename _IntType = int, typename _RealType = double>
1784*38fd1498Szrj     class geometric_distribution
1785*38fd1498Szrj     {
1786*38fd1498Szrj     public:
1787*38fd1498Szrj       // types
1788*38fd1498Szrj       typedef _RealType input_type;
1789*38fd1498Szrj       typedef _IntType  result_type;
1790*38fd1498Szrj 
1791*38fd1498Szrj       // constructors and member function
1792*38fd1498Szrj       explicit
1793*38fd1498Szrj       geometric_distribution(const _RealType& __p = _RealType(0.5))
1794*38fd1498Szrj       : _M_p(__p)
1795*38fd1498Szrj       {
1796*38fd1498Szrj 	_GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
1797*38fd1498Szrj 	_M_initialize();
1798*38fd1498Szrj       }
1799*38fd1498Szrj 
1800*38fd1498Szrj       /**
1801*38fd1498Szrj        * Gets the distribution parameter @p p.
1802*38fd1498Szrj        */
1803*38fd1498Szrj       _RealType
1804*38fd1498Szrj       p() const
1805*38fd1498Szrj       { return _M_p; }
1806*38fd1498Szrj 
1807*38fd1498Szrj       void
1808*38fd1498Szrj       reset() { }
1809*38fd1498Szrj 
1810*38fd1498Szrj       template<class _UniformRandomNumberGenerator>
1811*38fd1498Szrj         result_type
1812*38fd1498Szrj         operator()(_UniformRandomNumberGenerator& __urng);
1813*38fd1498Szrj 
1814*38fd1498Szrj       /**
1815*38fd1498Szrj        * Inserts a %geometric_distribution random number distribution
1816*38fd1498Szrj        * @p __x into the output stream @p __os.
1817*38fd1498Szrj        *
1818*38fd1498Szrj        * @param __os An output stream.
1819*38fd1498Szrj        * @param __x  A %geometric_distribution random number distribution.
1820*38fd1498Szrj        *
1821*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
1822*38fd1498Szrj        * an error state.
1823*38fd1498Szrj        */
1824*38fd1498Szrj       template<typename _IntType1, typename _RealType1,
1825*38fd1498Szrj 	       typename _CharT, typename _Traits>
1826*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
1827*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1828*38fd1498Szrj 		   const geometric_distribution<_IntType1, _RealType1>& __x);
1829*38fd1498Szrj 
1830*38fd1498Szrj       /**
1831*38fd1498Szrj        * Extracts a %geometric_distribution random number distribution
1832*38fd1498Szrj        * @p __x from the input stream @p __is.
1833*38fd1498Szrj        *
1834*38fd1498Szrj        * @param __is An input stream.
1835*38fd1498Szrj        * @param __x  A %geometric_distribution random number generator engine.
1836*38fd1498Szrj        *
1837*38fd1498Szrj        * @returns The input stream with @p __x extracted or in an error state.
1838*38fd1498Szrj        */
1839*38fd1498Szrj       template<typename _CharT, typename _Traits>
1840*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
1841*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1842*38fd1498Szrj 		   geometric_distribution& __x)
1843*38fd1498Szrj         {
1844*38fd1498Szrj 	  __is >> __x._M_p;
1845*38fd1498Szrj 	  __x._M_initialize();
1846*38fd1498Szrj 	  return __is;
1847*38fd1498Szrj 	}
1848*38fd1498Szrj 
1849*38fd1498Szrj     private:
1850*38fd1498Szrj       void
1851*38fd1498Szrj       _M_initialize()
1852*38fd1498Szrj       { _M_log_p = std::log(_M_p); }
1853*38fd1498Szrj 
1854*38fd1498Szrj       _RealType _M_p;
1855*38fd1498Szrj       _RealType _M_log_p;
1856*38fd1498Szrj     };
1857*38fd1498Szrj 
1858*38fd1498Szrj 
1859*38fd1498Szrj   template<typename _RealType>
1860*38fd1498Szrj     class normal_distribution;
1861*38fd1498Szrj 
1862*38fd1498Szrj   /**
1863*38fd1498Szrj    * @brief A discrete Poisson random number distribution.
1864*38fd1498Szrj    *
1865*38fd1498Szrj    * The formula for the Poisson probability mass function is
1866*38fd1498Szrj    * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1867*38fd1498Szrj    * parameter of the distribution.
1868*38fd1498Szrj    */
1869*38fd1498Szrj   template<typename _IntType = int, typename _RealType = double>
1870*38fd1498Szrj     class poisson_distribution
1871*38fd1498Szrj     {
1872*38fd1498Szrj     public:
1873*38fd1498Szrj       // types
1874*38fd1498Szrj       typedef _RealType input_type;
1875*38fd1498Szrj       typedef _IntType  result_type;
1876*38fd1498Szrj 
1877*38fd1498Szrj       // constructors and member function
1878*38fd1498Szrj       explicit
1879*38fd1498Szrj       poisson_distribution(const _RealType& __mean = _RealType(1))
1880*38fd1498Szrj       : _M_mean(__mean), _M_nd()
1881*38fd1498Szrj       {
1882*38fd1498Szrj 	_GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
1883*38fd1498Szrj 	_M_initialize();
1884*38fd1498Szrj       }
1885*38fd1498Szrj 
1886*38fd1498Szrj       /**
1887*38fd1498Szrj        * Gets the distribution parameter @p mean.
1888*38fd1498Szrj        */
1889*38fd1498Szrj       _RealType
1890*38fd1498Szrj       mean() const
1891*38fd1498Szrj       { return _M_mean; }
1892*38fd1498Szrj 
1893*38fd1498Szrj       void
1894*38fd1498Szrj       reset()
1895*38fd1498Szrj       { _M_nd.reset(); }
1896*38fd1498Szrj 
1897*38fd1498Szrj       template<class _UniformRandomNumberGenerator>
1898*38fd1498Szrj         result_type
1899*38fd1498Szrj         operator()(_UniformRandomNumberGenerator& __urng);
1900*38fd1498Szrj 
1901*38fd1498Szrj       /**
1902*38fd1498Szrj        * Inserts a %poisson_distribution random number distribution
1903*38fd1498Szrj        * @p __x into the output stream @p __os.
1904*38fd1498Szrj        *
1905*38fd1498Szrj        * @param __os An output stream.
1906*38fd1498Szrj        * @param __x  A %poisson_distribution random number distribution.
1907*38fd1498Szrj        *
1908*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
1909*38fd1498Szrj        * an error state.
1910*38fd1498Szrj        */
1911*38fd1498Szrj       template<typename _IntType1, typename _RealType1,
1912*38fd1498Szrj 	       typename _CharT, typename _Traits>
1913*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
1914*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1915*38fd1498Szrj 		   const poisson_distribution<_IntType1, _RealType1>& __x);
1916*38fd1498Szrj 
1917*38fd1498Szrj       /**
1918*38fd1498Szrj        * Extracts a %poisson_distribution random number distribution
1919*38fd1498Szrj        * @p __x from the input stream @p __is.
1920*38fd1498Szrj        *
1921*38fd1498Szrj        * @param __is An input stream.
1922*38fd1498Szrj        * @param __x  A %poisson_distribution random number generator engine.
1923*38fd1498Szrj        *
1924*38fd1498Szrj        * @returns The input stream with @p __x extracted or in an error state.
1925*38fd1498Szrj        */
1926*38fd1498Szrj       template<typename _IntType1, typename _RealType1,
1927*38fd1498Szrj 	       typename _CharT, typename _Traits>
1928*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
1929*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
1930*38fd1498Szrj 		   poisson_distribution<_IntType1, _RealType1>& __x);
1931*38fd1498Szrj 
1932*38fd1498Szrj     private:
1933*38fd1498Szrj       void
1934*38fd1498Szrj       _M_initialize();
1935*38fd1498Szrj 
1936*38fd1498Szrj       // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1937*38fd1498Szrj       normal_distribution<_RealType> _M_nd;
1938*38fd1498Szrj 
1939*38fd1498Szrj       _RealType _M_mean;
1940*38fd1498Szrj 
1941*38fd1498Szrj       // Hosts either log(mean) or the threshold of the simple method.
1942*38fd1498Szrj       _RealType _M_lm_thr;
1943*38fd1498Szrj #if _GLIBCXX_USE_C99_MATH_TR1
1944*38fd1498Szrj       _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
1945*38fd1498Szrj #endif
1946*38fd1498Szrj     };
1947*38fd1498Szrj 
1948*38fd1498Szrj 
1949*38fd1498Szrj   /**
1950*38fd1498Szrj    * @brief A discrete binomial random number distribution.
1951*38fd1498Szrj    *
1952*38fd1498Szrj    * The formula for the binomial probability mass function is
1953*38fd1498Szrj    * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1954*38fd1498Szrj    * and @f$ p @f$ are the parameters of the distribution.
1955*38fd1498Szrj    */
1956*38fd1498Szrj   template<typename _IntType = int, typename _RealType = double>
1957*38fd1498Szrj     class binomial_distribution
1958*38fd1498Szrj     {
1959*38fd1498Szrj     public:
1960*38fd1498Szrj       // types
1961*38fd1498Szrj       typedef _RealType input_type;
1962*38fd1498Szrj       typedef _IntType  result_type;
1963*38fd1498Szrj 
1964*38fd1498Szrj       // constructors and member function
1965*38fd1498Szrj       explicit
1966*38fd1498Szrj       binomial_distribution(_IntType __t = 1,
1967*38fd1498Szrj 			    const _RealType& __p = _RealType(0.5))
1968*38fd1498Szrj       : _M_t(__t), _M_p(__p), _M_nd()
1969*38fd1498Szrj       {
1970*38fd1498Szrj 	_GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
1971*38fd1498Szrj 	_M_initialize();
1972*38fd1498Szrj       }
1973*38fd1498Szrj 
1974*38fd1498Szrj       /**
1975*38fd1498Szrj        * Gets the distribution @p t parameter.
1976*38fd1498Szrj        */
1977*38fd1498Szrj       _IntType
1978*38fd1498Szrj       t() const
1979*38fd1498Szrj       { return _M_t; }
1980*38fd1498Szrj 
1981*38fd1498Szrj       /**
1982*38fd1498Szrj        * Gets the distribution @p p parameter.
1983*38fd1498Szrj        */
1984*38fd1498Szrj       _RealType
1985*38fd1498Szrj       p() const
1986*38fd1498Szrj       { return _M_p; }
1987*38fd1498Szrj 
1988*38fd1498Szrj       void
1989*38fd1498Szrj       reset()
1990*38fd1498Szrj       { _M_nd.reset(); }
1991*38fd1498Szrj 
1992*38fd1498Szrj       template<class _UniformRandomNumberGenerator>
1993*38fd1498Szrj         result_type
1994*38fd1498Szrj         operator()(_UniformRandomNumberGenerator& __urng);
1995*38fd1498Szrj 
1996*38fd1498Szrj       /**
1997*38fd1498Szrj        * Inserts a %binomial_distribution random number distribution
1998*38fd1498Szrj        * @p __x into the output stream @p __os.
1999*38fd1498Szrj        *
2000*38fd1498Szrj        * @param __os An output stream.
2001*38fd1498Szrj        * @param __x  A %binomial_distribution random number distribution.
2002*38fd1498Szrj        *
2003*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
2004*38fd1498Szrj        * an error state.
2005*38fd1498Szrj        */
2006*38fd1498Szrj       template<typename _IntType1, typename _RealType1,
2007*38fd1498Szrj 	       typename _CharT, typename _Traits>
2008*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
2009*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2010*38fd1498Szrj 		   const binomial_distribution<_IntType1, _RealType1>& __x);
2011*38fd1498Szrj 
2012*38fd1498Szrj       /**
2013*38fd1498Szrj        * Extracts a %binomial_distribution random number distribution
2014*38fd1498Szrj        * @p __x from the input stream @p __is.
2015*38fd1498Szrj        *
2016*38fd1498Szrj        * @param __is An input stream.
2017*38fd1498Szrj        * @param __x  A %binomial_distribution random number generator engine.
2018*38fd1498Szrj        *
2019*38fd1498Szrj        * @returns The input stream with @p __x extracted or in an error state.
2020*38fd1498Szrj        */
2021*38fd1498Szrj       template<typename _IntType1, typename _RealType1,
2022*38fd1498Szrj 	       typename _CharT, typename _Traits>
2023*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
2024*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
2025*38fd1498Szrj 		   binomial_distribution<_IntType1, _RealType1>& __x);
2026*38fd1498Szrj 
2027*38fd1498Szrj     private:
2028*38fd1498Szrj       void
2029*38fd1498Szrj       _M_initialize();
2030*38fd1498Szrj 
2031*38fd1498Szrj       template<class _UniformRandomNumberGenerator>
2032*38fd1498Szrj         result_type
2033*38fd1498Szrj         _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
2034*38fd1498Szrj 
2035*38fd1498Szrj       // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
2036*38fd1498Szrj       normal_distribution<_RealType> _M_nd;
2037*38fd1498Szrj 
2038*38fd1498Szrj       _RealType _M_q;
2039*38fd1498Szrj #if _GLIBCXX_USE_C99_MATH_TR1
2040*38fd1498Szrj       _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
2041*38fd1498Szrj 	        _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
2042*38fd1498Szrj #endif
2043*38fd1498Szrj       _RealType _M_p;
2044*38fd1498Szrj       _IntType  _M_t;
2045*38fd1498Szrj 
2046*38fd1498Szrj       bool      _M_easy;
2047*38fd1498Szrj     };
2048*38fd1498Szrj 
2049*38fd1498Szrj   /* @} */ // group tr1_random_distributions_discrete
2050*38fd1498Szrj 
2051*38fd1498Szrj   /**
2052*38fd1498Szrj    * @addtogroup tr1_random_distributions_continuous Continuous Distributions
2053*38fd1498Szrj    * @ingroup tr1_random_distributions
2054*38fd1498Szrj    * @{
2055*38fd1498Szrj    */
2056*38fd1498Szrj 
2057*38fd1498Szrj   /**
2058*38fd1498Szrj    * @brief Uniform continuous distribution for random numbers.
2059*38fd1498Szrj    *
2060*38fd1498Szrj    * A continuous random distribution on the range [min, max) with equal
2061*38fd1498Szrj    * probability throughout the range.  The URNG should be real-valued and
2062*38fd1498Szrj    * deliver number in the range [0, 1).
2063*38fd1498Szrj    */
2064*38fd1498Szrj   template<typename _RealType = double>
2065*38fd1498Szrj     class uniform_real
2066*38fd1498Szrj     {
2067*38fd1498Szrj     public:
2068*38fd1498Szrj       // types
2069*38fd1498Szrj       typedef _RealType input_type;
2070*38fd1498Szrj       typedef _RealType result_type;
2071*38fd1498Szrj 
2072*38fd1498Szrj     public:
2073*38fd1498Szrj       /**
2074*38fd1498Szrj        * Constructs a uniform_real object.
2075*38fd1498Szrj        *
2076*38fd1498Szrj        * @param __min [IN]  The lower bound of the distribution.
2077*38fd1498Szrj        * @param __max [IN]  The upper bound of the distribution.
2078*38fd1498Szrj        */
2079*38fd1498Szrj       explicit
2080*38fd1498Szrj       uniform_real(_RealType __min = _RealType(0),
2081*38fd1498Szrj 		   _RealType __max = _RealType(1))
2082*38fd1498Szrj       : _M_min(__min), _M_max(__max)
2083*38fd1498Szrj       {
2084*38fd1498Szrj 	_GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
2085*38fd1498Szrj       }
2086*38fd1498Szrj 
2087*38fd1498Szrj       result_type
2088*38fd1498Szrj       min() const
2089*38fd1498Szrj       { return _M_min; }
2090*38fd1498Szrj 
2091*38fd1498Szrj       result_type
2092*38fd1498Szrj       max() const
2093*38fd1498Szrj       { return _M_max; }
2094*38fd1498Szrj 
2095*38fd1498Szrj       void
2096*38fd1498Szrj       reset() { }
2097*38fd1498Szrj 
2098*38fd1498Szrj       template<class _UniformRandomNumberGenerator>
2099*38fd1498Szrj         result_type
2100*38fd1498Szrj         operator()(_UniformRandomNumberGenerator& __urng)
2101*38fd1498Szrj         { return (__urng() * (_M_max - _M_min)) + _M_min; }
2102*38fd1498Szrj 
2103*38fd1498Szrj       /**
2104*38fd1498Szrj        * Inserts a %uniform_real random number distribution @p __x into the
2105*38fd1498Szrj        * output stream @p __os.
2106*38fd1498Szrj        *
2107*38fd1498Szrj        * @param __os An output stream.
2108*38fd1498Szrj        * @param __x  A %uniform_real random number distribution.
2109*38fd1498Szrj        *
2110*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
2111*38fd1498Szrj        * an error state.
2112*38fd1498Szrj        */
2113*38fd1498Szrj       template<typename _RealType1, typename _CharT, typename _Traits>
2114*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
2115*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2116*38fd1498Szrj 		   const uniform_real<_RealType1>& __x);
2117*38fd1498Szrj 
2118*38fd1498Szrj       /**
2119*38fd1498Szrj        * Extracts a %uniform_real random number distribution
2120*38fd1498Szrj        * @p __x from the input stream @p __is.
2121*38fd1498Szrj        *
2122*38fd1498Szrj        * @param __is An input stream.
2123*38fd1498Szrj        * @param __x  A %uniform_real random number generator engine.
2124*38fd1498Szrj        *
2125*38fd1498Szrj        * @returns The input stream with @p __x extracted or in an error state.
2126*38fd1498Szrj        */
2127*38fd1498Szrj       template<typename _RealType1, typename _CharT, typename _Traits>
2128*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
2129*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
2130*38fd1498Szrj 		   uniform_real<_RealType1>& __x);
2131*38fd1498Szrj 
2132*38fd1498Szrj     private:
2133*38fd1498Szrj       _RealType _M_min;
2134*38fd1498Szrj       _RealType _M_max;
2135*38fd1498Szrj     };
2136*38fd1498Szrj 
2137*38fd1498Szrj 
2138*38fd1498Szrj   /**
2139*38fd1498Szrj    * @brief An exponential continuous distribution for random numbers.
2140*38fd1498Szrj    *
2141*38fd1498Szrj    * The formula for the exponential probability mass function is
2142*38fd1498Szrj    * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2143*38fd1498Szrj    *
2144*38fd1498Szrj    * <table border=1 cellpadding=10 cellspacing=0>
2145*38fd1498Szrj    * <caption align=top>Distribution Statistics</caption>
2146*38fd1498Szrj    * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2147*38fd1498Szrj    * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2148*38fd1498Szrj    * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2149*38fd1498Szrj    * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2150*38fd1498Szrj    * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2151*38fd1498Szrj    * </table>
2152*38fd1498Szrj    */
2153*38fd1498Szrj   template<typename _RealType = double>
2154*38fd1498Szrj     class exponential_distribution
2155*38fd1498Szrj     {
2156*38fd1498Szrj     public:
2157*38fd1498Szrj       // types
2158*38fd1498Szrj       typedef _RealType input_type;
2159*38fd1498Szrj       typedef _RealType result_type;
2160*38fd1498Szrj 
2161*38fd1498Szrj     public:
2162*38fd1498Szrj       /**
2163*38fd1498Szrj        * Constructs an exponential distribution with inverse scale parameter
2164*38fd1498Szrj        * @f$ \lambda @f$.
2165*38fd1498Szrj        */
2166*38fd1498Szrj       explicit
2167*38fd1498Szrj       exponential_distribution(const result_type& __lambda = result_type(1))
2168*38fd1498Szrj       : _M_lambda(__lambda)
2169*38fd1498Szrj       {
2170*38fd1498Szrj 	_GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
2171*38fd1498Szrj       }
2172*38fd1498Szrj 
2173*38fd1498Szrj       /**
2174*38fd1498Szrj        * Gets the inverse scale parameter of the distribution.
2175*38fd1498Szrj        */
2176*38fd1498Szrj       _RealType
2177*38fd1498Szrj       lambda() const
2178*38fd1498Szrj       { return _M_lambda; }
2179*38fd1498Szrj 
2180*38fd1498Szrj       /**
2181*38fd1498Szrj        * Resets the distribution.
2182*38fd1498Szrj        *
2183*38fd1498Szrj        * Has no effect on exponential distributions.
2184*38fd1498Szrj        */
2185*38fd1498Szrj       void
2186*38fd1498Szrj       reset() { }
2187*38fd1498Szrj 
2188*38fd1498Szrj       template<class _UniformRandomNumberGenerator>
2189*38fd1498Szrj         result_type
2190*38fd1498Szrj         operator()(_UniformRandomNumberGenerator& __urng)
2191*38fd1498Szrj         { return -std::log(__urng()) / _M_lambda; }
2192*38fd1498Szrj 
2193*38fd1498Szrj       /**
2194*38fd1498Szrj        * Inserts a %exponential_distribution random number distribution
2195*38fd1498Szrj        * @p __x into the output stream @p __os.
2196*38fd1498Szrj        *
2197*38fd1498Szrj        * @param __os An output stream.
2198*38fd1498Szrj        * @param __x  A %exponential_distribution random number distribution.
2199*38fd1498Szrj        *
2200*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
2201*38fd1498Szrj        * an error state.
2202*38fd1498Szrj        */
2203*38fd1498Szrj       template<typename _RealType1, typename _CharT, typename _Traits>
2204*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
2205*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2206*38fd1498Szrj 		   const exponential_distribution<_RealType1>& __x);
2207*38fd1498Szrj 
2208*38fd1498Szrj       /**
2209*38fd1498Szrj        * Extracts a %exponential_distribution random number distribution
2210*38fd1498Szrj        * @p __x from the input stream @p __is.
2211*38fd1498Szrj        *
2212*38fd1498Szrj        * @param __is An input stream.
2213*38fd1498Szrj        * @param __x A %exponential_distribution random number
2214*38fd1498Szrj        *            generator engine.
2215*38fd1498Szrj        *
2216*38fd1498Szrj        * @returns The input stream with @p __x extracted or in an error state.
2217*38fd1498Szrj        */
2218*38fd1498Szrj       template<typename _CharT, typename _Traits>
2219*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
2220*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
2221*38fd1498Szrj 		   exponential_distribution& __x)
2222*38fd1498Szrj         { return __is >> __x._M_lambda; }
2223*38fd1498Szrj 
2224*38fd1498Szrj     private:
2225*38fd1498Szrj       result_type _M_lambda;
2226*38fd1498Szrj     };
2227*38fd1498Szrj 
2228*38fd1498Szrj 
2229*38fd1498Szrj   /**
2230*38fd1498Szrj    * @brief A normal continuous distribution for random numbers.
2231*38fd1498Szrj    *
2232*38fd1498Szrj    * The formula for the normal probability mass function is
2233*38fd1498Szrj    * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2234*38fd1498Szrj    *            e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2235*38fd1498Szrj    */
2236*38fd1498Szrj   template<typename _RealType = double>
2237*38fd1498Szrj     class normal_distribution
2238*38fd1498Szrj     {
2239*38fd1498Szrj     public:
2240*38fd1498Szrj       // types
2241*38fd1498Szrj       typedef _RealType input_type;
2242*38fd1498Szrj       typedef _RealType result_type;
2243*38fd1498Szrj 
2244*38fd1498Szrj     public:
2245*38fd1498Szrj       /**
2246*38fd1498Szrj        * Constructs a normal distribution with parameters @f$ mean @f$ and
2247*38fd1498Szrj        * @f$ \sigma @f$.
2248*38fd1498Szrj        */
2249*38fd1498Szrj       explicit
2250*38fd1498Szrj       normal_distribution(const result_type& __mean = result_type(0),
2251*38fd1498Szrj 			  const result_type& __sigma = result_type(1))
2252*38fd1498Szrj       : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
2253*38fd1498Szrj       {
2254*38fd1498Szrj 	_GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
2255*38fd1498Szrj       }
2256*38fd1498Szrj 
2257*38fd1498Szrj       /**
2258*38fd1498Szrj        * Gets the mean of the distribution.
2259*38fd1498Szrj        */
2260*38fd1498Szrj       _RealType
2261*38fd1498Szrj       mean() const
2262*38fd1498Szrj       { return _M_mean; }
2263*38fd1498Szrj 
2264*38fd1498Szrj       /**
2265*38fd1498Szrj        * Gets the @f$ \sigma @f$ of the distribution.
2266*38fd1498Szrj        */
2267*38fd1498Szrj       _RealType
2268*38fd1498Szrj       sigma() const
2269*38fd1498Szrj       { return _M_sigma; }
2270*38fd1498Szrj 
2271*38fd1498Szrj       /**
2272*38fd1498Szrj        * Resets the distribution.
2273*38fd1498Szrj        */
2274*38fd1498Szrj       void
2275*38fd1498Szrj       reset()
2276*38fd1498Szrj       { _M_saved_available = false; }
2277*38fd1498Szrj 
2278*38fd1498Szrj       template<class _UniformRandomNumberGenerator>
2279*38fd1498Szrj         result_type
2280*38fd1498Szrj         operator()(_UniformRandomNumberGenerator& __urng);
2281*38fd1498Szrj 
2282*38fd1498Szrj       /**
2283*38fd1498Szrj        * Inserts a %normal_distribution random number distribution
2284*38fd1498Szrj        * @p __x into the output stream @p __os.
2285*38fd1498Szrj        *
2286*38fd1498Szrj        * @param __os An output stream.
2287*38fd1498Szrj        * @param __x  A %normal_distribution random number distribution.
2288*38fd1498Szrj        *
2289*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
2290*38fd1498Szrj        * an error state.
2291*38fd1498Szrj        */
2292*38fd1498Szrj       template<typename _RealType1, typename _CharT, typename _Traits>
2293*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
2294*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2295*38fd1498Szrj 		   const normal_distribution<_RealType1>& __x);
2296*38fd1498Szrj 
2297*38fd1498Szrj       /**
2298*38fd1498Szrj        * Extracts a %normal_distribution random number distribution
2299*38fd1498Szrj        * @p __x from the input stream @p __is.
2300*38fd1498Szrj        *
2301*38fd1498Szrj        * @param __is An input stream.
2302*38fd1498Szrj        * @param __x  A %normal_distribution random number generator engine.
2303*38fd1498Szrj        *
2304*38fd1498Szrj        * @returns The input stream with @p __x extracted or in an error state.
2305*38fd1498Szrj        */
2306*38fd1498Szrj       template<typename _RealType1, typename _CharT, typename _Traits>
2307*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
2308*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
2309*38fd1498Szrj 		   normal_distribution<_RealType1>& __x);
2310*38fd1498Szrj 
2311*38fd1498Szrj     private:
2312*38fd1498Szrj       result_type _M_mean;
2313*38fd1498Szrj       result_type _M_sigma;
2314*38fd1498Szrj       result_type _M_saved;
2315*38fd1498Szrj       bool        _M_saved_available;
2316*38fd1498Szrj     };
2317*38fd1498Szrj 
2318*38fd1498Szrj 
2319*38fd1498Szrj   /**
2320*38fd1498Szrj    * @brief A gamma continuous distribution for random numbers.
2321*38fd1498Szrj    *
2322*38fd1498Szrj    * The formula for the gamma probability mass function is
2323*38fd1498Szrj    * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2324*38fd1498Szrj    */
2325*38fd1498Szrj   template<typename _RealType = double>
2326*38fd1498Szrj     class gamma_distribution
2327*38fd1498Szrj     {
2328*38fd1498Szrj     public:
2329*38fd1498Szrj       // types
2330*38fd1498Szrj       typedef _RealType input_type;
2331*38fd1498Szrj       typedef _RealType result_type;
2332*38fd1498Szrj 
2333*38fd1498Szrj     public:
2334*38fd1498Szrj       /**
2335*38fd1498Szrj        * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2336*38fd1498Szrj        */
2337*38fd1498Szrj       explicit
2338*38fd1498Szrj       gamma_distribution(const result_type& __alpha_val = result_type(1))
2339*38fd1498Szrj       : _M_alpha(__alpha_val)
2340*38fd1498Szrj       {
2341*38fd1498Szrj 	_GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
2342*38fd1498Szrj 	_M_initialize();
2343*38fd1498Szrj       }
2344*38fd1498Szrj 
2345*38fd1498Szrj       /**
2346*38fd1498Szrj        * Gets the @f$ \alpha @f$ of the distribution.
2347*38fd1498Szrj        */
2348*38fd1498Szrj       _RealType
2349*38fd1498Szrj       alpha() const
2350*38fd1498Szrj       { return _M_alpha; }
2351*38fd1498Szrj 
2352*38fd1498Szrj       /**
2353*38fd1498Szrj        * Resets the distribution.
2354*38fd1498Szrj        */
2355*38fd1498Szrj       void
2356*38fd1498Szrj       reset() { }
2357*38fd1498Szrj 
2358*38fd1498Szrj       template<class _UniformRandomNumberGenerator>
2359*38fd1498Szrj         result_type
2360*38fd1498Szrj         operator()(_UniformRandomNumberGenerator& __urng);
2361*38fd1498Szrj 
2362*38fd1498Szrj       /**
2363*38fd1498Szrj        * Inserts a %gamma_distribution random number distribution
2364*38fd1498Szrj        * @p __x into the output stream @p __os.
2365*38fd1498Szrj        *
2366*38fd1498Szrj        * @param __os An output stream.
2367*38fd1498Szrj        * @param __x  A %gamma_distribution random number distribution.
2368*38fd1498Szrj        *
2369*38fd1498Szrj        * @returns The output stream with the state of @p __x inserted or in
2370*38fd1498Szrj        * an error state.
2371*38fd1498Szrj        */
2372*38fd1498Szrj       template<typename _RealType1, typename _CharT, typename _Traits>
2373*38fd1498Szrj         friend std::basic_ostream<_CharT, _Traits>&
2374*38fd1498Szrj         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2375*38fd1498Szrj 		   const gamma_distribution<_RealType1>& __x);
2376*38fd1498Szrj 
2377*38fd1498Szrj       /**
2378*38fd1498Szrj        * Extracts a %gamma_distribution random number distribution
2379*38fd1498Szrj        * @p __x from the input stream @p __is.
2380*38fd1498Szrj        *
2381*38fd1498Szrj        * @param __is An input stream.
2382*38fd1498Szrj        * @param __x  A %gamma_distribution random number generator engine.
2383*38fd1498Szrj        *
2384*38fd1498Szrj        * @returns The input stream with @p __x extracted or in an error state.
2385*38fd1498Szrj        */
2386*38fd1498Szrj       template<typename _CharT, typename _Traits>
2387*38fd1498Szrj         friend std::basic_istream<_CharT, _Traits>&
2388*38fd1498Szrj         operator>>(std::basic_istream<_CharT, _Traits>& __is,
2389*38fd1498Szrj 		   gamma_distribution& __x)
2390*38fd1498Szrj         {
2391*38fd1498Szrj 	  __is >> __x._M_alpha;
2392*38fd1498Szrj 	  __x._M_initialize();
2393*38fd1498Szrj 	  return __is;
2394*38fd1498Szrj 	}
2395*38fd1498Szrj 
2396*38fd1498Szrj     private:
2397*38fd1498Szrj       void
2398*38fd1498Szrj       _M_initialize();
2399*38fd1498Szrj 
2400*38fd1498Szrj       result_type _M_alpha;
2401*38fd1498Szrj 
2402*38fd1498Szrj       // Hosts either lambda of GB or d of modified Vaduva's.
2403*38fd1498Szrj       result_type _M_l_d;
2404*38fd1498Szrj     };
2405*38fd1498Szrj 
2406*38fd1498Szrj   /* @} */ // group tr1_random_distributions_continuous
2407*38fd1498Szrj   /* @} */ // group tr1_random_distributions
2408*38fd1498Szrj   /* @} */ // group tr1_random
2409*38fd1498Szrj }
2410*38fd1498Szrj 
2411*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
2412*38fd1498Szrj }
2413*38fd1498Szrj 
2414*38fd1498Szrj #endif // _GLIBCXX_TR1_RANDOM_H
2415