Fix handling of large arguments passed by value.
[official-gcc.git] / libstdc++-v3 / include / tr1 / random.h
blobf18b74b6775266a5e070b50406bd5e20a99515a4
1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009-2023 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 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 namespace tr1
42 // [5.1] Random number generation
44 /**
45 * @addtogroup tr1_random Random Number Generation
46 * A facility for generating random numbers on selected distributions.
47 * @{
51 * Implementation-space details.
53 namespace __detail
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 _Engine::result_type _Engine_result_type;
85 typedef typename _Distribution::input_type result_type;
87 public:
88 _Adaptor(const _Engine& __g)
89 : _M_g(__g) { }
91 result_type
92 min() const
94 result_type __return_value;
95 if (is_integral<_Engine_result_type>::value
96 && is_integral<result_type>::value)
97 __return_value = _M_g.min();
98 else
99 __return_value = result_type(0);
100 return __return_value;
103 result_type
104 max() const
106 result_type __return_value;
107 if (is_integral<_Engine_result_type>::value
108 && is_integral<result_type>::value)
109 __return_value = _M_g.max();
110 else if (!is_integral<result_type>::value)
111 __return_value = result_type(1);
112 else
113 __return_value = std::numeric_limits<result_type>::max() - 1;
114 return __return_value;
118 * Converts a value generated by the adapted random number generator
119 * into a value in the input domain for the dependent random number
120 * distribution.
122 * Because the type traits are compile time constants only the
123 * appropriate clause of the if statements will actually be emitted
124 * by the compiler.
126 result_type
127 operator()()
129 result_type __return_value;
130 if (is_integral<_Engine_result_type>::value
131 && is_integral<result_type>::value)
132 __return_value = _M_g();
133 else if (!is_integral<_Engine_result_type>::value
134 && !is_integral<result_type>::value)
135 __return_value = result_type(_M_g() - _M_g.min())
136 / result_type(_M_g.max() - _M_g.min());
137 else if (is_integral<_Engine_result_type>::value
138 && !is_integral<result_type>::value)
139 __return_value = result_type(_M_g() - _M_g.min())
140 / result_type(_M_g.max() - _M_g.min() + result_type(1));
141 else
142 __return_value = (((_M_g() - _M_g.min())
143 / (_M_g.max() - _M_g.min()))
144 * std::numeric_limits<result_type>::max());
145 return __return_value;
148 _Engine _M_g;
150 } // namespace __detail
153 * Produces random numbers on a given distribution function using a
154 * non-uniform random number generation engine.
156 * @todo the engine_value_type needs to be studied more carefully.
158 template<typename _Engine, typename _Dist>
159 class variate_generator
161 template<typename _Eng>
162 struct _Value
164 typedef _Eng type;
166 static const _Eng&
167 _S_ref(const _Eng& __e) { return __e; }
170 template<typename _Eng>
171 struct _Value<_Eng*>
173 typedef _Eng type;
175 __attribute__((__nonnull__))
176 static const _Eng&
177 _S_ref(const _Eng* __e) { return *__e; }
180 template<typename _Eng>
181 struct _Value<_Eng&>
183 typedef _Eng type;
185 static const _Eng&
186 _S_ref(const _Eng& __e) { return __e; }
189 public:
190 typedef _Engine engine_type;
191 typedef typename _Value<_Engine>::type engine_value_type;
192 typedef _Dist distribution_type;
193 typedef typename _Dist::result_type result_type;
195 // Concept requirements.
196 __glibcxx_class_requires(engine_value_type, _CopyConstructibleConcept)
197 // __glibcxx_class_requires(_Engine, _EngineConcept)
198 // __glibcxx_class_requires(_Dist, _EngineConcept)
200 // tr1:5.1.1 table 5.1 requirement
201 typedef typename __gnu_cxx::__enable_if<
202 is_arithmetic<result_type>::value, result_type>::__type _IsValidType;
205 * Constructs a variate generator with the uniform random number
206 * generator @p __eng for the random distribution @p __dist.
208 * @throws Any exceptions which may thrown by the copy constructors of
209 * the @p _Engine or @p _Dist objects.
211 variate_generator(engine_type __eng, distribution_type __dist)
212 : _M_engine(_Value<_Engine>::_S_ref(__eng)), _M_dist(__dist) { }
215 * Gets the next generated value on the distribution.
217 result_type
218 operator()()
219 { return _M_dist(_M_engine); }
222 * WTF?
224 template<typename _Tp>
225 result_type
226 operator()(_Tp __value)
227 { return _M_dist(_M_engine, __value); }
230 * Gets a reference to the underlying uniform random number generator
231 * object.
233 engine_value_type&
234 engine()
235 { return _M_engine._M_g; }
238 * Gets a const reference to the underlying uniform random number
239 * generator object.
241 const engine_value_type&
242 engine() const
243 { return _M_engine._M_g; }
246 * Gets a reference to the underlying random distribution.
248 distribution_type&
249 distribution()
250 { return _M_dist; }
253 * Gets a const reference to the underlying random distribution.
255 const distribution_type&
256 distribution() const
257 { return _M_dist; }
260 * Gets the closed lower bound of the distribution interval.
262 result_type
263 min() const
264 { return this->distribution().min(); }
267 * Gets the closed upper bound of the distribution interval.
269 result_type
270 max() const
271 { return this->distribution().max(); }
273 private:
274 __detail::_Adaptor<engine_value_type, _Dist> _M_engine;
275 distribution_type _M_dist;
280 * @addtogroup tr1_random_generators Random Number Generators
281 * @ingroup tr1_random
283 * These classes define objects which provide random or pseudorandom
284 * numbers, either from a discrete or a continuous interval. The
285 * random number generator supplied as a part of this library are
286 * all uniform random number generators which provide a sequence of
287 * random number uniformly distributed over their range.
289 * A number generator is a function object with an operator() that
290 * takes zero arguments and returns a number.
292 * A compliant random number generator must satisfy the following
293 * requirements. <table border=1 cellpadding=10 cellspacing=0>
294 * <caption align=top>Random Number Generator Requirements</caption>
295 * <tr><td>To be documented.</td></tr> </table>
297 * @{
301 * @brief A model of a linear congruential random number generator.
303 * A random number generator that produces pseudorandom numbers using the
304 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
306 * The template parameter @p _UIntType must be an unsigned integral type
307 * large enough to store values up to (__m-1). If the template parameter
308 * @p __m is 0, the modulus @p __m used is
309 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
310 * parameters @p __a and @p __c must be less than @p __m.
312 * The size of the state is @f$ 1 @f$.
314 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
315 class linear_congruential
317 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
318 // __glibcpp_class_requires(__a < __m && __c < __m)
320 public:
321 /** The type of the generated random value. */
322 typedef _UIntType result_type;
324 /** The multiplier. */
325 static const _UIntType multiplier = __a;
326 /** An increment. */
327 static const _UIntType increment = __c;
328 /** The modulus. */
329 static const _UIntType modulus = __m;
332 * Constructs a %linear_congruential random number generator engine with
333 * seed @p __s. The default seed value is 1.
335 * @param __s The initial seed value.
337 explicit
338 linear_congruential(unsigned long __x0 = 1)
339 { this->seed(__x0); }
342 * Constructs a %linear_congruential random number generator engine
343 * seeded from the generator function @p __g.
345 * @param __g The seed generator function.
347 template<class _Gen>
348 linear_congruential(_Gen& __g)
349 { this->seed(__g); }
352 * Reseeds the %linear_congruential random number generator engine
353 * sequence to the seed @g __s.
355 * @param __s The new seed.
357 void
358 seed(unsigned long __s = 1);
361 * Reseeds the %linear_congruential random number generator engine
362 * sequence using values from the generator function @p __g.
364 * @param __g the seed generator function.
366 template<class _Gen>
367 void
368 seed(_Gen& __g)
369 { seed(__g, typename is_fundamental<_Gen>::type()); }
372 * Gets the smallest possible value in the output range.
374 * The minimum depends on the @p __c parameter: if it is zero, the
375 * minimum generated must be > 0, otherwise 0 is allowed.
377 result_type
378 min() const
379 { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
382 * Gets the largest possible value in the output range.
384 result_type
385 max() const
386 { return __m - 1; }
389 * Gets the next random number in the sequence.
391 result_type
392 operator()();
395 * Compares two linear congruential random number generator
396 * objects of the same type for equality.
398 * @param __lhs A linear congruential random number generator object.
399 * @param __rhs Another linear congruential random number generator obj.
401 * @returns true if the two objects are equal, false otherwise.
403 friend bool
404 operator==(const linear_congruential& __lhs,
405 const linear_congruential& __rhs)
406 { return __lhs._M_x == __rhs._M_x; }
409 * Compares two linear congruential random number generator
410 * objects of the same type for inequality.
412 * @param __lhs A linear congruential random number generator object.
413 * @param __rhs Another linear congruential random number generator obj.
415 * @returns true if the two objects are not equal, false otherwise.
417 friend bool
418 operator!=(const linear_congruential& __lhs,
419 const linear_congruential& __rhs)
420 { return !(__lhs == __rhs); }
423 * Writes the textual representation of the state x(i) of x to @p __os.
425 * @param __os The output stream.
426 * @param __lcr A % linear_congruential random number generator.
427 * @returns __os.
429 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
430 _UIntType1 __m1,
431 typename _CharT, typename _Traits>
432 friend std::basic_ostream<_CharT, _Traits>&
433 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
434 const linear_congruential<_UIntType1, __a1, __c1,
435 __m1>& __lcr);
438 * Sets the state of the engine by reading its textual
439 * representation from @p __is.
441 * The textual representation must have been previously written using an
442 * output stream whose imbued locale and whose type's template
443 * specialization arguments _CharT and _Traits were the same as those of
444 * @p __is.
446 * @param __is The input stream.
447 * @param __lcr A % linear_congruential random number generator.
448 * @returns __is.
450 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
451 _UIntType1 __m1,
452 typename _CharT, typename _Traits>
453 friend std::basic_istream<_CharT, _Traits>&
454 operator>>(std::basic_istream<_CharT, _Traits>& __is,
455 linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
457 private:
458 template<class _Gen>
459 void
460 seed(_Gen& __g, true_type)
461 { return seed(static_cast<unsigned long>(__g)); }
463 template<class _Gen>
464 void
465 seed(_Gen& __g, false_type);
467 _UIntType _M_x;
471 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
473 typedef linear_congruential<unsigned long, 16807, 0, 2147483647> minstd_rand0;
476 * An alternative LCR (Lehmer Generator function) .
478 typedef linear_congruential<unsigned long, 48271, 0, 2147483647> minstd_rand;
482 * A generalized feedback shift register discrete random number generator.
484 * This algorithm avoids multiplication and division and is designed to be
485 * friendly to a pipelined architecture. If the parameters are chosen
486 * correctly, this generator will produce numbers with a very long period and
487 * fairly good apparent entropy, although still not cryptographically strong.
489 * The best way to use this generator is with the predefined mt19937 class.
491 * This algorithm was originally invented by Makoto Matsumoto and
492 * Takuji Nishimura.
494 * @var word_size The number of bits in each element of the state vector.
495 * @var state_size The degree of recursion.
496 * @var shift_size The period parameter.
497 * @var mask_bits The separation point bit index.
498 * @var parameter_a The last row of the twist matrix.
499 * @var output_u The first right-shift tempering matrix parameter.
500 * @var output_s The first left-shift tempering matrix parameter.
501 * @var output_b The first left-shift tempering matrix mask.
502 * @var output_t The second left-shift tempering matrix parameter.
503 * @var output_c The second left-shift tempering matrix mask.
504 * @var output_l The second right-shift tempering matrix parameter.
506 template<class _UIntType, int __w, int __n, int __m, int __r,
507 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
508 _UIntType __c, int __l>
509 class mersenne_twister
511 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
513 public:
514 // types
515 typedef _UIntType result_type;
517 // parameter values
518 static const int word_size = __w;
519 static const int state_size = __n;
520 static const int shift_size = __m;
521 static const int mask_bits = __r;
522 static const _UIntType parameter_a = __a;
523 static const int output_u = __u;
524 static const int output_s = __s;
525 static const _UIntType output_b = __b;
526 static const int output_t = __t;
527 static const _UIntType output_c = __c;
528 static const int output_l = __l;
530 // constructors and member function
531 mersenne_twister()
532 { seed(); }
534 explicit
535 mersenne_twister(unsigned long __value)
536 { seed(__value); }
538 template<class _Gen>
539 mersenne_twister(_Gen& __g)
540 { seed(__g); }
542 void
543 seed()
544 { seed(5489UL); }
546 void
547 seed(unsigned long __value);
549 template<class _Gen>
550 void
551 seed(_Gen& __g)
552 { seed(__g, typename is_fundamental<_Gen>::type()); }
554 result_type
555 min() const
556 { return 0; }
558 result_type
559 max() const
560 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
562 result_type
563 operator()();
566 * Compares two % mersenne_twister random number generator objects of
567 * the same type for equality.
569 * @param __lhs A % mersenne_twister random number generator object.
570 * @param __rhs Another % mersenne_twister random number generator
571 * object.
573 * @returns true if the two objects are equal, false otherwise.
575 friend bool
576 operator==(const mersenne_twister& __lhs,
577 const mersenne_twister& __rhs)
578 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
581 * Compares two % mersenne_twister random number generator objects of
582 * the same type for inequality.
584 * @param __lhs A % mersenne_twister random number generator object.
585 * @param __rhs Another % mersenne_twister random number generator
586 * object.
588 * @returns true if the two objects are not equal, false otherwise.
590 friend bool
591 operator!=(const mersenne_twister& __lhs,
592 const mersenne_twister& __rhs)
593 { return !(__lhs == __rhs); }
596 * Inserts the current state of a % mersenne_twister random number
597 * generator engine @p __x into the output stream @p __os.
599 * @param __os An output stream.
600 * @param __x A % mersenne_twister random number generator engine.
602 * @returns The output stream with the state of @p __x inserted or in
603 * an error state.
605 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
606 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
607 _UIntType1 __c1, int __l1,
608 typename _CharT, typename _Traits>
609 friend std::basic_ostream<_CharT, _Traits>&
610 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
611 const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
612 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
615 * Extracts the current state of a % mersenne_twister random number
616 * generator engine @p __x from the input stream @p __is.
618 * @param __is An input stream.
619 * @param __x A % mersenne_twister random number generator engine.
621 * @returns The input stream with the state of @p __x extracted or in
622 * an error state.
624 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
625 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
626 _UIntType1 __c1, int __l1,
627 typename _CharT, typename _Traits>
628 friend std::basic_istream<_CharT, _Traits>&
629 operator>>(std::basic_istream<_CharT, _Traits>& __is,
630 mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
631 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
633 private:
634 template<class _Gen>
635 void
636 seed(_Gen& __g, true_type)
637 { return seed(static_cast<unsigned long>(__g)); }
639 template<class _Gen>
640 void
641 seed(_Gen& __g, false_type);
643 _UIntType _M_x[state_size];
644 int _M_p;
648 * The classic Mersenne Twister.
650 * Reference:
651 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
652 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
653 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
655 typedef mersenne_twister<
656 unsigned long, 32, 624, 397, 31,
657 0x9908b0dful, 11, 7,
658 0x9d2c5680ul, 15,
659 0xefc60000ul, 18
660 > mt19937;
664 * @brief The Marsaglia-Zaman generator.
666 * This is a model of a Generalized Fibonacci discrete random number
667 * generator, sometimes referred to as the SWC generator.
669 * A discrete random number generator that produces pseudorandom
670 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
671 * carry_{i-1}) \bmod m @f$.
673 * The size of the state is @f$ r @f$
674 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
676 * N1688[4.13] says <em>the template parameter _IntType shall denote
677 * an integral type large enough to store values up to m</em>.
679 * @var _M_x The state of the generator. This is a ring buffer.
680 * @var _M_carry The carry.
681 * @var _M_p Current index of x(i - r).
683 template<typename _IntType, _IntType __m, int __s, int __r>
684 class subtract_with_carry
686 __glibcxx_class_requires(_IntType, _IntegerConcept)
688 public:
689 /** The type of the generated random value. */
690 typedef _IntType result_type;
692 // parameter values
693 static const _IntType modulus = __m;
694 static const int long_lag = __r;
695 static const int short_lag = __s;
698 * Constructs a default-initialized % subtract_with_carry random number
699 * generator.
701 subtract_with_carry()
702 { this->seed(); }
705 * Constructs an explicitly seeded % subtract_with_carry random number
706 * generator.
708 explicit
709 subtract_with_carry(unsigned long __value)
710 { this->seed(__value); }
713 * Constructs a %subtract_with_carry random number generator engine
714 * seeded from the generator function @p __g.
716 * @param __g The seed generator function.
718 template<class _Gen>
719 subtract_with_carry(_Gen& __g)
720 { this->seed(__g); }
723 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
725 * N1688[4.19] modifies this as follows. If @p __value == 0,
726 * sets value to 19780503. In any case, with a linear
727 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
728 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
729 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
730 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
731 * set carry to 1, otherwise sets carry to 0.
733 void
734 seed(unsigned long __value = 19780503);
737 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
738 * random number generator.
740 template<class _Gen>
741 void
742 seed(_Gen& __g)
743 { seed(__g, typename is_fundamental<_Gen>::type()); }
746 * Gets the inclusive minimum value of the range of random integers
747 * returned by this generator.
749 result_type
750 min() const
751 { return 0; }
754 * Gets the inclusive maximum value of the range of random integers
755 * returned by this generator.
757 result_type
758 max() const
759 { return this->modulus - 1; }
762 * Gets the next random number in the sequence.
764 result_type
765 operator()();
768 * Compares two % subtract_with_carry random number generator objects of
769 * the same type for equality.
771 * @param __lhs A % subtract_with_carry random number generator object.
772 * @param __rhs Another % subtract_with_carry random number generator
773 * object.
775 * @returns true if the two objects are equal, false otherwise.
777 friend bool
778 operator==(const subtract_with_carry& __lhs,
779 const subtract_with_carry& __rhs)
780 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
783 * Compares two % subtract_with_carry random number generator objects of
784 * the same type for inequality.
786 * @param __lhs A % subtract_with_carry random number generator object.
787 * @param __rhs Another % subtract_with_carry random number generator
788 * object.
790 * @returns true if the two objects are not equal, false otherwise.
792 friend bool
793 operator!=(const subtract_with_carry& __lhs,
794 const subtract_with_carry& __rhs)
795 { return !(__lhs == __rhs); }
798 * Inserts the current state of a % subtract_with_carry random number
799 * generator engine @p __x into the output stream @p __os.
801 * @param __os An output stream.
802 * @param __x A % subtract_with_carry random number generator engine.
804 * @returns The output stream with the state of @p __x inserted or in
805 * an error state.
807 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
808 typename _CharT, typename _Traits>
809 friend std::basic_ostream<_CharT, _Traits>&
810 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
811 const subtract_with_carry<_IntType1, __m1, __s1,
812 __r1>& __x);
815 * Extracts the current state of a % subtract_with_carry random number
816 * generator engine @p __x from the input stream @p __is.
818 * @param __is An input stream.
819 * @param __x A % subtract_with_carry random number generator engine.
821 * @returns The input stream with the state of @p __x extracted or in
822 * an error state.
824 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
825 typename _CharT, typename _Traits>
826 friend std::basic_istream<_CharT, _Traits>&
827 operator>>(std::basic_istream<_CharT, _Traits>& __is,
828 subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
830 private:
831 template<class _Gen>
832 void
833 seed(_Gen& __g, true_type)
834 { return seed(static_cast<unsigned long>(__g)); }
836 template<class _Gen>
837 void
838 seed(_Gen& __g, false_type);
840 typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
842 _UIntType _M_x[long_lag];
843 _UIntType _M_carry;
844 int _M_p;
849 * @brief The Marsaglia-Zaman generator (floats version).
851 * @var _M_x The state of the generator. This is a ring buffer.
852 * @var _M_carry The carry.
853 * @var _M_p Current index of x(i - r).
854 * @var _M_npows Precomputed negative powers of 2.
856 template<typename _RealType, int __w, int __s, int __r>
857 class subtract_with_carry_01
859 public:
860 /** The type of the generated random value. */
861 typedef _RealType result_type;
863 // parameter values
864 static const int word_size = __w;
865 static const int long_lag = __r;
866 static const int short_lag = __s;
869 * Constructs a default-initialized % subtract_with_carry_01 random
870 * number generator.
872 subtract_with_carry_01()
874 this->seed();
875 _M_initialize_npows();
879 * Constructs an explicitly seeded % subtract_with_carry_01 random number
880 * generator.
882 explicit
883 subtract_with_carry_01(unsigned long __value)
885 this->seed(__value);
886 _M_initialize_npows();
890 * Constructs a % subtract_with_carry_01 random number generator engine
891 * seeded from the generator function @p __g.
893 * @param __g The seed generator function.
895 template<class _Gen>
896 subtract_with_carry_01(_Gen& __g)
898 this->seed(__g);
899 _M_initialize_npows();
903 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
905 void
906 seed(unsigned long __value = 19780503);
909 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
910 * random number generator.
912 template<class _Gen>
913 void
914 seed(_Gen& __g)
915 { seed(__g, typename is_fundamental<_Gen>::type()); }
918 * Gets the minimum value of the range of random floats
919 * returned by this generator.
921 result_type
922 min() const
923 { return 0.0; }
926 * Gets the maximum value of the range of random floats
927 * returned by this generator.
929 result_type
930 max() const
931 { return 1.0; }
934 * Gets the next random number in the sequence.
936 result_type
937 operator()();
940 * Compares two % subtract_with_carry_01 random number generator objects
941 * of the same type for equality.
943 * @param __lhs A % subtract_with_carry_01 random number
944 * generator object.
945 * @param __rhs Another % subtract_with_carry_01 random number generator
946 * object.
948 * @returns true if the two objects are equal, false otherwise.
950 friend bool
951 operator==(const subtract_with_carry_01& __lhs,
952 const subtract_with_carry_01& __rhs)
954 for (int __i = 0; __i < long_lag; ++__i)
955 if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
956 __rhs._M_x[__i]))
957 return false;
958 return true;
962 * Compares two % subtract_with_carry_01 random number generator objects
963 * of the same type for inequality.
965 * @param __lhs A % subtract_with_carry_01 random number
966 * generator object.
968 * @param __rhs Another % subtract_with_carry_01 random number generator
969 * object.
971 * @returns true if the two objects are not equal, false otherwise.
973 friend bool
974 operator!=(const subtract_with_carry_01& __lhs,
975 const subtract_with_carry_01& __rhs)
976 { return !(__lhs == __rhs); }
979 * Inserts the current state of a % subtract_with_carry_01 random number
980 * generator engine @p __x into the output stream @p __os.
982 * @param __os An output stream.
983 * @param __x A % subtract_with_carry_01 random number generator engine.
985 * @returns The output stream with the state of @p __x inserted or in
986 * an error state.
988 template<typename _RealType1, int __w1, int __s1, int __r1,
989 typename _CharT, typename _Traits>
990 friend std::basic_ostream<_CharT, _Traits>&
991 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
992 const subtract_with_carry_01<_RealType1, __w1, __s1,
993 __r1>& __x);
996 * Extracts the current state of a % subtract_with_carry_01 random number
997 * generator engine @p __x from the input stream @p __is.
999 * @param __is An input stream.
1000 * @param __x A % subtract_with_carry_01 random number generator engine.
1002 * @returns The input stream with the state of @p __x extracted or in
1003 * an error state.
1005 template<typename _RealType1, int __w1, int __s1, int __r1,
1006 typename _CharT, typename _Traits>
1007 friend std::basic_istream<_CharT, _Traits>&
1008 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1009 subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
1011 private:
1012 template<class _Gen>
1013 void
1014 seed(_Gen& __g, true_type)
1015 { return seed(static_cast<unsigned long>(__g)); }
1017 template<class _Gen>
1018 void
1019 seed(_Gen& __g, false_type);
1021 void
1022 _M_initialize_npows();
1024 static const int __n = (__w + 31) / 32;
1026 typedef __detail::_UInt32Type _UInt32Type;
1027 _UInt32Type _M_x[long_lag][__n];
1028 _RealType _M_npows[__n];
1029 _UInt32Type _M_carry;
1030 int _M_p;
1033 typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
1035 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1036 // 508. Bad parameters for ranlux64_base_01.
1037 typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
1041 * Produces random numbers from some base engine by discarding blocks of
1042 * data.
1044 * 0 <= @p __r <= @p __p
1046 template<class _UniformRandomNumberGenerator, int __p, int __r>
1047 class discard_block
1049 // __glibcxx_class_requires(typename base_type::result_type,
1050 // ArithmeticTypeConcept)
1052 public:
1053 /** The type of the underlying generator engine. */
1054 typedef _UniformRandomNumberGenerator base_type;
1055 /** The type of the generated random value. */
1056 typedef typename base_type::result_type result_type;
1058 // parameter values
1059 static const int block_size = __p;
1060 static const int used_block = __r;
1063 * Constructs a default %discard_block engine.
1065 * The underlying engine is default constructed as well.
1067 discard_block()
1068 : _M_n(0) { }
1071 * Copy constructs a %discard_block engine.
1073 * Copies an existing base class random number generator.
1074 * @param rng An existing (base class) engine object.
1076 explicit
1077 discard_block(const base_type& __rng)
1078 : _M_b(__rng), _M_n(0) { }
1081 * Seed constructs a %discard_block engine.
1083 * Constructs the underlying generator engine seeded with @p __s.
1084 * @param __s A seed value for the base class engine.
1086 explicit
1087 discard_block(unsigned long __s)
1088 : _M_b(__s), _M_n(0) { }
1091 * Generator construct a %discard_block engine.
1093 * @param __g A seed generator function.
1095 template<class _Gen>
1096 discard_block(_Gen& __g)
1097 : _M_b(__g), _M_n(0) { }
1100 * Reseeds the %discard_block object with the default seed for the
1101 * underlying base class generator engine.
1103 void seed()
1105 _M_b.seed();
1106 _M_n = 0;
1110 * Reseeds the %discard_block object with the given seed generator
1111 * function.
1112 * @param __g A seed generator function.
1114 template<class _Gen>
1115 void seed(_Gen& __g)
1117 _M_b.seed(__g);
1118 _M_n = 0;
1122 * Gets a const reference to the underlying generator engine object.
1124 const base_type&
1125 base() const
1126 { return _M_b; }
1129 * Gets the minimum value in the generated random number range.
1131 result_type
1132 min() const
1133 { return _M_b.min(); }
1136 * Gets the maximum value in the generated random number range.
1138 result_type
1139 max() const
1140 { return _M_b.max(); }
1143 * Gets the next value in the generated random number sequence.
1145 result_type
1146 operator()();
1149 * Compares two %discard_block random number generator objects of
1150 * the same type for equality.
1152 * @param __lhs A %discard_block random number generator object.
1153 * @param __rhs Another %discard_block random number generator
1154 * object.
1156 * @returns true if the two objects are equal, false otherwise.
1158 friend bool
1159 operator==(const discard_block& __lhs, const discard_block& __rhs)
1160 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
1163 * Compares two %discard_block random number generator objects of
1164 * the same type for inequality.
1166 * @param __lhs A %discard_block random number generator object.
1167 * @param __rhs Another %discard_block random number generator
1168 * object.
1170 * @returns true if the two objects are not equal, false otherwise.
1172 friend bool
1173 operator!=(const discard_block& __lhs, const discard_block& __rhs)
1174 { return !(__lhs == __rhs); }
1177 * Inserts the current state of a %discard_block random number
1178 * generator engine @p __x into the output stream @p __os.
1180 * @param __os An output stream.
1181 * @param __x A %discard_block random number generator engine.
1183 * @returns The output stream with the state of @p __x inserted or in
1184 * an error state.
1186 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1187 typename _CharT, typename _Traits>
1188 friend std::basic_ostream<_CharT, _Traits>&
1189 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1190 const discard_block<_UniformRandomNumberGenerator1,
1191 __p1, __r1>& __x);
1194 * Extracts the current state of a % subtract_with_carry random number
1195 * generator engine @p __x from the input stream @p __is.
1197 * @param __is An input stream.
1198 * @param __x A %discard_block random number generator engine.
1200 * @returns The input stream with the state of @p __x extracted or in
1201 * an error state.
1203 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1204 typename _CharT, typename _Traits>
1205 friend std::basic_istream<_CharT, _Traits>&
1206 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1207 discard_block<_UniformRandomNumberGenerator1,
1208 __p1, __r1>& __x);
1210 private:
1211 base_type _M_b;
1212 int _M_n;
1217 * James's luxury-level-3 integer adaptation of Luescher's generator.
1219 typedef discard_block<
1220 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1221 223,
1223 > ranlux3;
1226 * James's luxury-level-4 integer adaptation of Luescher's generator.
1228 typedef discard_block<
1229 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1230 389,
1232 > ranlux4;
1234 typedef discard_block<
1235 subtract_with_carry_01<float, 24, 10, 24>,
1236 223,
1238 > ranlux3_01;
1240 typedef discard_block<
1241 subtract_with_carry_01<float, 24, 10, 24>,
1242 389,
1244 > ranlux4_01;
1248 * A random number generator adaptor class that combines two random number
1249 * generator engines into a single output sequence.
1251 template<class _UniformRandomNumberGenerator1, int __s1,
1252 class _UniformRandomNumberGenerator2, int __s2>
1253 class xor_combine
1255 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1256 // result_type, ArithmeticTypeConcept)
1257 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1258 // result_type, ArithmeticTypeConcept)
1260 public:
1261 /** The type of the first underlying generator engine. */
1262 typedef _UniformRandomNumberGenerator1 base1_type;
1263 /** The type of the second underlying generator engine. */
1264 typedef _UniformRandomNumberGenerator2 base2_type;
1266 private:
1267 typedef typename base1_type::result_type _Result_type1;
1268 typedef typename base2_type::result_type _Result_type2;
1270 public:
1271 /** The type of the generated random value. */
1272 typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1)
1273 > sizeof(_Result_type2)),
1274 _Result_type1, _Result_type2>::__type result_type;
1276 // parameter values
1277 static const int shift1 = __s1;
1278 static const int shift2 = __s2;
1280 // constructors and member function
1281 xor_combine()
1282 : _M_b1(), _M_b2()
1283 { _M_initialize_max(); }
1285 xor_combine(const base1_type& __rng1, const base2_type& __rng2)
1286 : _M_b1(__rng1), _M_b2(__rng2)
1287 { _M_initialize_max(); }
1289 xor_combine(unsigned long __s)
1290 : _M_b1(__s), _M_b2(__s + 1)
1291 { _M_initialize_max(); }
1293 template<class _Gen>
1294 xor_combine(_Gen& __g)
1295 : _M_b1(__g), _M_b2(__g)
1296 { _M_initialize_max(); }
1298 void
1299 seed()
1301 _M_b1.seed();
1302 _M_b2.seed();
1305 template<class _Gen>
1306 void
1307 seed(_Gen& __g)
1309 _M_b1.seed(__g);
1310 _M_b2.seed(__g);
1313 const base1_type&
1314 base1() const
1315 { return _M_b1; }
1317 const base2_type&
1318 base2() const
1319 { return _M_b2; }
1321 result_type
1322 min() const
1323 { return 0; }
1325 result_type
1326 max() const
1327 { return _M_max; }
1330 * Gets the next random number in the sequence.
1332 // NB: Not exactly the TR1 formula, per N2079 instead.
1333 result_type
1334 operator()()
1336 return ((result_type(_M_b1() - _M_b1.min()) << shift1)
1337 ^ (result_type(_M_b2() - _M_b2.min()) << shift2));
1341 * Compares two %xor_combine random number generator objects of
1342 * the same type for equality.
1344 * @param __lhs A %xor_combine random number generator object.
1345 * @param __rhs Another %xor_combine random number generator
1346 * object.
1348 * @returns true if the two objects are equal, false otherwise.
1350 friend bool
1351 operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1353 return (__lhs.base1() == __rhs.base1())
1354 && (__lhs.base2() == __rhs.base2());
1358 * Compares two %xor_combine random number generator objects of
1359 * the same type for inequality.
1361 * @param __lhs A %xor_combine random number generator object.
1362 * @param __rhs Another %xor_combine random number generator
1363 * object.
1365 * @returns true if the two objects are not equal, false otherwise.
1367 friend bool
1368 operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1369 { return !(__lhs == __rhs); }
1372 * Inserts the current state of a %xor_combine random number
1373 * generator engine @p __x into the output stream @p __os.
1375 * @param __os An output stream.
1376 * @param __x A %xor_combine random number generator engine.
1378 * @returns The output stream with the state of @p __x inserted or in
1379 * an error state.
1381 template<class _UniformRandomNumberGenerator11, int __s11,
1382 class _UniformRandomNumberGenerator21, int __s21,
1383 typename _CharT, typename _Traits>
1384 friend std::basic_ostream<_CharT, _Traits>&
1385 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1386 const xor_combine<_UniformRandomNumberGenerator11, __s11,
1387 _UniformRandomNumberGenerator21, __s21>& __x);
1390 * Extracts the current state of a %xor_combine random number
1391 * generator engine @p __x from the input stream @p __is.
1393 * @param __is An input stream.
1394 * @param __x A %xor_combine random number generator engine.
1396 * @returns The input stream with the state of @p __x extracted or in
1397 * an error state.
1399 template<class _UniformRandomNumberGenerator11, int __s11,
1400 class _UniformRandomNumberGenerator21, int __s21,
1401 typename _CharT, typename _Traits>
1402 friend std::basic_istream<_CharT, _Traits>&
1403 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1404 xor_combine<_UniformRandomNumberGenerator11, __s11,
1405 _UniformRandomNumberGenerator21, __s21>& __x);
1407 private:
1408 void
1409 _M_initialize_max();
1411 result_type
1412 _M_initialize_max_aux(result_type, result_type, int);
1414 base1_type _M_b1;
1415 base2_type _M_b2;
1416 result_type _M_max;
1421 * A standard interface to a platform-specific non-deterministic
1422 * random number generator (if any are available).
1424 class random_device
1426 public:
1427 // types
1428 typedef unsigned int result_type;
1430 // constructors, destructors and member functions
1432 #ifdef _GLIBCXX_USE_RANDOM_TR1
1434 explicit
1435 random_device(const std::string& __token = "/dev/urandom")
1437 if ((__token != "/dev/urandom" && __token != "/dev/random")
1438 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1439 std::__throw_runtime_error(__N("random_device::"
1440 "random_device(const std::string&)"));
1443 ~random_device()
1444 { std::fclose(_M_file); }
1446 #else
1448 explicit
1449 random_device(const std::string& __token = "mt19937")
1450 : _M_mt(_M_strtoul(__token)) { }
1452 private:
1453 static unsigned long
1454 _M_strtoul(const std::string& __str)
1456 unsigned long __ret = 5489UL;
1457 if (__str != "mt19937")
1459 const char* __nptr = __str.c_str();
1460 char* __endptr;
1461 __ret = std::strtoul(__nptr, &__endptr, 0);
1462 if (*__nptr == '\0' || *__endptr != '\0')
1463 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1464 "(const std::string&)"));
1466 return __ret;
1469 public:
1471 #endif
1473 result_type
1474 min() const
1475 { return std::numeric_limits<result_type>::min(); }
1477 result_type
1478 max() const
1479 { return std::numeric_limits<result_type>::max(); }
1481 double
1482 entropy() const
1483 { return 0.0; }
1485 result_type
1486 operator()()
1488 #ifdef _GLIBCXX_USE_RANDOM_TR1
1489 result_type __ret;
1490 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1491 1, _M_file);
1492 return __ret;
1493 #else
1494 return _M_mt();
1495 #endif
1498 private:
1499 random_device(const random_device&);
1500 void operator=(const random_device&);
1502 #ifdef _GLIBCXX_USE_RANDOM_TR1
1503 FILE* _M_file;
1504 #else
1505 mt19937 _M_mt;
1506 #endif
1509 /// @} group tr1_random_generators
1512 * @addtogroup tr1_random_distributions Random Number Distributions
1513 * @ingroup tr1_random
1514 * @{
1518 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1519 * @ingroup tr1_random_distributions
1520 * @{
1524 * @brief Uniform discrete distribution for random numbers.
1525 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1526 * probability throughout the range.
1528 template<typename _IntType = int>
1529 class uniform_int
1531 __glibcxx_class_requires(_IntType, _IntegerConcept)
1533 public:
1534 /** The type of the parameters of the distribution. */
1535 typedef _IntType input_type;
1536 /** The type of the range of the distribution. */
1537 typedef _IntType result_type;
1539 public:
1541 * Constructs a uniform distribution object.
1543 explicit
1544 uniform_int(_IntType __min = 0, _IntType __max = 9)
1545 : _M_min(__min), _M_max(__max)
1547 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1551 * Gets the inclusive lower bound of the distribution range.
1553 result_type
1554 min() const
1555 { return _M_min; }
1558 * Gets the inclusive upper bound of the distribution range.
1560 result_type
1561 max() const
1562 { return _M_max; }
1565 * Resets the distribution state.
1567 * Does nothing for the uniform integer distribution.
1569 void
1570 reset() { }
1573 * Gets a uniformly distributed random number in the range
1574 * @f$(min, max)@f$.
1576 template<typename _UniformRandomNumberGenerator>
1577 result_type
1578 operator()(_UniformRandomNumberGenerator& __urng)
1580 typedef typename _UniformRandomNumberGenerator::result_type
1581 _UResult_type;
1582 return _M_call(__urng, _M_min, _M_max,
1583 typename is_integral<_UResult_type>::type());
1587 * Gets a uniform random number in the range @f$[0, n)@f$.
1589 * This function is aimed at use with std::random_shuffle.
1591 template<typename _UniformRandomNumberGenerator>
1592 result_type
1593 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1595 typedef typename _UniformRandomNumberGenerator::result_type
1596 _UResult_type;
1597 return _M_call(__urng, 0, __n - 1,
1598 typename is_integral<_UResult_type>::type());
1602 * Inserts a %uniform_int random number distribution @p __x into the
1603 * output stream @p os.
1605 * @param __os An output stream.
1606 * @param __x A %uniform_int random number distribution.
1608 * @returns The output stream with the state of @p __x inserted or in
1609 * an error state.
1611 template<typename _IntType1, typename _CharT, typename _Traits>
1612 friend std::basic_ostream<_CharT, _Traits>&
1613 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1614 const uniform_int<_IntType1>& __x);
1617 * Extracts a %uniform_int random number distribution
1618 * @p __x from the input stream @p __is.
1620 * @param __is An input stream.
1621 * @param __x A %uniform_int random number generator engine.
1623 * @returns The input stream with @p __x extracted or in an error state.
1625 template<typename _IntType1, typename _CharT, typename _Traits>
1626 friend std::basic_istream<_CharT, _Traits>&
1627 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1628 uniform_int<_IntType1>& __x);
1630 private:
1631 template<typename _UniformRandomNumberGenerator>
1632 result_type
1633 _M_call(_UniformRandomNumberGenerator& __urng,
1634 result_type __min, result_type __max, true_type);
1636 template<typename _UniformRandomNumberGenerator>
1637 result_type
1638 _M_call(_UniformRandomNumberGenerator& __urng,
1639 result_type __min, result_type __max, false_type)
1641 return result_type((__urng() - __urng.min())
1642 / (__urng.max() - __urng.min())
1643 * (__max - __min + 1)) + __min;
1646 _IntType _M_min;
1647 _IntType _M_max;
1652 * @brief A Bernoulli random number distribution.
1654 * Generates a sequence of true and false values with likelihood @f$ p @f$
1655 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1657 class bernoulli_distribution
1659 public:
1660 typedef int input_type;
1661 typedef bool result_type;
1663 public:
1665 * Constructs a Bernoulli distribution with likelihood @p p.
1667 * @param __p [IN] The likelihood of a true result being returned. Must
1668 * be in the interval @f$ [0, 1] @f$.
1670 explicit
1671 bernoulli_distribution(double __p = 0.5)
1672 : _M_p(__p)
1674 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1678 * Gets the @p p parameter of the distribution.
1680 double
1681 p() const
1682 { return _M_p; }
1685 * Resets the distribution state.
1687 * Does nothing for a Bernoulli distribution.
1689 void
1690 reset() { }
1693 * Gets the next value in the Bernoullian sequence.
1695 template<class _UniformRandomNumberGenerator>
1696 result_type
1697 operator()(_UniformRandomNumberGenerator& __urng)
1699 if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
1700 return true;
1701 return false;
1705 * Inserts a %bernoulli_distribution random number distribution
1706 * @p __x into the output stream @p __os.
1708 * @param __os An output stream.
1709 * @param __x A %bernoulli_distribution random number distribution.
1711 * @returns The output stream with the state of @p __x inserted or in
1712 * an error state.
1714 template<typename _CharT, typename _Traits>
1715 friend std::basic_ostream<_CharT, _Traits>&
1716 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1717 const bernoulli_distribution& __x);
1720 * Extracts a %bernoulli_distribution random number distribution
1721 * @p __x from the input stream @p __is.
1723 * @param __is An input stream.
1724 * @param __x A %bernoulli_distribution random number generator engine.
1726 * @returns The input stream with @p __x extracted or in an error state.
1728 template<typename _CharT, typename _Traits>
1729 friend std::basic_istream<_CharT, _Traits>&
1730 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1731 bernoulli_distribution& __x)
1732 { return __is >> __x._M_p; }
1734 private:
1735 double _M_p;
1740 * @brief A discrete geometric random number distribution.
1742 * The formula for the geometric probability mass function is
1743 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1744 * distribution.
1746 template<typename _IntType = int, typename _RealType = double>
1747 class geometric_distribution
1749 public:
1750 // types
1751 typedef _RealType input_type;
1752 typedef _IntType result_type;
1754 // constructors and member function
1755 explicit
1756 geometric_distribution(const _RealType& __p = _RealType(0.5))
1757 : _M_p(__p)
1759 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
1760 _M_initialize();
1764 * Gets the distribution parameter @p p.
1766 _RealType
1767 p() const
1768 { return _M_p; }
1770 void
1771 reset() { }
1773 template<class _UniformRandomNumberGenerator>
1774 result_type
1775 operator()(_UniformRandomNumberGenerator& __urng);
1778 * Inserts a %geometric_distribution random number distribution
1779 * @p __x into the output stream @p __os.
1781 * @param __os An output stream.
1782 * @param __x A %geometric_distribution random number distribution.
1784 * @returns The output stream with the state of @p __x inserted or in
1785 * an error state.
1787 template<typename _IntType1, typename _RealType1,
1788 typename _CharT, typename _Traits>
1789 friend std::basic_ostream<_CharT, _Traits>&
1790 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1791 const geometric_distribution<_IntType1, _RealType1>& __x);
1794 * Extracts a %geometric_distribution random number distribution
1795 * @p __x from the input stream @p __is.
1797 * @param __is An input stream.
1798 * @param __x A %geometric_distribution random number generator engine.
1800 * @returns The input stream with @p __x extracted or in an error state.
1802 template<typename _CharT, typename _Traits>
1803 friend std::basic_istream<_CharT, _Traits>&
1804 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1805 geometric_distribution& __x)
1807 __is >> __x._M_p;
1808 __x._M_initialize();
1809 return __is;
1812 private:
1813 void
1814 _M_initialize()
1815 { _M_log_p = std::log(_M_p); }
1817 _RealType _M_p;
1818 _RealType _M_log_p;
1822 template<typename _RealType>
1823 class normal_distribution;
1826 * @brief A discrete Poisson random number distribution.
1828 * The formula for the Poisson probability mass function is
1829 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1830 * parameter of the distribution.
1832 template<typename _IntType = int, typename _RealType = double>
1833 class poisson_distribution
1835 public:
1836 // types
1837 typedef _RealType input_type;
1838 typedef _IntType result_type;
1840 // constructors and member function
1841 explicit
1842 poisson_distribution(const _RealType& __mean = _RealType(1))
1843 : _M_mean(__mean), _M_nd()
1845 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
1846 _M_initialize();
1850 * Gets the distribution parameter @p mean.
1852 _RealType
1853 mean() const
1854 { return _M_mean; }
1856 void
1857 reset()
1858 { _M_nd.reset(); }
1860 template<class _UniformRandomNumberGenerator>
1861 result_type
1862 operator()(_UniformRandomNumberGenerator& __urng);
1865 * Inserts a %poisson_distribution random number distribution
1866 * @p __x into the output stream @p __os.
1868 * @param __os An output stream.
1869 * @param __x A %poisson_distribution random number distribution.
1871 * @returns The output stream with the state of @p __x inserted or in
1872 * an error state.
1874 template<typename _IntType1, typename _RealType1,
1875 typename _CharT, typename _Traits>
1876 friend std::basic_ostream<_CharT, _Traits>&
1877 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1878 const poisson_distribution<_IntType1, _RealType1>& __x);
1881 * Extracts a %poisson_distribution random number distribution
1882 * @p __x from the input stream @p __is.
1884 * @param __is An input stream.
1885 * @param __x A %poisson_distribution random number generator engine.
1887 * @returns The input stream with @p __x extracted or in an error state.
1889 template<typename _IntType1, typename _RealType1,
1890 typename _CharT, typename _Traits>
1891 friend std::basic_istream<_CharT, _Traits>&
1892 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1893 poisson_distribution<_IntType1, _RealType1>& __x);
1895 private:
1896 void
1897 _M_initialize();
1899 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1900 normal_distribution<_RealType> _M_nd;
1902 _RealType _M_mean;
1904 // Hosts either log(mean) or the threshold of the simple method.
1905 _RealType _M_lm_thr;
1906 #if _GLIBCXX_USE_C99_MATH_TR1
1907 _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
1908 #endif
1913 * @brief A discrete binomial random number distribution.
1915 * The formula for the binomial probability mass function is
1916 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1917 * and @f$ p @f$ are the parameters of the distribution.
1919 template<typename _IntType = int, typename _RealType = double>
1920 class binomial_distribution
1922 public:
1923 // types
1924 typedef _RealType input_type;
1925 typedef _IntType result_type;
1927 // constructors and member function
1928 explicit
1929 binomial_distribution(_IntType __t = 1,
1930 const _RealType& __p = _RealType(0.5))
1931 : _M_t(__t), _M_p(__p), _M_nd()
1933 _GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
1934 _M_initialize();
1938 * Gets the distribution @p t parameter.
1940 _IntType
1941 t() const
1942 { return _M_t; }
1945 * Gets the distribution @p p parameter.
1947 _RealType
1948 p() const
1949 { return _M_p; }
1951 void
1952 reset()
1953 { _M_nd.reset(); }
1955 template<class _UniformRandomNumberGenerator>
1956 result_type
1957 operator()(_UniformRandomNumberGenerator& __urng);
1960 * Inserts a %binomial_distribution random number distribution
1961 * @p __x into the output stream @p __os.
1963 * @param __os An output stream.
1964 * @param __x A %binomial_distribution random number distribution.
1966 * @returns The output stream with the state of @p __x inserted or in
1967 * an error state.
1969 template<typename _IntType1, typename _RealType1,
1970 typename _CharT, typename _Traits>
1971 friend std::basic_ostream<_CharT, _Traits>&
1972 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1973 const binomial_distribution<_IntType1, _RealType1>& __x);
1976 * Extracts a %binomial_distribution random number distribution
1977 * @p __x from the input stream @p __is.
1979 * @param __is An input stream.
1980 * @param __x A %binomial_distribution random number generator engine.
1982 * @returns The input stream with @p __x extracted or in an error state.
1984 template<typename _IntType1, typename _RealType1,
1985 typename _CharT, typename _Traits>
1986 friend std::basic_istream<_CharT, _Traits>&
1987 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1988 binomial_distribution<_IntType1, _RealType1>& __x);
1990 private:
1991 void
1992 _M_initialize();
1994 template<class _UniformRandomNumberGenerator>
1995 result_type
1996 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
1998 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1999 normal_distribution<_RealType> _M_nd;
2001 _RealType _M_q;
2002 #if _GLIBCXX_USE_C99_MATH_TR1
2003 _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
2004 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
2005 #endif
2006 _RealType _M_p;
2007 _IntType _M_t;
2009 bool _M_easy;
2012 /// @} group tr1_random_distributions_discrete
2015 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
2016 * @ingroup tr1_random_distributions
2017 * @{
2021 * @brief Uniform continuous distribution for random numbers.
2023 * A continuous random distribution on the range [min, max) with equal
2024 * probability throughout the range. The URNG should be real-valued and
2025 * deliver number in the range [0, 1).
2027 template<typename _RealType = double>
2028 class uniform_real
2030 public:
2031 // types
2032 typedef _RealType input_type;
2033 typedef _RealType result_type;
2035 public:
2037 * Constructs a uniform_real object.
2039 * @param __min [IN] The lower bound of the distribution.
2040 * @param __max [IN] The upper bound of the distribution.
2042 explicit
2043 uniform_real(_RealType __min = _RealType(0),
2044 _RealType __max = _RealType(1))
2045 : _M_min(__min), _M_max(__max)
2047 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
2050 result_type
2051 min() const
2052 { return _M_min; }
2054 result_type
2055 max() const
2056 { return _M_max; }
2058 void
2059 reset() { }
2061 template<class _UniformRandomNumberGenerator>
2062 result_type
2063 operator()(_UniformRandomNumberGenerator& __urng)
2064 { return (__urng() * (_M_max - _M_min)) + _M_min; }
2067 * Inserts a %uniform_real random number distribution @p __x into the
2068 * output stream @p __os.
2070 * @param __os An output stream.
2071 * @param __x A %uniform_real random number distribution.
2073 * @returns The output stream with the state of @p __x inserted or in
2074 * an error state.
2076 template<typename _RealType1, typename _CharT, typename _Traits>
2077 friend std::basic_ostream<_CharT, _Traits>&
2078 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2079 const uniform_real<_RealType1>& __x);
2082 * Extracts a %uniform_real random number distribution
2083 * @p __x from the input stream @p __is.
2085 * @param __is An input stream.
2086 * @param __x A %uniform_real random number generator engine.
2088 * @returns The input stream with @p __x extracted or in an error state.
2090 template<typename _RealType1, typename _CharT, typename _Traits>
2091 friend std::basic_istream<_CharT, _Traits>&
2092 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2093 uniform_real<_RealType1>& __x);
2095 private:
2096 _RealType _M_min;
2097 _RealType _M_max;
2102 * @brief An exponential continuous distribution for random numbers.
2104 * The formula for the exponential probability mass function is
2105 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2107 * <table border=1 cellpadding=10 cellspacing=0>
2108 * <caption align=top>Distribution Statistics</caption>
2109 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2110 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2111 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2112 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2113 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2114 * </table>
2116 template<typename _RealType = double>
2117 class exponential_distribution
2119 public:
2120 // types
2121 typedef _RealType input_type;
2122 typedef _RealType result_type;
2124 public:
2126 * Constructs an exponential distribution with inverse scale parameter
2127 * @f$ \lambda @f$.
2129 explicit
2130 exponential_distribution(const result_type& __lambda = result_type(1))
2131 : _M_lambda(__lambda)
2133 _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
2137 * Gets the inverse scale parameter of the distribution.
2139 _RealType
2140 lambda() const
2141 { return _M_lambda; }
2144 * Resets the distribution.
2146 * Has no effect on exponential distributions.
2148 void
2149 reset() { }
2151 template<class _UniformRandomNumberGenerator>
2152 result_type
2153 operator()(_UniformRandomNumberGenerator& __urng)
2154 { return -std::log(__urng()) / _M_lambda; }
2157 * Inserts a %exponential_distribution random number distribution
2158 * @p __x into the output stream @p __os.
2160 * @param __os An output stream.
2161 * @param __x A %exponential_distribution random number distribution.
2163 * @returns The output stream with the state of @p __x inserted or in
2164 * an error state.
2166 template<typename _RealType1, typename _CharT, typename _Traits>
2167 friend std::basic_ostream<_CharT, _Traits>&
2168 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2169 const exponential_distribution<_RealType1>& __x);
2172 * Extracts a %exponential_distribution random number distribution
2173 * @p __x from the input stream @p __is.
2175 * @param __is An input stream.
2176 * @param __x A %exponential_distribution random number
2177 * generator engine.
2179 * @returns The input stream with @p __x extracted or in an error state.
2181 template<typename _CharT, typename _Traits>
2182 friend std::basic_istream<_CharT, _Traits>&
2183 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2184 exponential_distribution& __x)
2185 { return __is >> __x._M_lambda; }
2187 private:
2188 result_type _M_lambda;
2193 * @brief A normal continuous distribution for random numbers.
2195 * The formula for the normal probability mass function is
2196 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2197 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2199 template<typename _RealType = double>
2200 class normal_distribution
2202 public:
2203 // types
2204 typedef _RealType input_type;
2205 typedef _RealType result_type;
2207 public:
2209 * Constructs a normal distribution with parameters @f$ mean @f$ and
2210 * @f$ \sigma @f$.
2212 explicit
2213 normal_distribution(const result_type& __mean = result_type(0),
2214 const result_type& __sigma = result_type(1))
2215 : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
2217 _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
2221 * Gets the mean of the distribution.
2223 _RealType
2224 mean() const
2225 { return _M_mean; }
2228 * Gets the @f$ \sigma @f$ of the distribution.
2230 _RealType
2231 sigma() const
2232 { return _M_sigma; }
2235 * Resets the distribution.
2237 void
2238 reset()
2239 { _M_saved_available = false; }
2241 template<class _UniformRandomNumberGenerator>
2242 result_type
2243 operator()(_UniformRandomNumberGenerator& __urng);
2246 * Inserts a %normal_distribution random number distribution
2247 * @p __x into the output stream @p __os.
2249 * @param __os An output stream.
2250 * @param __x A %normal_distribution random number distribution.
2252 * @returns The output stream with the state of @p __x inserted or in
2253 * an error state.
2255 template<typename _RealType1, typename _CharT, typename _Traits>
2256 friend std::basic_ostream<_CharT, _Traits>&
2257 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2258 const normal_distribution<_RealType1>& __x);
2261 * Extracts a %normal_distribution random number distribution
2262 * @p __x from the input stream @p __is.
2264 * @param __is An input stream.
2265 * @param __x A %normal_distribution random number generator engine.
2267 * @returns The input stream with @p __x extracted or in an error state.
2269 template<typename _RealType1, typename _CharT, typename _Traits>
2270 friend std::basic_istream<_CharT, _Traits>&
2271 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2272 normal_distribution<_RealType1>& __x);
2274 private:
2275 result_type _M_mean;
2276 result_type _M_sigma;
2277 result_type _M_saved;
2278 bool _M_saved_available;
2283 * @brief A gamma continuous distribution for random numbers.
2285 * The formula for the gamma probability mass function is
2286 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2288 template<typename _RealType = double>
2289 class gamma_distribution
2291 public:
2292 // types
2293 typedef _RealType input_type;
2294 typedef _RealType result_type;
2296 public:
2298 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2300 explicit
2301 gamma_distribution(const result_type& __alpha_val = result_type(1))
2302 : _M_alpha(__alpha_val)
2304 _GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
2305 _M_initialize();
2309 * Gets the @f$ \alpha @f$ of the distribution.
2311 _RealType
2312 alpha() const
2313 { return _M_alpha; }
2316 * Resets the distribution.
2318 void
2319 reset() { }
2321 template<class _UniformRandomNumberGenerator>
2322 result_type
2323 operator()(_UniformRandomNumberGenerator& __urng);
2326 * Inserts a %gamma_distribution random number distribution
2327 * @p __x into the output stream @p __os.
2329 * @param __os An output stream.
2330 * @param __x A %gamma_distribution random number distribution.
2332 * @returns The output stream with the state of @p __x inserted or in
2333 * an error state.
2335 template<typename _RealType1, typename _CharT, typename _Traits>
2336 friend std::basic_ostream<_CharT, _Traits>&
2337 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2338 const gamma_distribution<_RealType1>& __x);
2341 * Extracts a %gamma_distribution random number distribution
2342 * @p __x from the input stream @p __is.
2344 * @param __is An input stream.
2345 * @param __x A %gamma_distribution random number generator engine.
2347 * @returns The input stream with @p __x extracted or in an error state.
2349 template<typename _CharT, typename _Traits>
2350 friend std::basic_istream<_CharT, _Traits>&
2351 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2352 gamma_distribution& __x)
2354 __is >> __x._M_alpha;
2355 __x._M_initialize();
2356 return __is;
2359 private:
2360 void
2361 _M_initialize();
2363 result_type _M_alpha;
2365 // Hosts either lambda of GB or d of modified Vaduva's.
2366 result_type _M_l_d;
2369 /// @} group tr1_random_distributions_continuous
2370 /// @} group tr1_random_distributions
2371 /// @} group tr1_random
2374 _GLIBCXX_END_NAMESPACE_VERSION
2377 #endif // _GLIBCXX_TR1_RANDOM_H