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