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