Update copyright in libstdc++-v3.
[official-gcc.git] / libstdc++-v3 / include / tr1 / random.h
blob9ef953e6adb4d1a59d2837a49e8ee4648d9ecdb9
1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009-2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
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.
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.
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/>.
25 /**
26 * @file tr1/random.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{tr1/random}
31 #ifndef _GLIBCXX_TR1_RANDOM_H
32 #define _GLIBCXX_TR1_RANDOM_H 1
34 #pragma GCC system_header
36 namespace std _GLIBCXX_VISIBILITY(default)
38 namespace tr1
40 // [5.1] Random number generation
42 /**
43 * @addtogroup tr1_random Random Number Generation
44 * A facility for generating random numbers on selected distributions.
45 * @{
49 * Implementation-space details.
51 namespace __detail
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 template<typename _UIntType, int __w,
56 bool = __w < std::numeric_limits<_UIntType>::digits>
57 struct _Shift
58 { static const _UIntType __value = 0; };
60 template<typename _UIntType, int __w>
61 struct _Shift<_UIntType, __w, true>
62 { static const _UIntType __value = _UIntType(1) << __w; };
64 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
65 struct _Mod;
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); }
74 typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
75 unsigned, unsigned long>::__type _UInt32Type;
78 * An adaptor class for converting the output of any Generator into
79 * the input for a specific Distribution.
81 template<typename _Engine, typename _Distribution>
82 struct _Adaptor
84 typedef typename remove_reference<_Engine>::type _BEngine;
85 typedef typename _BEngine::result_type _Engine_result_type;
86 typedef typename _Distribution::input_type result_type;
88 public:
89 _Adaptor(const _Engine& __g)
90 : _M_g(__g) { }
92 result_type
93 min() const
95 result_type __return_value;
96 if (is_integral<_Engine_result_type>::value
97 && is_integral<result_type>::value)
98 __return_value = _M_g.min();
99 else
100 __return_value = result_type(0);
101 return __return_value;
104 result_type
105 max() const
107 result_type __return_value;
108 if (is_integral<_Engine_result_type>::value
109 && is_integral<result_type>::value)
110 __return_value = _M_g.max();
111 else if (!is_integral<result_type>::value)
112 __return_value = result_type(1);
113 else
114 __return_value = std::numeric_limits<result_type>::max() - 1;
115 return __return_value;
119 * Converts a value generated by the adapted random number generator
120 * into a value in the input domain for the dependent random number
121 * distribution.
123 * Because the type traits are compile time constants only the
124 * appropriate clause of the if statements will actually be emitted
125 * by the compiler.
127 result_type
128 operator()()
130 result_type __return_value;
131 if (is_integral<_Engine_result_type>::value
132 && is_integral<result_type>::value)
133 __return_value = _M_g();
134 else if (!is_integral<_Engine_result_type>::value
135 && !is_integral<result_type>::value)
136 __return_value = result_type(_M_g() - _M_g.min())
137 / result_type(_M_g.max() - _M_g.min());
138 else if (is_integral<_Engine_result_type>::value
139 && !is_integral<result_type>::value)
140 __return_value = result_type(_M_g() - _M_g.min())
141 / result_type(_M_g.max() - _M_g.min() + result_type(1));
142 else
143 __return_value = (((_M_g() - _M_g.min())
144 / (_M_g.max() - _M_g.min()))
145 * std::numeric_limits<result_type>::max());
146 return __return_value;
149 private:
150 _Engine _M_g;
153 // Specialization for _Engine*.
154 template<typename _Engine, typename _Distribution>
155 struct _Adaptor<_Engine*, _Distribution>
157 typedef typename _Engine::result_type _Engine_result_type;
158 typedef typename _Distribution::input_type result_type;
160 public:
161 _Adaptor(_Engine* __g)
162 : _M_g(__g) { }
164 result_type
165 min() const
167 result_type __return_value;
168 if (is_integral<_Engine_result_type>::value
169 && is_integral<result_type>::value)
170 __return_value = _M_g->min();
171 else
172 __return_value = result_type(0);
173 return __return_value;
176 result_type
177 max() const
179 result_type __return_value;
180 if (is_integral<_Engine_result_type>::value
181 && is_integral<result_type>::value)
182 __return_value = _M_g->max();
183 else if (!is_integral<result_type>::value)
184 __return_value = result_type(1);
185 else
186 __return_value = std::numeric_limits<result_type>::max() - 1;
187 return __return_value;
190 result_type
191 operator()()
193 result_type __return_value;
194 if (is_integral<_Engine_result_type>::value
195 && is_integral<result_type>::value)
196 __return_value = (*_M_g)();
197 else if (!is_integral<_Engine_result_type>::value
198 && !is_integral<result_type>::value)
199 __return_value = result_type((*_M_g)() - _M_g->min())
200 / result_type(_M_g->max() - _M_g->min());
201 else if (is_integral<_Engine_result_type>::value
202 && !is_integral<result_type>::value)
203 __return_value = result_type((*_M_g)() - _M_g->min())
204 / result_type(_M_g->max() - _M_g->min() + result_type(1));
205 else
206 __return_value = ((((*_M_g)() - _M_g->min())
207 / (_M_g->max() - _M_g->min()))
208 * std::numeric_limits<result_type>::max());
209 return __return_value;
212 private:
213 _Engine* _M_g;
216 _GLIBCXX_END_NAMESPACE_VERSION
217 } // namespace __detail
219 _GLIBCXX_BEGIN_NAMESPACE_VERSION
222 * Produces random numbers on a given distribution function using a
223 * non-uniform random number generation engine.
225 * @todo the engine_value_type needs to be studied more carefully.
227 template<typename _Engine, typename _Dist>
228 class variate_generator
230 // Concept requirements.
231 __glibcxx_class_requires(_Engine, _CopyConstructibleConcept)
232 // __glibcxx_class_requires(_Engine, _EngineConcept)
233 // __glibcxx_class_requires(_Dist, _EngineConcept)
235 public:
236 typedef _Engine engine_type;
237 typedef __detail::_Adaptor<_Engine, _Dist> engine_value_type;
238 typedef _Dist distribution_type;
239 typedef typename _Dist::result_type result_type;
241 // tr1:5.1.1 table 5.1 requirement
242 typedef typename __gnu_cxx::__enable_if<
243 is_arithmetic<result_type>::value, result_type>::__type _IsValidType;
246 * Constructs a variate generator with the uniform random number
247 * generator @p __eng for the random distribution @p __dist.
249 * @throws Any exceptions which may thrown by the copy constructors of
250 * the @p _Engine or @p _Dist objects.
252 variate_generator(engine_type __eng, distribution_type __dist)
253 : _M_engine(__eng), _M_dist(__dist) { }
256 * Gets the next generated value on the distribution.
258 result_type
259 operator()()
260 { return _M_dist(_M_engine); }
263 * WTF?
265 template<typename _Tp>
266 result_type
267 operator()(_Tp __value)
268 { return _M_dist(_M_engine, __value); }
271 * Gets a reference to the underlying uniform random number generator
272 * object.
274 engine_value_type&
275 engine()
276 { return _M_engine; }
279 * Gets a const reference to the underlying uniform random number
280 * generator object.
282 const engine_value_type&
283 engine() const
284 { return _M_engine; }
287 * Gets a reference to the underlying random distribution.
289 distribution_type&
290 distribution()
291 { return _M_dist; }
294 * Gets a const reference to the underlying random distribution.
296 const distribution_type&
297 distribution() const
298 { return _M_dist; }
301 * Gets the closed lower bound of the distribution interval.
303 result_type
304 min() const
305 { return this->distribution().min(); }
308 * Gets the closed upper bound of the distribution interval.
310 result_type
311 max() const
312 { return this->distribution().max(); }
314 private:
315 engine_value_type _M_engine;
316 distribution_type _M_dist;
321 * @addtogroup tr1_random_generators Random Number Generators
322 * @ingroup tr1_random
324 * These classes define objects which provide random or pseudorandom
325 * numbers, either from a discrete or a continuous interval. The
326 * random number generator supplied as a part of this library are
327 * all uniform random number generators which provide a sequence of
328 * random number uniformly distributed over their range.
330 * A number generator is a function object with an operator() that
331 * takes zero arguments and returns a number.
333 * A compliant random number generator must satisfy the following
334 * requirements. <table border=1 cellpadding=10 cellspacing=0>
335 * <caption align=top>Random Number Generator Requirements</caption>
336 * <tr><td>To be documented.</td></tr> </table>
338 * @{
342 * @brief A model of a linear congruential random number generator.
344 * A random number generator that produces pseudorandom numbers using the
345 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
347 * The template parameter @p _UIntType must be an unsigned integral type
348 * large enough to store values up to (__m-1). If the template parameter
349 * @p __m is 0, the modulus @p __m used is
350 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
351 * parameters @p __a and @p __c must be less than @p __m.
353 * The size of the state is @f$ 1 @f$.
355 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
356 class linear_congruential
358 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
359 // __glibcpp_class_requires(__a < __m && __c < __m)
361 public:
362 /** The type of the generated random value. */
363 typedef _UIntType result_type;
365 /** The multiplier. */
366 static const _UIntType multiplier = __a;
367 /** An increment. */
368 static const _UIntType increment = __c;
369 /** The modulus. */
370 static const _UIntType modulus = __m;
373 * Constructs a %linear_congruential random number generator engine with
374 * seed @p __s. The default seed value is 1.
376 * @param __s The initial seed value.
378 explicit
379 linear_congruential(unsigned long __x0 = 1)
380 { this->seed(__x0); }
383 * Constructs a %linear_congruential random number generator engine
384 * seeded from the generator function @p __g.
386 * @param __g The seed generator function.
388 template<class _Gen>
389 linear_congruential(_Gen& __g)
390 { this->seed(__g); }
393 * Reseeds the %linear_congruential random number generator engine
394 * sequence to the seed @g __s.
396 * @param __s The new seed.
398 void
399 seed(unsigned long __s = 1);
402 * Reseeds the %linear_congruential random number generator engine
403 * sequence using values from the generator function @p __g.
405 * @param __g the seed generator function.
407 template<class _Gen>
408 void
409 seed(_Gen& __g)
410 { seed(__g, typename is_fundamental<_Gen>::type()); }
413 * Gets the smallest possible value in the output range.
415 * The minimum depends on the @p __c parameter: if it is zero, the
416 * minimum generated must be > 0, otherwise 0 is allowed.
418 result_type
419 min() const
420 { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
423 * Gets the largest possible value in the output range.
425 result_type
426 max() const
427 { return __m - 1; }
430 * Gets the next random number in the sequence.
432 result_type
433 operator()();
436 * Compares two linear congruential random number generator
437 * objects of the same type for equality.
439 * @param __lhs A linear congruential random number generator object.
440 * @param __rhs Another linear congruential random number generator obj.
442 * @returns true if the two objects are equal, false otherwise.
444 friend bool
445 operator==(const linear_congruential& __lhs,
446 const linear_congruential& __rhs)
447 { return __lhs._M_x == __rhs._M_x; }
450 * Compares two linear congruential random number generator
451 * objects of the same type for inequality.
453 * @param __lhs A linear congruential random number generator object.
454 * @param __rhs Another linear congruential random number generator obj.
456 * @returns true if the two objects are not equal, false otherwise.
458 friend bool
459 operator!=(const linear_congruential& __lhs,
460 const linear_congruential& __rhs)
461 { return !(__lhs == __rhs); }
464 * Writes the textual representation of the state x(i) of x to @p __os.
466 * @param __os The output stream.
467 * @param __lcr A % linear_congruential random number generator.
468 * @returns __os.
470 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
471 _UIntType1 __m1,
472 typename _CharT, typename _Traits>
473 friend std::basic_ostream<_CharT, _Traits>&
474 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
475 const linear_congruential<_UIntType1, __a1, __c1,
476 __m1>& __lcr);
479 * Sets the state of the engine by reading its textual
480 * representation from @p __is.
482 * The textual representation must have been previously written using an
483 * output stream whose imbued locale and whose type's template
484 * specialization arguments _CharT and _Traits were the same as those of
485 * @p __is.
487 * @param __is The input stream.
488 * @param __lcr A % linear_congruential random number generator.
489 * @returns __is.
491 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
492 _UIntType1 __m1,
493 typename _CharT, typename _Traits>
494 friend std::basic_istream<_CharT, _Traits>&
495 operator>>(std::basic_istream<_CharT, _Traits>& __is,
496 linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
498 private:
499 template<class _Gen>
500 void
501 seed(_Gen& __g, true_type)
502 { return seed(static_cast<unsigned long>(__g)); }
504 template<class _Gen>
505 void
506 seed(_Gen& __g, false_type);
508 _UIntType _M_x;
512 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
514 typedef linear_congruential<unsigned long, 16807, 0, 2147483647> minstd_rand0;
517 * An alternative LCR (Lehmer Generator function) .
519 typedef linear_congruential<unsigned long, 48271, 0, 2147483647> minstd_rand;
523 * A generalized feedback shift register discrete random number generator.
525 * This algorithm avoids multiplication and division and is designed to be
526 * friendly to a pipelined architecture. If the parameters are chosen
527 * correctly, this generator will produce numbers with a very long period and
528 * fairly good apparent entropy, although still not cryptographically strong.
530 * The best way to use this generator is with the predefined mt19937 class.
532 * This algorithm was originally invented by Makoto Matsumoto and
533 * Takuji Nishimura.
535 * @var word_size The number of bits in each element of the state vector.
536 * @var state_size The degree of recursion.
537 * @var shift_size The period parameter.
538 * @var mask_bits The separation point bit index.
539 * @var parameter_a The last row of the twist matrix.
540 * @var output_u The first right-shift tempering matrix parameter.
541 * @var output_s The first left-shift tempering matrix parameter.
542 * @var output_b The first left-shift tempering matrix mask.
543 * @var output_t The second left-shift tempering matrix parameter.
544 * @var output_c The second left-shift tempering matrix mask.
545 * @var output_l The second right-shift tempering matrix parameter.
547 template<class _UIntType, int __w, int __n, int __m, int __r,
548 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
549 _UIntType __c, int __l>
550 class mersenne_twister
552 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
554 public:
555 // types
556 typedef _UIntType result_type;
558 // parameter values
559 static const int word_size = __w;
560 static const int state_size = __n;
561 static const int shift_size = __m;
562 static const int mask_bits = __r;
563 static const _UIntType parameter_a = __a;
564 static const int output_u = __u;
565 static const int output_s = __s;
566 static const _UIntType output_b = __b;
567 static const int output_t = __t;
568 static const _UIntType output_c = __c;
569 static const int output_l = __l;
571 // constructors and member function
572 mersenne_twister()
573 { seed(); }
575 explicit
576 mersenne_twister(unsigned long __value)
577 { seed(__value); }
579 template<class _Gen>
580 mersenne_twister(_Gen& __g)
581 { seed(__g); }
583 void
584 seed()
585 { seed(5489UL); }
587 void
588 seed(unsigned long __value);
590 template<class _Gen>
591 void
592 seed(_Gen& __g)
593 { seed(__g, typename is_fundamental<_Gen>::type()); }
595 result_type
596 min() const
597 { return 0; };
599 result_type
600 max() const
601 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
603 result_type
604 operator()();
607 * Compares two % mersenne_twister random number generator objects of
608 * the same type for equality.
610 * @param __lhs A % mersenne_twister random number generator object.
611 * @param __rhs Another % mersenne_twister random number generator
612 * object.
614 * @returns true if the two objects are equal, false otherwise.
616 friend bool
617 operator==(const mersenne_twister& __lhs,
618 const mersenne_twister& __rhs)
619 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
622 * Compares two % mersenne_twister random number generator objects of
623 * the same type for inequality.
625 * @param __lhs A % mersenne_twister random number generator object.
626 * @param __rhs Another % mersenne_twister random number generator
627 * object.
629 * @returns true if the two objects are not equal, false otherwise.
631 friend bool
632 operator!=(const mersenne_twister& __lhs,
633 const mersenne_twister& __rhs)
634 { return !(__lhs == __rhs); }
637 * Inserts the current state of a % mersenne_twister random number
638 * generator engine @p __x into the output stream @p __os.
640 * @param __os An output stream.
641 * @param __x A % mersenne_twister random number generator engine.
643 * @returns The output stream with the state of @p __x inserted or in
644 * an error state.
646 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
647 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
648 _UIntType1 __c1, int __l1,
649 typename _CharT, typename _Traits>
650 friend std::basic_ostream<_CharT, _Traits>&
651 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
652 const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
653 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
656 * Extracts the current state of a % mersenne_twister random number
657 * generator engine @p __x from the input stream @p __is.
659 * @param __is An input stream.
660 * @param __x A % mersenne_twister random number generator engine.
662 * @returns The input stream with the state of @p __x extracted or in
663 * an error state.
665 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
666 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
667 _UIntType1 __c1, int __l1,
668 typename _CharT, typename _Traits>
669 friend std::basic_istream<_CharT, _Traits>&
670 operator>>(std::basic_istream<_CharT, _Traits>& __is,
671 mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
672 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
674 private:
675 template<class _Gen>
676 void
677 seed(_Gen& __g, true_type)
678 { return seed(static_cast<unsigned long>(__g)); }
680 template<class _Gen>
681 void
682 seed(_Gen& __g, false_type);
684 _UIntType _M_x[state_size];
685 int _M_p;
689 * The classic Mersenne Twister.
691 * Reference:
692 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
693 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
694 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
696 typedef mersenne_twister<
697 unsigned long, 32, 624, 397, 31,
698 0x9908b0dful, 11, 7,
699 0x9d2c5680ul, 15,
700 0xefc60000ul, 18
701 > mt19937;
705 * @brief The Marsaglia-Zaman generator.
707 * This is a model of a Generalized Fibonacci discrete random number
708 * generator, sometimes referred to as the SWC generator.
710 * A discrete random number generator that produces pseudorandom
711 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
712 * carry_{i-1}) \bmod m @f$.
714 * The size of the state is @f$ r @f$
715 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
717 * N1688[4.13] says <em>the template parameter _IntType shall denote
718 * an integral type large enough to store values up to m</em>.
720 * @var _M_x The state of the generator. This is a ring buffer.
721 * @var _M_carry The carry.
722 * @var _M_p Current index of x(i - r).
724 template<typename _IntType, _IntType __m, int __s, int __r>
725 class subtract_with_carry
727 __glibcxx_class_requires(_IntType, _IntegerConcept)
729 public:
730 /** The type of the generated random value. */
731 typedef _IntType result_type;
733 // parameter values
734 static const _IntType modulus = __m;
735 static const int long_lag = __r;
736 static const int short_lag = __s;
739 * Constructs a default-initialized % subtract_with_carry random number
740 * generator.
742 subtract_with_carry()
743 { this->seed(); }
746 * Constructs an explicitly seeded % subtract_with_carry random number
747 * generator.
749 explicit
750 subtract_with_carry(unsigned long __value)
751 { this->seed(__value); }
754 * Constructs a %subtract_with_carry random number generator engine
755 * seeded from the generator function @p __g.
757 * @param __g The seed generator function.
759 template<class _Gen>
760 subtract_with_carry(_Gen& __g)
761 { this->seed(__g); }
764 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
766 * N1688[4.19] modifies this as follows. If @p __value == 0,
767 * sets value to 19780503. In any case, with a linear
768 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
769 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
770 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
771 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
772 * set carry to 1, otherwise sets carry to 0.
774 void
775 seed(unsigned long __value = 19780503);
778 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
779 * random number generator.
781 template<class _Gen>
782 void
783 seed(_Gen& __g)
784 { seed(__g, typename is_fundamental<_Gen>::type()); }
787 * Gets the inclusive minimum value of the range of random integers
788 * returned by this generator.
790 result_type
791 min() const
792 { return 0; }
795 * Gets the inclusive maximum value of the range of random integers
796 * returned by this generator.
798 result_type
799 max() const
800 { return this->modulus - 1; }
803 * Gets the next random number in the sequence.
805 result_type
806 operator()();
809 * Compares two % subtract_with_carry random number generator objects of
810 * the same type for equality.
812 * @param __lhs A % subtract_with_carry random number generator object.
813 * @param __rhs Another % subtract_with_carry random number generator
814 * object.
816 * @returns true if the two objects are equal, false otherwise.
818 friend bool
819 operator==(const subtract_with_carry& __lhs,
820 const subtract_with_carry& __rhs)
821 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
824 * Compares two % subtract_with_carry random number generator objects of
825 * the same type for inequality.
827 * @param __lhs A % subtract_with_carry random number generator object.
828 * @param __rhs Another % subtract_with_carry random number generator
829 * object.
831 * @returns true if the two objects are not equal, false otherwise.
833 friend bool
834 operator!=(const subtract_with_carry& __lhs,
835 const subtract_with_carry& __rhs)
836 { return !(__lhs == __rhs); }
839 * Inserts the current state of a % subtract_with_carry random number
840 * generator engine @p __x into the output stream @p __os.
842 * @param __os An output stream.
843 * @param __x A % subtract_with_carry random number generator engine.
845 * @returns The output stream with the state of @p __x inserted or in
846 * an error state.
848 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
849 typename _CharT, typename _Traits>
850 friend std::basic_ostream<_CharT, _Traits>&
851 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
852 const subtract_with_carry<_IntType1, __m1, __s1,
853 __r1>& __x);
856 * Extracts the current state of a % subtract_with_carry random number
857 * generator engine @p __x from the input stream @p __is.
859 * @param __is An input stream.
860 * @param __x A % subtract_with_carry random number generator engine.
862 * @returns The input stream with the state of @p __x extracted or in
863 * an error state.
865 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
866 typename _CharT, typename _Traits>
867 friend std::basic_istream<_CharT, _Traits>&
868 operator>>(std::basic_istream<_CharT, _Traits>& __is,
869 subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
871 private:
872 template<class _Gen>
873 void
874 seed(_Gen& __g, true_type)
875 { return seed(static_cast<unsigned long>(__g)); }
877 template<class _Gen>
878 void
879 seed(_Gen& __g, false_type);
881 typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
883 _UIntType _M_x[long_lag];
884 _UIntType _M_carry;
885 int _M_p;
890 * @brief The Marsaglia-Zaman generator (floats version).
892 * @var _M_x The state of the generator. This is a ring buffer.
893 * @var _M_carry The carry.
894 * @var _M_p Current index of x(i - r).
895 * @var _M_npows Precomputed negative powers of 2.
897 template<typename _RealType, int __w, int __s, int __r>
898 class subtract_with_carry_01
900 public:
901 /** The type of the generated random value. */
902 typedef _RealType result_type;
904 // parameter values
905 static const int word_size = __w;
906 static const int long_lag = __r;
907 static const int short_lag = __s;
910 * Constructs a default-initialized % subtract_with_carry_01 random
911 * number generator.
913 subtract_with_carry_01()
915 this->seed();
916 _M_initialize_npows();
920 * Constructs an explicitly seeded % subtract_with_carry_01 random number
921 * generator.
923 explicit
924 subtract_with_carry_01(unsigned long __value)
926 this->seed(__value);
927 _M_initialize_npows();
931 * Constructs a % subtract_with_carry_01 random number generator engine
932 * seeded from the generator function @p __g.
934 * @param __g The seed generator function.
936 template<class _Gen>
937 subtract_with_carry_01(_Gen& __g)
939 this->seed(__g);
940 _M_initialize_npows();
944 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
946 void
947 seed(unsigned long __value = 19780503);
950 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
951 * random number generator.
953 template<class _Gen>
954 void
955 seed(_Gen& __g)
956 { seed(__g, typename is_fundamental<_Gen>::type()); }
959 * Gets the minimum value of the range of random floats
960 * returned by this generator.
962 result_type
963 min() const
964 { return 0.0; }
967 * Gets the maximum value of the range of random floats
968 * returned by this generator.
970 result_type
971 max() const
972 { return 1.0; }
975 * Gets the next random number in the sequence.
977 result_type
978 operator()();
981 * Compares two % subtract_with_carry_01 random number generator objects
982 * of the same type for equality.
984 * @param __lhs A % subtract_with_carry_01 random number
985 * generator object.
986 * @param __rhs Another % subtract_with_carry_01 random number generator
987 * object.
989 * @returns true if the two objects are equal, false otherwise.
991 friend bool
992 operator==(const subtract_with_carry_01& __lhs,
993 const subtract_with_carry_01& __rhs)
995 for (int __i = 0; __i < long_lag; ++__i)
996 if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
997 __rhs._M_x[__i]))
998 return false;
999 return true;
1003 * Compares two % subtract_with_carry_01 random number generator objects
1004 * of the same type for inequality.
1006 * @param __lhs A % subtract_with_carry_01 random number
1007 * generator object.
1009 * @param __rhs Another % subtract_with_carry_01 random number generator
1010 * object.
1012 * @returns true if the two objects are not equal, false otherwise.
1014 friend bool
1015 operator!=(const subtract_with_carry_01& __lhs,
1016 const subtract_with_carry_01& __rhs)
1017 { return !(__lhs == __rhs); }
1020 * Inserts the current state of a % subtract_with_carry_01 random number
1021 * generator engine @p __x into the output stream @p __os.
1023 * @param __os An output stream.
1024 * @param __x A % subtract_with_carry_01 random number generator engine.
1026 * @returns The output stream with the state of @p __x inserted or in
1027 * an error state.
1029 template<typename _RealType1, int __w1, int __s1, int __r1,
1030 typename _CharT, typename _Traits>
1031 friend std::basic_ostream<_CharT, _Traits>&
1032 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1033 const subtract_with_carry_01<_RealType1, __w1, __s1,
1034 __r1>& __x);
1037 * Extracts the current state of a % subtract_with_carry_01 random number
1038 * generator engine @p __x from the input stream @p __is.
1040 * @param __is An input stream.
1041 * @param __x A % subtract_with_carry_01 random number generator engine.
1043 * @returns The input stream with the state of @p __x extracted or in
1044 * an error state.
1046 template<typename _RealType1, int __w1, int __s1, int __r1,
1047 typename _CharT, typename _Traits>
1048 friend std::basic_istream<_CharT, _Traits>&
1049 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1050 subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
1052 private:
1053 template<class _Gen>
1054 void
1055 seed(_Gen& __g, true_type)
1056 { return seed(static_cast<unsigned long>(__g)); }
1058 template<class _Gen>
1059 void
1060 seed(_Gen& __g, false_type);
1062 void
1063 _M_initialize_npows();
1065 static const int __n = (__w + 31) / 32;
1067 typedef __detail::_UInt32Type _UInt32Type;
1068 _UInt32Type _M_x[long_lag][__n];
1069 _RealType _M_npows[__n];
1070 _UInt32Type _M_carry;
1071 int _M_p;
1074 typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
1076 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1077 // 508. Bad parameters for ranlux64_base_01.
1078 typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
1082 * Produces random numbers from some base engine by discarding blocks of
1083 * data.
1085 * 0 <= @p __r <= @p __p
1087 template<class _UniformRandomNumberGenerator, int __p, int __r>
1088 class discard_block
1090 // __glibcxx_class_requires(typename base_type::result_type,
1091 // ArithmeticTypeConcept)
1093 public:
1094 /** The type of the underlying generator engine. */
1095 typedef _UniformRandomNumberGenerator base_type;
1096 /** The type of the generated random value. */
1097 typedef typename base_type::result_type result_type;
1099 // parameter values
1100 static const int block_size = __p;
1101 static const int used_block = __r;
1104 * Constructs a default %discard_block engine.
1106 * The underlying engine is default constructed as well.
1108 discard_block()
1109 : _M_n(0) { }
1112 * Copy constructs a %discard_block engine.
1114 * Copies an existing base class random number generator.
1115 * @param rng An existing (base class) engine object.
1117 explicit
1118 discard_block(const base_type& __rng)
1119 : _M_b(__rng), _M_n(0) { }
1122 * Seed constructs a %discard_block engine.
1124 * Constructs the underlying generator engine seeded with @p __s.
1125 * @param __s A seed value for the base class engine.
1127 explicit
1128 discard_block(unsigned long __s)
1129 : _M_b(__s), _M_n(0) { }
1132 * Generator construct a %discard_block engine.
1134 * @param __g A seed generator function.
1136 template<class _Gen>
1137 discard_block(_Gen& __g)
1138 : _M_b(__g), _M_n(0) { }
1141 * Reseeds the %discard_block object with the default seed for the
1142 * underlying base class generator engine.
1144 void seed()
1146 _M_b.seed();
1147 _M_n = 0;
1151 * Reseeds the %discard_block object with the given seed generator
1152 * function.
1153 * @param __g A seed generator function.
1155 template<class _Gen>
1156 void seed(_Gen& __g)
1158 _M_b.seed(__g);
1159 _M_n = 0;
1163 * Gets a const reference to the underlying generator engine object.
1165 const base_type&
1166 base() const
1167 { return _M_b; }
1170 * Gets the minimum value in the generated random number range.
1172 result_type
1173 min() const
1174 { return _M_b.min(); }
1177 * Gets the maximum value in the generated random number range.
1179 result_type
1180 max() const
1181 { return _M_b.max(); }
1184 * Gets the next value in the generated random number sequence.
1186 result_type
1187 operator()();
1190 * Compares two %discard_block random number generator objects of
1191 * the same type for equality.
1193 * @param __lhs A %discard_block random number generator object.
1194 * @param __rhs Another %discard_block random number generator
1195 * object.
1197 * @returns true if the two objects are equal, false otherwise.
1199 friend bool
1200 operator==(const discard_block& __lhs, const discard_block& __rhs)
1201 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
1204 * Compares two %discard_block random number generator objects of
1205 * the same type for inequality.
1207 * @param __lhs A %discard_block random number generator object.
1208 * @param __rhs Another %discard_block random number generator
1209 * object.
1211 * @returns true if the two objects are not equal, false otherwise.
1213 friend bool
1214 operator!=(const discard_block& __lhs, const discard_block& __rhs)
1215 { return !(__lhs == __rhs); }
1218 * Inserts the current state of a %discard_block random number
1219 * generator engine @p __x into the output stream @p __os.
1221 * @param __os An output stream.
1222 * @param __x A %discard_block random number generator engine.
1224 * @returns The output stream with the state of @p __x inserted or in
1225 * an error state.
1227 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1228 typename _CharT, typename _Traits>
1229 friend std::basic_ostream<_CharT, _Traits>&
1230 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1231 const discard_block<_UniformRandomNumberGenerator1,
1232 __p1, __r1>& __x);
1235 * Extracts the current state of a % subtract_with_carry random number
1236 * generator engine @p __x from the input stream @p __is.
1238 * @param __is An input stream.
1239 * @param __x A %discard_block random number generator engine.
1241 * @returns The input stream with the state of @p __x extracted or in
1242 * an error state.
1244 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1245 typename _CharT, typename _Traits>
1246 friend std::basic_istream<_CharT, _Traits>&
1247 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1248 discard_block<_UniformRandomNumberGenerator1,
1249 __p1, __r1>& __x);
1251 private:
1252 base_type _M_b;
1253 int _M_n;
1258 * James's luxury-level-3 integer adaptation of Luescher's generator.
1260 typedef discard_block<
1261 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1262 223,
1264 > ranlux3;
1267 * James's luxury-level-4 integer adaptation of Luescher's generator.
1269 typedef discard_block<
1270 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1271 389,
1273 > ranlux4;
1275 typedef discard_block<
1276 subtract_with_carry_01<float, 24, 10, 24>,
1277 223,
1279 > ranlux3_01;
1281 typedef discard_block<
1282 subtract_with_carry_01<float, 24, 10, 24>,
1283 389,
1285 > ranlux4_01;
1289 * A random number generator adaptor class that combines two random number
1290 * generator engines into a single output sequence.
1292 template<class _UniformRandomNumberGenerator1, int __s1,
1293 class _UniformRandomNumberGenerator2, int __s2>
1294 class xor_combine
1296 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1297 // result_type, ArithmeticTypeConcept)
1298 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1299 // result_type, ArithmeticTypeConcept)
1301 public:
1302 /** The type of the first underlying generator engine. */
1303 typedef _UniformRandomNumberGenerator1 base1_type;
1304 /** The type of the second underlying generator engine. */
1305 typedef _UniformRandomNumberGenerator2 base2_type;
1307 private:
1308 typedef typename base1_type::result_type _Result_type1;
1309 typedef typename base2_type::result_type _Result_type2;
1311 public:
1312 /** The type of the generated random value. */
1313 typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1)
1314 > sizeof(_Result_type2)),
1315 _Result_type1, _Result_type2>::__type result_type;
1317 // parameter values
1318 static const int shift1 = __s1;
1319 static const int shift2 = __s2;
1321 // constructors and member function
1322 xor_combine()
1323 : _M_b1(), _M_b2()
1324 { _M_initialize_max(); }
1326 xor_combine(const base1_type& __rng1, const base2_type& __rng2)
1327 : _M_b1(__rng1), _M_b2(__rng2)
1328 { _M_initialize_max(); }
1330 xor_combine(unsigned long __s)
1331 : _M_b1(__s), _M_b2(__s + 1)
1332 { _M_initialize_max(); }
1334 template<class _Gen>
1335 xor_combine(_Gen& __g)
1336 : _M_b1(__g), _M_b2(__g)
1337 { _M_initialize_max(); }
1339 void
1340 seed()
1342 _M_b1.seed();
1343 _M_b2.seed();
1346 template<class _Gen>
1347 void
1348 seed(_Gen& __g)
1350 _M_b1.seed(__g);
1351 _M_b2.seed(__g);
1354 const base1_type&
1355 base1() const
1356 { return _M_b1; }
1358 const base2_type&
1359 base2() const
1360 { return _M_b2; }
1362 result_type
1363 min() const
1364 { return 0; }
1366 result_type
1367 max() const
1368 { return _M_max; }
1371 * Gets the next random number in the sequence.
1373 // NB: Not exactly the TR1 formula, per N2079 instead.
1374 result_type
1375 operator()()
1377 return ((result_type(_M_b1() - _M_b1.min()) << shift1)
1378 ^ (result_type(_M_b2() - _M_b2.min()) << shift2));
1382 * Compares two %xor_combine random number generator objects of
1383 * the same type for equality.
1385 * @param __lhs A %xor_combine random number generator object.
1386 * @param __rhs Another %xor_combine random number generator
1387 * object.
1389 * @returns true if the two objects are equal, false otherwise.
1391 friend bool
1392 operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1394 return (__lhs.base1() == __rhs.base1())
1395 && (__lhs.base2() == __rhs.base2());
1399 * Compares two %xor_combine random number generator objects of
1400 * the same type for inequality.
1402 * @param __lhs A %xor_combine random number generator object.
1403 * @param __rhs Another %xor_combine random number generator
1404 * object.
1406 * @returns true if the two objects are not equal, false otherwise.
1408 friend bool
1409 operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1410 { return !(__lhs == __rhs); }
1413 * Inserts the current state of a %xor_combine random number
1414 * generator engine @p __x into the output stream @p __os.
1416 * @param __os An output stream.
1417 * @param __x A %xor_combine random number generator engine.
1419 * @returns The output stream with the state of @p __x inserted or in
1420 * an error state.
1422 template<class _UniformRandomNumberGenerator11, int __s11,
1423 class _UniformRandomNumberGenerator21, int __s21,
1424 typename _CharT, typename _Traits>
1425 friend std::basic_ostream<_CharT, _Traits>&
1426 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1427 const xor_combine<_UniformRandomNumberGenerator11, __s11,
1428 _UniformRandomNumberGenerator21, __s21>& __x);
1431 * Extracts the current state of a %xor_combine random number
1432 * generator engine @p __x from the input stream @p __is.
1434 * @param __is An input stream.
1435 * @param __x A %xor_combine random number generator engine.
1437 * @returns The input stream with the state of @p __x extracted or in
1438 * an error state.
1440 template<class _UniformRandomNumberGenerator11, int __s11,
1441 class _UniformRandomNumberGenerator21, int __s21,
1442 typename _CharT, typename _Traits>
1443 friend std::basic_istream<_CharT, _Traits>&
1444 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1445 xor_combine<_UniformRandomNumberGenerator11, __s11,
1446 _UniformRandomNumberGenerator21, __s21>& __x);
1448 private:
1449 void
1450 _M_initialize_max();
1452 result_type
1453 _M_initialize_max_aux(result_type, result_type, int);
1455 base1_type _M_b1;
1456 base2_type _M_b2;
1457 result_type _M_max;
1462 * A standard interface to a platform-specific non-deterministic
1463 * random number generator (if any are available).
1465 class random_device
1467 public:
1468 // types
1469 typedef unsigned int result_type;
1471 // constructors, destructors and member functions
1473 #ifdef _GLIBCXX_USE_RANDOM_TR1
1475 explicit
1476 random_device(const std::string& __token = "/dev/urandom")
1478 if ((__token != "/dev/urandom" && __token != "/dev/random")
1479 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1480 std::__throw_runtime_error(__N("random_device::"
1481 "random_device(const std::string&)"));
1484 ~random_device()
1485 { std::fclose(_M_file); }
1487 #else
1489 explicit
1490 random_device(const std::string& __token = "mt19937")
1491 : _M_mt(_M_strtoul(__token)) { }
1493 private:
1494 static unsigned long
1495 _M_strtoul(const std::string& __str)
1497 unsigned long __ret = 5489UL;
1498 if (__str != "mt19937")
1500 const char* __nptr = __str.c_str();
1501 char* __endptr;
1502 __ret = std::strtoul(__nptr, &__endptr, 0);
1503 if (*__nptr == '\0' || *__endptr != '\0')
1504 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1505 "(const std::string&)"));
1507 return __ret;
1510 public:
1512 #endif
1514 result_type
1515 min() const
1516 { return std::numeric_limits<result_type>::min(); }
1518 result_type
1519 max() const
1520 { return std::numeric_limits<result_type>::max(); }
1522 double
1523 entropy() const
1524 { return 0.0; }
1526 result_type
1527 operator()()
1529 #ifdef _GLIBCXX_USE_RANDOM_TR1
1530 result_type __ret;
1531 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1532 1, _M_file);
1533 return __ret;
1534 #else
1535 return _M_mt();
1536 #endif
1539 private:
1540 random_device(const random_device&);
1541 void operator=(const random_device&);
1543 #ifdef _GLIBCXX_USE_RANDOM_TR1
1544 FILE* _M_file;
1545 #else
1546 mt19937 _M_mt;
1547 #endif
1550 /* @} */ // group tr1_random_generators
1553 * @addtogroup tr1_random_distributions Random Number Distributions
1554 * @ingroup tr1_random
1555 * @{
1559 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1560 * @ingroup tr1_random_distributions
1561 * @{
1565 * @brief Uniform discrete distribution for random numbers.
1566 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1567 * probability throughout the range.
1569 template<typename _IntType = int>
1570 class uniform_int
1572 __glibcxx_class_requires(_IntType, _IntegerConcept)
1574 public:
1575 /** The type of the parameters of the distribution. */
1576 typedef _IntType input_type;
1577 /** The type of the range of the distribution. */
1578 typedef _IntType result_type;
1580 public:
1582 * Constructs a uniform distribution object.
1584 explicit
1585 uniform_int(_IntType __min = 0, _IntType __max = 9)
1586 : _M_min(__min), _M_max(__max)
1588 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1592 * Gets the inclusive lower bound of the distribution range.
1594 result_type
1595 min() const
1596 { return _M_min; }
1599 * Gets the inclusive upper bound of the distribution range.
1601 result_type
1602 max() const
1603 { return _M_max; }
1606 * Resets the distribution state.
1608 * Does nothing for the uniform integer distribution.
1610 void
1611 reset() { }
1614 * Gets a uniformly distributed random number in the range
1615 * @f$(min, max)@f$.
1617 template<typename _UniformRandomNumberGenerator>
1618 result_type
1619 operator()(_UniformRandomNumberGenerator& __urng)
1621 typedef typename _UniformRandomNumberGenerator::result_type
1622 _UResult_type;
1623 return _M_call(__urng, _M_min, _M_max,
1624 typename is_integral<_UResult_type>::type());
1628 * Gets a uniform random number in the range @f$[0, n)@f$.
1630 * This function is aimed at use with std::random_shuffle.
1632 template<typename _UniformRandomNumberGenerator>
1633 result_type
1634 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1636 typedef typename _UniformRandomNumberGenerator::result_type
1637 _UResult_type;
1638 return _M_call(__urng, 0, __n - 1,
1639 typename is_integral<_UResult_type>::type());
1643 * Inserts a %uniform_int random number distribution @p __x into the
1644 * output stream @p os.
1646 * @param __os An output stream.
1647 * @param __x A %uniform_int random number distribution.
1649 * @returns The output stream with the state of @p __x inserted or in
1650 * an error state.
1652 template<typename _IntType1, typename _CharT, typename _Traits>
1653 friend std::basic_ostream<_CharT, _Traits>&
1654 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1655 const uniform_int<_IntType1>& __x);
1658 * Extracts a %uniform_int random number distribution
1659 * @p __x from the input stream @p __is.
1661 * @param __is An input stream.
1662 * @param __x A %uniform_int random number generator engine.
1664 * @returns The input stream with @p __x extracted or in an error state.
1666 template<typename _IntType1, typename _CharT, typename _Traits>
1667 friend std::basic_istream<_CharT, _Traits>&
1668 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1669 uniform_int<_IntType1>& __x);
1671 private:
1672 template<typename _UniformRandomNumberGenerator>
1673 result_type
1674 _M_call(_UniformRandomNumberGenerator& __urng,
1675 result_type __min, result_type __max, true_type);
1677 template<typename _UniformRandomNumberGenerator>
1678 result_type
1679 _M_call(_UniformRandomNumberGenerator& __urng,
1680 result_type __min, result_type __max, false_type)
1682 return result_type((__urng() - __urng.min())
1683 / (__urng.max() - __urng.min())
1684 * (__max - __min + 1)) + __min;
1687 _IntType _M_min;
1688 _IntType _M_max;
1693 * @brief A Bernoulli random number distribution.
1695 * Generates a sequence of true and false values with likelihood @f$ p @f$
1696 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1698 class bernoulli_distribution
1700 public:
1701 typedef int input_type;
1702 typedef bool result_type;
1704 public:
1706 * Constructs a Bernoulli distribution with likelihood @p p.
1708 * @param __p [IN] The likelihood of a true result being returned. Must
1709 * be in the interval @f$ [0, 1] @f$.
1711 explicit
1712 bernoulli_distribution(double __p = 0.5)
1713 : _M_p(__p)
1715 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1719 * Gets the @p p parameter of the distribution.
1721 double
1722 p() const
1723 { return _M_p; }
1726 * Resets the distribution state.
1728 * Does nothing for a Bernoulli distribution.
1730 void
1731 reset() { }
1734 * Gets the next value in the Bernoullian sequence.
1736 template<class _UniformRandomNumberGenerator>
1737 result_type
1738 operator()(_UniformRandomNumberGenerator& __urng)
1740 if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
1741 return true;
1742 return false;
1746 * Inserts a %bernoulli_distribution random number distribution
1747 * @p __x into the output stream @p __os.
1749 * @param __os An output stream.
1750 * @param __x A %bernoulli_distribution random number distribution.
1752 * @returns The output stream with the state of @p __x inserted or in
1753 * an error state.
1755 template<typename _CharT, typename _Traits>
1756 friend std::basic_ostream<_CharT, _Traits>&
1757 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1758 const bernoulli_distribution& __x);
1761 * Extracts a %bernoulli_distribution random number distribution
1762 * @p __x from the input stream @p __is.
1764 * @param __is An input stream.
1765 * @param __x A %bernoulli_distribution random number generator engine.
1767 * @returns The input stream with @p __x extracted or in an error state.
1769 template<typename _CharT, typename _Traits>
1770 friend std::basic_istream<_CharT, _Traits>&
1771 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1772 bernoulli_distribution& __x)
1773 { return __is >> __x._M_p; }
1775 private:
1776 double _M_p;
1781 * @brief A discrete geometric random number distribution.
1783 * The formula for the geometric probability mass function is
1784 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1785 * distribution.
1787 template<typename _IntType = int, typename _RealType = double>
1788 class geometric_distribution
1790 public:
1791 // types
1792 typedef _RealType input_type;
1793 typedef _IntType result_type;
1795 // constructors and member function
1796 explicit
1797 geometric_distribution(const _RealType& __p = _RealType(0.5))
1798 : _M_p(__p)
1800 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
1801 _M_initialize();
1805 * Gets the distribution parameter @p p.
1807 _RealType
1808 p() const
1809 { return _M_p; }
1811 void
1812 reset() { }
1814 template<class _UniformRandomNumberGenerator>
1815 result_type
1816 operator()(_UniformRandomNumberGenerator& __urng);
1819 * Inserts a %geometric_distribution random number distribution
1820 * @p __x into the output stream @p __os.
1822 * @param __os An output stream.
1823 * @param __x A %geometric_distribution random number distribution.
1825 * @returns The output stream with the state of @p __x inserted or in
1826 * an error state.
1828 template<typename _IntType1, typename _RealType1,
1829 typename _CharT, typename _Traits>
1830 friend std::basic_ostream<_CharT, _Traits>&
1831 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1832 const geometric_distribution<_IntType1, _RealType1>& __x);
1835 * Extracts a %geometric_distribution random number distribution
1836 * @p __x from the input stream @p __is.
1838 * @param __is An input stream.
1839 * @param __x A %geometric_distribution random number generator engine.
1841 * @returns The input stream with @p __x extracted or in an error state.
1843 template<typename _CharT, typename _Traits>
1844 friend std::basic_istream<_CharT, _Traits>&
1845 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1846 geometric_distribution& __x)
1848 __is >> __x._M_p;
1849 __x._M_initialize();
1850 return __is;
1853 private:
1854 void
1855 _M_initialize()
1856 { _M_log_p = std::log(_M_p); }
1858 _RealType _M_p;
1859 _RealType _M_log_p;
1863 template<typename _RealType>
1864 class normal_distribution;
1867 * @brief A discrete Poisson random number distribution.
1869 * The formula for the Poisson probability mass function is
1870 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1871 * parameter of the distribution.
1873 template<typename _IntType = int, typename _RealType = double>
1874 class poisson_distribution
1876 public:
1877 // types
1878 typedef _RealType input_type;
1879 typedef _IntType result_type;
1881 // constructors and member function
1882 explicit
1883 poisson_distribution(const _RealType& __mean = _RealType(1))
1884 : _M_mean(__mean), _M_nd()
1886 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
1887 _M_initialize();
1891 * Gets the distribution parameter @p mean.
1893 _RealType
1894 mean() const
1895 { return _M_mean; }
1897 void
1898 reset()
1899 { _M_nd.reset(); }
1901 template<class _UniformRandomNumberGenerator>
1902 result_type
1903 operator()(_UniformRandomNumberGenerator& __urng);
1906 * Inserts a %poisson_distribution random number distribution
1907 * @p __x into the output stream @p __os.
1909 * @param __os An output stream.
1910 * @param __x A %poisson_distribution random number distribution.
1912 * @returns The output stream with the state of @p __x inserted or in
1913 * an error state.
1915 template<typename _IntType1, typename _RealType1,
1916 typename _CharT, typename _Traits>
1917 friend std::basic_ostream<_CharT, _Traits>&
1918 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1919 const poisson_distribution<_IntType1, _RealType1>& __x);
1922 * Extracts a %poisson_distribution random number distribution
1923 * @p __x from the input stream @p __is.
1925 * @param __is An input stream.
1926 * @param __x A %poisson_distribution random number generator engine.
1928 * @returns The input stream with @p __x extracted or in an error state.
1930 template<typename _IntType1, typename _RealType1,
1931 typename _CharT, typename _Traits>
1932 friend std::basic_istream<_CharT, _Traits>&
1933 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1934 poisson_distribution<_IntType1, _RealType1>& __x);
1936 private:
1937 void
1938 _M_initialize();
1940 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1941 normal_distribution<_RealType> _M_nd;
1943 _RealType _M_mean;
1945 // Hosts either log(mean) or the threshold of the simple method.
1946 _RealType _M_lm_thr;
1947 #if _GLIBCXX_USE_C99_MATH_TR1
1948 _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
1949 #endif
1954 * @brief A discrete binomial random number distribution.
1956 * The formula for the binomial probability mass function is
1957 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1958 * and @f$ p @f$ are the parameters of the distribution.
1960 template<typename _IntType = int, typename _RealType = double>
1961 class binomial_distribution
1963 public:
1964 // types
1965 typedef _RealType input_type;
1966 typedef _IntType result_type;
1968 // constructors and member function
1969 explicit
1970 binomial_distribution(_IntType __t = 1,
1971 const _RealType& __p = _RealType(0.5))
1972 : _M_t(__t), _M_p(__p), _M_nd()
1974 _GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
1975 _M_initialize();
1979 * Gets the distribution @p t parameter.
1981 _IntType
1982 t() const
1983 { return _M_t; }
1986 * Gets the distribution @p p parameter.
1988 _RealType
1989 p() const
1990 { return _M_p; }
1992 void
1993 reset()
1994 { _M_nd.reset(); }
1996 template<class _UniformRandomNumberGenerator>
1997 result_type
1998 operator()(_UniformRandomNumberGenerator& __urng);
2001 * Inserts a %binomial_distribution random number distribution
2002 * @p __x into the output stream @p __os.
2004 * @param __os An output stream.
2005 * @param __x A %binomial_distribution random number distribution.
2007 * @returns The output stream with the state of @p __x inserted or in
2008 * an error state.
2010 template<typename _IntType1, typename _RealType1,
2011 typename _CharT, typename _Traits>
2012 friend std::basic_ostream<_CharT, _Traits>&
2013 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2014 const binomial_distribution<_IntType1, _RealType1>& __x);
2017 * Extracts a %binomial_distribution random number distribution
2018 * @p __x from the input stream @p __is.
2020 * @param __is An input stream.
2021 * @param __x A %binomial_distribution random number generator engine.
2023 * @returns The input stream with @p __x extracted or in an error state.
2025 template<typename _IntType1, typename _RealType1,
2026 typename _CharT, typename _Traits>
2027 friend std::basic_istream<_CharT, _Traits>&
2028 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2029 binomial_distribution<_IntType1, _RealType1>& __x);
2031 private:
2032 void
2033 _M_initialize();
2035 template<class _UniformRandomNumberGenerator>
2036 result_type
2037 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
2039 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
2040 normal_distribution<_RealType> _M_nd;
2042 _RealType _M_q;
2043 #if _GLIBCXX_USE_C99_MATH_TR1
2044 _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
2045 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
2046 #endif
2047 _RealType _M_p;
2048 _IntType _M_t;
2050 bool _M_easy;
2053 /* @} */ // group tr1_random_distributions_discrete
2056 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
2057 * @ingroup tr1_random_distributions
2058 * @{
2062 * @brief Uniform continuous distribution for random numbers.
2064 * A continuous random distribution on the range [min, max) with equal
2065 * probability throughout the range. The URNG should be real-valued and
2066 * deliver number in the range [0, 1).
2068 template<typename _RealType = double>
2069 class uniform_real
2071 public:
2072 // types
2073 typedef _RealType input_type;
2074 typedef _RealType result_type;
2076 public:
2078 * Constructs a uniform_real object.
2080 * @param __min [IN] The lower bound of the distribution.
2081 * @param __max [IN] The upper bound of the distribution.
2083 explicit
2084 uniform_real(_RealType __min = _RealType(0),
2085 _RealType __max = _RealType(1))
2086 : _M_min(__min), _M_max(__max)
2088 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
2091 result_type
2092 min() const
2093 { return _M_min; }
2095 result_type
2096 max() const
2097 { return _M_max; }
2099 void
2100 reset() { }
2102 template<class _UniformRandomNumberGenerator>
2103 result_type
2104 operator()(_UniformRandomNumberGenerator& __urng)
2105 { return (__urng() * (_M_max - _M_min)) + _M_min; }
2108 * Inserts a %uniform_real random number distribution @p __x into the
2109 * output stream @p __os.
2111 * @param __os An output stream.
2112 * @param __x A %uniform_real random number distribution.
2114 * @returns The output stream with the state of @p __x inserted or in
2115 * an error state.
2117 template<typename _RealType1, typename _CharT, typename _Traits>
2118 friend std::basic_ostream<_CharT, _Traits>&
2119 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2120 const uniform_real<_RealType1>& __x);
2123 * Extracts a %uniform_real random number distribution
2124 * @p __x from the input stream @p __is.
2126 * @param __is An input stream.
2127 * @param __x A %uniform_real random number generator engine.
2129 * @returns The input stream with @p __x extracted or in an error state.
2131 template<typename _RealType1, typename _CharT, typename _Traits>
2132 friend std::basic_istream<_CharT, _Traits>&
2133 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2134 uniform_real<_RealType1>& __x);
2136 private:
2137 _RealType _M_min;
2138 _RealType _M_max;
2143 * @brief An exponential continuous distribution for random numbers.
2145 * The formula for the exponential probability mass function is
2146 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2148 * <table border=1 cellpadding=10 cellspacing=0>
2149 * <caption align=top>Distribution Statistics</caption>
2150 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2151 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2152 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2153 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2154 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2155 * </table>
2157 template<typename _RealType = double>
2158 class exponential_distribution
2160 public:
2161 // types
2162 typedef _RealType input_type;
2163 typedef _RealType result_type;
2165 public:
2167 * Constructs an exponential distribution with inverse scale parameter
2168 * @f$ \lambda @f$.
2170 explicit
2171 exponential_distribution(const result_type& __lambda = result_type(1))
2172 : _M_lambda(__lambda)
2174 _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
2178 * Gets the inverse scale parameter of the distribution.
2180 _RealType
2181 lambda() const
2182 { return _M_lambda; }
2185 * Resets the distribution.
2187 * Has no effect on exponential distributions.
2189 void
2190 reset() { }
2192 template<class _UniformRandomNumberGenerator>
2193 result_type
2194 operator()(_UniformRandomNumberGenerator& __urng)
2195 { return -std::log(__urng()) / _M_lambda; }
2198 * Inserts a %exponential_distribution random number distribution
2199 * @p __x into the output stream @p __os.
2201 * @param __os An output stream.
2202 * @param __x A %exponential_distribution random number distribution.
2204 * @returns The output stream with the state of @p __x inserted or in
2205 * an error state.
2207 template<typename _RealType1, typename _CharT, typename _Traits>
2208 friend std::basic_ostream<_CharT, _Traits>&
2209 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2210 const exponential_distribution<_RealType1>& __x);
2213 * Extracts a %exponential_distribution random number distribution
2214 * @p __x from the input stream @p __is.
2216 * @param __is An input stream.
2217 * @param __x A %exponential_distribution random number
2218 * generator engine.
2220 * @returns The input stream with @p __x extracted or in an error state.
2222 template<typename _CharT, typename _Traits>
2223 friend std::basic_istream<_CharT, _Traits>&
2224 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2225 exponential_distribution& __x)
2226 { return __is >> __x._M_lambda; }
2228 private:
2229 result_type _M_lambda;
2234 * @brief A normal continuous distribution for random numbers.
2236 * The formula for the normal probability mass function is
2237 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2238 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2240 template<typename _RealType = double>
2241 class normal_distribution
2243 public:
2244 // types
2245 typedef _RealType input_type;
2246 typedef _RealType result_type;
2248 public:
2250 * Constructs a normal distribution with parameters @f$ mean @f$ and
2251 * @f$ \sigma @f$.
2253 explicit
2254 normal_distribution(const result_type& __mean = result_type(0),
2255 const result_type& __sigma = result_type(1))
2256 : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
2258 _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
2262 * Gets the mean of the distribution.
2264 _RealType
2265 mean() const
2266 { return _M_mean; }
2269 * Gets the @f$ \sigma @f$ of the distribution.
2271 _RealType
2272 sigma() const
2273 { return _M_sigma; }
2276 * Resets the distribution.
2278 void
2279 reset()
2280 { _M_saved_available = false; }
2282 template<class _UniformRandomNumberGenerator>
2283 result_type
2284 operator()(_UniformRandomNumberGenerator& __urng);
2287 * Inserts a %normal_distribution random number distribution
2288 * @p __x into the output stream @p __os.
2290 * @param __os An output stream.
2291 * @param __x A %normal_distribution random number distribution.
2293 * @returns The output stream with the state of @p __x inserted or in
2294 * an error state.
2296 template<typename _RealType1, typename _CharT, typename _Traits>
2297 friend std::basic_ostream<_CharT, _Traits>&
2298 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2299 const normal_distribution<_RealType1>& __x);
2302 * Extracts a %normal_distribution random number distribution
2303 * @p __x from the input stream @p __is.
2305 * @param __is An input stream.
2306 * @param __x A %normal_distribution random number generator engine.
2308 * @returns The input stream with @p __x extracted or in an error state.
2310 template<typename _RealType1, typename _CharT, typename _Traits>
2311 friend std::basic_istream<_CharT, _Traits>&
2312 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2313 normal_distribution<_RealType1>& __x);
2315 private:
2316 result_type _M_mean;
2317 result_type _M_sigma;
2318 result_type _M_saved;
2319 bool _M_saved_available;
2324 * @brief A gamma continuous distribution for random numbers.
2326 * The formula for the gamma probability mass function is
2327 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2329 template<typename _RealType = double>
2330 class gamma_distribution
2332 public:
2333 // types
2334 typedef _RealType input_type;
2335 typedef _RealType result_type;
2337 public:
2339 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2341 explicit
2342 gamma_distribution(const result_type& __alpha_val = result_type(1))
2343 : _M_alpha(__alpha_val)
2345 _GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
2346 _M_initialize();
2350 * Gets the @f$ \alpha @f$ of the distribution.
2352 _RealType
2353 alpha() const
2354 { return _M_alpha; }
2357 * Resets the distribution.
2359 void
2360 reset() { }
2362 template<class _UniformRandomNumberGenerator>
2363 result_type
2364 operator()(_UniformRandomNumberGenerator& __urng);
2367 * Inserts a %gamma_distribution random number distribution
2368 * @p __x into the output stream @p __os.
2370 * @param __os An output stream.
2371 * @param __x A %gamma_distribution random number distribution.
2373 * @returns The output stream with the state of @p __x inserted or in
2374 * an error state.
2376 template<typename _RealType1, typename _CharT, typename _Traits>
2377 friend std::basic_ostream<_CharT, _Traits>&
2378 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2379 const gamma_distribution<_RealType1>& __x);
2382 * Extracts a %gamma_distribution random number distribution
2383 * @p __x from the input stream @p __is.
2385 * @param __is An input stream.
2386 * @param __x A %gamma_distribution random number generator engine.
2388 * @returns The input stream with @p __x extracted or in an error state.
2390 template<typename _CharT, typename _Traits>
2391 friend std::basic_istream<_CharT, _Traits>&
2392 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2393 gamma_distribution& __x)
2395 __is >> __x._M_alpha;
2396 __x._M_initialize();
2397 return __is;
2400 private:
2401 void
2402 _M_initialize();
2404 result_type _M_alpha;
2406 // Hosts either lambda of GB or d of modified Vaduva's.
2407 result_type _M_l_d;
2410 /* @} */ // group tr1_random_distributions_continuous
2411 /* @} */ // group tr1_random_distributions
2412 /* @} */ // group tr1_random
2413 _GLIBCXX_END_NAMESPACE_VERSION
2417 #endif // _GLIBCXX_TR1_RANDOM_H