7eb8f670b75ac65f46bc76a38074ee9f09d52eb1
[gcc.git] / libstdc++-v3 / include / tr1 / random
1 // random number generation -*- C++ -*-
2
3 // Copyright (C) 2006 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 #ifndef _STD_TR1_RANDOM
31 #define _STD_TR1_RANDOM 1
32
33 /**
34 * @file
35 * This is a TR1 C++ Library header.
36 */
37
38 #include <algorithm>
39 #include <bits/concept_check.h>
40 #include <bits/cpp_type_traits.h>
41 #include <cmath>
42 #include <debug/debug.h>
43 #include <iterator>
44 #include <iosfwd>
45 #include <limits>
46 #include <tr1/type_traits>
47 #include <fstream>
48
49 namespace std
50 {
51 _GLIBCXX_BEGIN_NAMESPACE(tr1)
52
53 // [5.1] Random number generation
54
55 /**
56 * @addtogroup tr1_random Random Number Generation
57 * A facility for generating random numbers on selected distributions.
58 * @{
59 */
60
61 /*
62 * Implementation-space details.
63 */
64 namespace _Private
65 {
66 // Type selectors -- are these already implemented elsewhere?
67 template<bool, typename _TpTrue, typename _TpFalse>
68 struct _Select
69 {
70 typedef _TpTrue _Type;
71 };
72
73 template<typename _TpTrue, typename _TpFalse>
74 struct _Select<false, _TpTrue, _TpFalse>
75 {
76 typedef _TpFalse _Type;
77 };
78
79 /*
80 * An adaptor class for converting the output of any Generator into
81 * the input for a specific Distribution.
82 */
83 template<typename _Generator, typename _Distribution>
84 struct _Adaptor
85 {
86 typedef typename _Generator::result_type generated_type;
87 typedef typename _Distribution::input_type result_type;
88
89 public:
90 _Adaptor(const _Generator& __g)
91 : _M_g(__g) { }
92
93 result_type
94 operator()();
95
96 private:
97 _Generator _M_g;
98 };
99
100 /*
101 * Converts a value generated by the adapted random number generator into a
102 * value in the input domain for the dependent random number distribution.
103 *
104 * Because the type traits are compile time constants only the appropriate
105 * clause of the if statements will actually be emitted by the compiler.
106 */
107 template<typename _Generator, typename _Distribution>
108 typename _Adaptor<_Generator, _Distribution>::result_type
109 _Adaptor<_Generator, _Distribution>::
110 operator()()
111 {
112 result_type __return_value = 0;
113 if (is_integral<generated_type>::value
114 && is_integral<result_type>::value)
115 __return_value = _M_g();
116 else if (is_integral<generated_type>::value
117 && !is_integral<result_type>::value)
118 __return_value = result_type(_M_g())
119 / result_type(_M_g.max() - _M_g.min() + 1);
120 else if (!is_integral<generated_type>::value
121 && !is_integral<result_type>::value)
122 __return_value = result_type(_M_g())
123 / result_type(_M_g.max() - _M_g.min());
124 return __return_value;
125 }
126
127 template<typename _UIntType, int __w, bool =
128 __w != std::numeric_limits<_UIntType>::digits>
129 struct _Shift
130 { static const _UIntType __value = 0; };
131
132 template<typename _UIntType, int __w>
133 struct _Shift<_UIntType, __w, true>
134 { static const _UIntType __value = _UIntType(1) << __w; };
135
136 } // namespace std::tr1::_Private
137
138
139 /**
140 * Produces random numbers on a given disribution function using a un uniform
141 * random number generation engine.
142 *
143 * @todo the engine_value_type needs to be studied more carefully.
144 */
145 template<typename _Generator, typename _Dist>
146 class variate_generator
147 {
148 // Concept requirements.
149 __glibcxx_class_requires(_Generator, _CopyConstructibleConcept)
150 // __glibcxx_class_requires(_Generator, _GeneratorConcept)
151 // __glibcxx_class_requires(_Dist, _GeneratorConcept)
152
153 public:
154 typedef _Generator engine_type;
155 typedef _Private::_Adaptor<_Generator, _Dist> engine_value_type;
156 typedef _Dist distribution_type;
157 typedef typename _Dist::result_type result_type;
158
159 // tr1:5.1.1 table 5.1 requirement
160 typedef typename std::__enable_if<result_type,
161 is_arithmetic<result_type>::value
162 >::__type _IsValidType;
163
164 public:
165 /**
166 * Constructs a variate generator with the uniform random number
167 * generator @p __eng for the random distribution @p __dist.
168 *
169 * @throws Any exceptions which may thrown by the copy constructors of
170 * the @p _Generator or @p _Dist objects.
171 */
172 variate_generator(engine_type __eng, distribution_type __dist)
173 : _M_engine(__eng), _M_dist(__dist) { }
174
175 /**
176 * Gets the next generated value on the distribution.
177 */
178 result_type
179 operator()();
180
181 template<typename _Tp>
182 result_type
183 operator()(_Tp __value);
184
185 /**
186 * Gets a reference to the underlying uniform random number generator
187 * object.
188 */
189 engine_value_type&
190 engine()
191 { return _M_engine; }
192
193 /**
194 * Gets a const reference to the underlying uniform random number
195 * generator object.
196 */
197 const engine_value_type&
198 engine() const
199 { return _M_engine; }
200
201 /**
202 * Gets a reference to the underlying random distribution.
203 */
204 distribution_type&
205 distribution()
206 { return _M_dist; }
207
208 /**
209 * Gets a const reference to the underlying random distribution.
210 */
211 const distribution_type&
212 distribution() const
213 { return _M_dist; }
214
215 /**
216 * Gets the closed lower bound of the distribution interval.
217 */
218 result_type
219 min() const
220 { return this->distribution().min(); }
221
222 /**
223 * Gets the closed upper bound of the distribution interval.
224 */
225 result_type
226 max() const
227 { return this->distribution().max(); }
228
229 private:
230 engine_value_type _M_engine;
231 distribution_type _M_dist;
232 };
233
234 /**
235 * Gets the next random value on the given distribution.
236 */
237 template<typename _Generator, typename _Dist>
238 typename variate_generator<_Generator, _Dist>::result_type
239 variate_generator<_Generator, _Dist>::
240 operator()()
241 { return _M_dist(_M_engine); }
242
243 /**
244 * WTF?
245 */
246 template<typename _Generator, typename _Dist>
247 template<typename _Tp>
248 typename variate_generator<_Generator, _Dist>::result_type
249 variate_generator<_Generator, _Dist>::
250 operator()(_Tp __value)
251 { return _M_dist(_M_engine, __value); }
252
253
254 /**
255 * @addtogroup tr1_random_generators Random Number Generators
256 * @ingroup tr1_random
257 *
258 * These classes define objects which provide random or pseudorandom numbers,
259 * either from a discrete or a continuous interval. The random number
260 * generator supplied as a part of this library are all uniform random number
261 * generators which provide a sequence of random number uniformly distributed
262 * over their range.
263 *
264 * A number generator is a function object with an operator() that takes zero
265 * arguments and returns a number.
266 *
267 * A compliant random number generator must satisy the following requirements.
268 * <table border=1 cellpadding=10 cellspacing=0>
269 * <caption align=top>Random Number Generator Requirements</caption>
270 * <tr><td>To be documented.</td></tr>
271 * </table>
272 *
273 * @{
274 */
275
276 /**
277 * @brief A model of a linear congruential random number generator.
278 *
279 * A random number generator that produces pseudorandom numbers using the
280 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
281 *
282 * The template parameter @p _UIntType must be an unsigned integral type
283 * large enough to store values up to (__m-1). If the template parameter
284 * @p __m is 0, the modulus @p __m used is
285 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
286 * parameters @p __a and @p __c must be less than @p __m.
287 *
288 * The size of the state is @f$ 1 @f$.
289 */
290 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
291 class linear_congruential
292 {
293 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
294 // __glibcpp_class_requires(__a < __m && __c < __m)
295
296 public:
297 /** The type of the generated random value. */
298 typedef _UIntType result_type;
299
300 /** The multiplier. */
301 static const _UIntType multiplier = __a;
302 /** An increment. */
303 static const _UIntType increment = __c;
304 /** The modulus. */
305 static const _UIntType modulus = __m;
306
307 /**
308 * Constructs a %linear_congruential random number generator engine with
309 * seed @p __s. The default seed value is 1.
310 *
311 * @param __s The initial seed value.
312 */
313 explicit
314 linear_congruential(unsigned long __x0 = 1)
315 { this->seed(__x0); }
316
317 /**
318 * Constructs a %linear_congruential random number generator engine
319 * seeded from the generator function @p __g.
320 *
321 * @param __g The seed generator function.
322 */
323 template<class _Gen>
324 linear_congruential(_Gen& __g)
325 { this->seed(__g); }
326
327 /**
328 * Reseeds the %linear_congruential random number generator engine
329 * sequence to the seed @g __s.
330 *
331 * @param __s The new seed.
332 */
333 void
334 seed(unsigned long __s = 1);
335
336 /**
337 * Reseeds the %linear_congruential random number generator engine
338 * sequence using values from the generator function @p __g.
339 *
340 * @param __g the seed generator function.
341 */
342 template<class _Gen>
343 void
344 seed(_Gen& __g)
345 { seed(__g, typename is_fundamental<_Gen>::type()); }
346
347 /**
348 * Gets the smallest possible value in the output range.
349 */
350 result_type
351 min() const;
352
353 /**
354 * Gets the largest possible value in the output range.
355 */
356 result_type
357 max() const;
358
359 /**
360 * Gets the next random number in the sequence.
361 */
362 result_type
363 operator()();
364
365 /**
366 * Compares two linear congruential random number generator objects of the
367 * same type for equality.
368 *
369 * @param __lhs A linear congruential random number generator object.
370 * @param __rhs Another linear congruential random number generator obj.
371 *
372 * @returns true if the two objects are equal, false otherwise.
373 */
374 friend bool
375 operator==(const linear_congruential& __lhs,
376 const linear_congruential& __rhs)
377 { return __lhs._M_x == __rhs._M_x; }
378
379 /**
380 * Compares two linear congruential random number generator objects of the
381 * same type for inequality.
382 *
383 * @param __lhs A linear congruential random number generator object.
384 * @param __rhs Another linear congruential random number generator obj.
385 *
386 * @returns true if the two objects are not equal, false otherwise.
387 */
388 friend bool
389 operator!=(const linear_congruential& __lhs,
390 const linear_congruential& __rhs)
391 { return !(__lhs == __rhs); }
392
393 /**
394 * Writes the textual representation of the state x(i) of x to @p __os.
395 *
396 * @param __os The output stream.
397 * @param __lcr A linear_congruential random number generator.
398 * @returns __os.
399 */
400 template<typename _CharT, typename _Traits>
401 friend std::basic_ostream<_CharT, _Traits>&
402 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
403 const linear_congruential& __lcr)
404 { return __os << __lcr._M_x; }
405
406 /**
407 * Sets the state of the engine by reading its textual
408 * representation from @p __is.
409 *
410 * The textual representation must have been previously written using an
411 * output stream whose imbued locale and whose type's template
412 * specialization arguments _CharT and _Traits were the same as those of
413 * @p __is.
414 *
415 * @param __is The input stream.
416 * @param __lcr A linear_congruential random number generator.
417 * @returns __is.
418 */
419 template<typename _CharT, typename _Traits>
420 friend std::basic_istream<_CharT, _Traits>&
421 operator>>(std::basic_istream<_CharT, _Traits>& __is,
422 linear_congruential& __lcr)
423 { return __is >> __lcr._M_x; }
424
425 private:
426 template<class _Gen>
427 void
428 seed(_Gen& __g, true_type)
429 { return seed(static_cast<unsigned long>(__g)); }
430
431 template<class _Gen>
432 void
433 seed(_Gen& __g, false_type);
434
435 private:
436 _UIntType _M_x;
437 };
438
439 /**
440 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
441 */
442 typedef linear_congruential<unsigned int, 16807, 0, 2147483647> minstd_rand0;
443
444 /**
445 * An alternative LCR (Lehmer Generator function) .
446 */
447 typedef linear_congruential<unsigned int, 48271, 0, 2147483647> minstd_rand;
448
449
450 /**
451 * A generalized feedback shift register discrete random number generator.
452 *
453 * This algorithm avoind multiplication and division and is designed to be
454 * friendly to a pipelined architecture. If the parameters are chosen
455 * correctly, this generator will produce numbers with a very long period and
456 * fairly good apparent entropy, although still not cryptographically strong.
457 *
458 * The best way to use this generator is with the predefined mt19937 class.
459 *
460 * This algorithm was originally invented by Makoto Matsumoto and
461 * Takuji Nishimura.
462 *
463 * @var word_size The number of bits in each element of the state vector.
464 * @var state_size The degree of recursion.
465 * @var shift_size The period parameter.
466 * @var mask_bits The separation point bit index.
467 * @var parameter_a The last row of the twist matrix.
468 * @var output_u The first right-shift tempering matrix parameter.
469 * @var output_s The first left-shift tempering matrix parameter.
470 * @var output_b The first left-shift tempering matrix mask.
471 * @var output_t The second left-shift tempering matrix parameter.
472 * @var output_c The second left-shift tempering matrix mask.
473 * @var output_l The second right-shift tempering matrix parameter.
474 */
475 template<class _UIntType, int __w, int __n, int __m, int __r,
476 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
477 _UIntType __c, int __l>
478 class mersenne_twister
479 {
480 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
481
482 public:
483 // types
484 typedef _UIntType result_type ;
485
486 // parameter values
487 static const int word_size = __w;
488 static const int state_size = __n;
489 static const int shift_size = __m;
490 static const int mask_bits = __r;
491 static const _UIntType parameter_a = __a;
492 static const int output_u = __u;
493 static const int output_s = __s;
494 static const _UIntType output_b = __b;
495 static const int output_t = __t;
496 static const _UIntType output_c = __c;
497 static const int output_l = __l;
498
499 // constructors and member function
500 mersenne_twister()
501 { seed(); }
502
503 explicit
504 mersenne_twister(unsigned long __value)
505 { seed(__value); }
506
507 template<class _Gen>
508 mersenne_twister(_Gen& __g)
509 { seed(__g); }
510
511 void
512 seed()
513 { seed(5489UL); }
514
515 void
516 seed(unsigned long __value);
517
518 template<class _Gen>
519 void
520 seed(_Gen& __g)
521 { seed(__g, typename is_fundamental<_Gen>::type()); }
522
523 result_type
524 min() const
525 { return 0; };
526
527 result_type
528 max() const
529 { return _Private::_Shift<_UIntType, __w>::__value - 1; }
530
531 result_type
532 operator()();
533
534 /**
535 * Compares two % mersenne_twister random number generator objects of
536 * the same type for equality.
537 *
538 * @param __lhs A % mersenne_twister random number generator object.
539 * @param __rhs Another % mersenne_twister random number generator
540 * object.
541 *
542 * @returns true if the two objects are equal, false otherwise.
543 */
544 friend bool
545 operator==(const mersenne_twister& __lhs,
546 const mersenne_twister& __rhs)
547 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
548
549 /**
550 * Compares two % mersenne_twister random number generator objects of
551 * the same type for inequality.
552 *
553 * @param __lhs A % mersenne_twister random number generator object.
554 * @param __rhs Another % mersenne_twister random number generator
555 * object.
556 *
557 * @returns true if the two objects are not equal, false otherwise.
558 */
559 friend bool
560 operator!=(const mersenne_twister& __lhs,
561 const mersenne_twister& __rhs)
562 { return !(__lhs == __rhs); }
563
564 /**
565 * Inserts the current state of a % mersenne_twister random number
566 * generator engine @p __x into the output stream @p __os.
567 *
568 * @param __os An output stream.
569 * @param __x A % mersenne_twister random number generator engine.
570 *
571 * @returns The output stream with the state of @p __x inserted or in
572 * an error state.
573 */
574 template<typename _CharT, typename _Traits>
575 friend std::basic_ostream<_CharT, _Traits>&
576 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
577 const mersenne_twister& __x)
578 {
579 std::copy(__x._M_x, __x._M_x + state_size,
580 std::ostream_iterator<_UIntType>(__os, " "));
581 return __os;
582 }
583
584 /**
585 * Extracts the current state of a % mersenne_twister random number
586 * generator engine @p __x from the input stream @p __is.
587 *
588 * @param __is An input stream.
589 * @param __x A % mersenne_twister random number generator engine.
590 *
591 * @returns The input stream with the state of @p __x extracted or in
592 * an error state.
593 */
594 template<typename _CharT, typename _Traits>
595 friend std::basic_istream<_CharT, _Traits>&
596 operator>>(std::basic_istream<_CharT, _Traits>& __is,
597 mersenne_twister& __x)
598 {
599 for (int __i = 0; __i < state_size; ++__i)
600 __is >> __x._M_x[__i];
601 return __is;
602 }
603
604 private:
605 template<class _Gen>
606 void
607 seed(_Gen& __g, true_type)
608 { return seed(static_cast<unsigned long>(__g)); }
609
610 template<class _Gen>
611 void
612 seed(_Gen& __g, false_type);
613
614 private:
615 _UIntType _M_x[state_size];
616 int _M_p;
617 };
618
619 /**
620 * The classic Mersenne Twister.
621 *
622 * Reference:
623 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
624 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
625 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
626 */
627 typedef mersenne_twister<
628 unsigned long, 32, 624, 397, 31,
629 0x9908b0dful, 11, 7,
630 0x9d2c5680ul, 15,
631 0xefc60000ul, 18
632 > mt19937;
633
634
635 /**
636 * @brief The Marsaglia-Zaman generator.
637 *
638 * This is a model of a Generalized Fibonacci discrete random number
639 * generator, sometimes referred to as the SWC generator.
640 *
641 * A discrete random number generator that produces pseudorandom numbers using
642 * @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m @f$.
643 *
644 * The size of the state is @f$ r @f$
645 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
646 *
647 * N1688[4.13] says "the template parameter _IntType shall denote an integral
648 * type large enough to store values up to m."
649 *
650 * @if maint
651 * @var _M_x The state of te generator. This is a ring buffer.
652 * @var _M_carry The carry.
653 * @var _M_p Current index of x(i - r).
654 * @endif
655 */
656 template<typename _IntType, _IntType __m, int __s, int __r>
657 class subtract_with_carry
658 {
659 __glibcxx_class_requires(_IntType, _IntegerConcept)
660
661 public:
662 /** The type of the generated random value. */
663 typedef _IntType result_type;
664
665 // parameter values
666 static const _IntType modulus = __m;
667 static const int long_lag = __r;
668 static const int short_lag = __s;
669
670 public:
671 /**
672 * Constructs a default-initialized % subtract_with_carry random number
673 * generator.
674 */
675 subtract_with_carry()
676 { this->seed(); }
677
678 /**
679 * Constructs an explicitly seeded % subtract_with_carry random number
680 * generator.
681 */
682 explicit
683 subtract_with_carry(unsigned long __value)
684 { this->seed(__value); }
685
686 /**
687 * Constructs a % subtract_with_carry random number generator seeded from
688 * the PAD iterated by [__first, last).
689 */
690 template<class _Gen>
691 subtract_with_carry(_Gen& __g)
692 { this->seed(__g); }
693
694 /**
695 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
696 *
697 * @note This implementation follows the tr1 specification but will
698 * obviously not work correctly on all platforms, since it has hardcoded
699 * values that may overflow ints on some platforms.
700 *
701 * N1688[4.19] modifies this as follows.
702 * If @p __value == 0, sets value to 19780503. In any case, with a linear
703 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
704 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value @f$, sets
705 * @f$ x_{-r} \dots x_{-1} @f$ to
706 * @f$ lcg(1) \bmod m \dots lcg(r) \bmod m @f$ respectively.
707 * If @f$ x_{-1} = 0 @f$ set carry to 1, otherwise sets carry to 0.
708 */
709 void
710 seed(unsigned long __value = 19780503);
711
712 /**
713 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
714 * random number generator.
715 */
716 template<class _Gen>
717 void
718 seed(_Gen& __g)
719 { seed(__g, typename is_fundamental<_Gen>::type()); }
720
721 /**
722 * Gets the inclusive minimum value of the range of random integers
723 * returned by this generator.
724 */
725 result_type
726 min() const
727 { return 0; }
728
729 /**
730 * Gets the inclusive maximum value of the range of random integers
731 * returned by this generator.
732 */
733 result_type
734 max() const
735 { return this->modulus - 1; }
736
737 /**
738 * Gets the next random number in the sequence.
739 */
740 result_type
741 operator()();
742
743 /**
744 * Compares two % subtract_with_carry random number generator objects of
745 * the same type for equality.
746 *
747 * @param __lhs A % subtract_with_carry random number generator object.
748 * @param __rhs Another % subtract_with_carry random number generator
749 * object.
750 *
751 * @returns true if the two objects are equal, false otherwise.
752 */
753 friend bool
754 operator==(const subtract_with_carry& __lhs,
755 const subtract_with_carry& __rhs)
756 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
757
758 /**
759 * Compares two % subtract_with_carry random number generator objects of
760 * the same type for inequality.
761 *
762 * @param __lhs A % subtract_with_carry random number generator object.
763 * @param __rhs Another % subtract_with_carry random number generator
764 * object.
765 *
766 * @returns true if the two objects are not equal, false otherwise.
767 */
768 friend bool
769 operator!=(const subtract_with_carry& __lhs,
770 const subtract_with_carry& __rhs)
771 { return !(__lhs == __rhs); }
772
773 /**
774 * Inserts the current state of a % subtract_with_carry random number
775 * generator engine @p __x into the output stream @p __os.
776 *
777 * @param __os An output stream.
778 * @param __x A % subtract_with_carry random number generator engine.
779 *
780 * @returns The output stream with the state of @p __x inserted or in
781 * an error state.
782 */
783 template<typename _CharT, typename _Traits>
784 friend std::basic_ostream<_CharT, _Traits>&
785 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
786 const subtract_with_carry& __x)
787 {
788 std::copy(__x._M_x, __x._M_x + long_lag,
789 std::ostream_iterator<_IntType>(__os, " "));
790 return __os << __x._M_carry;
791 }
792
793 /**
794 * Extracts the current state of a % subtract_with_carry random number
795 * generator engine @p __x from the input stream @p __is.
796 *
797 * @param __is An input stream.
798 * @param __x A % subtract_with_carry random number generator engine.
799 *
800 * @returns The input stream with the state of @p __x extracted or in
801 * an error state.
802 */
803 template<typename _CharT, typename _Traits>
804 friend std::basic_istream<_CharT, _Traits>&
805 operator>>(std::basic_istream<_CharT, _Traits>& __is,
806 subtract_with_carry& __x)
807 {
808 for (int __i = 0; __i < long_lag; ++__i)
809 __is >> __x._M_x[__i];
810 __is >> __x._M_carry;
811 return __is;
812 }
813
814 private:
815 template<class _Gen>
816 void
817 seed(_Gen& __g, true_type)
818 { return seed(static_cast<unsigned long>(__g)); }
819
820 template<class _Gen>
821 void
822 seed(_Gen& __g, false_type);
823
824 private:
825 int _M_p;
826 result_type _M_x[long_lag];
827 result_type _M_carry;
828 };
829
830
831 /**
832 * Produces random numbers from some base engine by discarding blocks of
833 * data.
834 *
835 * 0 <= @p __r <= @p __p
836 */
837 template<class _UniformRandomNumberGenerator, int __p, int __r>
838 class discard_block
839 {
840 // __glibcxx_class_requires(typename base_type::result_type,
841 // ArithmeticTypeConcept)
842
843 public:
844 /** The type of the underlying generator engine. */
845 typedef _UniformRandomNumberGenerator base_type;
846 /** The type of the generated random value. */
847 typedef typename base_type::result_type result_type;
848
849 // parameter values
850 static const int block_size = __p;
851 static const int used_block = __r;
852
853 /**
854 * Constructs a default %discard_block engine.
855 *
856 * The underlying engine is default constructed as well.
857 */
858 discard_block()
859 : _M_n(0) { }
860
861 /**
862 * Copy constructs a %discard_block engine.
863 *
864 * Copies an existing base class random number geenerator.
865 * @param rng An existing (base class) engine object.
866 */
867 explicit
868 discard_block(const base_type& __rng)
869 : _M_b(__rng), _M_n(0) { }
870
871 /**
872 * Seed constructs a %discard_block engine.
873 *
874 * Constructs the underlying generator engine seeded with @p __s.
875 * @param __s A seed value for the base class engine.
876 */
877 explicit
878 discard_block(unsigned long __s)
879 : _M_b(__s), _M_n(0) { }
880
881 /**
882 * Generator constructs a %discard_block engine.
883 *
884 * @param __g A seed generator function.
885 */
886 template<class _Gen>
887 discard_block(_Gen& __g)
888 : _M_b(__g), _M_n(0) { }
889
890 /**
891 * Reseeds the %discard_block object with the default seed for the
892 * underlying base class generator engine.
893 */
894 void seed()
895 {
896 _M_b.seed();
897 _M_n = 0;
898 }
899
900 /**
901 * Reseeds the %discard_block object with the given seed generator
902 * function.
903 * @param __g A seed generator function.
904 */
905 template<class _Gen>
906 void seed(_Gen& __g)
907 {
908 _M_b.seed(__g);
909 _M_n = 0;
910 }
911
912 /**
913 * Gets a const reference to the underlying generator engine object.
914 */
915 const base_type&
916 base() const
917 { return _M_b; }
918
919 /**
920 * Gets the minimum value in the generated random number range.
921 */
922 result_type
923 min() const
924 { return _M_b.min(); }
925
926 /**
927 * Gets the maximum value in the generated random number range.
928 */
929 result_type
930 max() const
931 { return _M_b.max(); }
932
933 /**
934 * Gets the next value in the generated random number sequence.
935 */
936 result_type
937 operator()();
938
939 /**
940 * Compares two %discard_block random number generator objects of
941 * the same type for equality.
942 *
943 * @param __lhs A %discard_block random number generator object.
944 * @param __rhs Another %discard_block random number generator
945 * object.
946 *
947 * @returns true if the two objects are equal, false otherwise.
948 */
949 friend bool
950 operator==(const discard_block& __lhs, const discard_block& __rhs)
951 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
952
953 /**
954 * Compares two %discard_block random number generator objects of
955 * the same type for inequality.
956 *
957 * @param __lhs A %discard_block random number generator object.
958 * @param __rhs Another %discard_block random number generator
959 * object.
960 *
961 * @returns true if the two objects are not equal, false otherwise.
962 */
963 friend bool
964 operator!=(const discard_block& __lhs, const discard_block& __rhs)
965 { return !(__lhs == __rhs); }
966
967 /**
968 * Inserts the current state of a %discard_block random number
969 * generator engine @p __x into the output stream @p __os.
970 *
971 * @param __os An output stream.
972 * @param __x A %discard_block random number generator engine.
973 *
974 * @returns The output stream with the state of @p __x inserted or in
975 * an error state.
976 */
977 template<typename _CharT, typename _Traits>
978 friend std::basic_ostream<_CharT, _Traits>&
979 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
980 const discard_block& __x)
981 { return __os << __x._M_b << " " << __x._M_n; }
982
983 /**
984 * Extracts the current state of a % subtract_with_carry random number
985 * generator engine @p __x from the input stream @p __is.
986 *
987 * @param __is An input stream.
988 * @param __x A %discard_block random number generator engine.
989 *
990 * @returns The input stream with the state of @p __x extracted or in
991 * an error state.
992 */
993 template<typename _CharT, typename _Traits>
994 friend std::basic_istream<_CharT, _Traits>&
995 operator>>(std::basic_istream<_CharT, _Traits>& __is,
996 discard_block& __x)
997 { return __is >> __x._M_b >> __x._M_n; }
998
999 private:
1000 base_type _M_b;
1001 int _M_n;
1002 };
1003
1004
1005 /**
1006 * James's luxury-level-3 integer adaptation of Luescher's generator.
1007 */
1008 typedef discard_block<
1009 subtract_with_carry<int, (1 << 24), 10, 24>,
1010 223,
1011 24
1012 > ranlux3;
1013
1014 /**
1015 * James's luxury-level-4 integer adaptation of Luescher's generator.
1016 */
1017 typedef discard_block<
1018 subtract_with_carry<int, (1 << 24), 10, 24>,
1019 389,
1020 24
1021 > ranlux4;
1022
1023
1024 /**
1025 * A random number generator adaptor class that combines two random number
1026 * generator engines into a single output sequence.
1027 */
1028 template<class _UniformRandomNumberGenerator1, int __s1,
1029 class _UniformRandomNumberGenerator2, int __s2>
1030 class xor_combine
1031 {
1032 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1033 // result_type, ArithmeticTypeConcept)
1034 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1035 // result_type, ArithmeticTypeConcept)
1036
1037 public:
1038 /** The type of the the first underlying generator engine. */
1039 typedef _UniformRandomNumberGenerator1 base1_type;
1040 /** The type of the the second underlying generator engine. */
1041 typedef _UniformRandomNumberGenerator2 base2_type;
1042
1043 private:
1044 typedef typename base1_type::result_type _Result_type1;
1045 typedef typename base2_type::result_type _Result_type2;
1046
1047 public:
1048 /** The type of the generated random value. */
1049 typedef typename _Private::_Select<
1050 (sizeof(_Result_type1) > sizeof(_Result_type2)),
1051 _Result_type1, _Result_type2>::_Type result_type;
1052
1053 // parameter values
1054 static const int shift1 = __s1;
1055 static const int shift2 = __s2;
1056
1057 // constructors and member function
1058 xor_combine() { }
1059
1060 xor_combine(const base1_type& __rng1, const base2_type& __rng2)
1061 : _M_b1(__rng1), _M_b2(__rng2) { }
1062
1063 xor_combine(unsigned long __s)
1064 : _M_b1(__s), _M_b2(__s + 1) { }
1065
1066 template<class _Gen>
1067 xor_combine(_Gen& __g)
1068 : _M_b1(__g), _M_b2(__g) { }
1069
1070 void
1071 seed()
1072 {
1073 _M_b1.seed();
1074 _M_b2.seed();
1075 }
1076
1077 template<class _Gen>
1078 void
1079 seed(_Gen& __g)
1080 {
1081 _M_b1.seed(__g);
1082 _M_b2.seed(__g);
1083 }
1084
1085 const base1_type&
1086 base1() const
1087 { return _M_b1; }
1088
1089 const base2_type&
1090 base2() const
1091 { return _M_b2; }
1092
1093 result_type
1094 min() const
1095 { return _M_b1.min() ^ _M_b2.min(); }
1096
1097 result_type
1098 max() const
1099 { return _M_b1.max() | _M_b2.max(); }
1100
1101 /**
1102 * Gets the next random number in the sequence.
1103 */
1104 result_type
1105 operator()()
1106 { return ((_M_b1() << shift1) ^ (_M_b2() << shift2)); }
1107
1108 /**
1109 * Compares two %xor_combine random number generator objects of
1110 * the same type for equality.
1111 *
1112 * @param __lhs A %xor_combine random number generator object.
1113 * @param __rhs Another %xor_combine random number generator
1114 * object.
1115 *
1116 * @returns true if the two objects are equal, false otherwise.
1117 */
1118 friend bool
1119 operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1120 {
1121 return (__lhs.base1() == __rhs.base1())
1122 && (__lhs.base2() == __rhs.base2());
1123 }
1124
1125 /**
1126 * Compares two %xor_combine random number generator objects of
1127 * the same type for inequality.
1128 *
1129 * @param __lhs A %xor_combine random number generator object.
1130 * @param __rhs Another %xor_combine random number generator
1131 * object.
1132 *
1133 * @returns true if the two objects are not equal, false otherwise.
1134 */
1135 friend bool
1136 operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1137 { return !(__lhs == __rhs); }
1138
1139 /**
1140 * Inserts the current state of a %xor_combine random number
1141 * generator engine @p __x into the output stream @p __os.
1142 *
1143 * @param __os An output stream.
1144 * @param __x A %xor_combine random number generator engine.
1145 *
1146 * @returns The output stream with the state of @p __x inserted or in
1147 * an error state.
1148 */
1149 template<typename _CharT, typename _Traits>
1150 friend std::basic_ostream<_CharT, _Traits>&
1151 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1152 const xor_combine& __x)
1153 { return __os << __x.base1() << " " << __x.base2(); }
1154
1155 /**
1156 * Extracts the current state of a %xor_combine random number
1157 * generator engine @p __x from the input stream @p __is.
1158 *
1159 * @param __is An input stream.
1160 * @param __x A %xor_combine random number generator engine.
1161 *
1162 * @returns The input stream with the state of @p __x extracted or in
1163 * an error state.
1164 */
1165 template<typename _CharT, typename _Traits>
1166 friend std::basic_istream<_CharT, _Traits>&
1167 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1168 xor_combine& __x)
1169 { return __is >> __x._M_b1 >> __x._M_b2; }
1170
1171 private:
1172 base1_type _M_b1;
1173 base2_type _M_b2;
1174 };
1175
1176
1177 /**
1178 * A standard interface to a platform-specific non-deterministic random number
1179 * generator (if any are available).
1180 */
1181 class random_device
1182 {
1183 public:
1184 // types
1185 typedef unsigned int result_type;
1186
1187 // constructors, destructors and member functions
1188
1189 #ifdef _GLIBCXX_USE_RANDOM_TR1
1190
1191 explicit
1192 random_device(const std::string& __token = "/dev/urandom")
1193 {
1194 if ((__token != "/dev/urandom" && __token != "/dev/random")
1195 || !_M_filebuf.open(__token.c_str(),
1196 std::ios_base::in | std::ios_base::binary))
1197 std::__throw_runtime_error(__N("random_device::"
1198 "random_device(const std::string&)"));
1199 }
1200
1201 ~random_device()
1202 { _M_filebuf.close(); }
1203
1204 #else
1205
1206 explicit
1207 random_device(const std::string& __token = "mt19937")
1208 : _M_mt(_M_strtoul(__token)) { }
1209
1210 private:
1211 static unsigned long
1212 _M_strtoul(const std::string& __str)
1213 {
1214 unsigned long __ret = 5489UL;
1215 if (__str != "mt19937")
1216 {
1217 const char* __nptr = __str.c_str();
1218 char* __endptr;
1219 __ret = std::strtoul(__nptr, &__endptr, 0);
1220 if (*__nptr == '\0' || *__endptr != '\0')
1221 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1222 "(const std::string&)"));
1223 }
1224 return __ret;
1225 }
1226
1227 public:
1228
1229 #endif
1230
1231 result_type
1232 min() const
1233 { return std::numeric_limits<result_type>::min(); }
1234
1235 result_type
1236 max() const
1237 { return std::numeric_limits<result_type>::max(); }
1238
1239 double
1240 entropy() const
1241 { return 0.0; }
1242
1243 result_type
1244 operator()()
1245 {
1246 #ifdef _GLIBCXX_USE_RANDOM_TR1
1247 result_type __ret;
1248 _M_filebuf.sgetn(reinterpret_cast<char*>(&__ret), sizeof(result_type));
1249 return __ret;
1250 #else
1251 return _M_mt();
1252 #endif
1253 }
1254
1255 private:
1256 random_device(const random_device&);
1257 void operator=(const random_device&);
1258
1259 #ifdef _GLIBCXX_USE_RANDOM_TR1
1260 std::filebuf _M_filebuf;
1261 #else
1262 mt19937 _M_mt;
1263 #endif
1264 };
1265
1266 /* @} */ // group tr1_random_generators
1267
1268 /**
1269 * @addtogroup tr1_random_distributions Random Number Distributions
1270 * @ingroup tr1_random
1271 * @{
1272 */
1273
1274 /**
1275 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1276 * @ingroup tr1_random_distributions
1277 * @{
1278 */
1279
1280 /**
1281 * @brief Uniform discrete distribution for random numbers.
1282 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1283 * probability throughout the range.
1284 */
1285 template<typename _IntType = int>
1286 class uniform_int
1287 {
1288 __glibcxx_class_requires(_IntType, _IntegerConcept)
1289
1290 public:
1291 /** The type of the parameters of the distribution. */
1292 typedef _IntType input_type;
1293 /** The type of the range of the distribution. */
1294 typedef _IntType result_type;
1295
1296 public:
1297 /**
1298 * Constructs a uniform distribution object.
1299 */
1300 explicit
1301 uniform_int(_IntType __min = 0, _IntType __max = 9)
1302 : _M_min(__min), _M_max(__max)
1303 {
1304 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1305 }
1306
1307 /**
1308 * Gets the inclusive lower bound of the distribution range.
1309 */
1310 result_type
1311 min() const
1312 { return _M_min; }
1313
1314 /**
1315 * Gets the inclusive upper bound of the distribution range.
1316 */
1317 result_type
1318 max() const
1319 { return _M_max; }
1320
1321 /**
1322 * Resets the distribution state.
1323 *
1324 * Does nothing for the uniform integer distribution.
1325 */
1326 void
1327 reset() { }
1328
1329 /**
1330 * Gets a uniformly distributed random number in the range
1331 * @f$(min, max)@f$.
1332 */
1333 template<typename _UniformRandomNumberGenerator>
1334 result_type
1335 operator()(_UniformRandomNumberGenerator& __urng)
1336 { return (__urng() % (_M_max - _M_min + 1)) + _M_min; }
1337
1338 /**
1339 * Gets a uniform random number in the range @f$[0, n)@f$.
1340 *
1341 * This function is aimed at use with std::random_shuffle.
1342 */
1343 template<typename _UniformRandomNumberGenerator>
1344 result_type
1345 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1346 { return __urng() % __n; }
1347
1348 /**
1349 * Inserts a %uniform_int random number distribution @p __x into the
1350 * output stream @p os.
1351 *
1352 * @param __os An output stream.
1353 * @param __x A %uniform_int random number distribution.
1354 *
1355 * @returns The output stream with the state of @p __x inserted or in
1356 * an error state.
1357 */
1358 template<typename _CharT, typename _Traits>
1359 friend std::basic_ostream<_CharT, _Traits>&
1360 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1361 const uniform_int& __x)
1362 { return __os << __x.min() << " " << __x.max(); }
1363
1364 /**
1365 * Extracts a %unform_int random number distribution
1366 * @p __u from the input stream @p __is.
1367 *
1368 * @param __is An input stream.
1369 * @param __u A %uniform_int random number generator engine.
1370 *
1371 * @returns The input stream with @p __u extracted or in an error state.
1372 */
1373 template<typename _CharT, typename _Traits>
1374 friend std::basic_istream<_CharT, _Traits>&
1375 operator>>(std::basic_istream<_CharT, _Traits>& __is, uniform_int& __u)
1376 { return __is >> __u._M_min >> __u._M_max; }
1377
1378 private:
1379 _IntType _M_min;
1380 _IntType _M_max;
1381 };
1382
1383
1384 /**
1385 * @brief A Bernoulli random number distribution.
1386 *
1387 * Generates a sequence of true and false values with likelihood @f$ p @f$
1388 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1389 */
1390 class bernoulli_distribution
1391 {
1392 public:
1393 typedef int input_type;
1394 typedef bool result_type;
1395
1396 public:
1397 /**
1398 * Constructs a Bernoulli distribution with likelihood @p p.
1399 *
1400 * @param __p [IN] The likelihood of a true result being returned. Must
1401 * be in the interval @f$ [0, 1] @f$.
1402 */
1403 explicit
1404 bernoulli_distribution(double __p = 0.5)
1405 : _M_p(__p)
1406 {
1407 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1408 }
1409
1410 /**
1411 * Gets the @p p parameter of the distribution.
1412 */
1413 double
1414 p() const
1415 { return _M_p; }
1416
1417 /**
1418 * Resets the distribution state.
1419 *
1420 * Does nothing for a bernoulli distribution.
1421 */
1422 void
1423 reset() { }
1424
1425 /**
1426 * Gets the next value in the Bernoullian sequence.
1427 */
1428 template<class UniformRandomNumberGenerator>
1429 result_type
1430 operator()(UniformRandomNumberGenerator& __urng)
1431 {
1432 if (__urng() < _M_p)
1433 return true;
1434 return false;
1435 }
1436
1437 /**
1438 * Inserts a %bernoulli_distribution random number distribution
1439 * @p __x into the output stream @p __os.
1440 *
1441 * @param __os An output stream.
1442 * @param __x A %bernoulli_distribution random number distribution.
1443 *
1444 * @returns The output stream with the state of @p __x inserted or in
1445 * an error state.
1446 */
1447 template<typename _CharT, typename _Traits>
1448 friend std::basic_ostream<_CharT, _Traits>&
1449 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1450 const bernoulli_distribution& __x)
1451 { return __os << __x.p(); }
1452
1453 /**
1454 * Extracts a %bernoulli_distribution random number distribution
1455 * @p __u from the input stream @p __is.
1456 *
1457 * @param __is An input stream.
1458 * @param __u A %bernoulli_distribution random number generator engine.
1459 *
1460 * @returns The input stream with @p __u extracted or in an error state.
1461 */
1462 template<typename _CharT, typename _Traits>
1463 friend std::basic_istream<_CharT, _Traits>&
1464 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1465 bernoulli_distribution& __u)
1466 { return __is >> __u._M_p; }
1467
1468 protected:
1469 double _M_p;
1470 };
1471
1472
1473 /**
1474 * @brief A discrete geometric random number distribution.
1475 *
1476 * The formula for the geometric probability mass function is
1477 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1478 * distribution.
1479 */
1480 template<typename _IntType = int, typename _RealType = double>
1481 class geometric_distribution
1482 {
1483 public:
1484 // types
1485 typedef _RealType input_type;
1486 typedef _IntType result_type;
1487
1488 // constructors and member function
1489
1490 explicit
1491 geometric_distribution(const _RealType& __p = _RealType(0.5))
1492 : _M_p(__p), _M_log_p(std::log(_M_p))
1493 {
1494 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1495 }
1496
1497 /**
1498 * Gets the distribution parameter @p p.
1499 */
1500 _RealType
1501 p() const
1502 { return _M_p; }
1503
1504 void
1505 reset() { }
1506
1507 template<class _UniformRandomNumberGenerator>
1508 result_type
1509 operator()(_UniformRandomNumberGenerator& __urng)
1510 { return result_type(std::ceil(std::log(__urng()) / _M_log_p)); }
1511
1512 /**
1513 * Inserts a %geometric_distribution random number distribution
1514 * @p __x into the output stream @p __os.
1515 *
1516 * @param __os An output stream.
1517 * @param __x A %geometric_distribution random number distribution.
1518 *
1519 * @returns The output stream with the state of @p __x inserted or in
1520 * an error state.
1521 */
1522 template<typename _CharT, typename _Traits>
1523 friend std::basic_ostream<_CharT, _Traits>&
1524 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1525 const geometric_distribution& __x)
1526 { return __os << __x.p(); }
1527
1528 /**
1529 * Extracts a %geometric_distribution random number distribution
1530 * @p __u from the input stream @p __is.
1531 *
1532 * @param __is An input stream.
1533 * @param __u A %geometric_distribution random number generator engine.
1534 *
1535 * @returns The input stream with @p __u extracted or in an error state.
1536 */
1537 template<typename _CharT, typename _Traits>
1538 friend std::basic_istream<_CharT, _Traits>&
1539 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1540 geometric_distribution& __u)
1541 {
1542 __is >> __u._M_p;
1543 __u._M_log_p = std::log(__u._M_p);
1544 return __is;
1545 }
1546
1547 protected:
1548 _RealType _M_p;
1549 _RealType _M_log_p;
1550 };
1551
1552 /* @} */ // group tr1_random_distributions_discrete
1553
1554 /**
1555 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
1556 * @ingroup tr1_random_distributions
1557 * @{
1558 */
1559
1560 /**
1561 * @brief Uniform continuous distribution for random numbers.
1562 *
1563 * A continuous random distribution on the range [min, max) with equal
1564 * probability throughout the range. The URNG should be real-valued and
1565 * deliver number in the range [0, 1).
1566 */
1567 template<typename _RealType = double>
1568 class uniform_real
1569 {
1570 public:
1571 // types
1572 typedef _RealType input_type;
1573 typedef _RealType result_type;
1574
1575 public:
1576 /**
1577 * Constructs a uniform_real object.
1578 *
1579 * @param __min [IN] The lower bound of the distribution.
1580 * @param __max [IN] The upper bound of the distribution.
1581 */
1582 explicit
1583 uniform_real(_RealType __min = _RealType(0),
1584 _RealType __max = _RealType(1))
1585 : _M_min(__min), _M_max(__max)
1586 {
1587 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1588 }
1589
1590 result_type
1591 min() const
1592 { return _M_min; }
1593
1594 result_type
1595 max() const
1596 { return _M_max; }
1597
1598 void
1599 reset() { }
1600
1601 template<class _UniformRandomNumberGenerator>
1602 result_type
1603 operator()(_UniformRandomNumberGenerator& __urng)
1604 { return (__urng() * (_M_max - _M_min)) + _M_min; }
1605
1606 /**
1607 * Inserts a %uniform_real random number distribution @p __x into the
1608 * output stream @p __os.
1609 *
1610 * @param __os An output stream.
1611 * @param __x A %uniform_real random number distribution.
1612 *
1613 * @returns The output stream with the state of @p __x inserted or in
1614 * an error state.
1615 */
1616 template<typename _CharT, typename _Traits>
1617 friend std::basic_ostream<_CharT, _Traits>&
1618 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1619 const uniform_real& __x)
1620 { return __os << __x.min() << " " << __x.max(); }
1621
1622 /**
1623 * Extracts a %unform_real random number distribution
1624 * @p __u from the input stream @p __is.
1625 *
1626 * @param __is An input stream.
1627 * @param __u A %uniform_real random number generator engine.
1628 *
1629 * @returns The input stream with @p __u extracted or in an error state.
1630 */
1631 template<typename _CharT, typename _Traits>
1632 friend std::basic_istream<_CharT, _Traits>&
1633 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1634 uniform_real& __u)
1635 { return __is >> __u._M_min >> __u._M_max; }
1636
1637 protected:
1638 _RealType _M_min;
1639 _RealType _M_max;
1640 };
1641
1642
1643 /**
1644 * @brief An exponential continuous distribution for random numbers.
1645 *
1646 * The formula for the exponential probability mass function is
1647 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
1648 *
1649 * <table border=1 cellpadding=10 cellspacing=0>
1650 * <caption align=top>Distribution Statistics</caption>
1651 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
1652 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
1653 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
1654 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
1655 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
1656 * </table>
1657 */
1658 template<typename _RealType = double>
1659 class exponential_distribution
1660 {
1661 public:
1662 // types
1663 typedef _RealType input_type;
1664 typedef _RealType result_type;
1665
1666 public:
1667 /**
1668 * Constructs an exponential distribution with inverse scale parameter
1669 * @f$ \lambda @f$.
1670 */
1671 explicit
1672 exponential_distribution(const result_type& __lambda = result_type(1))
1673 : _M_lambda(__lambda)
1674 {
1675 _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
1676 }
1677
1678 /**
1679 * Gets the inverse scale parameter of the distribution.
1680 */
1681 _RealType
1682 lambda() const
1683 { return _M_lambda; }
1684
1685 /**
1686 * Resets the distribution.
1687 *
1688 * Has no effect on exponential distributions.
1689 */
1690 void
1691 reset() { }
1692
1693 template<class _UniformRandomNumberGenerator>
1694 result_type
1695 operator()(_UniformRandomNumberGenerator& __urng)
1696 { return -std::log(__urng()) / _M_lambda; }
1697
1698 /**
1699 * Inserts a %exponential_distribution random number distribution
1700 * @p __x into the output stream @p __os.
1701 *
1702 * @param __os An output stream.
1703 * @param __x A %exponential_distribution random number distribution.
1704 *
1705 * @returns The output stream with the state of @p __x inserted or in
1706 * an error state.
1707 */
1708 template<typename _CharT, typename _Traits>
1709 friend std::basic_ostream<_CharT, _Traits>&
1710 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1711 const exponential_distribution& __x)
1712 { return __os << __x.lambda(); }
1713
1714 /**
1715 * Extracts a %exponential_distribution random number distribution
1716 * @p __u from the input stream @p __is.
1717 *
1718 * @param __is An input stream.
1719 * @param __u A %exponential_distribution random number generator engine.
1720 *
1721 * @returns The input stream with @p __u extracted or in an error state.
1722 */
1723 template<typename _CharT, typename _Traits>
1724 friend std::basic_istream<_CharT, _Traits>&
1725 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1726 exponential_distribution& __u)
1727 { return __is >> __u._M_lambda; }
1728
1729 private:
1730 result_type _M_lambda;
1731 };
1732
1733
1734 /**
1735 * @brief A normal continuous distribution for random numbers.
1736 *
1737 * The formula for the normal probability mass function is
1738 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
1739 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
1740 */
1741 template<typename _RealType = double>
1742 class normal_distribution
1743 {
1744 public:
1745 // types
1746 typedef _RealType input_type;
1747 typedef _RealType result_type;
1748
1749 public:
1750 /**
1751 * Constructs a normal distribution with parameters @f$ mean @f$ and
1752 * @f$ \sigma @f$.
1753 */
1754 explicit
1755 normal_distribution(const result_type& __mean = result_type(0),
1756 const result_type& __sigma = result_type(1))
1757 : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
1758 {
1759 _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
1760 }
1761
1762 /**
1763 * Gets the mean of the distribution.
1764 */
1765 _RealType
1766 mean() const
1767 { return _M_mean; }
1768
1769 /**
1770 * Gets the @f$ \sigma @f$ of the distribution.
1771 */
1772 _RealType
1773 sigma() const
1774 { return _M_sigma; }
1775
1776 /**
1777 * Resets the distribution.
1778 */
1779 void
1780 reset()
1781 { _M_saved_available = false; }
1782
1783 template<class _UniformRandomNumberGenerator>
1784 result_type
1785 operator()(_UniformRandomNumberGenerator& __urng);
1786
1787 /**
1788 * Inserts a %normal_distribution random number distribution
1789 * @p __x into the output stream @p __os.
1790 *
1791 * @param __os An output stream.
1792 * @param __x A %normal_distribution random number distribution.
1793 *
1794 * @returns The output stream with the state of @p __x inserted or in
1795 * an error state.
1796 */
1797 template<typename _CharT, typename _Traits>
1798 friend std::basic_ostream<_CharT, _Traits>&
1799 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1800 const normal_distribution& __x)
1801 {
1802 return __os << __x.mean() << " " << __x.sigma()
1803 << " " << __x._M_saved << " " << __x._M_saved_available;
1804 }
1805
1806 /**
1807 * Extracts a %normal_distribution random number distribution
1808 * @p __u from the input stream @p __is.
1809 *
1810 * @param __is An input stream.
1811 * @param __u A %normal_distribution random number generator engine.
1812 *
1813 * @returns The input stream with @p __u extracted or in an error state.
1814 */
1815 template<typename _CharT, typename _Traits>
1816 friend std::basic_istream<_CharT, _Traits>&
1817 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1818 normal_distribution& __u)
1819 {
1820 return __is >> __u._M_mean >> __u._M_sigma
1821 >> __u._M_saved >> __u._M_saved_available;
1822 }
1823
1824 private:
1825 result_type _M_mean;
1826 result_type _M_sigma;
1827 result_type _M_saved;
1828 bool _M_saved_available;
1829 };
1830
1831 /* @} */ // group tr1_random_distributions_continuous
1832 /* @} */ // group tr1_random_distributions
1833 /* @} */ // group tr1_random
1834
1835 _GLIBCXX_END_NAMESPACE
1836 }
1837
1838 #include <tr1/random.tcc>
1839
1840 #endif // _STD_TR1_RANDOM