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