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