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