]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/tr1/random.h
testsuite: Rename a test
[thirdparty/gcc.git] / libstdc++-v3 / include / tr1 / random.h
CommitLineData
8e79468d
BK
1// random number generation -*- C++ -*-
2
6441eb6d 3// Copyright (C) 2009-2025 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 tr1/random.h
27 * This is an internal header file, included by other library headers.
f910786b 28 * Do not attempt to use it directly. @headername{tr1/random}
8e79468d
BK
29 */
30
31#ifndef _GLIBCXX_TR1_RANDOM_H
32#define _GLIBCXX_TR1_RANDOM_H 1
33
63a598de 34#ifdef _GLIBCXX_SYSHDR
8e79468d 35#pragma GCC system_header
63a598de 36#endif
8e79468d 37
12ffa228 38namespace std _GLIBCXX_VISIBILITY(default)
8e79468d 39{
4a15d842
FD
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
41
8e79468d
BK
42namespace tr1
43{
8e79468d
BK
44 // [5.1] Random number generation
45
46 /**
47 * @addtogroup tr1_random Random Number Generation
48 * A facility for generating random numbers on selected distributions.
49 * @{
50 */
51
52 /*
53 * Implementation-space details.
54 */
55 namespace __detail
56 {
45ab93d9 57 template<typename _UIntType, int __w,
8e79468d
BK
58 bool = __w < std::numeric_limits<_UIntType>::digits>
59 struct _Shift
60 { static const _UIntType __value = 0; };
61
62 template<typename _UIntType, int __w>
63 struct _Shift<_UIntType, __w, true>
64 { static const _UIntType __value = _UIntType(1) << __w; };
65
66 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
67 struct _Mod;
68
69 // Dispatch based on modulus value to prevent divide-by-zero compile-time
70 // errors when m == 0.
71 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
72 inline _Tp
73 __mod(_Tp __x)
74 { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
75
76 typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
77 unsigned, unsigned long>::__type _UInt32Type;
78
79 /*
80 * An adaptor class for converting the output of any Generator into
81 * the input for a specific Distribution.
82 */
83 template<typename _Engine, typename _Distribution>
84 struct _Adaptor
45ab93d9 85 {
b6adc625
JW
86 typedef typename _Engine::result_type _Engine_result_type;
87 typedef typename _Distribution::input_type result_type;
8e79468d
BK
88
89 public:
90 _Adaptor(const _Engine& __g)
91 : _M_g(__g) { }
92
93 result_type
94 min() const
95 {
96 result_type __return_value;
97 if (is_integral<_Engine_result_type>::value
98 && is_integral<result_type>::value)
99 __return_value = _M_g.min();
100 else
101 __return_value = result_type(0);
102 return __return_value;
103 }
104
105 result_type
106 max() const
107 {
108 result_type __return_value;
109 if (is_integral<_Engine_result_type>::value
110 && is_integral<result_type>::value)
111 __return_value = _M_g.max();
112 else if (!is_integral<result_type>::value)
113 __return_value = result_type(1);
114 else
115 __return_value = std::numeric_limits<result_type>::max() - 1;
116 return __return_value;
117 }
118
119 /*
120 * Converts a value generated by the adapted random number generator
121 * into a value in the input domain for the dependent random number
122 * distribution.
123 *
124 * Because the type traits are compile time constants only the
125 * appropriate clause of the if statements will actually be emitted
126 * by the compiler.
127 */
128 result_type
129 operator()()
130 {
131 result_type __return_value;
132 if (is_integral<_Engine_result_type>::value
133 && is_integral<result_type>::value)
134 __return_value = _M_g();
135 else if (!is_integral<_Engine_result_type>::value
136 && !is_integral<result_type>::value)
137 __return_value = result_type(_M_g() - _M_g.min())
138 / result_type(_M_g.max() - _M_g.min());
139 else if (is_integral<_Engine_result_type>::value
140 && !is_integral<result_type>::value)
141 __return_value = result_type(_M_g() - _M_g.min())
142 / result_type(_M_g.max() - _M_g.min() + result_type(1));
143 else
45ab93d9 144 __return_value = (((_M_g() - _M_g.min())
8e79468d
BK
145 / (_M_g.max() - _M_g.min()))
146 * std::numeric_limits<result_type>::max());
147 return __return_value;
148 }
149
8e79468d
BK
150 _Engine _M_g;
151 };
8e79468d
BK
152 } // namespace __detail
153
154 /**
155 * Produces random numbers on a given distribution function using a
156 * non-uniform random number generation engine.
157 *
158 * @todo the engine_value_type needs to be studied more carefully.
159 */
160 template<typename _Engine, typename _Dist>
161 class variate_generator
162 {
b6adc625
JW
163 template<typename _Eng>
164 struct _Value
165 {
166 typedef _Eng type;
167
168 static const _Eng&
169 _S_ref(const _Eng& __e) { return __e; }
170 };
171
172 template<typename _Eng>
173 struct _Value<_Eng*>
174 {
175 typedef _Eng type;
176
177 __attribute__((__nonnull__))
178 static const _Eng&
179 _S_ref(const _Eng* __e) { return *__e; }
180 };
181
182 template<typename _Eng>
183 struct _Value<_Eng&>
184 {
185 typedef _Eng type;
186
187 static const _Eng&
188 _S_ref(const _Eng& __e) { return __e; }
189 };
8e79468d
BK
190
191 public:
192 typedef _Engine engine_type;
b6adc625 193 typedef typename _Value<_Engine>::type engine_value_type;
8e79468d
BK
194 typedef _Dist distribution_type;
195 typedef typename _Dist::result_type result_type;
196
b6adc625
JW
197 // Concept requirements.
198 __glibcxx_class_requires(engine_value_type, _CopyConstructibleConcept)
199 // __glibcxx_class_requires(_Engine, _EngineConcept)
200 // __glibcxx_class_requires(_Dist, _EngineConcept)
201
8e79468d
BK
202 // tr1:5.1.1 table 5.1 requirement
203 typedef typename __gnu_cxx::__enable_if<
204 is_arithmetic<result_type>::value, result_type>::__type _IsValidType;
205
206 /**
207 * Constructs a variate generator with the uniform random number
208 * generator @p __eng for the random distribution @p __dist.
209 *
210 * @throws Any exceptions which may thrown by the copy constructors of
211 * the @p _Engine or @p _Dist objects.
212 */
213 variate_generator(engine_type __eng, distribution_type __dist)
b6adc625 214 : _M_engine(_Value<_Engine>::_S_ref(__eng)), _M_dist(__dist) { }
8e79468d
BK
215
216 /**
217 * Gets the next generated value on the distribution.
218 */
219 result_type
220 operator()()
221 { return _M_dist(_M_engine); }
222
223 /**
224 * WTF?
225 */
226 template<typename _Tp>
227 result_type
228 operator()(_Tp __value)
229 { return _M_dist(_M_engine, __value); }
230
231 /**
232 * Gets a reference to the underlying uniform random number generator
233 * object.
234 */
235 engine_value_type&
236 engine()
b6adc625 237 { return _M_engine._M_g; }
8e79468d
BK
238
239 /**
240 * Gets a const reference to the underlying uniform random number
241 * generator object.
242 */
243 const engine_value_type&
244 engine() const
b6adc625 245 { return _M_engine._M_g; }
8e79468d
BK
246
247 /**
248 * Gets a reference to the underlying random distribution.
249 */
250 distribution_type&
251 distribution()
252 { return _M_dist; }
253
254 /**
255 * Gets a const reference to the underlying random distribution.
256 */
257 const distribution_type&
258 distribution() const
259 { return _M_dist; }
260
261 /**
262 * Gets the closed lower bound of the distribution interval.
263 */
264 result_type
265 min() const
266 { return this->distribution().min(); }
267
268 /**
269 * Gets the closed upper bound of the distribution interval.
270 */
271 result_type
272 max() const
273 { return this->distribution().max(); }
274
275 private:
b6adc625 276 __detail::_Adaptor<engine_value_type, _Dist> _M_engine;
8e79468d
BK
277 distribution_type _M_dist;
278 };
279
280
281 /**
282 * @addtogroup tr1_random_generators Random Number Generators
283 * @ingroup tr1_random
284 *
285 * These classes define objects which provide random or pseudorandom
286 * numbers, either from a discrete or a continuous interval. The
287 * random number generator supplied as a part of this library are
288 * all uniform random number generators which provide a sequence of
289 * random number uniformly distributed over their range.
290 *
291 * A number generator is a function object with an operator() that
292 * takes zero arguments and returns a number.
293 *
294 * A compliant random number generator must satisfy the following
295 * requirements. <table border=1 cellpadding=10 cellspacing=0>
296 * <caption align=top>Random Number Generator Requirements</caption>
297 * <tr><td>To be documented.</td></tr> </table>
45ab93d9 298 *
8e79468d
BK
299 * @{
300 */
301
302 /**
303 * @brief A model of a linear congruential random number generator.
304 *
305 * A random number generator that produces pseudorandom numbers using the
306 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
307 *
308 * The template parameter @p _UIntType must be an unsigned integral type
309 * large enough to store values up to (__m-1). If the template parameter
310 * @p __m is 0, the modulus @p __m used is
311 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
312 * parameters @p __a and @p __c must be less than @p __m.
313 *
314 * The size of the state is @f$ 1 @f$.
315 */
316 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
317 class linear_congruential
318 {
319 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
320 // __glibcpp_class_requires(__a < __m && __c < __m)
321
322 public:
323 /** The type of the generated random value. */
324 typedef _UIntType result_type;
325
326 /** The multiplier. */
327 static const _UIntType multiplier = __a;
328 /** An increment. */
329 static const _UIntType increment = __c;
330 /** The modulus. */
331 static const _UIntType modulus = __m;
332
333 /**
334 * Constructs a %linear_congruential random number generator engine with
335 * seed @p __s. The default seed value is 1.
336 *
337 * @param __s The initial seed value.
338 */
339 explicit
340 linear_congruential(unsigned long __x0 = 1)
341 { this->seed(__x0); }
342
343 /**
344 * Constructs a %linear_congruential random number generator engine
345 * seeded from the generator function @p __g.
346 *
347 * @param __g The seed generator function.
348 */
349 template<class _Gen>
350 linear_congruential(_Gen& __g)
351 { this->seed(__g); }
352
353 /**
354 * Reseeds the %linear_congruential random number generator engine
355 * sequence to the seed @g __s.
356 *
357 * @param __s The new seed.
358 */
359 void
360 seed(unsigned long __s = 1);
361
362 /**
363 * Reseeds the %linear_congruential random number generator engine
364 * sequence using values from the generator function @p __g.
365 *
366 * @param __g the seed generator function.
367 */
368 template<class _Gen>
369 void
370 seed(_Gen& __g)
371 { seed(__g, typename is_fundamental<_Gen>::type()); }
372
373 /**
374 * Gets the smallest possible value in the output range.
375 *
376 * The minimum depends on the @p __c parameter: if it is zero, the
377 * minimum generated must be > 0, otherwise 0 is allowed.
378 */
379 result_type
380 min() const
381 { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
382
383 /**
384 * Gets the largest possible value in the output range.
385 */
386 result_type
387 max() const
388 { return __m - 1; }
389
390 /**
391 * Gets the next random number in the sequence.
392 */
393 result_type
394 operator()();
395
396 /**
397 * Compares two linear congruential random number generator
398 * objects of the same type for equality.
45ab93d9 399 *
8e79468d
BK
400 * @param __lhs A linear congruential random number generator object.
401 * @param __rhs Another linear congruential random number generator obj.
402 *
403 * @returns true if the two objects are equal, false otherwise.
404 */
405 friend bool
406 operator==(const linear_congruential& __lhs,
407 const linear_congruential& __rhs)
408 { return __lhs._M_x == __rhs._M_x; }
409
410 /**
411 * Compares two linear congruential random number generator
412 * objects of the same type for inequality.
413 *
414 * @param __lhs A linear congruential random number generator object.
415 * @param __rhs Another linear congruential random number generator obj.
416 *
417 * @returns true if the two objects are not equal, false otherwise.
418 */
419 friend bool
420 operator!=(const linear_congruential& __lhs,
421 const linear_congruential& __rhs)
422 { return !(__lhs == __rhs); }
423
424 /**
425 * Writes the textual representation of the state x(i) of x to @p __os.
426 *
427 * @param __os The output stream.
428 * @param __lcr A % linear_congruential random number generator.
429 * @returns __os.
430 */
431 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
432 _UIntType1 __m1,
433 typename _CharT, typename _Traits>
434 friend std::basic_ostream<_CharT, _Traits>&
435 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
436 const linear_congruential<_UIntType1, __a1, __c1,
437 __m1>& __lcr);
438
439 /**
440 * Sets the state of the engine by reading its textual
441 * representation from @p __is.
442 *
443 * The textual representation must have been previously written using an
444 * output stream whose imbued locale and whose type's template
445 * specialization arguments _CharT and _Traits were the same as those of
446 * @p __is.
447 *
448 * @param __is The input stream.
449 * @param __lcr A % linear_congruential random number generator.
450 * @returns __is.
451 */
452 template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
453 _UIntType1 __m1,
454 typename _CharT, typename _Traits>
455 friend std::basic_istream<_CharT, _Traits>&
456 operator>>(std::basic_istream<_CharT, _Traits>& __is,
457 linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
458
459 private:
460 template<class _Gen>
461 void
462 seed(_Gen& __g, true_type)
463 { return seed(static_cast<unsigned long>(__g)); }
464
465 template<class _Gen>
466 void
467 seed(_Gen& __g, false_type);
468
469 _UIntType _M_x;
470 };
471
472 /**
473 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
474 */
475 typedef linear_congruential<unsigned long, 16807, 0, 2147483647> minstd_rand0;
476
477 /**
478 * An alternative LCR (Lehmer Generator function) .
479 */
480 typedef linear_congruential<unsigned long, 48271, 0, 2147483647> minstd_rand;
481
482
483 /**
484 * A generalized feedback shift register discrete random number generator.
485 *
486 * This algorithm avoids multiplication and division and is designed to be
487 * friendly to a pipelined architecture. If the parameters are chosen
488 * correctly, this generator will produce numbers with a very long period and
489 * fairly good apparent entropy, although still not cryptographically strong.
490 *
491 * The best way to use this generator is with the predefined mt19937 class.
492 *
493 * This algorithm was originally invented by Makoto Matsumoto and
494 * Takuji Nishimura.
495 *
496 * @var word_size The number of bits in each element of the state vector.
497 * @var state_size The degree of recursion.
498 * @var shift_size The period parameter.
499 * @var mask_bits The separation point bit index.
500 * @var parameter_a The last row of the twist matrix.
501 * @var output_u The first right-shift tempering matrix parameter.
502 * @var output_s The first left-shift tempering matrix parameter.
503 * @var output_b The first left-shift tempering matrix mask.
504 * @var output_t The second left-shift tempering matrix parameter.
505 * @var output_c The second left-shift tempering matrix mask.
506 * @var output_l The second right-shift tempering matrix parameter.
507 */
508 template<class _UIntType, int __w, int __n, int __m, int __r,
509 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
510 _UIntType __c, int __l>
511 class mersenne_twister
512 {
513 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
514
515 public:
516 // types
517 typedef _UIntType result_type;
518
519 // parameter values
520 static const int word_size = __w;
521 static const int state_size = __n;
522 static const int shift_size = __m;
523 static const int mask_bits = __r;
524 static const _UIntType parameter_a = __a;
525 static const int output_u = __u;
526 static const int output_s = __s;
527 static const _UIntType output_b = __b;
528 static const int output_t = __t;
529 static const _UIntType output_c = __c;
530 static const int output_l = __l;
531
532 // constructors and member function
533 mersenne_twister()
534 { seed(); }
535
536 explicit
537 mersenne_twister(unsigned long __value)
538 { seed(__value); }
539
540 template<class _Gen>
541 mersenne_twister(_Gen& __g)
542 { seed(__g); }
543
544 void
545 seed()
546 { seed(5489UL); }
547
548 void
549 seed(unsigned long __value);
550
551 template<class _Gen>
552 void
553 seed(_Gen& __g)
554 { seed(__g, typename is_fundamental<_Gen>::type()); }
555
556 result_type
557 min() const
57c51668 558 { return 0; }
8e79468d
BK
559
560 result_type
561 max() const
562 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
563
564 result_type
565 operator()();
566
567 /**
568 * Compares two % mersenne_twister random number generator objects of
569 * the same type for equality.
570 *
571 * @param __lhs A % mersenne_twister random number generator object.
572 * @param __rhs Another % mersenne_twister random number generator
573 * object.
574 *
575 * @returns true if the two objects are equal, false otherwise.
576 */
577 friend bool
578 operator==(const mersenne_twister& __lhs,
579 const mersenne_twister& __rhs)
580 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
581
582 /**
583 * Compares two % mersenne_twister random number generator objects of
584 * the same type for inequality.
585 *
586 * @param __lhs A % mersenne_twister random number generator object.
587 * @param __rhs Another % mersenne_twister random number generator
588 * object.
589 *
590 * @returns true if the two objects are not equal, false otherwise.
591 */
592 friend bool
593 operator!=(const mersenne_twister& __lhs,
594 const mersenne_twister& __rhs)
595 { return !(__lhs == __rhs); }
596
597 /**
598 * Inserts the current state of a % mersenne_twister random number
599 * generator engine @p __x into the output stream @p __os.
600 *
601 * @param __os An output stream.
602 * @param __x A % mersenne_twister random number generator engine.
603 *
604 * @returns The output stream with the state of @p __x inserted or in
605 * an error state.
606 */
607 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
608 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
609 _UIntType1 __c1, int __l1,
610 typename _CharT, typename _Traits>
611 friend std::basic_ostream<_CharT, _Traits>&
612 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
613 const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
614 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
615
616 /**
617 * Extracts the current state of a % mersenne_twister random number
618 * generator engine @p __x from the input stream @p __is.
619 *
620 * @param __is An input stream.
621 * @param __x A % mersenne_twister random number generator engine.
622 *
623 * @returns The input stream with the state of @p __x extracted or in
624 * an error state.
625 */
626 template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
627 _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
628 _UIntType1 __c1, int __l1,
629 typename _CharT, typename _Traits>
630 friend std::basic_istream<_CharT, _Traits>&
631 operator>>(std::basic_istream<_CharT, _Traits>& __is,
632 mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
633 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
634
635 private:
636 template<class _Gen>
637 void
638 seed(_Gen& __g, true_type)
639 { return seed(static_cast<unsigned long>(__g)); }
640
641 template<class _Gen>
642 void
643 seed(_Gen& __g, false_type);
644
645 _UIntType _M_x[state_size];
646 int _M_p;
647 };
648
649 /**
650 * The classic Mersenne Twister.
651 *
652 * Reference:
2a60a9f6
BK
653 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
654 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
8e79468d
BK
655 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
656 */
657 typedef mersenne_twister<
658 unsigned long, 32, 624, 397, 31,
659 0x9908b0dful, 11, 7,
660 0x9d2c5680ul, 15,
661 0xefc60000ul, 18
662 > mt19937;
663
664
665 /**
666 * @brief The Marsaglia-Zaman generator.
45ab93d9 667 *
8e79468d
BK
668 * This is a model of a Generalized Fibonacci discrete random number
669 * generator, sometimes referred to as the SWC generator.
670 *
671 * A discrete random number generator that produces pseudorandom
672 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
673 * carry_{i-1}) \bmod m @f$.
674 *
675 * The size of the state is @f$ r @f$
676 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
677 *
2a60a9f6
BK
678 * N1688[4.13] says <em>the template parameter _IntType shall denote
679 * an integral type large enough to store values up to m</em>.
8e79468d
BK
680 *
681 * @var _M_x The state of the generator. This is a ring buffer.
682 * @var _M_carry The carry.
683 * @var _M_p Current index of x(i - r).
684 */
685 template<typename _IntType, _IntType __m, int __s, int __r>
686 class subtract_with_carry
687 {
688 __glibcxx_class_requires(_IntType, _IntegerConcept)
689
690 public:
691 /** The type of the generated random value. */
692 typedef _IntType result_type;
45ab93d9 693
8e79468d
BK
694 // parameter values
695 static const _IntType modulus = __m;
696 static const int long_lag = __r;
697 static const int short_lag = __s;
698
699 /**
700 * Constructs a default-initialized % subtract_with_carry random number
701 * generator.
702 */
703 subtract_with_carry()
704 { this->seed(); }
705
706 /**
707 * Constructs an explicitly seeded % subtract_with_carry random number
708 * generator.
709 */
710 explicit
711 subtract_with_carry(unsigned long __value)
712 { this->seed(__value); }
713
714 /**
715 * Constructs a %subtract_with_carry random number generator engine
716 * seeded from the generator function @p __g.
717 *
718 * @param __g The seed generator function.
719 */
720 template<class _Gen>
721 subtract_with_carry(_Gen& __g)
722 { this->seed(__g); }
723
724 /**
725 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
726 *
727 * N1688[4.19] modifies this as follows. If @p __value == 0,
728 * sets value to 19780503. In any case, with a linear
729 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
730 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
731 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
732 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
733 * set carry to 1, otherwise sets carry to 0.
734 */
735 void
736 seed(unsigned long __value = 19780503);
737
738 /**
739 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
740 * random number generator.
741 */
742 template<class _Gen>
743 void
744 seed(_Gen& __g)
745 { seed(__g, typename is_fundamental<_Gen>::type()); }
746
747 /**
748 * Gets the inclusive minimum value of the range of random integers
749 * returned by this generator.
750 */
751 result_type
752 min() const
753 { return 0; }
754
755 /**
756 * Gets the inclusive maximum value of the range of random integers
757 * returned by this generator.
758 */
759 result_type
760 max() const
761 { return this->modulus - 1; }
762
763 /**
764 * Gets the next random number in the sequence.
765 */
766 result_type
767 operator()();
768
769 /**
770 * Compares two % subtract_with_carry random number generator objects of
771 * the same type for equality.
772 *
773 * @param __lhs A % subtract_with_carry random number generator object.
774 * @param __rhs Another % subtract_with_carry random number generator
775 * object.
776 *
777 * @returns true if the two objects are equal, false otherwise.
778 */
779 friend bool
780 operator==(const subtract_with_carry& __lhs,
781 const subtract_with_carry& __rhs)
782 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
783
784 /**
785 * Compares two % subtract_with_carry random number generator objects of
786 * the same type for inequality.
787 *
788 * @param __lhs A % subtract_with_carry random number generator object.
789 * @param __rhs Another % subtract_with_carry random number generator
790 * object.
791 *
792 * @returns true if the two objects are not equal, false otherwise.
793 */
794 friend bool
795 operator!=(const subtract_with_carry& __lhs,
796 const subtract_with_carry& __rhs)
797 { return !(__lhs == __rhs); }
798
799 /**
800 * Inserts the current state of a % subtract_with_carry random number
801 * generator engine @p __x into the output stream @p __os.
802 *
803 * @param __os An output stream.
804 * @param __x A % subtract_with_carry random number generator engine.
805 *
806 * @returns The output stream with the state of @p __x inserted or in
807 * an error state.
808 */
809 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
810 typename _CharT, typename _Traits>
811 friend std::basic_ostream<_CharT, _Traits>&
812 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
813 const subtract_with_carry<_IntType1, __m1, __s1,
814 __r1>& __x);
815
816 /**
817 * Extracts the current state of a % subtract_with_carry random number
818 * generator engine @p __x from the input stream @p __is.
819 *
820 * @param __is An input stream.
821 * @param __x A % subtract_with_carry random number generator engine.
822 *
823 * @returns The input stream with the state of @p __x extracted or in
824 * an error state.
825 */
826 template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
827 typename _CharT, typename _Traits>
828 friend std::basic_istream<_CharT, _Traits>&
829 operator>>(std::basic_istream<_CharT, _Traits>& __is,
830 subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
831
832 private:
833 template<class _Gen>
834 void
835 seed(_Gen& __g, true_type)
836 { return seed(static_cast<unsigned long>(__g)); }
837
838 template<class _Gen>
839 void
840 seed(_Gen& __g, false_type);
841
842 typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
843
844 _UIntType _M_x[long_lag];
845 _UIntType _M_carry;
846 int _M_p;
847 };
848
849
850 /**
851 * @brief The Marsaglia-Zaman generator (floats version).
852 *
853 * @var _M_x The state of the generator. This is a ring buffer.
854 * @var _M_carry The carry.
855 * @var _M_p Current index of x(i - r).
45ab93d9 856 * @var _M_npows Precomputed negative powers of 2.
8e79468d
BK
857 */
858 template<typename _RealType, int __w, int __s, int __r>
859 class subtract_with_carry_01
860 {
861 public:
862 /** The type of the generated random value. */
863 typedef _RealType result_type;
45ab93d9 864
8e79468d
BK
865 // parameter values
866 static const int word_size = __w;
867 static const int long_lag = __r;
868 static const int short_lag = __s;
869
870 /**
871 * Constructs a default-initialized % subtract_with_carry_01 random
872 * number generator.
873 */
874 subtract_with_carry_01()
875 {
876 this->seed();
877 _M_initialize_npows();
878 }
879
880 /**
881 * Constructs an explicitly seeded % subtract_with_carry_01 random number
882 * generator.
883 */
884 explicit
885 subtract_with_carry_01(unsigned long __value)
886 {
887 this->seed(__value);
888 _M_initialize_npows();
889 }
890
891 /**
892 * Constructs a % subtract_with_carry_01 random number generator engine
893 * seeded from the generator function @p __g.
894 *
895 * @param __g The seed generator function.
896 */
897 template<class _Gen>
898 subtract_with_carry_01(_Gen& __g)
899 {
900 this->seed(__g);
45ab93d9 901 _M_initialize_npows();
8e79468d
BK
902 }
903
904 /**
905 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
906 */
907 void
908 seed(unsigned long __value = 19780503);
909
910 /**
911 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
912 * random number generator.
913 */
914 template<class _Gen>
915 void
916 seed(_Gen& __g)
917 { seed(__g, typename is_fundamental<_Gen>::type()); }
918
919 /**
920 * Gets the minimum value of the range of random floats
921 * returned by this generator.
922 */
923 result_type
924 min() const
925 { return 0.0; }
926
927 /**
928 * Gets the maximum value of the range of random floats
929 * returned by this generator.
930 */
931 result_type
932 max() const
933 { return 1.0; }
934
935 /**
936 * Gets the next random number in the sequence.
937 */
938 result_type
939 operator()();
940
941 /**
942 * Compares two % subtract_with_carry_01 random number generator objects
943 * of the same type for equality.
944 *
945 * @param __lhs A % subtract_with_carry_01 random number
946 * generator object.
947 * @param __rhs Another % subtract_with_carry_01 random number generator
948 * object.
949 *
950 * @returns true if the two objects are equal, false otherwise.
951 */
952 friend bool
953 operator==(const subtract_with_carry_01& __lhs,
954 const subtract_with_carry_01& __rhs)
955 {
956 for (int __i = 0; __i < long_lag; ++__i)
957 if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
958 __rhs._M_x[__i]))
959 return false;
960 return true;
961 }
962
963 /**
964 * Compares two % subtract_with_carry_01 random number generator objects
965 * of the same type for inequality.
966 *
967 * @param __lhs A % subtract_with_carry_01 random number
968 * generator object.
969 *
970 * @param __rhs Another % subtract_with_carry_01 random number generator
971 * object.
972 *
973 * @returns true if the two objects are not equal, false otherwise.
974 */
975 friend bool
976 operator!=(const subtract_with_carry_01& __lhs,
977 const subtract_with_carry_01& __rhs)
978 { return !(__lhs == __rhs); }
979
980 /**
981 * Inserts the current state of a % subtract_with_carry_01 random number
982 * generator engine @p __x into the output stream @p __os.
983 *
984 * @param __os An output stream.
985 * @param __x A % subtract_with_carry_01 random number generator engine.
986 *
987 * @returns The output stream with the state of @p __x inserted or in
988 * an error state.
989 */
990 template<typename _RealType1, int __w1, int __s1, int __r1,
991 typename _CharT, typename _Traits>
992 friend std::basic_ostream<_CharT, _Traits>&
993 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
994 const subtract_with_carry_01<_RealType1, __w1, __s1,
995 __r1>& __x);
996
997 /**
998 * Extracts the current state of a % subtract_with_carry_01 random number
999 * generator engine @p __x from the input stream @p __is.
1000 *
1001 * @param __is An input stream.
1002 * @param __x A % subtract_with_carry_01 random number generator engine.
1003 *
1004 * @returns The input stream with the state of @p __x extracted or in
1005 * an error state.
1006 */
1007 template<typename _RealType1, int __w1, int __s1, int __r1,
1008 typename _CharT, typename _Traits>
1009 friend std::basic_istream<_CharT, _Traits>&
1010 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1011 subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
1012
1013 private:
1014 template<class _Gen>
1015 void
1016 seed(_Gen& __g, true_type)
1017 { return seed(static_cast<unsigned long>(__g)); }
1018
1019 template<class _Gen>
1020 void
1021 seed(_Gen& __g, false_type);
1022
1023 void
1024 _M_initialize_npows();
1025
1026 static const int __n = (__w + 31) / 32;
1027
1028 typedef __detail::_UInt32Type _UInt32Type;
1029 _UInt32Type _M_x[long_lag][__n];
1030 _RealType _M_npows[__n];
1031 _UInt32Type _M_carry;
1032 int _M_p;
1033 };
1034
1035 typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
1036
1037 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1038 // 508. Bad parameters for ranlux64_base_01.
45ab93d9 1039 typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
8e79468d
BK
1040
1041
1042 /**
1043 * Produces random numbers from some base engine by discarding blocks of
1044 * data.
1045 *
1046 * 0 <= @p __r <= @p __p
1047 */
1048 template<class _UniformRandomNumberGenerator, int __p, int __r>
1049 class discard_block
1050 {
1051 // __glibcxx_class_requires(typename base_type::result_type,
1052 // ArithmeticTypeConcept)
1053
1054 public:
1055 /** The type of the underlying generator engine. */
1056 typedef _UniformRandomNumberGenerator base_type;
1057 /** The type of the generated random value. */
1058 typedef typename base_type::result_type result_type;
1059
1060 // parameter values
1061 static const int block_size = __p;
1062 static const int used_block = __r;
1063
1064 /**
1065 * Constructs a default %discard_block engine.
1066 *
1067 * The underlying engine is default constructed as well.
1068 */
1069 discard_block()
1070 : _M_n(0) { }
1071
1072 /**
1073 * Copy constructs a %discard_block engine.
1074 *
1075 * Copies an existing base class random number generator.
1076 * @param rng An existing (base class) engine object.
1077 */
1078 explicit
1079 discard_block(const base_type& __rng)
1080 : _M_b(__rng), _M_n(0) { }
1081
1082 /**
1083 * Seed constructs a %discard_block engine.
1084 *
1085 * Constructs the underlying generator engine seeded with @p __s.
1086 * @param __s A seed value for the base class engine.
1087 */
1088 explicit
1089 discard_block(unsigned long __s)
1090 : _M_b(__s), _M_n(0) { }
1091
1092 /**
1093 * Generator construct a %discard_block engine.
1094 *
1095 * @param __g A seed generator function.
1096 */
1097 template<class _Gen>
1098 discard_block(_Gen& __g)
1099 : _M_b(__g), _M_n(0) { }
1100
1101 /**
1102 * Reseeds the %discard_block object with the default seed for the
1103 * underlying base class generator engine.
1104 */
1105 void seed()
1106 {
1107 _M_b.seed();
1108 _M_n = 0;
1109 }
1110
1111 /**
1112 * Reseeds the %discard_block object with the given seed generator
1113 * function.
1114 * @param __g A seed generator function.
1115 */
1116 template<class _Gen>
1117 void seed(_Gen& __g)
1118 {
1119 _M_b.seed(__g);
1120 _M_n = 0;
1121 }
1122
1123 /**
1124 * Gets a const reference to the underlying generator engine object.
1125 */
1126 const base_type&
1127 base() const
1128 { return _M_b; }
1129
1130 /**
1131 * Gets the minimum value in the generated random number range.
1132 */
1133 result_type
1134 min() const
1135 { return _M_b.min(); }
1136
1137 /**
1138 * Gets the maximum value in the generated random number range.
1139 */
1140 result_type
1141 max() const
1142 { return _M_b.max(); }
1143
1144 /**
1145 * Gets the next value in the generated random number sequence.
1146 */
1147 result_type
1148 operator()();
1149
1150 /**
1151 * Compares two %discard_block random number generator objects of
1152 * the same type for equality.
1153 *
1154 * @param __lhs A %discard_block random number generator object.
1155 * @param __rhs Another %discard_block random number generator
1156 * object.
1157 *
1158 * @returns true if the two objects are equal, false otherwise.
1159 */
1160 friend bool
1161 operator==(const discard_block& __lhs, const discard_block& __rhs)
1162 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
1163
1164 /**
1165 * Compares two %discard_block random number generator objects of
1166 * the same type for inequality.
1167 *
1168 * @param __lhs A %discard_block random number generator object.
1169 * @param __rhs Another %discard_block random number generator
1170 * object.
1171 *
1172 * @returns true if the two objects are not equal, false otherwise.
1173 */
1174 friend bool
1175 operator!=(const discard_block& __lhs, const discard_block& __rhs)
1176 { return !(__lhs == __rhs); }
1177
1178 /**
1179 * Inserts the current state of a %discard_block random number
1180 * generator engine @p __x into the output stream @p __os.
1181 *
1182 * @param __os An output stream.
1183 * @param __x A %discard_block random number generator engine.
1184 *
1185 * @returns The output stream with the state of @p __x inserted or in
1186 * an error state.
1187 */
1188 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1189 typename _CharT, typename _Traits>
1190 friend std::basic_ostream<_CharT, _Traits>&
1191 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1192 const discard_block<_UniformRandomNumberGenerator1,
1193 __p1, __r1>& __x);
1194
1195 /**
1196 * Extracts the current state of a % subtract_with_carry random number
1197 * generator engine @p __x from the input stream @p __is.
1198 *
1199 * @param __is An input stream.
1200 * @param __x A %discard_block random number generator engine.
1201 *
1202 * @returns The input stream with the state of @p __x extracted or in
1203 * an error state.
1204 */
1205 template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
1206 typename _CharT, typename _Traits>
1207 friend std::basic_istream<_CharT, _Traits>&
1208 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1209 discard_block<_UniformRandomNumberGenerator1,
1210 __p1, __r1>& __x);
1211
1212 private:
1213 base_type _M_b;
1214 int _M_n;
1215 };
1216
1217
1218 /**
1219 * James's luxury-level-3 integer adaptation of Luescher's generator.
1220 */
1221 typedef discard_block<
1222 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1223 223,
1224 24
1225 > ranlux3;
1226
1227 /**
1228 * James's luxury-level-4 integer adaptation of Luescher's generator.
1229 */
1230 typedef discard_block<
1231 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1232 389,
1233 24
1234 > ranlux4;
1235
1236 typedef discard_block<
1237 subtract_with_carry_01<float, 24, 10, 24>,
1238 223,
1239 24
1240 > ranlux3_01;
1241
1242 typedef discard_block<
1243 subtract_with_carry_01<float, 24, 10, 24>,
1244 389,
1245 24
1246 > ranlux4_01;
1247
1248
1249 /**
1250 * A random number generator adaptor class that combines two random number
1251 * generator engines into a single output sequence.
1252 */
1253 template<class _UniformRandomNumberGenerator1, int __s1,
1254 class _UniformRandomNumberGenerator2, int __s2>
1255 class xor_combine
1256 {
1257 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1258 // result_type, ArithmeticTypeConcept)
1259 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1260 // result_type, ArithmeticTypeConcept)
1261
1262 public:
1263 /** The type of the first underlying generator engine. */
1264 typedef _UniformRandomNumberGenerator1 base1_type;
1265 /** The type of the second underlying generator engine. */
1266 typedef _UniformRandomNumberGenerator2 base2_type;
1267
1268 private:
1269 typedef typename base1_type::result_type _Result_type1;
1270 typedef typename base2_type::result_type _Result_type2;
1271
1272 public:
1273 /** The type of the generated random value. */
1274 typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1)
1275 > sizeof(_Result_type2)),
1276 _Result_type1, _Result_type2>::__type result_type;
1277
1278 // parameter values
1279 static const int shift1 = __s1;
1280 static const int shift2 = __s2;
1281
1282 // constructors and member function
1283 xor_combine()
45ab93d9 1284 : _M_b1(), _M_b2()
8e79468d
BK
1285 { _M_initialize_max(); }
1286
1287 xor_combine(const base1_type& __rng1, const base2_type& __rng2)
1288 : _M_b1(__rng1), _M_b2(__rng2)
1289 { _M_initialize_max(); }
1290
1291 xor_combine(unsigned long __s)
1292 : _M_b1(__s), _M_b2(__s + 1)
1293 { _M_initialize_max(); }
1294
1295 template<class _Gen>
1296 xor_combine(_Gen& __g)
1297 : _M_b1(__g), _M_b2(__g)
1298 { _M_initialize_max(); }
1299
1300 void
1301 seed()
1302 {
1303 _M_b1.seed();
1304 _M_b2.seed();
1305 }
1306
1307 template<class _Gen>
1308 void
1309 seed(_Gen& __g)
1310 {
1311 _M_b1.seed(__g);
1312 _M_b2.seed(__g);
1313 }
1314
1315 const base1_type&
1316 base1() const
1317 { return _M_b1; }
1318
1319 const base2_type&
1320 base2() const
1321 { return _M_b2; }
1322
1323 result_type
1324 min() const
1325 { return 0; }
1326
1327 result_type
1328 max() const
1329 { return _M_max; }
1330
1331 /**
1332 * Gets the next random number in the sequence.
1333 */
1334 // NB: Not exactly the TR1 formula, per N2079 instead.
1335 result_type
1336 operator()()
1337 {
1338 return ((result_type(_M_b1() - _M_b1.min()) << shift1)
1339 ^ (result_type(_M_b2() - _M_b2.min()) << shift2));
1340 }
1341
1342 /**
1343 * Compares two %xor_combine random number generator objects of
1344 * the same type for equality.
1345 *
1346 * @param __lhs A %xor_combine random number generator object.
1347 * @param __rhs Another %xor_combine random number generator
1348 * object.
1349 *
1350 * @returns true if the two objects are equal, false otherwise.
1351 */
1352 friend bool
1353 operator==(const xor_combine& __lhs, const xor_combine& __rhs)
1354 {
1355 return (__lhs.base1() == __rhs.base1())
1356 && (__lhs.base2() == __rhs.base2());
1357 }
1358
1359 /**
1360 * Compares two %xor_combine random number generator objects of
1361 * the same type for inequality.
1362 *
1363 * @param __lhs A %xor_combine random number generator object.
1364 * @param __rhs Another %xor_combine random number generator
1365 * object.
1366 *
1367 * @returns true if the two objects are not equal, false otherwise.
1368 */
1369 friend bool
1370 operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
1371 { return !(__lhs == __rhs); }
1372
1373 /**
1374 * Inserts the current state of a %xor_combine random number
1375 * generator engine @p __x into the output stream @p __os.
1376 *
1377 * @param __os An output stream.
1378 * @param __x A %xor_combine random number generator engine.
1379 *
1380 * @returns The output stream with the state of @p __x inserted or in
1381 * an error state.
1382 */
1383 template<class _UniformRandomNumberGenerator11, int __s11,
1384 class _UniformRandomNumberGenerator21, int __s21,
1385 typename _CharT, typename _Traits>
1386 friend std::basic_ostream<_CharT, _Traits>&
1387 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1388 const xor_combine<_UniformRandomNumberGenerator11, __s11,
1389 _UniformRandomNumberGenerator21, __s21>& __x);
1390
1391 /**
1392 * Extracts the current state of a %xor_combine random number
1393 * generator engine @p __x from the input stream @p __is.
1394 *
1395 * @param __is An input stream.
1396 * @param __x A %xor_combine random number generator engine.
1397 *
1398 * @returns The input stream with the state of @p __x extracted or in
1399 * an error state.
1400 */
1401 template<class _UniformRandomNumberGenerator11, int __s11,
1402 class _UniformRandomNumberGenerator21, int __s21,
1403 typename _CharT, typename _Traits>
1404 friend std::basic_istream<_CharT, _Traits>&
1405 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1406 xor_combine<_UniformRandomNumberGenerator11, __s11,
1407 _UniformRandomNumberGenerator21, __s21>& __x);
1408
1409 private:
1410 void
1411 _M_initialize_max();
1412
1413 result_type
1414 _M_initialize_max_aux(result_type, result_type, int);
1415
1416 base1_type _M_b1;
1417 base2_type _M_b2;
1418 result_type _M_max;
1419 };
1420
1421
1422 /**
1423 * A standard interface to a platform-specific non-deterministic
1424 * random number generator (if any are available).
1425 */
1426 class random_device
1427 {
1428 public:
1429 // types
1430 typedef unsigned int result_type;
1431
1432 // constructors, destructors and member functions
1433
1434#ifdef _GLIBCXX_USE_RANDOM_TR1
1435
1436 explicit
1437 random_device(const std::string& __token = "/dev/urandom")
1438 {
1439 if ((__token != "/dev/urandom" && __token != "/dev/random")
1440 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1441 std::__throw_runtime_error(__N("random_device::"
1442 "random_device(const std::string&)"));
1443 }
1444
1445 ~random_device()
1446 { std::fclose(_M_file); }
1447
1448#else
1449
1450 explicit
1451 random_device(const std::string& __token = "mt19937")
1452 : _M_mt(_M_strtoul(__token)) { }
1453
1454 private:
1455 static unsigned long
1456 _M_strtoul(const std::string& __str)
1457 {
1458 unsigned long __ret = 5489UL;
1459 if (__str != "mt19937")
1460 {
1461 const char* __nptr = __str.c_str();
1462 char* __endptr;
1463 __ret = std::strtoul(__nptr, &__endptr, 0);
1464 if (*__nptr == '\0' || *__endptr != '\0')
1465 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1466 "(const std::string&)"));
1467 }
1468 return __ret;
1469 }
1470
1471 public:
1472
1473#endif
1474
1475 result_type
1476 min() const
1477 { return std::numeric_limits<result_type>::min(); }
1478
1479 result_type
1480 max() const
1481 { return std::numeric_limits<result_type>::max(); }
1482
1483 double
1484 entropy() const
1485 { return 0.0; }
1486
1487 result_type
1488 operator()()
1489 {
1490#ifdef _GLIBCXX_USE_RANDOM_TR1
1491 result_type __ret;
1492 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1493 1, _M_file);
1494 return __ret;
1495#else
1496 return _M_mt();
1497#endif
1498 }
1499
1500 private:
1501 random_device(const random_device&);
1502 void operator=(const random_device&);
1503
1504#ifdef _GLIBCXX_USE_RANDOM_TR1
1505 FILE* _M_file;
1506#else
1507 mt19937 _M_mt;
1508#endif
1509 };
1510
014b6dbc 1511 /// @} group tr1_random_generators
8e79468d
BK
1512
1513 /**
1514 * @addtogroup tr1_random_distributions Random Number Distributions
1515 * @ingroup tr1_random
1516 * @{
1517 */
1518
1519 /**
1520 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1521 * @ingroup tr1_random_distributions
1522 * @{
1523 */
1524
1525 /**
1526 * @brief Uniform discrete distribution for random numbers.
1527 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1528 * probability throughout the range.
1529 */
1530 template<typename _IntType = int>
1531 class uniform_int
1532 {
1533 __glibcxx_class_requires(_IntType, _IntegerConcept)
45ab93d9 1534
8e79468d
BK
1535 public:
1536 /** The type of the parameters of the distribution. */
1537 typedef _IntType input_type;
1538 /** The type of the range of the distribution. */
1539 typedef _IntType result_type;
1540
1541 public:
1542 /**
1543 * Constructs a uniform distribution object.
1544 */
1545 explicit
1546 uniform_int(_IntType __min = 0, _IntType __max = 9)
1547 : _M_min(__min), _M_max(__max)
1548 {
1549 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1550 }
1551
1552 /**
1553 * Gets the inclusive lower bound of the distribution range.
1554 */
1555 result_type
1556 min() const
1557 { return _M_min; }
1558
1559 /**
1560 * Gets the inclusive upper bound of the distribution range.
1561 */
1562 result_type
1563 max() const
1564 { return _M_max; }
1565
1566 /**
1567 * Resets the distribution state.
1568 *
1569 * Does nothing for the uniform integer distribution.
1570 */
1571 void
1572 reset() { }
1573
1574 /**
1575 * Gets a uniformly distributed random number in the range
1576 * @f$(min, max)@f$.
1577 */
1578 template<typename _UniformRandomNumberGenerator>
1579 result_type
1580 operator()(_UniformRandomNumberGenerator& __urng)
1581 {
1582 typedef typename _UniformRandomNumberGenerator::result_type
1583 _UResult_type;
1584 return _M_call(__urng, _M_min, _M_max,
1585 typename is_integral<_UResult_type>::type());
1586 }
1587
1588 /**
1589 * Gets a uniform random number in the range @f$[0, n)@f$.
1590 *
1591 * This function is aimed at use with std::random_shuffle.
1592 */
1593 template<typename _UniformRandomNumberGenerator>
1594 result_type
1595 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1596 {
1597 typedef typename _UniformRandomNumberGenerator::result_type
1598 _UResult_type;
1599 return _M_call(__urng, 0, __n - 1,
1600 typename is_integral<_UResult_type>::type());
1601 }
1602
1603 /**
1604 * Inserts a %uniform_int random number distribution @p __x into the
1605 * output stream @p os.
1606 *
1607 * @param __os An output stream.
1608 * @param __x A %uniform_int random number distribution.
1609 *
1610 * @returns The output stream with the state of @p __x inserted or in
1611 * an error state.
1612 */
1613 template<typename _IntType1, typename _CharT, typename _Traits>
1614 friend std::basic_ostream<_CharT, _Traits>&
1615 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1616 const uniform_int<_IntType1>& __x);
1617
1618 /**
1619 * Extracts a %uniform_int random number distribution
1620 * @p __x from the input stream @p __is.
1621 *
1622 * @param __is An input stream.
1623 * @param __x A %uniform_int random number generator engine.
1624 *
1625 * @returns The input stream with @p __x extracted or in an error state.
1626 */
1627 template<typename _IntType1, typename _CharT, typename _Traits>
1628 friend std::basic_istream<_CharT, _Traits>&
1629 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1630 uniform_int<_IntType1>& __x);
1631
1632 private:
1633 template<typename _UniformRandomNumberGenerator>
1634 result_type
1635 _M_call(_UniformRandomNumberGenerator& __urng,
1636 result_type __min, result_type __max, true_type);
1637
1638 template<typename _UniformRandomNumberGenerator>
1639 result_type
1640 _M_call(_UniformRandomNumberGenerator& __urng,
1641 result_type __min, result_type __max, false_type)
1642 {
1643 return result_type((__urng() - __urng.min())
1644 / (__urng.max() - __urng.min())
1645 * (__max - __min + 1)) + __min;
1646 }
1647
1648 _IntType _M_min;
1649 _IntType _M_max;
1650 };
1651
1652
1653 /**
1654 * @brief A Bernoulli random number distribution.
1655 *
1656 * Generates a sequence of true and false values with likelihood @f$ p @f$
1657 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1658 */
1659 class bernoulli_distribution
1660 {
1661 public:
1662 typedef int input_type;
1663 typedef bool result_type;
1664
1665 public:
1666 /**
1667 * Constructs a Bernoulli distribution with likelihood @p p.
1668 *
1669 * @param __p [IN] The likelihood of a true result being returned. Must
1670 * be in the interval @f$ [0, 1] @f$.
1671 */
1672 explicit
1673 bernoulli_distribution(double __p = 0.5)
1674 : _M_p(__p)
45ab93d9 1675 {
8e79468d
BK
1676 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1677 }
1678
1679 /**
1680 * Gets the @p p parameter of the distribution.
1681 */
1682 double
1683 p() const
1684 { return _M_p; }
1685
1686 /**
1687 * Resets the distribution state.
1688 *
1689 * Does nothing for a Bernoulli distribution.
1690 */
1691 void
1692 reset() { }
1693
1694 /**
1695 * Gets the next value in the Bernoullian sequence.
1696 */
1697 template<class _UniformRandomNumberGenerator>
1698 result_type
1699 operator()(_UniformRandomNumberGenerator& __urng)
1700 {
1701 if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
1702 return true;
1703 return false;
1704 }
1705
1706 /**
1707 * Inserts a %bernoulli_distribution random number distribution
1708 * @p __x into the output stream @p __os.
1709 *
1710 * @param __os An output stream.
1711 * @param __x A %bernoulli_distribution random number distribution.
1712 *
1713 * @returns The output stream with the state of @p __x inserted or in
1714 * an error state.
1715 */
1716 template<typename _CharT, typename _Traits>
1717 friend std::basic_ostream<_CharT, _Traits>&
1718 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1719 const bernoulli_distribution& __x);
1720
1721 /**
1722 * Extracts a %bernoulli_distribution random number distribution
1723 * @p __x from the input stream @p __is.
1724 *
1725 * @param __is An input stream.
1726 * @param __x A %bernoulli_distribution random number generator engine.
1727 *
1728 * @returns The input stream with @p __x extracted or in an error state.
1729 */
1730 template<typename _CharT, typename _Traits>
1731 friend std::basic_istream<_CharT, _Traits>&
1732 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1733 bernoulli_distribution& __x)
1734 { return __is >> __x._M_p; }
1735
1736 private:
1737 double _M_p;
1738 };
1739
1740
1741 /**
1742 * @brief A discrete geometric random number distribution.
1743 *
45ab93d9 1744 * The formula for the geometric probability mass function is
8e79468d
BK
1745 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1746 * distribution.
1747 */
1748 template<typename _IntType = int, typename _RealType = double>
1749 class geometric_distribution
1750 {
1751 public:
1752 // types
1753 typedef _RealType input_type;
1754 typedef _IntType result_type;
1755
1756 // constructors and member function
1757 explicit
1758 geometric_distribution(const _RealType& __p = _RealType(0.5))
1759 : _M_p(__p)
1760 {
1761 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
1762 _M_initialize();
1763 }
1764
1765 /**
1766 * Gets the distribution parameter @p p.
1767 */
1768 _RealType
1769 p() const
1770 { return _M_p; }
1771
1772 void
1773 reset() { }
1774
1775 template<class _UniformRandomNumberGenerator>
1776 result_type
1777 operator()(_UniformRandomNumberGenerator& __urng);
1778
1779 /**
1780 * Inserts a %geometric_distribution random number distribution
1781 * @p __x into the output stream @p __os.
1782 *
1783 * @param __os An output stream.
1784 * @param __x A %geometric_distribution random number distribution.
1785 *
1786 * @returns The output stream with the state of @p __x inserted or in
1787 * an error state.
1788 */
1789 template<typename _IntType1, typename _RealType1,
1790 typename _CharT, typename _Traits>
1791 friend std::basic_ostream<_CharT, _Traits>&
1792 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1793 const geometric_distribution<_IntType1, _RealType1>& __x);
1794
1795 /**
1796 * Extracts a %geometric_distribution random number distribution
1797 * @p __x from the input stream @p __is.
1798 *
1799 * @param __is An input stream.
1800 * @param __x A %geometric_distribution random number generator engine.
1801 *
1802 * @returns The input stream with @p __x extracted or in an error state.
1803 */
1804 template<typename _CharT, typename _Traits>
1805 friend std::basic_istream<_CharT, _Traits>&
1806 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1807 geometric_distribution& __x)
1808 {
1809 __is >> __x._M_p;
1810 __x._M_initialize();
1811 return __is;
1812 }
1813
1814 private:
1815 void
1816 _M_initialize()
1817 { _M_log_p = std::log(_M_p); }
1818
1819 _RealType _M_p;
1820 _RealType _M_log_p;
1821 };
1822
1823
1824 template<typename _RealType>
1825 class normal_distribution;
1826
1827 /**
1828 * @brief A discrete Poisson random number distribution.
1829 *
1830 * The formula for the Poisson probability mass function is
1831 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1832 * parameter of the distribution.
1833 */
1834 template<typename _IntType = int, typename _RealType = double>
1835 class poisson_distribution
1836 {
1837 public:
1838 // types
1839 typedef _RealType input_type;
1840 typedef _IntType result_type;
1841
1842 // constructors and member function
1843 explicit
1844 poisson_distribution(const _RealType& __mean = _RealType(1))
1845 : _M_mean(__mean), _M_nd()
1846 {
1847 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
1848 _M_initialize();
1849 }
1850
1851 /**
1852 * Gets the distribution parameter @p mean.
1853 */
1854 _RealType
1855 mean() const
1856 { return _M_mean; }
1857
1858 void
1859 reset()
1860 { _M_nd.reset(); }
1861
1862 template<class _UniformRandomNumberGenerator>
1863 result_type
1864 operator()(_UniformRandomNumberGenerator& __urng);
1865
1866 /**
1867 * Inserts a %poisson_distribution random number distribution
1868 * @p __x into the output stream @p __os.
1869 *
1870 * @param __os An output stream.
1871 * @param __x A %poisson_distribution random number distribution.
1872 *
1873 * @returns The output stream with the state of @p __x inserted or in
1874 * an error state.
1875 */
1876 template<typename _IntType1, typename _RealType1,
1877 typename _CharT, typename _Traits>
1878 friend std::basic_ostream<_CharT, _Traits>&
1879 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1880 const poisson_distribution<_IntType1, _RealType1>& __x);
1881
1882 /**
1883 * Extracts a %poisson_distribution random number distribution
1884 * @p __x from the input stream @p __is.
1885 *
1886 * @param __is An input stream.
1887 * @param __x A %poisson_distribution random number generator engine.
1888 *
1889 * @returns The input stream with @p __x extracted or in an error state.
1890 */
1891 template<typename _IntType1, typename _RealType1,
1892 typename _CharT, typename _Traits>
1893 friend std::basic_istream<_CharT, _Traits>&
1894 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1895 poisson_distribution<_IntType1, _RealType1>& __x);
1896
1897 private:
1898 void
1899 _M_initialize();
1900
1901 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1902 normal_distribution<_RealType> _M_nd;
1903
1904 _RealType _M_mean;
1905
1906 // Hosts either log(mean) or the threshold of the simple method.
1907 _RealType _M_lm_thr;
1908#if _GLIBCXX_USE_C99_MATH_TR1
1909 _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
1910#endif
1911 };
1912
1913
1914 /**
1915 * @brief A discrete binomial random number distribution.
1916 *
45ab93d9 1917 * The formula for the binomial probability mass function is
8e79468d
BK
1918 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1919 * and @f$ p @f$ are the parameters of the distribution.
1920 */
1921 template<typename _IntType = int, typename _RealType = double>
1922 class binomial_distribution
1923 {
1924 public:
1925 // types
1926 typedef _RealType input_type;
1927 typedef _IntType result_type;
1928
1929 // constructors and member function
1930 explicit
1931 binomial_distribution(_IntType __t = 1,
1932 const _RealType& __p = _RealType(0.5))
1933 : _M_t(__t), _M_p(__p), _M_nd()
1934 {
1935 _GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
1936 _M_initialize();
1937 }
1938
1939 /**
1940 * Gets the distribution @p t parameter.
1941 */
1942 _IntType
1943 t() const
1944 { return _M_t; }
45ab93d9 1945
8e79468d
BK
1946 /**
1947 * Gets the distribution @p p parameter.
1948 */
1949 _RealType
1950 p() const
1951 { return _M_p; }
1952
1953 void
1954 reset()
1955 { _M_nd.reset(); }
1956
1957 template<class _UniformRandomNumberGenerator>
1958 result_type
1959 operator()(_UniformRandomNumberGenerator& __urng);
1960
1961 /**
1962 * Inserts a %binomial_distribution random number distribution
1963 * @p __x into the output stream @p __os.
1964 *
1965 * @param __os An output stream.
1966 * @param __x A %binomial_distribution random number distribution.
1967 *
1968 * @returns The output stream with the state of @p __x inserted or in
1969 * an error state.
1970 */
1971 template<typename _IntType1, typename _RealType1,
1972 typename _CharT, typename _Traits>
1973 friend std::basic_ostream<_CharT, _Traits>&
1974 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1975 const binomial_distribution<_IntType1, _RealType1>& __x);
1976
1977 /**
1978 * Extracts a %binomial_distribution random number distribution
1979 * @p __x from the input stream @p __is.
1980 *
1981 * @param __is An input stream.
1982 * @param __x A %binomial_distribution random number generator engine.
1983 *
1984 * @returns The input stream with @p __x extracted or in an error state.
1985 */
1986 template<typename _IntType1, typename _RealType1,
1987 typename _CharT, typename _Traits>
1988 friend std::basic_istream<_CharT, _Traits>&
1989 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1990 binomial_distribution<_IntType1, _RealType1>& __x);
1991
1992 private:
1993 void
1994 _M_initialize();
1995
1996 template<class _UniformRandomNumberGenerator>
1997 result_type
1998 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
1999
2000 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
2001 normal_distribution<_RealType> _M_nd;
2002
2003 _RealType _M_q;
2004#if _GLIBCXX_USE_C99_MATH_TR1
2005 _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
2006 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
2007#endif
2008 _RealType _M_p;
2009 _IntType _M_t;
2010
2011 bool _M_easy;
2012 };
2013
014b6dbc 2014 /// @} group tr1_random_distributions_discrete
8e79468d
BK
2015
2016 /**
2017 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
2018 * @ingroup tr1_random_distributions
2019 * @{
2020 */
2021
2022 /**
2023 * @brief Uniform continuous distribution for random numbers.
2024 *
2025 * A continuous random distribution on the range [min, max) with equal
2026 * probability throughout the range. The URNG should be real-valued and
2027 * deliver number in the range [0, 1).
2028 */
2029 template<typename _RealType = double>
2030 class uniform_real
2031 {
2032 public:
2033 // types
2034 typedef _RealType input_type;
2035 typedef _RealType result_type;
2036
2037 public:
2038 /**
2039 * Constructs a uniform_real object.
2040 *
2041 * @param __min [IN] The lower bound of the distribution.
2042 * @param __max [IN] The upper bound of the distribution.
2043 */
2044 explicit
2045 uniform_real(_RealType __min = _RealType(0),
2046 _RealType __max = _RealType(1))
2047 : _M_min(__min), _M_max(__max)
2048 {
2049 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
2050 }
2051
2052 result_type
2053 min() const
2054 { return _M_min; }
2055
2056 result_type
2057 max() const
2058 { return _M_max; }
2059
2060 void
2061 reset() { }
2062
2063 template<class _UniformRandomNumberGenerator>
2064 result_type
2065 operator()(_UniformRandomNumberGenerator& __urng)
2066 { return (__urng() * (_M_max - _M_min)) + _M_min; }
2067
2068 /**
2069 * Inserts a %uniform_real random number distribution @p __x into the
2070 * output stream @p __os.
2071 *
2072 * @param __os An output stream.
2073 * @param __x A %uniform_real random number distribution.
2074 *
2075 * @returns The output stream with the state of @p __x inserted or in
2076 * an error state.
2077 */
2078 template<typename _RealType1, typename _CharT, typename _Traits>
2079 friend std::basic_ostream<_CharT, _Traits>&
2080 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2081 const uniform_real<_RealType1>& __x);
2082
2083 /**
2084 * Extracts a %uniform_real random number distribution
2085 * @p __x from the input stream @p __is.
2086 *
2087 * @param __is An input stream.
2088 * @param __x A %uniform_real random number generator engine.
2089 *
2090 * @returns The input stream with @p __x extracted or in an error state.
2091 */
2092 template<typename _RealType1, typename _CharT, typename _Traits>
2093 friend std::basic_istream<_CharT, _Traits>&
2094 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2095 uniform_real<_RealType1>& __x);
2096
2097 private:
2098 _RealType _M_min;
2099 _RealType _M_max;
2100 };
2101
2102
2103 /**
2104 * @brief An exponential continuous distribution for random numbers.
2105 *
45ab93d9 2106 * The formula for the exponential probability mass function is
8e79468d
BK
2107 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2108 *
2109 * <table border=1 cellpadding=10 cellspacing=0>
2110 * <caption align=top>Distribution Statistics</caption>
2111 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2112 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2113 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2114 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2115 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2116 * </table>
2117 */
2118 template<typename _RealType = double>
2119 class exponential_distribution
2120 {
2121 public:
2122 // types
2123 typedef _RealType input_type;
2124 typedef _RealType result_type;
2125
2126 public:
2127 /**
2128 * Constructs an exponential distribution with inverse scale parameter
2129 * @f$ \lambda @f$.
2130 */
2131 explicit
2132 exponential_distribution(const result_type& __lambda = result_type(1))
2133 : _M_lambda(__lambda)
45ab93d9 2134 {
8e79468d
BK
2135 _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
2136 }
2137
2138 /**
2139 * Gets the inverse scale parameter of the distribution.
2140 */
2141 _RealType
2142 lambda() const
2143 { return _M_lambda; }
2144
2145 /**
2146 * Resets the distribution.
2147 *
2148 * Has no effect on exponential distributions.
2149 */
2150 void
2151 reset() { }
2152
2153 template<class _UniformRandomNumberGenerator>
2154 result_type
2155 operator()(_UniformRandomNumberGenerator& __urng)
2156 { return -std::log(__urng()) / _M_lambda; }
2157
2158 /**
2159 * Inserts a %exponential_distribution random number distribution
2160 * @p __x into the output stream @p __os.
2161 *
2162 * @param __os An output stream.
2163 * @param __x A %exponential_distribution random number distribution.
2164 *
2165 * @returns The output stream with the state of @p __x inserted or in
2166 * an error state.
2167 */
2168 template<typename _RealType1, typename _CharT, typename _Traits>
2169 friend std::basic_ostream<_CharT, _Traits>&
2170 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2171 const exponential_distribution<_RealType1>& __x);
2172
2173 /**
2174 * Extracts a %exponential_distribution random number distribution
2175 * @p __x from the input stream @p __is.
2176 *
2177 * @param __is An input stream.
2178 * @param __x A %exponential_distribution random number
2179 * generator engine.
2180 *
2181 * @returns The input stream with @p __x extracted or in an error state.
2182 */
2183 template<typename _CharT, typename _Traits>
2184 friend std::basic_istream<_CharT, _Traits>&
2185 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2186 exponential_distribution& __x)
2187 { return __is >> __x._M_lambda; }
2188
2189 private:
2190 result_type _M_lambda;
2191 };
2192
2193
2194 /**
2195 * @brief A normal continuous distribution for random numbers.
2196 *
45ab93d9
JJ
2197 * The formula for the normal probability mass function is
2198 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
8e79468d
BK
2199 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2200 */
2201 template<typename _RealType = double>
2202 class normal_distribution
2203 {
2204 public:
2205 // types
2206 typedef _RealType input_type;
2207 typedef _RealType result_type;
2208
2209 public:
2210 /**
2211 * Constructs a normal distribution with parameters @f$ mean @f$ and
2212 * @f$ \sigma @f$.
2213 */
2214 explicit
2215 normal_distribution(const result_type& __mean = result_type(0),
2216 const result_type& __sigma = result_type(1))
2217 : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
45ab93d9 2218 {
8e79468d
BK
2219 _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
2220 }
2221
2222 /**
2223 * Gets the mean of the distribution.
2224 */
2225 _RealType
2226 mean() const
2227 { return _M_mean; }
2228
2229 /**
2230 * Gets the @f$ \sigma @f$ of the distribution.
2231 */
2232 _RealType
2233 sigma() const
2234 { return _M_sigma; }
2235
2236 /**
2237 * Resets the distribution.
2238 */
2239 void
2240 reset()
2241 { _M_saved_available = false; }
2242
2243 template<class _UniformRandomNumberGenerator>
2244 result_type
2245 operator()(_UniformRandomNumberGenerator& __urng);
2246
2247 /**
2248 * Inserts a %normal_distribution random number distribution
2249 * @p __x into the output stream @p __os.
2250 *
2251 * @param __os An output stream.
2252 * @param __x A %normal_distribution random number distribution.
2253 *
2254 * @returns The output stream with the state of @p __x inserted or in
2255 * an error state.
2256 */
2257 template<typename _RealType1, typename _CharT, typename _Traits>
2258 friend std::basic_ostream<_CharT, _Traits>&
2259 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2260 const normal_distribution<_RealType1>& __x);
2261
2262 /**
2263 * Extracts a %normal_distribution random number distribution
2264 * @p __x from the input stream @p __is.
2265 *
2266 * @param __is An input stream.
2267 * @param __x A %normal_distribution random number generator engine.
2268 *
2269 * @returns The input stream with @p __x extracted or in an error state.
2270 */
2271 template<typename _RealType1, typename _CharT, typename _Traits>
2272 friend std::basic_istream<_CharT, _Traits>&
2273 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2274 normal_distribution<_RealType1>& __x);
2275
2276 private:
2277 result_type _M_mean;
2278 result_type _M_sigma;
2279 result_type _M_saved;
45ab93d9 2280 bool _M_saved_available;
8e79468d
BK
2281 };
2282
2283
2284 /**
2285 * @brief A gamma continuous distribution for random numbers.
2286 *
45ab93d9 2287 * The formula for the gamma probability mass function is
8e79468d
BK
2288 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2289 */
2290 template<typename _RealType = double>
2291 class gamma_distribution
2292 {
2293 public:
2294 // types
2295 typedef _RealType input_type;
2296 typedef _RealType result_type;
2297
2298 public:
2299 /**
2300 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2301 */
2302 explicit
2303 gamma_distribution(const result_type& __alpha_val = result_type(1))
2304 : _M_alpha(__alpha_val)
45ab93d9 2305 {
8e79468d
BK
2306 _GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
2307 _M_initialize();
2308 }
2309
2310 /**
2311 * Gets the @f$ \alpha @f$ of the distribution.
2312 */
2313 _RealType
2314 alpha() const
2315 { return _M_alpha; }
2316
2317 /**
2318 * Resets the distribution.
2319 */
2320 void
2321 reset() { }
2322
2323 template<class _UniformRandomNumberGenerator>
2324 result_type
2325 operator()(_UniformRandomNumberGenerator& __urng);
2326
2327 /**
2328 * Inserts a %gamma_distribution random number distribution
2329 * @p __x into the output stream @p __os.
2330 *
2331 * @param __os An output stream.
2332 * @param __x A %gamma_distribution random number distribution.
2333 *
2334 * @returns The output stream with the state of @p __x inserted or in
2335 * an error state.
2336 */
2337 template<typename _RealType1, typename _CharT, typename _Traits>
2338 friend std::basic_ostream<_CharT, _Traits>&
2339 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2340 const gamma_distribution<_RealType1>& __x);
2341
2342 /**
2343 * Extracts a %gamma_distribution random number distribution
2344 * @p __x from the input stream @p __is.
2345 *
2346 * @param __is An input stream.
2347 * @param __x A %gamma_distribution random number generator engine.
2348 *
2349 * @returns The input stream with @p __x extracted or in an error state.
2350 */
2351 template<typename _CharT, typename _Traits>
2352 friend std::basic_istream<_CharT, _Traits>&
2353 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2354 gamma_distribution& __x)
2355 {
2356 __is >> __x._M_alpha;
2357 __x._M_initialize();
2358 return __is;
2359 }
2360
2361 private:
2362 void
2363 _M_initialize();
2364
2365 result_type _M_alpha;
2366
2367 // Hosts either lambda of GB or d of modified Vaduva's.
2368 result_type _M_l_d;
2369 };
2370
014b6dbc
JW
2371 /// @} group tr1_random_distributions_continuous
2372 /// @} group tr1_random_distributions
2373 /// @} group tr1_random
8e79468d 2374}
4a15d842
FD
2375
2376_GLIBCXX_END_NAMESPACE_VERSION
8e79468d
BK
2377}
2378
8e79468d 2379#endif // _GLIBCXX_TR1_RANDOM_H