]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/random.h
random.h (mersenne_twister_engine): Don't inline discard here.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / random.h
1 // random number generation -*- C++ -*-
2
3 // Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /**
26 * @file bits/random.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{random}
29 */
30
31 #ifndef _RANDOM_H
32 #define _RANDOM_H 1
33
34 #include <vector>
35
36 namespace std _GLIBCXX_VISIBILITY(default)
37 {
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
39
40 // [26.4] Random number generation
41
42 /**
43 * @defgroup random Random Number Generation
44 * @ingroup numerics
45 *
46 * A facility for generating random numbers on selected distributions.
47 * @{
48 */
49
50 /**
51 * @brief A function template for converting the output of a (integral)
52 * uniform random number generator to a floatng point result in the range
53 * [0-1).
54 */
55 template<typename _RealType, size_t __bits,
56 typename _UniformRandomNumberGenerator>
57 _RealType
58 generate_canonical(_UniformRandomNumberGenerator& __g);
59
60 _GLIBCXX_END_NAMESPACE_VERSION
61
62 /*
63 * Implementation-space details.
64 */
65 namespace __detail
66 {
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
68
69 template<typename _UIntType, size_t __w,
70 bool = __w < static_cast<size_t>
71 (std::numeric_limits<_UIntType>::digits)>
72 struct _Shift
73 { static const _UIntType __value = 0; };
74
75 template<typename _UIntType, size_t __w>
76 struct _Shift<_UIntType, __w, true>
77 { static const _UIntType __value = _UIntType(1) << __w; };
78
79 template<int __s,
80 int __which = ((__s <= __CHAR_BIT__ * sizeof (int))
81 + (__s <= __CHAR_BIT__ * sizeof (long))
82 + (__s <= __CHAR_BIT__ * sizeof (long long))
83 /* assume long long no bigger than __int128 */
84 + (__s <= 128))>
85 struct _Select_uint_least_t
86 {
87 static_assert(__which < 0, /* needs to be dependent */
88 "sorry, would be too much trouble for a slow result");
89 };
90
91 template<int __s>
92 struct _Select_uint_least_t<__s, 4>
93 { typedef unsigned int type; };
94
95 template<int __s>
96 struct _Select_uint_least_t<__s, 3>
97 { typedef unsigned long type; };
98
99 template<int __s>
100 struct _Select_uint_least_t<__s, 2>
101 { typedef unsigned long long type; };
102
103 #ifdef _GLIBCXX_USE_INT128
104 template<int __s>
105 struct _Select_uint_least_t<__s, 1>
106 { typedef unsigned __int128 type; };
107 #endif
108
109 // Assume a != 0, a < m, c < m, x < m.
110 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c,
111 bool __big_enough = (!(__m & (__m - 1))
112 || (_Tp(-1) - __c) / __a >= __m - 1),
113 bool __schrage_ok = __m % __a < __m / __a>
114 struct _Mod
115 {
116 typedef typename _Select_uint_least_t<std::__lg(__a)
117 + std::__lg(__m) + 2>::type _Tp2;
118 static _Tp
119 __calc(_Tp __x)
120 { return static_cast<_Tp>((_Tp2(__a) * __x + __c) % __m); }
121 };
122
123 // Schrage.
124 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
125 struct _Mod<_Tp, __m, __a, __c, false, true>
126 {
127 static _Tp
128 __calc(_Tp __x);
129 };
130
131 // Special cases:
132 // - for m == 2^n or m == 0, unsigned integer overflow is safe.
133 // - a * (m - 1) + c fits in _Tp, there is no overflow.
134 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool __s>
135 struct _Mod<_Tp, __m, __a, __c, true, __s>
136 {
137 static _Tp
138 __calc(_Tp __x)
139 {
140 _Tp __res = __a * __x + __c;
141 if (__m)
142 __res %= __m;
143 return __res;
144 }
145 };
146
147 template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0>
148 inline _Tp
149 __mod(_Tp __x)
150 { return _Mod<_Tp, __m, __a, __c>::__calc(__x); }
151
152 /*
153 * An adaptor class for converting the output of any Generator into
154 * the input for a specific Distribution.
155 */
156 template<typename _Engine, typename _DInputType>
157 struct _Adaptor
158 {
159
160 public:
161 _Adaptor(_Engine& __g)
162 : _M_g(__g) { }
163
164 _DInputType
165 min() const
166 { return _DInputType(0); }
167
168 _DInputType
169 max() const
170 { return _DInputType(1); }
171
172 /*
173 * Converts a value generated by the adapted random number generator
174 * into a value in the input domain for the dependent random number
175 * distribution.
176 */
177 _DInputType
178 operator()()
179 {
180 return std::generate_canonical<_DInputType,
181 std::numeric_limits<_DInputType>::digits,
182 _Engine>(_M_g);
183 }
184
185 private:
186 _Engine& _M_g;
187 };
188
189 _GLIBCXX_END_NAMESPACE_VERSION
190 } // namespace __detail
191
192 _GLIBCXX_BEGIN_NAMESPACE_VERSION
193
194 /**
195 * @addtogroup random_generators Random Number Generators
196 * @ingroup random
197 *
198 * These classes define objects which provide random or pseudorandom
199 * numbers, either from a discrete or a continuous interval. The
200 * random number generator supplied as a part of this library are
201 * all uniform random number generators which provide a sequence of
202 * random number uniformly distributed over their range.
203 *
204 * A number generator is a function object with an operator() that
205 * takes zero arguments and returns a number.
206 *
207 * A compliant random number generator must satisfy the following
208 * requirements. <table border=1 cellpadding=10 cellspacing=0>
209 * <caption align=top>Random Number Generator Requirements</caption>
210 * <tr><td>To be documented.</td></tr> </table>
211 *
212 * @{
213 */
214
215 /**
216 * @brief A model of a linear congruential random number generator.
217 *
218 * A random number generator that produces pseudorandom numbers via
219 * linear function:
220 * @f[
221 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
222 * @f]
223 *
224 * The template parameter @p _UIntType must be an unsigned integral type
225 * large enough to store values up to (__m-1). If the template parameter
226 * @p __m is 0, the modulus @p __m used is
227 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
228 * parameters @p __a and @p __c must be less than @p __m.
229 *
230 * The size of the state is @f$1@f$.
231 */
232 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
233 class linear_congruential_engine
234 {
235 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
236 "substituting _UIntType not an unsigned integral type");
237 static_assert(__m == 0u || (__a < __m && __c < __m),
238 "template argument substituting __m out of bounds");
239
240 public:
241 /** The type of the generated random value. */
242 typedef _UIntType result_type;
243
244 /** The multiplier. */
245 static constexpr result_type multiplier = __a;
246 /** An increment. */
247 static constexpr result_type increment = __c;
248 /** The modulus. */
249 static constexpr result_type modulus = __m;
250 static constexpr result_type default_seed = 1u;
251
252 /**
253 * @brief Constructs a %linear_congruential_engine random number
254 * generator engine with seed @p __s. The default seed value
255 * is 1.
256 *
257 * @param __s The initial seed value.
258 */
259 explicit
260 linear_congruential_engine(result_type __s = default_seed)
261 { seed(__s); }
262
263 /**
264 * @brief Constructs a %linear_congruential_engine random number
265 * generator engine seeded from the seed sequence @p __q.
266 *
267 * @param __q the seed sequence.
268 */
269 template<typename _Sseq, typename = typename
270 std::enable_if<!std::is_same<_Sseq, linear_congruential_engine>::value>
271 ::type>
272 explicit
273 linear_congruential_engine(_Sseq& __q)
274 { seed(__q); }
275
276 /**
277 * @brief Reseeds the %linear_congruential_engine random number generator
278 * engine sequence to the seed @p __s.
279 *
280 * @param __s The new seed.
281 */
282 void
283 seed(result_type __s = default_seed);
284
285 /**
286 * @brief Reseeds the %linear_congruential_engine random number generator
287 * engine
288 * sequence using values from the seed sequence @p __q.
289 *
290 * @param __q the seed sequence.
291 */
292 template<typename _Sseq>
293 typename std::enable_if<std::is_class<_Sseq>::value>::type
294 seed(_Sseq& __q);
295
296 /**
297 * @brief Gets the smallest possible value in the output range.
298 *
299 * The minimum depends on the @p __c parameter: if it is zero, the
300 * minimum generated must be > 0, otherwise 0 is allowed.
301 */
302 static constexpr result_type
303 min()
304 { return __c == 0u ? 1u : 0u; }
305
306 /**
307 * @brief Gets the largest possible value in the output range.
308 */
309 static constexpr result_type
310 max()
311 { return __m - 1u; }
312
313 /**
314 * @brief Discard a sequence of random numbers.
315 */
316 void
317 discard(unsigned long long __z)
318 {
319 for (; __z != 0ULL; --__z)
320 (*this)();
321 }
322
323 /**
324 * @brief Gets the next random number in the sequence.
325 */
326 result_type
327 operator()()
328 {
329 _M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x);
330 return _M_x;
331 }
332
333 /**
334 * @brief Compares two linear congruential random number generator
335 * objects of the same type for equality.
336 *
337 * @param __lhs A linear congruential random number generator object.
338 * @param __rhs Another linear congruential random number generator
339 * object.
340 *
341 * @returns true if the infinite sequences of generated values
342 * would be equal, false otherwise.
343 */
344 friend bool
345 operator==(const linear_congruential_engine& __lhs,
346 const linear_congruential_engine& __rhs)
347 { return __lhs._M_x == __rhs._M_x; }
348
349 /**
350 * @brief Writes the textual representation of the state x(i) of x to
351 * @p __os.
352 *
353 * @param __os The output stream.
354 * @param __lcr A % linear_congruential_engine random number generator.
355 * @returns __os.
356 */
357 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
358 _UIntType1 __m1, typename _CharT, typename _Traits>
359 friend std::basic_ostream<_CharT, _Traits>&
360 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
361 const std::linear_congruential_engine<_UIntType1,
362 __a1, __c1, __m1>& __lcr);
363
364 /**
365 * @brief Sets the state of the engine by reading its textual
366 * representation from @p __is.
367 *
368 * The textual representation must have been previously written using
369 * an output stream whose imbued locale and whose type's template
370 * specialization arguments _CharT and _Traits were the same as those
371 * of @p __is.
372 *
373 * @param __is The input stream.
374 * @param __lcr A % linear_congruential_engine random number generator.
375 * @returns __is.
376 */
377 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
378 _UIntType1 __m1, typename _CharT, typename _Traits>
379 friend std::basic_istream<_CharT, _Traits>&
380 operator>>(std::basic_istream<_CharT, _Traits>& __is,
381 std::linear_congruential_engine<_UIntType1, __a1,
382 __c1, __m1>& __lcr);
383
384 private:
385 _UIntType _M_x;
386 };
387
388 /**
389 * @brief Compares two linear congruential random number generator
390 * objects of the same type for inequality.
391 *
392 * @param __lhs A linear congruential random number generator object.
393 * @param __rhs Another linear congruential random number generator
394 * object.
395 *
396 * @returns true if the infinite sequences of generated values
397 * would be different, false otherwise.
398 */
399 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
400 inline bool
401 operator!=(const std::linear_congruential_engine<_UIntType, __a,
402 __c, __m>& __lhs,
403 const std::linear_congruential_engine<_UIntType, __a,
404 __c, __m>& __rhs)
405 { return !(__lhs == __rhs); }
406
407
408 /**
409 * A generalized feedback shift register discrete random number generator.
410 *
411 * This algorithm avoids multiplication and division and is designed to be
412 * friendly to a pipelined architecture. If the parameters are chosen
413 * correctly, this generator will produce numbers with a very long period and
414 * fairly good apparent entropy, although still not cryptographically strong.
415 *
416 * The best way to use this generator is with the predefined mt19937 class.
417 *
418 * This algorithm was originally invented by Makoto Matsumoto and
419 * Takuji Nishimura.
420 *
421 * @tparam __w Word size, the number of bits in each element of
422 * the state vector.
423 * @tparam __n The degree of recursion.
424 * @tparam __m The period parameter.
425 * @tparam __r The separation point bit index.
426 * @tparam __a The last row of the twist matrix.
427 * @tparam __u The first right-shift tempering matrix parameter.
428 * @tparam __d The first right-shift tempering matrix mask.
429 * @tparam __s The first left-shift tempering matrix parameter.
430 * @tparam __b The first left-shift tempering matrix mask.
431 * @tparam __t The second left-shift tempering matrix parameter.
432 * @tparam __c The second left-shift tempering matrix mask.
433 * @tparam __l The second right-shift tempering matrix parameter.
434 * @tparam __f Initialization multiplier.
435 */
436 template<typename _UIntType, size_t __w,
437 size_t __n, size_t __m, size_t __r,
438 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
439 _UIntType __b, size_t __t,
440 _UIntType __c, size_t __l, _UIntType __f>
441 class mersenne_twister_engine
442 {
443 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
444 "substituting _UIntType not an unsigned integral type");
445 static_assert(1u <= __m && __m <= __n,
446 "template argument substituting __m out of bounds");
447 static_assert(__r <= __w, "template argument substituting "
448 "__r out of bound");
449 static_assert(__u <= __w, "template argument substituting "
450 "__u out of bound");
451 static_assert(__s <= __w, "template argument substituting "
452 "__s out of bound");
453 static_assert(__t <= __w, "template argument substituting "
454 "__t out of bound");
455 static_assert(__l <= __w, "template argument substituting "
456 "__l out of bound");
457 static_assert(__w <= std::numeric_limits<_UIntType>::digits,
458 "template argument substituting __w out of bound");
459 static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1),
460 "template argument substituting __a out of bound");
461 static_assert(__b <= (__detail::_Shift<_UIntType, __w>::__value - 1),
462 "template argument substituting __b out of bound");
463 static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1),
464 "template argument substituting __c out of bound");
465 static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1),
466 "template argument substituting __d out of bound");
467 static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1),
468 "template argument substituting __f out of bound");
469
470 public:
471 /** The type of the generated random value. */
472 typedef _UIntType result_type;
473
474 // parameter values
475 static constexpr size_t word_size = __w;
476 static constexpr size_t state_size = __n;
477 static constexpr size_t shift_size = __m;
478 static constexpr size_t mask_bits = __r;
479 static constexpr result_type xor_mask = __a;
480 static constexpr size_t tempering_u = __u;
481 static constexpr result_type tempering_d = __d;
482 static constexpr size_t tempering_s = __s;
483 static constexpr result_type tempering_b = __b;
484 static constexpr size_t tempering_t = __t;
485 static constexpr result_type tempering_c = __c;
486 static constexpr size_t tempering_l = __l;
487 static constexpr result_type initialization_multiplier = __f;
488 static constexpr result_type default_seed = 5489u;
489
490 // constructors and member function
491 explicit
492 mersenne_twister_engine(result_type __sd = default_seed)
493 { seed(__sd); }
494
495 /**
496 * @brief Constructs a %mersenne_twister_engine random number generator
497 * engine seeded from the seed sequence @p __q.
498 *
499 * @param __q the seed sequence.
500 */
501 template<typename _Sseq, typename = typename
502 std::enable_if<!std::is_same<_Sseq, mersenne_twister_engine>::value>
503 ::type>
504 explicit
505 mersenne_twister_engine(_Sseq& __q)
506 { seed(__q); }
507
508 void
509 seed(result_type __sd = default_seed);
510
511 template<typename _Sseq>
512 typename std::enable_if<std::is_class<_Sseq>::value>::type
513 seed(_Sseq& __q);
514
515 /**
516 * @brief Gets the smallest possible value in the output range.
517 */
518 static constexpr result_type
519 min()
520 { return 0; };
521
522 /**
523 * @brief Gets the largest possible value in the output range.
524 */
525 static constexpr result_type
526 max()
527 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
528
529 /**
530 * @brief Discard a sequence of random numbers.
531 */
532 void
533 discard(unsigned long long __z);
534
535 result_type
536 operator()();
537
538 /**
539 * @brief Compares two % mersenne_twister_engine random number generator
540 * objects of the same type for equality.
541 *
542 * @param __lhs A % mersenne_twister_engine random number generator
543 * object.
544 * @param __rhs Another % mersenne_twister_engine random number
545 * generator object.
546 *
547 * @returns true if the infinite sequences of generated values
548 * would be equal, false otherwise.
549 */
550 friend bool
551 operator==(const mersenne_twister_engine& __lhs,
552 const mersenne_twister_engine& __rhs)
553 { return (std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x)
554 && __lhs._M_p == __rhs._M_p); }
555
556 /**
557 * @brief Inserts the current state of a % mersenne_twister_engine
558 * random number generator engine @p __x into the output stream
559 * @p __os.
560 *
561 * @param __os An output stream.
562 * @param __x A % mersenne_twister_engine random number generator
563 * engine.
564 *
565 * @returns The output stream with the state of @p __x inserted or in
566 * an error state.
567 */
568 template<typename _UIntType1,
569 size_t __w1, size_t __n1,
570 size_t __m1, size_t __r1,
571 _UIntType1 __a1, size_t __u1,
572 _UIntType1 __d1, size_t __s1,
573 _UIntType1 __b1, size_t __t1,
574 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
575 typename _CharT, typename _Traits>
576 friend std::basic_ostream<_CharT, _Traits>&
577 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
578 const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
579 __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
580 __l1, __f1>& __x);
581
582 /**
583 * @brief Extracts the current state of a % mersenne_twister_engine
584 * random number generator engine @p __x from the input stream
585 * @p __is.
586 *
587 * @param __is An input stream.
588 * @param __x A % mersenne_twister_engine random number generator
589 * engine.
590 *
591 * @returns The input stream with the state of @p __x extracted or in
592 * an error state.
593 */
594 template<typename _UIntType1,
595 size_t __w1, size_t __n1,
596 size_t __m1, size_t __r1,
597 _UIntType1 __a1, size_t __u1,
598 _UIntType1 __d1, size_t __s1,
599 _UIntType1 __b1, size_t __t1,
600 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
601 typename _CharT, typename _Traits>
602 friend std::basic_istream<_CharT, _Traits>&
603 operator>>(std::basic_istream<_CharT, _Traits>& __is,
604 std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
605 __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
606 __l1, __f1>& __x);
607
608 private:
609 void _M_gen_rand();
610
611 _UIntType _M_x[state_size];
612 size_t _M_p;
613 };
614
615 /**
616 * @brief Compares two % mersenne_twister_engine random number generator
617 * objects of the same type for inequality.
618 *
619 * @param __lhs A % mersenne_twister_engine random number generator
620 * object.
621 * @param __rhs Another % mersenne_twister_engine random number
622 * generator object.
623 *
624 * @returns true if the infinite sequences of generated values
625 * would be different, false otherwise.
626 */
627 template<typename _UIntType, size_t __w,
628 size_t __n, size_t __m, size_t __r,
629 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
630 _UIntType __b, size_t __t,
631 _UIntType __c, size_t __l, _UIntType __f>
632 inline bool
633 operator!=(const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
634 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __lhs,
635 const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
636 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __rhs)
637 { return !(__lhs == __rhs); }
638
639
640 /**
641 * @brief The Marsaglia-Zaman generator.
642 *
643 * This is a model of a Generalized Fibonacci discrete random number
644 * generator, sometimes referred to as the SWC generator.
645 *
646 * A discrete random number generator that produces pseudorandom
647 * numbers using:
648 * @f[
649 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
650 * @f]
651 *
652 * The size of the state is @f$r@f$
653 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
654 *
655 * @var _M_x The state of the generator. This is a ring buffer.
656 * @var _M_carry The carry.
657 * @var _M_p Current index of x(i - r).
658 */
659 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
660 class subtract_with_carry_engine
661 {
662 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
663 "substituting _UIntType not an unsigned integral type");
664 static_assert(0u < __s && __s < __r,
665 "template argument substituting __s out of bounds");
666 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
667 "template argument substituting __w out of bounds");
668
669 public:
670 /** The type of the generated random value. */
671 typedef _UIntType result_type;
672
673 // parameter values
674 static constexpr size_t word_size = __w;
675 static constexpr size_t short_lag = __s;
676 static constexpr size_t long_lag = __r;
677 static constexpr result_type default_seed = 19780503u;
678
679 /**
680 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
681 * random number generator.
682 */
683 explicit
684 subtract_with_carry_engine(result_type __sd = default_seed)
685 { seed(__sd); }
686
687 /**
688 * @brief Constructs a %subtract_with_carry_engine random number engine
689 * seeded from the seed sequence @p __q.
690 *
691 * @param __q the seed sequence.
692 */
693 template<typename _Sseq, typename = typename
694 std::enable_if<!std::is_same<_Sseq, subtract_with_carry_engine>::value>
695 ::type>
696 explicit
697 subtract_with_carry_engine(_Sseq& __q)
698 { seed(__q); }
699
700 /**
701 * @brief Seeds the initial state @f$x_0@f$ of the random number
702 * generator.
703 *
704 * N1688[4.19] modifies this as follows. If @p __value == 0,
705 * sets value to 19780503. In any case, with a linear
706 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
707 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
708 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
709 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
710 * set carry to 1, otherwise sets carry to 0.
711 */
712 void
713 seed(result_type __sd = default_seed);
714
715 /**
716 * @brief Seeds the initial state @f$x_0@f$ of the
717 * % subtract_with_carry_engine random number generator.
718 */
719 template<typename _Sseq>
720 typename std::enable_if<std::is_class<_Sseq>::value>::type
721 seed(_Sseq& __q);
722
723 /**
724 * @brief Gets the inclusive minimum value of the range of random
725 * integers returned by this generator.
726 */
727 static constexpr result_type
728 min()
729 { return 0; }
730
731 /**
732 * @brief Gets the inclusive maximum value of the range of random
733 * integers returned by this generator.
734 */
735 static constexpr result_type
736 max()
737 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
738
739 /**
740 * @brief Discard a sequence of random numbers.
741 */
742 void
743 discard(unsigned long long __z)
744 {
745 for (; __z != 0ULL; --__z)
746 (*this)();
747 }
748
749 /**
750 * @brief Gets the next random number in the sequence.
751 */
752 result_type
753 operator()();
754
755 /**
756 * @brief Compares two % subtract_with_carry_engine random number
757 * generator objects of the same type for equality.
758 *
759 * @param __lhs A % subtract_with_carry_engine random number generator
760 * object.
761 * @param __rhs Another % subtract_with_carry_engine random number
762 * generator object.
763 *
764 * @returns true if the infinite sequences of generated values
765 * would be equal, false otherwise.
766 */
767 friend bool
768 operator==(const subtract_with_carry_engine& __lhs,
769 const subtract_with_carry_engine& __rhs)
770 { return (std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x)
771 && __lhs._M_carry == __rhs._M_carry
772 && __lhs._M_p == __rhs._M_p); }
773
774 /**
775 * @brief Inserts the current state of a % subtract_with_carry_engine
776 * random number generator engine @p __x into the output stream
777 * @p __os.
778 *
779 * @param __os An output stream.
780 * @param __x A % subtract_with_carry_engine random number generator
781 * engine.
782 *
783 * @returns The output stream with the state of @p __x inserted or in
784 * an error state.
785 */
786 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
787 typename _CharT, typename _Traits>
788 friend std::basic_ostream<_CharT, _Traits>&
789 operator<<(std::basic_ostream<_CharT, _Traits>&,
790 const std::subtract_with_carry_engine<_UIntType1, __w1,
791 __s1, __r1>&);
792
793 /**
794 * @brief Extracts the current state of a % subtract_with_carry_engine
795 * random number generator engine @p __x from the input stream
796 * @p __is.
797 *
798 * @param __is An input stream.
799 * @param __x A % subtract_with_carry_engine random number generator
800 * engine.
801 *
802 * @returns The input stream with the state of @p __x extracted or in
803 * an error state.
804 */
805 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
806 typename _CharT, typename _Traits>
807 friend std::basic_istream<_CharT, _Traits>&
808 operator>>(std::basic_istream<_CharT, _Traits>&,
809 std::subtract_with_carry_engine<_UIntType1, __w1,
810 __s1, __r1>&);
811
812 private:
813 _UIntType _M_x[long_lag];
814 _UIntType _M_carry;
815 size_t _M_p;
816 };
817
818 /**
819 * @brief Compares two % subtract_with_carry_engine random number
820 * generator objects of the same type for inequality.
821 *
822 * @param __lhs A % subtract_with_carry_engine random number generator
823 * object.
824 * @param __rhs Another % subtract_with_carry_engine random number
825 * generator object.
826 *
827 * @returns true if the infinite sequences of generated values
828 * would be different, false otherwise.
829 */
830 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
831 inline bool
832 operator!=(const std::subtract_with_carry_engine<_UIntType, __w,
833 __s, __r>& __lhs,
834 const std::subtract_with_carry_engine<_UIntType, __w,
835 __s, __r>& __rhs)
836 { return !(__lhs == __rhs); }
837
838
839 /**
840 * Produces random numbers from some base engine by discarding blocks of
841 * data.
842 *
843 * 0 <= @p __r <= @p __p
844 */
845 template<typename _RandomNumberEngine, size_t __p, size_t __r>
846 class discard_block_engine
847 {
848 static_assert(1 <= __r && __r <= __p,
849 "template argument substituting __r out of bounds");
850
851 public:
852 /** The type of the generated random value. */
853 typedef typename _RandomNumberEngine::result_type result_type;
854
855 // parameter values
856 static constexpr size_t block_size = __p;
857 static constexpr size_t used_block = __r;
858
859 /**
860 * @brief Constructs a default %discard_block_engine engine.
861 *
862 * The underlying engine is default constructed as well.
863 */
864 discard_block_engine()
865 : _M_b(), _M_n(0) { }
866
867 /**
868 * @brief Copy constructs a %discard_block_engine engine.
869 *
870 * Copies an existing base class random number generator.
871 * @param __rng An existing (base class) engine object.
872 */
873 explicit
874 discard_block_engine(const _RandomNumberEngine& __rng)
875 : _M_b(__rng), _M_n(0) { }
876
877 /**
878 * @brief Move constructs a %discard_block_engine engine.
879 *
880 * Copies an existing base class random number generator.
881 * @param __rng An existing (base class) engine object.
882 */
883 explicit
884 discard_block_engine(_RandomNumberEngine&& __rng)
885 : _M_b(std::move(__rng)), _M_n(0) { }
886
887 /**
888 * @brief Seed constructs a %discard_block_engine engine.
889 *
890 * Constructs the underlying generator engine seeded with @p __s.
891 * @param __s A seed value for the base class engine.
892 */
893 explicit
894 discard_block_engine(result_type __s)
895 : _M_b(__s), _M_n(0) { }
896
897 /**
898 * @brief Generator construct a %discard_block_engine engine.
899 *
900 * @param __q A seed sequence.
901 */
902 template<typename _Sseq, typename = typename
903 std::enable_if<!std::is_same<_Sseq, discard_block_engine>::value
904 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
905 ::type>
906 explicit
907 discard_block_engine(_Sseq& __q)
908 : _M_b(__q), _M_n(0)
909 { }
910
911 /**
912 * @brief Reseeds the %discard_block_engine object with the default
913 * seed for the underlying base class generator engine.
914 */
915 void
916 seed()
917 {
918 _M_b.seed();
919 _M_n = 0;
920 }
921
922 /**
923 * @brief Reseeds the %discard_block_engine object with the default
924 * seed for the underlying base class generator engine.
925 */
926 void
927 seed(result_type __s)
928 {
929 _M_b.seed(__s);
930 _M_n = 0;
931 }
932
933 /**
934 * @brief Reseeds the %discard_block_engine object with the given seed
935 * sequence.
936 * @param __q A seed generator function.
937 */
938 template<typename _Sseq>
939 void
940 seed(_Sseq& __q)
941 {
942 _M_b.seed(__q);
943 _M_n = 0;
944 }
945
946 /**
947 * @brief Gets a const reference to the underlying generator engine
948 * object.
949 */
950 const _RandomNumberEngine&
951 base() const noexcept
952 { return _M_b; }
953
954 /**
955 * @brief Gets the minimum value in the generated random number range.
956 */
957 static constexpr result_type
958 min()
959 { return _RandomNumberEngine::min(); }
960
961 /**
962 * @brief Gets the maximum value in the generated random number range.
963 */
964 static constexpr result_type
965 max()
966 { return _RandomNumberEngine::max(); }
967
968 /**
969 * @brief Discard a sequence of random numbers.
970 */
971 void
972 discard(unsigned long long __z)
973 {
974 for (; __z != 0ULL; --__z)
975 (*this)();
976 }
977
978 /**
979 * @brief Gets the next value in the generated random number sequence.
980 */
981 result_type
982 operator()();
983
984 /**
985 * @brief Compares two %discard_block_engine random number generator
986 * objects of the same type for equality.
987 *
988 * @param __lhs A %discard_block_engine random number generator object.
989 * @param __rhs Another %discard_block_engine random number generator
990 * object.
991 *
992 * @returns true if the infinite sequences of generated values
993 * would be equal, false otherwise.
994 */
995 friend bool
996 operator==(const discard_block_engine& __lhs,
997 const discard_block_engine& __rhs)
998 { return __lhs._M_b == __rhs._M_b && __lhs._M_n == __rhs._M_n; }
999
1000 /**
1001 * @brief Inserts the current state of a %discard_block_engine random
1002 * number generator engine @p __x into the output stream
1003 * @p __os.
1004 *
1005 * @param __os An output stream.
1006 * @param __x A %discard_block_engine random number generator engine.
1007 *
1008 * @returns The output stream with the state of @p __x inserted or in
1009 * an error state.
1010 */
1011 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
1012 typename _CharT, typename _Traits>
1013 friend std::basic_ostream<_CharT, _Traits>&
1014 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1015 const std::discard_block_engine<_RandomNumberEngine1,
1016 __p1, __r1>& __x);
1017
1018 /**
1019 * @brief Extracts the current state of a % subtract_with_carry_engine
1020 * random number generator engine @p __x from the input stream
1021 * @p __is.
1022 *
1023 * @param __is An input stream.
1024 * @param __x A %discard_block_engine random number generator engine.
1025 *
1026 * @returns The input stream with the state of @p __x extracted or in
1027 * an error state.
1028 */
1029 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
1030 typename _CharT, typename _Traits>
1031 friend std::basic_istream<_CharT, _Traits>&
1032 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1033 std::discard_block_engine<_RandomNumberEngine1,
1034 __p1, __r1>& __x);
1035
1036 private:
1037 _RandomNumberEngine _M_b;
1038 size_t _M_n;
1039 };
1040
1041 /**
1042 * @brief Compares two %discard_block_engine random number generator
1043 * objects of the same type for inequality.
1044 *
1045 * @param __lhs A %discard_block_engine random number generator object.
1046 * @param __rhs Another %discard_block_engine random number generator
1047 * object.
1048 *
1049 * @returns true if the infinite sequences of generated values
1050 * would be different, false otherwise.
1051 */
1052 template<typename _RandomNumberEngine, size_t __p, size_t __r>
1053 inline bool
1054 operator!=(const std::discard_block_engine<_RandomNumberEngine, __p,
1055 __r>& __lhs,
1056 const std::discard_block_engine<_RandomNumberEngine, __p,
1057 __r>& __rhs)
1058 { return !(__lhs == __rhs); }
1059
1060
1061 /**
1062 * Produces random numbers by combining random numbers from some base
1063 * engine to produce random numbers with a specifies number of bits @p __w.
1064 */
1065 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
1066 class independent_bits_engine
1067 {
1068 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
1069 "substituting _UIntType not an unsigned integral type");
1070 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
1071 "template argument substituting __w out of bounds");
1072
1073 public:
1074 /** The type of the generated random value. */
1075 typedef _UIntType result_type;
1076
1077 /**
1078 * @brief Constructs a default %independent_bits_engine engine.
1079 *
1080 * The underlying engine is default constructed as well.
1081 */
1082 independent_bits_engine()
1083 : _M_b() { }
1084
1085 /**
1086 * @brief Copy constructs a %independent_bits_engine engine.
1087 *
1088 * Copies an existing base class random number generator.
1089 * @param __rng An existing (base class) engine object.
1090 */
1091 explicit
1092 independent_bits_engine(const _RandomNumberEngine& __rng)
1093 : _M_b(__rng) { }
1094
1095 /**
1096 * @brief Move constructs a %independent_bits_engine engine.
1097 *
1098 * Copies an existing base class random number generator.
1099 * @param __rng An existing (base class) engine object.
1100 */
1101 explicit
1102 independent_bits_engine(_RandomNumberEngine&& __rng)
1103 : _M_b(std::move(__rng)) { }
1104
1105 /**
1106 * @brief Seed constructs a %independent_bits_engine engine.
1107 *
1108 * Constructs the underlying generator engine seeded with @p __s.
1109 * @param __s A seed value for the base class engine.
1110 */
1111 explicit
1112 independent_bits_engine(result_type __s)
1113 : _M_b(__s) { }
1114
1115 /**
1116 * @brief Generator construct a %independent_bits_engine engine.
1117 *
1118 * @param __q A seed sequence.
1119 */
1120 template<typename _Sseq, typename = typename
1121 std::enable_if<!std::is_same<_Sseq, independent_bits_engine>::value
1122 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
1123 ::type>
1124 explicit
1125 independent_bits_engine(_Sseq& __q)
1126 : _M_b(__q)
1127 { }
1128
1129 /**
1130 * @brief Reseeds the %independent_bits_engine object with the default
1131 * seed for the underlying base class generator engine.
1132 */
1133 void
1134 seed()
1135 { _M_b.seed(); }
1136
1137 /**
1138 * @brief Reseeds the %independent_bits_engine object with the default
1139 * seed for the underlying base class generator engine.
1140 */
1141 void
1142 seed(result_type __s)
1143 { _M_b.seed(__s); }
1144
1145 /**
1146 * @brief Reseeds the %independent_bits_engine object with the given
1147 * seed sequence.
1148 * @param __q A seed generator function.
1149 */
1150 template<typename _Sseq>
1151 void
1152 seed(_Sseq& __q)
1153 { _M_b.seed(__q); }
1154
1155 /**
1156 * @brief Gets a const reference to the underlying generator engine
1157 * object.
1158 */
1159 const _RandomNumberEngine&
1160 base() const noexcept
1161 { return _M_b; }
1162
1163 /**
1164 * @brief Gets the minimum value in the generated random number range.
1165 */
1166 static constexpr result_type
1167 min()
1168 { return 0U; }
1169
1170 /**
1171 * @brief Gets the maximum value in the generated random number range.
1172 */
1173 static constexpr result_type
1174 max()
1175 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
1176
1177 /**
1178 * @brief Discard a sequence of random numbers.
1179 */
1180 void
1181 discard(unsigned long long __z)
1182 {
1183 for (; __z != 0ULL; --__z)
1184 (*this)();
1185 }
1186
1187 /**
1188 * @brief Gets the next value in the generated random number sequence.
1189 */
1190 result_type
1191 operator()();
1192
1193 /**
1194 * @brief Compares two %independent_bits_engine random number generator
1195 * objects of the same type for equality.
1196 *
1197 * @param __lhs A %independent_bits_engine random number generator
1198 * object.
1199 * @param __rhs Another %independent_bits_engine random number generator
1200 * object.
1201 *
1202 * @returns true if the infinite sequences of generated values
1203 * would be equal, false otherwise.
1204 */
1205 friend bool
1206 operator==(const independent_bits_engine& __lhs,
1207 const independent_bits_engine& __rhs)
1208 { return __lhs._M_b == __rhs._M_b; }
1209
1210 /**
1211 * @brief Extracts the current state of a % subtract_with_carry_engine
1212 * random number generator engine @p __x from the input stream
1213 * @p __is.
1214 *
1215 * @param __is An input stream.
1216 * @param __x A %independent_bits_engine random number generator
1217 * engine.
1218 *
1219 * @returns The input stream with the state of @p __x extracted or in
1220 * an error state.
1221 */
1222 template<typename _CharT, typename _Traits>
1223 friend std::basic_istream<_CharT, _Traits>&
1224 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1225 std::independent_bits_engine<_RandomNumberEngine,
1226 __w, _UIntType>& __x)
1227 {
1228 __is >> __x._M_b;
1229 return __is;
1230 }
1231
1232 private:
1233 _RandomNumberEngine _M_b;
1234 };
1235
1236 /**
1237 * @brief Compares two %independent_bits_engine random number generator
1238 * objects of the same type for inequality.
1239 *
1240 * @param __lhs A %independent_bits_engine random number generator
1241 * object.
1242 * @param __rhs Another %independent_bits_engine random number generator
1243 * object.
1244 *
1245 * @returns true if the infinite sequences of generated values
1246 * would be different, false otherwise.
1247 */
1248 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
1249 inline bool
1250 operator!=(const std::independent_bits_engine<_RandomNumberEngine, __w,
1251 _UIntType>& __lhs,
1252 const std::independent_bits_engine<_RandomNumberEngine, __w,
1253 _UIntType>& __rhs)
1254 { return !(__lhs == __rhs); }
1255
1256 /**
1257 * @brief Inserts the current state of a %independent_bits_engine random
1258 * number generator engine @p __x into the output stream @p __os.
1259 *
1260 * @param __os An output stream.
1261 * @param __x A %independent_bits_engine random number generator engine.
1262 *
1263 * @returns The output stream with the state of @p __x inserted or in
1264 * an error state.
1265 */
1266 template<typename _RandomNumberEngine, size_t __w, typename _UIntType,
1267 typename _CharT, typename _Traits>
1268 std::basic_ostream<_CharT, _Traits>&
1269 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1270 const std::independent_bits_engine<_RandomNumberEngine,
1271 __w, _UIntType>& __x)
1272 {
1273 __os << __x.base();
1274 return __os;
1275 }
1276
1277
1278 /**
1279 * @brief Produces random numbers by combining random numbers from some
1280 * base engine to produce random numbers with a specifies number of bits
1281 * @p __w.
1282 */
1283 template<typename _RandomNumberEngine, size_t __k>
1284 class shuffle_order_engine
1285 {
1286 static_assert(1u <= __k, "template argument substituting "
1287 "__k out of bound");
1288
1289 public:
1290 /** The type of the generated random value. */
1291 typedef typename _RandomNumberEngine::result_type result_type;
1292
1293 static constexpr size_t table_size = __k;
1294
1295 /**
1296 * @brief Constructs a default %shuffle_order_engine engine.
1297 *
1298 * The underlying engine is default constructed as well.
1299 */
1300 shuffle_order_engine()
1301 : _M_b()
1302 { _M_initialize(); }
1303
1304 /**
1305 * @brief Copy constructs a %shuffle_order_engine engine.
1306 *
1307 * Copies an existing base class random number generator.
1308 * @param __rng An existing (base class) engine object.
1309 */
1310 explicit
1311 shuffle_order_engine(const _RandomNumberEngine& __rng)
1312 : _M_b(__rng)
1313 { _M_initialize(); }
1314
1315 /**
1316 * @brief Move constructs a %shuffle_order_engine engine.
1317 *
1318 * Copies an existing base class random number generator.
1319 * @param __rng An existing (base class) engine object.
1320 */
1321 explicit
1322 shuffle_order_engine(_RandomNumberEngine&& __rng)
1323 : _M_b(std::move(__rng))
1324 { _M_initialize(); }
1325
1326 /**
1327 * @brief Seed constructs a %shuffle_order_engine engine.
1328 *
1329 * Constructs the underlying generator engine seeded with @p __s.
1330 * @param __s A seed value for the base class engine.
1331 */
1332 explicit
1333 shuffle_order_engine(result_type __s)
1334 : _M_b(__s)
1335 { _M_initialize(); }
1336
1337 /**
1338 * @brief Generator construct a %shuffle_order_engine engine.
1339 *
1340 * @param __q A seed sequence.
1341 */
1342 template<typename _Sseq, typename = typename
1343 std::enable_if<!std::is_same<_Sseq, shuffle_order_engine>::value
1344 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
1345 ::type>
1346 explicit
1347 shuffle_order_engine(_Sseq& __q)
1348 : _M_b(__q)
1349 { _M_initialize(); }
1350
1351 /**
1352 * @brief Reseeds the %shuffle_order_engine object with the default seed
1353 for the underlying base class generator engine.
1354 */
1355 void
1356 seed()
1357 {
1358 _M_b.seed();
1359 _M_initialize();
1360 }
1361
1362 /**
1363 * @brief Reseeds the %shuffle_order_engine object with the default seed
1364 * for the underlying base class generator engine.
1365 */
1366 void
1367 seed(result_type __s)
1368 {
1369 _M_b.seed(__s);
1370 _M_initialize();
1371 }
1372
1373 /**
1374 * @brief Reseeds the %shuffle_order_engine object with the given seed
1375 * sequence.
1376 * @param __q A seed generator function.
1377 */
1378 template<typename _Sseq>
1379 void
1380 seed(_Sseq& __q)
1381 {
1382 _M_b.seed(__q);
1383 _M_initialize();
1384 }
1385
1386 /**
1387 * Gets a const reference to the underlying generator engine object.
1388 */
1389 const _RandomNumberEngine&
1390 base() const noexcept
1391 { return _M_b; }
1392
1393 /**
1394 * Gets the minimum value in the generated random number range.
1395 */
1396 static constexpr result_type
1397 min()
1398 { return _RandomNumberEngine::min(); }
1399
1400 /**
1401 * Gets the maximum value in the generated random number range.
1402 */
1403 static constexpr result_type
1404 max()
1405 { return _RandomNumberEngine::max(); }
1406
1407 /**
1408 * Discard a sequence of random numbers.
1409 */
1410 void
1411 discard(unsigned long long __z)
1412 {
1413 for (; __z != 0ULL; --__z)
1414 (*this)();
1415 }
1416
1417 /**
1418 * Gets the next value in the generated random number sequence.
1419 */
1420 result_type
1421 operator()();
1422
1423 /**
1424 * Compares two %shuffle_order_engine random number generator objects
1425 * of the same type for equality.
1426 *
1427 * @param __lhs A %shuffle_order_engine random number generator object.
1428 * @param __rhs Another %shuffle_order_engine random number generator
1429 * object.
1430 *
1431 * @returns true if the infinite sequences of generated values
1432 * would be equal, false otherwise.
1433 */
1434 friend bool
1435 operator==(const shuffle_order_engine& __lhs,
1436 const shuffle_order_engine& __rhs)
1437 { return (__lhs._M_b == __rhs._M_b
1438 && std::equal(__lhs._M_v, __lhs._M_v + __k, __rhs._M_v)
1439 && __lhs._M_y == __rhs._M_y); }
1440
1441 /**
1442 * @brief Inserts the current state of a %shuffle_order_engine random
1443 * number generator engine @p __x into the output stream
1444 @p __os.
1445 *
1446 * @param __os An output stream.
1447 * @param __x A %shuffle_order_engine random number generator engine.
1448 *
1449 * @returns The output stream with the state of @p __x inserted or in
1450 * an error state.
1451 */
1452 template<typename _RandomNumberEngine1, size_t __k1,
1453 typename _CharT, typename _Traits>
1454 friend std::basic_ostream<_CharT, _Traits>&
1455 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1456 const std::shuffle_order_engine<_RandomNumberEngine1,
1457 __k1>& __x);
1458
1459 /**
1460 * @brief Extracts the current state of a % subtract_with_carry_engine
1461 * random number generator engine @p __x from the input stream
1462 * @p __is.
1463 *
1464 * @param __is An input stream.
1465 * @param __x A %shuffle_order_engine random number generator engine.
1466 *
1467 * @returns The input stream with the state of @p __x extracted or in
1468 * an error state.
1469 */
1470 template<typename _RandomNumberEngine1, size_t __k1,
1471 typename _CharT, typename _Traits>
1472 friend std::basic_istream<_CharT, _Traits>&
1473 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1474 std::shuffle_order_engine<_RandomNumberEngine1, __k1>& __x);
1475
1476 private:
1477 void _M_initialize()
1478 {
1479 for (size_t __i = 0; __i < __k; ++__i)
1480 _M_v[__i] = _M_b();
1481 _M_y = _M_b();
1482 }
1483
1484 _RandomNumberEngine _M_b;
1485 result_type _M_v[__k];
1486 result_type _M_y;
1487 };
1488
1489 /**
1490 * Compares two %shuffle_order_engine random number generator objects
1491 * of the same type for inequality.
1492 *
1493 * @param __lhs A %shuffle_order_engine random number generator object.
1494 * @param __rhs Another %shuffle_order_engine random number generator
1495 * object.
1496 *
1497 * @returns true if the infinite sequences of generated values
1498 * would be different, false otherwise.
1499 */
1500 template<typename _RandomNumberEngine, size_t __k>
1501 inline bool
1502 operator!=(const std::shuffle_order_engine<_RandomNumberEngine,
1503 __k>& __lhs,
1504 const std::shuffle_order_engine<_RandomNumberEngine,
1505 __k>& __rhs)
1506 { return !(__lhs == __rhs); }
1507
1508
1509 /**
1510 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1511 */
1512 typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1513 minstd_rand0;
1514
1515 /**
1516 * An alternative LCR (Lehmer Generator function).
1517 */
1518 typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1519 minstd_rand;
1520
1521 /**
1522 * The classic Mersenne Twister.
1523 *
1524 * Reference:
1525 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1526 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1527 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1528 */
1529 typedef mersenne_twister_engine<
1530 uint_fast32_t,
1531 32, 624, 397, 31,
1532 0x9908b0dfUL, 11,
1533 0xffffffffUL, 7,
1534 0x9d2c5680UL, 15,
1535 0xefc60000UL, 18, 1812433253UL> mt19937;
1536
1537 /**
1538 * An alternative Mersenne Twister.
1539 */
1540 typedef mersenne_twister_engine<
1541 uint_fast64_t,
1542 64, 312, 156, 31,
1543 0xb5026f5aa96619e9ULL, 29,
1544 0x5555555555555555ULL, 17,
1545 0x71d67fffeda60000ULL, 37,
1546 0xfff7eee000000000ULL, 43,
1547 6364136223846793005ULL> mt19937_64;
1548
1549 typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
1550 ranlux24_base;
1551
1552 typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
1553 ranlux48_base;
1554
1555 typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
1556
1557 typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
1558
1559 typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
1560
1561 typedef minstd_rand0 default_random_engine;
1562
1563 /**
1564 * A standard interface to a platform-specific non-deterministic
1565 * random number generator (if any are available).
1566 */
1567 class random_device
1568 {
1569 public:
1570 /** The type of the generated random value. */
1571 typedef unsigned int result_type;
1572
1573 // constructors, destructors and member functions
1574
1575 #ifdef _GLIBCXX_USE_RANDOM_TR1
1576
1577 explicit
1578 random_device(const std::string& __token = "/dev/urandom")
1579 {
1580 if ((__token != "/dev/urandom" && __token != "/dev/random")
1581 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1582 std::__throw_runtime_error(__N("random_device::"
1583 "random_device(const std::string&)"));
1584 }
1585
1586 ~random_device()
1587 { std::fclose(_M_file); }
1588
1589 #else
1590
1591 explicit
1592 random_device(const std::string& __token = "mt19937")
1593 : _M_mt(_M_strtoul(__token)) { }
1594
1595 private:
1596 static unsigned long
1597 _M_strtoul(const std::string& __str)
1598 {
1599 unsigned long __ret = 5489UL;
1600 if (__str != "mt19937")
1601 {
1602 const char* __nptr = __str.c_str();
1603 char* __endptr;
1604 __ret = std::strtoul(__nptr, &__endptr, 0);
1605 if (*__nptr == '\0' || *__endptr != '\0')
1606 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1607 "(const std::string&)"));
1608 }
1609 return __ret;
1610 }
1611
1612 public:
1613
1614 #endif
1615
1616 static constexpr result_type
1617 min()
1618 { return std::numeric_limits<result_type>::min(); }
1619
1620 static constexpr result_type
1621 max()
1622 { return std::numeric_limits<result_type>::max(); }
1623
1624 double
1625 entropy() const noexcept
1626 { return 0.0; }
1627
1628 result_type
1629 operator()()
1630 {
1631 #ifdef _GLIBCXX_USE_RANDOM_TR1
1632 result_type __ret;
1633 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1634 1, _M_file);
1635 return __ret;
1636 #else
1637 return _M_mt();
1638 #endif
1639 }
1640
1641 // No copy functions.
1642 random_device(const random_device&) = delete;
1643 void operator=(const random_device&) = delete;
1644
1645 private:
1646
1647 #ifdef _GLIBCXX_USE_RANDOM_TR1
1648 FILE* _M_file;
1649 #else
1650 mt19937 _M_mt;
1651 #endif
1652 };
1653
1654 /* @} */ // group random_generators
1655
1656 /**
1657 * @addtogroup random_distributions Random Number Distributions
1658 * @ingroup random
1659 * @{
1660 */
1661
1662 /**
1663 * @addtogroup random_distributions_uniform Uniform Distributions
1664 * @ingroup random_distributions
1665 * @{
1666 */
1667
1668 /**
1669 * @brief Uniform discrete distribution for random numbers.
1670 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1671 * probability throughout the range.
1672 */
1673 template<typename _IntType = int>
1674 class uniform_int_distribution
1675 {
1676 static_assert(std::is_integral<_IntType>::value,
1677 "template argument not an integral type");
1678
1679 public:
1680 /** The type of the range of the distribution. */
1681 typedef _IntType result_type;
1682 /** Parameter type. */
1683 struct param_type
1684 {
1685 typedef uniform_int_distribution<_IntType> distribution_type;
1686
1687 explicit
1688 param_type(_IntType __a = 0,
1689 _IntType __b = std::numeric_limits<_IntType>::max())
1690 : _M_a(__a), _M_b(__b)
1691 {
1692 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1693 }
1694
1695 result_type
1696 a() const
1697 { return _M_a; }
1698
1699 result_type
1700 b() const
1701 { return _M_b; }
1702
1703 friend bool
1704 operator==(const param_type& __p1, const param_type& __p2)
1705 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
1706
1707 private:
1708 _IntType _M_a;
1709 _IntType _M_b;
1710 };
1711
1712 public:
1713 /**
1714 * @brief Constructs a uniform distribution object.
1715 */
1716 explicit
1717 uniform_int_distribution(_IntType __a = 0,
1718 _IntType __b = std::numeric_limits<_IntType>::max())
1719 : _M_param(__a, __b)
1720 { }
1721
1722 explicit
1723 uniform_int_distribution(const param_type& __p)
1724 : _M_param(__p)
1725 { }
1726
1727 /**
1728 * @brief Resets the distribution state.
1729 *
1730 * Does nothing for the uniform integer distribution.
1731 */
1732 void
1733 reset() { }
1734
1735 result_type
1736 a() const
1737 { return _M_param.a(); }
1738
1739 result_type
1740 b() const
1741 { return _M_param.b(); }
1742
1743 /**
1744 * @brief Returns the parameter set of the distribution.
1745 */
1746 param_type
1747 param() const
1748 { return _M_param; }
1749
1750 /**
1751 * @brief Sets the parameter set of the distribution.
1752 * @param __param The new parameter set of the distribution.
1753 */
1754 void
1755 param(const param_type& __param)
1756 { _M_param = __param; }
1757
1758 /**
1759 * @brief Returns the inclusive lower bound of the distribution range.
1760 */
1761 result_type
1762 min() const
1763 { return this->a(); }
1764
1765 /**
1766 * @brief Returns the inclusive upper bound of the distribution range.
1767 */
1768 result_type
1769 max() const
1770 { return this->b(); }
1771
1772 /**
1773 * @brief Generating functions.
1774 */
1775 template<typename _UniformRandomNumberGenerator>
1776 result_type
1777 operator()(_UniformRandomNumberGenerator& __urng)
1778 { return this->operator()(__urng, this->param()); }
1779
1780 template<typename _UniformRandomNumberGenerator>
1781 result_type
1782 operator()(_UniformRandomNumberGenerator& __urng,
1783 const param_type& __p);
1784
1785 param_type _M_param;
1786 };
1787
1788 /**
1789 * @brief Return true if two uniform integer distributions have
1790 * the same parameters.
1791 */
1792 template<typename _IntType>
1793 inline bool
1794 operator==(const std::uniform_int_distribution<_IntType>& __d1,
1795 const std::uniform_int_distribution<_IntType>& __d2)
1796 { return __d1.param() == __d2.param(); }
1797
1798 /**
1799 * @brief Return true if two uniform integer distributions have
1800 * different parameters.
1801 */
1802 template<typename _IntType>
1803 inline bool
1804 operator!=(const std::uniform_int_distribution<_IntType>& __d1,
1805 const std::uniform_int_distribution<_IntType>& __d2)
1806 { return !(__d1 == __d2); }
1807
1808 /**
1809 * @brief Inserts a %uniform_int_distribution random number
1810 * distribution @p __x into the output stream @p os.
1811 *
1812 * @param __os An output stream.
1813 * @param __x A %uniform_int_distribution random number distribution.
1814 *
1815 * @returns The output stream with the state of @p __x inserted or in
1816 * an error state.
1817 */
1818 template<typename _IntType, typename _CharT, typename _Traits>
1819 std::basic_ostream<_CharT, _Traits>&
1820 operator<<(std::basic_ostream<_CharT, _Traits>&,
1821 const std::uniform_int_distribution<_IntType>&);
1822
1823 /**
1824 * @brief Extracts a %uniform_int_distribution random number distribution
1825 * @p __x from the input stream @p __is.
1826 *
1827 * @param __is An input stream.
1828 * @param __x A %uniform_int_distribution random number generator engine.
1829 *
1830 * @returns The input stream with @p __x extracted or in an error state.
1831 */
1832 template<typename _IntType, typename _CharT, typename _Traits>
1833 std::basic_istream<_CharT, _Traits>&
1834 operator>>(std::basic_istream<_CharT, _Traits>&,
1835 std::uniform_int_distribution<_IntType>&);
1836
1837
1838 /**
1839 * @brief Uniform continuous distribution for random numbers.
1840 *
1841 * A continuous random distribution on the range [min, max) with equal
1842 * probability throughout the range. The URNG should be real-valued and
1843 * deliver number in the range [0, 1).
1844 */
1845 template<typename _RealType = double>
1846 class uniform_real_distribution
1847 {
1848 static_assert(std::is_floating_point<_RealType>::value,
1849 "template argument not a floating point type");
1850
1851 public:
1852 /** The type of the range of the distribution. */
1853 typedef _RealType result_type;
1854 /** Parameter type. */
1855 struct param_type
1856 {
1857 typedef uniform_real_distribution<_RealType> distribution_type;
1858
1859 explicit
1860 param_type(_RealType __a = _RealType(0),
1861 _RealType __b = _RealType(1))
1862 : _M_a(__a), _M_b(__b)
1863 {
1864 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1865 }
1866
1867 result_type
1868 a() const
1869 { return _M_a; }
1870
1871 result_type
1872 b() const
1873 { return _M_b; }
1874
1875 friend bool
1876 operator==(const param_type& __p1, const param_type& __p2)
1877 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
1878
1879 private:
1880 _RealType _M_a;
1881 _RealType _M_b;
1882 };
1883
1884 public:
1885 /**
1886 * @brief Constructs a uniform_real_distribution object.
1887 *
1888 * @param __a [IN] The lower bound of the distribution.
1889 * @param __b [IN] The upper bound of the distribution.
1890 */
1891 explicit
1892 uniform_real_distribution(_RealType __a = _RealType(0),
1893 _RealType __b = _RealType(1))
1894 : _M_param(__a, __b)
1895 { }
1896
1897 explicit
1898 uniform_real_distribution(const param_type& __p)
1899 : _M_param(__p)
1900 { }
1901
1902 /**
1903 * @brief Resets the distribution state.
1904 *
1905 * Does nothing for the uniform real distribution.
1906 */
1907 void
1908 reset() { }
1909
1910 result_type
1911 a() const
1912 { return _M_param.a(); }
1913
1914 result_type
1915 b() const
1916 { return _M_param.b(); }
1917
1918 /**
1919 * @brief Returns the parameter set of the distribution.
1920 */
1921 param_type
1922 param() const
1923 { return _M_param; }
1924
1925 /**
1926 * @brief Sets the parameter set of the distribution.
1927 * @param __param The new parameter set of the distribution.
1928 */
1929 void
1930 param(const param_type& __param)
1931 { _M_param = __param; }
1932
1933 /**
1934 * @brief Returns the inclusive lower bound of the distribution range.
1935 */
1936 result_type
1937 min() const
1938 { return this->a(); }
1939
1940 /**
1941 * @brief Returns the inclusive upper bound of the distribution range.
1942 */
1943 result_type
1944 max() const
1945 { return this->b(); }
1946
1947 /**
1948 * @brief Generating functions.
1949 */
1950 template<typename _UniformRandomNumberGenerator>
1951 result_type
1952 operator()(_UniformRandomNumberGenerator& __urng)
1953 { return this->operator()(__urng, this->param()); }
1954
1955 template<typename _UniformRandomNumberGenerator>
1956 result_type
1957 operator()(_UniformRandomNumberGenerator& __urng,
1958 const param_type& __p)
1959 {
1960 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1961 __aurng(__urng);
1962 return (__aurng() * (__p.b() - __p.a())) + __p.a();
1963 }
1964
1965 private:
1966 param_type _M_param;
1967 };
1968
1969 /**
1970 * @brief Return true if two uniform real distributions have
1971 * the same parameters.
1972 */
1973 template<typename _IntType>
1974 inline bool
1975 operator==(const std::uniform_real_distribution<_IntType>& __d1,
1976 const std::uniform_real_distribution<_IntType>& __d2)
1977 { return __d1.param() == __d2.param(); }
1978
1979 /**
1980 * @brief Return true if two uniform real distributions have
1981 * different parameters.
1982 */
1983 template<typename _IntType>
1984 inline bool
1985 operator!=(const std::uniform_real_distribution<_IntType>& __d1,
1986 const std::uniform_real_distribution<_IntType>& __d2)
1987 { return !(__d1 == __d2); }
1988
1989 /**
1990 * @brief Inserts a %uniform_real_distribution random number
1991 * distribution @p __x into the output stream @p __os.
1992 *
1993 * @param __os An output stream.
1994 * @param __x A %uniform_real_distribution random number distribution.
1995 *
1996 * @returns The output stream with the state of @p __x inserted or in
1997 * an error state.
1998 */
1999 template<typename _RealType, typename _CharT, typename _Traits>
2000 std::basic_ostream<_CharT, _Traits>&
2001 operator<<(std::basic_ostream<_CharT, _Traits>&,
2002 const std::uniform_real_distribution<_RealType>&);
2003
2004 /**
2005 * @brief Extracts a %uniform_real_distribution random number distribution
2006 * @p __x from the input stream @p __is.
2007 *
2008 * @param __is An input stream.
2009 * @param __x A %uniform_real_distribution random number generator engine.
2010 *
2011 * @returns The input stream with @p __x extracted or in an error state.
2012 */
2013 template<typename _RealType, typename _CharT, typename _Traits>
2014 std::basic_istream<_CharT, _Traits>&
2015 operator>>(std::basic_istream<_CharT, _Traits>&,
2016 std::uniform_real_distribution<_RealType>&);
2017
2018 /* @} */ // group random_distributions_uniform
2019
2020 /**
2021 * @addtogroup random_distributions_normal Normal Distributions
2022 * @ingroup random_distributions
2023 * @{
2024 */
2025
2026 /**
2027 * @brief A normal continuous distribution for random numbers.
2028 *
2029 * The formula for the normal probability density function is
2030 * @f[
2031 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
2032 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
2033 * @f]
2034 */
2035 template<typename _RealType = double>
2036 class normal_distribution
2037 {
2038 static_assert(std::is_floating_point<_RealType>::value,
2039 "template argument not a floating point type");
2040
2041 public:
2042 /** The type of the range of the distribution. */
2043 typedef _RealType result_type;
2044 /** Parameter type. */
2045 struct param_type
2046 {
2047 typedef normal_distribution<_RealType> distribution_type;
2048
2049 explicit
2050 param_type(_RealType __mean = _RealType(0),
2051 _RealType __stddev = _RealType(1))
2052 : _M_mean(__mean), _M_stddev(__stddev)
2053 {
2054 _GLIBCXX_DEBUG_ASSERT(_M_stddev > _RealType(0));
2055 }
2056
2057 _RealType
2058 mean() const
2059 { return _M_mean; }
2060
2061 _RealType
2062 stddev() const
2063 { return _M_stddev; }
2064
2065 friend bool
2066 operator==(const param_type& __p1, const param_type& __p2)
2067 { return (__p1._M_mean == __p2._M_mean
2068 && __p1._M_stddev == __p2._M_stddev); }
2069
2070 private:
2071 _RealType _M_mean;
2072 _RealType _M_stddev;
2073 };
2074
2075 public:
2076 /**
2077 * Constructs a normal distribution with parameters @f$mean@f$ and
2078 * standard deviation.
2079 */
2080 explicit
2081 normal_distribution(result_type __mean = result_type(0),
2082 result_type __stddev = result_type(1))
2083 : _M_param(__mean, __stddev), _M_saved_available(false)
2084 { }
2085
2086 explicit
2087 normal_distribution(const param_type& __p)
2088 : _M_param(__p), _M_saved_available(false)
2089 { }
2090
2091 /**
2092 * @brief Resets the distribution state.
2093 */
2094 void
2095 reset()
2096 { _M_saved_available = false; }
2097
2098 /**
2099 * @brief Returns the mean of the distribution.
2100 */
2101 _RealType
2102 mean() const
2103 { return _M_param.mean(); }
2104
2105 /**
2106 * @brief Returns the standard deviation of the distribution.
2107 */
2108 _RealType
2109 stddev() const
2110 { return _M_param.stddev(); }
2111
2112 /**
2113 * @brief Returns the parameter set of the distribution.
2114 */
2115 param_type
2116 param() const
2117 { return _M_param; }
2118
2119 /**
2120 * @brief Sets the parameter set of the distribution.
2121 * @param __param The new parameter set of the distribution.
2122 */
2123 void
2124 param(const param_type& __param)
2125 { _M_param = __param; }
2126
2127 /**
2128 * @brief Returns the greatest lower bound value of the distribution.
2129 */
2130 result_type
2131 min() const
2132 { return std::numeric_limits<result_type>::min(); }
2133
2134 /**
2135 * @brief Returns the least upper bound value of the distribution.
2136 */
2137 result_type
2138 max() const
2139 { return std::numeric_limits<result_type>::max(); }
2140
2141 /**
2142 * @brief Generating functions.
2143 */
2144 template<typename _UniformRandomNumberGenerator>
2145 result_type
2146 operator()(_UniformRandomNumberGenerator& __urng)
2147 { return this->operator()(__urng, this->param()); }
2148
2149 template<typename _UniformRandomNumberGenerator>
2150 result_type
2151 operator()(_UniformRandomNumberGenerator& __urng,
2152 const param_type& __p);
2153
2154 /**
2155 * @brief Return true if two normal distributions have
2156 * the same parameters and the sequences that would
2157 * be generated are equal.
2158 */
2159 template<typename _RealType1>
2160 friend bool
2161 operator==(const std::normal_distribution<_RealType1>& __d1,
2162 const std::normal_distribution<_RealType1>& __d2);
2163
2164 /**
2165 * @brief Inserts a %normal_distribution random number distribution
2166 * @p __x into the output stream @p __os.
2167 *
2168 * @param __os An output stream.
2169 * @param __x A %normal_distribution random number distribution.
2170 *
2171 * @returns The output stream with the state of @p __x inserted or in
2172 * an error state.
2173 */
2174 template<typename _RealType1, typename _CharT, typename _Traits>
2175 friend std::basic_ostream<_CharT, _Traits>&
2176 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2177 const std::normal_distribution<_RealType1>& __x);
2178
2179 /**
2180 * @brief Extracts a %normal_distribution random number distribution
2181 * @p __x from the input stream @p __is.
2182 *
2183 * @param __is An input stream.
2184 * @param __x A %normal_distribution random number generator engine.
2185 *
2186 * @returns The input stream with @p __x extracted or in an error
2187 * state.
2188 */
2189 template<typename _RealType1, typename _CharT, typename _Traits>
2190 friend std::basic_istream<_CharT, _Traits>&
2191 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2192 std::normal_distribution<_RealType1>& __x);
2193
2194 private:
2195 param_type _M_param;
2196 result_type _M_saved;
2197 bool _M_saved_available;
2198 };
2199
2200 /**
2201 * @brief Return true if two normal distributions are different.
2202 */
2203 template<typename _RealType>
2204 inline bool
2205 operator!=(const std::normal_distribution<_RealType>& __d1,
2206 const std::normal_distribution<_RealType>& __d2)
2207 { return !(__d1 == __d2); }
2208
2209
2210 /**
2211 * @brief A lognormal_distribution random number distribution.
2212 *
2213 * The formula for the normal probability mass function is
2214 * @f[
2215 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2216 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2217 * @f]
2218 */
2219 template<typename _RealType = double>
2220 class lognormal_distribution
2221 {
2222 static_assert(std::is_floating_point<_RealType>::value,
2223 "template argument not a floating point type");
2224
2225 public:
2226 /** The type of the range of the distribution. */
2227 typedef _RealType result_type;
2228 /** Parameter type. */
2229 struct param_type
2230 {
2231 typedef lognormal_distribution<_RealType> distribution_type;
2232
2233 explicit
2234 param_type(_RealType __m = _RealType(0),
2235 _RealType __s = _RealType(1))
2236 : _M_m(__m), _M_s(__s)
2237 { }
2238
2239 _RealType
2240 m() const
2241 { return _M_m; }
2242
2243 _RealType
2244 s() const
2245 { return _M_s; }
2246
2247 friend bool
2248 operator==(const param_type& __p1, const param_type& __p2)
2249 { return __p1._M_m == __p2._M_m && __p1._M_s == __p2._M_s; }
2250
2251 private:
2252 _RealType _M_m;
2253 _RealType _M_s;
2254 };
2255
2256 explicit
2257 lognormal_distribution(_RealType __m = _RealType(0),
2258 _RealType __s = _RealType(1))
2259 : _M_param(__m, __s), _M_nd()
2260 { }
2261
2262 explicit
2263 lognormal_distribution(const param_type& __p)
2264 : _M_param(__p), _M_nd()
2265 { }
2266
2267 /**
2268 * Resets the distribution state.
2269 */
2270 void
2271 reset()
2272 { _M_nd.reset(); }
2273
2274 /**
2275 *
2276 */
2277 _RealType
2278 m() const
2279 { return _M_param.m(); }
2280
2281 _RealType
2282 s() const
2283 { return _M_param.s(); }
2284
2285 /**
2286 * @brief Returns the parameter set of the distribution.
2287 */
2288 param_type
2289 param() const
2290 { return _M_param; }
2291
2292 /**
2293 * @brief Sets the parameter set of the distribution.
2294 * @param __param The new parameter set of the distribution.
2295 */
2296 void
2297 param(const param_type& __param)
2298 { _M_param = __param; }
2299
2300 /**
2301 * @brief Returns the greatest lower bound value of the distribution.
2302 */
2303 result_type
2304 min() const
2305 { return result_type(0); }
2306
2307 /**
2308 * @brief Returns the least upper bound value of the distribution.
2309 */
2310 result_type
2311 max() const
2312 { return std::numeric_limits<result_type>::max(); }
2313
2314 /**
2315 * @brief Generating functions.
2316 */
2317 template<typename _UniformRandomNumberGenerator>
2318 result_type
2319 operator()(_UniformRandomNumberGenerator& __urng)
2320 { return this->operator()(__urng, this->param()); }
2321
2322 template<typename _UniformRandomNumberGenerator>
2323 result_type
2324 operator()(_UniformRandomNumberGenerator& __urng,
2325 const param_type& __p)
2326 { return std::exp(__p.s() * _M_nd(__urng) + __p.m()); }
2327
2328 /**
2329 * @brief Return true if two lognormal distributions have
2330 * the same parameters and the sequences that would
2331 * be generated are equal.
2332 */
2333 friend bool
2334 operator==(const lognormal_distribution& __d1,
2335 const lognormal_distribution& __d2)
2336 { return (__d1.param() == __d2.param()
2337 && __d1._M_nd == __d2._M_nd); }
2338
2339 /**
2340 * @brief Inserts a %lognormal_distribution random number distribution
2341 * @p __x into the output stream @p __os.
2342 *
2343 * @param __os An output stream.
2344 * @param __x A %lognormal_distribution random number distribution.
2345 *
2346 * @returns The output stream with the state of @p __x inserted or in
2347 * an error state.
2348 */
2349 template<typename _RealType1, typename _CharT, typename _Traits>
2350 friend std::basic_ostream<_CharT, _Traits>&
2351 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2352 const std::lognormal_distribution<_RealType1>& __x);
2353
2354 /**
2355 * @brief Extracts a %lognormal_distribution random number distribution
2356 * @p __x from the input stream @p __is.
2357 *
2358 * @param __is An input stream.
2359 * @param __x A %lognormal_distribution random number
2360 * generator engine.
2361 *
2362 * @returns The input stream with @p __x extracted or in an error state.
2363 */
2364 template<typename _RealType1, typename _CharT, typename _Traits>
2365 friend std::basic_istream<_CharT, _Traits>&
2366 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2367 std::lognormal_distribution<_RealType1>& __x);
2368
2369 private:
2370 param_type _M_param;
2371
2372 std::normal_distribution<result_type> _M_nd;
2373 };
2374
2375 /**
2376 * @brief Return true if two lognormal distributions are different.
2377 */
2378 template<typename _RealType>
2379 inline bool
2380 operator!=(const std::lognormal_distribution<_RealType>& __d1,
2381 const std::lognormal_distribution<_RealType>& __d2)
2382 { return !(__d1 == __d2); }
2383
2384
2385 /**
2386 * @brief A gamma continuous distribution for random numbers.
2387 *
2388 * The formula for the gamma probability density function is:
2389 * @f[
2390 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2391 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2392 * @f]
2393 */
2394 template<typename _RealType = double>
2395 class gamma_distribution
2396 {
2397 static_assert(std::is_floating_point<_RealType>::value,
2398 "template argument not a floating point type");
2399
2400 public:
2401 /** The type of the range of the distribution. */
2402 typedef _RealType result_type;
2403 /** Parameter type. */
2404 struct param_type
2405 {
2406 typedef gamma_distribution<_RealType> distribution_type;
2407 friend class gamma_distribution<_RealType>;
2408
2409 explicit
2410 param_type(_RealType __alpha_val = _RealType(1),
2411 _RealType __beta_val = _RealType(1))
2412 : _M_alpha(__alpha_val), _M_beta(__beta_val)
2413 {
2414 _GLIBCXX_DEBUG_ASSERT(_M_alpha > _RealType(0));
2415 _M_initialize();
2416 }
2417
2418 _RealType
2419 alpha() const
2420 { return _M_alpha; }
2421
2422 _RealType
2423 beta() const
2424 { return _M_beta; }
2425
2426 friend bool
2427 operator==(const param_type& __p1, const param_type& __p2)
2428 { return (__p1._M_alpha == __p2._M_alpha
2429 && __p1._M_beta == __p2._M_beta); }
2430
2431 private:
2432 void
2433 _M_initialize();
2434
2435 _RealType _M_alpha;
2436 _RealType _M_beta;
2437
2438 _RealType _M_malpha, _M_a2;
2439 };
2440
2441 public:
2442 /**
2443 * @brief Constructs a gamma distribution with parameters
2444 * @f$\alpha@f$ and @f$\beta@f$.
2445 */
2446 explicit
2447 gamma_distribution(_RealType __alpha_val = _RealType(1),
2448 _RealType __beta_val = _RealType(1))
2449 : _M_param(__alpha_val, __beta_val), _M_nd()
2450 { }
2451
2452 explicit
2453 gamma_distribution(const param_type& __p)
2454 : _M_param(__p), _M_nd()
2455 { }
2456
2457 /**
2458 * @brief Resets the distribution state.
2459 */
2460 void
2461 reset()
2462 { _M_nd.reset(); }
2463
2464 /**
2465 * @brief Returns the @f$\alpha@f$ of the distribution.
2466 */
2467 _RealType
2468 alpha() const
2469 { return _M_param.alpha(); }
2470
2471 /**
2472 * @brief Returns the @f$\beta@f$ of the distribution.
2473 */
2474 _RealType
2475 beta() const
2476 { return _M_param.beta(); }
2477
2478 /**
2479 * @brief Returns the parameter set of the distribution.
2480 */
2481 param_type
2482 param() const
2483 { return _M_param; }
2484
2485 /**
2486 * @brief Sets the parameter set of the distribution.
2487 * @param __param The new parameter set of the distribution.
2488 */
2489 void
2490 param(const param_type& __param)
2491 { _M_param = __param; }
2492
2493 /**
2494 * @brief Returns the greatest lower bound value of the distribution.
2495 */
2496 result_type
2497 min() const
2498 { return result_type(0); }
2499
2500 /**
2501 * @brief Returns the least upper bound value of the distribution.
2502 */
2503 result_type
2504 max() const
2505 { return std::numeric_limits<result_type>::max(); }
2506
2507 /**
2508 * @brief Generating functions.
2509 */
2510 template<typename _UniformRandomNumberGenerator>
2511 result_type
2512 operator()(_UniformRandomNumberGenerator& __urng)
2513 { return this->operator()(__urng, this->param()); }
2514
2515 template<typename _UniformRandomNumberGenerator>
2516 result_type
2517 operator()(_UniformRandomNumberGenerator& __urng,
2518 const param_type& __p);
2519
2520 /**
2521 * @brief Return true if two gamma distributions have the same
2522 * parameters and the sequences that would be generated
2523 * are equal.
2524 */
2525 friend bool
2526 operator==(const gamma_distribution& __d1,
2527 const gamma_distribution& __d2)
2528 { return (__d1.param() == __d2.param()
2529 && __d1._M_nd == __d2._M_nd); }
2530
2531 /**
2532 * @brief Inserts a %gamma_distribution random number distribution
2533 * @p __x into the output stream @p __os.
2534 *
2535 * @param __os An output stream.
2536 * @param __x A %gamma_distribution random number distribution.
2537 *
2538 * @returns The output stream with the state of @p __x inserted or in
2539 * an error state.
2540 */
2541 template<typename _RealType1, typename _CharT, typename _Traits>
2542 friend std::basic_ostream<_CharT, _Traits>&
2543 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2544 const std::gamma_distribution<_RealType1>& __x);
2545
2546 /**
2547 * @brief Extracts a %gamma_distribution random number distribution
2548 * @p __x from the input stream @p __is.
2549 *
2550 * @param __is An input stream.
2551 * @param __x A %gamma_distribution random number generator engine.
2552 *
2553 * @returns The input stream with @p __x extracted or in an error state.
2554 */
2555 template<typename _RealType1, typename _CharT, typename _Traits>
2556 friend std::basic_istream<_CharT, _Traits>&
2557 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2558 std::gamma_distribution<_RealType1>& __x);
2559
2560 private:
2561 param_type _M_param;
2562
2563 std::normal_distribution<result_type> _M_nd;
2564 };
2565
2566 /**
2567 * @brief Return true if two gamma distributions are different.
2568 */
2569 template<typename _RealType>
2570 inline bool
2571 operator!=(const std::gamma_distribution<_RealType>& __d1,
2572 const std::gamma_distribution<_RealType>& __d2)
2573 { return !(__d1 == __d2); }
2574
2575
2576 /**
2577 * @brief A chi_squared_distribution random number distribution.
2578 *
2579 * The formula for the normal probability mass function is
2580 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2581 */
2582 template<typename _RealType = double>
2583 class chi_squared_distribution
2584 {
2585 static_assert(std::is_floating_point<_RealType>::value,
2586 "template argument not a floating point type");
2587
2588 public:
2589 /** The type of the range of the distribution. */
2590 typedef _RealType result_type;
2591 /** Parameter type. */
2592 struct param_type
2593 {
2594 typedef chi_squared_distribution<_RealType> distribution_type;
2595
2596 explicit
2597 param_type(_RealType __n = _RealType(1))
2598 : _M_n(__n)
2599 { }
2600
2601 _RealType
2602 n() const
2603 { return _M_n; }
2604
2605 friend bool
2606 operator==(const param_type& __p1, const param_type& __p2)
2607 { return __p1._M_n == __p2._M_n; }
2608
2609 private:
2610 _RealType _M_n;
2611 };
2612
2613 explicit
2614 chi_squared_distribution(_RealType __n = _RealType(1))
2615 : _M_param(__n), _M_gd(__n / 2)
2616 { }
2617
2618 explicit
2619 chi_squared_distribution(const param_type& __p)
2620 : _M_param(__p), _M_gd(__p.n() / 2)
2621 { }
2622
2623 /**
2624 * @brief Resets the distribution state.
2625 */
2626 void
2627 reset()
2628 { _M_gd.reset(); }
2629
2630 /**
2631 *
2632 */
2633 _RealType
2634 n() const
2635 { return _M_param.n(); }
2636
2637 /**
2638 * @brief Returns the parameter set of the distribution.
2639 */
2640 param_type
2641 param() const
2642 { return _M_param; }
2643
2644 /**
2645 * @brief Sets the parameter set of the distribution.
2646 * @param __param The new parameter set of the distribution.
2647 */
2648 void
2649 param(const param_type& __param)
2650 { _M_param = __param; }
2651
2652 /**
2653 * @brief Returns the greatest lower bound value of the distribution.
2654 */
2655 result_type
2656 min() const
2657 { return result_type(0); }
2658
2659 /**
2660 * @brief Returns the least upper bound value of the distribution.
2661 */
2662 result_type
2663 max() const
2664 { return std::numeric_limits<result_type>::max(); }
2665
2666 /**
2667 * @brief Generating functions.
2668 */
2669 template<typename _UniformRandomNumberGenerator>
2670 result_type
2671 operator()(_UniformRandomNumberGenerator& __urng)
2672 { return 2 * _M_gd(__urng); }
2673
2674 template<typename _UniformRandomNumberGenerator>
2675 result_type
2676 operator()(_UniformRandomNumberGenerator& __urng,
2677 const param_type& __p)
2678 {
2679 typedef typename std::gamma_distribution<result_type>::param_type
2680 param_type;
2681 return 2 * _M_gd(__urng, param_type(__p.n() / 2));
2682 }
2683
2684 /**
2685 * @brief Return true if two Chi-squared distributions have
2686 * the same parameters and the sequences that would be
2687 * generated are equal.
2688 */
2689 friend bool
2690 operator==(const chi_squared_distribution& __d1,
2691 const chi_squared_distribution& __d2)
2692 { return __d1.param() == __d2.param() && __d1._M_gd == __d2._M_gd; }
2693
2694 /**
2695 * @brief Inserts a %chi_squared_distribution random number distribution
2696 * @p __x into the output stream @p __os.
2697 *
2698 * @param __os An output stream.
2699 * @param __x A %chi_squared_distribution random number distribution.
2700 *
2701 * @returns The output stream with the state of @p __x inserted or in
2702 * an error state.
2703 */
2704 template<typename _RealType1, typename _CharT, typename _Traits>
2705 friend std::basic_ostream<_CharT, _Traits>&
2706 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2707 const std::chi_squared_distribution<_RealType1>& __x);
2708
2709 /**
2710 * @brief Extracts a %chi_squared_distribution random number distribution
2711 * @p __x from the input stream @p __is.
2712 *
2713 * @param __is An input stream.
2714 * @param __x A %chi_squared_distribution random number
2715 * generator engine.
2716 *
2717 * @returns The input stream with @p __x extracted or in an error state.
2718 */
2719 template<typename _RealType1, typename _CharT, typename _Traits>
2720 friend std::basic_istream<_CharT, _Traits>&
2721 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2722 std::chi_squared_distribution<_RealType1>& __x);
2723
2724 private:
2725 param_type _M_param;
2726
2727 std::gamma_distribution<result_type> _M_gd;
2728 };
2729
2730 /**
2731 * @brief Return true if two Chi-squared distributions are different.
2732 */
2733 template<typename _RealType>
2734 inline bool
2735 operator!=(const std::chi_squared_distribution<_RealType>& __d1,
2736 const std::chi_squared_distribution<_RealType>& __d2)
2737 { return !(__d1 == __d2); }
2738
2739
2740 /**
2741 * @brief A cauchy_distribution random number distribution.
2742 *
2743 * The formula for the normal probability mass function is
2744 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2745 */
2746 template<typename _RealType = double>
2747 class cauchy_distribution
2748 {
2749 static_assert(std::is_floating_point<_RealType>::value,
2750 "template argument not a floating point type");
2751
2752 public:
2753 /** The type of the range of the distribution. */
2754 typedef _RealType result_type;
2755 /** Parameter type. */
2756 struct param_type
2757 {
2758 typedef cauchy_distribution<_RealType> distribution_type;
2759
2760 explicit
2761 param_type(_RealType __a = _RealType(0),
2762 _RealType __b = _RealType(1))
2763 : _M_a(__a), _M_b(__b)
2764 { }
2765
2766 _RealType
2767 a() const
2768 { return _M_a; }
2769
2770 _RealType
2771 b() const
2772 { return _M_b; }
2773
2774 friend bool
2775 operator==(const param_type& __p1, const param_type& __p2)
2776 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
2777
2778 private:
2779 _RealType _M_a;
2780 _RealType _M_b;
2781 };
2782
2783 explicit
2784 cauchy_distribution(_RealType __a = _RealType(0),
2785 _RealType __b = _RealType(1))
2786 : _M_param(__a, __b)
2787 { }
2788
2789 explicit
2790 cauchy_distribution(const param_type& __p)
2791 : _M_param(__p)
2792 { }
2793
2794 /**
2795 * @brief Resets the distribution state.
2796 */
2797 void
2798 reset()
2799 { }
2800
2801 /**
2802 *
2803 */
2804 _RealType
2805 a() const
2806 { return _M_param.a(); }
2807
2808 _RealType
2809 b() const
2810 { return _M_param.b(); }
2811
2812 /**
2813 * @brief Returns the parameter set of the distribution.
2814 */
2815 param_type
2816 param() const
2817 { return _M_param; }
2818
2819 /**
2820 * @brief Sets the parameter set of the distribution.
2821 * @param __param The new parameter set of the distribution.
2822 */
2823 void
2824 param(const param_type& __param)
2825 { _M_param = __param; }
2826
2827 /**
2828 * @brief Returns the greatest lower bound value of the distribution.
2829 */
2830 result_type
2831 min() const
2832 { return std::numeric_limits<result_type>::min(); }
2833
2834 /**
2835 * @brief Returns the least upper bound value of the distribution.
2836 */
2837 result_type
2838 max() const
2839 { return std::numeric_limits<result_type>::max(); }
2840
2841 /**
2842 * @brief Generating functions.
2843 */
2844 template<typename _UniformRandomNumberGenerator>
2845 result_type
2846 operator()(_UniformRandomNumberGenerator& __urng)
2847 { return this->operator()(__urng, this->param()); }
2848
2849 template<typename _UniformRandomNumberGenerator>
2850 result_type
2851 operator()(_UniformRandomNumberGenerator& __urng,
2852 const param_type& __p);
2853
2854 private:
2855 param_type _M_param;
2856 };
2857
2858 /**
2859 * @brief Return true if two Cauchy distributions have
2860 * the same parameters.
2861 */
2862 template<typename _RealType>
2863 inline bool
2864 operator==(const std::cauchy_distribution<_RealType>& __d1,
2865 const std::cauchy_distribution<_RealType>& __d2)
2866 { return __d1.param() == __d2.param(); }
2867
2868 /**
2869 * @brief Return true if two Cauchy distributions have
2870 * different parameters.
2871 */
2872 template<typename _RealType>
2873 inline bool
2874 operator!=(const std::cauchy_distribution<_RealType>& __d1,
2875 const std::cauchy_distribution<_RealType>& __d2)
2876 { return !(__d1 == __d2); }
2877
2878 /**
2879 * @brief Inserts a %cauchy_distribution random number distribution
2880 * @p __x into the output stream @p __os.
2881 *
2882 * @param __os An output stream.
2883 * @param __x A %cauchy_distribution random number distribution.
2884 *
2885 * @returns The output stream with the state of @p __x inserted or in
2886 * an error state.
2887 */
2888 template<typename _RealType, typename _CharT, typename _Traits>
2889 std::basic_ostream<_CharT, _Traits>&
2890 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2891 const std::cauchy_distribution<_RealType>& __x);
2892
2893 /**
2894 * @brief Extracts a %cauchy_distribution random number distribution
2895 * @p __x from the input stream @p __is.
2896 *
2897 * @param __is An input stream.
2898 * @param __x A %cauchy_distribution random number
2899 * generator engine.
2900 *
2901 * @returns The input stream with @p __x extracted or in an error state.
2902 */
2903 template<typename _RealType, typename _CharT, typename _Traits>
2904 std::basic_istream<_CharT, _Traits>&
2905 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2906 std::cauchy_distribution<_RealType>& __x);
2907
2908
2909 /**
2910 * @brief A fisher_f_distribution random number distribution.
2911 *
2912 * The formula for the normal probability mass function is
2913 * @f[
2914 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2915 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2916 * (1 + \frac{mx}{n})^{-(m+n)/2}
2917 * @f]
2918 */
2919 template<typename _RealType = double>
2920 class fisher_f_distribution
2921 {
2922 static_assert(std::is_floating_point<_RealType>::value,
2923 "template argument not a floating point type");
2924
2925 public:
2926 /** The type of the range of the distribution. */
2927 typedef _RealType result_type;
2928 /** Parameter type. */
2929 struct param_type
2930 {
2931 typedef fisher_f_distribution<_RealType> distribution_type;
2932
2933 explicit
2934 param_type(_RealType __m = _RealType(1),
2935 _RealType __n = _RealType(1))
2936 : _M_m(__m), _M_n(__n)
2937 { }
2938
2939 _RealType
2940 m() const
2941 { return _M_m; }
2942
2943 _RealType
2944 n() const
2945 { return _M_n; }
2946
2947 friend bool
2948 operator==(const param_type& __p1, const param_type& __p2)
2949 { return __p1._M_m == __p2._M_m && __p1._M_n == __p2._M_n; }
2950
2951 private:
2952 _RealType _M_m;
2953 _RealType _M_n;
2954 };
2955
2956 explicit
2957 fisher_f_distribution(_RealType __m = _RealType(1),
2958 _RealType __n = _RealType(1))
2959 : _M_param(__m, __n), _M_gd_x(__m / 2), _M_gd_y(__n / 2)
2960 { }
2961
2962 explicit
2963 fisher_f_distribution(const param_type& __p)
2964 : _M_param(__p), _M_gd_x(__p.m() / 2), _M_gd_y(__p.n() / 2)
2965 { }
2966
2967 /**
2968 * @brief Resets the distribution state.
2969 */
2970 void
2971 reset()
2972 {
2973 _M_gd_x.reset();
2974 _M_gd_y.reset();
2975 }
2976
2977 /**
2978 *
2979 */
2980 _RealType
2981 m() const
2982 { return _M_param.m(); }
2983
2984 _RealType
2985 n() const
2986 { return _M_param.n(); }
2987
2988 /**
2989 * @brief Returns the parameter set of the distribution.
2990 */
2991 param_type
2992 param() const
2993 { return _M_param; }
2994
2995 /**
2996 * @brief Sets the parameter set of the distribution.
2997 * @param __param The new parameter set of the distribution.
2998 */
2999 void
3000 param(const param_type& __param)
3001 { _M_param = __param; }
3002
3003 /**
3004 * @brief Returns the greatest lower bound value of the distribution.
3005 */
3006 result_type
3007 min() const
3008 { return result_type(0); }
3009
3010 /**
3011 * @brief Returns the least upper bound value of the distribution.
3012 */
3013 result_type
3014 max() const
3015 { return std::numeric_limits<result_type>::max(); }
3016
3017 /**
3018 * @brief Generating functions.
3019 */
3020 template<typename _UniformRandomNumberGenerator>
3021 result_type
3022 operator()(_UniformRandomNumberGenerator& __urng)
3023 { return (_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()); }
3024
3025 template<typename _UniformRandomNumberGenerator>
3026 result_type
3027 operator()(_UniformRandomNumberGenerator& __urng,
3028 const param_type& __p)
3029 {
3030 typedef typename std::gamma_distribution<result_type>::param_type
3031 param_type;
3032 return ((_M_gd_x(__urng, param_type(__p.m() / 2)) * n())
3033 / (_M_gd_y(__urng, param_type(__p.n() / 2)) * m()));
3034 }
3035
3036 /**
3037 * @brief Return true if two Fisher f distributions have
3038 * the same parameters and the sequences that would
3039 * be generated are equal.
3040 */
3041 friend bool
3042 operator==(const fisher_f_distribution& __d1,
3043 const fisher_f_distribution& __d2)
3044 { return (__d1.param() == __d2.param()
3045 && __d1._M_gd_x == __d2._M_gd_x
3046 && __d1._M_gd_y == __d2._M_gd_y); }
3047
3048 /**
3049 * @brief Inserts a %fisher_f_distribution random number distribution
3050 * @p __x into the output stream @p __os.
3051 *
3052 * @param __os An output stream.
3053 * @param __x A %fisher_f_distribution random number distribution.
3054 *
3055 * @returns The output stream with the state of @p __x inserted or in
3056 * an error state.
3057 */
3058 template<typename _RealType1, typename _CharT, typename _Traits>
3059 friend std::basic_ostream<_CharT, _Traits>&
3060 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3061 const std::fisher_f_distribution<_RealType1>& __x);
3062
3063 /**
3064 * @brief Extracts a %fisher_f_distribution random number distribution
3065 * @p __x from the input stream @p __is.
3066 *
3067 * @param __is An input stream.
3068 * @param __x A %fisher_f_distribution random number
3069 * generator engine.
3070 *
3071 * @returns The input stream with @p __x extracted or in an error state.
3072 */
3073 template<typename _RealType1, typename _CharT, typename _Traits>
3074 friend std::basic_istream<_CharT, _Traits>&
3075 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3076 std::fisher_f_distribution<_RealType1>& __x);
3077
3078 private:
3079 param_type _M_param;
3080
3081 std::gamma_distribution<result_type> _M_gd_x, _M_gd_y;
3082 };
3083
3084 /**
3085 * @brief Return true if two Fisher f distributions are diferent.
3086 */
3087 template<typename _RealType>
3088 inline bool
3089 operator!=(const std::fisher_f_distribution<_RealType>& __d1,
3090 const std::fisher_f_distribution<_RealType>& __d2)
3091 { return !(__d1 == __d2); }
3092
3093 /**
3094 * @brief A student_t_distribution random number distribution.
3095 *
3096 * The formula for the normal probability mass function is:
3097 * @f[
3098 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3099 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3100 * @f]
3101 */
3102 template<typename _RealType = double>
3103 class student_t_distribution
3104 {
3105 static_assert(std::is_floating_point<_RealType>::value,
3106 "template argument not a floating point type");
3107
3108 public:
3109 /** The type of the range of the distribution. */
3110 typedef _RealType result_type;
3111 /** Parameter type. */
3112 struct param_type
3113 {
3114 typedef student_t_distribution<_RealType> distribution_type;
3115
3116 explicit
3117 param_type(_RealType __n = _RealType(1))
3118 : _M_n(__n)
3119 { }
3120
3121 _RealType
3122 n() const
3123 { return _M_n; }
3124
3125 friend bool
3126 operator==(const param_type& __p1, const param_type& __p2)
3127 { return __p1._M_n == __p2._M_n; }
3128
3129 private:
3130 _RealType _M_n;
3131 };
3132
3133 explicit
3134 student_t_distribution(_RealType __n = _RealType(1))
3135 : _M_param(__n), _M_nd(), _M_gd(__n / 2, 2)
3136 { }
3137
3138 explicit
3139 student_t_distribution(const param_type& __p)
3140 : _M_param(__p), _M_nd(), _M_gd(__p.n() / 2, 2)
3141 { }
3142
3143 /**
3144 * @brief Resets the distribution state.
3145 */
3146 void
3147 reset()
3148 {
3149 _M_nd.reset();
3150 _M_gd.reset();
3151 }
3152
3153 /**
3154 *
3155 */
3156 _RealType
3157 n() const
3158 { return _M_param.n(); }
3159
3160 /**
3161 * @brief Returns the parameter set of the distribution.
3162 */
3163 param_type
3164 param() const
3165 { return _M_param; }
3166
3167 /**
3168 * @brief Sets the parameter set of the distribution.
3169 * @param __param The new parameter set of the distribution.
3170 */
3171 void
3172 param(const param_type& __param)
3173 { _M_param = __param; }
3174
3175 /**
3176 * @brief Returns the greatest lower bound value of the distribution.
3177 */
3178 result_type
3179 min() const
3180 { return std::numeric_limits<result_type>::min(); }
3181
3182 /**
3183 * @brief Returns the least upper bound value of the distribution.
3184 */
3185 result_type
3186 max() const
3187 { return std::numeric_limits<result_type>::max(); }
3188
3189 /**
3190 * @brief Generating functions.
3191 */
3192 template<typename _UniformRandomNumberGenerator>
3193 result_type
3194 operator()(_UniformRandomNumberGenerator& __urng)
3195 { return _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng)); }
3196
3197 template<typename _UniformRandomNumberGenerator>
3198 result_type
3199 operator()(_UniformRandomNumberGenerator& __urng,
3200 const param_type& __p)
3201 {
3202 typedef typename std::gamma_distribution<result_type>::param_type
3203 param_type;
3204
3205 const result_type __g = _M_gd(__urng, param_type(__p.n() / 2, 2));
3206 return _M_nd(__urng) * std::sqrt(__p.n() / __g);
3207 }
3208
3209 /**
3210 * @brief Return true if two Student t distributions have
3211 * the same parameters and the sequences that would
3212 * be generated are equal.
3213 */
3214 friend bool
3215 operator==(const student_t_distribution& __d1,
3216 const student_t_distribution& __d2)
3217 { return (__d1.param() == __d2.param()
3218 && __d1._M_nd == __d2._M_nd && __d1._M_gd == __d2._M_gd); }
3219
3220 /**
3221 * @brief Inserts a %student_t_distribution random number distribution
3222 * @p __x into the output stream @p __os.
3223 *
3224 * @param __os An output stream.
3225 * @param __x A %student_t_distribution random number distribution.
3226 *
3227 * @returns The output stream with the state of @p __x inserted or in
3228 * an error state.
3229 */
3230 template<typename _RealType1, typename _CharT, typename _Traits>
3231 friend std::basic_ostream<_CharT, _Traits>&
3232 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3233 const std::student_t_distribution<_RealType1>& __x);
3234
3235 /**
3236 * @brief Extracts a %student_t_distribution random number distribution
3237 * @p __x from the input stream @p __is.
3238 *
3239 * @param __is An input stream.
3240 * @param __x A %student_t_distribution random number
3241 * generator engine.
3242 *
3243 * @returns The input stream with @p __x extracted or in an error state.
3244 */
3245 template<typename _RealType1, typename _CharT, typename _Traits>
3246 friend std::basic_istream<_CharT, _Traits>&
3247 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3248 std::student_t_distribution<_RealType1>& __x);
3249
3250 private:
3251 param_type _M_param;
3252
3253 std::normal_distribution<result_type> _M_nd;
3254 std::gamma_distribution<result_type> _M_gd;
3255 };
3256
3257 /**
3258 * @brief Return true if two Student t distributions are different.
3259 */
3260 template<typename _RealType>
3261 inline bool
3262 operator!=(const std::student_t_distribution<_RealType>& __d1,
3263 const std::student_t_distribution<_RealType>& __d2)
3264 { return !(__d1 == __d2); }
3265
3266
3267 /* @} */ // group random_distributions_normal
3268
3269 /**
3270 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3271 * @ingroup random_distributions
3272 * @{
3273 */
3274
3275 /**
3276 * @brief A Bernoulli random number distribution.
3277 *
3278 * Generates a sequence of true and false values with likelihood @f$p@f$
3279 * that true will come up and @f$(1 - p)@f$ that false will appear.
3280 */
3281 class bernoulli_distribution
3282 {
3283 public:
3284 /** The type of the range of the distribution. */
3285 typedef bool result_type;
3286 /** Parameter type. */
3287 struct param_type
3288 {
3289 typedef bernoulli_distribution distribution_type;
3290
3291 explicit
3292 param_type(double __p = 0.5)
3293 : _M_p(__p)
3294 {
3295 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
3296 }
3297
3298 double
3299 p() const
3300 { return _M_p; }
3301
3302 friend bool
3303 operator==(const param_type& __p1, const param_type& __p2)
3304 { return __p1._M_p == __p2._M_p; }
3305
3306 private:
3307 double _M_p;
3308 };
3309
3310 public:
3311 /**
3312 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3313 *
3314 * @param __p [IN] The likelihood of a true result being returned.
3315 * Must be in the interval @f$[0, 1]@f$.
3316 */
3317 explicit
3318 bernoulli_distribution(double __p = 0.5)
3319 : _M_param(__p)
3320 { }
3321
3322 explicit
3323 bernoulli_distribution(const param_type& __p)
3324 : _M_param(__p)
3325 { }
3326
3327 /**
3328 * @brief Resets the distribution state.
3329 *
3330 * Does nothing for a Bernoulli distribution.
3331 */
3332 void
3333 reset() { }
3334
3335 /**
3336 * @brief Returns the @p p parameter of the distribution.
3337 */
3338 double
3339 p() const
3340 { return _M_param.p(); }
3341
3342 /**
3343 * @brief Returns the parameter set of the distribution.
3344 */
3345 param_type
3346 param() const
3347 { return _M_param; }
3348
3349 /**
3350 * @brief Sets the parameter set of the distribution.
3351 * @param __param The new parameter set of the distribution.
3352 */
3353 void
3354 param(const param_type& __param)
3355 { _M_param = __param; }
3356
3357 /**
3358 * @brief Returns the greatest lower bound value of the distribution.
3359 */
3360 result_type
3361 min() const
3362 { return std::numeric_limits<result_type>::min(); }
3363
3364 /**
3365 * @brief Returns the least upper bound value of the distribution.
3366 */
3367 result_type
3368 max() const
3369 { return std::numeric_limits<result_type>::max(); }
3370
3371 /**
3372 * @brief Generating functions.
3373 */
3374 template<typename _UniformRandomNumberGenerator>
3375 result_type
3376 operator()(_UniformRandomNumberGenerator& __urng)
3377 { return this->operator()(__urng, this->param()); }
3378
3379 template<typename _UniformRandomNumberGenerator>
3380 result_type
3381 operator()(_UniformRandomNumberGenerator& __urng,
3382 const param_type& __p)
3383 {
3384 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3385 __aurng(__urng);
3386 if ((__aurng() - __aurng.min())
3387 < __p.p() * (__aurng.max() - __aurng.min()))
3388 return true;
3389 return false;
3390 }
3391
3392 private:
3393 param_type _M_param;
3394 };
3395
3396 /**
3397 * @brief Return true if two Bernoulli distributions have
3398 * the same parameters.
3399 */
3400 inline bool
3401 operator==(const std::bernoulli_distribution& __d1,
3402 const std::bernoulli_distribution& __d2)
3403 { return __d1.param() == __d2.param(); }
3404
3405 /**
3406 * @brief Return true if two Bernoulli distributions have
3407 * different parameters.
3408 */
3409 inline bool
3410 operator!=(const std::bernoulli_distribution& __d1,
3411 const std::bernoulli_distribution& __d2)
3412 { return !(__d1 == __d2); }
3413
3414 /**
3415 * @brief Inserts a %bernoulli_distribution random number distribution
3416 * @p __x into the output stream @p __os.
3417 *
3418 * @param __os An output stream.
3419 * @param __x A %bernoulli_distribution random number distribution.
3420 *
3421 * @returns The output stream with the state of @p __x inserted or in
3422 * an error state.
3423 */
3424 template<typename _CharT, typename _Traits>
3425 std::basic_ostream<_CharT, _Traits>&
3426 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3427 const std::bernoulli_distribution& __x);
3428
3429 /**
3430 * @brief Extracts a %bernoulli_distribution random number distribution
3431 * @p __x from the input stream @p __is.
3432 *
3433 * @param __is An input stream.
3434 * @param __x A %bernoulli_distribution random number generator engine.
3435 *
3436 * @returns The input stream with @p __x extracted or in an error state.
3437 */
3438 template<typename _CharT, typename _Traits>
3439 std::basic_istream<_CharT, _Traits>&
3440 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3441 std::bernoulli_distribution& __x)
3442 {
3443 double __p;
3444 __is >> __p;
3445 __x.param(bernoulli_distribution::param_type(__p));
3446 return __is;
3447 }
3448
3449
3450 /**
3451 * @brief A discrete binomial random number distribution.
3452 *
3453 * The formula for the binomial probability density function is
3454 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3455 * and @f$p@f$ are the parameters of the distribution.
3456 */
3457 template<typename _IntType = int>
3458 class binomial_distribution
3459 {
3460 static_assert(std::is_integral<_IntType>::value,
3461 "template argument not an integral type");
3462
3463 public:
3464 /** The type of the range of the distribution. */
3465 typedef _IntType result_type;
3466 /** Parameter type. */
3467 struct param_type
3468 {
3469 typedef binomial_distribution<_IntType> distribution_type;
3470 friend class binomial_distribution<_IntType>;
3471
3472 explicit
3473 param_type(_IntType __t = _IntType(1), double __p = 0.5)
3474 : _M_t(__t), _M_p(__p)
3475 {
3476 _GLIBCXX_DEBUG_ASSERT((_M_t >= _IntType(0))
3477 && (_M_p >= 0.0)
3478 && (_M_p <= 1.0));
3479 _M_initialize();
3480 }
3481
3482 _IntType
3483 t() const
3484 { return _M_t; }
3485
3486 double
3487 p() const
3488 { return _M_p; }
3489
3490 friend bool
3491 operator==(const param_type& __p1, const param_type& __p2)
3492 { return __p1._M_t == __p2._M_t && __p1._M_p == __p2._M_p; }
3493
3494 private:
3495 void
3496 _M_initialize();
3497
3498 _IntType _M_t;
3499 double _M_p;
3500
3501 double _M_q;
3502 #if _GLIBCXX_USE_C99_MATH_TR1
3503 double _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
3504 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
3505 #endif
3506 bool _M_easy;
3507 };
3508
3509 // constructors and member function
3510 explicit
3511 binomial_distribution(_IntType __t = _IntType(1),
3512 double __p = 0.5)
3513 : _M_param(__t, __p), _M_nd()
3514 { }
3515
3516 explicit
3517 binomial_distribution(const param_type& __p)
3518 : _M_param(__p), _M_nd()
3519 { }
3520
3521 /**
3522 * @brief Resets the distribution state.
3523 */
3524 void
3525 reset()
3526 { _M_nd.reset(); }
3527
3528 /**
3529 * @brief Returns the distribution @p t parameter.
3530 */
3531 _IntType
3532 t() const
3533 { return _M_param.t(); }
3534
3535 /**
3536 * @brief Returns the distribution @p p parameter.
3537 */
3538 double
3539 p() const
3540 { return _M_param.p(); }
3541
3542 /**
3543 * @brief Returns the parameter set of the distribution.
3544 */
3545 param_type
3546 param() const
3547 { return _M_param; }
3548
3549 /**
3550 * @brief Sets the parameter set of the distribution.
3551 * @param __param The new parameter set of the distribution.
3552 */
3553 void
3554 param(const param_type& __param)
3555 { _M_param = __param; }
3556
3557 /**
3558 * @brief Returns the greatest lower bound value of the distribution.
3559 */
3560 result_type
3561 min() const
3562 { return 0; }
3563
3564 /**
3565 * @brief Returns the least upper bound value of the distribution.
3566 */
3567 result_type
3568 max() const
3569 { return _M_param.t(); }
3570
3571 /**
3572 * @brief Generating functions.
3573 */
3574 template<typename _UniformRandomNumberGenerator>
3575 result_type
3576 operator()(_UniformRandomNumberGenerator& __urng)
3577 { return this->operator()(__urng, this->param()); }
3578
3579 template<typename _UniformRandomNumberGenerator>
3580 result_type
3581 operator()(_UniformRandomNumberGenerator& __urng,
3582 const param_type& __p);
3583
3584 /**
3585 * @brief Return true if two binomial distributions have
3586 * the same parameters and the sequences that would
3587 * be generated are equal.
3588 */
3589 friend bool
3590 operator==(const binomial_distribution& __d1,
3591 const binomial_distribution& __d2)
3592 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3593 { return __d1.param() == __d2.param() && __d1._M_nd == __d2._M_nd; }
3594 #else
3595 { return __d1.param() == __d2.param(); }
3596 #endif
3597
3598 /**
3599 * @brief Inserts a %binomial_distribution random number distribution
3600 * @p __x into the output stream @p __os.
3601 *
3602 * @param __os An output stream.
3603 * @param __x A %binomial_distribution random number distribution.
3604 *
3605 * @returns The output stream with the state of @p __x inserted or in
3606 * an error state.
3607 */
3608 template<typename _IntType1,
3609 typename _CharT, typename _Traits>
3610 friend std::basic_ostream<_CharT, _Traits>&
3611 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3612 const std::binomial_distribution<_IntType1>& __x);
3613
3614 /**
3615 * @brief Extracts a %binomial_distribution random number distribution
3616 * @p __x from the input stream @p __is.
3617 *
3618 * @param __is An input stream.
3619 * @param __x A %binomial_distribution random number generator engine.
3620 *
3621 * @returns The input stream with @p __x extracted or in an error
3622 * state.
3623 */
3624 template<typename _IntType1,
3625 typename _CharT, typename _Traits>
3626 friend std::basic_istream<_CharT, _Traits>&
3627 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3628 std::binomial_distribution<_IntType1>& __x);
3629
3630 private:
3631 template<typename _UniformRandomNumberGenerator>
3632 result_type
3633 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
3634
3635 param_type _M_param;
3636
3637 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3638 std::normal_distribution<double> _M_nd;
3639 };
3640
3641 /**
3642 * @brief Return true if two binomial distributions are different.
3643 */
3644 template<typename _IntType>
3645 inline bool
3646 operator!=(const std::binomial_distribution<_IntType>& __d1,
3647 const std::binomial_distribution<_IntType>& __d2)
3648 { return !(__d1 == __d2); }
3649
3650
3651 /**
3652 * @brief A discrete geometric random number distribution.
3653 *
3654 * The formula for the geometric probability density function is
3655 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
3656 * distribution.
3657 */
3658 template<typename _IntType = int>
3659 class geometric_distribution
3660 {
3661 static_assert(std::is_integral<_IntType>::value,
3662 "template argument not an integral type");
3663
3664 public:
3665 /** The type of the range of the distribution. */
3666 typedef _IntType result_type;
3667 /** Parameter type. */
3668 struct param_type
3669 {
3670 typedef geometric_distribution<_IntType> distribution_type;
3671 friend class geometric_distribution<_IntType>;
3672
3673 explicit
3674 param_type(double __p = 0.5)
3675 : _M_p(__p)
3676 {
3677 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
3678 _M_initialize();
3679 }
3680
3681 double
3682 p() const
3683 { return _M_p; }
3684
3685 friend bool
3686 operator==(const param_type& __p1, const param_type& __p2)
3687 { return __p1._M_p == __p2._M_p; }
3688
3689 private:
3690 void
3691 _M_initialize()
3692 { _M_log_1_p = std::log(1.0 - _M_p); }
3693
3694 double _M_p;
3695
3696 double _M_log_1_p;
3697 };
3698
3699 // constructors and member function
3700 explicit
3701 geometric_distribution(double __p = 0.5)
3702 : _M_param(__p)
3703 { }
3704
3705 explicit
3706 geometric_distribution(const param_type& __p)
3707 : _M_param(__p)
3708 { }
3709
3710 /**
3711 * @brief Resets the distribution state.
3712 *
3713 * Does nothing for the geometric distribution.
3714 */
3715 void
3716 reset() { }
3717
3718 /**
3719 * @brief Returns the distribution parameter @p p.
3720 */
3721 double
3722 p() const
3723 { return _M_param.p(); }
3724
3725 /**
3726 * @brief Returns the parameter set of the distribution.
3727 */
3728 param_type
3729 param() const
3730 { return _M_param; }
3731
3732 /**
3733 * @brief Sets the parameter set of the distribution.
3734 * @param __param The new parameter set of the distribution.
3735 */
3736 void
3737 param(const param_type& __param)
3738 { _M_param = __param; }
3739
3740 /**
3741 * @brief Returns the greatest lower bound value of the distribution.
3742 */
3743 result_type
3744 min() const
3745 { return 0; }
3746
3747 /**
3748 * @brief Returns the least upper bound value of the distribution.
3749 */
3750 result_type
3751 max() const
3752 { return std::numeric_limits<result_type>::max(); }
3753
3754 /**
3755 * @brief Generating functions.
3756 */
3757 template<typename _UniformRandomNumberGenerator>
3758 result_type
3759 operator()(_UniformRandomNumberGenerator& __urng)
3760 { return this->operator()(__urng, this->param()); }
3761
3762 template<typename _UniformRandomNumberGenerator>
3763 result_type
3764 operator()(_UniformRandomNumberGenerator& __urng,
3765 const param_type& __p);
3766
3767 private:
3768 param_type _M_param;
3769 };
3770
3771 /**
3772 * @brief Return true if two geometric distributions have
3773 * the same parameters.
3774 */
3775 template<typename _IntType>
3776 inline bool
3777 operator==(const std::geometric_distribution<_IntType>& __d1,
3778 const std::geometric_distribution<_IntType>& __d2)
3779 { return __d1.param() == __d2.param(); }
3780
3781 /**
3782 * @brief Return true if two geometric distributions have
3783 * different parameters.
3784 */
3785 template<typename _IntType>
3786 inline bool
3787 operator!=(const std::geometric_distribution<_IntType>& __d1,
3788 const std::geometric_distribution<_IntType>& __d2)
3789 { return !(__d1 == __d2); }
3790
3791 /**
3792 * @brief Inserts a %geometric_distribution random number distribution
3793 * @p __x into the output stream @p __os.
3794 *
3795 * @param __os An output stream.
3796 * @param __x A %geometric_distribution random number distribution.
3797 *
3798 * @returns The output stream with the state of @p __x inserted or in
3799 * an error state.
3800 */
3801 template<typename _IntType,
3802 typename _CharT, typename _Traits>
3803 std::basic_ostream<_CharT, _Traits>&
3804 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3805 const std::geometric_distribution<_IntType>& __x);
3806
3807 /**
3808 * @brief Extracts a %geometric_distribution random number distribution
3809 * @p __x from the input stream @p __is.
3810 *
3811 * @param __is An input stream.
3812 * @param __x A %geometric_distribution random number generator engine.
3813 *
3814 * @returns The input stream with @p __x extracted or in an error state.
3815 */
3816 template<typename _IntType,
3817 typename _CharT, typename _Traits>
3818 std::basic_istream<_CharT, _Traits>&
3819 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3820 std::geometric_distribution<_IntType>& __x);
3821
3822
3823 /**
3824 * @brief A negative_binomial_distribution random number distribution.
3825 *
3826 * The formula for the negative binomial probability mass function is
3827 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3828 * and @f$p@f$ are the parameters of the distribution.
3829 */
3830 template<typename _IntType = int>
3831 class negative_binomial_distribution
3832 {
3833 static_assert(std::is_integral<_IntType>::value,
3834 "template argument not an integral type");
3835
3836 public:
3837 /** The type of the range of the distribution. */
3838 typedef _IntType result_type;
3839 /** Parameter type. */
3840 struct param_type
3841 {
3842 typedef negative_binomial_distribution<_IntType> distribution_type;
3843
3844 explicit
3845 param_type(_IntType __k = 1, double __p = 0.5)
3846 : _M_k(__k), _M_p(__p)
3847 {
3848 _GLIBCXX_DEBUG_ASSERT((_M_k > 0) && (_M_p > 0.0) && (_M_p <= 1.0));
3849 }
3850
3851 _IntType
3852 k() const
3853 { return _M_k; }
3854
3855 double
3856 p() const
3857 { return _M_p; }
3858
3859 friend bool
3860 operator==(const param_type& __p1, const param_type& __p2)
3861 { return __p1._M_k == __p2._M_k && __p1._M_p == __p2._M_p; }
3862
3863 private:
3864 _IntType _M_k;
3865 double _M_p;
3866 };
3867
3868 explicit
3869 negative_binomial_distribution(_IntType __k = 1, double __p = 0.5)
3870 : _M_param(__k, __p), _M_gd(__k, (1.0 - __p) / __p)
3871 { }
3872
3873 explicit
3874 negative_binomial_distribution(const param_type& __p)
3875 : _M_param(__p), _M_gd(__p.k(), (1.0 - __p.p()) / __p.p())
3876 { }
3877
3878 /**
3879 * @brief Resets the distribution state.
3880 */
3881 void
3882 reset()
3883 { _M_gd.reset(); }
3884
3885 /**
3886 * @brief Return the @f$k@f$ parameter of the distribution.
3887 */
3888 _IntType
3889 k() const
3890 { return _M_param.k(); }
3891
3892 /**
3893 * @brief Return the @f$p@f$ parameter of the distribution.
3894 */
3895 double
3896 p() const
3897 { return _M_param.p(); }
3898
3899 /**
3900 * @brief Returns the parameter set of the distribution.
3901 */
3902 param_type
3903 param() const
3904 { return _M_param; }
3905
3906 /**
3907 * @brief Sets the parameter set of the distribution.
3908 * @param __param The new parameter set of the distribution.
3909 */
3910 void
3911 param(const param_type& __param)
3912 { _M_param = __param; }
3913
3914 /**
3915 * @brief Returns the greatest lower bound value of the distribution.
3916 */
3917 result_type
3918 min() const
3919 { return result_type(0); }
3920
3921 /**
3922 * @brief Returns the least upper bound value of the distribution.
3923 */
3924 result_type
3925 max() const
3926 { return std::numeric_limits<result_type>::max(); }
3927
3928 /**
3929 * @brief Generating functions.
3930 */
3931 template<typename _UniformRandomNumberGenerator>
3932 result_type
3933 operator()(_UniformRandomNumberGenerator& __urng);
3934
3935 template<typename _UniformRandomNumberGenerator>
3936 result_type
3937 operator()(_UniformRandomNumberGenerator& __urng,
3938 const param_type& __p);
3939
3940 /**
3941 * @brief Return true if two negative binomial distributions have
3942 * the same parameters and the sequences that would be
3943 * generated are equal.
3944 */
3945 friend bool
3946 operator==(const negative_binomial_distribution& __d1,
3947 const negative_binomial_distribution& __d2)
3948 { return __d1.param() == __d2.param() && __d1._M_gd == __d2._M_gd; }
3949
3950 /**
3951 * @brief Inserts a %negative_binomial_distribution random
3952 * number distribution @p __x into the output stream @p __os.
3953 *
3954 * @param __os An output stream.
3955 * @param __x A %negative_binomial_distribution random number
3956 * distribution.
3957 *
3958 * @returns The output stream with the state of @p __x inserted or in
3959 * an error state.
3960 */
3961 template<typename _IntType1, typename _CharT, typename _Traits>
3962 friend std::basic_ostream<_CharT, _Traits>&
3963 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3964 const std::negative_binomial_distribution<_IntType1>& __x);
3965
3966 /**
3967 * @brief Extracts a %negative_binomial_distribution random number
3968 * distribution @p __x from the input stream @p __is.
3969 *
3970 * @param __is An input stream.
3971 * @param __x A %negative_binomial_distribution random number
3972 * generator engine.
3973 *
3974 * @returns The input stream with @p __x extracted or in an error state.
3975 */
3976 template<typename _IntType1, typename _CharT, typename _Traits>
3977 friend std::basic_istream<_CharT, _Traits>&
3978 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3979 std::negative_binomial_distribution<_IntType1>& __x);
3980
3981 private:
3982 param_type _M_param;
3983
3984 std::gamma_distribution<double> _M_gd;
3985 };
3986
3987 /**
3988 * @brief Return true if two negative binomial distributions are different.
3989 */
3990 template<typename _IntType>
3991 inline bool
3992 operator!=(const std::negative_binomial_distribution<_IntType>& __d1,
3993 const std::negative_binomial_distribution<_IntType>& __d2)
3994 { return !(__d1 == __d2); }
3995
3996
3997 /* @} */ // group random_distributions_bernoulli
3998
3999 /**
4000 * @addtogroup random_distributions_poisson Poisson Distributions
4001 * @ingroup random_distributions
4002 * @{
4003 */
4004
4005 /**
4006 * @brief A discrete Poisson random number distribution.
4007 *
4008 * The formula for the Poisson probability density function is
4009 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
4010 * parameter of the distribution.
4011 */
4012 template<typename _IntType = int>
4013 class poisson_distribution
4014 {
4015 static_assert(std::is_integral<_IntType>::value,
4016 "template argument not an integral type");
4017
4018 public:
4019 /** The type of the range of the distribution. */
4020 typedef _IntType result_type;
4021 /** Parameter type. */
4022 struct param_type
4023 {
4024 typedef poisson_distribution<_IntType> distribution_type;
4025 friend class poisson_distribution<_IntType>;
4026
4027 explicit
4028 param_type(double __mean = 1.0)
4029 : _M_mean(__mean)
4030 {
4031 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
4032 _M_initialize();
4033 }
4034
4035 double
4036 mean() const
4037 { return _M_mean; }
4038
4039 friend bool
4040 operator==(const param_type& __p1, const param_type& __p2)
4041 { return __p1._M_mean == __p2._M_mean; }
4042
4043 private:
4044 // Hosts either log(mean) or the threshold of the simple method.
4045 void
4046 _M_initialize();
4047
4048 double _M_mean;
4049
4050 double _M_lm_thr;
4051 #if _GLIBCXX_USE_C99_MATH_TR1
4052 double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
4053 #endif
4054 };
4055
4056 // constructors and member function
4057 explicit
4058 poisson_distribution(double __mean = 1.0)
4059 : _M_param(__mean), _M_nd()
4060 { }
4061
4062 explicit
4063 poisson_distribution(const param_type& __p)
4064 : _M_param(__p), _M_nd()
4065 { }
4066
4067 /**
4068 * @brief Resets the distribution state.
4069 */
4070 void
4071 reset()
4072 { _M_nd.reset(); }
4073
4074 /**
4075 * @brief Returns the distribution parameter @p mean.
4076 */
4077 double
4078 mean() const
4079 { return _M_param.mean(); }
4080
4081 /**
4082 * @brief Returns the parameter set of the distribution.
4083 */
4084 param_type
4085 param() const
4086 { return _M_param; }
4087
4088 /**
4089 * @brief Sets the parameter set of the distribution.
4090 * @param __param The new parameter set of the distribution.
4091 */
4092 void
4093 param(const param_type& __param)
4094 { _M_param = __param; }
4095
4096 /**
4097 * @brief Returns the greatest lower bound value of the distribution.
4098 */
4099 result_type
4100 min() const
4101 { return 0; }
4102
4103 /**
4104 * @brief Returns the least upper bound value of the distribution.
4105 */
4106 result_type
4107 max() const
4108 { return std::numeric_limits<result_type>::max(); }
4109
4110 /**
4111 * @brief Generating functions.
4112 */
4113 template<typename _UniformRandomNumberGenerator>
4114 result_type
4115 operator()(_UniformRandomNumberGenerator& __urng)
4116 { return this->operator()(__urng, this->param()); }
4117
4118 template<typename _UniformRandomNumberGenerator>
4119 result_type
4120 operator()(_UniformRandomNumberGenerator& __urng,
4121 const param_type& __p);
4122
4123 /**
4124 * @brief Return true if two Poisson distributions have the same
4125 * parameters and the sequences that would be generated
4126 * are equal.
4127 */
4128 friend bool
4129 operator==(const poisson_distribution& __d1,
4130 const poisson_distribution& __d2)
4131 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4132 { return __d1.param() == __d2.param() && __d1._M_nd == __d2._M_nd; }
4133 #else
4134 { return __d1.param() == __d2.param(); }
4135 #endif
4136
4137 /**
4138 * @brief Inserts a %poisson_distribution random number distribution
4139 * @p __x into the output stream @p __os.
4140 *
4141 * @param __os An output stream.
4142 * @param __x A %poisson_distribution random number distribution.
4143 *
4144 * @returns The output stream with the state of @p __x inserted or in
4145 * an error state.
4146 */
4147 template<typename _IntType1, typename _CharT, typename _Traits>
4148 friend std::basic_ostream<_CharT, _Traits>&
4149 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
4150 const std::poisson_distribution<_IntType1>& __x);
4151
4152 /**
4153 * @brief Extracts a %poisson_distribution random number distribution
4154 * @p __x from the input stream @p __is.
4155 *
4156 * @param __is An input stream.
4157 * @param __x A %poisson_distribution random number generator engine.
4158 *
4159 * @returns The input stream with @p __x extracted or in an error
4160 * state.
4161 */
4162 template<typename _IntType1, typename _CharT, typename _Traits>
4163 friend std::basic_istream<_CharT, _Traits>&
4164 operator>>(std::basic_istream<_CharT, _Traits>& __is,
4165 std::poisson_distribution<_IntType1>& __x);
4166
4167 private:
4168 param_type _M_param;
4169
4170 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4171 std::normal_distribution<double> _M_nd;
4172 };
4173
4174 /**
4175 * @brief Return true if two Poisson distributions are different.
4176 */
4177 template<typename _IntType>
4178 inline bool
4179 operator!=(const std::poisson_distribution<_IntType>& __d1,
4180 const std::poisson_distribution<_IntType>& __d2)
4181 { return !(__d1 == __d2); }
4182
4183
4184 /**
4185 * @brief An exponential continuous distribution for random numbers.
4186 *
4187 * The formula for the exponential probability density function is
4188 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4189 *
4190 * <table border=1 cellpadding=10 cellspacing=0>
4191 * <caption align=top>Distribution Statistics</caption>
4192 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4193 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4194 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4195 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4196 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4197 * </table>
4198 */
4199 template<typename _RealType = double>
4200 class exponential_distribution
4201 {
4202 static_assert(std::is_floating_point<_RealType>::value,
4203 "template argument not a floating point type");
4204
4205 public:
4206 /** The type of the range of the distribution. */
4207 typedef _RealType result_type;
4208 /** Parameter type. */
4209 struct param_type
4210 {
4211 typedef exponential_distribution<_RealType> distribution_type;
4212
4213 explicit
4214 param_type(_RealType __lambda = _RealType(1))
4215 : _M_lambda(__lambda)
4216 {
4217 _GLIBCXX_DEBUG_ASSERT(_M_lambda > _RealType(0));
4218 }
4219
4220 _RealType
4221 lambda() const
4222 { return _M_lambda; }
4223
4224 friend bool
4225 operator==(const param_type& __p1, const param_type& __p2)
4226 { return __p1._M_lambda == __p2._M_lambda; }
4227
4228 private:
4229 _RealType _M_lambda;
4230 };
4231
4232 public:
4233 /**
4234 * @brief Constructs an exponential distribution with inverse scale
4235 * parameter @f$\lambda@f$.
4236 */
4237 explicit
4238 exponential_distribution(const result_type& __lambda = result_type(1))
4239 : _M_param(__lambda)
4240 { }
4241
4242 explicit
4243 exponential_distribution(const param_type& __p)
4244 : _M_param(__p)
4245 { }
4246
4247 /**
4248 * @brief Resets the distribution state.
4249 *
4250 * Has no effect on exponential distributions.
4251 */
4252 void
4253 reset() { }
4254
4255 /**
4256 * @brief Returns the inverse scale parameter of the distribution.
4257 */
4258 _RealType
4259 lambda() const
4260 { return _M_param.lambda(); }
4261
4262 /**
4263 * @brief Returns the parameter set of the distribution.
4264 */
4265 param_type
4266 param() const
4267 { return _M_param; }
4268
4269 /**
4270 * @brief Sets the parameter set of the distribution.
4271 * @param __param The new parameter set of the distribution.
4272 */
4273 void
4274 param(const param_type& __param)
4275 { _M_param = __param; }
4276
4277 /**
4278 * @brief Returns the greatest lower bound value of the distribution.
4279 */
4280 result_type
4281 min() const
4282 { return result_type(0); }
4283
4284 /**
4285 * @brief Returns the least upper bound value of the distribution.
4286 */
4287 result_type
4288 max() const
4289 { return std::numeric_limits<result_type>::max(); }
4290
4291 /**
4292 * @brief Generating functions.
4293 */
4294 template<typename _UniformRandomNumberGenerator>
4295 result_type
4296 operator()(_UniformRandomNumberGenerator& __urng)
4297 { return this->operator()(__urng, this->param()); }
4298
4299 template<typename _UniformRandomNumberGenerator>
4300 result_type
4301 operator()(_UniformRandomNumberGenerator& __urng,
4302 const param_type& __p)
4303 {
4304 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
4305 __aurng(__urng);
4306 return -std::log(__aurng()) / __p.lambda();
4307 }
4308
4309 private:
4310 param_type _M_param;
4311 };
4312
4313 /**
4314 * @brief Return true if two exponential distributions have the same
4315 * parameters.
4316 */
4317 template<typename _RealType>
4318 inline bool
4319 operator==(const std::exponential_distribution<_RealType>& __d1,
4320 const std::exponential_distribution<_RealType>& __d2)
4321 { return __d1.param() == __d2.param(); }
4322
4323 /**
4324 * @brief Return true if two exponential distributions have different
4325 * parameters.
4326 */
4327 template<typename _RealType>
4328 inline bool
4329 operator!=(const std::exponential_distribution<_RealType>& __d1,
4330 const std::exponential_distribution<_RealType>& __d2)
4331 { return !(__d1 == __d2); }
4332
4333 /**
4334 * @brief Inserts a %exponential_distribution random number distribution
4335 * @p __x into the output stream @p __os.
4336 *
4337 * @param __os An output stream.
4338 * @param __x A %exponential_distribution random number distribution.
4339 *
4340 * @returns The output stream with the state of @p __x inserted or in
4341 * an error state.
4342 */
4343 template<typename _RealType, typename _CharT, typename _Traits>
4344 std::basic_ostream<_CharT, _Traits>&
4345 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
4346 const std::exponential_distribution<_RealType>& __x);
4347
4348 /**
4349 * @brief Extracts a %exponential_distribution random number distribution
4350 * @p __x from the input stream @p __is.
4351 *
4352 * @param __is An input stream.
4353 * @param __x A %exponential_distribution random number
4354 * generator engine.
4355 *
4356 * @returns The input stream with @p __x extracted or in an error state.
4357 */
4358 template<typename _RealType, typename _CharT, typename _Traits>
4359 std::basic_istream<_CharT, _Traits>&
4360 operator>>(std::basic_istream<_CharT, _Traits>& __is,
4361 std::exponential_distribution<_RealType>& __x);
4362
4363
4364 /**
4365 * @brief A weibull_distribution random number distribution.
4366 *
4367 * The formula for the normal probability density function is:
4368 * @f[
4369 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4370 * \exp{(-(\frac{x}{\beta})^\alpha)}
4371 * @f]
4372 */
4373 template<typename _RealType = double>
4374 class weibull_distribution
4375 {
4376 static_assert(std::is_floating_point<_RealType>::value,
4377 "template argument not a floating point type");
4378
4379 public:
4380 /** The type of the range of the distribution. */
4381 typedef _RealType result_type;
4382 /** Parameter type. */
4383 struct param_type
4384 {
4385 typedef weibull_distribution<_RealType> distribution_type;
4386
4387 explicit
4388 param_type(_RealType __a = _RealType(1),
4389 _RealType __b = _RealType(1))
4390 : _M_a(__a), _M_b(__b)
4391 { }
4392
4393 _RealType
4394 a() const
4395 { return _M_a; }
4396
4397 _RealType
4398 b() const
4399 { return _M_b; }
4400
4401 friend bool
4402 operator==(const param_type& __p1, const param_type& __p2)
4403 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
4404
4405 private:
4406 _RealType _M_a;
4407 _RealType _M_b;
4408 };
4409
4410 explicit
4411 weibull_distribution(_RealType __a = _RealType(1),
4412 _RealType __b = _RealType(1))
4413 : _M_param(__a, __b)
4414 { }
4415
4416 explicit
4417 weibull_distribution(const param_type& __p)
4418 : _M_param(__p)
4419 { }
4420
4421 /**
4422 * @brief Resets the distribution state.
4423 */
4424 void
4425 reset()
4426 { }
4427
4428 /**
4429 * @brief Return the @f$a@f$ parameter of the distribution.
4430 */
4431 _RealType
4432 a() const
4433 { return _M_param.a(); }
4434
4435 /**
4436 * @brief Return the @f$b@f$ parameter of the distribution.
4437 */
4438 _RealType
4439 b() const
4440 { return _M_param.b(); }
4441
4442 /**
4443 * @brief Returns the parameter set of the distribution.
4444 */
4445 param_type
4446 param() const
4447 { return _M_param; }
4448
4449 /**
4450 * @brief Sets the parameter set of the distribution.
4451 * @param __param The new parameter set of the distribution.
4452 */
4453 void
4454 param(const param_type& __param)
4455 { _M_param = __param; }
4456
4457 /**
4458 * @brief Returns the greatest lower bound value of the distribution.
4459 */
4460 result_type
4461 min() const
4462 { return result_type(0); }
4463
4464 /**
4465 * @brief Returns the least upper bound value of the distribution.
4466 */
4467 result_type
4468 max() const
4469 { return std::numeric_limits<result_type>::max(); }
4470
4471 /**
4472 * @brief Generating functions.
4473 */
4474 template<typename _UniformRandomNumberGenerator>
4475 result_type
4476 operator()(_UniformRandomNumberGenerator& __urng)
4477 { return this->operator()(__urng, this->param()); }
4478
4479 template<typename _UniformRandomNumberGenerator>
4480 result_type
4481 operator()(_UniformRandomNumberGenerator& __urng,
4482 const param_type& __p);
4483
4484 private:
4485 param_type _M_param;
4486 };
4487
4488 /**
4489 * @brief Return true if two Weibull distributions have the same
4490 * parameters.
4491 */
4492 template<typename _RealType>
4493 inline bool
4494 operator==(const std::weibull_distribution<_RealType>& __d1,
4495 const std::weibull_distribution<_RealType>& __d2)
4496 { return __d1.param() == __d2.param(); }
4497
4498 /**
4499 * @brief Return true if two Weibull distributions have different
4500 * parameters.
4501 */
4502 template<typename _RealType>
4503 inline bool
4504 operator!=(const std::weibull_distribution<_RealType>& __d1,
4505 const std::weibull_distribution<_RealType>& __d2)
4506 { return !(__d1 == __d2); }
4507
4508 /**
4509 * @brief Inserts a %weibull_distribution random number distribution
4510 * @p __x into the output stream @p __os.
4511 *
4512 * @param __os An output stream.
4513 * @param __x A %weibull_distribution random number distribution.
4514 *
4515 * @returns The output stream with the state of @p __x inserted or in
4516 * an error state.
4517 */
4518 template<typename _RealType, typename _CharT, typename _Traits>
4519 std::basic_ostream<_CharT, _Traits>&
4520 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
4521 const std::weibull_distribution<_RealType>& __x);
4522
4523 /**
4524 * @brief Extracts a %weibull_distribution random number distribution
4525 * @p __x from the input stream @p __is.
4526 *
4527 * @param __is An input stream.
4528 * @param __x A %weibull_distribution random number
4529 * generator engine.
4530 *
4531 * @returns The input stream with @p __x extracted or in an error state.
4532 */
4533 template<typename _RealType, typename _CharT, typename _Traits>
4534 std::basic_istream<_CharT, _Traits>&
4535 operator>>(std::basic_istream<_CharT, _Traits>& __is,
4536 std::weibull_distribution<_RealType>& __x);
4537
4538
4539 /**
4540 * @brief A extreme_value_distribution random number distribution.
4541 *
4542 * The formula for the normal probability mass function is
4543 * @f[
4544 * p(x|a,b) = \frac{1}{b}
4545 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
4546 * @f]
4547 */
4548 template<typename _RealType = double>
4549 class extreme_value_distribution
4550 {
4551 static_assert(std::is_floating_point<_RealType>::value,
4552 "template argument not a floating point type");
4553
4554 public:
4555 /** The type of the range of the distribution. */
4556 typedef _RealType result_type;
4557 /** Parameter type. */
4558 struct param_type
4559 {
4560 typedef extreme_value_distribution<_RealType> distribution_type;
4561
4562 explicit
4563 param_type(_RealType __a = _RealType(0),
4564 _RealType __b = _RealType(1))
4565 : _M_a(__a), _M_b(__b)
4566 { }
4567
4568 _RealType
4569 a() const
4570 { return _M_a; }
4571
4572 _RealType
4573 b() const
4574 { return _M_b; }
4575
4576 friend bool
4577 operator==(const param_type& __p1, const param_type& __p2)
4578 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
4579
4580 private:
4581 _RealType _M_a;
4582 _RealType _M_b;
4583 };
4584
4585 explicit
4586 extreme_value_distribution(_RealType __a = _RealType(0),
4587 _RealType __b = _RealType(1))
4588 : _M_param(__a, __b)
4589 { }
4590
4591 explicit
4592 extreme_value_distribution(const param_type& __p)
4593 : _M_param(__p)
4594 { }
4595
4596 /**
4597 * @brief Resets the distribution state.
4598 */
4599 void
4600 reset()
4601 { }
4602
4603 /**
4604 * @brief Return the @f$a@f$ parameter of the distribution.
4605 */
4606 _RealType
4607 a() const
4608 { return _M_param.a(); }
4609
4610 /**
4611 * @brief Return the @f$b@f$ parameter of the distribution.
4612 */
4613 _RealType
4614 b() const
4615 { return _M_param.b(); }
4616
4617 /**
4618 * @brief Returns the parameter set of the distribution.
4619 */
4620 param_type
4621 param() const
4622 { return _M_param; }
4623
4624 /**
4625 * @brief Sets the parameter set of the distribution.
4626 * @param __param The new parameter set of the distribution.
4627 */
4628 void
4629 param(const param_type& __param)
4630 { _M_param = __param; }
4631
4632 /**
4633 * @brief Returns the greatest lower bound value of the distribution.
4634 */
4635 result_type
4636 min() const
4637 { return std::numeric_limits<result_type>::min(); }
4638
4639 /**
4640 * @brief Returns the least upper bound value of the distribution.
4641 */
4642 result_type
4643 max() const
4644 { return std::numeric_limits<result_type>::max(); }
4645
4646 /**
4647 * @brief Generating functions.
4648 */
4649 template<typename _UniformRandomNumberGenerator>
4650 result_type
4651 operator()(_UniformRandomNumberGenerator& __urng)
4652 { return this->operator()(__urng, this->param()); }
4653
4654 template<typename _UniformRandomNumberGenerator>
4655 result_type
4656 operator()(_UniformRandomNumberGenerator& __urng,
4657 const param_type& __p);
4658
4659 private:
4660 param_type _M_param;
4661 };
4662
4663 /**
4664 * @brief Return true if two extreme value distributions have the same
4665 * parameters.
4666 */
4667 template<typename _RealType>
4668 inline bool
4669 operator==(const std::extreme_value_distribution<_RealType>& __d1,
4670 const std::extreme_value_distribution<_RealType>& __d2)
4671 { return __d1.param() == __d2.param(); }
4672
4673 /**
4674 * @brief Return true if two extreme value distributions have different
4675 * parameters.
4676 */
4677 template<typename _RealType>
4678 inline bool
4679 operator!=(const std::extreme_value_distribution<_RealType>& __d1,
4680 const std::extreme_value_distribution<_RealType>& __d2)
4681 { return !(__d1 == __d2); }
4682
4683 /**
4684 * @brief Inserts a %extreme_value_distribution random number distribution
4685 * @p __x into the output stream @p __os.
4686 *
4687 * @param __os An output stream.
4688 * @param __x A %extreme_value_distribution random number distribution.
4689 *
4690 * @returns The output stream with the state of @p __x inserted or in
4691 * an error state.
4692 */
4693 template<typename _RealType, typename _CharT, typename _Traits>
4694 std::basic_ostream<_CharT, _Traits>&
4695 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
4696 const std::extreme_value_distribution<_RealType>& __x);
4697
4698 /**
4699 * @brief Extracts a %extreme_value_distribution random number
4700 * distribution @p __x from the input stream @p __is.
4701 *
4702 * @param __is An input stream.
4703 * @param __x A %extreme_value_distribution random number
4704 * generator engine.
4705 *
4706 * @returns The input stream with @p __x extracted or in an error state.
4707 */
4708 template<typename _RealType, typename _CharT, typename _Traits>
4709 std::basic_istream<_CharT, _Traits>&
4710 operator>>(std::basic_istream<_CharT, _Traits>& __is,
4711 std::extreme_value_distribution<_RealType>& __x);
4712
4713
4714 /**
4715 * @brief A discrete_distribution random number distribution.
4716 *
4717 * The formula for the discrete probability mass function is
4718 *
4719 */
4720 template<typename _IntType = int>
4721 class discrete_distribution
4722 {
4723 static_assert(std::is_integral<_IntType>::value,
4724 "template argument not an integral type");
4725
4726 public:
4727 /** The type of the range of the distribution. */
4728 typedef _IntType result_type;
4729 /** Parameter type. */
4730 struct param_type
4731 {
4732 typedef discrete_distribution<_IntType> distribution_type;
4733 friend class discrete_distribution<_IntType>;
4734
4735 param_type()
4736 : _M_prob(), _M_cp()
4737 { }
4738
4739 template<typename _InputIterator>
4740 param_type(_InputIterator __wbegin,
4741 _InputIterator __wend)
4742 : _M_prob(__wbegin, __wend), _M_cp()
4743 { _M_initialize(); }
4744
4745 param_type(initializer_list<double> __wil)
4746 : _M_prob(__wil.begin(), __wil.end()), _M_cp()
4747 { _M_initialize(); }
4748
4749 template<typename _Func>
4750 param_type(size_t __nw, double __xmin, double __xmax,
4751 _Func __fw);
4752
4753 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4754 param_type(const param_type&) = default;
4755 param_type& operator=(const param_type&) = default;
4756
4757 std::vector<double>
4758 probabilities() const
4759 { return _M_prob.empty() ? std::vector<double>(1, 1.0) : _M_prob; }
4760
4761 friend bool
4762 operator==(const param_type& __p1, const param_type& __p2)
4763 { return __p1._M_prob == __p2._M_prob; }
4764
4765 private:
4766 void
4767 _M_initialize();
4768
4769 std::vector<double> _M_prob;
4770 std::vector<double> _M_cp;
4771 };
4772
4773 discrete_distribution()
4774 : _M_param()
4775 { }
4776
4777 template<typename _InputIterator>
4778 discrete_distribution(_InputIterator __wbegin,
4779 _InputIterator __wend)
4780 : _M_param(__wbegin, __wend)
4781 { }
4782
4783 discrete_distribution(initializer_list<double> __wl)
4784 : _M_param(__wl)
4785 { }
4786
4787 template<typename _Func>
4788 discrete_distribution(size_t __nw, double __xmin, double __xmax,
4789 _Func __fw)
4790 : _M_param(__nw, __xmin, __xmax, __fw)
4791 { }
4792
4793 explicit
4794 discrete_distribution(const param_type& __p)
4795 : _M_param(__p)
4796 { }
4797
4798 /**
4799 * @brief Resets the distribution state.
4800 */
4801 void
4802 reset()
4803 { }
4804
4805 /**
4806 * @brief Returns the probabilities of the distribution.
4807 */
4808 std::vector<double>
4809 probabilities() const
4810 {
4811 return _M_param._M_prob.empty()
4812 ? std::vector<double>(1, 1.0) : _M_param._M_prob;
4813 }
4814
4815 /**
4816 * @brief Returns the parameter set of the distribution.
4817 */
4818 param_type
4819 param() const
4820 { return _M_param; }
4821
4822 /**
4823 * @brief Sets the parameter set of the distribution.
4824 * @param __param The new parameter set of the distribution.
4825 */
4826 void
4827 param(const param_type& __param)
4828 { _M_param = __param; }
4829
4830 /**
4831 * @brief Returns the greatest lower bound value of the distribution.
4832 */
4833 result_type
4834 min() const
4835 { return result_type(0); }
4836
4837 /**
4838 * @brief Returns the least upper bound value of the distribution.
4839 */
4840 result_type
4841 max() const
4842 {
4843 return _M_param._M_prob.empty()
4844 ? result_type(0) : result_type(_M_param._M_prob.size() - 1);
4845 }
4846
4847 /**
4848 * @brief Generating functions.
4849 */
4850 template<typename _UniformRandomNumberGenerator>
4851 result_type
4852 operator()(_UniformRandomNumberGenerator& __urng)
4853 { return this->operator()(__urng, this->param()); }
4854
4855 template<typename _UniformRandomNumberGenerator>
4856 result_type
4857 operator()(_UniformRandomNumberGenerator& __urng,
4858 const param_type& __p);
4859
4860 /**
4861 * @brief Inserts a %discrete_distribution random number distribution
4862 * @p __x into the output stream @p __os.
4863 *
4864 * @param __os An output stream.
4865 * @param __x A %discrete_distribution random number distribution.
4866 *
4867 * @returns The output stream with the state of @p __x inserted or in
4868 * an error state.
4869 */
4870 template<typename _IntType1, typename _CharT, typename _Traits>
4871 friend std::basic_ostream<_CharT, _Traits>&
4872 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
4873 const std::discrete_distribution<_IntType1>& __x);
4874
4875 /**
4876 * @brief Extracts a %discrete_distribution random number distribution
4877 * @p __x from the input stream @p __is.
4878 *
4879 * @param __is An input stream.
4880 * @param __x A %discrete_distribution random number
4881 * generator engine.
4882 *
4883 * @returns The input stream with @p __x extracted or in an error
4884 * state.
4885 */
4886 template<typename _IntType1, typename _CharT, typename _Traits>
4887 friend std::basic_istream<_CharT, _Traits>&
4888 operator>>(std::basic_istream<_CharT, _Traits>& __is,
4889 std::discrete_distribution<_IntType1>& __x);
4890
4891 private:
4892 param_type _M_param;
4893 };
4894
4895 /**
4896 * @brief Return true if two discrete distributions have the same
4897 * parameters.
4898 */
4899 template<typename _IntType>
4900 inline bool
4901 operator==(const std::discrete_distribution<_IntType>& __d1,
4902 const std::discrete_distribution<_IntType>& __d2)
4903 { return __d1.param() == __d2.param(); }
4904
4905 /**
4906 * @brief Return true if two discrete distributions have different
4907 * parameters.
4908 */
4909 template<typename _IntType>
4910 inline bool
4911 operator!=(const std::discrete_distribution<_IntType>& __d1,
4912 const std::discrete_distribution<_IntType>& __d2)
4913 { return !(__d1 == __d2); }
4914
4915
4916 /**
4917 * @brief A piecewise_constant_distribution random number distribution.
4918 *
4919 * The formula for the piecewise constant probability mass function is
4920 *
4921 */
4922 template<typename _RealType = double>
4923 class piecewise_constant_distribution
4924 {
4925 static_assert(std::is_floating_point<_RealType>::value,
4926 "template argument not a floating point type");
4927
4928 public:
4929 /** The type of the range of the distribution. */
4930 typedef _RealType result_type;
4931 /** Parameter type. */
4932 struct param_type
4933 {
4934 typedef piecewise_constant_distribution<_RealType> distribution_type;
4935 friend class piecewise_constant_distribution<_RealType>;
4936
4937 param_type()
4938 : _M_int(), _M_den(), _M_cp()
4939 { }
4940
4941 template<typename _InputIteratorB, typename _InputIteratorW>
4942 param_type(_InputIteratorB __bfirst,
4943 _InputIteratorB __bend,
4944 _InputIteratorW __wbegin);
4945
4946 template<typename _Func>
4947 param_type(initializer_list<_RealType> __bi, _Func __fw);
4948
4949 template<typename _Func>
4950 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4951 _Func __fw);
4952
4953 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4954 param_type(const param_type&) = default;
4955 param_type& operator=(const param_type&) = default;
4956
4957 std::vector<_RealType>
4958 intervals() const
4959 {
4960 if (_M_int.empty())
4961 {
4962 std::vector<_RealType> __tmp(2);
4963 __tmp[1] = _RealType(1);
4964 return __tmp;
4965 }
4966 else
4967 return _M_int;
4968 }
4969
4970 std::vector<double>
4971 densities() const
4972 { return _M_den.empty() ? std::vector<double>(1, 1.0) : _M_den; }
4973
4974 friend bool
4975 operator==(const param_type& __p1, const param_type& __p2)
4976 { return __p1._M_int == __p2._M_int && __p1._M_den == __p2._M_den; }
4977
4978 private:
4979 void
4980 _M_initialize();
4981
4982 std::vector<_RealType> _M_int;
4983 std::vector<double> _M_den;
4984 std::vector<double> _M_cp;
4985 };
4986
4987 explicit
4988 piecewise_constant_distribution()
4989 : _M_param()
4990 { }
4991
4992 template<typename _InputIteratorB, typename _InputIteratorW>
4993 piecewise_constant_distribution(_InputIteratorB __bfirst,
4994 _InputIteratorB __bend,
4995 _InputIteratorW __wbegin)
4996 : _M_param(__bfirst, __bend, __wbegin)
4997 { }
4998
4999 template<typename _Func>
5000 piecewise_constant_distribution(initializer_list<_RealType> __bl,
5001 _Func __fw)
5002 : _M_param(__bl, __fw)
5003 { }
5004
5005 template<typename _Func>
5006 piecewise_constant_distribution(size_t __nw,
5007 _RealType __xmin, _RealType __xmax,
5008 _Func __fw)
5009 : _M_param(__nw, __xmin, __xmax, __fw)
5010 { }
5011
5012 explicit
5013 piecewise_constant_distribution(const param_type& __p)
5014 : _M_param(__p)
5015 { }
5016
5017 /**
5018 * @brief Resets the distribution state.
5019 */
5020 void
5021 reset()
5022 { }
5023
5024 /**
5025 * @brief Returns a vector of the intervals.
5026 */
5027 std::vector<_RealType>
5028 intervals() const
5029 {
5030 if (_M_param._M_int.empty())
5031 {
5032 std::vector<_RealType> __tmp(2);
5033 __tmp[1] = _RealType(1);
5034 return __tmp;
5035 }
5036 else
5037 return _M_param._M_int;
5038 }
5039
5040 /**
5041 * @brief Returns a vector of the probability densities.
5042 */
5043 std::vector<double>
5044 densities() const
5045 {
5046 return _M_param._M_den.empty()
5047 ? std::vector<double>(1, 1.0) : _M_param._M_den;
5048 }
5049
5050 /**
5051 * @brief Returns the parameter set of the distribution.
5052 */
5053 param_type
5054 param() const
5055 { return _M_param; }
5056
5057 /**
5058 * @brief Sets the parameter set of the distribution.
5059 * @param __param The new parameter set of the distribution.
5060 */
5061 void
5062 param(const param_type& __param)
5063 { _M_param = __param; }
5064
5065 /**
5066 * @brief Returns the greatest lower bound value of the distribution.
5067 */
5068 result_type
5069 min() const
5070 {
5071 return _M_param._M_int.empty()
5072 ? result_type(0) : _M_param._M_int.front();
5073 }
5074
5075 /**
5076 * @brief Returns the least upper bound value of the distribution.
5077 */
5078 result_type
5079 max() const
5080 {
5081 return _M_param._M_int.empty()
5082 ? result_type(1) : _M_param._M_int.back();
5083 }
5084
5085 /**
5086 * @brief Generating functions.
5087 */
5088 template<typename _UniformRandomNumberGenerator>
5089 result_type
5090 operator()(_UniformRandomNumberGenerator& __urng)
5091 { return this->operator()(__urng, this->param()); }
5092
5093 template<typename _UniformRandomNumberGenerator>
5094 result_type
5095 operator()(_UniformRandomNumberGenerator& __urng,
5096 const param_type& __p);
5097
5098 /**
5099 * @brief Inserts a %piecewise_constan_distribution random
5100 * number distribution @p __x into the output stream @p __os.
5101 *
5102 * @param __os An output stream.
5103 * @param __x A %piecewise_constan_distribution random number
5104 * distribution.
5105 *
5106 * @returns The output stream with the state of @p __x inserted or in
5107 * an error state.
5108 */
5109 template<typename _RealType1, typename _CharT, typename _Traits>
5110 friend std::basic_ostream<_CharT, _Traits>&
5111 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
5112 const std::piecewise_constant_distribution<_RealType1>& __x);
5113
5114 /**
5115 * @brief Extracts a %piecewise_constan_distribution random
5116 * number distribution @p __x from the input stream @p __is.
5117 *
5118 * @param __is An input stream.
5119 * @param __x A %piecewise_constan_distribution random number
5120 * generator engine.
5121 *
5122 * @returns The input stream with @p __x extracted or in an error
5123 * state.
5124 */
5125 template<typename _RealType1, typename _CharT, typename _Traits>
5126 friend std::basic_istream<_CharT, _Traits>&
5127 operator>>(std::basic_istream<_CharT, _Traits>& __is,
5128 std::piecewise_constant_distribution<_RealType1>& __x);
5129
5130 private:
5131 param_type _M_param;
5132 };
5133
5134 /**
5135 * @brief Return true if two piecewise constant distributions have the
5136 * same parameters.
5137 */
5138 template<typename _RealType>
5139 inline bool
5140 operator==(const std::piecewise_constant_distribution<_RealType>& __d1,
5141 const std::piecewise_constant_distribution<_RealType>& __d2)
5142 { return __d1.param() == __d2.param(); }
5143
5144 /**
5145 * @brief Return true if two piecewise constant distributions have
5146 * different parameters.
5147 */
5148 template<typename _RealType>
5149 inline bool
5150 operator!=(const std::piecewise_constant_distribution<_RealType>& __d1,
5151 const std::piecewise_constant_distribution<_RealType>& __d2)
5152 { return !(__d1 == __d2); }
5153
5154
5155 /**
5156 * @brief A piecewise_linear_distribution random number distribution.
5157 *
5158 * The formula for the piecewise linear probability mass function is
5159 *
5160 */
5161 template<typename _RealType = double>
5162 class piecewise_linear_distribution
5163 {
5164 static_assert(std::is_floating_point<_RealType>::value,
5165 "template argument not a floating point type");
5166
5167 public:
5168 /** The type of the range of the distribution. */
5169 typedef _RealType result_type;
5170 /** Parameter type. */
5171 struct param_type
5172 {
5173 typedef piecewise_linear_distribution<_RealType> distribution_type;
5174 friend class piecewise_linear_distribution<_RealType>;
5175
5176 param_type()
5177 : _M_int(), _M_den(), _M_cp(), _M_m()
5178 { }
5179
5180 template<typename _InputIteratorB, typename _InputIteratorW>
5181 param_type(_InputIteratorB __bfirst,
5182 _InputIteratorB __bend,
5183 _InputIteratorW __wbegin);
5184
5185 template<typename _Func>
5186 param_type(initializer_list<_RealType> __bl, _Func __fw);
5187
5188 template<typename _Func>
5189 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
5190 _Func __fw);
5191
5192 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5193 param_type(const param_type&) = default;
5194 param_type& operator=(const param_type&) = default;
5195
5196 std::vector<_RealType>
5197 intervals() const
5198 {
5199 if (_M_int.empty())
5200 {
5201 std::vector<_RealType> __tmp(2);
5202 __tmp[1] = _RealType(1);
5203 return __tmp;
5204 }
5205 else
5206 return _M_int;
5207 }
5208
5209 std::vector<double>
5210 densities() const
5211 { return _M_den.empty() ? std::vector<double>(2, 1.0) : _M_den; }
5212
5213 friend bool
5214 operator==(const param_type& __p1, const param_type& __p2)
5215 { return (__p1._M_int == __p2._M_int
5216 && __p1._M_den == __p2._M_den); }
5217
5218 private:
5219 void
5220 _M_initialize();
5221
5222 std::vector<_RealType> _M_int;
5223 std::vector<double> _M_den;
5224 std::vector<double> _M_cp;
5225 std::vector<double> _M_m;
5226 };
5227
5228 explicit
5229 piecewise_linear_distribution()
5230 : _M_param()
5231 { }
5232
5233 template<typename _InputIteratorB, typename _InputIteratorW>
5234 piecewise_linear_distribution(_InputIteratorB __bfirst,
5235 _InputIteratorB __bend,
5236 _InputIteratorW __wbegin)
5237 : _M_param(__bfirst, __bend, __wbegin)
5238 { }
5239
5240 template<typename _Func>
5241 piecewise_linear_distribution(initializer_list<_RealType> __bl,
5242 _Func __fw)
5243 : _M_param(__bl, __fw)
5244 { }
5245
5246 template<typename _Func>
5247 piecewise_linear_distribution(size_t __nw,
5248 _RealType __xmin, _RealType __xmax,
5249 _Func __fw)
5250 : _M_param(__nw, __xmin, __xmax, __fw)
5251 { }
5252
5253 explicit
5254 piecewise_linear_distribution(const param_type& __p)
5255 : _M_param(__p)
5256 { }
5257
5258 /**
5259 * Resets the distribution state.
5260 */
5261 void
5262 reset()
5263 { }
5264
5265 /**
5266 * @brief Return the intervals of the distribution.
5267 */
5268 std::vector<_RealType>
5269 intervals() const
5270 {
5271 if (_M_param._M_int.empty())
5272 {
5273 std::vector<_RealType> __tmp(2);
5274 __tmp[1] = _RealType(1);
5275 return __tmp;
5276 }
5277 else
5278 return _M_param._M_int;
5279 }
5280
5281 /**
5282 * @brief Return a vector of the probability densities of the
5283 * distribution.
5284 */
5285 std::vector<double>
5286 densities() const
5287 {
5288 return _M_param._M_den.empty()
5289 ? std::vector<double>(2, 1.0) : _M_param._M_den;
5290 }
5291
5292 /**
5293 * @brief Returns the parameter set of the distribution.
5294 */
5295 param_type
5296 param() const
5297 { return _M_param; }
5298
5299 /**
5300 * @brief Sets the parameter set of the distribution.
5301 * @param __param The new parameter set of the distribution.
5302 */
5303 void
5304 param(const param_type& __param)
5305 { _M_param = __param; }
5306
5307 /**
5308 * @brief Returns the greatest lower bound value of the distribution.
5309 */
5310 result_type
5311 min() const
5312 {
5313 return _M_param._M_int.empty()
5314 ? result_type(0) : _M_param._M_int.front();
5315 }
5316
5317 /**
5318 * @brief Returns the least upper bound value of the distribution.
5319 */
5320 result_type
5321 max() const
5322 {
5323 return _M_param._M_int.empty()
5324 ? result_type(1) : _M_param._M_int.back();
5325 }
5326
5327 /**
5328 * @brief Generating functions.
5329 */
5330 template<typename _UniformRandomNumberGenerator>
5331 result_type
5332 operator()(_UniformRandomNumberGenerator& __urng)
5333 { return this->operator()(__urng, this->param()); }
5334
5335 template<typename _UniformRandomNumberGenerator>
5336 result_type
5337 operator()(_UniformRandomNumberGenerator& __urng,
5338 const param_type& __p);
5339
5340 /**
5341 * @brief Inserts a %piecewise_linear_distribution random number
5342 * distribution @p __x into the output stream @p __os.
5343 *
5344 * @param __os An output stream.
5345 * @param __x A %piecewise_linear_distribution random number
5346 * distribution.
5347 *
5348 * @returns The output stream with the state of @p __x inserted or in
5349 * an error state.
5350 */
5351 template<typename _RealType1, typename _CharT, typename _Traits>
5352 friend std::basic_ostream<_CharT, _Traits>&
5353 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
5354 const std::piecewise_linear_distribution<_RealType1>& __x);
5355
5356 /**
5357 * @brief Extracts a %piecewise_linear_distribution random number
5358 * distribution @p __x from the input stream @p __is.
5359 *
5360 * @param __is An input stream.
5361 * @param __x A %piecewise_linear_distribution random number
5362 * generator engine.
5363 *
5364 * @returns The input stream with @p __x extracted or in an error
5365 * state.
5366 */
5367 template<typename _RealType1, typename _CharT, typename _Traits>
5368 friend std::basic_istream<_CharT, _Traits>&
5369 operator>>(std::basic_istream<_CharT, _Traits>& __is,
5370 std::piecewise_linear_distribution<_RealType1>& __x);
5371
5372 private:
5373 param_type _M_param;
5374 };
5375
5376 /**
5377 * @brief Return true if two piecewise linear distributions have the
5378 * same parameters.
5379 */
5380 template<typename _RealType>
5381 inline bool
5382 operator==(const std::piecewise_linear_distribution<_RealType>& __d1,
5383 const std::piecewise_linear_distribution<_RealType>& __d2)
5384 { return __d1.param() == __d2.param(); }
5385
5386 /**
5387 * @brief Return true if two piecewise linear distributions have
5388 * different parameters.
5389 */
5390 template<typename _RealType>
5391 inline bool
5392 operator!=(const std::piecewise_linear_distribution<_RealType>& __d1,
5393 const std::piecewise_linear_distribution<_RealType>& __d2)
5394 { return !(__d1 == __d2); }
5395
5396
5397 /* @} */ // group random_distributions_poisson
5398
5399 /* @} */ // group random_distributions
5400
5401 /**
5402 * @addtogroup random_utilities Random Number Utilities
5403 * @ingroup random
5404 * @{
5405 */
5406
5407 /**
5408 * @brief The seed_seq class generates sequences of seeds for random
5409 * number generators.
5410 */
5411 class seed_seq
5412 {
5413
5414 public:
5415 /** The type of the seed vales. */
5416 typedef uint_least32_t result_type;
5417
5418 /** Default constructor. */
5419 seed_seq()
5420 : _M_v()
5421 { }
5422
5423 template<typename _IntType>
5424 seed_seq(std::initializer_list<_IntType> il);
5425
5426 template<typename _InputIterator>
5427 seed_seq(_InputIterator __begin, _InputIterator __end);
5428
5429 // generating functions
5430 template<typename _RandomAccessIterator>
5431 void
5432 generate(_RandomAccessIterator __begin, _RandomAccessIterator __end);
5433
5434 // property functions
5435 size_t size() const
5436 { return _M_v.size(); }
5437
5438 template<typename OutputIterator>
5439 void
5440 param(OutputIterator __dest) const
5441 { std::copy(_M_v.begin(), _M_v.end(), __dest); }
5442
5443 private:
5444 ///
5445 std::vector<result_type> _M_v;
5446 };
5447
5448 /* @} */ // group random_utilities
5449
5450 /* @} */ // group random
5451
5452 _GLIBCXX_END_NAMESPACE_VERSION
5453 } // namespace std
5454
5455 #endif