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