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