2009-04-07 Andrew Stubbs <ams@codesourcery.com>
[official-gcc.git] / libstdc++-v3 / include / bits / random.h
blob5f725e7868268d324f258f425344d03abe4d2954
1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /**
31 * @file bits/random.h
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
36 #include <vector>
38 namespace std
41 // [26.4] Random number generation
43 /**
44 * @addtogroup std_random Random Number Generation
45 * A facility for generating random numbers on selected distributions.
46 * @{
49 /**
50 * @brief A function template for converting the output of a (integral)
51 * uniform random number generator to a floatng point result in the range
52 * [0-1).
54 template<typename _RealType, size_t __bits,
55 typename _UniformRandomNumberGenerator>
56 _RealType
57 generate_canonical(_UniformRandomNumberGenerator& __g);
59 class seed_seq;
62 * Implementation-space details.
64 namespace __detail
66 template<typename _UIntType, size_t __w,
67 bool = __w < static_cast<size_t>
68 (std::numeric_limits<_UIntType>::digits)>
69 struct _Shift
70 { static const _UIntType __value = 0; };
72 template<typename _UIntType, size_t __w>
73 struct _Shift<_UIntType, __w, true>
74 { static const _UIntType __value = _UIntType(1) << __w; };
76 // XXX need constexpr
77 template<typename _UIntType, size_t __w,
78 bool = __w <static_cast<size_t>
79 (std::numeric_limits<_UIntType>::digits)>
80 struct _ShiftMin1
82 static const _UIntType __value =
83 __gnu_cxx::__numeric_traits<_UIntType>::__max;
86 template<typename _UIntType, size_t __w>
87 struct _ShiftMin1<_UIntType, __w, true>
89 static const _UIntType __value =
90 (_UIntType(1) << __w) - _UIntType(1);
93 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
94 struct _Mod;
96 // Dispatch based on modulus value to prevent divide-by-zero compile-time
97 // errors when m == 0.
98 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
99 inline _Tp
100 __mod(_Tp __x)
101 { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
103 typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
104 unsigned, unsigned long>::__type _UInt32Type;
107 * An adaptor class for converting the output of any Generator into
108 * the input for a specific Distribution.
110 template<typename _Engine, typename _DInputType>
111 struct _Adaptor
114 public:
115 _Adaptor(_Engine& __g)
116 : _M_g(__g) { }
118 _DInputType
119 min() const
121 if (is_integral<_DInputType>::value)
122 return _M_g.min();
123 else
124 return _DInputType(0);
127 _DInputType
128 max() const
130 if (is_integral<_DInputType>::value)
131 return _M_g.max();
132 else
133 return _DInputType(1);
137 * Converts a value generated by the adapted random number generator
138 * into a value in the input domain for the dependent random number
139 * distribution.
141 * Because the type traits are compile time constants only the
142 * appropriate clause of the if statements will actually be emitted
143 * by the compiler.
145 _DInputType
146 operator()()
148 if (is_integral<_DInputType>::value)
149 return _M_g();
150 else
151 return generate_canonical<_DInputType,
152 numeric_limits<_DInputType>::digits,
153 _Engine>(_M_g);
156 private:
157 _Engine& _M_g;
159 } // namespace __detail
162 * @addtogroup std_random_generators Random Number Generators
163 * @ingroup std_random
165 * These classes define objects which provide random or pseudorandom
166 * numbers, either from a discrete or a continuous interval. The
167 * random number generator supplied as a part of this library are
168 * all uniform random number generators which provide a sequence of
169 * random number uniformly distributed over their range.
171 * A number generator is a function object with an operator() that
172 * takes zero arguments and returns a number.
174 * A compliant random number generator must satisfy the following
175 * requirements. <table border=1 cellpadding=10 cellspacing=0>
176 * <caption align=top>Random Number Generator Requirements</caption>
177 * <tr><td>To be documented.</td></tr> </table>
179 * @{
183 * @brief A model of a linear congruential random number generator.
185 * A random number generator that produces pseudorandom numbers using the
186 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
188 * The template parameter @p _UIntType must be an unsigned integral type
189 * large enough to store values up to (__m-1). If the template parameter
190 * @p __m is 0, the modulus @p __m used is
191 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
192 * parameters @p __a and @p __c must be less than @p __m.
194 * The size of the state is @f$ 1 @f$.
196 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
197 class linear_congruential_engine
199 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
200 static_assert(__m == 0 || (__a < __m && __c < __m),
201 "template arguments out of bounds"
202 " in linear_congruential_engine");
204 public:
205 /** The type of the generated random value. */
206 typedef _UIntType result_type;
208 /** The multiplier. */
209 static const result_type multiplier = __a;
210 /** An increment. */
211 static const result_type increment = __c;
212 /** The modulus. */
213 static const result_type modulus = __m;
214 static const result_type default_seed = 1UL;
217 * @brief Constructs a %linear_congruential_engine random number
218 * generator engine with seed @p __s. The default seed value
219 * is 1.
221 * @param __s The initial seed value.
223 explicit
224 linear_congruential_engine(result_type __s = default_seed)
225 { this->seed(__s); }
228 * @brief Constructs a %linear_congruential_engine random number
229 * generator engine seeded from the seed sequence @p __q.
231 * @param __q the seed sequence.
233 explicit
234 linear_congruential_engine(seed_seq& __q)
235 { this->seed(__q); }
238 * @brief Reseeds the %linear_congruential_engine random number generator
239 * engine sequence to the seed @p __s.
241 * @param __s The new seed.
243 void
244 seed(result_type __s = default_seed);
247 * @brief Reseeds the %linear_congruential_engine random number generator
248 * engine
249 * sequence using values from the seed sequence @p __q.
251 * @param __q the seed sequence.
253 void
254 seed(seed_seq& __q);
257 * @brief Gets the smallest possible value in the output range.
259 * The minimum depends on the @p __c parameter: if it is zero, the
260 * minimum generated must be > 0, otherwise 0 is allowed.
262 * @todo This should be constexpr.
264 result_type
265 min() const
266 { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
269 * @brief Gets the largest possible value in the output range.
271 * @todo This should be constexpr.
273 result_type
274 max() const
275 { return __m - 1; }
278 * @brief Discard a sequence of random numbers.
280 * @todo Look for a faster way to do discard.
282 void
283 discard(unsigned long long __z)
285 for (; __z != 0ULL; --__z)
286 (*this)();
290 * @brief Gets the next random number in the sequence.
292 result_type
293 operator()();
296 * @brief Compares two linear congruential random number generator
297 * objects of the same type for equality.
299 * @param __lhs A linear congruential random number generator object.
300 * @param __rhs Another linear congruential random number generator
301 * object.
303 * @returns true if the two objects are equal, false otherwise.
305 friend bool
306 operator==(const linear_congruential_engine& __lhs,
307 const linear_congruential_engine& __rhs)
308 { return __lhs._M_x == __rhs._M_x; }
311 * @brief Writes the textual representation of the state x(i) of x to
312 * @p __os.
314 * @param __os The output stream.
315 * @param __lcr A % linear_congruential_engine random number generator.
316 * @returns __os.
318 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
319 _UIntType1 __m1,
320 typename _CharT, typename _Traits>
321 friend std::basic_ostream<_CharT, _Traits>&
322 operator<<(std::basic_ostream<_CharT, _Traits>&,
323 const std::linear_congruential_engine<_UIntType1,
324 __a1, __c1, __m1>&);
327 * @brief Sets the state of the engine by reading its textual
328 * representation from @p __is.
330 * The textual representation must have been previously written using
331 * an output stream whose imbued locale and whose type's template
332 * specialization arguments _CharT and _Traits were the same as those
333 * of @p __is.
335 * @param __is The input stream.
336 * @param __lcr A % linear_congruential_engine random number generator.
337 * @returns __is.
339 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
340 _UIntType1 __m1,
341 typename _CharT, typename _Traits>
342 friend std::basic_istream<_CharT, _Traits>&
343 operator>>(std::basic_istream<_CharT, _Traits>&,
344 std::linear_congruential_engine<_UIntType1, __a1,
345 __c1, __m1>&);
347 private:
348 template<typename _Gen>
349 void
350 seed(_Gen& __g, true_type)
351 { return seed(static_cast<unsigned long>(__g)); }
353 template<typename _Gen>
354 void
355 seed(_Gen& __g, false_type);
357 _UIntType _M_x;
362 * A generalized feedback shift register discrete random number generator.
364 * This algorithm avoids multiplication and division and is designed to be
365 * friendly to a pipelined architecture. If the parameters are chosen
366 * correctly, this generator will produce numbers with a very long period and
367 * fairly good apparent entropy, although still not cryptographically strong.
369 * The best way to use this generator is with the predefined mt19937 class.
371 * This algorithm was originally invented by Makoto Matsumoto and
372 * Takuji Nishimura.
374 * @var word_size The number of bits in each element of the state vector.
375 * @var state_size The degree of recursion.
376 * @var shift_size The period parameter.
377 * @var mask_bits The separation point bit index.
378 * @var parameter_a The last row of the twist matrix.
379 * @var output_u The first right-shift tempering matrix parameter.
380 * @var output_s The first left-shift tempering matrix parameter.
381 * @var output_b The first left-shift tempering matrix mask.
382 * @var output_t The second left-shift tempering matrix parameter.
383 * @var output_c The second left-shift tempering matrix mask.
384 * @var output_l The second right-shift tempering matrix parameter.
386 template<typename _UIntType, size_t __w,
387 size_t __n, size_t __m, size_t __r,
388 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
389 _UIntType __b, size_t __t,
390 _UIntType __c, size_t __l, _UIntType __f>
391 class mersenne_twister_engine
393 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
395 static_assert(__m >= 1U,
396 "mersenne_twister_engine template arguments out of bounds");
397 static_assert(__n >= __m,
398 "mersenne_twister_engine template arguments out of bounds");
399 static_assert(__w >= __r,
400 "mersenne_twister_engine template arguments out of bounds");
401 static_assert(__w >= __u,
402 "mersenne_twister_engine template arguments out of bounds");
403 static_assert(__w >= __s,
404 "mersenne_twister_engine template arguments out of bounds");
405 static_assert(__w >= __t,
406 "mersenne_twister_engine template arguments out of bounds");
407 static_assert(__w >= __l,
408 "mersenne_twister_engine template arguments out of bounds");
409 static_assert(__w <=
410 static_cast<size_t>(numeric_limits<_UIntType>::digits),
411 "mersenne_twister_engine template arguments out of bounds");
412 static_assert(__a <= __detail::_ShiftMin1<_UIntType, __w>::__value,
413 "mersenne_twister_engine template arguments out of bounds");
414 static_assert(__b <= __detail::_ShiftMin1<_UIntType, __w>::__value,
415 "mersenne_twister_engine template arguments out of bounds");
416 static_assert(__c <= __detail::_ShiftMin1<_UIntType, __w>::__value,
417 "mersenne_twister_engine template arguments out of bounds");
419 public:
420 /** The type of the generated random value. */
421 typedef _UIntType result_type;
423 // parameter values
424 static const size_t word_size = __w;
425 static const size_t state_size = __n;
426 static const size_t shift_size = __m;
427 static const size_t mask_bits = __r;
428 static const result_type xor_mask = __a;
429 static const size_t tempering_u = __u;
430 static const result_type tempering_d = __d;
431 static const size_t tempering_s = __s;
432 static const result_type tempering_b = __b;
433 static const size_t tempering_t = __t;
434 static const result_type tempering_c = __c;
435 static const size_t tempering_l = __l;
436 static const size_t initialization_multiplier = __f;
437 static const result_type default_seed = 5489UL;
439 // constructors and member function
440 explicit
441 mersenne_twister_engine(result_type __sd = default_seed)
442 { seed(__sd); }
445 * @brief Constructs a %mersenne_twister_engine random number generator
446 * engine seeded from the seed sequence @p __q.
448 * @param __q the seed sequence.
450 explicit
451 mersenne_twister_engine(seed_seq& __q)
452 { seed(__q); }
454 void
455 seed(result_type __sd = default_seed);
457 void
458 seed(seed_seq& __q);
461 * @brief Gets the smallest possible value in the output range.
463 * @todo This should be constexpr.
465 result_type
466 min() const
467 { return 0; };
470 * @brief Gets the largest possible value in the output range.
472 * @todo This should be constexpr.
474 result_type
475 max() const
476 { return __detail::_ShiftMin1<_UIntType, __w>::__value; }
479 * @brief Discard a sequence of random numbers.
481 * @todo Look for a faster way to do discard.
483 void
484 discard(unsigned long long __z)
486 for (; __z != 0ULL; --__z)
487 (*this)();
490 result_type
491 operator()();
494 * @brief Compares two % mersenne_twister_engine random number generator
495 * objects of the same type for equality.
497 * @param __lhs A % mersenne_twister_engine random number generator
498 * object.
499 * @param __rhs Another % mersenne_twister_engine random number
500 * generator object.
502 * @returns true if the two objects are equal, false otherwise.
504 friend bool
505 operator==(const mersenne_twister_engine& __lhs,
506 const mersenne_twister_engine& __rhs)
507 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
510 * @brief Inserts the current state of a % mersenne_twister_engine
511 * random number generator engine @p __x into the output stream
512 * @p __os.
514 * @param __os An output stream.
515 * @param __x A % mersenne_twister_engine random number generator
516 * engine.
518 * @returns The output stream with the state of @p __x inserted or in
519 * an error state.
521 template<typename _UIntType1,
522 size_t __w1, size_t __n1,
523 size_t __m1, size_t __r1,
524 _UIntType1 __a1, size_t __u1,
525 _UIntType1 __d1, size_t __s1,
526 _UIntType1 __b1, size_t __t1,
527 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
528 typename _CharT, typename _Traits>
529 friend std::basic_ostream<_CharT, _Traits>&
530 operator<<(std::basic_ostream<_CharT, _Traits>&,
531 const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
532 __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
533 __l1, __f1>&);
536 * @brief Extracts the current state of a % mersenne_twister_engine
537 * random number generator engine @p __x from the input stream
538 * @p __is.
540 * @param __is An input stream.
541 * @param __x A % mersenne_twister_engine random number generator
542 * engine.
544 * @returns The input stream with the state of @p __x extracted or in
545 * an error state.
547 template<typename _UIntType1,
548 size_t __w1, size_t __n1,
549 size_t __m1, size_t __r1,
550 _UIntType1 __a1, size_t __u1,
551 _UIntType1 __d1, size_t __s1,
552 _UIntType1 __b1, size_t __t1,
553 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
554 typename _CharT, typename _Traits>
555 friend std::basic_istream<_CharT, _Traits>&
556 operator>>(std::basic_istream<_CharT, _Traits>&,
557 std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
558 __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
559 __l1, __f1>&);
561 private:
562 template<typename _Gen>
563 void
564 seed(_Gen& __g, true_type)
565 { return seed(static_cast<unsigned long>(__g)); }
567 template<typename _Gen>
568 void
569 seed(_Gen& __g, false_type);
571 _UIntType _M_x[state_size];
572 size_t _M_p;
576 * @brief The Marsaglia-Zaman generator.
578 * This is a model of a Generalized Fibonacci discrete random number
579 * generator, sometimes referred to as the SWC generator.
581 * A discrete random number generator that produces pseudorandom
582 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
583 * carry_{i-1}) \bmod m @f$.
585 * The size of the state is @f$ r @f$
586 * and the maximum period of the generator is @f$ m^r - m^s - 1 @f$.
588 * @var _M_x The state of the generator. This is a ring buffer.
589 * @var _M_carry The carry.
590 * @var _M_p Current index of x(i - r).
592 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
593 class subtract_with_carry_engine
595 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
596 static_assert(__s > 0U && __r > __s
597 && __w > 0U
598 && __w <= static_cast<size_t>(numeric_limits<_UIntType>::digits),
599 "template arguments out of bounds"
600 " in subtract_with_carry_engine");
602 public:
603 /** The type of the generated random value. */
604 typedef _UIntType result_type;
606 // parameter values
607 static const size_t word_size = __w;
608 static const size_t short_lag = __s;
609 static const size_t long_lag = __r;
610 static const result_type default_seed = 19780503;
613 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
614 * random number generator.
616 explicit
617 subtract_with_carry_engine(result_type __sd = default_seed)
618 { this->seed(__sd); }
621 * @brief Constructs a %subtract_with_carry_engine random number engine
622 * seeded from the seed sequence @p __q.
624 * @param __q the seed sequence.
626 explicit
627 subtract_with_carry_engine(seed_seq& __q)
628 { this->seed(__q); }
631 * @brief Seeds the initial state @f$ x_0 @f$ of the random number
632 * generator.
634 * N1688[4.19] modifies this as follows. If @p __value == 0,
635 * sets value to 19780503. In any case, with a linear
636 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
637 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
638 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
639 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
640 * set carry to 1, otherwise sets carry to 0.
642 void
643 seed(result_type __sd = default_seed);
646 * @brief Seeds the initial state @f$ x_0 @f$ of the
647 * % subtract_with_carry_engine random number generator.
649 void
650 seed(seed_seq& __q);
653 * @brief Gets the inclusive minimum value of the range of random
654 * integers returned by this generator.
656 * @todo This should be constexpr.
658 result_type
659 min() const
660 { return 0; }
663 * @brief Gets the inclusive maximum value of the range of random
664 * integers returned by this generator.
666 * @todo This should be constexpr.
668 result_type
669 max() const
670 { return _S_modulus - 1U; }
673 * @brief Discard a sequence of random numbers.
675 * @todo Look for a faster way to do discard.
677 void
678 discard(unsigned long long __z)
680 for (; __z != 0ULL; --__z)
681 (*this)();
685 * @brief Gets the next random number in the sequence.
687 result_type
688 operator()();
691 * @brief Compares two % subtract_with_carry_engine random number
692 * generator objects of the same type for equality.
694 * @param __lhs A % subtract_with_carry_engine random number generator
695 * object.
696 * @param __rhs Another % subtract_with_carry_engine random number
697 * generator object.
699 * @returns true if the two objects are equal, false otherwise.
701 friend bool
702 operator==(const subtract_with_carry_engine& __lhs,
703 const subtract_with_carry_engine& __rhs)
704 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
707 * @brief Inserts the current state of a % subtract_with_carry_engine
708 * random number generator engine @p __x into the output stream
709 * @p __os.
711 * @param __os An output stream.
712 * @param __x A % subtract_with_carry_engine random number generator
713 * engine.
715 * @returns The output stream with the state of @p __x inserted or in
716 * an error state.
718 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
719 typename _CharT, typename _Traits>
720 friend std::basic_ostream<_CharT, _Traits>&
721 operator<<(std::basic_ostream<_CharT, _Traits>&,
722 const std::subtract_with_carry_engine<_UIntType1, __w1,
723 __s1, __r1>&);
726 * @brief Extracts the current state of a % subtract_with_carry_engine
727 * random number generator engine @p __x from the input stream
728 * @p __is.
730 * @param __is An input stream.
731 * @param __x A % subtract_with_carry_engine random number generator engine.
733 * @returns The input stream with the state of @p __x extracted or in
734 * an error state.
736 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
737 typename _CharT, typename _Traits>
738 friend std::basic_istream<_CharT, _Traits>&
739 operator>>(std::basic_istream<_CharT, _Traits>&,
740 std::subtract_with_carry_engine<_UIntType1, __w1,
741 __s1, __r1>&);
743 private:
744 template<typename _Gen>
745 void
746 seed(_Gen& __g, true_type)
747 { return seed(static_cast<unsigned long>(__g)); }
749 template<typename _Gen>
750 void
751 seed(_Gen& __g, false_type);
753 static const size_t _S_modulus
754 = __detail::_Shift<_UIntType, __w>::__value;
756 _UIntType _M_x[long_lag];
757 _UIntType _M_carry;
758 size_t _M_p;
762 * Produces random numbers from some base engine by discarding blocks of
763 * data.
765 * 0 <= @p __r <= @p __p
767 template<typename _RandomNumberEngine, size_t __p, size_t __r>
768 class discard_block_engine
770 static_assert(__r >= 1U && __p >= __r,
771 "template arguments out of bounds"
772 " in discard_block_engine");
774 public:
775 /** The type of the generated random value. */
776 typedef typename _RandomNumberEngine::result_type result_type;
778 // parameter values
779 static const size_t block_size = __p;
780 static const size_t used_block = __r;
783 * @brief Constructs a default %discard_block_engine engine.
785 * The underlying engine is default constructed as well.
787 discard_block_engine()
788 : _M_b(), _M_n(0) { }
791 * @brief Copy constructs a %discard_block_engine engine.
793 * Copies an existing base class random number generator.
794 * @param rng An existing (base class) engine object.
796 explicit
797 discard_block_engine(const _RandomNumberEngine& __rne)
798 : _M_b(__rne), _M_n(0) { }
801 * @brief Move constructs a %discard_block_engine engine.
803 * Copies an existing base class random number generator.
804 * @param rng An existing (base class) engine object.
806 explicit
807 discard_block_engine(_RandomNumberEngine&& __rne)
808 : _M_b(std::move(__rne)), _M_n(0) { }
811 * @brief Seed constructs a %discard_block_engine engine.
813 * Constructs the underlying generator engine seeded with @p __s.
814 * @param __s A seed value for the base class engine.
816 explicit
817 discard_block_engine(result_type __s)
818 : _M_b(__s), _M_n(0) { }
821 * @brief Generator construct a %discard_block_engine engine.
823 * @param __q A seed sequence.
825 explicit
826 discard_block_engine(seed_seq& __q)
827 : _M_b(__q), _M_n(0)
831 * @brief Reseeds the %discard_block_engine object with the default
832 * seed for the underlying base class generator engine.
834 void
835 seed()
837 _M_b.seed();
838 _M_n = 0;
842 * @brief Reseeds the %discard_block_engine object with the default
843 * seed for the underlying base class generator engine.
845 void
846 seed(result_type __s)
848 _M_b.seed(__s);
849 _M_n = 0;
853 * @brief Reseeds the %discard_block_engine object with the given seed
854 * sequence.
855 * @param __q A seed generator function.
857 void
858 seed(seed_seq& __q)
860 _M_b.seed(__q);
861 _M_n = 0;
865 * @brief Gets a const reference to the underlying generator engine
866 * object.
868 const _RandomNumberEngine&
869 base() const
870 { return _M_b; }
873 * @brief Gets the minimum value in the generated random number range.
875 * @todo This should be constexpr.
877 result_type
878 min() const
879 { return _M_b.min(); }
882 * @brief Gets the maximum value in the generated random number range.
884 * @todo This should be constexpr.
886 result_type
887 max() const
888 { return _M_b.max(); }
891 * @brief Discard a sequence of random numbers.
893 * @todo Look for a faster way to do discard.
895 void
896 discard(unsigned long long __z)
898 for (; __z != 0ULL; --__z)
899 (*this)();
903 * @brief Gets the next value in the generated random number sequence.
905 result_type
906 operator()();
909 * @brief Compares two %discard_block_engine random number generator
910 * objects of the same type for equality.
912 * @param __lhs A %discard_block_engine random number generator object.
913 * @param __rhs Another %discard_block_engine random number generator
914 * object.
916 * @returns true if the two objects are equal, false otherwise.
918 friend bool
919 operator==(const discard_block_engine& __lhs,
920 const discard_block_engine& __rhs)
921 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
924 * @brief Inserts the current state of a %discard_block_engine random
925 * number generator engine @p __x into the output stream
926 * @p __os.
928 * @param __os An output stream.
929 * @param __x A %discard_block_engine random number generator engine.
931 * @returns The output stream with the state of @p __x inserted or in
932 * an error state.
934 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
935 typename _CharT, typename _Traits>
936 friend std::basic_ostream<_CharT, _Traits>&
937 operator<<(std::basic_ostream<_CharT, _Traits>&,
938 const std::discard_block_engine<_RandomNumberEngine1,
939 __p1, __r1>&);
942 * @brief Extracts the current state of a % subtract_with_carry_engine
943 * random number generator engine @p __x from the input stream
944 * @p __is.
946 * @param __is An input stream.
947 * @param __x A %discard_block_engine random number generator engine.
949 * @returns The input stream with the state of @p __x extracted or in
950 * an error state.
952 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
953 typename _CharT, typename _Traits>
954 friend std::basic_istream<_CharT, _Traits>&
955 operator>>(std::basic_istream<_CharT, _Traits>&,
956 std::discard_block_engine<_RandomNumberEngine1,
957 __p1, __r1>&);
959 private:
960 _RandomNumberEngine _M_b;
961 size_t _M_n;
965 * Produces random numbers by combining random numbers from some base
966 * engine to produce random numbers with a specifies number of bits @p __w.
968 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
969 class independent_bits_engine
971 static_assert(__w > 0U
972 && __w <=
973 static_cast<size_t>(numeric_limits<_UIntType>::digits),
974 "template arguments out of bounds "
975 "in independent_bits_engine");
977 public:
978 /** The type of the generated random value. */
979 typedef _UIntType result_type;
982 * @brief Constructs a default %independent_bits_engine engine.
984 * The underlying engine is default constructed as well.
986 independent_bits_engine()
987 : _M_b() { }
990 * @brief Copy constructs a %independent_bits_engine engine.
992 * Copies an existing base class random number generator.
993 * @param rng An existing (base class) engine object.
995 explicit
996 independent_bits_engine(const _RandomNumberEngine& __rne)
997 : _M_b(__rne) { }
1000 * @brief Move constructs a %independent_bits_engine engine.
1002 * Copies an existing base class random number generator.
1003 * @param rng An existing (base class) engine object.
1005 explicit
1006 independent_bits_engine(_RandomNumberEngine&& __rne)
1007 : _M_b(std::move(__rne)) { }
1010 * @brief Seed constructs a %independent_bits_engine engine.
1012 * Constructs the underlying generator engine seeded with @p __s.
1013 * @param __s A seed value for the base class engine.
1015 explicit
1016 independent_bits_engine(result_type __s)
1017 : _M_b(__s) { }
1020 * @brief Generator construct a %independent_bits_engine engine.
1022 * @param __q A seed sequence.
1024 explicit
1025 independent_bits_engine(seed_seq& __q)
1026 : _M_b(__q)
1030 * @brief Reseeds the %independent_bits_engine object with the default
1031 * seed for the underlying base class generator engine.
1033 void
1034 seed()
1035 { _M_b.seed(); }
1038 * @brief Reseeds the %independent_bits_engine object with the default
1039 * seed for the underlying base class generator engine.
1041 void
1042 seed(result_type __s)
1043 { _M_b.seed(__s); }
1046 * @brief Reseeds the %independent_bits_engine object with the given
1047 * seed sequence.
1048 * @param __q A seed generator function.
1050 void
1051 seed(seed_seq& __q)
1052 { _M_b.seed(__q); }
1055 * @brief Gets a const reference to the underlying generator engine
1056 * object.
1058 const _RandomNumberEngine&
1059 base() const
1060 { return _M_b; }
1063 * @brief Gets the minimum value in the generated random number range.
1065 * @todo This should be constexpr.
1067 result_type
1068 min() const
1069 { return 0U; }
1072 * @brief Gets the maximum value in the generated random number range.
1074 * @todo This should be constexpr.
1076 result_type
1077 max() const
1078 { return __detail::_ShiftMin1<_UIntType, __w>::__value; }
1081 * @brief Discard a sequence of random numbers.
1083 * @todo Look for a faster way to do discard.
1085 void
1086 discard(unsigned long long __z)
1088 for (; __z != 0ULL; --__z)
1089 (*this)();
1093 * @brief Gets the next value in the generated random number sequence.
1095 result_type
1096 operator()();
1099 * @brief Compares two %independent_bits_engine random number generator
1100 * objects of the same type for equality.
1102 * @param __lhs A %independent_bits_engine random number generator
1103 * object.
1104 * @param __rhs Another %independent_bits_engine random number generator
1105 * object.
1107 * @returns true if the two objects are equal, false otherwise.
1109 friend bool
1110 operator==(const independent_bits_engine& __lhs,
1111 const independent_bits_engine& __rhs)
1112 { return __lhs._M_b == __rhs._M_b; }
1115 * @brief Extracts the current state of a % subtract_with_carry_engine
1116 * random number generator engine @p __x from the input stream
1117 * @p __is.
1119 * @param __is An input stream.
1120 * @param __x A %independent_bits_engine random number generator
1121 * engine.
1123 * @returns The input stream with the state of @p __x extracted or in
1124 * an error state.
1126 template<typename _CharT, typename _Traits>
1127 friend std::basic_istream<_CharT, _Traits>&
1128 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1129 std::independent_bits_engine<_RandomNumberEngine,
1130 __w, _UIntType>& __x)
1132 __is >> __x._M_b;
1133 return __is;
1136 private:
1137 _RandomNumberEngine _M_b;
1141 * @brief Inserts the current state of a %independent_bits_engine random
1142 * number generator engine @p __x into the output stream @p __os.
1144 * @param __os An output stream.
1145 * @param __x A %independent_bits_engine random number generator engine.
1147 * @returns The output stream with the state of @p __x inserted or in
1148 * an error state.
1150 template<typename _RandomNumberEngine, size_t __w, typename _UIntType,
1151 typename _CharT, typename _Traits>
1152 std::basic_ostream<_CharT, _Traits>&
1153 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1154 const std::independent_bits_engine<_RandomNumberEngine,
1155 __w, _UIntType>& __x)
1157 __os << __x.base();
1158 return __os;
1162 * @brief Produces random numbers by combining random numbers from some
1163 * base engine to produce random numbers with a specifies number of bits
1164 * @p __w.
1166 template<typename _RandomNumberEngine, size_t __k>
1167 class shuffle_order_engine
1169 static_assert(__k >= 1U,
1170 "template arguments out of bounds"
1171 " in shuffle_order_engine");
1173 public:
1174 /** The type of the generated random value. */
1175 typedef typename _RandomNumberEngine::result_type result_type;
1177 static const size_t table_size = __k;
1180 * @brief Constructs a default %shuffle_order_engine engine.
1182 * The underlying engine is default constructed as well.
1184 shuffle_order_engine()
1185 : _M_b()
1186 { _M_initialize(); }
1189 * @brief Copy constructs a %shuffle_order_engine engine.
1191 * Copies an existing base class random number generator.
1192 * @param rng An existing (base class) engine object.
1194 explicit
1195 shuffle_order_engine(const _RandomNumberEngine& __rne)
1196 : _M_b(__rne)
1197 { _M_initialize(); }
1200 * @brief Move constructs a %shuffle_order_engine engine.
1202 * Copies an existing base class random number generator.
1203 * @param rng An existing (base class) engine object.
1205 explicit
1206 shuffle_order_engine(_RandomNumberEngine&& __rne)
1207 : _M_b(std::move(__rne))
1208 { _M_initialize(); }
1211 * @brief Seed constructs a %shuffle_order_engine engine.
1213 * Constructs the underlying generator engine seeded with @p __s.
1214 * @param __s A seed value for the base class engine.
1216 explicit
1217 shuffle_order_engine(result_type __s)
1218 : _M_b(__s)
1219 { _M_initialize(); }
1222 * @brief Generator construct a %shuffle_order_engine engine.
1224 * @param __q A seed sequence.
1226 explicit
1227 shuffle_order_engine(seed_seq& __q)
1228 : _M_b(__q)
1229 { _M_initialize(); }
1232 * @brief Reseeds the %shuffle_order_engine object with the default seed
1233 for the underlying base class generator engine.
1235 void
1236 seed()
1238 _M_b.seed();
1239 _M_initialize();
1243 * @brief Reseeds the %shuffle_order_engine object with the default seed
1244 * for the underlying base class generator engine.
1246 void
1247 seed(result_type __s)
1249 _M_b.seed(__s);
1250 _M_initialize();
1254 * @brief Reseeds the %shuffle_order_engine object with the given seed
1255 * sequence.
1256 * @param __q A seed generator function.
1258 void
1259 seed(seed_seq& __q)
1261 _M_b.seed(__q);
1262 _M_initialize();
1266 * Gets a const reference to the underlying generator engine object.
1268 const _RandomNumberEngine&
1269 base() const
1270 { return _M_b; }
1273 * Gets the minimum value in the generated random number range.
1275 * @todo This should be constexpr.
1277 result_type
1278 min() const
1279 { return _M_b.min(); }
1282 * Gets the maximum value in the generated random number range.
1284 * @todo This should be constexpr.
1286 result_type
1287 max() const
1288 { return _M_b.max(); }
1291 * Discard a sequence of random numbers.
1293 * @todo Look for a faster way to do discard.
1295 void
1296 discard(unsigned long long __z)
1298 for (; __z != 0ULL; --__z)
1299 (*this)();
1303 * Gets the next value in the generated random number sequence.
1305 result_type
1306 operator()();
1309 * Compares two %shuffle_order_engine random number generator objects
1310 * of the same type for equality.
1312 * @param __lhs A %shuffle_order_engine random number generator object.
1313 * @param __rhs Another %shuffle_order_engine random number generator
1314 * object.
1316 * @returns true if the two objects are equal, false otherwise.
1318 friend bool
1319 operator==(const shuffle_order_engine& __lhs,
1320 const shuffle_order_engine& __rhs)
1321 { return __lhs._M_b == __rhs._M_b; }
1324 * @brief Inserts the current state of a %shuffle_order_engine random
1325 * number generator engine @p __x into the output stream
1326 @p __os.
1328 * @param __os An output stream.
1329 * @param __x A %shuffle_order_engine random number generator engine.
1331 * @returns The output stream with the state of @p __x inserted or in
1332 * an error state.
1334 template<typename _RandomNumberEngine1, size_t __k1,
1335 typename _CharT, typename _Traits>
1336 friend std::basic_ostream<_CharT, _Traits>&
1337 operator<<(std::basic_ostream<_CharT, _Traits>&,
1338 const std::shuffle_order_engine<_RandomNumberEngine1,
1339 __k1>&);
1342 * @brief Extracts the current state of a % subtract_with_carry_engine
1343 * random number generator engine @p __x from the input stream
1344 * @p __is.
1346 * @param __is An input stream.
1347 * @param __x A %shuffle_order_engine random number generator engine.
1349 * @returns The input stream with the state of @p __x extracted or in
1350 * an error state.
1352 template<typename _RandomNumberEngine1, size_t __k1,
1353 typename _CharT, typename _Traits>
1354 friend std::basic_istream<_CharT, _Traits>&
1355 operator>>(std::basic_istream<_CharT, _Traits>&,
1356 std::shuffle_order_engine<_RandomNumberEngine1, __k1>&);
1358 private:
1359 void _M_initialize()
1361 for (size_t __i = 0; __i < __k; ++__i)
1362 _M_v[__i] = _M_b();
1363 _M_y = _M_b();
1366 _RandomNumberEngine _M_b;
1367 result_type _M_v[__k];
1368 result_type _M_y;
1372 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1374 typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1375 minstd_rand0;
1378 * An alternative LCR (Lehmer Generator function) .
1380 typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1381 minstd_rand;
1384 * The classic Mersenne Twister.
1386 * Reference:
1387 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
1388 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
1389 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1391 typedef mersenne_twister_engine<
1392 uint_fast32_t,
1393 32, 624, 397, 31,
1394 0x9908b0dfUL, 11,
1395 0xffffffffUL, 7,
1396 0x9d2c5680UL, 15,
1397 0xefc60000UL, 18, 1812433253UL> mt19937;
1400 * An alternative Mersenne Twister.
1402 typedef mersenne_twister_engine<
1403 uint_fast64_t,
1404 64, 312, 156, 31,
1405 0xb5026f5aa96619e9ULL, 29,
1406 0x5555555555555555ULL, 17,
1407 0x71d67fffeda60000ULL, 37,
1408 0xfff7eee000000000ULL, 43,
1409 6364136223846793005ULL> mt19937_64;
1414 typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
1415 ranlux24_base;
1417 typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
1419 typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
1420 ranlux48_base;
1422 typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
1427 typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
1432 typedef minstd_rand0 default_random_engine;
1435 * A standard interface to a platform-specific non-deterministic
1436 * random number generator (if any are available).
1438 class random_device
1440 public:
1441 /** The type of the generated random value. */
1442 typedef unsigned int result_type;
1444 // constructors, destructors and member functions
1446 #ifdef _GLIBCXX_USE_RANDOM_TR1
1448 explicit
1449 random_device(const std::string& __token = "/dev/urandom")
1451 if ((__token != "/dev/urandom" && __token != "/dev/random")
1452 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1453 std::__throw_runtime_error(__N("random_device::"
1454 "random_device(const std::string&)"));
1457 ~random_device()
1458 { std::fclose(_M_file); }
1460 #else
1462 explicit
1463 random_device(const std::string& __token = "mt19937")
1464 : _M_mt(_M_strtoul(__token)) { }
1466 private:
1467 static unsigned long
1468 _M_strtoul(const std::string& __str)
1470 unsigned long __ret = 5489UL;
1471 if (__str != "mt19937")
1473 const char* __nptr = __str.c_str();
1474 char* __endptr;
1475 __ret = std::strtoul(__nptr, &__endptr, 0);
1476 if (*__nptr == '\0' || *__endptr != '\0')
1477 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1478 "(const std::string&)"));
1480 return __ret;
1483 public:
1485 #endif
1487 result_type
1488 min() const
1489 { return std::numeric_limits<result_type>::min(); }
1491 result_type
1492 max() const
1493 { return std::numeric_limits<result_type>::max(); }
1495 double
1496 entropy() const
1497 { return 0.0; }
1499 result_type
1500 operator()()
1502 #ifdef _GLIBCXX_USE_RANDOM_TR1
1503 result_type __ret;
1504 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1505 1, _M_file);
1506 return __ret;
1507 #else
1508 return _M_mt();
1509 #endif
1512 // No copy functions.
1513 random_device(const random_device&) = delete;
1514 void operator=(const random_device&) = delete;
1516 private:
1518 #ifdef _GLIBCXX_USE_RANDOM_TR1
1519 FILE* _M_file;
1520 #else
1521 mt19937 _M_mt;
1522 #endif
1525 /* @} */ // group std_random_generators
1528 * @addtogroup std_random_distributions Random Number Distributions
1529 * @ingroup std_random
1530 * @{
1534 * @addtogroup std_random_distributions_uniform Uniform Distributions
1535 * @ingroup std_random_distributions
1536 * @{
1540 * @brief Uniform discrete distribution for random numbers.
1541 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1542 * probability throughout the range.
1544 template<typename _IntType = int>
1545 class uniform_int_distribution
1547 __glibcxx_class_requires(_IntType, _IntegerConcept)
1549 public:
1550 /** The type of the range of the distribution. */
1551 typedef _IntType result_type;
1552 /** Parameter type. */
1553 struct param_type
1555 typedef uniform_int_distribution<_IntType> distribution_type;
1557 explicit
1558 param_type(_IntType __a = 0, _IntType __b = 9)
1559 : _M_a(__a), _M_b(__b)
1561 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1564 result_type
1565 a() const
1566 { return _M_a; }
1568 result_type
1569 b() const
1570 { return _M_b; }
1572 friend bool
1573 operator==(const param_type& __p1, const param_type& __p2)
1574 { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
1576 private:
1577 _IntType _M_a;
1578 _IntType _M_b;
1581 public:
1583 * @brief Constructs a uniform distribution object.
1585 explicit
1586 uniform_int_distribution(_IntType __a = 0, _IntType __b = 9)
1587 : _M_param(__a, __b)
1590 explicit
1591 uniform_int_distribution(const param_type& __p)
1592 : _M_param(__p)
1596 * @brief Resets the distribution state.
1598 * Does nothing for the uniform integer distribution.
1600 void
1601 reset() { }
1603 result_type
1604 a() const
1605 { return _M_param.a(); }
1607 result_type
1608 b() const
1609 { return _M_param.b(); }
1612 * @brief Returns the inclusive lower bound of the distribution range.
1614 result_type
1615 min() const
1616 { return this->a(); }
1619 * @brief Returns the inclusive upper bound of the distribution range.
1621 result_type
1622 max() const
1623 { return this->b(); }
1626 * @brief Returns the parameter set of the distribution.
1628 param_type
1629 param() const
1630 { return _M_param; }
1633 * @brief Sets the parameter set of the distribution.
1634 * @param __param The new parameter set of the distribution.
1636 void
1637 param(const param_type& __param)
1638 { _M_param = __param; }
1641 * Gets a uniformly distributed random number in the range
1642 * @f$(min, max)@f$.
1644 template<typename _UniformRandomNumberGenerator>
1645 result_type
1646 operator()(_UniformRandomNumberGenerator& __urng)
1648 typedef typename _UniformRandomNumberGenerator::result_type
1649 _UResult_type;
1650 return _M_call(__urng, this->a(), this->b(),
1651 typename is_integral<_UResult_type>::type());
1655 * Gets a uniform random number in the range @f$[0, n)@f$.
1657 * This function is aimed at use with std::random_shuffle.
1659 template<typename _UniformRandomNumberGenerator>
1660 result_type
1661 operator()(_UniformRandomNumberGenerator& __urng,
1662 const param_type& __p)
1664 typedef typename _UniformRandomNumberGenerator::result_type
1665 _UResult_type;
1666 return _M_call(__urng, __p.a(), __p.b(),
1667 typename is_integral<_UResult_type>::type());
1670 private:
1671 template<typename _UniformRandomNumberGenerator>
1672 result_type
1673 _M_call(_UniformRandomNumberGenerator& __urng,
1674 result_type __min, result_type __max, true_type);
1676 template<typename _UniformRandomNumberGenerator>
1677 result_type
1678 _M_call(_UniformRandomNumberGenerator& __urng,
1679 result_type __min, result_type __max, false_type)
1681 return result_type((__urng() - __urng.min())
1682 / (__urng.max() - __urng.min())
1683 * (__max - __min + 1)) + __min;
1686 param_type _M_param;
1690 * @brief Return true if two uniform integer distributions have
1691 * the same parameters.
1693 template<typename _IntType>
1694 inline bool
1695 operator==(const std::uniform_int_distribution<_IntType>& __d1,
1696 const std::uniform_int_distribution<_IntType>& __d2)
1697 { return __d1.param() == __d2.param(); }
1700 * @brief Inserts a %uniform_int_distribution random number
1701 * distribution @p __x into the output stream @p os.
1703 * @param __os An output stream.
1704 * @param __x A %uniform_int_distribution random number distribution.
1706 * @returns The output stream with the state of @p __x inserted or in
1707 * an error state.
1709 template<typename _IntType, typename _CharT, typename _Traits>
1710 std::basic_ostream<_CharT, _Traits>&
1711 operator<<(std::basic_ostream<_CharT, _Traits>&,
1712 const std::uniform_int_distribution<_IntType>&);
1715 * @brief Extracts a %uniform_int_distribution random number distribution
1716 * @p __x from the input stream @p __is.
1718 * @param __is An input stream.
1719 * @param __x A %uniform_int_distribution random number generator engine.
1721 * @returns The input stream with @p __x extracted or in an error state.
1723 template<typename _IntType, typename _CharT, typename _Traits>
1724 std::basic_istream<_CharT, _Traits>&
1725 operator>>(std::basic_istream<_CharT, _Traits>&,
1726 std::uniform_int_distribution<_IntType>&);
1730 * @brief Uniform continuous distribution for random numbers.
1732 * A continuous random distribution on the range [min, max) with equal
1733 * probability throughout the range. The URNG should be real-valued and
1734 * deliver number in the range [0, 1).
1736 template<typename _RealType = double>
1737 class uniform_real_distribution
1739 public:
1740 /** The type of the range of the distribution. */
1741 typedef _RealType result_type;
1742 /** Parameter type. */
1743 struct param_type
1745 typedef uniform_real_distribution<_RealType> distribution_type;
1747 explicit
1748 param_type(_RealType __a = _RealType(0),
1749 _RealType __b = _RealType(1))
1750 : _M_a(__a), _M_b(__b)
1752 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1755 result_type
1756 a() const
1757 { return _M_a; }
1759 result_type
1760 b() const
1761 { return _M_b; }
1763 friend bool
1764 operator==(const param_type& __p1, const param_type& __p2)
1765 { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
1767 private:
1768 _RealType _M_a;
1769 _RealType _M_b;
1772 public:
1774 * @brief Constructs a uniform_real_distribution object.
1776 * @param __min [IN] The lower bound of the distribution.
1777 * @param __max [IN] The upper bound of the distribution.
1779 explicit
1780 uniform_real_distribution(_RealType __a = _RealType(0),
1781 _RealType __b = _RealType(1))
1782 : _M_param(__a, __b)
1785 explicit
1786 uniform_real_distribution(const param_type& __p)
1787 : _M_param(__p)
1791 * @brief Resets the distribution state.
1793 * Does nothing for the uniform real distribution.
1795 void
1796 reset() { }
1798 result_type
1799 a() const
1800 { return _M_param.a(); }
1802 result_type
1803 b() const
1804 { return _M_param.b(); }
1807 * @brief Returns the inclusive lower bound of the distribution range.
1809 result_type
1810 min() const
1811 { return this->a(); }
1814 * @brief Returns the inclusive upper bound of the distribution range.
1816 result_type
1817 max() const
1818 { return this->b(); }
1821 * @brief Returns the parameter set of the distribution.
1823 param_type
1824 param() const
1825 { return _M_param; }
1828 * @brief Sets the parameter set of the distribution.
1829 * @param __param The new parameter set of the distribution.
1831 void
1832 param(const param_type& __param)
1833 { _M_param = __param; }
1835 template<typename _UniformRandomNumberGenerator>
1836 result_type
1837 operator()(_UniformRandomNumberGenerator& __urng)
1839 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1840 __aurng(__urng);
1841 return (__aurng() * (this->b() - this->a())) + this->a();
1844 template<typename _UniformRandomNumberGenerator>
1845 result_type
1846 operator()(_UniformRandomNumberGenerator& __urng,
1847 const param_type& __p)
1849 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1850 __aurng(__urng);
1851 return (__aurng() * (__p.b() - __p.a())) + __p.a();
1854 private:
1855 param_type _M_param;
1859 * @brief Return true if two uniform real distributions have
1860 * the same parameters.
1862 template<typename _IntType>
1863 inline bool
1864 operator==(const std::uniform_real_distribution<_IntType>& __d1,
1865 const std::uniform_real_distribution<_IntType>& __d2)
1866 { return __d1.param() == __d2.param(); }
1869 * @brief Inserts a %uniform_real_distribution random number
1870 * distribution @p __x into the output stream @p __os.
1872 * @param __os An output stream.
1873 * @param __x A %uniform_real_distribution random number distribution.
1875 * @returns The output stream with the state of @p __x inserted or in
1876 * an error state.
1878 template<typename _RealType, typename _CharT, typename _Traits>
1879 std::basic_ostream<_CharT, _Traits>&
1880 operator<<(std::basic_ostream<_CharT, _Traits>&,
1881 const std::uniform_real_distribution<_RealType>&);
1884 * @brief Extracts a %uniform_real_distribution random number distribution
1885 * @p __x from the input stream @p __is.
1887 * @param __is An input stream.
1888 * @param __x A %uniform_real_distribution random number generator engine.
1890 * @returns The input stream with @p __x extracted or in an error state.
1892 template<typename _RealType, typename _CharT, typename _Traits>
1893 std::basic_istream<_CharT, _Traits>&
1894 operator>>(std::basic_istream<_CharT, _Traits>&,
1895 std::uniform_real_distribution<_RealType>&);
1897 /* @} */ // group std_random_distributions_uniform
1900 * @addtogroup std_random_distributions_normal Normal Distributions
1901 * @ingroup std_random_distributions
1902 * @{
1906 * @brief A normal continuous distribution for random numbers.
1908 * The formula for the normal probability density function is
1909 * @f$ p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1910 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } @f$.
1912 template<typename _RealType = double>
1913 class normal_distribution
1915 public:
1916 /** The type of the range of the distribution. */
1917 typedef _RealType result_type;
1918 /** Parameter type. */
1919 struct param_type
1921 typedef normal_distribution<_RealType> distribution_type;
1923 explicit
1924 param_type(_RealType __mean = _RealType(0),
1925 _RealType __stddev = _RealType(1))
1926 : _M_mean(__mean), _M_stddev(__stddev)
1928 _GLIBCXX_DEBUG_ASSERT(_M_stddev > _RealType(0));
1931 _RealType
1932 mean() const
1933 { return _M_mean; }
1935 _RealType
1936 stddev() const
1937 { return _M_stddev; }
1939 friend bool
1940 operator==(const param_type& __p1, const param_type& __p2)
1941 { return (__p1._M_mean == __p2._M_mean)
1942 && (__p1._M_stddev == __p2._M_stddev); }
1944 private:
1945 _RealType _M_mean;
1946 _RealType _M_stddev;
1949 public:
1951 * Constructs a normal distribution with parameters @f$ mean @f$ and
1952 * standard deviation.
1954 explicit
1955 normal_distribution(result_type __mean = result_type(0),
1956 result_type __stddev = result_type(1))
1957 : _M_param(__mean, __stddev), _M_saved_available(false)
1960 explicit
1961 normal_distribution(const param_type& __p)
1962 : _M_param(__p), _M_saved_available(false)
1966 * @brief Resets the distribution state.
1968 void
1969 reset()
1970 { _M_saved_available = false; }
1973 * @brief Returns the mean of the distribution.
1975 _RealType
1976 mean() const
1977 { return _M_param.mean(); }
1980 * @brief Returns the standard deviation of the distribution.
1982 _RealType
1983 stddev() const
1984 { return _M_param.stddev(); }
1987 * @brief Returns the parameter set of the distribution.
1989 param_type
1990 param() const
1991 { return _M_param; }
1994 * @brief Sets the parameter set of the distribution.
1995 * @param __param The new parameter set of the distribution.
1997 void
1998 param(const param_type& __param)
1999 { _M_param = __param; }
2002 * @brief Returns the greatest lower bound value of the distribution.
2004 result_type
2005 min() const
2006 { return std::numeric_limits<result_type>::min(); }
2009 * @brief Returns the least upper bound value of the distribution.
2011 result_type
2012 max() const
2013 { return std::numeric_limits<result_type>::max(); }
2015 template<typename _UniformRandomNumberGenerator>
2016 result_type
2017 operator()(_UniformRandomNumberGenerator& __urng)
2018 { return this->operator()(__urng, this->param()); }
2020 template<typename _UniformRandomNumberGenerator>
2021 result_type
2022 operator()(_UniformRandomNumberGenerator& __urng,
2023 const param_type& __p);
2026 * @brief Return true if two normal distributions have
2027 * the same parameters.
2029 template<typename _RealType1>
2030 friend bool
2031 operator==(const std::normal_distribution<_RealType1>& __d1,
2032 const std::normal_distribution<_RealType1>& __d2);
2035 * @brief Inserts a %normal_distribution random number distribution
2036 * @p __x into the output stream @p __os.
2038 * @param __os An output stream.
2039 * @param __x A %normal_distribution random number distribution.
2041 * @returns The output stream with the state of @p __x inserted or in
2042 * an error state.
2044 template<typename _RealType1, typename _CharT, typename _Traits>
2045 friend std::basic_ostream<_CharT, _Traits>&
2046 operator<<(std::basic_ostream<_CharT, _Traits>&,
2047 const std::normal_distribution<_RealType1>&);
2050 * @brief Extracts a %normal_distribution random number distribution
2051 * @p __x from the input stream @p __is.
2053 * @param __is An input stream.
2054 * @param __x A %normal_distribution random number generator engine.
2056 * @returns The input stream with @p __x extracted or in an error
2057 * state.
2059 template<typename _RealType1, typename _CharT, typename _Traits>
2060 friend std::basic_istream<_CharT, _Traits>&
2061 operator>>(std::basic_istream<_CharT, _Traits>&,
2062 std::normal_distribution<_RealType1>&);
2064 private:
2065 param_type _M_param;
2066 result_type _M_saved;
2067 bool _M_saved_available;
2072 * @brief A lognormal_distribution random number distribution.
2074 * The formula for the normal probability mass function is
2075 * @f$ p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2076 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}} @f$
2078 template<typename _RealType = double>
2079 class lognormal_distribution
2081 public:
2082 /** The type of the range of the distribution. */
2083 typedef _RealType result_type;
2084 /** Parameter type. */
2085 struct param_type
2087 typedef lognormal_distribution<_RealType> distribution_type;
2089 explicit
2090 param_type(_RealType __m = _RealType(0),
2091 _RealType __s = _RealType(1))
2092 : _M_m(__m), _M_s(__s)
2095 _RealType
2096 m() const
2097 { return _M_m; }
2099 _RealType
2100 s() const
2101 { return _M_s; }
2103 friend bool
2104 operator==(const param_type& __p1, const param_type& __p2)
2105 { return (__p1._M_m == __p2._M_m) && (__p1._M_s == __p2._M_s); }
2107 private:
2108 _RealType _M_m;
2109 _RealType _M_s;
2112 explicit
2113 lognormal_distribution(_RealType __m = _RealType(0),
2114 _RealType __s = _RealType(1))
2115 : _M_param(__m, __s)
2118 explicit
2119 lognormal_distribution(const param_type& __p)
2120 : _M_param(__p)
2124 * Resets the distribution state.
2126 void
2127 reset()
2133 _RealType
2134 m() const
2135 { return _M_param.m(); }
2137 _RealType
2138 s() const
2139 { return _M_param.s(); }
2142 * @brief Returns the parameter set of the distribution.
2144 param_type
2145 param() const
2146 { return _M_param; }
2149 * @brief Sets the parameter set of the distribution.
2150 * @param __param The new parameter set of the distribution.
2152 void
2153 param(const param_type& __param)
2154 { _M_param = __param; }
2157 * @brief Returns the greatest lower bound value of the distribution.
2159 result_type
2160 min() const
2161 { return result_type(0); }
2164 * @brief Returns the least upper bound value of the distribution.
2166 result_type
2167 max() const
2168 { return std::numeric_limits<result_type>::max(); }
2170 template<typename _UniformRandomNumberGenerator>
2171 result_type
2172 operator()(_UniformRandomNumberGenerator& __urng)
2173 { return this->operator()(__urng, this->param()); }
2175 template<typename _UniformRandomNumberGenerator>
2176 result_type
2177 operator()(_UniformRandomNumberGenerator& __urng,
2178 const param_type& __p);
2180 private:
2181 param_type _M_param;
2185 * @brief Return true if two lognormal distributions have
2186 * the same parameters.
2188 template<typename _RealType>
2189 inline bool
2190 operator==(const std::lognormal_distribution<_RealType>& __d1,
2191 const std::lognormal_distribution<_RealType>& __d2)
2192 { return __d1.param() == __d2.param(); }
2195 * @brief Inserts a %lognormal_distribution random number distribution
2196 * @p __x into the output stream @p __os.
2198 * @param __os An output stream.
2199 * @param __x A %lognormal_distribution random number distribution.
2201 * @returns The output stream with the state of @p __x inserted or in
2202 * an error state.
2204 template<typename _RealType, typename _CharT, typename _Traits>
2205 std::basic_ostream<_CharT, _Traits>&
2206 operator<<(std::basic_ostream<_CharT, _Traits>&,
2207 const std::lognormal_distribution<_RealType>&);
2210 * @brief Extracts a %lognormal_distribution random number distribution
2211 * @p __x from the input stream @p __is.
2213 * @param __is An input stream.
2214 * @param __x A %lognormal_distribution random number
2215 * generator engine.
2217 * @returns The input stream with @p __x extracted or in an error state.
2219 template<typename _RealType, typename _CharT, typename _Traits>
2220 std::basic_istream<_CharT, _Traits>&
2221 operator>>(std::basic_istream<_CharT, _Traits>&,
2222 std::lognormal_distribution<_RealType>&);
2226 * @brief A chi_squared_distribution random number distribution.
2228 * The formula for the normal probability mass function is
2229 * @f$ p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}} @f$
2231 template<typename _RealType = double>
2232 class chi_squared_distribution
2234 public:
2235 /** The type of the range of the distribution. */
2236 typedef _RealType result_type;
2237 /** Parameter type. */
2238 struct param_type
2240 typedef chi_squared_distribution<_RealType> distribution_type;
2242 explicit
2243 param_type(_RealType __n = _RealType(1))
2244 : _M_n(__n)
2247 _RealType
2248 n() const
2249 { return _M_n; }
2251 friend bool
2252 operator==(const param_type& __p1, const param_type& __p2)
2253 { return __p1._M_n == __p2._M_n; }
2255 private:
2256 _RealType _M_n;
2259 explicit
2260 chi_squared_distribution(_RealType __n = _RealType(1))
2261 : _M_param(__n)
2264 explicit
2265 chi_squared_distribution(const param_type& __p)
2266 : _M_param(__p)
2270 * @brief Resets the distribution state.
2272 void
2273 reset()
2279 _RealType
2280 n() const
2281 { return _M_param.n(); }
2284 * @brief Returns the parameter set of the distribution.
2286 param_type
2287 param() const
2288 { return _M_param; }
2291 * @brief Sets the parameter set of the distribution.
2292 * @param __param The new parameter set of the distribution.
2294 void
2295 param(const param_type& __param)
2296 { _M_param = __param; }
2299 * @brief Returns the greatest lower bound value of the distribution.
2301 result_type
2302 min() const
2303 { return result_type(0); }
2306 * @brief Returns the least upper bound value of the distribution.
2308 result_type
2309 max() const
2310 { return std::numeric_limits<result_type>::max(); }
2312 template<typename _UniformRandomNumberGenerator>
2313 result_type
2314 operator()(_UniformRandomNumberGenerator& __urng)
2315 { return this->operator()(__urng, this->param()); }
2317 template<typename _UniformRandomNumberGenerator>
2318 result_type
2319 operator()(_UniformRandomNumberGenerator& __urng,
2320 const param_type& __p);
2322 private:
2323 param_type _M_param;
2327 * @brief Return true if two Chi-squared distributions have
2328 * the same parameters.
2330 template<typename _RealType>
2331 inline bool
2332 operator==(const std::chi_squared_distribution<_RealType>& __d1,
2333 const std::chi_squared_distribution<_RealType>& __d2)
2334 { return __d1.param() == __d2.param(); }
2337 * @brief Inserts a %chi_squared_distribution random number distribution
2338 * @p __x into the output stream @p __os.
2340 * @param __os An output stream.
2341 * @param __x A %chi_squared_distribution random number distribution.
2343 * @returns The output stream with the state of @p __x inserted or in
2344 * an error state.
2346 template<typename _RealType, typename _CharT, typename _Traits>
2347 std::basic_ostream<_CharT, _Traits>&
2348 operator<<(std::basic_ostream<_CharT, _Traits>&,
2349 const std::chi_squared_distribution<_RealType>&);
2352 * @brief Extracts a %chi_squared_distribution random number distribution
2353 * @p __x from the input stream @p __is.
2355 * @param __is An input stream.
2356 * @param __x A %chi_squared_distribution random number
2357 * generator engine.
2359 * @returns The input stream with @p __x extracted or in an error state.
2361 template<typename _RealType, typename _CharT, typename _Traits>
2362 std::basic_istream<_CharT, _Traits>&
2363 operator>>(std::basic_istream<_CharT, _Traits>&,
2364 std::chi_squared_distribution<_RealType>&);
2368 * @brief A cauchy_distribution random number distribution.
2370 * The formula for the normal probability mass function is
2371 * @f$ p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1} @f$
2373 template<typename _RealType = double>
2374 class cauchy_distribution
2376 public:
2377 /** The type of the range of the distribution. */
2378 typedef _RealType result_type;
2379 /** Parameter type. */
2380 struct param_type
2382 typedef cauchy_distribution<_RealType> distribution_type;
2384 explicit
2385 param_type(_RealType __a = _RealType(0),
2386 _RealType __b = _RealType(1))
2387 : _M_a(__a), _M_b(__b)
2390 _RealType
2391 a() const
2392 { return _M_a; }
2394 _RealType
2395 b() const
2396 { return _M_b; }
2398 friend bool
2399 operator==(const param_type& __p1, const param_type& __p2)
2400 { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
2402 private:
2403 _RealType _M_a;
2404 _RealType _M_b;
2407 explicit
2408 cauchy_distribution(_RealType __a = _RealType(0),
2409 _RealType __b = _RealType(1))
2410 : _M_param(__a, __b)
2413 explicit
2414 cauchy_distribution(const param_type& __p)
2415 : _M_param(__p)
2419 * @brief Resets the distribution state.
2421 void
2422 reset()
2428 _RealType
2429 a() const
2430 { return _M_param.a(); }
2432 _RealType
2433 b() const
2434 { return _M_param.b(); }
2437 * @brief Returns the parameter set of the distribution.
2439 param_type
2440 param() const
2441 { return _M_param; }
2444 * @brief Sets the parameter set of the distribution.
2445 * @param __param The new parameter set of the distribution.
2447 void
2448 param(const param_type& __param)
2449 { _M_param = __param; }
2452 * @brief Returns the greatest lower bound value of the distribution.
2454 result_type
2455 min() const
2456 { return std::numeric_limits<result_type>::min(); }
2459 * @brief Returns the least upper bound value of the distribution.
2461 result_type
2462 max() const
2463 { return std::numeric_limits<result_type>::max(); }
2465 template<typename _UniformRandomNumberGenerator>
2466 result_type
2467 operator()(_UniformRandomNumberGenerator& __urng)
2468 { return this->operator()(__urng, this->param()); }
2470 template<typename _UniformRandomNumberGenerator>
2471 result_type
2472 operator()(_UniformRandomNumberGenerator& __urng,
2473 const param_type& __p);
2475 private:
2476 param_type _M_param;
2480 * @brief Return true if two Cauchy distributions have
2481 * the same parameters.
2483 template<typename _RealType>
2484 inline bool
2485 operator==(const std::cauchy_distribution<_RealType>& __d1,
2486 const std::cauchy_distribution<_RealType>& __d2)
2487 { return __d1.param() == __d2.param(); }
2490 * @brief Inserts a %cauchy_distribution random number distribution
2491 * @p __x into the output stream @p __os.
2493 * @param __os An output stream.
2494 * @param __x A %cauchy_distribution random number distribution.
2496 * @returns The output stream with the state of @p __x inserted or in
2497 * an error state.
2499 template<typename _RealType, typename _CharT, typename _Traits>
2500 std::basic_ostream<_CharT, _Traits>&
2501 operator<<(std::basic_ostream<_CharT, _Traits>&,
2502 const std::cauchy_distribution<_RealType>&);
2505 * @brief Extracts a %cauchy_distribution random number distribution
2506 * @p __x from the input stream @p __is.
2508 * @param __is An input stream.
2509 * @param __x A %cauchy_distribution random number
2510 * generator engine.
2512 * @returns The input stream with @p __x extracted or in an error state.
2514 template<typename _RealType, typename _CharT, typename _Traits>
2515 std::basic_istream<_CharT, _Traits>&
2516 operator>>(std::basic_istream<_CharT, _Traits>&,
2517 std::cauchy_distribution<_RealType>&);
2521 * @brief A fisher_f_distribution random number distribution.
2523 * The formula for the normal probability mass function is
2524 * @f$ p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2525 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2526 * (1 + \frac{mx}{n})^{-(m+n)/2} @f$
2528 template<typename _RealType = double>
2529 class fisher_f_distribution
2531 public:
2532 /** The type of the range of the distribution. */
2533 typedef _RealType result_type;
2534 /** Parameter type. */
2535 struct param_type
2537 typedef fisher_f_distribution<_RealType> distribution_type;
2539 explicit
2540 param_type(_RealType __m = _RealType(1),
2541 _RealType __n = _RealType(1))
2542 : _M_m(__m), _M_n(__n)
2545 _RealType
2546 m() const
2547 { return _M_m; }
2549 _RealType
2550 n() const
2551 { return _M_n; }
2553 friend bool
2554 operator==(const param_type& __p1, const param_type& __p2)
2555 { return (__p1._M_m == __p2._M_m) && (__p1._M_n == __p2._M_n); }
2557 private:
2558 _RealType _M_m;
2559 _RealType _M_n;
2562 explicit
2563 fisher_f_distribution(_RealType __m = _RealType(1),
2564 _RealType __n = _RealType(1))
2565 : _M_param(__m, __n)
2568 explicit
2569 fisher_f_distribution(const param_type& __p)
2570 : _M_param(__p)
2574 * @brief Resets the distribution state.
2576 void
2577 reset()
2583 _RealType
2584 m() const
2585 { return _M_param.m(); }
2587 _RealType
2588 n() const
2589 { return _M_param.n(); }
2592 * @brief Returns the parameter set of the distribution.
2594 param_type
2595 param() const
2596 { return _M_param; }
2599 * @brief Sets the parameter set of the distribution.
2600 * @param __param The new parameter set of the distribution.
2602 void
2603 param(const param_type& __param)
2604 { _M_param = __param; }
2607 * @brief Returns the greatest lower bound value of the distribution.
2609 result_type
2610 min() const
2611 { return result_type(0); }
2614 * @brief Returns the least upper bound value of the distribution.
2616 result_type
2617 max() const
2618 { return std::numeric_limits<result_type>::max(); }
2620 template<typename _UniformRandomNumberGenerator>
2621 result_type
2622 operator()(_UniformRandomNumberGenerator& __urng)
2623 { return this->operator()(__urng, this->param()); }
2625 template<typename _UniformRandomNumberGenerator>
2626 result_type
2627 operator()(_UniformRandomNumberGenerator& __urng,
2628 const param_type& __p);
2630 private:
2631 param_type _M_param;
2635 * @brief Return true if two Fisher f distributions have
2636 * the same parameters.
2638 template<typename _RealType>
2639 inline bool
2640 operator==(const std::fisher_f_distribution<_RealType>& __d1,
2641 const std::fisher_f_distribution<_RealType>& __d2)
2642 { return __d1.param() == __d2.param(); }
2645 * @brief Inserts a %fisher_f_distribution random number distribution
2646 * @p __x into the output stream @p __os.
2648 * @param __os An output stream.
2649 * @param __x A %fisher_f_distribution random number distribution.
2651 * @returns The output stream with the state of @p __x inserted or in
2652 * an error state.
2654 template<typename _RealType, typename _CharT, typename _Traits>
2655 std::basic_ostream<_CharT, _Traits>&
2656 operator<<(std::basic_ostream<_CharT, _Traits>&,
2657 const std::fisher_f_distribution<_RealType>&);
2660 * @brief Extracts a %fisher_f_distribution random number distribution
2661 * @p __x from the input stream @p __is.
2663 * @param __is An input stream.
2664 * @param __x A %fisher_f_distribution random number
2665 * generator engine.
2667 * @returns The input stream with @p __x extracted or in an error state.
2669 template<typename _RealType, typename _CharT, typename _Traits>
2670 std::basic_istream<_CharT, _Traits>&
2671 operator>>(std::basic_istream<_CharT, _Traits>&,
2672 std::fisher_f_distribution<_RealType>&);
2676 * @brief A student_t_distribution random number distribution.
2678 * The formula for the normal probability mass function is
2679 * @f$ p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2680 * (1 + \frac{x^2}{n}) ^{-(n+1)/2} @f$
2682 template<typename _RealType = double>
2683 class student_t_distribution
2685 public:
2686 /** The type of the range of the distribution. */
2687 typedef _RealType result_type;
2688 /** Parameter type. */
2689 struct param_type
2691 typedef student_t_distribution<_RealType> distribution_type;
2693 explicit
2694 param_type(_RealType __n = _RealType(1))
2695 : _M_n(__n)
2698 _RealType
2699 n() const
2700 { return _M_n; }
2702 friend bool
2703 operator==(const param_type& __p1, const param_type& __p2)
2704 { return __p1._M_n == __p2._M_n; }
2706 private:
2707 _RealType _M_n;
2710 explicit
2711 student_t_distribution(_RealType __n = _RealType(1))
2712 : _M_param(__n)
2715 explicit
2716 student_t_distribution(const param_type& __p)
2717 : _M_param(__p)
2721 * @brief Resets the distribution state.
2723 void
2724 reset()
2730 _RealType
2731 n() const
2732 { return _M_param.n(); }
2735 * @brief Returns the parameter set of the distribution.
2737 param_type
2738 param() const
2739 { return _M_param; }
2742 * @brief Sets the parameter set of the distribution.
2743 * @param __param The new parameter set of the distribution.
2745 void
2746 param(const param_type& __param)
2747 { _M_param = __param; }
2750 * @brief Returns the greatest lower bound value of the distribution.
2752 result_type
2753 min() const
2754 { return std::numeric_limits<result_type>::min(); }
2757 * @brief Returns the least upper bound value of the distribution.
2759 result_type
2760 max() const
2761 { return std::numeric_limits<result_type>::max(); }
2763 template<typename _UniformRandomNumberGenerator>
2764 result_type
2765 operator()(_UniformRandomNumberGenerator& __urng)
2766 { return this->operator()(__urng, this->param()); }
2768 template<typename _UniformRandomNumberGenerator>
2769 result_type
2770 operator()(_UniformRandomNumberGenerator& __urng,
2771 const param_type& __p);
2773 private:
2774 template<typename _UniformRandomNumberGenerator>
2775 result_type
2776 _M_gaussian(_UniformRandomNumberGenerator& __urng,
2777 const result_type __sigma);
2779 param_type _M_param;
2783 * @brief Return true if two Student t distributions have
2784 * the same parameters.
2786 template<typename _RealType>
2787 inline bool
2788 operator==(const std::student_t_distribution<_RealType>& __d1,
2789 const std::student_t_distribution<_RealType>& __d2)
2790 { return __d1.param() == __d2.param(); }
2793 * @brief Inserts a %student_t_distribution random number distribution
2794 * @p __x into the output stream @p __os.
2796 * @param __os An output stream.
2797 * @param __x A %student_t_distribution random number distribution.
2799 * @returns The output stream with the state of @p __x inserted or in
2800 * an error state.
2802 template<typename _RealType, typename _CharT, typename _Traits>
2803 std::basic_ostream<_CharT, _Traits>&
2804 operator<<(std::basic_ostream<_CharT, _Traits>&,
2805 const std::student_t_distribution<_RealType>&);
2808 * @brief Extracts a %student_t_distribution random number distribution
2809 * @p __x from the input stream @p __is.
2811 * @param __is An input stream.
2812 * @param __x A %student_t_distribution random number
2813 * generator engine.
2815 * @returns The input stream with @p __x extracted or in an error state.
2817 template<typename _RealType, typename _CharT, typename _Traits>
2818 std::basic_istream<_CharT, _Traits>&
2819 operator>>(std::basic_istream<_CharT, _Traits>&,
2820 std::student_t_distribution<_RealType>&);
2822 /* @} */ // group std_random_distributions_normal
2825 * @addtogroup std_random_distributions_bernoulli Bernoulli Distributions
2826 * @ingroup std_random_distributions
2827 * @{
2831 * @brief A Bernoulli random number distribution.
2833 * Generates a sequence of true and false values with likelihood @f$ p @f$
2834 * that true will come up and @f$ (1 - p) @f$ that false will appear.
2836 class bernoulli_distribution
2838 public:
2839 /** The type of the range of the distribution. */
2840 typedef bool result_type;
2841 /** Parameter type. */
2842 struct param_type
2844 typedef bernoulli_distribution distribution_type;
2846 explicit
2847 param_type(double __p = 0.5)
2848 : _M_p(__p)
2850 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
2853 double
2854 p() const
2855 { return _M_p; }
2857 friend bool
2858 operator==(const param_type& __p1, const param_type& __p2)
2859 { return __p1._M_p == __p2._M_p; }
2861 private:
2862 double _M_p;
2865 public:
2867 * @brief Constructs a Bernoulli distribution with likelihood @p p.
2869 * @param __p [IN] The likelihood of a true result being returned.
2870 * Must be in the interval @f$ [0, 1] @f$.
2872 explicit
2873 bernoulli_distribution(double __p = 0.5)
2874 : _M_param(__p)
2877 explicit
2878 bernoulli_distribution(const param_type& __p)
2879 : _M_param(__p)
2883 * @brief Resets the distribution state.
2885 * Does nothing for a Bernoulli distribution.
2887 void
2888 reset() { }
2891 * @brief Returns the @p p parameter of the distribution.
2893 double
2894 p() const
2895 { return _M_param.p(); }
2898 * @brief Returns the parameter set of the distribution.
2900 param_type
2901 param() const
2902 { return _M_param; }
2905 * @brief Sets the parameter set of the distribution.
2906 * @param __param The new parameter set of the distribution.
2908 void
2909 param(const param_type& __param)
2910 { _M_param = __param; }
2913 * @brief Returns the greatest lower bound value of the distribution.
2915 result_type
2916 min() const
2917 { return std::numeric_limits<result_type>::min(); }
2920 * @brief Returns the least upper bound value of the distribution.
2922 result_type
2923 max() const
2924 { return std::numeric_limits<result_type>::max(); }
2927 * @brief Returns the next value in the Bernoullian sequence.
2929 template<typename _UniformRandomNumberGenerator>
2930 result_type
2931 operator()(_UniformRandomNumberGenerator& __urng)
2933 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2934 __aurng(__urng);
2935 if ((__aurng() - __aurng.min())
2936 < this->p() * (__aurng.max() - __aurng.min()))
2937 return true;
2938 return false;
2941 template<typename _UniformRandomNumberGenerator>
2942 result_type
2943 operator()(_UniformRandomNumberGenerator& __urng,
2944 const param_type& __p)
2946 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2947 __aurng(__urng);
2948 if ((__aurng() - __aurng.min())
2949 < __p.p() * (__aurng.max() - __aurng.min()))
2950 return true;
2951 return false;
2954 private:
2955 param_type _M_param;
2959 * @brief Return true if two Bernoulli distributions have
2960 * the same parameters.
2962 inline bool
2963 operator==(const std::bernoulli_distribution& __d1,
2964 const std::bernoulli_distribution& __d2)
2965 { return __d1.param() == __d2.param(); }
2968 * @brief Inserts a %bernoulli_distribution random number distribution
2969 * @p __x into the output stream @p __os.
2971 * @param __os An output stream.
2972 * @param __x A %bernoulli_distribution random number distribution.
2974 * @returns The output stream with the state of @p __x inserted or in
2975 * an error state.
2977 template<typename _CharT, typename _Traits>
2978 std::basic_ostream<_CharT, _Traits>&
2979 operator<<(std::basic_ostream<_CharT, _Traits>&,
2980 const std::bernoulli_distribution&);
2983 * @brief Extracts a %bernoulli_distribution random number distribution
2984 * @p __x from the input stream @p __is.
2986 * @param __is An input stream.
2987 * @param __x A %bernoulli_distribution random number generator engine.
2989 * @returns The input stream with @p __x extracted or in an error state.
2991 template<typename _CharT, typename _Traits>
2992 std::basic_istream<_CharT, _Traits>&
2993 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2994 std::bernoulli_distribution& __x)
2996 double __p;
2997 __is >> __p;
2998 __x.param(bernoulli_distribution::param_type(__p));
2999 return __is;
3004 * @brief A discrete binomial random number distribution.
3006 * The formula for the binomial probability density function is
3007 * @f$ p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3008 * and @f$ p @f$ are the parameters of the distribution.
3010 template<typename _IntType = int>
3011 class binomial_distribution
3013 __glibcxx_class_requires(_IntType, _IntegerConcept)
3015 public:
3016 /** The type of the range of the distribution. */
3017 typedef _IntType result_type;
3018 /** Parameter type. */
3019 struct param_type
3021 typedef binomial_distribution<_IntType> distribution_type;
3022 friend class binomial_distribution<_IntType>;
3024 explicit
3025 param_type(_IntType __t = _IntType(1), double __p = 0.5)
3026 : _M_t(__t), _M_p(__p)
3028 _GLIBCXX_DEBUG_ASSERT((_M_t >= _IntType(0))
3029 && (_M_p >= 0.0)
3030 && (_M_p <= 1.0));
3031 _M_initialize();
3034 _IntType
3035 t() const
3036 { return _M_t; }
3038 double
3039 p() const
3040 { return _M_p; }
3042 friend bool
3043 operator==(const param_type& __p1, const param_type& __p2)
3044 { return (__p1._M_t == __p2._M_t) && (__p1._M_p == __p2._M_p); }
3046 private:
3047 void
3048 _M_initialize();
3050 _IntType _M_t;
3051 double _M_p;
3053 double _M_q;
3054 #if _GLIBCXX_USE_C99_MATH_TR1
3055 double _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
3056 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
3057 #endif
3058 bool _M_easy;
3061 // constructors and member function
3062 explicit
3063 binomial_distribution(_IntType __t = _IntType(1),
3064 double __p = 0.5)
3065 : _M_param(__t, __p), _M_nd()
3068 explicit
3069 binomial_distribution(const param_type& __p)
3070 : _M_param(__p), _M_nd()
3074 * @brief Resets the distribution state.
3076 void
3077 reset()
3078 { _M_nd.reset(); }
3081 * @brief Returns the distribution @p t parameter.
3083 _IntType
3084 t() const
3085 { return _M_param.t(); }
3088 * @brief Returns the distribution @p p parameter.
3090 double
3091 p() const
3092 { return _M_param.p(); }
3095 * @brief Returns the parameter set of the distribution.
3097 param_type
3098 param() const
3099 { return _M_param; }
3102 * @brief Sets the parameter set of the distribution.
3103 * @param __param The new parameter set of the distribution.
3105 void
3106 param(const param_type& __param)
3107 { _M_param = __param; }
3110 * @brief Returns the greatest lower bound value of the distribution.
3112 result_type
3113 min() const
3114 { return 0; }
3117 * @brief Returns the least upper bound value of the distribution.
3119 result_type
3120 max() const
3121 { return _M_param.t(); }
3124 * @brief Return true if two binomial distributions have
3125 * the same parameters.
3127 template<typename _IntType1>
3128 friend bool
3129 operator==(const std::binomial_distribution<_IntType1>& __d1,
3130 const std::binomial_distribution<_IntType1>& __d2)
3131 { return ((__d1.param() == __d2.param())
3132 && (__d1._M_nd == __d2._M_nd)); }
3134 template<typename _UniformRandomNumberGenerator>
3135 result_type
3136 operator()(_UniformRandomNumberGenerator& __urng)
3137 { return this->operator()(__urng, this->param()); }
3139 template<typename _UniformRandomNumberGenerator>
3140 result_type
3141 operator()(_UniformRandomNumberGenerator& __urng,
3142 const param_type& __p);
3145 * @brief Inserts a %binomial_distribution random number distribution
3146 * @p __x into the output stream @p __os.
3148 * @param __os An output stream.
3149 * @param __x A %binomial_distribution random number distribution.
3151 * @returns The output stream with the state of @p __x inserted or in
3152 * an error state.
3154 template<typename _IntType1,
3155 typename _CharT, typename _Traits>
3156 friend std::basic_ostream<_CharT, _Traits>&
3157 operator<<(std::basic_ostream<_CharT, _Traits>&,
3158 const std::binomial_distribution<_IntType1>&);
3161 * @brief Extracts a %binomial_distribution random number distribution
3162 * @p __x from the input stream @p __is.
3164 * @param __is An input stream.
3165 * @param __x A %binomial_distribution random number generator engine.
3167 * @returns The input stream with @p __x extracted or in an error
3168 * state.
3170 template<typename _IntType1,
3171 typename _CharT, typename _Traits>
3172 friend std::basic_istream<_CharT, _Traits>&
3173 operator>>(std::basic_istream<_CharT, _Traits>&,
3174 std::binomial_distribution<_IntType1>&);
3176 private:
3177 template<typename _UniformRandomNumberGenerator>
3178 result_type
3179 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
3181 param_type _M_param;
3183 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3184 normal_distribution<double> _M_nd;
3189 * @brief A discrete geometric random number distribution.
3191 * The formula for the geometric probability density function is
3192 * @f$ p(i|p) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
3193 * distribution.
3195 template<typename _IntType = int>
3196 class geometric_distribution
3198 __glibcxx_class_requires(_IntType, _IntegerConcept)
3200 public:
3201 /** The type of the range of the distribution. */
3202 typedef _IntType result_type;
3203 /** Parameter type. */
3204 struct param_type
3206 typedef geometric_distribution<_IntType> distribution_type;
3207 friend class geometric_distribution<_IntType>;
3209 explicit
3210 param_type(double __p = 0.5)
3211 : _M_p(__p)
3213 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0)
3214 && (_M_p <= 1.0));
3215 _M_initialize();
3218 double
3219 p() const
3220 { return _M_p; }
3222 friend bool
3223 operator==(const param_type& __p1, const param_type& __p2)
3224 { return __p1._M_p == __p2._M_p; }
3226 private:
3227 void
3228 _M_initialize()
3229 { _M_log_p = std::log(_M_p); }
3231 double _M_p;
3233 double _M_log_p;
3236 // constructors and member function
3237 explicit
3238 geometric_distribution(double __p = 0.5)
3239 : _M_param(__p)
3242 explicit
3243 geometric_distribution(const param_type& __p)
3244 : _M_param(__p)
3248 * @brief Resets the distribution state.
3250 * Does nothing for the geometric distribution.
3252 void
3253 reset() { }
3256 * @brief Returns the distribution parameter @p p.
3258 double
3259 p() const
3260 { return _M_param.p(); }
3263 * @brief Returns the parameter set of the distribution.
3265 param_type
3266 param() const
3267 { return _M_param; }
3270 * @brief Sets the parameter set of the distribution.
3271 * @param __param The new parameter set of the distribution.
3273 void
3274 param(const param_type& __param)
3275 { _M_param = __param; }
3278 * @brief Returns the greatest lower bound value of the distribution.
3280 result_type
3281 min() const
3282 { return 0; }
3285 * @brief Returns the least upper bound value of the distribution.
3287 result_type
3288 max() const
3289 { return std::numeric_limits<result_type>::max(); }
3291 template<typename _UniformRandomNumberGenerator>
3292 result_type
3293 operator()(_UniformRandomNumberGenerator& __urng)
3294 { return this->operator()(__urng, this->param()); }
3296 template<typename _UniformRandomNumberGenerator>
3297 result_type
3298 operator()(_UniformRandomNumberGenerator& __urng,
3299 const param_type& __p);
3301 private:
3302 param_type _M_param;
3306 * @brief Return true if two geometric distributions have
3307 * the same parameters.
3309 template<typename _IntType>
3310 inline bool
3311 operator==(const geometric_distribution<_IntType>& __d1,
3312 const geometric_distribution<_IntType>& __d2)
3313 { return __d1.param() == __d2.param(); }
3316 * @brief Inserts a %geometric_distribution random number distribution
3317 * @p __x into the output stream @p __os.
3319 * @param __os An output stream.
3320 * @param __x A %geometric_distribution random number distribution.
3322 * @returns The output stream with the state of @p __x inserted or in
3323 * an error state.
3325 template<typename _IntType,
3326 typename _CharT, typename _Traits>
3327 std::basic_ostream<_CharT, _Traits>&
3328 operator<<(std::basic_ostream<_CharT, _Traits>&,
3329 const std::geometric_distribution<_IntType>&);
3332 * @brief Extracts a %geometric_distribution random number distribution
3333 * @p __x from the input stream @p __is.
3335 * @param __is An input stream.
3336 * @param __x A %geometric_distribution random number generator engine.
3338 * @returns The input stream with @p __x extracted or in an error state.
3340 template<typename _IntType,
3341 typename _CharT, typename _Traits>
3342 std::basic_istream<_CharT, _Traits>&
3343 operator>>(std::basic_istream<_CharT, _Traits>&,
3344 std::geometric_distribution<_IntType>&);
3348 * @brief A negative_binomial_distribution random number distribution.
3350 * The formula for the negative binomial probability mass function is
3351 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3352 * and @f$ p @f$ are the parameters of the distribution.
3354 template<typename _IntType = int>
3355 class negative_binomial_distribution
3357 __glibcxx_class_requires(_IntType, _IntegerConcept)
3359 public:
3360 /** The type of the range of the distribution. */
3361 typedef _IntType result_type;
3362 /** Parameter type. */
3363 struct param_type
3365 typedef negative_binomial_distribution<_IntType> distribution_type;
3367 explicit
3368 param_type(_IntType __k = 1, double __p = 0.5)
3369 : _M_k(__k), _M_p(__p)
3372 _IntType
3373 k() const
3374 { return _M_k; }
3376 double
3377 p() const
3378 { return _M_p; }
3380 friend bool
3381 operator==(const param_type& __p1, const param_type& __p2)
3382 { return (__p1._M_k == __p2._M_k) && (__p1._M_p == __p2._M_p); }
3384 private:
3385 _IntType _M_k;
3386 double _M_p;
3389 explicit
3390 negative_binomial_distribution(_IntType __k = 1, double __p = 0.5)
3391 : _M_param(__k, __p)
3394 explicit
3395 negative_binomial_distribution(const param_type& __p)
3396 : _M_param(__p)
3400 * @brief Resets the distribution state.
3402 void
3403 reset()
3407 * @brief Return the @f$ k @f$ parameter of the distribution.
3409 _IntType
3410 k() const
3411 { return _M_param.k(); }
3414 * @brief Return the @f$ p @f$ parameter of the distribution.
3416 double
3417 p() const
3418 { return _M_param.p(); }
3421 * @brief Returns the parameter set of the distribution.
3423 param_type
3424 param() const
3425 { return _M_param; }
3428 * @brief Sets the parameter set of the distribution.
3429 * @param __param The new parameter set of the distribution.
3431 void
3432 param(const param_type& __param)
3433 { _M_param = __param; }
3436 * @brief Returns the greatest lower bound value of the distribution.
3438 result_type
3439 min() const
3440 { return result_type(0); }
3443 * @brief Returns the least upper bound value of the distribution.
3445 result_type
3446 max() const
3447 { return std::numeric_limits<result_type>::max(); }
3449 template<typename _UniformRandomNumberGenerator>
3450 result_type
3451 operator()(_UniformRandomNumberGenerator& __urng)
3452 { return this->operator()(__urng, this->param()); }
3454 template<typename _UniformRandomNumberGenerator>
3455 result_type
3456 operator()(_UniformRandomNumberGenerator& __urng,
3457 const param_type& __p);
3459 private:
3460 param_type _M_param;
3464 * @brief Return true if two negative binomial distributions have
3465 * the same parameters.
3467 template<typename _IntType>
3468 inline bool
3469 operator==(const std::negative_binomial_distribution<_IntType>& __d1,
3470 const std::negative_binomial_distribution<_IntType>& __d2)
3471 { return __d1.param() == __d2.param(); }
3474 * @brief Inserts a %negative_binomial_distribution random
3475 * number distribution @p __x into the output stream @p __os.
3477 * @param __os An output stream.
3478 * @param __x A %negative_binomial_distribution random number
3479 * distribution.
3481 * @returns The output stream with the state of @p __x inserted or in
3482 * an error state.
3484 template<typename _IntType, typename _CharT, typename _Traits>
3485 std::basic_ostream<_CharT, _Traits>&
3486 operator<<(std::basic_ostream<_CharT, _Traits>&,
3487 const std::negative_binomial_distribution<_IntType>&);
3490 * @brief Extracts a %negative_binomial_distribution random number
3491 * distribution @p __x from the input stream @p __is.
3493 * @param __is An input stream.
3494 * @param __x A %negative_binomial_distribution random number
3495 * generator engine.
3497 * @returns The input stream with @p __x extracted or in an error state.
3499 template<typename _IntType, typename _CharT, typename _Traits>
3500 std::basic_istream<_CharT, _Traits>&
3501 operator>>(std::basic_istream<_CharT, _Traits>&,
3502 std::negative_binomial_distribution<_IntType>&);
3504 /* @} */ // group std_random_distributions_bernoulli
3507 * @addtogroup std_random_distributions_poisson Poisson Distributions
3508 * @ingroup std_random_distributions
3509 * @{
3513 * @brief A discrete Poisson random number distribution.
3515 * The formula for the Poisson probability density function is
3516 * @f$ p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu} @f$ where @f$ \mu @f$ is the
3517 * parameter of the distribution.
3519 template<typename _IntType = int>
3520 class poisson_distribution
3522 __glibcxx_class_requires(_IntType, _IntegerConcept)
3524 public:
3525 /** The type of the range of the distribution. */
3526 typedef _IntType result_type;
3527 /** Parameter type. */
3528 struct param_type
3530 typedef poisson_distribution<_IntType> distribution_type;
3531 friend class poisson_distribution<_IntType>;
3533 explicit
3534 param_type(double __mean = 1.0)
3535 : _M_mean(__mean)
3537 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
3538 _M_initialize();
3541 double
3542 mean() const
3543 { return _M_mean; }
3545 friend bool
3546 operator==(const param_type& __p1, const param_type& __p2)
3547 { return __p1._M_mean == __p2._M_mean; }
3549 private:
3550 // Hosts either log(mean) or the threshold of the simple method.
3551 void
3552 _M_initialize();
3554 double _M_mean;
3556 double _M_lm_thr;
3557 #if _GLIBCXX_USE_C99_MATH_TR1
3558 double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
3559 #endif
3562 // constructors and member function
3563 explicit
3564 poisson_distribution(double __mean = 1.0)
3565 : _M_param(__mean), _M_nd()
3568 explicit
3569 poisson_distribution(const param_type& __p)
3570 : _M_param(__p), _M_nd()
3574 * @brief Resets the distribution state.
3576 void
3577 reset()
3578 { _M_nd.reset(); }
3581 * @brief Returns the distribution parameter @p mean.
3583 double
3584 mean() const
3585 { return _M_param.mean(); }
3588 * @brief Returns the parameter set of the distribution.
3590 param_type
3591 param() const
3592 { return _M_param; }
3595 * @brief Sets the parameter set of the distribution.
3596 * @param __param The new parameter set of the distribution.
3598 void
3599 param(const param_type& __param)
3600 { _M_param = __param; }
3603 * @brief Returns the greatest lower bound value of the distribution.
3605 result_type
3606 min() const
3607 { return 0; }
3610 * @brief Returns the least upper bound value of the distribution.
3612 result_type
3613 max() const
3614 { return std::numeric_limits<result_type>::max(); }
3616 template<typename _UniformRandomNumberGenerator>
3617 result_type
3618 operator()(_UniformRandomNumberGenerator& __urng)
3619 { return this->operator()(__urng, this->param()); }
3621 template<typename _UniformRandomNumberGenerator>
3622 result_type
3623 operator()(_UniformRandomNumberGenerator& __urng,
3624 const param_type& __p);
3627 * @brief Return true if two Poisson distributions have the same
3628 * parameters.
3630 template<typename _IntType1>
3631 friend bool
3632 operator==(const std::poisson_distribution<_IntType1>& __d1,
3633 const std::poisson_distribution<_IntType1>& __d2)
3634 { return ((__d1.param() == __d2.param())
3635 && (__d1._M_nd == __d2._M_nd)); }
3638 * @brief Inserts a %poisson_distribution random number distribution
3639 * @p __x into the output stream @p __os.
3641 * @param __os An output stream.
3642 * @param __x A %poisson_distribution random number distribution.
3644 * @returns The output stream with the state of @p __x inserted or in
3645 * an error state.
3647 template<typename _IntType1, typename _CharT, typename _Traits>
3648 friend std::basic_ostream<_CharT, _Traits>&
3649 operator<<(std::basic_ostream<_CharT, _Traits>&,
3650 const std::poisson_distribution<_IntType1>&);
3653 * @brief Extracts a %poisson_distribution random number distribution
3654 * @p __x from the input stream @p __is.
3656 * @param __is An input stream.
3657 * @param __x A %poisson_distribution random number generator engine.
3659 * @returns The input stream with @p __x extracted or in an error
3660 * state.
3662 template<typename _IntType1, typename _CharT, typename _Traits>
3663 friend std::basic_istream<_CharT, _Traits>&
3664 operator>>(std::basic_istream<_CharT, _Traits>&,
3665 std::poisson_distribution<_IntType1>&);
3667 private:
3668 param_type _M_param;
3670 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3671 normal_distribution<double> _M_nd;
3675 * @brief An exponential continuous distribution for random numbers.
3677 * The formula for the exponential probability density function is
3678 * @f$ p(x|\lambda) = \lambda e^{-\lambda x} @f$.
3680 * <table border=1 cellpadding=10 cellspacing=0>
3681 * <caption align=top>Distribution Statistics</caption>
3682 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3683 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
3684 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
3685 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3686 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3687 * </table>
3689 template<typename _RealType = double>
3690 class exponential_distribution
3692 public:
3693 /** The type of the range of the distribution. */
3694 typedef _RealType result_type;
3695 /** Parameter type. */
3696 struct param_type
3698 typedef exponential_distribution<_RealType> distribution_type;
3700 explicit
3701 param_type(_RealType __lambda = _RealType(1))
3702 : _M_lambda(__lambda)
3704 _GLIBCXX_DEBUG_ASSERT(_M_lambda > _RealType(0));
3707 _RealType
3708 lambda() const
3709 { return _M_lambda; }
3711 friend bool
3712 operator==(const param_type& __p1, const param_type& __p2)
3713 { return __p1._M_lambda == __p2._M_lambda; }
3715 private:
3716 _RealType _M_lambda;
3719 public:
3721 * @brief Constructs an exponential distribution with inverse scale
3722 * parameter @f$ \lambda @f$.
3724 explicit
3725 exponential_distribution(const result_type& __lambda = result_type(1))
3726 : _M_param(__lambda)
3729 explicit
3730 exponential_distribution(const param_type& __p)
3731 : _M_param(__p)
3735 * @brief Resets the distribution state.
3737 * Has no effect on exponential distributions.
3739 void
3740 reset() { }
3743 * @brief Returns the inverse scale parameter of the distribution.
3745 _RealType
3746 lambda() const
3747 { return _M_param.lambda(); }
3750 * @brief Returns the parameter set of the distribution.
3752 param_type
3753 param() const
3754 { return _M_param; }
3757 * @brief Sets the parameter set of the distribution.
3758 * @param __param The new parameter set of the distribution.
3760 void
3761 param(const param_type& __param)
3762 { _M_param = __param; }
3765 * @brief Returns the greatest lower bound value of the distribution.
3767 result_type
3768 min() const
3769 { return result_type(0); }
3772 * @brief Returns the least upper bound value of the distribution.
3774 result_type
3775 max() const
3776 { return std::numeric_limits<result_type>::max(); }
3778 template<typename _UniformRandomNumberGenerator>
3779 result_type
3780 operator()(_UniformRandomNumberGenerator& __urng)
3782 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
3783 __aurng(__urng);
3784 return -std::log(__aurng()) / this->lambda();
3787 template<typename _UniformRandomNumberGenerator>
3788 result_type
3789 operator()(_UniformRandomNumberGenerator& __urng,
3790 const param_type& __p)
3792 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
3793 __aurng(__urng);
3794 return -std::log(__aurng()) / __p.lambda();
3797 private:
3798 param_type _M_param;
3802 * @brief Return true if two exponential distributions have the same
3803 * parameters.
3805 template<typename _RealType>
3806 inline bool
3807 operator==(const std::exponential_distribution<_RealType>& __d1,
3808 const std::exponential_distribution<_RealType>& __d2)
3809 { return __d1.param() == __d2.param(); }
3812 * @brief Inserts a %exponential_distribution random number distribution
3813 * @p __x into the output stream @p __os.
3815 * @param __os An output stream.
3816 * @param __x A %exponential_distribution random number distribution.
3818 * @returns The output stream with the state of @p __x inserted or in
3819 * an error state.
3821 template<typename _RealType, typename _CharT, typename _Traits>
3822 std::basic_ostream<_CharT, _Traits>&
3823 operator<<(std::basic_ostream<_CharT, _Traits>&,
3824 const std::exponential_distribution<_RealType>&);
3827 * @brief Extracts a %exponential_distribution random number distribution
3828 * @p __x from the input stream @p __is.
3830 * @param __is An input stream.
3831 * @param __x A %exponential_distribution random number
3832 * generator engine.
3834 * @returns The input stream with @p __x extracted or in an error state.
3836 template<typename _RealType, typename _CharT, typename _Traits>
3837 std::basic_istream<_CharT, _Traits>&
3838 operator>>(std::basic_istream<_CharT, _Traits>&,
3839 std::exponential_distribution<_RealType>&);
3843 * @brief A gamma continuous distribution for random numbers.
3845 * The formula for the gamma probability density function is
3846 * @f$ p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
3847 * (x/\beta)^{\alpha - 1} e^{-x/\beta} @f$.
3849 template<typename _RealType = double>
3850 class gamma_distribution
3852 public:
3853 /** The type of the range of the distribution. */
3854 typedef _RealType result_type;
3855 /** Parameter type. */
3856 struct param_type
3858 typedef gamma_distribution<_RealType> distribution_type;
3859 friend class gamma_distribution<_RealType>;
3861 explicit
3862 param_type(_RealType __alpha = _RealType(1),
3863 _RealType __beta = _RealType(1))
3864 : _M_alpha(__alpha), _M_beta(__beta)
3866 _GLIBCXX_DEBUG_ASSERT(_M_alpha > _RealType(0));
3867 _M_initialize();
3870 _RealType
3871 alpha() const
3872 { return _M_alpha; }
3874 _RealType
3875 beta() const
3876 { return _M_beta; }
3878 friend bool
3879 operator==(const param_type& __p1, const param_type& __p2)
3880 { return ((__p1._M_alpha == __p2._M_alpha)
3881 && (__p1._M_beta == __p2._M_beta)); }
3883 private:
3884 void
3885 _M_initialize();
3887 _RealType _M_alpha;
3888 _RealType _M_beta;
3890 // Hosts either lambda of GB or d of modified Vaduva's.
3891 _RealType _M_l_d;
3894 public:
3896 * @brief Constructs a gamma distribution with parameters
3897 * @f$ \alpha @f$ and @f$ \beta @f$.
3899 explicit
3900 gamma_distribution(_RealType __alpha = _RealType(1),
3901 _RealType __beta = _RealType(1))
3902 : _M_param(__alpha, __beta)
3905 explicit
3906 gamma_distribution(const param_type& __p)
3907 : _M_param(__p)
3911 * @brief Resets the distribution state.
3913 * Does nothing for the gamma distribution.
3915 void
3916 reset() { }
3919 * @brief Returns the @f$ \alpha @f$ of the distribution.
3921 _RealType
3922 alpha() const
3923 { return _M_param.alpha(); }
3926 * @brief Returns the @f$ \beta @f$ of the distribution.
3928 _RealType
3929 beta() const
3930 { return _M_param.beta(); }
3933 * @brief Returns the parameter set of the distribution.
3935 param_type
3936 param() const
3937 { return _M_param; }
3940 * @brief Sets the parameter set of the distribution.
3941 * @param __param The new parameter set of the distribution.
3943 void
3944 param(const param_type& __param)
3945 { _M_param = __param; }
3948 * @brief Returns the greatest lower bound value of the distribution.
3950 result_type
3951 min() const
3952 { return result_type(0); }
3955 * @brief Returns the least upper bound value of the distribution.
3957 result_type
3958 max() const
3959 { return std::numeric_limits<result_type>::max(); }
3961 template<typename _UniformRandomNumberGenerator>
3962 result_type
3963 operator()(_UniformRandomNumberGenerator& __urng)
3964 { return this->operator()(__urng, this->param()); }
3966 template<typename _UniformRandomNumberGenerator>
3967 result_type
3968 operator()(_UniformRandomNumberGenerator& __urng,
3969 const param_type& __p);
3971 private:
3972 param_type _M_param;
3976 * @brief Return true if two gamma distributions have the same
3977 * parameters.
3979 template<typename _RealType>
3980 inline bool
3981 operator==(const std::gamma_distribution<_RealType>& __d1,
3982 const std::gamma_distribution<_RealType>& __d2)
3983 { return __d1.param() == __d2.param(); }
3986 * @brief Inserts a %gamma_distribution random number distribution
3987 * @p __x into the output stream @p __os.
3989 * @param __os An output stream.
3990 * @param __x A %gamma_distribution random number distribution.
3992 * @returns The output stream with the state of @p __x inserted or in
3993 * an error state.
3995 template<typename _RealType, typename _CharT, typename _Traits>
3996 std::basic_ostream<_CharT, _Traits>&
3997 operator<<(std::basic_ostream<_CharT, _Traits>&,
3998 const std::gamma_distribution<_RealType>&);
4001 * @brief Extracts a %gamma_distribution random number distribution
4002 * @p __x from the input stream @p __is.
4004 * @param __is An input stream.
4005 * @param __x A %gamma_distribution random number generator engine.
4007 * @returns The input stream with @p __x extracted or in an error state.
4009 template<typename _RealType, typename _CharT, typename _Traits>
4010 std::basic_istream<_CharT, _Traits>&
4011 operator>>(std::basic_istream<_CharT, _Traits>&,
4012 std::gamma_distribution<_RealType>&);
4016 * @brief A weibull_distribution random number distribution.
4018 * The formula for the normal probability density function is
4019 * @f$ p(x|\alpha,\beta) = \frac{a}{b} (frac{x}{b})^{a-1}
4020 * \exp{(-(frac{x}{b})^a)} @f$.
4022 template<typename _RealType = double>
4023 class weibull_distribution
4025 public:
4026 /** The type of the range of the distribution. */
4027 typedef _RealType result_type;
4028 /** Parameter type. */
4029 struct param_type
4031 typedef weibull_distribution<_RealType> distribution_type;
4033 explicit
4034 param_type(_RealType __a = _RealType(1),
4035 _RealType __b = _RealType(1))
4036 : _M_a(__a), _M_b(__b)
4039 _RealType
4040 a() const
4041 { return _M_a; }
4043 _RealType
4044 b() const
4045 { return _M_b; }
4047 friend bool
4048 operator==(const param_type& __p1, const param_type& __p2)
4049 { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
4051 private:
4052 _RealType _M_a;
4053 _RealType _M_b;
4056 explicit
4057 weibull_distribution(_RealType __a = _RealType(1),
4058 _RealType __b = _RealType(1))
4059 : _M_param(__a, __b)
4062 explicit
4063 weibull_distribution(const param_type& __p)
4064 : _M_param(__p)
4068 * @brief Resets the distribution state.
4070 void
4071 reset()
4075 * @brief Return the @f$ a @f$ parameter of the distribution.
4077 _RealType
4078 a() const
4079 { return _M_param.a(); }
4082 * @brief Return the @f$ b @f$ parameter of the distribution.
4084 _RealType
4085 b() const
4086 { return _M_param.b(); }
4089 * @brief Returns the parameter set of the distribution.
4091 param_type
4092 param() const
4093 { return _M_param; }
4096 * @brief Sets the parameter set of the distribution.
4097 * @param __param The new parameter set of the distribution.
4099 void
4100 param(const param_type& __param)
4101 { _M_param = __param; }
4104 * @brief Returns the greatest lower bound value of the distribution.
4106 result_type
4107 min() const
4108 { return result_type(0); }
4111 * @brief Returns the least upper bound value of the distribution.
4113 result_type
4114 max() const
4115 { return std::numeric_limits<result_type>::max(); }
4117 template<typename _UniformRandomNumberGenerator>
4118 result_type
4119 operator()(_UniformRandomNumberGenerator& __urng)
4120 { return this->operator()(__urng, this->param()); }
4122 template<typename _UniformRandomNumberGenerator>
4123 result_type
4124 operator()(_UniformRandomNumberGenerator& __urng,
4125 const param_type& __p)
4127 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
4128 __aurng(__urng);
4129 return __p.b() * std::pow(-std::log(__aurng()),
4130 result_type(1) / __p.a());
4133 private:
4134 param_type _M_param;
4138 * @brief Return true if two Weibull distributions have the same
4139 * parameters.
4141 template<typename _RealType>
4142 inline bool
4143 operator==(const std::weibull_distribution<_RealType>& __d1,
4144 const std::weibull_distribution<_RealType>& __d2)
4145 { return __d1.param() == __d2.param(); }
4148 * @brief Inserts a %weibull_distribution random number distribution
4149 * @p __x into the output stream @p __os.
4151 * @param __os An output stream.
4152 * @param __x A %weibull_distribution random number distribution.
4154 * @returns The output stream with the state of @p __x inserted or in
4155 * an error state.
4157 template<typename _RealType, typename _CharT, typename _Traits>
4158 std::basic_ostream<_CharT, _Traits>&
4159 operator<<(std::basic_ostream<_CharT, _Traits>&,
4160 const std::weibull_distribution<_RealType>&);
4163 * @brief Extracts a %weibull_distribution random number distribution
4164 * @p __x from the input stream @p __is.
4166 * @param __is An input stream.
4167 * @param __x A %weibull_distribution random number
4168 * generator engine.
4170 * @returns The input stream with @p __x extracted or in an error state.
4172 template<typename _RealType, typename _CharT, typename _Traits>
4173 std::basic_istream<_CharT, _Traits>&
4174 operator>>(std::basic_istream<_CharT, _Traits>&,
4175 std::weibull_distribution<_RealType>&);
4179 * @brief A extreme_value_distribution random number distribution.
4181 * The formula for the normal probability mass function is
4182 * @f$ p(x|a,b) = \frac{1}{b}
4183 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) @f$
4185 template<typename _RealType = double>
4186 class extreme_value_distribution
4188 public:
4189 /** The type of the range of the distribution. */
4190 typedef _RealType result_type;
4191 /** Parameter type. */
4192 struct param_type
4194 typedef extreme_value_distribution<_RealType> distribution_type;
4196 explicit
4197 param_type(_RealType __a = _RealType(0),
4198 _RealType __b = _RealType(1))
4199 : _M_a(__a), _M_b(__b)
4202 _RealType
4203 a() const
4204 { return _M_a; }
4206 _RealType
4207 b() const
4208 { return _M_b; }
4210 friend bool
4211 operator==(const param_type& __p1, const param_type& __p2)
4212 { return (__p1._M_a == __p2._M_a) && (__p1._M_b == __p2._M_b); }
4214 private:
4215 _RealType _M_a;
4216 _RealType _M_b;
4219 explicit
4220 extreme_value_distribution(_RealType __a = _RealType(0),
4221 _RealType __b = _RealType(1))
4222 : _M_param(__a, __b)
4225 explicit
4226 extreme_value_distribution(const param_type& __p)
4227 : _M_param(__p)
4231 * @brief Resets the distribution state.
4233 void
4234 reset()
4238 * @brief Return the @f$ a @f$ parameter of the distribution.
4240 _RealType
4241 a() const
4242 { return _M_param.a(); }
4245 * @brief Return the @f$ b @f$ parameter of the distribution.
4247 _RealType
4248 b() const
4249 { return _M_param.b(); }
4252 * @brief Returns the parameter set of the distribution.
4254 param_type
4255 param() const
4256 { return _M_param; }
4259 * @brief Sets the parameter set of the distribution.
4260 * @param __param The new parameter set of the distribution.
4262 void
4263 param(const param_type& __param)
4264 { _M_param = __param; }
4267 * @brief Returns the greatest lower bound value of the distribution.
4269 result_type
4270 min() const
4271 { return std::numeric_limits<result_type>::min(); }
4274 * @brief Returns the least upper bound value of the distribution.
4276 result_type
4277 max() const
4278 { return std::numeric_limits<result_type>::max(); }
4280 template<typename _UniformRandomNumberGenerator>
4281 result_type
4282 operator()(_UniformRandomNumberGenerator& __urng)
4283 { return this->operator()(__urng, this->param()); }
4285 template<typename _UniformRandomNumberGenerator>
4286 result_type
4287 operator()(_UniformRandomNumberGenerator& __urng,
4288 const param_type& __p);
4290 private:
4291 param_type _M_param;
4297 template<typename _RealType>
4298 inline bool
4299 operator==(const std::extreme_value_distribution<_RealType>& __d1,
4300 const std::extreme_value_distribution<_RealType>& __d2)
4301 { return __d1.param() == __d2.param(); }
4304 * @brief Inserts a %extreme_value_distribution random number distribution
4305 * @p __x into the output stream @p __os.
4307 * @param __os An output stream.
4308 * @param __x A %extreme_value_distribution random number distribution.
4310 * @returns The output stream with the state of @p __x inserted or in
4311 * an error state.
4313 template<typename _RealType, typename _CharT, typename _Traits>
4314 std::basic_ostream<_CharT, _Traits>&
4315 operator<<(std::basic_ostream<_CharT, _Traits>&,
4316 const std::extreme_value_distribution<_RealType>&);
4319 * @brief Extracts a %extreme_value_distribution random number
4320 * distribution @p __x from the input stream @p __is.
4322 * @param __is An input stream.
4323 * @param __x A %extreme_value_distribution random number
4324 * generator engine.
4326 * @returns The input stream with @p __x extracted or in an error state.
4328 template<typename _RealType, typename _CharT, typename _Traits>
4329 std::basic_istream<_CharT, _Traits>&
4330 operator>>(std::basic_istream<_CharT, _Traits>&,
4331 std::extreme_value_distribution<_RealType>&);
4335 * @brief A discrete_distribution random number distribution.
4337 * The formula for the discrete probability mass function is
4340 template<typename _IntType = int>
4341 class discrete_distribution
4343 __glibcxx_class_requires(_IntType, _IntegerConcept)
4345 public:
4346 /** The type of the range of the distribution. */
4347 typedef _IntType result_type;
4348 /** Parameter type. */
4349 struct param_type
4351 typedef discrete_distribution<_IntType> distribution_type;
4352 friend class discrete_distribution<_IntType>;
4354 param_type()
4355 : _M_prob(), _M_cp()
4356 { _M_initialize(); }
4358 template<typename _InputIterator>
4359 param_type(_InputIterator __wbegin,
4360 _InputIterator __wend)
4361 : _M_prob(__wbegin, __wend), _M_cp()
4362 { _M_initialize(); }
4364 param_type(initializer_list<double> __wil)
4365 : _M_prob(__wil.begin(), __wil.end()), _M_cp()
4366 { _M_initialize(); }
4368 template<typename _Func>
4369 param_type(size_t __nw, double __xmin, double __xmax,
4370 _Func __fw);
4372 std::vector<double>
4373 probabilities() const
4374 { return _M_prob; }
4376 friend bool
4377 operator==(const param_type& __p1, const param_type& __p2)
4378 { return __p1._M_prob == __p2._M_prob; }
4380 private:
4381 void
4382 _M_initialize();
4384 std::vector<double> _M_prob;
4385 std::vector<double> _M_cp;
4388 discrete_distribution()
4389 : _M_param()
4392 template<typename _InputIterator>
4393 discrete_distribution(_InputIterator __wbegin,
4394 _InputIterator __wend)
4395 : _M_param(__wbegin, __wend)
4398 discrete_distribution(initializer_list<double> __wil)
4399 : _M_param(__wil)
4402 template<typename _Func>
4403 discrete_distribution(size_t __nw, double __xmin, double __xmax,
4404 _Func __fw)
4405 : _M_param(__nw, __xmin, __xmax, __fw)
4408 explicit
4409 discrete_distribution(const param_type& __p)
4410 : _M_param(__p)
4414 * @brief Resets the distribution state.
4416 void
4417 reset()
4421 * @brief Returns the probabilities of the distribution.
4423 std::vector<double>
4424 probabilities() const
4425 { return _M_param.probabilities(); }
4428 * @brief Returns the parameter set of the distribution.
4430 param_type
4431 param() const
4432 { return _M_param; }
4435 * @brief Sets the parameter set of the distribution.
4436 * @param __param The new parameter set of the distribution.
4438 void
4439 param(const param_type& __param)
4440 { _M_param = __param; }
4443 * @brief Returns the greatest lower bound value of the distribution.
4445 result_type
4446 min() const
4447 { return result_type(0); }
4450 * @brief Returns the least upper bound value of the distribution.
4452 result_type
4453 max() const
4454 { return this->_M_param._M_prob.size() - 1; }
4456 template<typename _UniformRandomNumberGenerator>
4457 result_type
4458 operator()(_UniformRandomNumberGenerator& __urng)
4459 { return this->operator()(__urng, this->param()); }
4461 template<typename _UniformRandomNumberGenerator>
4462 result_type
4463 operator()(_UniformRandomNumberGenerator& __urng,
4464 const param_type& __p);
4467 * @brief Inserts a %discrete_distribution random number distribution
4468 * @p __x into the output stream @p __os.
4470 * @param __os An output stream.
4471 * @param __x A %discrete_distribution random number distribution.
4473 * @returns The output stream with the state of @p __x inserted or in
4474 * an error state.
4476 template<typename _IntType1, typename _CharT, typename _Traits>
4477 friend std::basic_ostream<_CharT, _Traits>&
4478 operator<<(std::basic_ostream<_CharT, _Traits>&,
4479 const std::discrete_distribution<_IntType1>&);
4482 * @brief Extracts a %discrete_distribution random number distribution
4483 * @p __x from the input stream @p __is.
4485 * @param __is An input stream.
4486 * @param __x A %discrete_distribution random number
4487 * generator engine.
4489 * @returns The input stream with @p __x extracted or in an error
4490 * state.
4492 template<typename _IntType1, typename _CharT, typename _Traits>
4493 friend std::basic_istream<_CharT, _Traits>&
4494 operator>>(std::basic_istream<_CharT, _Traits>&,
4495 std::discrete_distribution<_IntType1>&);
4497 private:
4498 param_type _M_param;
4504 template<typename _IntType>
4505 inline bool
4506 operator==(const std::discrete_distribution<_IntType>& __d1,
4507 const std::discrete_distribution<_IntType>& __d2)
4508 { return __d1.param() == __d2.param(); }
4512 * @brief A piecewise_constant_distribution random number distribution.
4514 * The formula for the piecewise constant probability mass function is
4517 template<typename _RealType = double>
4518 class piecewise_constant_distribution
4520 public:
4521 /** The type of the range of the distribution. */
4522 typedef _RealType result_type;
4523 /** Parameter type. */
4524 struct param_type
4526 typedef piecewise_constant_distribution<_RealType> distribution_type;
4527 friend class piecewise_constant_distribution<_RealType>;
4529 param_type();
4531 template<typename _InputIteratorB, typename _InputIteratorW>
4532 param_type(_InputIteratorB __bfirst,
4533 _InputIteratorB __bend,
4534 _InputIteratorW __wbegin);
4536 template<typename _Func>
4537 param_type(initializer_list<_RealType> __bil, _Func __fw);
4539 template<typename _Func>
4540 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4541 _Func __fw);
4543 std::vector<_RealType>
4544 intervals() const
4545 { return _M_int; }
4547 std::vector<double>
4548 densities() const
4549 { return _M_den; }
4551 friend bool
4552 operator==(const param_type& __p1, const param_type& __p2)
4553 { return ((__p1._M_int == __p2._M_int)
4554 && (__p1._M_den == __p2._M_den)); }
4556 private:
4557 void
4558 _M_initialize();
4560 std::vector<_RealType> _M_int;
4561 std::vector<double> _M_den;
4562 std::vector<double> _M_cp;
4565 explicit
4566 piecewise_constant_distribution()
4567 : _M_param()
4570 template<typename _InputIteratorB, typename _InputIteratorW>
4571 piecewise_constant_distribution(_InputIteratorB __bfirst,
4572 _InputIteratorB __bend,
4573 _InputIteratorW __wbegin)
4574 : _M_param(__bfirst, __bend, __wbegin)
4577 template<typename _Func>
4578 piecewise_constant_distribution(initializer_list<_RealType> __bil,
4579 _Func __fw)
4580 : _M_param(__bil, __fw)
4583 template<typename _Func>
4584 piecewise_constant_distribution(size_t __nw,
4585 _RealType __xmin, _RealType __xmax,
4586 _Func __fw)
4587 : _M_param(__nw, __xmin, __xmax, __fw)
4590 explicit
4591 piecewise_constant_distribution(const param_type& __p)
4592 : _M_param(__p)
4596 * @brief Resets the distribution state.
4598 void
4599 reset()
4603 * @brief Returns a vector of the intervals.
4605 std::vector<_RealType>
4606 intervals() const
4607 { return _M_param.intervals(); }
4610 * @brief Returns a vector of the probability densities.
4612 std::vector<double>
4613 densities() const
4614 { return _M_param.densities(); }
4617 * @brief Returns the parameter set of the distribution.
4619 param_type
4620 param() const
4621 { return _M_param; }
4624 * @brief Sets the parameter set of the distribution.
4625 * @param __param The new parameter set of the distribution.
4627 void
4628 param(const param_type& __param)
4629 { _M_param = __param; }
4632 * @brief Returns the greatest lower bound value of the distribution.
4634 result_type
4635 min() const
4636 { return this->_M_param._M_int.front(); }
4639 * @brief Returns the least upper bound value of the distribution.
4641 result_type
4642 max() const
4643 { return this->_M_param._M_int.back(); }
4645 template<typename _UniformRandomNumberGenerator>
4646 result_type
4647 operator()(_UniformRandomNumberGenerator& __urng)
4648 { return this->operator()(__urng, this->param()); }
4650 template<typename _UniformRandomNumberGenerator>
4651 result_type
4652 operator()(_UniformRandomNumberGenerator& __urng,
4653 const param_type& __p);
4656 * @brief Inserts a %piecewise_constan_distribution random
4657 * number distribution @p __x into the output stream @p __os.
4659 * @param __os An output stream.
4660 * @param __x A %piecewise_constan_distribution random number
4661 * distribution.
4663 * @returns The output stream with the state of @p __x inserted or in
4664 * an error state.
4666 template<typename _RealType1, typename _CharT, typename _Traits>
4667 friend std::basic_ostream<_CharT, _Traits>&
4668 operator<<(std::basic_ostream<_CharT, _Traits>&,
4669 const std::piecewise_constant_distribution<_RealType1>&);
4672 * @brief Extracts a %piecewise_constan_distribution random
4673 * number distribution @p __x from the input stream @p __is.
4675 * @param __is An input stream.
4676 * @param __x A %piecewise_constan_distribution random number
4677 * generator engine.
4679 * @returns The input stream with @p __x extracted or in an error
4680 * state.
4682 template<typename _RealType1, typename _CharT, typename _Traits>
4683 friend std::basic_istream<_CharT, _Traits>&
4684 operator>>(std::basic_istream<_CharT, _Traits>&,
4685 std::piecewise_constant_distribution<_RealType1>&);
4687 private:
4688 param_type _M_param;
4694 template<typename _RealType>
4695 inline bool
4696 operator==(const std::piecewise_constant_distribution<_RealType>& __d1,
4697 const std::piecewise_constant_distribution<_RealType>& __d2)
4698 { return __d1.param() == __d2.param(); }
4702 * @brief A piecewise_linear_distribution random number distribution.
4704 * The formula for the piecewise linear probability mass function is
4707 template<typename _RealType = double>
4708 class piecewise_linear_distribution
4710 public:
4711 /** The type of the range of the distribution. */
4712 typedef _RealType result_type;
4713 /** Parameter type. */
4714 struct param_type
4716 typedef piecewise_linear_distribution<_RealType> distribution_type;
4717 friend class piecewise_linear_distribution<_RealType>;
4719 param_type();
4721 template<typename _InputIteratorB, typename _InputIteratorW>
4722 param_type(_InputIteratorB __bfirst,
4723 _InputIteratorB __bend,
4724 _InputIteratorW __wbegin);
4726 template<typename _Func>
4727 param_type(initializer_list<_RealType> __bil, _Func __fw);
4729 template<typename _Func>
4730 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4731 _Func __fw);
4733 std::vector<_RealType>
4734 intervals() const
4735 { return _M_int; }
4737 std::vector<double>
4738 densities() const
4739 { return _M_den; }
4741 friend bool
4742 operator==(const param_type& __p1, const param_type& __p2)
4743 { return ((__p1._M_int == __p2._M_int)
4744 && (__p1._M_den == __p2._M_den)); }
4746 private:
4747 void
4748 _M_initialize();
4750 std::vector<_RealType> _M_int;
4751 std::vector<double> _M_den;
4752 std::vector<double> _M_cp;
4753 std::vector<double> _M_m;
4756 explicit
4757 piecewise_linear_distribution()
4758 : _M_param()
4761 template<typename _InputIteratorB, typename _InputIteratorW>
4762 piecewise_linear_distribution(_InputIteratorB __bfirst,
4763 _InputIteratorB __bend,
4764 _InputIteratorW __wbegin)
4765 : _M_param(__bfirst, __bend, __wbegin)
4768 template<typename _Func>
4769 piecewise_linear_distribution(initializer_list<_RealType> __bil,
4770 _Func __fw)
4771 : _M_param(__bil, __fw)
4774 template<typename _Func>
4775 piecewise_linear_distribution(size_t __nw,
4776 _RealType __xmin, _RealType __xmax,
4777 _Func __fw)
4778 : _M_param(__nw, __xmin, __xmax, __fw)
4781 explicit
4782 piecewise_linear_distribution(const param_type& __p)
4783 : _M_param(__p)
4787 * Resets the distribution state.
4789 void
4790 reset()
4794 * @brief Return the intervals of the distribution.
4796 std::vector<_RealType>
4797 intervals() const
4798 { return _M_param.intervals(); }
4801 * @brief Return a vector of the probability densities of the
4802 * distribution.
4804 std::vector<double>
4805 densities() const
4806 { return _M_param.densities(); }
4809 * @brief Returns the parameter set of the distribution.
4811 param_type
4812 param() const
4813 { return _M_param; }
4816 * @brief Sets the parameter set of the distribution.
4817 * @param __param The new parameter set of the distribution.
4819 void
4820 param(const param_type& __param)
4821 { _M_param = __param; }
4824 * @brief Returns the greatest lower bound value of the distribution.
4826 result_type
4827 min() const
4828 { return this->_M_param._M_int.front(); }
4831 * @brief Returns the least upper bound value of the distribution.
4833 result_type
4834 max() const
4835 { return this->_M_param._M_int.back(); }
4837 template<typename _UniformRandomNumberGenerator>
4838 result_type
4839 operator()(_UniformRandomNumberGenerator& __urng)
4840 { return this->operator()(__urng, this->param()); }
4842 template<typename _UniformRandomNumberGenerator>
4843 result_type
4844 operator()(_UniformRandomNumberGenerator& __urng,
4845 const param_type& __p);
4848 * @brief Inserts a %piecewise_linear_distribution random number
4849 * distribution @p __x into the output stream @p __os.
4851 * @param __os An output stream.
4852 * @param __x A %piecewise_linear_distribution random number
4853 * distribution.
4855 * @returns The output stream with the state of @p __x inserted or in
4856 * an error state.
4858 template<typename _RealType1, typename _CharT, typename _Traits>
4859 friend std::basic_ostream<_CharT, _Traits>&
4860 operator<<(std::basic_ostream<_CharT, _Traits>&,
4861 const std::piecewise_linear_distribution<_RealType1>&);
4864 * @brief Extracts a %piecewise_linear_distribution random number
4865 * distribution @p __x from the input stream @p __is.
4867 * @param __is An input stream.
4868 * @param __x A %piecewise_linear_distribution random number
4869 * generator engine.
4871 * @returns The input stream with @p __x extracted or in an error
4872 * state.
4874 template<typename _RealType1, typename _CharT, typename _Traits>
4875 friend std::basic_istream<_CharT, _Traits>&
4876 operator>>(std::basic_istream<_CharT, _Traits>&,
4877 std::piecewise_linear_distribution<_RealType1>&);
4879 private:
4880 param_type _M_param;
4886 template<typename _RealType>
4887 inline bool
4888 operator==(const std::piecewise_linear_distribution<_RealType>& __d1,
4889 const std::piecewise_linear_distribution<_RealType>& __d2)
4890 { return __d1.param() == __d2.param(); }
4892 /* @} */ // group std_random_distributions_poisson
4894 /* @} */ // group std_random_distributions
4897 * @addtogroup std_random_utilities Random Number Utilities
4898 * @ingroup std_random
4899 * @{
4903 * @brief The seed_seq class generates sequences of seeds for random
4904 * number generators.
4906 class seed_seq
4909 public:
4910 /** The type of the seed vales. */
4911 typedef uint_least32_t result_type;
4913 /** Default constructor. */
4914 seed_seq()
4915 : _M_v()
4918 template<typename _IntType>
4919 seed_seq(std::initializer_list<_IntType> il);
4921 template<typename _InputIterator>
4922 seed_seq(_InputIterator __begin, _InputIterator __end);
4924 // generating functions
4925 template<typename _RandomAccessIterator>
4926 void
4927 generate(_RandomAccessIterator __begin, _RandomAccessIterator __end);
4929 // property functions
4930 size_t size() const
4931 { return _M_v.size(); }
4933 template<typename OutputIterator>
4934 void
4935 param(OutputIterator __dest) const
4936 { std::copy(_M_v.begin(), _M_v.end(), __dest); }
4938 private:
4940 std::vector<result_type> _M_v;
4943 /* @} */ // group std_random_utilities
4945 /* @} */ // group std_random