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