]>
Commit | Line | Data |
---|---|---|
8e79468d BK |
1 | // random number generation -*- C++ -*- |
2 | ||
cbe34bb5 | 3 | // Copyright (C) 2009-2017 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. | |
f910786b | 28 | * Do not attempt to use it directly. @headername{random} |
8e79468d BK |
29 | */ |
30 | ||
06f29237 PC |
31 | #ifndef _RANDOM_H |
32 | #define _RANDOM_H 1 | |
33 | ||
8e79468d | 34 | #include <vector> |
2944621e | 35 | #include <bits/uniform_int_dist.h> |
8e79468d | 36 | |
12ffa228 BK |
37 | namespace std _GLIBCXX_VISIBILITY(default) |
38 | { | |
39 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
53dc5044 | 40 | |
8e79468d BK |
41 | // [26.4] Random number generation |
42 | ||
43 | /** | |
037181bc BK |
44 | * @defgroup random Random Number Generation |
45 | * @ingroup numerics | |
46 | * | |
8e79468d BK |
47 | * A facility for generating random numbers on selected distributions. |
48 | * @{ | |
49 | */ | |
50 | ||
51 | /** | |
52 | * @brief A function template for converting the output of a (integral) | |
53 | * uniform random number generator to a floatng point result in the range | |
54 | * [0-1). | |
55 | */ | |
56 | template<typename _RealType, size_t __bits, | |
57 | typename _UniformRandomNumberGenerator> | |
58 | _RealType | |
59 | generate_canonical(_UniformRandomNumberGenerator& __g); | |
60 | ||
8e79468d BK |
61 | /* |
62 | * Implementation-space details. | |
63 | */ | |
64 | namespace __detail | |
65 | { | |
66 | template<typename _UIntType, size_t __w, | |
95fe602e PC |
67 | bool = __w < static_cast<size_t> |
68 | (std::numeric_limits<_UIntType>::digits)> | |
8e79468d BK |
69 | struct _Shift |
70 | { static const _UIntType __value = 0; }; | |
71 | ||
72 | template<typename _UIntType, size_t __w> | |
73 | struct _Shift<_UIntType, __w, true> | |
74 | { static const _UIntType __value = _UIntType(1) << __w; }; | |
75 | ||
cf48c255 MG |
76 | template<int __s, |
77 | int __which = ((__s <= __CHAR_BIT__ * sizeof (int)) | |
78 | + (__s <= __CHAR_BIT__ * sizeof (long)) | |
79 | + (__s <= __CHAR_BIT__ * sizeof (long long)) | |
80 | /* assume long long no bigger than __int128 */ | |
81 | + (__s <= 128))> | |
82 | struct _Select_uint_least_t | |
83 | { | |
84 | static_assert(__which < 0, /* needs to be dependent */ | |
85 | "sorry, would be too much trouble for a slow result"); | |
86 | }; | |
87 | ||
88 | template<int __s> | |
89 | struct _Select_uint_least_t<__s, 4> | |
90 | { typedef unsigned int type; }; | |
91 | ||
92 | template<int __s> | |
93 | struct _Select_uint_least_t<__s, 3> | |
94 | { typedef unsigned long type; }; | |
95 | ||
96 | template<int __s> | |
97 | struct _Select_uint_least_t<__s, 2> | |
98 | { typedef unsigned long long type; }; | |
99 | ||
100 | #ifdef _GLIBCXX_USE_INT128 | |
101 | template<int __s> | |
102 | struct _Select_uint_least_t<__s, 1> | |
103 | { typedef unsigned __int128 type; }; | |
104 | #endif | |
105 | ||
106 | // Assume a != 0, a < m, c < m, x < m. | |
107 | template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, | |
108 | bool __big_enough = (!(__m & (__m - 1)) | |
109 | || (_Tp(-1) - __c) / __a >= __m - 1), | |
110 | bool __schrage_ok = __m % __a < __m / __a> | |
111 | struct _Mod | |
112 | { | |
113 | typedef typename _Select_uint_least_t<std::__lg(__a) | |
114 | + std::__lg(__m) + 2>::type _Tp2; | |
115 | static _Tp | |
116 | __calc(_Tp __x) | |
117 | { return static_cast<_Tp>((_Tp2(__a) * __x + __c) % __m); } | |
118 | }; | |
119 | ||
120 | // Schrage. | |
121 | template<typename _Tp, _Tp __m, _Tp __a, _Tp __c> | |
122 | struct _Mod<_Tp, __m, __a, __c, false, true> | |
123 | { | |
124 | static _Tp | |
125 | __calc(_Tp __x); | |
126 | }; | |
127 | ||
128 | // Special cases: | |
129 | // - for m == 2^n or m == 0, unsigned integer overflow is safe. | |
130 | // - a * (m - 1) + c fits in _Tp, there is no overflow. | |
131 | template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool __s> | |
132 | struct _Mod<_Tp, __m, __a, __c, true, __s> | |
133 | { | |
134 | static _Tp | |
135 | __calc(_Tp __x) | |
136 | { | |
137 | _Tp __res = __a * __x + __c; | |
138 | if (__m) | |
139 | __res %= __m; | |
140 | return __res; | |
141 | } | |
142 | }; | |
8e79468d | 143 | |
b94f4bef | 144 | template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0> |
8e79468d BK |
145 | inline _Tp |
146 | __mod(_Tp __x) | |
cf48c255 | 147 | { return _Mod<_Tp, __m, __a, __c>::__calc(__x); } |
8e79468d BK |
148 | |
149 | /* | |
150 | * An adaptor class for converting the output of any Generator into | |
151 | * the input for a specific Distribution. | |
152 | */ | |
153 | template<typename _Engine, typename _DInputType> | |
154 | struct _Adaptor | |
155 | { | |
1c4ff014 | 156 | static_assert(std::is_floating_point<_DInputType>::value, |
0cded43d | 157 | "template argument must be a floating point type"); |
8e79468d BK |
158 | |
159 | public: | |
160 | _Adaptor(_Engine& __g) | |
161 | : _M_g(__g) { } | |
162 | ||
163 | _DInputType | |
164 | min() const | |
9b88236b | 165 | { return _DInputType(0); } |
8e79468d BK |
166 | |
167 | _DInputType | |
168 | max() const | |
9b88236b | 169 | { return _DInputType(1); } |
8e79468d BK |
170 | |
171 | /* | |
172 | * Converts a value generated by the adapted random number generator | |
173 | * into a value in the input domain for the dependent random number | |
174 | * distribution. | |
8e79468d BK |
175 | */ |
176 | _DInputType | |
177 | operator()() | |
178 | { | |
9b88236b PC |
179 | return std::generate_canonical<_DInputType, |
180 | std::numeric_limits<_DInputType>::digits, | |
181 | _Engine>(_M_g); | |
8e79468d BK |
182 | } |
183 | ||
184 | private: | |
185 | _Engine& _M_g; | |
186 | }; | |
12ffa228 | 187 | |
8e79468d BK |
188 | } // namespace __detail |
189 | ||
190 | /** | |
037181bc BK |
191 | * @addtogroup random_generators Random Number Generators |
192 | * @ingroup random | |
8e79468d BK |
193 | * |
194 | * These classes define objects which provide random or pseudorandom | |
195 | * numbers, either from a discrete or a continuous interval. The | |
196 | * random number generator supplied as a part of this library are | |
197 | * all uniform random number generators which provide a sequence of | |
198 | * random number uniformly distributed over their range. | |
199 | * | |
200 | * A number generator is a function object with an operator() that | |
201 | * takes zero arguments and returns a number. | |
202 | * | |
203 | * A compliant random number generator must satisfy the following | |
204 | * requirements. <table border=1 cellpadding=10 cellspacing=0> | |
205 | * <caption align=top>Random Number Generator Requirements</caption> | |
206 | * <tr><td>To be documented.</td></tr> </table> | |
207 | * | |
208 | * @{ | |
209 | */ | |
210 | ||
211 | /** | |
212 | * @brief A model of a linear congruential random number generator. | |
213 | * | |
6cc5a790 BK |
214 | * A random number generator that produces pseudorandom numbers via |
215 | * linear function: | |
216 | * @f[ | |
217 | * x_{i+1}\leftarrow(ax_{i} + c) \bmod m | |
218 | * @f] | |
8e79468d BK |
219 | * |
220 | * The template parameter @p _UIntType must be an unsigned integral type | |
221 | * large enough to store values up to (__m-1). If the template parameter | |
222 | * @p __m is 0, the modulus @p __m used is | |
223 | * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template | |
224 | * parameters @p __a and @p __c must be less than @p __m. | |
225 | * | |
6cc5a790 | 226 | * The size of the state is @f$1@f$. |
8e79468d BK |
227 | */ |
228 | template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | |
229 | class linear_congruential_engine | |
230 | { | |
0cded43d JW |
231 | static_assert(std::is_unsigned<_UIntType>::value, |
232 | "result_type must be an unsigned integral type"); | |
77e3c516 | 233 | static_assert(__m == 0u || (__a < __m && __c < __m), |
f6d08b43 | 234 | "template argument substituting __m out of bounds"); |
8e79468d BK |
235 | |
236 | public: | |
237 | /** The type of the generated random value. */ | |
238 | typedef _UIntType result_type; | |
239 | ||
240 | /** The multiplier. */ | |
94a86be0 | 241 | static constexpr result_type multiplier = __a; |
8e79468d | 242 | /** An increment. */ |
94a86be0 | 243 | static constexpr result_type increment = __c; |
8e79468d | 244 | /** The modulus. */ |
94a86be0 BK |
245 | static constexpr result_type modulus = __m; |
246 | static constexpr result_type default_seed = 1u; | |
8e79468d BK |
247 | |
248 | /** | |
249 | * @brief Constructs a %linear_congruential_engine random number | |
250 | * generator engine with seed @p __s. The default seed value | |
251 | * is 1. | |
252 | * | |
253 | * @param __s The initial seed value. | |
254 | */ | |
255 | explicit | |
256 | linear_congruential_engine(result_type __s = default_seed) | |
15ecdcc6 | 257 | { seed(__s); } |
8e79468d BK |
258 | |
259 | /** | |
260 | * @brief Constructs a %linear_congruential_engine random number | |
261 | * generator engine seeded from the seed sequence @p __q. | |
262 | * | |
263 | * @param __q the seed sequence. | |
264 | */ | |
05eeebfe PC |
265 | template<typename _Sseq, typename = typename |
266 | std::enable_if<!std::is_same<_Sseq, linear_congruential_engine>::value> | |
267 | ::type> | |
15ecdcc6 PC |
268 | explicit |
269 | linear_congruential_engine(_Sseq& __q) | |
05eeebfe | 270 | { seed(__q); } |
8e79468d BK |
271 | |
272 | /** | |
273 | * @brief Reseeds the %linear_congruential_engine random number generator | |
04b70271 | 274 | * engine sequence to the seed @p __s. |
8e79468d BK |
275 | * |
276 | * @param __s The new seed. | |
277 | */ | |
278 | void | |
279 | seed(result_type __s = default_seed); | |
280 | ||
281 | /** | |
282 | * @brief Reseeds the %linear_congruential_engine random number generator | |
283 | * engine | |
284 | * sequence using values from the seed sequence @p __q. | |
285 | * | |
286 | * @param __q the seed sequence. | |
287 | */ | |
05eeebfe PC |
288 | template<typename _Sseq> |
289 | typename std::enable_if<std::is_class<_Sseq>::value>::type | |
15ecdcc6 | 290 | seed(_Sseq& __q); |
8e79468d BK |
291 | |
292 | /** | |
293 | * @brief Gets the smallest possible value in the output range. | |
294 | * | |
295 | * The minimum depends on the @p __c parameter: if it is zero, the | |
296 | * minimum generated must be > 0, otherwise 0 is allowed. | |
8e79468d | 297 | */ |
94a86be0 BK |
298 | static constexpr result_type |
299 | min() | |
96a9203b | 300 | { return __c == 0u ? 1u : 0u; } |
8e79468d BK |
301 | |
302 | /** | |
303 | * @brief Gets the largest possible value in the output range. | |
8e79468d | 304 | */ |
94a86be0 BK |
305 | static constexpr result_type |
306 | max() | |
96a9203b | 307 | { return __m - 1u; } |
8e79468d BK |
308 | |
309 | /** | |
310 | * @brief Discard a sequence of random numbers. | |
8e79468d BK |
311 | */ |
312 | void | |
313 | discard(unsigned long long __z) | |
314 | { | |
315 | for (; __z != 0ULL; --__z) | |
316 | (*this)(); | |
317 | } | |
318 | ||
319 | /** | |
320 | * @brief Gets the next random number in the sequence. | |
321 | */ | |
322 | result_type | |
b01630bb PC |
323 | operator()() |
324 | { | |
b94f4bef | 325 | _M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x); |
b01630bb PC |
326 | return _M_x; |
327 | } | |
8e79468d BK |
328 | |
329 | /** | |
330 | * @brief Compares two linear congruential random number generator | |
331 | * objects of the same type for equality. | |
332 | * | |
333 | * @param __lhs A linear congruential random number generator object. | |
334 | * @param __rhs Another linear congruential random number generator | |
335 | * object. | |
336 | * | |
fc05e1ba PC |
337 | * @returns true if the infinite sequences of generated values |
338 | * would be equal, false otherwise. | |
8e79468d BK |
339 | */ |
340 | friend bool | |
341 | operator==(const linear_congruential_engine& __lhs, | |
342 | const linear_congruential_engine& __rhs) | |
343 | { return __lhs._M_x == __rhs._M_x; } | |
344 | ||
345 | /** | |
346 | * @brief Writes the textual representation of the state x(i) of x to | |
347 | * @p __os. | |
348 | * | |
349 | * @param __os The output stream. | |
350 | * @param __lcr A % linear_congruential_engine random number generator. | |
351 | * @returns __os. | |
352 | */ | |
353 | template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1, | |
b94f4bef | 354 | _UIntType1 __m1, typename _CharT, typename _Traits> |
8e79468d | 355 | friend std::basic_ostream<_CharT, _Traits>& |
93c66bc6 | 356 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
94986f6d | 357 | const std::linear_congruential_engine<_UIntType1, |
93c66bc6 | 358 | __a1, __c1, __m1>& __lcr); |
8e79468d BK |
359 | |
360 | /** | |
361 | * @brief Sets the state of the engine by reading its textual | |
362 | * representation from @p __is. | |
363 | * | |
364 | * The textual representation must have been previously written using | |
365 | * an output stream whose imbued locale and whose type's template | |
366 | * specialization arguments _CharT and _Traits were the same as those | |
367 | * of @p __is. | |
368 | * | |
369 | * @param __is The input stream. | |
370 | * @param __lcr A % linear_congruential_engine random number generator. | |
371 | * @returns __is. | |
372 | */ | |
373 | template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1, | |
b94f4bef | 374 | _UIntType1 __m1, typename _CharT, typename _Traits> |
8e79468d | 375 | friend std::basic_istream<_CharT, _Traits>& |
93c66bc6 | 376 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
94986f6d | 377 | std::linear_congruential_engine<_UIntType1, __a1, |
93c66bc6 | 378 | __c1, __m1>& __lcr); |
8e79468d BK |
379 | |
380 | private: | |
8e79468d BK |
381 | _UIntType _M_x; |
382 | }; | |
383 | ||
fc05e1ba PC |
384 | /** |
385 | * @brief Compares two linear congruential random number generator | |
386 | * objects of the same type for inequality. | |
387 | * | |
388 | * @param __lhs A linear congruential random number generator object. | |
389 | * @param __rhs Another linear congruential random number generator | |
390 | * object. | |
391 | * | |
392 | * @returns true if the infinite sequences of generated values | |
393 | * would be different, false otherwise. | |
394 | */ | |
395 | template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | |
396 | inline bool | |
397 | operator!=(const std::linear_congruential_engine<_UIntType, __a, | |
398 | __c, __m>& __lhs, | |
399 | const std::linear_congruential_engine<_UIntType, __a, | |
400 | __c, __m>& __rhs) | |
401 | { return !(__lhs == __rhs); } | |
402 | ||
8e79468d BK |
403 | |
404 | /** | |
405 | * A generalized feedback shift register discrete random number generator. | |
406 | * | |
407 | * This algorithm avoids multiplication and division and is designed to be | |
408 | * friendly to a pipelined architecture. If the parameters are chosen | |
409 | * correctly, this generator will produce numbers with a very long period and | |
410 | * fairly good apparent entropy, although still not cryptographically strong. | |
411 | * | |
412 | * The best way to use this generator is with the predefined mt19937 class. | |
413 | * | |
414 | * This algorithm was originally invented by Makoto Matsumoto and | |
415 | * Takuji Nishimura. | |
416 | * | |
93c66bc6 BK |
417 | * @tparam __w Word size, the number of bits in each element of |
418 | * the state vector. | |
419 | * @tparam __n The degree of recursion. | |
420 | * @tparam __m The period parameter. | |
421 | * @tparam __r The separation point bit index. | |
422 | * @tparam __a The last row of the twist matrix. | |
423 | * @tparam __u The first right-shift tempering matrix parameter. | |
424 | * @tparam __d The first right-shift tempering matrix mask. | |
425 | * @tparam __s The first left-shift tempering matrix parameter. | |
426 | * @tparam __b The first left-shift tempering matrix mask. | |
427 | * @tparam __t The second left-shift tempering matrix parameter. | |
428 | * @tparam __c The second left-shift tempering matrix mask. | |
429 | * @tparam __l The second right-shift tempering matrix parameter. | |
430 | * @tparam __f Initialization multiplier. | |
8e79468d BK |
431 | */ |
432 | template<typename _UIntType, size_t __w, | |
433 | size_t __n, size_t __m, size_t __r, | |
434 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
435 | _UIntType __b, size_t __t, | |
436 | _UIntType __c, size_t __l, _UIntType __f> | |
437 | class mersenne_twister_engine | |
438 | { | |
0cded43d JW |
439 | static_assert(std::is_unsigned<_UIntType>::value, |
440 | "result_type must be an unsigned integral type"); | |
77e3c516 | 441 | static_assert(1u <= __m && __m <= __n, |
f6d08b43 PC |
442 | "template argument substituting __m out of bounds"); |
443 | static_assert(__r <= __w, "template argument substituting " | |
444 | "__r out of bound"); | |
445 | static_assert(__u <= __w, "template argument substituting " | |
446 | "__u out of bound"); | |
447 | static_assert(__s <= __w, "template argument substituting " | |
448 | "__s out of bound"); | |
449 | static_assert(__t <= __w, "template argument substituting " | |
450 | "__t out of bound"); | |
451 | static_assert(__l <= __w, "template argument substituting " | |
452 | "__l out of bound"); | |
77e3c516 | 453 | static_assert(__w <= std::numeric_limits<_UIntType>::digits, |
f6d08b43 | 454 | "template argument substituting __w out of bound"); |
6855fe45 | 455 | static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1), |
f6d08b43 | 456 | "template argument substituting __a out of bound"); |
6855fe45 | 457 | static_assert(__b <= (__detail::_Shift<_UIntType, __w>::__value - 1), |
f6d08b43 | 458 | "template argument substituting __b out of bound"); |
6855fe45 | 459 | static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1), |
f6d08b43 | 460 | "template argument substituting __c out of bound"); |
b94f4bef | 461 | static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1), |
f6d08b43 | 462 | "template argument substituting __d out of bound"); |
b94f4bef | 463 | static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1), |
f6d08b43 | 464 | "template argument substituting __f out of bound"); |
8e79468d BK |
465 | |
466 | public: | |
467 | /** The type of the generated random value. */ | |
468 | typedef _UIntType result_type; | |
469 | ||
470 | // parameter values | |
94a86be0 BK |
471 | static constexpr size_t word_size = __w; |
472 | static constexpr size_t state_size = __n; | |
473 | static constexpr size_t shift_size = __m; | |
474 | static constexpr size_t mask_bits = __r; | |
475 | static constexpr result_type xor_mask = __a; | |
476 | static constexpr size_t tempering_u = __u; | |
477 | static constexpr result_type tempering_d = __d; | |
478 | static constexpr size_t tempering_s = __s; | |
479 | static constexpr result_type tempering_b = __b; | |
480 | static constexpr size_t tempering_t = __t; | |
481 | static constexpr result_type tempering_c = __c; | |
482 | static constexpr size_t tempering_l = __l; | |
483 | static constexpr result_type initialization_multiplier = __f; | |
484 | static constexpr result_type default_seed = 5489u; | |
8e79468d BK |
485 | |
486 | // constructors and member function | |
487 | explicit | |
488 | mersenne_twister_engine(result_type __sd = default_seed) | |
489 | { seed(__sd); } | |
490 | ||
491 | /** | |
492 | * @brief Constructs a %mersenne_twister_engine random number generator | |
493 | * engine seeded from the seed sequence @p __q. | |
494 | * | |
495 | * @param __q the seed sequence. | |
496 | */ | |
05eeebfe PC |
497 | template<typename _Sseq, typename = typename |
498 | std::enable_if<!std::is_same<_Sseq, mersenne_twister_engine>::value> | |
499 | ::type> | |
15ecdcc6 PC |
500 | explicit |
501 | mersenne_twister_engine(_Sseq& __q) | |
05eeebfe | 502 | { seed(__q); } |
8e79468d BK |
503 | |
504 | void | |
505 | seed(result_type __sd = default_seed); | |
506 | ||
05eeebfe PC |
507 | template<typename _Sseq> |
508 | typename std::enable_if<std::is_class<_Sseq>::value>::type | |
15ecdcc6 | 509 | seed(_Sseq& __q); |
8e79468d BK |
510 | |
511 | /** | |
512 | * @brief Gets the smallest possible value in the output range. | |
8e79468d | 513 | */ |
94a86be0 BK |
514 | static constexpr result_type |
515 | min() | |
57c51668 | 516 | { return 0; } |
8e79468d BK |
517 | |
518 | /** | |
519 | * @brief Gets the largest possible value in the output range. | |
8e79468d | 520 | */ |
94a86be0 BK |
521 | static constexpr result_type |
522 | max() | |
6855fe45 | 523 | { return __detail::_Shift<_UIntType, __w>::__value - 1; } |
8e79468d BK |
524 | |
525 | /** | |
526 | * @brief Discard a sequence of random numbers. | |
8e79468d BK |
527 | */ |
528 | void | |
b668e41a | 529 | discard(unsigned long long __z); |
8e79468d BK |
530 | |
531 | result_type | |
532 | operator()(); | |
533 | ||
534 | /** | |
535 | * @brief Compares two % mersenne_twister_engine random number generator | |
536 | * objects of the same type for equality. | |
537 | * | |
538 | * @param __lhs A % mersenne_twister_engine random number generator | |
539 | * object. | |
540 | * @param __rhs Another % mersenne_twister_engine random number | |
541 | * generator object. | |
542 | * | |
fc05e1ba PC |
543 | * @returns true if the infinite sequences of generated values |
544 | * would be equal, false otherwise. | |
8e79468d BK |
545 | */ |
546 | friend bool | |
547 | operator==(const mersenne_twister_engine& __lhs, | |
548 | const mersenne_twister_engine& __rhs) | |
31645179 PC |
549 | { return (std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x) |
550 | && __lhs._M_p == __rhs._M_p); } | |
8e79468d BK |
551 | |
552 | /** | |
553 | * @brief Inserts the current state of a % mersenne_twister_engine | |
554 | * random number generator engine @p __x into the output stream | |
555 | * @p __os. | |
556 | * | |
557 | * @param __os An output stream. | |
558 | * @param __x A % mersenne_twister_engine random number generator | |
559 | * engine. | |
560 | * | |
561 | * @returns The output stream with the state of @p __x inserted or in | |
562 | * an error state. | |
563 | */ | |
564 | template<typename _UIntType1, | |
565 | size_t __w1, size_t __n1, | |
566 | size_t __m1, size_t __r1, | |
567 | _UIntType1 __a1, size_t __u1, | |
568 | _UIntType1 __d1, size_t __s1, | |
569 | _UIntType1 __b1, size_t __t1, | |
570 | _UIntType1 __c1, size_t __l1, _UIntType1 __f1, | |
571 | typename _CharT, typename _Traits> | |
572 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 | 573 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
94986f6d PC |
574 | const std::mersenne_twister_engine<_UIntType1, __w1, __n1, |
575 | __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1, | |
93c66bc6 | 576 | __l1, __f1>& __x); |
8e79468d BK |
577 | |
578 | /** | |
579 | * @brief Extracts the current state of a % mersenne_twister_engine | |
580 | * random number generator engine @p __x from the input stream | |
581 | * @p __is. | |
582 | * | |
583 | * @param __is An input stream. | |
584 | * @param __x A % mersenne_twister_engine random number generator | |
585 | * engine. | |
586 | * | |
587 | * @returns The input stream with the state of @p __x extracted or in | |
588 | * an error state. | |
589 | */ | |
590 | template<typename _UIntType1, | |
591 | size_t __w1, size_t __n1, | |
592 | size_t __m1, size_t __r1, | |
593 | _UIntType1 __a1, size_t __u1, | |
594 | _UIntType1 __d1, size_t __s1, | |
595 | _UIntType1 __b1, size_t __t1, | |
596 | _UIntType1 __c1, size_t __l1, _UIntType1 __f1, | |
597 | typename _CharT, typename _Traits> | |
598 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 | 599 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
94986f6d PC |
600 | std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1, |
601 | __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1, | |
93c66bc6 | 602 | __l1, __f1>& __x); |
8e79468d BK |
603 | |
604 | private: | |
b668e41a UD |
605 | void _M_gen_rand(); |
606 | ||
8e79468d BK |
607 | _UIntType _M_x[state_size]; |
608 | size_t _M_p; | |
609 | }; | |
610 | ||
fc05e1ba PC |
611 | /** |
612 | * @brief Compares two % mersenne_twister_engine random number generator | |
613 | * objects of the same type for inequality. | |
614 | * | |
615 | * @param __lhs A % mersenne_twister_engine random number generator | |
616 | * object. | |
617 | * @param __rhs Another % mersenne_twister_engine random number | |
618 | * generator object. | |
619 | * | |
620 | * @returns true if the infinite sequences of generated values | |
621 | * would be different, false otherwise. | |
622 | */ | |
623 | template<typename _UIntType, size_t __w, | |
624 | size_t __n, size_t __m, size_t __r, | |
625 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
626 | _UIntType __b, size_t __t, | |
627 | _UIntType __c, size_t __l, _UIntType __f> | |
628 | inline bool | |
629 | operator!=(const std::mersenne_twister_engine<_UIntType, __w, __n, __m, | |
630 | __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __lhs, | |
631 | const std::mersenne_twister_engine<_UIntType, __w, __n, __m, | |
632 | __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __rhs) | |
633 | { return !(__lhs == __rhs); } | |
634 | ||
635 | ||
8e79468d BK |
636 | /** |
637 | * @brief The Marsaglia-Zaman generator. | |
638 | * | |
639 | * This is a model of a Generalized Fibonacci discrete random number | |
640 | * generator, sometimes referred to as the SWC generator. | |
641 | * | |
642 | * A discrete random number generator that produces pseudorandom | |
6cc5a790 BK |
643 | * numbers using: |
644 | * @f[ | |
645 | * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m | |
646 | * @f] | |
8e79468d | 647 | * |
6cc5a790 BK |
648 | * The size of the state is @f$r@f$ |
649 | * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$. | |
8e79468d BK |
650 | */ |
651 | template<typename _UIntType, size_t __w, size_t __s, size_t __r> | |
652 | class subtract_with_carry_engine | |
653 | { | |
0cded43d JW |
654 | static_assert(std::is_unsigned<_UIntType>::value, |
655 | "result_type must be an unsigned integral type"); | |
77e3c516 | 656 | static_assert(0u < __s && __s < __r, |
0cded43d | 657 | "0 < s < r"); |
77e3c516 | 658 | static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits, |
f6d08b43 | 659 | "template argument substituting __w out of bounds"); |
8e79468d BK |
660 | |
661 | public: | |
662 | /** The type of the generated random value. */ | |
663 | typedef _UIntType result_type; | |
664 | ||
665 | // parameter values | |
94a86be0 BK |
666 | static constexpr size_t word_size = __w; |
667 | static constexpr size_t short_lag = __s; | |
668 | static constexpr size_t long_lag = __r; | |
669 | static constexpr result_type default_seed = 19780503u; | |
8e79468d BK |
670 | |
671 | /** | |
672 | * @brief Constructs an explicitly seeded % subtract_with_carry_engine | |
673 | * random number generator. | |
674 | */ | |
675 | explicit | |
676 | subtract_with_carry_engine(result_type __sd = default_seed) | |
15ecdcc6 | 677 | { seed(__sd); } |
8e79468d BK |
678 | |
679 | /** | |
680 | * @brief Constructs a %subtract_with_carry_engine random number engine | |
681 | * seeded from the seed sequence @p __q. | |
682 | * | |
683 | * @param __q the seed sequence. | |
684 | */ | |
05eeebfe PC |
685 | template<typename _Sseq, typename = typename |
686 | std::enable_if<!std::is_same<_Sseq, subtract_with_carry_engine>::value> | |
687 | ::type> | |
15ecdcc6 PC |
688 | explicit |
689 | subtract_with_carry_engine(_Sseq& __q) | |
05eeebfe | 690 | { seed(__q); } |
8e79468d BK |
691 | |
692 | /** | |
6cc5a790 | 693 | * @brief Seeds the initial state @f$x_0@f$ of the random number |
8e79468d BK |
694 | * generator. |
695 | * | |
696 | * N1688[4.19] modifies this as follows. If @p __value == 0, | |
697 | * sets value to 19780503. In any case, with a linear | |
698 | * congruential generator lcg(i) having parameters @f$ m_{lcg} = | |
699 | * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value | |
700 | * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m | |
701 | * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$ | |
702 | * set carry to 1, otherwise sets carry to 0. | |
703 | */ | |
704 | void | |
705 | seed(result_type __sd = default_seed); | |
706 | ||
707 | /** | |
6cc5a790 | 708 | * @brief Seeds the initial state @f$x_0@f$ of the |
8e79468d BK |
709 | * % subtract_with_carry_engine random number generator. |
710 | */ | |
05eeebfe PC |
711 | template<typename _Sseq> |
712 | typename std::enable_if<std::is_class<_Sseq>::value>::type | |
15ecdcc6 | 713 | seed(_Sseq& __q); |
8e79468d BK |
714 | |
715 | /** | |
716 | * @brief Gets the inclusive minimum value of the range of random | |
717 | * integers returned by this generator. | |
8e79468d | 718 | */ |
94a86be0 BK |
719 | static constexpr result_type |
720 | min() | |
8e79468d BK |
721 | { return 0; } |
722 | ||
723 | /** | |
724 | * @brief Gets the inclusive maximum value of the range of random | |
725 | * integers returned by this generator. | |
8e79468d | 726 | */ |
94a86be0 BK |
727 | static constexpr result_type |
728 | max() | |
6855fe45 | 729 | { return __detail::_Shift<_UIntType, __w>::__value - 1; } |
8e79468d BK |
730 | |
731 | /** | |
732 | * @brief Discard a sequence of random numbers. | |
8e79468d BK |
733 | */ |
734 | void | |
735 | discard(unsigned long long __z) | |
736 | { | |
737 | for (; __z != 0ULL; --__z) | |
738 | (*this)(); | |
739 | } | |
740 | ||
741 | /** | |
742 | * @brief Gets the next random number in the sequence. | |
743 | */ | |
744 | result_type | |
745 | operator()(); | |
746 | ||
747 | /** | |
748 | * @brief Compares two % subtract_with_carry_engine random number | |
749 | * generator objects of the same type for equality. | |
750 | * | |
751 | * @param __lhs A % subtract_with_carry_engine random number generator | |
752 | * object. | |
753 | * @param __rhs Another % subtract_with_carry_engine random number | |
754 | * generator object. | |
755 | * | |
fc05e1ba PC |
756 | * @returns true if the infinite sequences of generated values |
757 | * would be equal, false otherwise. | |
758 | */ | |
8e79468d BK |
759 | friend bool |
760 | operator==(const subtract_with_carry_engine& __lhs, | |
761 | const subtract_with_carry_engine& __rhs) | |
31645179 PC |
762 | { return (std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x) |
763 | && __lhs._M_carry == __rhs._M_carry | |
764 | && __lhs._M_p == __rhs._M_p); } | |
8e79468d BK |
765 | |
766 | /** | |
767 | * @brief Inserts the current state of a % subtract_with_carry_engine | |
768 | * random number generator engine @p __x into the output stream | |
769 | * @p __os. | |
770 | * | |
771 | * @param __os An output stream. | |
772 | * @param __x A % subtract_with_carry_engine random number generator | |
773 | * engine. | |
774 | * | |
775 | * @returns The output stream with the state of @p __x inserted or in | |
776 | * an error state. | |
777 | */ | |
778 | template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1, | |
779 | typename _CharT, typename _Traits> | |
780 | friend std::basic_ostream<_CharT, _Traits>& | |
00541349 | 781 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
94986f6d | 782 | const std::subtract_with_carry_engine<_UIntType1, __w1, |
00541349 | 783 | __s1, __r1>& __x); |
8e79468d BK |
784 | |
785 | /** | |
786 | * @brief Extracts the current state of a % subtract_with_carry_engine | |
787 | * random number generator engine @p __x from the input stream | |
788 | * @p __is. | |
789 | * | |
790 | * @param __is An input stream. | |
15ecdcc6 PC |
791 | * @param __x A % subtract_with_carry_engine random number generator |
792 | * engine. | |
8e79468d BK |
793 | * |
794 | * @returns The input stream with the state of @p __x extracted or in | |
795 | * an error state. | |
796 | */ | |
797 | template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1, | |
798 | typename _CharT, typename _Traits> | |
799 | friend std::basic_istream<_CharT, _Traits>& | |
00541349 | 800 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
94986f6d | 801 | std::subtract_with_carry_engine<_UIntType1, __w1, |
00541349 | 802 | __s1, __r1>& __x); |
8e79468d BK |
803 | |
804 | private: | |
00541349 | 805 | /// The state of the generator. This is a ring buffer. |
8e79468d | 806 | _UIntType _M_x[long_lag]; |
00541349 JW |
807 | _UIntType _M_carry; ///< The carry |
808 | size_t _M_p; ///< Current index of x(i - r). | |
8e79468d BK |
809 | }; |
810 | ||
fc05e1ba PC |
811 | /** |
812 | * @brief Compares two % subtract_with_carry_engine random number | |
813 | * generator objects of the same type for inequality. | |
814 | * | |
815 | * @param __lhs A % subtract_with_carry_engine random number generator | |
816 | * object. | |
817 | * @param __rhs Another % subtract_with_carry_engine random number | |
818 | * generator object. | |
819 | * | |
820 | * @returns true if the infinite sequences of generated values | |
821 | * would be different, false otherwise. | |
822 | */ | |
823 | template<typename _UIntType, size_t __w, size_t __s, size_t __r> | |
824 | inline bool | |
825 | operator!=(const std::subtract_with_carry_engine<_UIntType, __w, | |
826 | __s, __r>& __lhs, | |
827 | const std::subtract_with_carry_engine<_UIntType, __w, | |
828 | __s, __r>& __rhs) | |
829 | { return !(__lhs == __rhs); } | |
830 | ||
831 | ||
8e79468d BK |
832 | /** |
833 | * Produces random numbers from some base engine by discarding blocks of | |
834 | * data. | |
835 | * | |
836 | * 0 <= @p __r <= @p __p | |
837 | */ | |
838 | template<typename _RandomNumberEngine, size_t __p, size_t __r> | |
839 | class discard_block_engine | |
840 | { | |
77e3c516 | 841 | static_assert(1 <= __r && __r <= __p, |
f6d08b43 | 842 | "template argument substituting __r out of bounds"); |
8e79468d BK |
843 | |
844 | public: | |
845 | /** The type of the generated random value. */ | |
846 | typedef typename _RandomNumberEngine::result_type result_type; | |
847 | ||
848 | // parameter values | |
94a86be0 BK |
849 | static constexpr size_t block_size = __p; |
850 | static constexpr size_t used_block = __r; | |
8e79468d BK |
851 | |
852 | /** | |
853 | * @brief Constructs a default %discard_block_engine engine. | |
854 | * | |
855 | * The underlying engine is default constructed as well. | |
856 | */ | |
857 | discard_block_engine() | |
858 | : _M_b(), _M_n(0) { } | |
859 | ||
860 | /** | |
861 | * @brief Copy constructs a %discard_block_engine engine. | |
862 | * | |
863 | * Copies an existing base class random number generator. | |
93c66bc6 | 864 | * @param __rng An existing (base class) engine object. |
8e79468d BK |
865 | */ |
866 | explicit | |
93c66bc6 BK |
867 | discard_block_engine(const _RandomNumberEngine& __rng) |
868 | : _M_b(__rng), _M_n(0) { } | |
8e79468d BK |
869 | |
870 | /** | |
871 | * @brief Move constructs a %discard_block_engine engine. | |
872 | * | |
873 | * Copies an existing base class random number generator. | |
93c66bc6 | 874 | * @param __rng An existing (base class) engine object. |
8e79468d BK |
875 | */ |
876 | explicit | |
93c66bc6 BK |
877 | discard_block_engine(_RandomNumberEngine&& __rng) |
878 | : _M_b(std::move(__rng)), _M_n(0) { } | |
8e79468d BK |
879 | |
880 | /** | |
881 | * @brief Seed constructs a %discard_block_engine engine. | |
882 | * | |
883 | * Constructs the underlying generator engine seeded with @p __s. | |
884 | * @param __s A seed value for the base class engine. | |
885 | */ | |
886 | explicit | |
887 | discard_block_engine(result_type __s) | |
888 | : _M_b(__s), _M_n(0) { } | |
889 | ||
890 | /** | |
891 | * @brief Generator construct a %discard_block_engine engine. | |
892 | * | |
893 | * @param __q A seed sequence. | |
894 | */ | |
05eeebfe PC |
895 | template<typename _Sseq, typename = typename |
896 | std::enable_if<!std::is_same<_Sseq, discard_block_engine>::value | |
897 | && !std::is_same<_Sseq, _RandomNumberEngine>::value> | |
898 | ::type> | |
15ecdcc6 PC |
899 | explicit |
900 | discard_block_engine(_Sseq& __q) | |
901 | : _M_b(__q), _M_n(0) | |
902 | { } | |
8e79468d BK |
903 | |
904 | /** | |
905 | * @brief Reseeds the %discard_block_engine object with the default | |
906 | * seed for the underlying base class generator engine. | |
907 | */ | |
908 | void | |
909 | seed() | |
910 | { | |
911 | _M_b.seed(); | |
912 | _M_n = 0; | |
913 | } | |
914 | ||
915 | /** | |
916 | * @brief Reseeds the %discard_block_engine object with the default | |
917 | * seed for the underlying base class generator engine. | |
918 | */ | |
919 | void | |
920 | seed(result_type __s) | |
921 | { | |
922 | _M_b.seed(__s); | |
923 | _M_n = 0; | |
924 | } | |
925 | ||
926 | /** | |
927 | * @brief Reseeds the %discard_block_engine object with the given seed | |
928 | * sequence. | |
929 | * @param __q A seed generator function. | |
930 | */ | |
05eeebfe | 931 | template<typename _Sseq> |
15ecdcc6 PC |
932 | void |
933 | seed(_Sseq& __q) | |
934 | { | |
05eeebfe | 935 | _M_b.seed(__q); |
15ecdcc6 PC |
936 | _M_n = 0; |
937 | } | |
8e79468d BK |
938 | |
939 | /** | |
940 | * @brief Gets a const reference to the underlying generator engine | |
941 | * object. | |
942 | */ | |
943 | const _RandomNumberEngine& | |
18eeaec4 | 944 | base() const noexcept |
8e79468d BK |
945 | { return _M_b; } |
946 | ||
947 | /** | |
948 | * @brief Gets the minimum value in the generated random number range. | |
8e79468d | 949 | */ |
94a86be0 BK |
950 | static constexpr result_type |
951 | min() | |
952 | { return _RandomNumberEngine::min(); } | |
8e79468d BK |
953 | |
954 | /** | |
955 | * @brief Gets the maximum value in the generated random number range. | |
8e79468d | 956 | */ |
94a86be0 BK |
957 | static constexpr result_type |
958 | max() | |
959 | { return _RandomNumberEngine::max(); } | |
8e79468d BK |
960 | |
961 | /** | |
962 | * @brief Discard a sequence of random numbers. | |
8e79468d BK |
963 | */ |
964 | void | |
965 | discard(unsigned long long __z) | |
966 | { | |
967 | for (; __z != 0ULL; --__z) | |
968 | (*this)(); | |
969 | } | |
970 | ||
971 | /** | |
972 | * @brief Gets the next value in the generated random number sequence. | |
973 | */ | |
974 | result_type | |
975 | operator()(); | |
976 | ||
977 | /** | |
978 | * @brief Compares two %discard_block_engine random number generator | |
979 | * objects of the same type for equality. | |
980 | * | |
981 | * @param __lhs A %discard_block_engine random number generator object. | |
982 | * @param __rhs Another %discard_block_engine random number generator | |
983 | * object. | |
984 | * | |
fc05e1ba PC |
985 | * @returns true if the infinite sequences of generated values |
986 | * would be equal, false otherwise. | |
8e79468d BK |
987 | */ |
988 | friend bool | |
989 | operator==(const discard_block_engine& __lhs, | |
990 | const discard_block_engine& __rhs) | |
fc05e1ba | 991 | { return __lhs._M_b == __rhs._M_b && __lhs._M_n == __rhs._M_n; } |
8e79468d BK |
992 | |
993 | /** | |
994 | * @brief Inserts the current state of a %discard_block_engine random | |
995 | * number generator engine @p __x into the output stream | |
996 | * @p __os. | |
997 | * | |
998 | * @param __os An output stream. | |
999 | * @param __x A %discard_block_engine random number generator engine. | |
1000 | * | |
1001 | * @returns The output stream with the state of @p __x inserted or in | |
1002 | * an error state. | |
1003 | */ | |
1004 | template<typename _RandomNumberEngine1, size_t __p1, size_t __r1, | |
1005 | typename _CharT, typename _Traits> | |
1006 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 | 1007 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
94986f6d | 1008 | const std::discard_block_engine<_RandomNumberEngine1, |
93c66bc6 | 1009 | __p1, __r1>& __x); |
8e79468d BK |
1010 | |
1011 | /** | |
1012 | * @brief Extracts the current state of a % subtract_with_carry_engine | |
1013 | * random number generator engine @p __x from the input stream | |
1014 | * @p __is. | |
1015 | * | |
1016 | * @param __is An input stream. | |
1017 | * @param __x A %discard_block_engine random number generator engine. | |
1018 | * | |
1019 | * @returns The input stream with the state of @p __x extracted or in | |
1020 | * an error state. | |
1021 | */ | |
1022 | template<typename _RandomNumberEngine1, size_t __p1, size_t __r1, | |
1023 | typename _CharT, typename _Traits> | |
1024 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 | 1025 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
94986f6d | 1026 | std::discard_block_engine<_RandomNumberEngine1, |
93c66bc6 | 1027 | __p1, __r1>& __x); |
8e79468d BK |
1028 | |
1029 | private: | |
1030 | _RandomNumberEngine _M_b; | |
1031 | size_t _M_n; | |
1032 | }; | |
1033 | ||
fc05e1ba PC |
1034 | /** |
1035 | * @brief Compares two %discard_block_engine random number generator | |
1036 | * objects of the same type for inequality. | |
1037 | * | |
1038 | * @param __lhs A %discard_block_engine random number generator object. | |
1039 | * @param __rhs Another %discard_block_engine random number generator | |
1040 | * object. | |
1041 | * | |
1042 | * @returns true if the infinite sequences of generated values | |
1043 | * would be different, false otherwise. | |
1044 | */ | |
1045 | template<typename _RandomNumberEngine, size_t __p, size_t __r> | |
1046 | inline bool | |
1047 | operator!=(const std::discard_block_engine<_RandomNumberEngine, __p, | |
1048 | __r>& __lhs, | |
1049 | const std::discard_block_engine<_RandomNumberEngine, __p, | |
1050 | __r>& __rhs) | |
1051 | { return !(__lhs == __rhs); } | |
1052 | ||
1053 | ||
8e79468d BK |
1054 | /** |
1055 | * Produces random numbers by combining random numbers from some base | |
1056 | * engine to produce random numbers with a specifies number of bits @p __w. | |
1057 | */ | |
1058 | template<typename _RandomNumberEngine, size_t __w, typename _UIntType> | |
1059 | class independent_bits_engine | |
1060 | { | |
0cded43d JW |
1061 | static_assert(std::is_unsigned<_UIntType>::value, |
1062 | "result_type must be an unsigned integral type"); | |
77e3c516 | 1063 | static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits, |
f6d08b43 | 1064 | "template argument substituting __w out of bounds"); |
8e79468d BK |
1065 | |
1066 | public: | |
1067 | /** The type of the generated random value. */ | |
1068 | typedef _UIntType result_type; | |
1069 | ||
1070 | /** | |
1071 | * @brief Constructs a default %independent_bits_engine engine. | |
1072 | * | |
1073 | * The underlying engine is default constructed as well. | |
1074 | */ | |
1075 | independent_bits_engine() | |
1076 | : _M_b() { } | |
1077 | ||
1078 | /** | |
1079 | * @brief Copy constructs a %independent_bits_engine engine. | |
1080 | * | |
1081 | * Copies an existing base class random number generator. | |
93c66bc6 | 1082 | * @param __rng An existing (base class) engine object. |
8e79468d BK |
1083 | */ |
1084 | explicit | |
93c66bc6 BK |
1085 | independent_bits_engine(const _RandomNumberEngine& __rng) |
1086 | : _M_b(__rng) { } | |
8e79468d BK |
1087 | |
1088 | /** | |
1089 | * @brief Move constructs a %independent_bits_engine engine. | |
1090 | * | |
1091 | * Copies an existing base class random number generator. | |
93c66bc6 | 1092 | * @param __rng An existing (base class) engine object. |
8e79468d BK |
1093 | */ |
1094 | explicit | |
93c66bc6 BK |
1095 | independent_bits_engine(_RandomNumberEngine&& __rng) |
1096 | : _M_b(std::move(__rng)) { } | |
8e79468d BK |
1097 | |
1098 | /** | |
1099 | * @brief Seed constructs a %independent_bits_engine engine. | |
1100 | * | |
1101 | * Constructs the underlying generator engine seeded with @p __s. | |
1102 | * @param __s A seed value for the base class engine. | |
1103 | */ | |
1104 | explicit | |
1105 | independent_bits_engine(result_type __s) | |
1106 | : _M_b(__s) { } | |
1107 | ||
1108 | /** | |
1109 | * @brief Generator construct a %independent_bits_engine engine. | |
1110 | * | |
1111 | * @param __q A seed sequence. | |
1112 | */ | |
05eeebfe PC |
1113 | template<typename _Sseq, typename = typename |
1114 | std::enable_if<!std::is_same<_Sseq, independent_bits_engine>::value | |
1115 | && !std::is_same<_Sseq, _RandomNumberEngine>::value> | |
1116 | ::type> | |
15ecdcc6 PC |
1117 | explicit |
1118 | independent_bits_engine(_Sseq& __q) | |
1119 | : _M_b(__q) | |
1120 | { } | |
8e79468d BK |
1121 | |
1122 | /** | |
1123 | * @brief Reseeds the %independent_bits_engine object with the default | |
1124 | * seed for the underlying base class generator engine. | |
1125 | */ | |
1126 | void | |
1127 | seed() | |
1128 | { _M_b.seed(); } | |
1129 | ||
1130 | /** | |
1131 | * @brief Reseeds the %independent_bits_engine object with the default | |
1132 | * seed for the underlying base class generator engine. | |
1133 | */ | |
1134 | void | |
1135 | seed(result_type __s) | |
1136 | { _M_b.seed(__s); } | |
1137 | ||
1138 | /** | |
1139 | * @brief Reseeds the %independent_bits_engine object with the given | |
1140 | * seed sequence. | |
1141 | * @param __q A seed generator function. | |
1142 | */ | |
05eeebfe | 1143 | template<typename _Sseq> |
15ecdcc6 PC |
1144 | void |
1145 | seed(_Sseq& __q) | |
05eeebfe | 1146 | { _M_b.seed(__q); } |
8e79468d BK |
1147 | |
1148 | /** | |
1149 | * @brief Gets a const reference to the underlying generator engine | |
1150 | * object. | |
1151 | */ | |
1152 | const _RandomNumberEngine& | |
18eeaec4 | 1153 | base() const noexcept |
8e79468d BK |
1154 | { return _M_b; } |
1155 | ||
1156 | /** | |
1157 | * @brief Gets the minimum value in the generated random number range. | |
8e79468d | 1158 | */ |
94a86be0 BK |
1159 | static constexpr result_type |
1160 | min() | |
8e79468d BK |
1161 | { return 0U; } |
1162 | ||
1163 | /** | |
1164 | * @brief Gets the maximum value in the generated random number range. | |
8e79468d | 1165 | */ |
94a86be0 BK |
1166 | static constexpr result_type |
1167 | max() | |
6855fe45 | 1168 | { return __detail::_Shift<_UIntType, __w>::__value - 1; } |
8e79468d BK |
1169 | |
1170 | /** | |
1171 | * @brief Discard a sequence of random numbers. | |
8e79468d BK |
1172 | */ |
1173 | void | |
1174 | discard(unsigned long long __z) | |
1175 | { | |
1176 | for (; __z != 0ULL; --__z) | |
1177 | (*this)(); | |
1178 | } | |
1179 | ||
1180 | /** | |
1181 | * @brief Gets the next value in the generated random number sequence. | |
1182 | */ | |
1183 | result_type | |
1184 | operator()(); | |
1185 | ||
1186 | /** | |
1187 | * @brief Compares two %independent_bits_engine random number generator | |
1188 | * objects of the same type for equality. | |
1189 | * | |
1190 | * @param __lhs A %independent_bits_engine random number generator | |
1191 | * object. | |
1192 | * @param __rhs Another %independent_bits_engine random number generator | |
1193 | * object. | |
1194 | * | |
fc05e1ba PC |
1195 | * @returns true if the infinite sequences of generated values |
1196 | * would be equal, false otherwise. | |
8e79468d BK |
1197 | */ |
1198 | friend bool | |
1199 | operator==(const independent_bits_engine& __lhs, | |
1200 | const independent_bits_engine& __rhs) | |
1201 | { return __lhs._M_b == __rhs._M_b; } | |
1202 | ||
1203 | /** | |
1204 | * @brief Extracts the current state of a % subtract_with_carry_engine | |
1205 | * random number generator engine @p __x from the input stream | |
1206 | * @p __is. | |
1207 | * | |
1208 | * @param __is An input stream. | |
1209 | * @param __x A %independent_bits_engine random number generator | |
1210 | * engine. | |
1211 | * | |
1212 | * @returns The input stream with the state of @p __x extracted or in | |
1213 | * an error state. | |
1214 | */ | |
1215 | template<typename _CharT, typename _Traits> | |
1216 | friend std::basic_istream<_CharT, _Traits>& | |
1217 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
94986f6d | 1218 | std::independent_bits_engine<_RandomNumberEngine, |
8e79468d BK |
1219 | __w, _UIntType>& __x) |
1220 | { | |
1221 | __is >> __x._M_b; | |
1222 | return __is; | |
1223 | } | |
1224 | ||
1225 | private: | |
1226 | _RandomNumberEngine _M_b; | |
1227 | }; | |
1228 | ||
fc05e1ba PC |
1229 | /** |
1230 | * @brief Compares two %independent_bits_engine random number generator | |
1231 | * objects of the same type for inequality. | |
1232 | * | |
1233 | * @param __lhs A %independent_bits_engine random number generator | |
1234 | * object. | |
1235 | * @param __rhs Another %independent_bits_engine random number generator | |
1236 | * object. | |
1237 | * | |
1238 | * @returns true if the infinite sequences of generated values | |
1239 | * would be different, false otherwise. | |
1240 | */ | |
1241 | template<typename _RandomNumberEngine, size_t __w, typename _UIntType> | |
1242 | inline bool | |
1243 | operator!=(const std::independent_bits_engine<_RandomNumberEngine, __w, | |
1244 | _UIntType>& __lhs, | |
1245 | const std::independent_bits_engine<_RandomNumberEngine, __w, | |
1246 | _UIntType>& __rhs) | |
1247 | { return !(__lhs == __rhs); } | |
1248 | ||
8e79468d BK |
1249 | /** |
1250 | * @brief Inserts the current state of a %independent_bits_engine random | |
1251 | * number generator engine @p __x into the output stream @p __os. | |
1252 | * | |
1253 | * @param __os An output stream. | |
1254 | * @param __x A %independent_bits_engine random number generator engine. | |
1255 | * | |
1256 | * @returns The output stream with the state of @p __x inserted or in | |
1257 | * an error state. | |
1258 | */ | |
1259 | template<typename _RandomNumberEngine, size_t __w, typename _UIntType, | |
1260 | typename _CharT, typename _Traits> | |
1261 | std::basic_ostream<_CharT, _Traits>& | |
1262 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
94986f6d | 1263 | const std::independent_bits_engine<_RandomNumberEngine, |
8e79468d BK |
1264 | __w, _UIntType>& __x) |
1265 | { | |
1266 | __os << __x.base(); | |
1267 | return __os; | |
1268 | } | |
1269 | ||
fc05e1ba | 1270 | |
8e79468d BK |
1271 | /** |
1272 | * @brief Produces random numbers by combining random numbers from some | |
1273 | * base engine to produce random numbers with a specifies number of bits | |
0cded43d | 1274 | * @p __k. |
8e79468d BK |
1275 | */ |
1276 | template<typename _RandomNumberEngine, size_t __k> | |
1277 | class shuffle_order_engine | |
1278 | { | |
a20c8540 | 1279 | static_assert(1u <= __k, "template argument substituting " |
f6d08b43 | 1280 | "__k out of bound"); |
8e79468d BK |
1281 | |
1282 | public: | |
1283 | /** The type of the generated random value. */ | |
1284 | typedef typename _RandomNumberEngine::result_type result_type; | |
1285 | ||
94a86be0 | 1286 | static constexpr size_t table_size = __k; |
8e79468d BK |
1287 | |
1288 | /** | |
1289 | * @brief Constructs a default %shuffle_order_engine engine. | |
1290 | * | |
1291 | * The underlying engine is default constructed as well. | |
1292 | */ | |
1293 | shuffle_order_engine() | |
1294 | : _M_b() | |
1295 | { _M_initialize(); } | |
1296 | ||
1297 | /** | |
1298 | * @brief Copy constructs a %shuffle_order_engine engine. | |
1299 | * | |
1300 | * Copies an existing base class random number generator. | |
93c66bc6 | 1301 | * @param __rng An existing (base class) engine object. |
8e79468d BK |
1302 | */ |
1303 | explicit | |
93c66bc6 BK |
1304 | shuffle_order_engine(const _RandomNumberEngine& __rng) |
1305 | : _M_b(__rng) | |
8e79468d BK |
1306 | { _M_initialize(); } |
1307 | ||
1308 | /** | |
1309 | * @brief Move constructs a %shuffle_order_engine engine. | |
1310 | * | |
1311 | * Copies an existing base class random number generator. | |
93c66bc6 | 1312 | * @param __rng An existing (base class) engine object. |
8e79468d BK |
1313 | */ |
1314 | explicit | |
93c66bc6 BK |
1315 | shuffle_order_engine(_RandomNumberEngine&& __rng) |
1316 | : _M_b(std::move(__rng)) | |
8e79468d BK |
1317 | { _M_initialize(); } |
1318 | ||
1319 | /** | |
1320 | * @brief Seed constructs a %shuffle_order_engine engine. | |
1321 | * | |
1322 | * Constructs the underlying generator engine seeded with @p __s. | |
1323 | * @param __s A seed value for the base class engine. | |
1324 | */ | |
1325 | explicit | |
1326 | shuffle_order_engine(result_type __s) | |
1327 | : _M_b(__s) | |
1328 | { _M_initialize(); } | |
1329 | ||
1330 | /** | |
1331 | * @brief Generator construct a %shuffle_order_engine engine. | |
1332 | * | |
1333 | * @param __q A seed sequence. | |
1334 | */ | |
05eeebfe PC |
1335 | template<typename _Sseq, typename = typename |
1336 | std::enable_if<!std::is_same<_Sseq, shuffle_order_engine>::value | |
1337 | && !std::is_same<_Sseq, _RandomNumberEngine>::value> | |
1338 | ::type> | |
15ecdcc6 PC |
1339 | explicit |
1340 | shuffle_order_engine(_Sseq& __q) | |
1341 | : _M_b(__q) | |
1342 | { _M_initialize(); } | |
8e79468d BK |
1343 | |
1344 | /** | |
94986f6d PC |
1345 | * @brief Reseeds the %shuffle_order_engine object with the default seed |
1346 | for the underlying base class generator engine. | |
8e79468d BK |
1347 | */ |
1348 | void | |
1349 | seed() | |
1350 | { | |
1351 | _M_b.seed(); | |
1352 | _M_initialize(); | |
1353 | } | |
1354 | ||
1355 | /** | |
1356 | * @brief Reseeds the %shuffle_order_engine object with the default seed | |
1357 | * for the underlying base class generator engine. | |
1358 | */ | |
1359 | void | |
1360 | seed(result_type __s) | |
1361 | { | |
1362 | _M_b.seed(__s); | |
1363 | _M_initialize(); | |
1364 | } | |
1365 | ||
1366 | /** | |
1367 | * @brief Reseeds the %shuffle_order_engine object with the given seed | |
1368 | * sequence. | |
1369 | * @param __q A seed generator function. | |
1370 | */ | |
05eeebfe | 1371 | template<typename _Sseq> |
15ecdcc6 PC |
1372 | void |
1373 | seed(_Sseq& __q) | |
1374 | { | |
05eeebfe | 1375 | _M_b.seed(__q); |
15ecdcc6 PC |
1376 | _M_initialize(); |
1377 | } | |
8e79468d BK |
1378 | |
1379 | /** | |
1380 | * Gets a const reference to the underlying generator engine object. | |
1381 | */ | |
1382 | const _RandomNumberEngine& | |
18eeaec4 | 1383 | base() const noexcept |
8e79468d BK |
1384 | { return _M_b; } |
1385 | ||
1386 | /** | |
1387 | * Gets the minimum value in the generated random number range. | |
8e79468d | 1388 | */ |
94a86be0 BK |
1389 | static constexpr result_type |
1390 | min() | |
1391 | { return _RandomNumberEngine::min(); } | |
8e79468d BK |
1392 | |
1393 | /** | |
1394 | * Gets the maximum value in the generated random number range. | |
8e79468d | 1395 | */ |
94a86be0 BK |
1396 | static constexpr result_type |
1397 | max() | |
1398 | { return _RandomNumberEngine::max(); } | |
8e79468d BK |
1399 | |
1400 | /** | |
1401 | * Discard a sequence of random numbers. | |
8e79468d BK |
1402 | */ |
1403 | void | |
1404 | discard(unsigned long long __z) | |
1405 | { | |
1406 | for (; __z != 0ULL; --__z) | |
1407 | (*this)(); | |
1408 | } | |
1409 | ||
1410 | /** | |
1411 | * Gets the next value in the generated random number sequence. | |
1412 | */ | |
1413 | result_type | |
1414 | operator()(); | |
1415 | ||
1416 | /** | |
1417 | * Compares two %shuffle_order_engine random number generator objects | |
1418 | * of the same type for equality. | |
1419 | * | |
1420 | * @param __lhs A %shuffle_order_engine random number generator object. | |
1421 | * @param __rhs Another %shuffle_order_engine random number generator | |
1422 | * object. | |
1423 | * | |
fc05e1ba PC |
1424 | * @returns true if the infinite sequences of generated values |
1425 | * would be equal, false otherwise. | |
1426 | */ | |
8e79468d BK |
1427 | friend bool |
1428 | operator==(const shuffle_order_engine& __lhs, | |
1429 | const shuffle_order_engine& __rhs) | |
31645179 PC |
1430 | { return (__lhs._M_b == __rhs._M_b |
1431 | && std::equal(__lhs._M_v, __lhs._M_v + __k, __rhs._M_v) | |
1432 | && __lhs._M_y == __rhs._M_y); } | |
8e79468d BK |
1433 | |
1434 | /** | |
1435 | * @brief Inserts the current state of a %shuffle_order_engine random | |
1436 | * number generator engine @p __x into the output stream | |
1437 | @p __os. | |
1438 | * | |
1439 | * @param __os An output stream. | |
1440 | * @param __x A %shuffle_order_engine random number generator engine. | |
1441 | * | |
1442 | * @returns The output stream with the state of @p __x inserted or in | |
1443 | * an error state. | |
1444 | */ | |
1445 | template<typename _RandomNumberEngine1, size_t __k1, | |
1446 | typename _CharT, typename _Traits> | |
1447 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 | 1448 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
94986f6d | 1449 | const std::shuffle_order_engine<_RandomNumberEngine1, |
93c66bc6 | 1450 | __k1>& __x); |
8e79468d BK |
1451 | |
1452 | /** | |
1453 | * @brief Extracts the current state of a % subtract_with_carry_engine | |
1454 | * random number generator engine @p __x from the input stream | |
1455 | * @p __is. | |
1456 | * | |
1457 | * @param __is An input stream. | |
1458 | * @param __x A %shuffle_order_engine random number generator engine. | |
1459 | * | |
1460 | * @returns The input stream with the state of @p __x extracted or in | |
1461 | * an error state. | |
1462 | */ | |
1463 | template<typename _RandomNumberEngine1, size_t __k1, | |
1464 | typename _CharT, typename _Traits> | |
1465 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
1466 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
1467 | std::shuffle_order_engine<_RandomNumberEngine1, __k1>& __x); | |
8e79468d BK |
1468 | |
1469 | private: | |
1470 | void _M_initialize() | |
1471 | { | |
1472 | for (size_t __i = 0; __i < __k; ++__i) | |
1473 | _M_v[__i] = _M_b(); | |
1474 | _M_y = _M_b(); | |
1475 | } | |
1476 | ||
1477 | _RandomNumberEngine _M_b; | |
1478 | result_type _M_v[__k]; | |
1479 | result_type _M_y; | |
1480 | }; | |
1481 | ||
fc05e1ba PC |
1482 | /** |
1483 | * Compares two %shuffle_order_engine random number generator objects | |
1484 | * of the same type for inequality. | |
1485 | * | |
1486 | * @param __lhs A %shuffle_order_engine random number generator object. | |
1487 | * @param __rhs Another %shuffle_order_engine random number generator | |
1488 | * object. | |
1489 | * | |
1490 | * @returns true if the infinite sequences of generated values | |
1491 | * would be different, false otherwise. | |
1492 | */ | |
1493 | template<typename _RandomNumberEngine, size_t __k> | |
1494 | inline bool | |
1495 | operator!=(const std::shuffle_order_engine<_RandomNumberEngine, | |
1496 | __k>& __lhs, | |
1497 | const std::shuffle_order_engine<_RandomNumberEngine, | |
1498 | __k>& __rhs) | |
1499 | { return !(__lhs == __rhs); } | |
1500 | ||
1501 | ||
8e79468d BK |
1502 | /** |
1503 | * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller. | |
1504 | */ | |
1505 | typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL> | |
1506 | minstd_rand0; | |
1507 | ||
1508 | /** | |
6cc5a790 | 1509 | * An alternative LCR (Lehmer Generator function). |
8e79468d BK |
1510 | */ |
1511 | typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL> | |
1512 | minstd_rand; | |
1513 | ||
1514 | /** | |
1515 | * The classic Mersenne Twister. | |
1516 | * | |
1517 | * Reference: | |
2a60a9f6 BK |
1518 | * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally |
1519 | * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions | |
8e79468d BK |
1520 | * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30. |
1521 | */ | |
1522 | typedef mersenne_twister_engine< | |
1523 | uint_fast32_t, | |
1524 | 32, 624, 397, 31, | |
1525 | 0x9908b0dfUL, 11, | |
1526 | 0xffffffffUL, 7, | |
1527 | 0x9d2c5680UL, 15, | |
1528 | 0xefc60000UL, 18, 1812433253UL> mt19937; | |
1529 | ||
1530 | /** | |
1531 | * An alternative Mersenne Twister. | |
1532 | */ | |
1533 | typedef mersenne_twister_engine< | |
1534 | uint_fast64_t, | |
1535 | 64, 312, 156, 31, | |
1536 | 0xb5026f5aa96619e9ULL, 29, | |
1537 | 0x5555555555555555ULL, 17, | |
1538 | 0x71d67fffeda60000ULL, 37, | |
1539 | 0xfff7eee000000000ULL, 43, | |
1540 | 6364136223846793005ULL> mt19937_64; | |
1541 | ||
8e79468d BK |
1542 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> |
1543 | ranlux24_base; | |
1544 | ||
8e79468d BK |
1545 | typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> |
1546 | ranlux48_base; | |
1547 | ||
b94f4bef PC |
1548 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
1549 | ||
8e79468d BK |
1550 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
1551 | ||
8e79468d BK |
1552 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
1553 | ||
8e79468d BK |
1554 | typedef minstd_rand0 default_random_engine; |
1555 | ||
1556 | /** | |
1557 | * A standard interface to a platform-specific non-deterministic | |
1558 | * random number generator (if any are available). | |
1559 | */ | |
1560 | class random_device | |
1561 | { | |
1562 | public: | |
1563 | /** The type of the generated random value. */ | |
1564 | typedef unsigned int result_type; | |
1565 | ||
1566 | // constructors, destructors and member functions | |
1567 | ||
1568 | #ifdef _GLIBCXX_USE_RANDOM_TR1 | |
1569 | ||
1570 | explicit | |
a8c3f4c9 | 1571 | random_device(const std::string& __token = "default") |
8e79468d | 1572 | { |
a8c3f4c9 | 1573 | _M_init(__token); |
8e79468d BK |
1574 | } |
1575 | ||
1576 | ~random_device() | |
a8c3f4c9 | 1577 | { _M_fini(); } |
8e79468d BK |
1578 | |
1579 | #else | |
1580 | ||
1581 | explicit | |
1582 | random_device(const std::string& __token = "mt19937") | |
25270f5e | 1583 | { _M_init_pretr1(__token); } |
8e79468d BK |
1584 | |
1585 | public: | |
1586 | ||
1587 | #endif | |
1588 | ||
deaf34a9 PC |
1589 | static constexpr result_type |
1590 | min() | |
8e79468d BK |
1591 | { return std::numeric_limits<result_type>::min(); } |
1592 | ||
deaf34a9 PC |
1593 | static constexpr result_type |
1594 | max() | |
8e79468d BK |
1595 | { return std::numeric_limits<result_type>::max(); } |
1596 | ||
1597 | double | |
18eeaec4 | 1598 | entropy() const noexcept |
78aa76df XR |
1599 | { |
1600 | #ifdef _GLIBCXX_USE_RANDOM_TR1 | |
1601 | return this->_M_getentropy(); | |
1602 | #else | |
1603 | return 0.0; | |
1604 | #endif | |
1605 | } | |
8e79468d BK |
1606 | |
1607 | result_type | |
1608 | operator()() | |
1609 | { | |
1610 | #ifdef _GLIBCXX_USE_RANDOM_TR1 | |
a8c3f4c9 | 1611 | return this->_M_getval(); |
8e79468d | 1612 | #else |
a8c3f4c9 | 1613 | return this->_M_getval_pretr1(); |
8e79468d BK |
1614 | #endif |
1615 | } | |
1616 | ||
1617 | // No copy functions. | |
1618 | random_device(const random_device&) = delete; | |
1619 | void operator=(const random_device&) = delete; | |
1620 | ||
1621 | private: | |
1622 | ||
a8c3f4c9 UD |
1623 | void _M_init(const std::string& __token); |
1624 | void _M_init_pretr1(const std::string& __token); | |
1625 | void _M_fini(); | |
1626 | ||
1627 | result_type _M_getval(); | |
1628 | result_type _M_getval_pretr1(); | |
78aa76df | 1629 | double _M_getentropy() const noexcept; |
a8c3f4c9 UD |
1630 | |
1631 | union | |
1632 | { | |
821f6f1b PC |
1633 | void* _M_file; |
1634 | mt19937 _M_mt; | |
1635 | }; | |
8e79468d BK |
1636 | }; |
1637 | ||
037181bc | 1638 | /* @} */ // group random_generators |
8e79468d BK |
1639 | |
1640 | /** | |
037181bc BK |
1641 | * @addtogroup random_distributions Random Number Distributions |
1642 | * @ingroup random | |
8e79468d BK |
1643 | * @{ |
1644 | */ | |
1645 | ||
1646 | /** | |
94a86be0 | 1647 | * @addtogroup random_distributions_uniform Uniform Distributions |
037181bc | 1648 | * @ingroup random_distributions |
8e79468d BK |
1649 | * @{ |
1650 | */ | |
1651 | ||
0cded43d | 1652 | // std::uniform_int_distribution is defined in <bits/uniform_int_dist.h> |
8e79468d | 1653 | |
fc05e1ba PC |
1654 | /** |
1655 | * @brief Return true if two uniform integer distributions have | |
1656 | * different parameters. | |
1657 | */ | |
1658 | template<typename _IntType> | |
1659 | inline bool | |
1660 | operator!=(const std::uniform_int_distribution<_IntType>& __d1, | |
1661 | const std::uniform_int_distribution<_IntType>& __d2) | |
1662 | { return !(__d1 == __d2); } | |
1663 | ||
8e79468d BK |
1664 | /** |
1665 | * @brief Inserts a %uniform_int_distribution random number | |
1666 | * distribution @p __x into the output stream @p os. | |
1667 | * | |
1668 | * @param __os An output stream. | |
1669 | * @param __x A %uniform_int_distribution random number distribution. | |
1670 | * | |
1671 | * @returns The output stream with the state of @p __x inserted or in | |
1672 | * an error state. | |
1673 | */ | |
1674 | template<typename _IntType, typename _CharT, typename _Traits> | |
1675 | std::basic_ostream<_CharT, _Traits>& | |
94986f6d PC |
1676 | operator<<(std::basic_ostream<_CharT, _Traits>&, |
1677 | const std::uniform_int_distribution<_IntType>&); | |
8e79468d BK |
1678 | |
1679 | /** | |
1680 | * @brief Extracts a %uniform_int_distribution random number distribution | |
1681 | * @p __x from the input stream @p __is. | |
1682 | * | |
1683 | * @param __is An input stream. | |
1684 | * @param __x A %uniform_int_distribution random number generator engine. | |
1685 | * | |
1686 | * @returns The input stream with @p __x extracted or in an error state. | |
1687 | */ | |
1688 | template<typename _IntType, typename _CharT, typename _Traits> | |
1689 | std::basic_istream<_CharT, _Traits>& | |
94986f6d PC |
1690 | operator>>(std::basic_istream<_CharT, _Traits>&, |
1691 | std::uniform_int_distribution<_IntType>&); | |
8e79468d BK |
1692 | |
1693 | ||
1694 | /** | |
1695 | * @brief Uniform continuous distribution for random numbers. | |
1696 | * | |
1697 | * A continuous random distribution on the range [min, max) with equal | |
1698 | * probability throughout the range. The URNG should be real-valued and | |
1699 | * deliver number in the range [0, 1). | |
1700 | */ | |
1701 | template<typename _RealType = double> | |
1702 | class uniform_real_distribution | |
1703 | { | |
77e3c516 | 1704 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 1705 | "result_type must be a floating point type"); |
77e3c516 | 1706 | |
8e79468d BK |
1707 | public: |
1708 | /** The type of the range of the distribution. */ | |
1709 | typedef _RealType result_type; | |
12905f10 | 1710 | |
8e79468d BK |
1711 | /** Parameter type. */ |
1712 | struct param_type | |
1713 | { | |
1714 | typedef uniform_real_distribution<_RealType> distribution_type; | |
1715 | ||
1716 | explicit | |
1717 | param_type(_RealType __a = _RealType(0), | |
1718 | _RealType __b = _RealType(1)) | |
1719 | : _M_a(__a), _M_b(__b) | |
1720 | { | |
2f1e8e7c | 1721 | __glibcxx_assert(_M_a <= _M_b); |
8e79468d BK |
1722 | } |
1723 | ||
1724 | result_type | |
1725 | a() const | |
1726 | { return _M_a; } | |
1727 | ||
1728 | result_type | |
1729 | b() const | |
1730 | { return _M_b; } | |
1731 | ||
fc05e1ba PC |
1732 | friend bool |
1733 | operator==(const param_type& __p1, const param_type& __p2) | |
1734 | { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } | |
1735 | ||
12905f10 JW |
1736 | friend bool |
1737 | operator!=(const param_type& __p1, const param_type& __p2) | |
1738 | { return !(__p1 == __p2); } | |
1739 | ||
8e79468d BK |
1740 | private: |
1741 | _RealType _M_a; | |
1742 | _RealType _M_b; | |
1743 | }; | |
1744 | ||
1745 | public: | |
1746 | /** | |
1747 | * @brief Constructs a uniform_real_distribution object. | |
1748 | * | |
93c66bc6 BK |
1749 | * @param __a [IN] The lower bound of the distribution. |
1750 | * @param __b [IN] The upper bound of the distribution. | |
8e79468d BK |
1751 | */ |
1752 | explicit | |
1753 | uniform_real_distribution(_RealType __a = _RealType(0), | |
1754 | _RealType __b = _RealType(1)) | |
1755 | : _M_param(__a, __b) | |
1756 | { } | |
1757 | ||
1758 | explicit | |
1759 | uniform_real_distribution(const param_type& __p) | |
1760 | : _M_param(__p) | |
1761 | { } | |
1762 | ||
1763 | /** | |
1764 | * @brief Resets the distribution state. | |
1765 | * | |
1766 | * Does nothing for the uniform real distribution. | |
1767 | */ | |
1768 | void | |
1769 | reset() { } | |
1770 | ||
1771 | result_type | |
1772 | a() const | |
1773 | { return _M_param.a(); } | |
1774 | ||
1775 | result_type | |
1776 | b() const | |
1777 | { return _M_param.b(); } | |
1778 | ||
247d8075 PC |
1779 | /** |
1780 | * @brief Returns the parameter set of the distribution. | |
1781 | */ | |
1782 | param_type | |
1783 | param() const | |
1784 | { return _M_param; } | |
1785 | ||
1786 | /** | |
1787 | * @brief Sets the parameter set of the distribution. | |
1788 | * @param __param The new parameter set of the distribution. | |
1789 | */ | |
1790 | void | |
1791 | param(const param_type& __param) | |
1792 | { _M_param = __param; } | |
1793 | ||
8e79468d BK |
1794 | /** |
1795 | * @brief Returns the inclusive lower bound of the distribution range. | |
1796 | */ | |
1797 | result_type | |
1798 | min() const | |
1799 | { return this->a(); } | |
1800 | ||
1801 | /** | |
1802 | * @brief Returns the inclusive upper bound of the distribution range. | |
1803 | */ | |
1804 | result_type | |
1805 | max() const | |
1806 | { return this->b(); } | |
1807 | ||
1808 | /** | |
247d8075 | 1809 | * @brief Generating functions. |
8e79468d | 1810 | */ |
8e79468d BK |
1811 | template<typename _UniformRandomNumberGenerator> |
1812 | result_type | |
1813 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 1814 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
1815 | |
1816 | template<typename _UniformRandomNumberGenerator> | |
1817 | result_type | |
1818 | operator()(_UniformRandomNumberGenerator& __urng, | |
1819 | const param_type& __p) | |
1820 | { | |
1821 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
1822 | __aurng(__urng); | |
1823 | return (__aurng() * (__p.b() - __p.a())) + __p.a(); | |
1824 | } | |
1825 | ||
7b93bdde UD |
1826 | template<typename _ForwardIterator, |
1827 | typename _UniformRandomNumberGenerator> | |
1828 | void | |
1829 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
1830 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 1831 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
1832 | |
1833 | template<typename _ForwardIterator, | |
1834 | typename _UniformRandomNumberGenerator> | |
1835 | void | |
1836 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
1837 | _UniformRandomNumberGenerator& __urng, | |
1838 | const param_type& __p) | |
1839 | { this->__generate_impl(__f, __t, __urng, __p); } | |
1840 | ||
1841 | template<typename _UniformRandomNumberGenerator> | |
1842 | void | |
1843 | __generate(result_type* __f, result_type* __t, | |
1844 | _UniformRandomNumberGenerator& __urng, | |
1845 | const param_type& __p) | |
1846 | { this->__generate_impl(__f, __t, __urng, __p); } | |
1847 | ||
5bcb3b4d PC |
1848 | /** |
1849 | * @brief Return true if two uniform real distributions have | |
1850 | * the same parameters. | |
1851 | */ | |
1852 | friend bool | |
1853 | operator==(const uniform_real_distribution& __d1, | |
1854 | const uniform_real_distribution& __d2) | |
1855 | { return __d1._M_param == __d2._M_param; } | |
1856 | ||
8e79468d | 1857 | private: |
7b93bdde UD |
1858 | template<typename _ForwardIterator, |
1859 | typename _UniformRandomNumberGenerator> | |
1860 | void | |
1861 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
1862 | _UniformRandomNumberGenerator& __urng, | |
1863 | const param_type& __p); | |
1864 | ||
8e79468d BK |
1865 | param_type _M_param; |
1866 | }; | |
1867 | ||
fc05e1ba PC |
1868 | /** |
1869 | * @brief Return true if two uniform real distributions have | |
1870 | * different parameters. | |
1871 | */ | |
1872 | template<typename _IntType> | |
1873 | inline bool | |
1874 | operator!=(const std::uniform_real_distribution<_IntType>& __d1, | |
1875 | const std::uniform_real_distribution<_IntType>& __d2) | |
1876 | { return !(__d1 == __d2); } | |
1877 | ||
8e79468d BK |
1878 | /** |
1879 | * @brief Inserts a %uniform_real_distribution random number | |
1880 | * distribution @p __x into the output stream @p __os. | |
1881 | * | |
1882 | * @param __os An output stream. | |
1883 | * @param __x A %uniform_real_distribution random number distribution. | |
1884 | * | |
1885 | * @returns The output stream with the state of @p __x inserted or in | |
1886 | * an error state. | |
1887 | */ | |
1888 | template<typename _RealType, typename _CharT, typename _Traits> | |
1889 | std::basic_ostream<_CharT, _Traits>& | |
94986f6d PC |
1890 | operator<<(std::basic_ostream<_CharT, _Traits>&, |
1891 | const std::uniform_real_distribution<_RealType>&); | |
8e79468d BK |
1892 | |
1893 | /** | |
1894 | * @brief Extracts a %uniform_real_distribution random number distribution | |
1895 | * @p __x from the input stream @p __is. | |
1896 | * | |
1897 | * @param __is An input stream. | |
1898 | * @param __x A %uniform_real_distribution random number generator engine. | |
1899 | * | |
1900 | * @returns The input stream with @p __x extracted or in an error state. | |
1901 | */ | |
1902 | template<typename _RealType, typename _CharT, typename _Traits> | |
1903 | std::basic_istream<_CharT, _Traits>& | |
94986f6d PC |
1904 | operator>>(std::basic_istream<_CharT, _Traits>&, |
1905 | std::uniform_real_distribution<_RealType>&); | |
8e79468d | 1906 | |
037181bc | 1907 | /* @} */ // group random_distributions_uniform |
8e79468d BK |
1908 | |
1909 | /** | |
94a86be0 | 1910 | * @addtogroup random_distributions_normal Normal Distributions |
037181bc | 1911 | * @ingroup random_distributions |
8e79468d BK |
1912 | * @{ |
1913 | */ | |
1914 | ||
1915 | /** | |
1916 | * @brief A normal continuous distribution for random numbers. | |
1917 | * | |
1918 | * The formula for the normal probability density function is | |
6cc5a790 BK |
1919 | * @f[ |
1920 | * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}} | |
1921 | * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } | |
1922 | * @f] | |
8e79468d BK |
1923 | */ |
1924 | template<typename _RealType = double> | |
1925 | class normal_distribution | |
1926 | { | |
77e3c516 | 1927 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 1928 | "result_type must be a floating point type"); |
77e3c516 | 1929 | |
8e79468d BK |
1930 | public: |
1931 | /** The type of the range of the distribution. */ | |
1932 | typedef _RealType result_type; | |
12905f10 | 1933 | |
8e79468d BK |
1934 | /** Parameter type. */ |
1935 | struct param_type | |
1936 | { | |
1937 | typedef normal_distribution<_RealType> distribution_type; | |
1938 | ||
1939 | explicit | |
1940 | param_type(_RealType __mean = _RealType(0), | |
1941 | _RealType __stddev = _RealType(1)) | |
1942 | : _M_mean(__mean), _M_stddev(__stddev) | |
1943 | { | |
2f1e8e7c | 1944 | __glibcxx_assert(_M_stddev > _RealType(0)); |
8e79468d BK |
1945 | } |
1946 | ||
1947 | _RealType | |
1948 | mean() const | |
1949 | { return _M_mean; } | |
1950 | ||
1951 | _RealType | |
1952 | stddev() const | |
1953 | { return _M_stddev; } | |
1954 | ||
fc05e1ba PC |
1955 | friend bool |
1956 | operator==(const param_type& __p1, const param_type& __p2) | |
1957 | { return (__p1._M_mean == __p2._M_mean | |
1958 | && __p1._M_stddev == __p2._M_stddev); } | |
1959 | ||
12905f10 JW |
1960 | friend bool |
1961 | operator!=(const param_type& __p1, const param_type& __p2) | |
1962 | { return !(__p1 == __p2); } | |
1963 | ||
8e79468d BK |
1964 | private: |
1965 | _RealType _M_mean; | |
1966 | _RealType _M_stddev; | |
1967 | }; | |
1968 | ||
1969 | public: | |
1970 | /** | |
6cc5a790 | 1971 | * Constructs a normal distribution with parameters @f$mean@f$ and |
8e79468d BK |
1972 | * standard deviation. |
1973 | */ | |
1974 | explicit | |
1975 | normal_distribution(result_type __mean = result_type(0), | |
1976 | result_type __stddev = result_type(1)) | |
1977 | : _M_param(__mean, __stddev), _M_saved_available(false) | |
1978 | { } | |
1979 | ||
1980 | explicit | |
1981 | normal_distribution(const param_type& __p) | |
1982 | : _M_param(__p), _M_saved_available(false) | |
1983 | { } | |
1984 | ||
1985 | /** | |
1986 | * @brief Resets the distribution state. | |
1987 | */ | |
1988 | void | |
1989 | reset() | |
1990 | { _M_saved_available = false; } | |
1991 | ||
1992 | /** | |
1993 | * @brief Returns the mean of the distribution. | |
1994 | */ | |
1995 | _RealType | |
1996 | mean() const | |
1997 | { return _M_param.mean(); } | |
1998 | ||
1999 | /** | |
2000 | * @brief Returns the standard deviation of the distribution. | |
2001 | */ | |
2002 | _RealType | |
2003 | stddev() const | |
2004 | { return _M_param.stddev(); } | |
2005 | ||
2006 | /** | |
2007 | * @brief Returns the parameter set of the distribution. | |
2008 | */ | |
2009 | param_type | |
2010 | param() const | |
2011 | { return _M_param; } | |
2012 | ||
2013 | /** | |
2014 | * @brief Sets the parameter set of the distribution. | |
2015 | * @param __param The new parameter set of the distribution. | |
2016 | */ | |
2017 | void | |
2018 | param(const param_type& __param) | |
2019 | { _M_param = __param; } | |
2020 | ||
2021 | /** | |
2022 | * @brief Returns the greatest lower bound value of the distribution. | |
2023 | */ | |
2024 | result_type | |
2025 | min() const | |
a803975d | 2026 | { return std::numeric_limits<result_type>::lowest(); } |
8e79468d BK |
2027 | |
2028 | /** | |
2029 | * @brief Returns the least upper bound value of the distribution. | |
2030 | */ | |
2031 | result_type | |
2032 | max() const | |
2033 | { return std::numeric_limits<result_type>::max(); } | |
2034 | ||
247d8075 PC |
2035 | /** |
2036 | * @brief Generating functions. | |
2037 | */ | |
8e79468d BK |
2038 | template<typename _UniformRandomNumberGenerator> |
2039 | result_type | |
2040 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 2041 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
2042 | |
2043 | template<typename _UniformRandomNumberGenerator> | |
2044 | result_type | |
2045 | operator()(_UniformRandomNumberGenerator& __urng, | |
2046 | const param_type& __p); | |
2047 | ||
7b93bdde UD |
2048 | template<typename _ForwardIterator, |
2049 | typename _UniformRandomNumberGenerator> | |
2050 | void | |
2051 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
2052 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 2053 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
2054 | |
2055 | template<typename _ForwardIterator, | |
2056 | typename _UniformRandomNumberGenerator> | |
2057 | void | |
2058 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
2059 | _UniformRandomNumberGenerator& __urng, | |
2060 | const param_type& __p) | |
2061 | { this->__generate_impl(__f, __t, __urng, __p); } | |
2062 | ||
2063 | template<typename _UniformRandomNumberGenerator> | |
2064 | void | |
2065 | __generate(result_type* __f, result_type* __t, | |
2066 | _UniformRandomNumberGenerator& __urng, | |
2067 | const param_type& __p) | |
2068 | { this->__generate_impl(__f, __t, __urng, __p); } | |
2069 | ||
fc05e1ba PC |
2070 | /** |
2071 | * @brief Return true if two normal distributions have | |
2072 | * the same parameters and the sequences that would | |
2073 | * be generated are equal. | |
2074 | */ | |
2075 | template<typename _RealType1> | |
2076 | friend bool | |
2077 | operator==(const std::normal_distribution<_RealType1>& __d1, | |
2078 | const std::normal_distribution<_RealType1>& __d2); | |
2079 | ||
8e79468d BK |
2080 | /** |
2081 | * @brief Inserts a %normal_distribution random number distribution | |
2082 | * @p __x into the output stream @p __os. | |
2083 | * | |
2084 | * @param __os An output stream. | |
2085 | * @param __x A %normal_distribution random number distribution. | |
2086 | * | |
2087 | * @returns The output stream with the state of @p __x inserted or in | |
2088 | * an error state. | |
2089 | */ | |
2090 | template<typename _RealType1, typename _CharT, typename _Traits> | |
2091 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
2092 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
2093 | const std::normal_distribution<_RealType1>& __x); | |
8e79468d BK |
2094 | |
2095 | /** | |
2096 | * @brief Extracts a %normal_distribution random number distribution | |
2097 | * @p __x from the input stream @p __is. | |
2098 | * | |
2099 | * @param __is An input stream. | |
2100 | * @param __x A %normal_distribution random number generator engine. | |
2101 | * | |
2102 | * @returns The input stream with @p __x extracted or in an error | |
2103 | * state. | |
2104 | */ | |
2105 | template<typename _RealType1, typename _CharT, typename _Traits> | |
2106 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
2107 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
2108 | std::normal_distribution<_RealType1>& __x); | |
8e79468d BK |
2109 | |
2110 | private: | |
7b93bdde UD |
2111 | template<typename _ForwardIterator, |
2112 | typename _UniformRandomNumberGenerator> | |
2113 | void | |
2114 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2115 | _UniformRandomNumberGenerator& __urng, | |
2116 | const param_type& __p); | |
2117 | ||
8e79468d BK |
2118 | param_type _M_param; |
2119 | result_type _M_saved; | |
2120 | bool _M_saved_available; | |
2121 | }; | |
2122 | ||
fc05e1ba PC |
2123 | /** |
2124 | * @brief Return true if two normal distributions are different. | |
2125 | */ | |
2126 | template<typename _RealType> | |
2127 | inline bool | |
2128 | operator!=(const std::normal_distribution<_RealType>& __d1, | |
2129 | const std::normal_distribution<_RealType>& __d2) | |
2130 | { return !(__d1 == __d2); } | |
2131 | ||
8e79468d BK |
2132 | |
2133 | /** | |
2134 | * @brief A lognormal_distribution random number distribution. | |
2135 | * | |
2136 | * The formula for the normal probability mass function is | |
6cc5a790 BK |
2137 | * @f[ |
2138 | * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}} | |
2139 | * \exp{-\frac{(\ln{x} - m)^2}{2s^2}} | |
2140 | * @f] | |
8e79468d BK |
2141 | */ |
2142 | template<typename _RealType = double> | |
2143 | class lognormal_distribution | |
2144 | { | |
77e3c516 | 2145 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 2146 | "result_type must be a floating point type"); |
77e3c516 | 2147 | |
8e79468d BK |
2148 | public: |
2149 | /** The type of the range of the distribution. */ | |
2150 | typedef _RealType result_type; | |
12905f10 | 2151 | |
8e79468d BK |
2152 | /** Parameter type. */ |
2153 | struct param_type | |
2154 | { | |
2155 | typedef lognormal_distribution<_RealType> distribution_type; | |
2156 | ||
2157 | explicit | |
2158 | param_type(_RealType __m = _RealType(0), | |
2159 | _RealType __s = _RealType(1)) | |
2160 | : _M_m(__m), _M_s(__s) | |
2161 | { } | |
2162 | ||
2163 | _RealType | |
2164 | m() const | |
2165 | { return _M_m; } | |
2166 | ||
2167 | _RealType | |
2168 | s() const | |
2169 | { return _M_s; } | |
2170 | ||
fc05e1ba PC |
2171 | friend bool |
2172 | operator==(const param_type& __p1, const param_type& __p2) | |
2173 | { return __p1._M_m == __p2._M_m && __p1._M_s == __p2._M_s; } | |
2174 | ||
12905f10 JW |
2175 | friend bool |
2176 | operator!=(const param_type& __p1, const param_type& __p2) | |
2177 | { return !(__p1 == __p2); } | |
2178 | ||
8e79468d BK |
2179 | private: |
2180 | _RealType _M_m; | |
2181 | _RealType _M_s; | |
2182 | }; | |
2183 | ||
2184 | explicit | |
2185 | lognormal_distribution(_RealType __m = _RealType(0), | |
2186 | _RealType __s = _RealType(1)) | |
b01630bb | 2187 | : _M_param(__m, __s), _M_nd() |
8e79468d BK |
2188 | { } |
2189 | ||
2190 | explicit | |
2191 | lognormal_distribution(const param_type& __p) | |
b01630bb | 2192 | : _M_param(__p), _M_nd() |
8e79468d BK |
2193 | { } |
2194 | ||
2195 | /** | |
2196 | * Resets the distribution state. | |
2197 | */ | |
2198 | void | |
2199 | reset() | |
b01630bb | 2200 | { _M_nd.reset(); } |
8e79468d BK |
2201 | |
2202 | /** | |
2203 | * | |
2204 | */ | |
2205 | _RealType | |
2206 | m() const | |
2207 | { return _M_param.m(); } | |
2208 | ||
2209 | _RealType | |
2210 | s() const | |
2211 | { return _M_param.s(); } | |
2212 | ||
2213 | /** | |
2214 | * @brief Returns the parameter set of the distribution. | |
2215 | */ | |
2216 | param_type | |
2217 | param() const | |
2218 | { return _M_param; } | |
2219 | ||
2220 | /** | |
2221 | * @brief Sets the parameter set of the distribution. | |
2222 | * @param __param The new parameter set of the distribution. | |
2223 | */ | |
2224 | void | |
2225 | param(const param_type& __param) | |
2226 | { _M_param = __param; } | |
2227 | ||
2228 | /** | |
2229 | * @brief Returns the greatest lower bound value of the distribution. | |
2230 | */ | |
2231 | result_type | |
2232 | min() const | |
2233 | { return result_type(0); } | |
2234 | ||
2235 | /** | |
2236 | * @brief Returns the least upper bound value of the distribution. | |
2237 | */ | |
2238 | result_type | |
2239 | max() const | |
2240 | { return std::numeric_limits<result_type>::max(); } | |
2241 | ||
247d8075 PC |
2242 | /** |
2243 | * @brief Generating functions. | |
2244 | */ | |
8e79468d BK |
2245 | template<typename _UniformRandomNumberGenerator> |
2246 | result_type | |
2247 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 2248 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
2249 | |
2250 | template<typename _UniformRandomNumberGenerator> | |
2251 | result_type | |
2252 | operator()(_UniformRandomNumberGenerator& __urng, | |
b01630bb PC |
2253 | const param_type& __p) |
2254 | { return std::exp(__p.s() * _M_nd(__urng) + __p.m()); } | |
8e79468d | 2255 | |
7b93bdde UD |
2256 | template<typename _ForwardIterator, |
2257 | typename _UniformRandomNumberGenerator> | |
2258 | void | |
2259 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
2260 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 2261 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
2262 | |
2263 | template<typename _ForwardIterator, | |
2264 | typename _UniformRandomNumberGenerator> | |
2265 | void | |
2266 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
2267 | _UniformRandomNumberGenerator& __urng, | |
2268 | const param_type& __p) | |
2269 | { this->__generate_impl(__f, __t, __urng, __p); } | |
2270 | ||
2271 | template<typename _UniformRandomNumberGenerator> | |
2272 | void | |
2273 | __generate(result_type* __f, result_type* __t, | |
2274 | _UniformRandomNumberGenerator& __urng, | |
2275 | const param_type& __p) | |
2276 | { this->__generate_impl(__f, __t, __urng, __p); } | |
2277 | ||
fc05e1ba PC |
2278 | /** |
2279 | * @brief Return true if two lognormal distributions have | |
2280 | * the same parameters and the sequences that would | |
2281 | * be generated are equal. | |
2282 | */ | |
a30e18c1 MG |
2283 | friend bool |
2284 | operator==(const lognormal_distribution& __d1, | |
2285 | const lognormal_distribution& __d2) | |
5bcb3b4d | 2286 | { return (__d1._M_param == __d2._M_param |
a30e18c1 | 2287 | && __d1._M_nd == __d2._M_nd); } |
fc05e1ba | 2288 | |
e1923769 ESR |
2289 | /** |
2290 | * @brief Inserts a %lognormal_distribution random number distribution | |
2291 | * @p __x into the output stream @p __os. | |
2292 | * | |
2293 | * @param __os An output stream. | |
2294 | * @param __x A %lognormal_distribution random number distribution. | |
2295 | * | |
2296 | * @returns The output stream with the state of @p __x inserted or in | |
2297 | * an error state. | |
2298 | */ | |
2299 | template<typename _RealType1, typename _CharT, typename _Traits> | |
2300 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
2301 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
2302 | const std::lognormal_distribution<_RealType1>& __x); | |
e1923769 ESR |
2303 | |
2304 | /** | |
2305 | * @brief Extracts a %lognormal_distribution random number distribution | |
2306 | * @p __x from the input stream @p __is. | |
2307 | * | |
2308 | * @param __is An input stream. | |
2309 | * @param __x A %lognormal_distribution random number | |
2310 | * generator engine. | |
2311 | * | |
2312 | * @returns The input stream with @p __x extracted or in an error state. | |
2313 | */ | |
2314 | template<typename _RealType1, typename _CharT, typename _Traits> | |
2315 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
2316 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
2317 | std::lognormal_distribution<_RealType1>& __x); | |
e1923769 | 2318 | |
8e79468d | 2319 | private: |
7b93bdde UD |
2320 | template<typename _ForwardIterator, |
2321 | typename _UniformRandomNumberGenerator> | |
2322 | void | |
2323 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2324 | _UniformRandomNumberGenerator& __urng, | |
2325 | const param_type& __p); | |
2326 | ||
8e79468d | 2327 | param_type _M_param; |
b01630bb | 2328 | |
f9b09dec | 2329 | std::normal_distribution<result_type> _M_nd; |
8e79468d BK |
2330 | }; |
2331 | ||
fc05e1ba PC |
2332 | /** |
2333 | * @brief Return true if two lognormal distributions are different. | |
2334 | */ | |
2335 | template<typename _RealType> | |
2336 | inline bool | |
2337 | operator!=(const std::lognormal_distribution<_RealType>& __d1, | |
2338 | const std::lognormal_distribution<_RealType>& __d2) | |
2339 | { return !(__d1 == __d2); } | |
2340 | ||
2341 | ||
f9b09dec PC |
2342 | /** |
2343 | * @brief A gamma continuous distribution for random numbers. | |
2344 | * | |
6cc5a790 BK |
2345 | * The formula for the gamma probability density function is: |
2346 | * @f[ | |
2347 | * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)} | |
2348 | * (x/\beta)^{\alpha - 1} e^{-x/\beta} | |
2349 | * @f] | |
f9b09dec PC |
2350 | */ |
2351 | template<typename _RealType = double> | |
2352 | class gamma_distribution | |
2353 | { | |
77e3c516 | 2354 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 2355 | "result_type must be a floating point type"); |
77e3c516 | 2356 | |
f9b09dec PC |
2357 | public: |
2358 | /** The type of the range of the distribution. */ | |
2359 | typedef _RealType result_type; | |
12905f10 | 2360 | |
f9b09dec PC |
2361 | /** Parameter type. */ |
2362 | struct param_type | |
2363 | { | |
2364 | typedef gamma_distribution<_RealType> distribution_type; | |
2365 | friend class gamma_distribution<_RealType>; | |
2366 | ||
2367 | explicit | |
2368 | param_type(_RealType __alpha_val = _RealType(1), | |
2369 | _RealType __beta_val = _RealType(1)) | |
2370 | : _M_alpha(__alpha_val), _M_beta(__beta_val) | |
2371 | { | |
2f1e8e7c | 2372 | __glibcxx_assert(_M_alpha > _RealType(0)); |
f9b09dec PC |
2373 | _M_initialize(); |
2374 | } | |
2375 | ||
2376 | _RealType | |
2377 | alpha() const | |
2378 | { return _M_alpha; } | |
2379 | ||
2380 | _RealType | |
2381 | beta() const | |
2382 | { return _M_beta; } | |
2383 | ||
fc05e1ba PC |
2384 | friend bool |
2385 | operator==(const param_type& __p1, const param_type& __p2) | |
2386 | { return (__p1._M_alpha == __p2._M_alpha | |
2387 | && __p1._M_beta == __p2._M_beta); } | |
2388 | ||
12905f10 JW |
2389 | friend bool |
2390 | operator!=(const param_type& __p1, const param_type& __p2) | |
2391 | { return !(__p1 == __p2); } | |
2392 | ||
f9b09dec PC |
2393 | private: |
2394 | void | |
2395 | _M_initialize(); | |
2396 | ||
2397 | _RealType _M_alpha; | |
2398 | _RealType _M_beta; | |
2399 | ||
2400 | _RealType _M_malpha, _M_a2; | |
2401 | }; | |
2402 | ||
2403 | public: | |
2404 | /** | |
2405 | * @brief Constructs a gamma distribution with parameters | |
6cc5a790 | 2406 | * @f$\alpha@f$ and @f$\beta@f$. |
f9b09dec PC |
2407 | */ |
2408 | explicit | |
2409 | gamma_distribution(_RealType __alpha_val = _RealType(1), | |
2410 | _RealType __beta_val = _RealType(1)) | |
2411 | : _M_param(__alpha_val, __beta_val), _M_nd() | |
2412 | { } | |
2413 | ||
2414 | explicit | |
2415 | gamma_distribution(const param_type& __p) | |
2416 | : _M_param(__p), _M_nd() | |
2417 | { } | |
2418 | ||
2419 | /** | |
2420 | * @brief Resets the distribution state. | |
2421 | */ | |
2422 | void | |
2423 | reset() | |
2424 | { _M_nd.reset(); } | |
2425 | ||
2426 | /** | |
6cc5a790 | 2427 | * @brief Returns the @f$\alpha@f$ of the distribution. |
f9b09dec PC |
2428 | */ |
2429 | _RealType | |
2430 | alpha() const | |
2431 | { return _M_param.alpha(); } | |
2432 | ||
2433 | /** | |
6cc5a790 | 2434 | * @brief Returns the @f$\beta@f$ of the distribution. |
f9b09dec PC |
2435 | */ |
2436 | _RealType | |
2437 | beta() const | |
2438 | { return _M_param.beta(); } | |
2439 | ||
2440 | /** | |
2441 | * @brief Returns the parameter set of the distribution. | |
2442 | */ | |
2443 | param_type | |
2444 | param() const | |
2445 | { return _M_param; } | |
2446 | ||
2447 | /** | |
2448 | * @brief Sets the parameter set of the distribution. | |
2449 | * @param __param The new parameter set of the distribution. | |
2450 | */ | |
2451 | void | |
2452 | param(const param_type& __param) | |
2453 | { _M_param = __param; } | |
2454 | ||
2455 | /** | |
2456 | * @brief Returns the greatest lower bound value of the distribution. | |
2457 | */ | |
2458 | result_type | |
2459 | min() const | |
2460 | { return result_type(0); } | |
2461 | ||
2462 | /** | |
2463 | * @brief Returns the least upper bound value of the distribution. | |
2464 | */ | |
2465 | result_type | |
2466 | max() const | |
2467 | { return std::numeric_limits<result_type>::max(); } | |
2468 | ||
247d8075 PC |
2469 | /** |
2470 | * @brief Generating functions. | |
2471 | */ | |
f9b09dec PC |
2472 | template<typename _UniformRandomNumberGenerator> |
2473 | result_type | |
2474 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 2475 | { return this->operator()(__urng, _M_param); } |
f9b09dec PC |
2476 | |
2477 | template<typename _UniformRandomNumberGenerator> | |
2478 | result_type | |
2479 | operator()(_UniformRandomNumberGenerator& __urng, | |
2480 | const param_type& __p); | |
2481 | ||
7b93bdde UD |
2482 | template<typename _ForwardIterator, |
2483 | typename _UniformRandomNumberGenerator> | |
2484 | void | |
2485 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
2486 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 2487 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
2488 | |
2489 | template<typename _ForwardIterator, | |
2490 | typename _UniformRandomNumberGenerator> | |
2491 | void | |
2492 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
2493 | _UniformRandomNumberGenerator& __urng, | |
2494 | const param_type& __p) | |
2495 | { this->__generate_impl(__f, __t, __urng, __p); } | |
2496 | ||
2497 | template<typename _UniformRandomNumberGenerator> | |
2498 | void | |
2499 | __generate(result_type* __f, result_type* __t, | |
2500 | _UniformRandomNumberGenerator& __urng, | |
2501 | const param_type& __p) | |
2502 | { this->__generate_impl(__f, __t, __urng, __p); } | |
2503 | ||
fc05e1ba PC |
2504 | /** |
2505 | * @brief Return true if two gamma distributions have the same | |
2506 | * parameters and the sequences that would be generated | |
2507 | * are equal. | |
2508 | */ | |
a30e18c1 MG |
2509 | friend bool |
2510 | operator==(const gamma_distribution& __d1, | |
2511 | const gamma_distribution& __d2) | |
5bcb3b4d | 2512 | { return (__d1._M_param == __d2._M_param |
a30e18c1 | 2513 | && __d1._M_nd == __d2._M_nd); } |
fc05e1ba | 2514 | |
e1923769 ESR |
2515 | /** |
2516 | * @brief Inserts a %gamma_distribution random number distribution | |
2517 | * @p __x into the output stream @p __os. | |
2518 | * | |
2519 | * @param __os An output stream. | |
2520 | * @param __x A %gamma_distribution random number distribution. | |
2521 | * | |
2522 | * @returns The output stream with the state of @p __x inserted or in | |
2523 | * an error state. | |
2524 | */ | |
2525 | template<typename _RealType1, typename _CharT, typename _Traits> | |
2526 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
2527 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
2528 | const std::gamma_distribution<_RealType1>& __x); | |
e1923769 ESR |
2529 | |
2530 | /** | |
2531 | * @brief Extracts a %gamma_distribution random number distribution | |
2532 | * @p __x from the input stream @p __is. | |
2533 | * | |
2534 | * @param __is An input stream. | |
2535 | * @param __x A %gamma_distribution random number generator engine. | |
2536 | * | |
2537 | * @returns The input stream with @p __x extracted or in an error state. | |
2538 | */ | |
2539 | template<typename _RealType1, typename _CharT, typename _Traits> | |
2540 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
2541 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
2542 | std::gamma_distribution<_RealType1>& __x); | |
e1923769 | 2543 | |
f9b09dec | 2544 | private: |
7b93bdde UD |
2545 | template<typename _ForwardIterator, |
2546 | typename _UniformRandomNumberGenerator> | |
2547 | void | |
2548 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2549 | _UniformRandomNumberGenerator& __urng, | |
2550 | const param_type& __p); | |
2551 | ||
f9b09dec PC |
2552 | param_type _M_param; |
2553 | ||
2554 | std::normal_distribution<result_type> _M_nd; | |
2555 | }; | |
2556 | ||
fc05e1ba PC |
2557 | /** |
2558 | * @brief Return true if two gamma distributions are different. | |
2559 | */ | |
2560 | template<typename _RealType> | |
a30e18c1 | 2561 | inline bool |
fc05e1ba PC |
2562 | operator!=(const std::gamma_distribution<_RealType>& __d1, |
2563 | const std::gamma_distribution<_RealType>& __d2) | |
2564 | { return !(__d1 == __d2); } | |
2565 | ||
8e79468d BK |
2566 | |
2567 | /** | |
2568 | * @brief A chi_squared_distribution random number distribution. | |
2569 | * | |
2570 | * The formula for the normal probability mass function is | |
6cc5a790 | 2571 | * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$ |
8e79468d BK |
2572 | */ |
2573 | template<typename _RealType = double> | |
2574 | class chi_squared_distribution | |
2575 | { | |
77e3c516 | 2576 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 2577 | "result_type must be a floating point type"); |
77e3c516 | 2578 | |
8e79468d BK |
2579 | public: |
2580 | /** The type of the range of the distribution. */ | |
2581 | typedef _RealType result_type; | |
12905f10 | 2582 | |
8e79468d BK |
2583 | /** Parameter type. */ |
2584 | struct param_type | |
2585 | { | |
2586 | typedef chi_squared_distribution<_RealType> distribution_type; | |
2587 | ||
2588 | explicit | |
2589 | param_type(_RealType __n = _RealType(1)) | |
2590 | : _M_n(__n) | |
2591 | { } | |
2592 | ||
2593 | _RealType | |
2594 | n() const | |
2595 | { return _M_n; } | |
2596 | ||
fc05e1ba PC |
2597 | friend bool |
2598 | operator==(const param_type& __p1, const param_type& __p2) | |
2599 | { return __p1._M_n == __p2._M_n; } | |
2600 | ||
12905f10 JW |
2601 | friend bool |
2602 | operator!=(const param_type& __p1, const param_type& __p2) | |
2603 | { return !(__p1 == __p2); } | |
2604 | ||
8e79468d BK |
2605 | private: |
2606 | _RealType _M_n; | |
2607 | }; | |
2608 | ||
2609 | explicit | |
2610 | chi_squared_distribution(_RealType __n = _RealType(1)) | |
f9b09dec | 2611 | : _M_param(__n), _M_gd(__n / 2) |
8e79468d BK |
2612 | { } |
2613 | ||
2614 | explicit | |
2615 | chi_squared_distribution(const param_type& __p) | |
f9b09dec | 2616 | : _M_param(__p), _M_gd(__p.n() / 2) |
8e79468d BK |
2617 | { } |
2618 | ||
2619 | /** | |
2620 | * @brief Resets the distribution state. | |
2621 | */ | |
2622 | void | |
2623 | reset() | |
f9b09dec | 2624 | { _M_gd.reset(); } |
8e79468d BK |
2625 | |
2626 | /** | |
2627 | * | |
2628 | */ | |
2629 | _RealType | |
2630 | n() const | |
2631 | { return _M_param.n(); } | |
2632 | ||
2633 | /** | |
2634 | * @brief Returns the parameter set of the distribution. | |
2635 | */ | |
2636 | param_type | |
2637 | param() const | |
2638 | { return _M_param; } | |
2639 | ||
2640 | /** | |
2641 | * @brief Sets the parameter set of the distribution. | |
2642 | * @param __param The new parameter set of the distribution. | |
2643 | */ | |
2644 | void | |
2645 | param(const param_type& __param) | |
2646 | { _M_param = __param; } | |
2647 | ||
2648 | /** | |
2649 | * @brief Returns the greatest lower bound value of the distribution. | |
2650 | */ | |
2651 | result_type | |
2652 | min() const | |
2653 | { return result_type(0); } | |
2654 | ||
2655 | /** | |
2656 | * @brief Returns the least upper bound value of the distribution. | |
2657 | */ | |
2658 | result_type | |
2659 | max() const | |
2660 | { return std::numeric_limits<result_type>::max(); } | |
2661 | ||
247d8075 PC |
2662 | /** |
2663 | * @brief Generating functions. | |
2664 | */ | |
8e79468d BK |
2665 | template<typename _UniformRandomNumberGenerator> |
2666 | result_type | |
2667 | operator()(_UniformRandomNumberGenerator& __urng) | |
f9b09dec | 2668 | { return 2 * _M_gd(__urng); } |
8e79468d BK |
2669 | |
2670 | template<typename _UniformRandomNumberGenerator> | |
2671 | result_type | |
2672 | operator()(_UniformRandomNumberGenerator& __urng, | |
f9b09dec PC |
2673 | const param_type& __p) |
2674 | { | |
2675 | typedef typename std::gamma_distribution<result_type>::param_type | |
2676 | param_type; | |
2677 | return 2 * _M_gd(__urng, param_type(__p.n() / 2)); | |
2678 | } | |
8e79468d | 2679 | |
7b93bdde UD |
2680 | template<typename _ForwardIterator, |
2681 | typename _UniformRandomNumberGenerator> | |
2682 | void | |
2683 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
2684 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 2685 | { this->__generate_impl(__f, __t, __urng); } |
7b93bdde UD |
2686 | |
2687 | template<typename _ForwardIterator, | |
2688 | typename _UniformRandomNumberGenerator> | |
2689 | void | |
2690 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
2691 | _UniformRandomNumberGenerator& __urng, | |
2692 | const param_type& __p) | |
2693 | { typename std::gamma_distribution<result_type>::param_type | |
2694 | __p2(__p.n() / 2); | |
2695 | this->__generate_impl(__f, __t, __urng, __p2); } | |
2696 | ||
2697 | template<typename _UniformRandomNumberGenerator> | |
2698 | void | |
2699 | __generate(result_type* __f, result_type* __t, | |
2700 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 2701 | { this->__generate_impl(__f, __t, __urng); } |
7b93bdde UD |
2702 | |
2703 | template<typename _UniformRandomNumberGenerator> | |
2704 | void | |
2705 | __generate(result_type* __f, result_type* __t, | |
2706 | _UniformRandomNumberGenerator& __urng, | |
2707 | const param_type& __p) | |
2708 | { typename std::gamma_distribution<result_type>::param_type | |
2709 | __p2(__p.n() / 2); | |
2710 | this->__generate_impl(__f, __t, __urng, __p2); } | |
2711 | ||
fc05e1ba PC |
2712 | /** |
2713 | * @brief Return true if two Chi-squared distributions have | |
2714 | * the same parameters and the sequences that would be | |
2715 | * generated are equal. | |
2716 | */ | |
a30e18c1 MG |
2717 | friend bool |
2718 | operator==(const chi_squared_distribution& __d1, | |
2719 | const chi_squared_distribution& __d2) | |
5bcb3b4d | 2720 | { return __d1._M_param == __d2._M_param && __d1._M_gd == __d2._M_gd; } |
fc05e1ba | 2721 | |
e1923769 ESR |
2722 | /** |
2723 | * @brief Inserts a %chi_squared_distribution random number distribution | |
2724 | * @p __x into the output stream @p __os. | |
2725 | * | |
2726 | * @param __os An output stream. | |
2727 | * @param __x A %chi_squared_distribution random number distribution. | |
2728 | * | |
2729 | * @returns The output stream with the state of @p __x inserted or in | |
2730 | * an error state. | |
2731 | */ | |
2732 | template<typename _RealType1, typename _CharT, typename _Traits> | |
2733 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
2734 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
2735 | const std::chi_squared_distribution<_RealType1>& __x); | |
e1923769 ESR |
2736 | |
2737 | /** | |
2738 | * @brief Extracts a %chi_squared_distribution random number distribution | |
2739 | * @p __x from the input stream @p __is. | |
2740 | * | |
2741 | * @param __is An input stream. | |
2742 | * @param __x A %chi_squared_distribution random number | |
2743 | * generator engine. | |
2744 | * | |
2745 | * @returns The input stream with @p __x extracted or in an error state. | |
2746 | */ | |
2747 | template<typename _RealType1, typename _CharT, typename _Traits> | |
2748 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
2749 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
2750 | std::chi_squared_distribution<_RealType1>& __x); | |
e1923769 | 2751 | |
8e79468d | 2752 | private: |
5bcb3b4d PC |
2753 | template<typename _ForwardIterator, |
2754 | typename _UniformRandomNumberGenerator> | |
2755 | void | |
2756 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2757 | _UniformRandomNumberGenerator& __urng); | |
2758 | ||
7b93bdde UD |
2759 | template<typename _ForwardIterator, |
2760 | typename _UniformRandomNumberGenerator> | |
2761 | void | |
2762 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2763 | _UniformRandomNumberGenerator& __urng, | |
5bcb3b4d PC |
2764 | const typename |
2765 | std::gamma_distribution<result_type>::param_type& __p); | |
7b93bdde | 2766 | |
8e79468d | 2767 | param_type _M_param; |
f9b09dec PC |
2768 | |
2769 | std::gamma_distribution<result_type> _M_gd; | |
8e79468d BK |
2770 | }; |
2771 | ||
fc05e1ba PC |
2772 | /** |
2773 | * @brief Return true if two Chi-squared distributions are different. | |
2774 | */ | |
2775 | template<typename _RealType> | |
2776 | inline bool | |
2777 | operator!=(const std::chi_squared_distribution<_RealType>& __d1, | |
2778 | const std::chi_squared_distribution<_RealType>& __d2) | |
2779 | { return !(__d1 == __d2); } | |
2780 | ||
8e79468d BK |
2781 | |
2782 | /** | |
2783 | * @brief A cauchy_distribution random number distribution. | |
2784 | * | |
2785 | * The formula for the normal probability mass function is | |
6cc5a790 | 2786 | * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$ |
8e79468d BK |
2787 | */ |
2788 | template<typename _RealType = double> | |
2789 | class cauchy_distribution | |
2790 | { | |
77e3c516 | 2791 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 2792 | "result_type must be a floating point type"); |
77e3c516 | 2793 | |
8e79468d BK |
2794 | public: |
2795 | /** The type of the range of the distribution. */ | |
2796 | typedef _RealType result_type; | |
12905f10 | 2797 | |
8e79468d BK |
2798 | /** Parameter type. */ |
2799 | struct param_type | |
2800 | { | |
2801 | typedef cauchy_distribution<_RealType> distribution_type; | |
2802 | ||
2803 | explicit | |
2804 | param_type(_RealType __a = _RealType(0), | |
2805 | _RealType __b = _RealType(1)) | |
2806 | : _M_a(__a), _M_b(__b) | |
2807 | { } | |
2808 | ||
2809 | _RealType | |
2810 | a() const | |
2811 | { return _M_a; } | |
2812 | ||
2813 | _RealType | |
2814 | b() const | |
2815 | { return _M_b; } | |
2816 | ||
fc05e1ba PC |
2817 | friend bool |
2818 | operator==(const param_type& __p1, const param_type& __p2) | |
2819 | { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } | |
2820 | ||
12905f10 JW |
2821 | friend bool |
2822 | operator!=(const param_type& __p1, const param_type& __p2) | |
2823 | { return !(__p1 == __p2); } | |
2824 | ||
8e79468d BK |
2825 | private: |
2826 | _RealType _M_a; | |
2827 | _RealType _M_b; | |
2828 | }; | |
2829 | ||
2830 | explicit | |
2831 | cauchy_distribution(_RealType __a = _RealType(0), | |
2832 | _RealType __b = _RealType(1)) | |
2833 | : _M_param(__a, __b) | |
2834 | { } | |
2835 | ||
2836 | explicit | |
2837 | cauchy_distribution(const param_type& __p) | |
2838 | : _M_param(__p) | |
2839 | { } | |
2840 | ||
2841 | /** | |
2842 | * @brief Resets the distribution state. | |
2843 | */ | |
2844 | void | |
2845 | reset() | |
2846 | { } | |
2847 | ||
2848 | /** | |
2849 | * | |
2850 | */ | |
2851 | _RealType | |
2852 | a() const | |
2853 | { return _M_param.a(); } | |
2854 | ||
2855 | _RealType | |
2856 | b() const | |
2857 | { return _M_param.b(); } | |
2858 | ||
2859 | /** | |
2860 | * @brief Returns the parameter set of the distribution. | |
2861 | */ | |
2862 | param_type | |
2863 | param() const | |
2864 | { return _M_param; } | |
2865 | ||
2866 | /** | |
2867 | * @brief Sets the parameter set of the distribution. | |
2868 | * @param __param The new parameter set of the distribution. | |
2869 | */ | |
2870 | void | |
2871 | param(const param_type& __param) | |
2872 | { _M_param = __param; } | |
2873 | ||
2874 | /** | |
2875 | * @brief Returns the greatest lower bound value of the distribution. | |
2876 | */ | |
2877 | result_type | |
2878 | min() const | |
a803975d | 2879 | { return std::numeric_limits<result_type>::lowest(); } |
8e79468d BK |
2880 | |
2881 | /** | |
2882 | * @brief Returns the least upper bound value of the distribution. | |
2883 | */ | |
2884 | result_type | |
2885 | max() const | |
2886 | { return std::numeric_limits<result_type>::max(); } | |
2887 | ||
247d8075 PC |
2888 | /** |
2889 | * @brief Generating functions. | |
2890 | */ | |
8e79468d BK |
2891 | template<typename _UniformRandomNumberGenerator> |
2892 | result_type | |
2893 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 2894 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
2895 | |
2896 | template<typename _UniformRandomNumberGenerator> | |
2897 | result_type | |
2898 | operator()(_UniformRandomNumberGenerator& __urng, | |
2899 | const param_type& __p); | |
2900 | ||
7b93bdde UD |
2901 | template<typename _ForwardIterator, |
2902 | typename _UniformRandomNumberGenerator> | |
2903 | void | |
2904 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
2905 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 2906 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
2907 | |
2908 | template<typename _ForwardIterator, | |
2909 | typename _UniformRandomNumberGenerator> | |
2910 | void | |
2911 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
2912 | _UniformRandomNumberGenerator& __urng, | |
2913 | const param_type& __p) | |
2914 | { this->__generate_impl(__f, __t, __urng, __p); } | |
2915 | ||
2916 | template<typename _UniformRandomNumberGenerator> | |
2917 | void | |
2918 | __generate(result_type* __f, result_type* __t, | |
2919 | _UniformRandomNumberGenerator& __urng, | |
2920 | const param_type& __p) | |
2921 | { this->__generate_impl(__f, __t, __urng, __p); } | |
2922 | ||
5bcb3b4d PC |
2923 | /** |
2924 | * @brief Return true if two Cauchy distributions have | |
2925 | * the same parameters. | |
2926 | */ | |
2927 | friend bool | |
2928 | operator==(const cauchy_distribution& __d1, | |
2929 | const cauchy_distribution& __d2) | |
2930 | { return __d1._M_param == __d2._M_param; } | |
2931 | ||
8e79468d | 2932 | private: |
7b93bdde UD |
2933 | template<typename _ForwardIterator, |
2934 | typename _UniformRandomNumberGenerator> | |
2935 | void | |
2936 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2937 | _UniformRandomNumberGenerator& __urng, | |
2938 | const param_type& __p); | |
2939 | ||
8e79468d BK |
2940 | param_type _M_param; |
2941 | }; | |
2942 | ||
fc05e1ba PC |
2943 | /** |
2944 | * @brief Return true if two Cauchy distributions have | |
2945 | * different parameters. | |
2946 | */ | |
2947 | template<typename _RealType> | |
2948 | inline bool | |
2949 | operator!=(const std::cauchy_distribution<_RealType>& __d1, | |
2950 | const std::cauchy_distribution<_RealType>& __d2) | |
2951 | { return !(__d1 == __d2); } | |
2952 | ||
8e79468d BK |
2953 | /** |
2954 | * @brief Inserts a %cauchy_distribution random number distribution | |
2955 | * @p __x into the output stream @p __os. | |
2956 | * | |
2957 | * @param __os An output stream. | |
2958 | * @param __x A %cauchy_distribution random number distribution. | |
2959 | * | |
2960 | * @returns The output stream with the state of @p __x inserted or in | |
2961 | * an error state. | |
2962 | */ | |
2963 | template<typename _RealType, typename _CharT, typename _Traits> | |
2964 | std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
2965 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
2966 | const std::cauchy_distribution<_RealType>& __x); | |
8e79468d BK |
2967 | |
2968 | /** | |
2969 | * @brief Extracts a %cauchy_distribution random number distribution | |
2970 | * @p __x from the input stream @p __is. | |
2971 | * | |
2972 | * @param __is An input stream. | |
2973 | * @param __x A %cauchy_distribution random number | |
2974 | * generator engine. | |
2975 | * | |
2976 | * @returns The input stream with @p __x extracted or in an error state. | |
2977 | */ | |
2978 | template<typename _RealType, typename _CharT, typename _Traits> | |
2979 | std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
2980 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
2981 | std::cauchy_distribution<_RealType>& __x); | |
8e79468d BK |
2982 | |
2983 | ||
2984 | /** | |
2985 | * @brief A fisher_f_distribution random number distribution. | |
2986 | * | |
2987 | * The formula for the normal probability mass function is | |
6cc5a790 BK |
2988 | * @f[ |
2989 | * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)} | |
04b70271 | 2990 | * (\frac{m}{n})^{m/2} x^{(m/2)-1} |
6cc5a790 BK |
2991 | * (1 + \frac{mx}{n})^{-(m+n)/2} |
2992 | * @f] | |
8e79468d BK |
2993 | */ |
2994 | template<typename _RealType = double> | |
2995 | class fisher_f_distribution | |
2996 | { | |
77e3c516 | 2997 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 2998 | "result_type must be a floating point type"); |
77e3c516 | 2999 | |
8e79468d BK |
3000 | public: |
3001 | /** The type of the range of the distribution. */ | |
3002 | typedef _RealType result_type; | |
12905f10 | 3003 | |
8e79468d BK |
3004 | /** Parameter type. */ |
3005 | struct param_type | |
3006 | { | |
3007 | typedef fisher_f_distribution<_RealType> distribution_type; | |
3008 | ||
3009 | explicit | |
3010 | param_type(_RealType __m = _RealType(1), | |
3011 | _RealType __n = _RealType(1)) | |
3012 | : _M_m(__m), _M_n(__n) | |
3013 | { } | |
3014 | ||
3015 | _RealType | |
3016 | m() const | |
3017 | { return _M_m; } | |
3018 | ||
3019 | _RealType | |
3020 | n() const | |
3021 | { return _M_n; } | |
3022 | ||
fc05e1ba PC |
3023 | friend bool |
3024 | operator==(const param_type& __p1, const param_type& __p2) | |
3025 | { return __p1._M_m == __p2._M_m && __p1._M_n == __p2._M_n; } | |
3026 | ||
12905f10 JW |
3027 | friend bool |
3028 | operator!=(const param_type& __p1, const param_type& __p2) | |
3029 | { return !(__p1 == __p2); } | |
3030 | ||
8e79468d BK |
3031 | private: |
3032 | _RealType _M_m; | |
3033 | _RealType _M_n; | |
3034 | }; | |
3035 | ||
3036 | explicit | |
3037 | fisher_f_distribution(_RealType __m = _RealType(1), | |
3038 | _RealType __n = _RealType(1)) | |
f9b09dec | 3039 | : _M_param(__m, __n), _M_gd_x(__m / 2), _M_gd_y(__n / 2) |
8e79468d BK |
3040 | { } |
3041 | ||
3042 | explicit | |
3043 | fisher_f_distribution(const param_type& __p) | |
f9b09dec | 3044 | : _M_param(__p), _M_gd_x(__p.m() / 2), _M_gd_y(__p.n() / 2) |
8e79468d BK |
3045 | { } |
3046 | ||
3047 | /** | |
3048 | * @brief Resets the distribution state. | |
3049 | */ | |
3050 | void | |
3051 | reset() | |
f9b09dec PC |
3052 | { |
3053 | _M_gd_x.reset(); | |
3054 | _M_gd_y.reset(); | |
3055 | } | |
8e79468d BK |
3056 | |
3057 | /** | |
3058 | * | |
3059 | */ | |
3060 | _RealType | |
3061 | m() const | |
3062 | { return _M_param.m(); } | |
3063 | ||
3064 | _RealType | |
3065 | n() const | |
3066 | { return _M_param.n(); } | |
3067 | ||
3068 | /** | |
3069 | * @brief Returns the parameter set of the distribution. | |
3070 | */ | |
3071 | param_type | |
3072 | param() const | |
3073 | { return _M_param; } | |
3074 | ||
3075 | /** | |
3076 | * @brief Sets the parameter set of the distribution. | |
3077 | * @param __param The new parameter set of the distribution. | |
3078 | */ | |
3079 | void | |
3080 | param(const param_type& __param) | |
3081 | { _M_param = __param; } | |
3082 | ||
3083 | /** | |
3084 | * @brief Returns the greatest lower bound value of the distribution. | |
3085 | */ | |
3086 | result_type | |
3087 | min() const | |
3088 | { return result_type(0); } | |
3089 | ||
3090 | /** | |
3091 | * @brief Returns the least upper bound value of the distribution. | |
3092 | */ | |
3093 | result_type | |
3094 | max() const | |
3095 | { return std::numeric_limits<result_type>::max(); } | |
3096 | ||
247d8075 PC |
3097 | /** |
3098 | * @brief Generating functions. | |
3099 | */ | |
8e79468d BK |
3100 | template<typename _UniformRandomNumberGenerator> |
3101 | result_type | |
3102 | operator()(_UniformRandomNumberGenerator& __urng) | |
f9b09dec | 3103 | { return (_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()); } |
8e79468d BK |
3104 | |
3105 | template<typename _UniformRandomNumberGenerator> | |
3106 | result_type | |
3107 | operator()(_UniformRandomNumberGenerator& __urng, | |
f9b09dec PC |
3108 | const param_type& __p) |
3109 | { | |
3110 | typedef typename std::gamma_distribution<result_type>::param_type | |
3111 | param_type; | |
3112 | return ((_M_gd_x(__urng, param_type(__p.m() / 2)) * n()) | |
3113 | / (_M_gd_y(__urng, param_type(__p.n() / 2)) * m())); | |
3114 | } | |
8e79468d | 3115 | |
7b93bdde UD |
3116 | template<typename _ForwardIterator, |
3117 | typename _UniformRandomNumberGenerator> | |
3118 | void | |
3119 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
3120 | _UniformRandomNumberGenerator& __urng) | |
3121 | { this->__generate_impl(__f, __t, __urng); } | |
3122 | ||
3123 | template<typename _ForwardIterator, | |
3124 | typename _UniformRandomNumberGenerator> | |
3125 | void | |
3126 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
3127 | _UniformRandomNumberGenerator& __urng, | |
3128 | const param_type& __p) | |
3129 | { this->__generate_impl(__f, __t, __urng, __p); } | |
3130 | ||
3131 | template<typename _UniformRandomNumberGenerator> | |
3132 | void | |
3133 | __generate(result_type* __f, result_type* __t, | |
3134 | _UniformRandomNumberGenerator& __urng) | |
3135 | { this->__generate_impl(__f, __t, __urng); } | |
3136 | ||
3137 | template<typename _UniformRandomNumberGenerator> | |
3138 | void | |
3139 | __generate(result_type* __f, result_type* __t, | |
3140 | _UniformRandomNumberGenerator& __urng, | |
3141 | const param_type& __p) | |
3142 | { this->__generate_impl(__f, __t, __urng, __p); } | |
3143 | ||
fc05e1ba PC |
3144 | /** |
3145 | * @brief Return true if two Fisher f distributions have | |
3146 | * the same parameters and the sequences that would | |
3147 | * be generated are equal. | |
3148 | */ | |
a30e18c1 MG |
3149 | friend bool |
3150 | operator==(const fisher_f_distribution& __d1, | |
3151 | const fisher_f_distribution& __d2) | |
5bcb3b4d | 3152 | { return (__d1._M_param == __d2._M_param |
a30e18c1 MG |
3153 | && __d1._M_gd_x == __d2._M_gd_x |
3154 | && __d1._M_gd_y == __d2._M_gd_y); } | |
fc05e1ba | 3155 | |
e1923769 ESR |
3156 | /** |
3157 | * @brief Inserts a %fisher_f_distribution random number distribution | |
3158 | * @p __x into the output stream @p __os. | |
3159 | * | |
3160 | * @param __os An output stream. | |
3161 | * @param __x A %fisher_f_distribution random number distribution. | |
3162 | * | |
3163 | * @returns The output stream with the state of @p __x inserted or in | |
3164 | * an error state. | |
3165 | */ | |
3166 | template<typename _RealType1, typename _CharT, typename _Traits> | |
3167 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
3168 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
3169 | const std::fisher_f_distribution<_RealType1>& __x); | |
e1923769 ESR |
3170 | |
3171 | /** | |
3172 | * @brief Extracts a %fisher_f_distribution random number distribution | |
3173 | * @p __x from the input stream @p __is. | |
3174 | * | |
3175 | * @param __is An input stream. | |
3176 | * @param __x A %fisher_f_distribution random number | |
3177 | * generator engine. | |
3178 | * | |
3179 | * @returns The input stream with @p __x extracted or in an error state. | |
3180 | */ | |
3181 | template<typename _RealType1, typename _CharT, typename _Traits> | |
3182 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
3183 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
3184 | std::fisher_f_distribution<_RealType1>& __x); | |
e1923769 | 3185 | |
8e79468d | 3186 | private: |
7b93bdde UD |
3187 | template<typename _ForwardIterator, |
3188 | typename _UniformRandomNumberGenerator> | |
3189 | void | |
3190 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
3191 | _UniformRandomNumberGenerator& __urng); | |
3192 | ||
3193 | template<typename _ForwardIterator, | |
3194 | typename _UniformRandomNumberGenerator> | |
3195 | void | |
3196 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
3197 | _UniformRandomNumberGenerator& __urng, | |
3198 | const param_type& __p); | |
3199 | ||
8e79468d | 3200 | param_type _M_param; |
f9b09dec PC |
3201 | |
3202 | std::gamma_distribution<result_type> _M_gd_x, _M_gd_y; | |
8e79468d BK |
3203 | }; |
3204 | ||
fc05e1ba | 3205 | /** |
1d77bc54 | 3206 | * @brief Return true if two Fisher f distributions are different. |
fc05e1ba PC |
3207 | */ |
3208 | template<typename _RealType> | |
3209 | inline bool | |
3210 | operator!=(const std::fisher_f_distribution<_RealType>& __d1, | |
3211 | const std::fisher_f_distribution<_RealType>& __d2) | |
3212 | { return !(__d1 == __d2); } | |
8e79468d BK |
3213 | |
3214 | /** | |
3215 | * @brief A student_t_distribution random number distribution. | |
3216 | * | |
6cc5a790 BK |
3217 | * The formula for the normal probability mass function is: |
3218 | * @f[ | |
3219 | * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)} | |
3220 | * (1 + \frac{x^2}{n}) ^{-(n+1)/2} | |
3221 | * @f] | |
8e79468d BK |
3222 | */ |
3223 | template<typename _RealType = double> | |
3224 | class student_t_distribution | |
3225 | { | |
77e3c516 | 3226 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 3227 | "result_type must be a floating point type"); |
77e3c516 | 3228 | |
8e79468d BK |
3229 | public: |
3230 | /** The type of the range of the distribution. */ | |
3231 | typedef _RealType result_type; | |
12905f10 | 3232 | |
8e79468d BK |
3233 | /** Parameter type. */ |
3234 | struct param_type | |
3235 | { | |
3236 | typedef student_t_distribution<_RealType> distribution_type; | |
3237 | ||
3238 | explicit | |
3239 | param_type(_RealType __n = _RealType(1)) | |
3240 | : _M_n(__n) | |
3241 | { } | |
3242 | ||
3243 | _RealType | |
3244 | n() const | |
3245 | { return _M_n; } | |
3246 | ||
fc05e1ba PC |
3247 | friend bool |
3248 | operator==(const param_type& __p1, const param_type& __p2) | |
3249 | { return __p1._M_n == __p2._M_n; } | |
3250 | ||
12905f10 JW |
3251 | friend bool |
3252 | operator!=(const param_type& __p1, const param_type& __p2) | |
3253 | { return !(__p1 == __p2); } | |
3254 | ||
8e79468d BK |
3255 | private: |
3256 | _RealType _M_n; | |
3257 | }; | |
3258 | ||
3259 | explicit | |
3260 | student_t_distribution(_RealType __n = _RealType(1)) | |
f9b09dec | 3261 | : _M_param(__n), _M_nd(), _M_gd(__n / 2, 2) |
8e79468d BK |
3262 | { } |
3263 | ||
3264 | explicit | |
3265 | student_t_distribution(const param_type& __p) | |
f9b09dec | 3266 | : _M_param(__p), _M_nd(), _M_gd(__p.n() / 2, 2) |
8e79468d BK |
3267 | { } |
3268 | ||
3269 | /** | |
3270 | * @brief Resets the distribution state. | |
3271 | */ | |
3272 | void | |
3273 | reset() | |
f9b09dec PC |
3274 | { |
3275 | _M_nd.reset(); | |
3276 | _M_gd.reset(); | |
3277 | } | |
8e79468d BK |
3278 | |
3279 | /** | |
3280 | * | |
3281 | */ | |
3282 | _RealType | |
3283 | n() const | |
3284 | { return _M_param.n(); } | |
3285 | ||
3286 | /** | |
3287 | * @brief Returns the parameter set of the distribution. | |
3288 | */ | |
3289 | param_type | |
3290 | param() const | |
3291 | { return _M_param; } | |
3292 | ||
3293 | /** | |
3294 | * @brief Sets the parameter set of the distribution. | |
3295 | * @param __param The new parameter set of the distribution. | |
3296 | */ | |
3297 | void | |
3298 | param(const param_type& __param) | |
3299 | { _M_param = __param; } | |
3300 | ||
3301 | /** | |
3302 | * @brief Returns the greatest lower bound value of the distribution. | |
3303 | */ | |
3304 | result_type | |
3305 | min() const | |
a803975d | 3306 | { return std::numeric_limits<result_type>::lowest(); } |
8e79468d BK |
3307 | |
3308 | /** | |
3309 | * @brief Returns the least upper bound value of the distribution. | |
3310 | */ | |
3311 | result_type | |
3312 | max() const | |
3313 | { return std::numeric_limits<result_type>::max(); } | |
3314 | ||
247d8075 PC |
3315 | /** |
3316 | * @brief Generating functions. | |
3317 | */ | |
8e79468d BK |
3318 | template<typename _UniformRandomNumberGenerator> |
3319 | result_type | |
f9b09dec PC |
3320 | operator()(_UniformRandomNumberGenerator& __urng) |
3321 | { return _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng)); } | |
8e79468d BK |
3322 | |
3323 | template<typename _UniformRandomNumberGenerator> | |
3324 | result_type | |
3325 | operator()(_UniformRandomNumberGenerator& __urng, | |
f9b09dec PC |
3326 | const param_type& __p) |
3327 | { | |
3328 | typedef typename std::gamma_distribution<result_type>::param_type | |
3329 | param_type; | |
3330 | ||
3331 | const result_type __g = _M_gd(__urng, param_type(__p.n() / 2, 2)); | |
3332 | return _M_nd(__urng) * std::sqrt(__p.n() / __g); | |
3333 | } | |
8e79468d | 3334 | |
7b93bdde UD |
3335 | template<typename _ForwardIterator, |
3336 | typename _UniformRandomNumberGenerator> | |
3337 | void | |
3338 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
3339 | _UniformRandomNumberGenerator& __urng) | |
3340 | { this->__generate_impl(__f, __t, __urng); } | |
3341 | ||
3342 | template<typename _ForwardIterator, | |
3343 | typename _UniformRandomNumberGenerator> | |
3344 | void | |
3345 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
3346 | _UniformRandomNumberGenerator& __urng, | |
3347 | const param_type& __p) | |
3348 | { this->__generate_impl(__f, __t, __urng, __p); } | |
3349 | ||
3350 | template<typename _UniformRandomNumberGenerator> | |
3351 | void | |
3352 | __generate(result_type* __f, result_type* __t, | |
3353 | _UniformRandomNumberGenerator& __urng) | |
3354 | { this->__generate_impl(__f, __t, __urng); } | |
3355 | ||
3356 | template<typename _UniformRandomNumberGenerator> | |
3357 | void | |
3358 | __generate(result_type* __f, result_type* __t, | |
3359 | _UniformRandomNumberGenerator& __urng, | |
3360 | const param_type& __p) | |
3361 | { this->__generate_impl(__f, __t, __urng, __p); } | |
3362 | ||
fc05e1ba PC |
3363 | /** |
3364 | * @brief Return true if two Student t distributions have | |
3365 | * the same parameters and the sequences that would | |
3366 | * be generated are equal. | |
3367 | */ | |
a30e18c1 MG |
3368 | friend bool |
3369 | operator==(const student_t_distribution& __d1, | |
3370 | const student_t_distribution& __d2) | |
5bcb3b4d | 3371 | { return (__d1._M_param == __d2._M_param |
a30e18c1 | 3372 | && __d1._M_nd == __d2._M_nd && __d1._M_gd == __d2._M_gd); } |
fc05e1ba | 3373 | |
e1923769 ESR |
3374 | /** |
3375 | * @brief Inserts a %student_t_distribution random number distribution | |
3376 | * @p __x into the output stream @p __os. | |
3377 | * | |
3378 | * @param __os An output stream. | |
3379 | * @param __x A %student_t_distribution random number distribution. | |
3380 | * | |
3381 | * @returns The output stream with the state of @p __x inserted or in | |
3382 | * an error state. | |
3383 | */ | |
3384 | template<typename _RealType1, typename _CharT, typename _Traits> | |
3385 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
3386 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
3387 | const std::student_t_distribution<_RealType1>& __x); | |
e1923769 ESR |
3388 | |
3389 | /** | |
3390 | * @brief Extracts a %student_t_distribution random number distribution | |
3391 | * @p __x from the input stream @p __is. | |
3392 | * | |
3393 | * @param __is An input stream. | |
3394 | * @param __x A %student_t_distribution random number | |
3395 | * generator engine. | |
3396 | * | |
3397 | * @returns The input stream with @p __x extracted or in an error state. | |
3398 | */ | |
3399 | template<typename _RealType1, typename _CharT, typename _Traits> | |
3400 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
3401 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
3402 | std::student_t_distribution<_RealType1>& __x); | |
e1923769 | 3403 | |
8e79468d | 3404 | private: |
7b93bdde UD |
3405 | template<typename _ForwardIterator, |
3406 | typename _UniformRandomNumberGenerator> | |
3407 | void | |
3408 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
3409 | _UniformRandomNumberGenerator& __urng); | |
3410 | template<typename _ForwardIterator, | |
3411 | typename _UniformRandomNumberGenerator> | |
3412 | void | |
3413 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
3414 | _UniformRandomNumberGenerator& __urng, | |
3415 | const param_type& __p); | |
3416 | ||
8e79468d | 3417 | param_type _M_param; |
b01630bb | 3418 | |
f9b09dec PC |
3419 | std::normal_distribution<result_type> _M_nd; |
3420 | std::gamma_distribution<result_type> _M_gd; | |
8e79468d BK |
3421 | }; |
3422 | ||
fc05e1ba PC |
3423 | /** |
3424 | * @brief Return true if two Student t distributions are different. | |
3425 | */ | |
3426 | template<typename _RealType> | |
3427 | inline bool | |
3428 | operator!=(const std::student_t_distribution<_RealType>& __d1, | |
3429 | const std::student_t_distribution<_RealType>& __d2) | |
3430 | { return !(__d1 == __d2); } | |
3431 | ||
3432 | ||
037181bc | 3433 | /* @} */ // group random_distributions_normal |
8e79468d BK |
3434 | |
3435 | /** | |
94a86be0 | 3436 | * @addtogroup random_distributions_bernoulli Bernoulli Distributions |
037181bc | 3437 | * @ingroup random_distributions |
8e79468d BK |
3438 | * @{ |
3439 | */ | |
3440 | ||
3441 | /** | |
3442 | * @brief A Bernoulli random number distribution. | |
3443 | * | |
6cc5a790 BK |
3444 | * Generates a sequence of true and false values with likelihood @f$p@f$ |
3445 | * that true will come up and @f$(1 - p)@f$ that false will appear. | |
8e79468d BK |
3446 | */ |
3447 | class bernoulli_distribution | |
3448 | { | |
3449 | public: | |
3450 | /** The type of the range of the distribution. */ | |
3451 | typedef bool result_type; | |
12905f10 | 3452 | |
8e79468d BK |
3453 | /** Parameter type. */ |
3454 | struct param_type | |
3455 | { | |
3456 | typedef bernoulli_distribution distribution_type; | |
3457 | ||
3458 | explicit | |
3459 | param_type(double __p = 0.5) | |
3460 | : _M_p(__p) | |
3461 | { | |
2f1e8e7c | 3462 | __glibcxx_assert((_M_p >= 0.0) && (_M_p <= 1.0)); |
8e79468d BK |
3463 | } |
3464 | ||
3465 | double | |
3466 | p() const | |
3467 | { return _M_p; } | |
3468 | ||
fc05e1ba PC |
3469 | friend bool |
3470 | operator==(const param_type& __p1, const param_type& __p2) | |
3471 | { return __p1._M_p == __p2._M_p; } | |
3472 | ||
12905f10 JW |
3473 | friend bool |
3474 | operator!=(const param_type& __p1, const param_type& __p2) | |
3475 | { return !(__p1 == __p2); } | |
3476 | ||
8e79468d BK |
3477 | private: |
3478 | double _M_p; | |
3479 | }; | |
3480 | ||
3481 | public: | |
3482 | /** | |
3483 | * @brief Constructs a Bernoulli distribution with likelihood @p p. | |
3484 | * | |
3485 | * @param __p [IN] The likelihood of a true result being returned. | |
6cc5a790 | 3486 | * Must be in the interval @f$[0, 1]@f$. |
8e79468d BK |
3487 | */ |
3488 | explicit | |
3489 | bernoulli_distribution(double __p = 0.5) | |
3490 | : _M_param(__p) | |
3491 | { } | |
3492 | ||
3493 | explicit | |
3494 | bernoulli_distribution(const param_type& __p) | |
3495 | : _M_param(__p) | |
3496 | { } | |
3497 | ||
3498 | /** | |
3499 | * @brief Resets the distribution state. | |
3500 | * | |
3501 | * Does nothing for a Bernoulli distribution. | |
3502 | */ | |
3503 | void | |
3504 | reset() { } | |
3505 | ||
3506 | /** | |
3507 | * @brief Returns the @p p parameter of the distribution. | |
3508 | */ | |
3509 | double | |
3510 | p() const | |
3511 | { return _M_param.p(); } | |
3512 | ||
3513 | /** | |
3514 | * @brief Returns the parameter set of the distribution. | |
3515 | */ | |
3516 | param_type | |
3517 | param() const | |
3518 | { return _M_param; } | |
3519 | ||
3520 | /** | |
3521 | * @brief Sets the parameter set of the distribution. | |
3522 | * @param __param The new parameter set of the distribution. | |
3523 | */ | |
3524 | void | |
3525 | param(const param_type& __param) | |
3526 | { _M_param = __param; } | |
3527 | ||
3528 | /** | |
3529 | * @brief Returns the greatest lower bound value of the distribution. | |
3530 | */ | |
3531 | result_type | |
3532 | min() const | |
3533 | { return std::numeric_limits<result_type>::min(); } | |
3534 | ||
3535 | /** | |
3536 | * @brief Returns the least upper bound value of the distribution. | |
3537 | */ | |
3538 | result_type | |
3539 | max() const | |
3540 | { return std::numeric_limits<result_type>::max(); } | |
3541 | ||
3542 | /** | |
247d8075 | 3543 | * @brief Generating functions. |
8e79468d BK |
3544 | */ |
3545 | template<typename _UniformRandomNumberGenerator> | |
3546 | result_type | |
3547 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 3548 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
3549 | |
3550 | template<typename _UniformRandomNumberGenerator> | |
3551 | result_type | |
3552 | operator()(_UniformRandomNumberGenerator& __urng, | |
3553 | const param_type& __p) | |
3554 | { | |
3555 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> | |
3556 | __aurng(__urng); | |
3557 | if ((__aurng() - __aurng.min()) | |
3558 | < __p.p() * (__aurng.max() - __aurng.min())) | |
3559 | return true; | |
3560 | return false; | |
3561 | } | |
3562 | ||
7b93bdde UD |
3563 | template<typename _ForwardIterator, |
3564 | typename _UniformRandomNumberGenerator> | |
3565 | void | |
3566 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
3567 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 3568 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
3569 | |
3570 | template<typename _ForwardIterator, | |
3571 | typename _UniformRandomNumberGenerator> | |
3572 | void | |
3573 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
3574 | _UniformRandomNumberGenerator& __urng, const param_type& __p) | |
3575 | { this->__generate_impl(__f, __t, __urng, __p); } | |
3576 | ||
3577 | template<typename _UniformRandomNumberGenerator> | |
3578 | void | |
3579 | __generate(result_type* __f, result_type* __t, | |
3580 | _UniformRandomNumberGenerator& __urng, | |
3581 | const param_type& __p) | |
3582 | { this->__generate_impl(__f, __t, __urng, __p); } | |
3583 | ||
5bcb3b4d PC |
3584 | /** |
3585 | * @brief Return true if two Bernoulli distributions have | |
3586 | * the same parameters. | |
3587 | */ | |
3588 | friend bool | |
3589 | operator==(const bernoulli_distribution& __d1, | |
3590 | const bernoulli_distribution& __d2) | |
3591 | { return __d1._M_param == __d2._M_param; } | |
3592 | ||
8e79468d | 3593 | private: |
7b93bdde UD |
3594 | template<typename _ForwardIterator, |
3595 | typename _UniformRandomNumberGenerator> | |
3596 | void | |
3597 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
3598 | _UniformRandomNumberGenerator& __urng, | |
3599 | const param_type& __p); | |
3600 | ||
8e79468d BK |
3601 | param_type _M_param; |
3602 | }; | |
3603 | ||
fc05e1ba PC |
3604 | /** |
3605 | * @brief Return true if two Bernoulli distributions have | |
3606 | * different parameters. | |
3607 | */ | |
3608 | inline bool | |
3609 | operator!=(const std::bernoulli_distribution& __d1, | |
3610 | const std::bernoulli_distribution& __d2) | |
3611 | { return !(__d1 == __d2); } | |
3612 | ||
8e79468d BK |
3613 | /** |
3614 | * @brief Inserts a %bernoulli_distribution random number distribution | |
3615 | * @p __x into the output stream @p __os. | |
3616 | * | |
3617 | * @param __os An output stream. | |
3618 | * @param __x A %bernoulli_distribution random number distribution. | |
3619 | * | |
3620 | * @returns The output stream with the state of @p __x inserted or in | |
3621 | * an error state. | |
3622 | */ | |
3623 | template<typename _CharT, typename _Traits> | |
3624 | std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
3625 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
3626 | const std::bernoulli_distribution& __x); | |
8e79468d BK |
3627 | |
3628 | /** | |
3629 | * @brief Extracts a %bernoulli_distribution random number distribution | |
3630 | * @p __x from the input stream @p __is. | |
3631 | * | |
3632 | * @param __is An input stream. | |
3633 | * @param __x A %bernoulli_distribution random number generator engine. | |
3634 | * | |
3635 | * @returns The input stream with @p __x extracted or in an error state. | |
3636 | */ | |
3637 | template<typename _CharT, typename _Traits> | |
3638 | std::basic_istream<_CharT, _Traits>& | |
3639 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
94986f6d | 3640 | std::bernoulli_distribution& __x) |
8e79468d BK |
3641 | { |
3642 | double __p; | |
3643 | __is >> __p; | |
3644 | __x.param(bernoulli_distribution::param_type(__p)); | |
3645 | return __is; | |
3646 | } | |
3647 | ||
3648 | ||
3649 | /** | |
3650 | * @brief A discrete binomial random number distribution. | |
3651 | * | |
3652 | * The formula for the binomial probability density function is | |
9ea146e6 | 3653 | * @f$p(i|t,p) = \binom{t}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$ |
6cc5a790 | 3654 | * and @f$p@f$ are the parameters of the distribution. |
8e79468d BK |
3655 | */ |
3656 | template<typename _IntType = int> | |
3657 | class binomial_distribution | |
3658 | { | |
77e3c516 | 3659 | static_assert(std::is_integral<_IntType>::value, |
0cded43d | 3660 | "result_type must be an integral type"); |
8e79468d BK |
3661 | |
3662 | public: | |
3663 | /** The type of the range of the distribution. */ | |
3664 | typedef _IntType result_type; | |
12905f10 | 3665 | |
8e79468d BK |
3666 | /** Parameter type. */ |
3667 | struct param_type | |
3668 | { | |
3669 | typedef binomial_distribution<_IntType> distribution_type; | |
3670 | friend class binomial_distribution<_IntType>; | |
3671 | ||
3672 | explicit | |
3673 | param_type(_IntType __t = _IntType(1), double __p = 0.5) | |
3674 | : _M_t(__t), _M_p(__p) | |
3675 | { | |
2f1e8e7c | 3676 | __glibcxx_assert((_M_t >= _IntType(0)) |
94986f6d PC |
3677 | && (_M_p >= 0.0) |
3678 | && (_M_p <= 1.0)); | |
8e79468d BK |
3679 | _M_initialize(); |
3680 | } | |
3681 | ||
3682 | _IntType | |
3683 | t() const | |
3684 | { return _M_t; } | |
3685 | ||
3686 | double | |
3687 | p() const | |
3688 | { return _M_p; } | |
3689 | ||
fc05e1ba PC |
3690 | friend bool |
3691 | operator==(const param_type& __p1, const param_type& __p2) | |
3692 | { return __p1._M_t == __p2._M_t && __p1._M_p == __p2._M_p; } | |
3693 | ||
12905f10 JW |
3694 | friend bool |
3695 | operator!=(const param_type& __p1, const param_type& __p2) | |
3696 | { return !(__p1 == __p2); } | |
3697 | ||
8e79468d BK |
3698 | private: |
3699 | void | |
3700 | _M_initialize(); | |
3701 | ||
3702 | _IntType _M_t; | |
3703 | double _M_p; | |
3704 | ||
3705 | double _M_q; | |
3706 | #if _GLIBCXX_USE_C99_MATH_TR1 | |
3707 | double _M_d1, _M_d2, _M_s1, _M_s2, _M_c, | |
3708 | _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p; | |
3709 | #endif | |
3710 | bool _M_easy; | |
3711 | }; | |
3712 | ||
3713 | // constructors and member function | |
3714 | explicit | |
3715 | binomial_distribution(_IntType __t = _IntType(1), | |
3716 | double __p = 0.5) | |
3717 | : _M_param(__t, __p), _M_nd() | |
3718 | { } | |
3719 | ||
3720 | explicit | |
3721 | binomial_distribution(const param_type& __p) | |
3722 | : _M_param(__p), _M_nd() | |
3723 | { } | |
3724 | ||
3725 | /** | |
3726 | * @brief Resets the distribution state. | |
3727 | */ | |
3728 | void | |
3729 | reset() | |
3730 | { _M_nd.reset(); } | |
3731 | ||
3732 | /** | |
3733 | * @brief Returns the distribution @p t parameter. | |
3734 | */ | |
3735 | _IntType | |
3736 | t() const | |
3737 | { return _M_param.t(); } | |
3738 | ||
3739 | /** | |
3740 | * @brief Returns the distribution @p p parameter. | |
3741 | */ | |
3742 | double | |
3743 | p() const | |
3744 | { return _M_param.p(); } | |
3745 | ||
3746 | /** | |
3747 | * @brief Returns the parameter set of the distribution. | |
3748 | */ | |
3749 | param_type | |
3750 | param() const | |
3751 | { return _M_param; } | |
3752 | ||
3753 | /** | |
3754 | * @brief Sets the parameter set of the distribution. | |
3755 | * @param __param The new parameter set of the distribution. | |
3756 | */ | |
3757 | void | |
3758 | param(const param_type& __param) | |
3759 | { _M_param = __param; } | |
3760 | ||
3761 | /** | |
3762 | * @brief Returns the greatest lower bound value of the distribution. | |
3763 | */ | |
3764 | result_type | |
3765 | min() const | |
3766 | { return 0; } | |
3767 | ||
3768 | /** | |
3769 | * @brief Returns the least upper bound value of the distribution. | |
3770 | */ | |
3771 | result_type | |
3772 | max() const | |
3773 | { return _M_param.t(); } | |
3774 | ||
247d8075 PC |
3775 | /** |
3776 | * @brief Generating functions. | |
3777 | */ | |
3778 | template<typename _UniformRandomNumberGenerator> | |
3779 | result_type | |
3780 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 3781 | { return this->operator()(__urng, _M_param); } |
247d8075 PC |
3782 | |
3783 | template<typename _UniformRandomNumberGenerator> | |
3784 | result_type | |
3785 | operator()(_UniformRandomNumberGenerator& __urng, | |
3786 | const param_type& __p); | |
3787 | ||
7b93bdde UD |
3788 | template<typename _ForwardIterator, |
3789 | typename _UniformRandomNumberGenerator> | |
3790 | void | |
3791 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
3792 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 3793 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
3794 | |
3795 | template<typename _ForwardIterator, | |
3796 | typename _UniformRandomNumberGenerator> | |
3797 | void | |
3798 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
3799 | _UniformRandomNumberGenerator& __urng, | |
3800 | const param_type& __p) | |
3801 | { this->__generate_impl(__f, __t, __urng, __p); } | |
3802 | ||
3803 | template<typename _UniformRandomNumberGenerator> | |
3804 | void | |
3805 | __generate(result_type* __f, result_type* __t, | |
3806 | _UniformRandomNumberGenerator& __urng, | |
3807 | const param_type& __p) | |
3808 | { this->__generate_impl(__f, __t, __urng, __p); } | |
3809 | ||
fc05e1ba PC |
3810 | /** |
3811 | * @brief Return true if two binomial distributions have | |
3812 | * the same parameters and the sequences that would | |
3813 | * be generated are equal. | |
3814 | */ | |
fc05e1ba | 3815 | friend bool |
a30e18c1 MG |
3816 | operator==(const binomial_distribution& __d1, |
3817 | const binomial_distribution& __d2) | |
fc05e1ba | 3818 | #ifdef _GLIBCXX_USE_C99_MATH_TR1 |
5bcb3b4d | 3819 | { return __d1._M_param == __d2._M_param && __d1._M_nd == __d2._M_nd; } |
fc05e1ba | 3820 | #else |
5bcb3b4d | 3821 | { return __d1._M_param == __d2._M_param; } |
fc05e1ba PC |
3822 | #endif |
3823 | ||
8e79468d BK |
3824 | /** |
3825 | * @brief Inserts a %binomial_distribution random number distribution | |
3826 | * @p __x into the output stream @p __os. | |
3827 | * | |
3828 | * @param __os An output stream. | |
3829 | * @param __x A %binomial_distribution random number distribution. | |
3830 | * | |
3831 | * @returns The output stream with the state of @p __x inserted or in | |
3832 | * an error state. | |
3833 | */ | |
3834 | template<typename _IntType1, | |
3835 | typename _CharT, typename _Traits> | |
3836 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
3837 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
3838 | const std::binomial_distribution<_IntType1>& __x); | |
8e79468d BK |
3839 | |
3840 | /** | |
3841 | * @brief Extracts a %binomial_distribution random number distribution | |
3842 | * @p __x from the input stream @p __is. | |
3843 | * | |
3844 | * @param __is An input stream. | |
3845 | * @param __x A %binomial_distribution random number generator engine. | |
3846 | * | |
3847 | * @returns The input stream with @p __x extracted or in an error | |
3848 | * state. | |
3849 | */ | |
3850 | template<typename _IntType1, | |
3851 | typename _CharT, typename _Traits> | |
3852 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
3853 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
3854 | std::binomial_distribution<_IntType1>& __x); | |
8e79468d BK |
3855 | |
3856 | private: | |
7b93bdde UD |
3857 | template<typename _ForwardIterator, |
3858 | typename _UniformRandomNumberGenerator> | |
3859 | void | |
3860 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
3861 | _UniformRandomNumberGenerator& __urng, | |
3862 | const param_type& __p); | |
3863 | ||
8e79468d BK |
3864 | template<typename _UniformRandomNumberGenerator> |
3865 | result_type | |
07bba3b1 PC |
3866 | _M_waiting(_UniformRandomNumberGenerator& __urng, |
3867 | _IntType __t, double __q); | |
8e79468d BK |
3868 | |
3869 | param_type _M_param; | |
3870 | ||
3871 | // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined. | |
f9b09dec | 3872 | std::normal_distribution<double> _M_nd; |
8e79468d BK |
3873 | }; |
3874 | ||
fc05e1ba PC |
3875 | /** |
3876 | * @brief Return true if two binomial distributions are different. | |
3877 | */ | |
3878 | template<typename _IntType> | |
3879 | inline bool | |
3880 | operator!=(const std::binomial_distribution<_IntType>& __d1, | |
3881 | const std::binomial_distribution<_IntType>& __d2) | |
3882 | { return !(__d1 == __d2); } | |
3883 | ||
8e79468d BK |
3884 | |
3885 | /** | |
3886 | * @brief A discrete geometric random number distribution. | |
3887 | * | |
3888 | * The formula for the geometric probability density function is | |
d8d4db33 | 3889 | * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the |
8e79468d BK |
3890 | * distribution. |
3891 | */ | |
3892 | template<typename _IntType = int> | |
3893 | class geometric_distribution | |
3894 | { | |
77e3c516 | 3895 | static_assert(std::is_integral<_IntType>::value, |
0cded43d | 3896 | "result_type must be an integral type"); |
8e79468d BK |
3897 | |
3898 | public: | |
3899 | /** The type of the range of the distribution. */ | |
3900 | typedef _IntType result_type; | |
12905f10 | 3901 | |
8e79468d BK |
3902 | /** Parameter type. */ |
3903 | struct param_type | |
3904 | { | |
3905 | typedef geometric_distribution<_IntType> distribution_type; | |
3906 | friend class geometric_distribution<_IntType>; | |
3907 | ||
3908 | explicit | |
3909 | param_type(double __p = 0.5) | |
3910 | : _M_p(__p) | |
3911 | { | |
2f1e8e7c | 3912 | __glibcxx_assert((_M_p > 0.0) && (_M_p < 1.0)); |
8e79468d BK |
3913 | _M_initialize(); |
3914 | } | |
3915 | ||
3916 | double | |
3917 | p() const | |
3918 | { return _M_p; } | |
3919 | ||
fc05e1ba PC |
3920 | friend bool |
3921 | operator==(const param_type& __p1, const param_type& __p2) | |
3922 | { return __p1._M_p == __p2._M_p; } | |
3923 | ||
12905f10 JW |
3924 | friend bool |
3925 | operator!=(const param_type& __p1, const param_type& __p2) | |
3926 | { return !(__p1 == __p2); } | |
3927 | ||
8e79468d BK |
3928 | private: |
3929 | void | |
3930 | _M_initialize() | |
d8d4db33 | 3931 | { _M_log_1_p = std::log(1.0 - _M_p); } |
8e79468d BK |
3932 | |
3933 | double _M_p; | |
3934 | ||
d8d4db33 | 3935 | double _M_log_1_p; |
8e79468d BK |
3936 | }; |
3937 | ||
3938 | // constructors and member function | |
3939 | explicit | |
3940 | geometric_distribution(double __p = 0.5) | |
3941 | : _M_param(__p) | |
3942 | { } | |
3943 | ||
3944 | explicit | |
3945 | geometric_distribution(const param_type& __p) | |
3946 | : _M_param(__p) | |
3947 | { } | |
3948 | ||
3949 | /** | |
3950 | * @brief Resets the distribution state. | |
3951 | * | |
3952 | * Does nothing for the geometric distribution. | |
3953 | */ | |
3954 | void | |
3955 | reset() { } | |
3956 | ||
3957 | /** | |
3958 | * @brief Returns the distribution parameter @p p. | |
3959 | */ | |
3960 | double | |
3961 | p() const | |
3962 | { return _M_param.p(); } | |
3963 | ||
3964 | /** | |
3965 | * @brief Returns the parameter set of the distribution. | |
3966 | */ | |
3967 | param_type | |
3968 | param() const | |
3969 | { return _M_param; } | |
3970 | ||
3971 | /** | |
3972 | * @brief Sets the parameter set of the distribution. | |
3973 | * @param __param The new parameter set of the distribution. | |
3974 | */ | |
3975 | void | |
3976 | param(const param_type& __param) | |
3977 | { _M_param = __param; } | |
3978 | ||
3979 | /** | |
3980 | * @brief Returns the greatest lower bound value of the distribution. | |
3981 | */ | |
3982 | result_type | |
3983 | min() const | |
3984 | { return 0; } | |
3985 | ||
3986 | /** | |
3987 | * @brief Returns the least upper bound value of the distribution. | |
3988 | */ | |
3989 | result_type | |
3990 | max() const | |
3991 | { return std::numeric_limits<result_type>::max(); } | |
3992 | ||
247d8075 PC |
3993 | /** |
3994 | * @brief Generating functions. | |
3995 | */ | |
8e79468d BK |
3996 | template<typename _UniformRandomNumberGenerator> |
3997 | result_type | |
3998 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 3999 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
4000 | |
4001 | template<typename _UniformRandomNumberGenerator> | |
4002 | result_type | |
4003 | operator()(_UniformRandomNumberGenerator& __urng, | |
4004 | const param_type& __p); | |
4005 | ||
7b93bdde UD |
4006 | template<typename _ForwardIterator, |
4007 | typename _UniformRandomNumberGenerator> | |
4008 | void | |
4009 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
4010 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 4011 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
4012 | |
4013 | template<typename _ForwardIterator, | |
4014 | typename _UniformRandomNumberGenerator> | |
4015 | void | |
4016 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
4017 | _UniformRandomNumberGenerator& __urng, | |
4018 | const param_type& __p) | |
4019 | { this->__generate_impl(__f, __t, __urng, __p); } | |
4020 | ||
4021 | template<typename _UniformRandomNumberGenerator> | |
4022 | void | |
4023 | __generate(result_type* __f, result_type* __t, | |
4024 | _UniformRandomNumberGenerator& __urng, | |
4025 | const param_type& __p) | |
4026 | { this->__generate_impl(__f, __t, __urng, __p); } | |
4027 | ||
5bcb3b4d PC |
4028 | /** |
4029 | * @brief Return true if two geometric distributions have | |
4030 | * the same parameters. | |
4031 | */ | |
4032 | friend bool | |
4033 | operator==(const geometric_distribution& __d1, | |
4034 | const geometric_distribution& __d2) | |
4035 | { return __d1._M_param == __d2._M_param; } | |
4036 | ||
8e79468d | 4037 | private: |
7b93bdde UD |
4038 | template<typename _ForwardIterator, |
4039 | typename _UniformRandomNumberGenerator> | |
4040 | void | |
4041 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
4042 | _UniformRandomNumberGenerator& __urng, | |
4043 | const param_type& __p); | |
4044 | ||
8e79468d BK |
4045 | param_type _M_param; |
4046 | }; | |
4047 | ||
fc05e1ba PC |
4048 | /** |
4049 | * @brief Return true if two geometric distributions have | |
4050 | * different parameters. | |
4051 | */ | |
4052 | template<typename _IntType> | |
4053 | inline bool | |
4054 | operator!=(const std::geometric_distribution<_IntType>& __d1, | |
4055 | const std::geometric_distribution<_IntType>& __d2) | |
4056 | { return !(__d1 == __d2); } | |
4057 | ||
8e79468d BK |
4058 | /** |
4059 | * @brief Inserts a %geometric_distribution random number distribution | |
4060 | * @p __x into the output stream @p __os. | |
4061 | * | |
4062 | * @param __os An output stream. | |
4063 | * @param __x A %geometric_distribution random number distribution. | |
4064 | * | |
4065 | * @returns The output stream with the state of @p __x inserted or in | |
4066 | * an error state. | |
4067 | */ | |
4068 | template<typename _IntType, | |
4069 | typename _CharT, typename _Traits> | |
4070 | std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
4071 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
4072 | const std::geometric_distribution<_IntType>& __x); | |
8e79468d BK |
4073 | |
4074 | /** | |
4075 | * @brief Extracts a %geometric_distribution random number distribution | |
4076 | * @p __x from the input stream @p __is. | |
4077 | * | |
4078 | * @param __is An input stream. | |
4079 | * @param __x A %geometric_distribution random number generator engine. | |
4080 | * | |
4081 | * @returns The input stream with @p __x extracted or in an error state. | |
4082 | */ | |
4083 | template<typename _IntType, | |
4084 | typename _CharT, typename _Traits> | |
4085 | std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
4086 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
4087 | std::geometric_distribution<_IntType>& __x); | |
8e79468d BK |
4088 | |
4089 | ||
4090 | /** | |
4091 | * @brief A negative_binomial_distribution random number distribution. | |
4092 | * | |
4093 | * The formula for the negative binomial probability mass function is | |
6cc5a790 BK |
4094 | * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$ |
4095 | * and @f$p@f$ are the parameters of the distribution. | |
8e79468d BK |
4096 | */ |
4097 | template<typename _IntType = int> | |
4098 | class negative_binomial_distribution | |
4099 | { | |
77e3c516 | 4100 | static_assert(std::is_integral<_IntType>::value, |
0cded43d | 4101 | "result_type must be an integral type"); |
8e79468d BK |
4102 | |
4103 | public: | |
4104 | /** The type of the range of the distribution. */ | |
4105 | typedef _IntType result_type; | |
12905f10 | 4106 | |
8e79468d BK |
4107 | /** Parameter type. */ |
4108 | struct param_type | |
4109 | { | |
4110 | typedef negative_binomial_distribution<_IntType> distribution_type; | |
4111 | ||
4112 | explicit | |
4113 | param_type(_IntType __k = 1, double __p = 0.5) | |
4114 | : _M_k(__k), _M_p(__p) | |
113b21bd | 4115 | { |
2f1e8e7c | 4116 | __glibcxx_assert((_M_k > 0) && (_M_p > 0.0) && (_M_p <= 1.0)); |
113b21bd | 4117 | } |
8e79468d BK |
4118 | |
4119 | _IntType | |
4120 | k() const | |
4121 | { return _M_k; } | |
4122 | ||
4123 | double | |
4124 | p() const | |
4125 | { return _M_p; } | |
4126 | ||
fc05e1ba PC |
4127 | friend bool |
4128 | operator==(const param_type& __p1, const param_type& __p2) | |
4129 | { return __p1._M_k == __p2._M_k && __p1._M_p == __p2._M_p; } | |
4130 | ||
12905f10 JW |
4131 | friend bool |
4132 | operator!=(const param_type& __p1, const param_type& __p2) | |
4133 | { return !(__p1 == __p2); } | |
4134 | ||
8e79468d BK |
4135 | private: |
4136 | _IntType _M_k; | |
4137 | double _M_p; | |
4138 | }; | |
4139 | ||
4140 | explicit | |
4141 | negative_binomial_distribution(_IntType __k = 1, double __p = 0.5) | |
ff2e697a | 4142 | : _M_param(__k, __p), _M_gd(__k, (1.0 - __p) / __p) |
8e79468d BK |
4143 | { } |
4144 | ||
4145 | explicit | |
4146 | negative_binomial_distribution(const param_type& __p) | |
ff2e697a | 4147 | : _M_param(__p), _M_gd(__p.k(), (1.0 - __p.p()) / __p.p()) |
8e79468d BK |
4148 | { } |
4149 | ||
4150 | /** | |
4151 | * @brief Resets the distribution state. | |
4152 | */ | |
4153 | void | |
4154 | reset() | |
f9b09dec | 4155 | { _M_gd.reset(); } |
8e79468d BK |
4156 | |
4157 | /** | |
6cc5a790 | 4158 | * @brief Return the @f$k@f$ parameter of the distribution. |
8e79468d BK |
4159 | */ |
4160 | _IntType | |
4161 | k() const | |
4162 | { return _M_param.k(); } | |
4163 | ||
4164 | /** | |
6cc5a790 | 4165 | * @brief Return the @f$p@f$ parameter of the distribution. |
8e79468d BK |
4166 | */ |
4167 | double | |
4168 | p() const | |
4169 | { return _M_param.p(); } | |
4170 | ||
4171 | /** | |
4172 | * @brief Returns the parameter set of the distribution. | |
4173 | */ | |
4174 | param_type | |
4175 | param() const | |
4176 | { return _M_param; } | |
4177 | ||
4178 | /** | |
4179 | * @brief Sets the parameter set of the distribution. | |
4180 | * @param __param The new parameter set of the distribution. | |
4181 | */ | |
4182 | void | |
4183 | param(const param_type& __param) | |
4184 | { _M_param = __param; } | |
4185 | ||
4186 | /** | |
4187 | * @brief Returns the greatest lower bound value of the distribution. | |
4188 | */ | |
4189 | result_type | |
4190 | min() const | |
4191 | { return result_type(0); } | |
4192 | ||
4193 | /** | |
4194 | * @brief Returns the least upper bound value of the distribution. | |
4195 | */ | |
4196 | result_type | |
4197 | max() const | |
4198 | { return std::numeric_limits<result_type>::max(); } | |
4199 | ||
247d8075 PC |
4200 | /** |
4201 | * @brief Generating functions. | |
4202 | */ | |
8e79468d BK |
4203 | template<typename _UniformRandomNumberGenerator> |
4204 | result_type | |
f9b09dec | 4205 | operator()(_UniformRandomNumberGenerator& __urng); |
8e79468d BK |
4206 | |
4207 | template<typename _UniformRandomNumberGenerator> | |
4208 | result_type | |
4209 | operator()(_UniformRandomNumberGenerator& __urng, | |
4210 | const param_type& __p); | |
4211 | ||
7b93bdde UD |
4212 | template<typename _ForwardIterator, |
4213 | typename _UniformRandomNumberGenerator> | |
4214 | void | |
4215 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
4216 | _UniformRandomNumberGenerator& __urng) | |
4217 | { this->__generate_impl(__f, __t, __urng); } | |
4218 | ||
4219 | template<typename _ForwardIterator, | |
4220 | typename _UniformRandomNumberGenerator> | |
4221 | void | |
4222 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
4223 | _UniformRandomNumberGenerator& __urng, | |
4224 | const param_type& __p) | |
4225 | { this->__generate_impl(__f, __t, __urng, __p); } | |
4226 | ||
4227 | template<typename _UniformRandomNumberGenerator> | |
4228 | void | |
4229 | __generate(result_type* __f, result_type* __t, | |
4230 | _UniformRandomNumberGenerator& __urng) | |
4231 | { this->__generate_impl(__f, __t, __urng); } | |
4232 | ||
4233 | template<typename _UniformRandomNumberGenerator> | |
4234 | void | |
4235 | __generate(result_type* __f, result_type* __t, | |
4236 | _UniformRandomNumberGenerator& __urng, | |
4237 | const param_type& __p) | |
4238 | { this->__generate_impl(__f, __t, __urng, __p); } | |
4239 | ||
fc05e1ba PC |
4240 | /** |
4241 | * @brief Return true if two negative binomial distributions have | |
4242 | * the same parameters and the sequences that would be | |
4243 | * generated are equal. | |
4244 | */ | |
a30e18c1 MG |
4245 | friend bool |
4246 | operator==(const negative_binomial_distribution& __d1, | |
4247 | const negative_binomial_distribution& __d2) | |
5bcb3b4d | 4248 | { return __d1._M_param == __d2._M_param && __d1._M_gd == __d2._M_gd; } |
fc05e1ba | 4249 | |
e1923769 ESR |
4250 | /** |
4251 | * @brief Inserts a %negative_binomial_distribution random | |
4252 | * number distribution @p __x into the output stream @p __os. | |
4253 | * | |
4254 | * @param __os An output stream. | |
4255 | * @param __x A %negative_binomial_distribution random number | |
4256 | * distribution. | |
4257 | * | |
4258 | * @returns The output stream with the state of @p __x inserted or in | |
4259 | * an error state. | |
4260 | */ | |
4261 | template<typename _IntType1, typename _CharT, typename _Traits> | |
4262 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
4263 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
4264 | const std::negative_binomial_distribution<_IntType1>& __x); | |
e1923769 ESR |
4265 | |
4266 | /** | |
4267 | * @brief Extracts a %negative_binomial_distribution random number | |
4268 | * distribution @p __x from the input stream @p __is. | |
4269 | * | |
4270 | * @param __is An input stream. | |
4271 | * @param __x A %negative_binomial_distribution random number | |
4272 | * generator engine. | |
4273 | * | |
4274 | * @returns The input stream with @p __x extracted or in an error state. | |
4275 | */ | |
4276 | template<typename _IntType1, typename _CharT, typename _Traits> | |
4277 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
4278 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
4279 | std::negative_binomial_distribution<_IntType1>& __x); | |
e1923769 | 4280 | |
8e79468d | 4281 | private: |
7b93bdde UD |
4282 | template<typename _ForwardIterator, |
4283 | typename _UniformRandomNumberGenerator> | |
4284 | void | |
4285 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
4286 | _UniformRandomNumberGenerator& __urng); | |
4287 | template<typename _ForwardIterator, | |
4288 | typename _UniformRandomNumberGenerator> | |
4289 | void | |
4290 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
4291 | _UniformRandomNumberGenerator& __urng, | |
4292 | const param_type& __p); | |
4293 | ||
8e79468d | 4294 | param_type _M_param; |
f9b09dec PC |
4295 | |
4296 | std::gamma_distribution<double> _M_gd; | |
8e79468d BK |
4297 | }; |
4298 | ||
fc05e1ba PC |
4299 | /** |
4300 | * @brief Return true if two negative binomial distributions are different. | |
4301 | */ | |
4302 | template<typename _IntType> | |
4303 | inline bool | |
4304 | operator!=(const std::negative_binomial_distribution<_IntType>& __d1, | |
4305 | const std::negative_binomial_distribution<_IntType>& __d2) | |
4306 | { return !(__d1 == __d2); } | |
4307 | ||
4308 | ||
037181bc | 4309 | /* @} */ // group random_distributions_bernoulli |
8e79468d BK |
4310 | |
4311 | /** | |
94a86be0 | 4312 | * @addtogroup random_distributions_poisson Poisson Distributions |
037181bc | 4313 | * @ingroup random_distributions |
8e79468d BK |
4314 | * @{ |
4315 | */ | |
4316 | ||
4317 | /** | |
4318 | * @brief A discrete Poisson random number distribution. | |
4319 | * | |
4320 | * The formula for the Poisson probability density function is | |
6cc5a790 | 4321 | * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the |
8e79468d BK |
4322 | * parameter of the distribution. |
4323 | */ | |
4324 | template<typename _IntType = int> | |
4325 | class poisson_distribution | |
4326 | { | |
77e3c516 | 4327 | static_assert(std::is_integral<_IntType>::value, |
0cded43d | 4328 | "result_type must be an integral type"); |
8e79468d BK |
4329 | |
4330 | public: | |
4331 | /** The type of the range of the distribution. */ | |
4332 | typedef _IntType result_type; | |
12905f10 | 4333 | |
8e79468d BK |
4334 | /** Parameter type. */ |
4335 | struct param_type | |
4336 | { | |
4337 | typedef poisson_distribution<_IntType> distribution_type; | |
4338 | friend class poisson_distribution<_IntType>; | |
4339 | ||
4340 | explicit | |
4341 | param_type(double __mean = 1.0) | |
4342 | : _M_mean(__mean) | |
4343 | { | |
2f1e8e7c | 4344 | __glibcxx_assert(_M_mean > 0.0); |
8e79468d BK |
4345 | _M_initialize(); |
4346 | } | |
4347 | ||
4348 | double | |
4349 | mean() const | |
4350 | { return _M_mean; } | |
4351 | ||
fc05e1ba PC |
4352 | friend bool |
4353 | operator==(const param_type& __p1, const param_type& __p2) | |
4354 | { return __p1._M_mean == __p2._M_mean; } | |
4355 | ||
12905f10 JW |
4356 | friend bool |
4357 | operator!=(const param_type& __p1, const param_type& __p2) | |
4358 | { return !(__p1 == __p2); } | |
4359 | ||
8e79468d BK |
4360 | private: |
4361 | // Hosts either log(mean) or the threshold of the simple method. | |
4362 | void | |
4363 | _M_initialize(); | |
4364 | ||
4365 | double _M_mean; | |
4366 | ||
4367 | double _M_lm_thr; | |
4368 | #if _GLIBCXX_USE_C99_MATH_TR1 | |
4369 | double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb; | |
4370 | #endif | |
4371 | }; | |
4372 | ||
4373 | // constructors and member function | |
4374 | explicit | |
4375 | poisson_distribution(double __mean = 1.0) | |
4376 | : _M_param(__mean), _M_nd() | |
4377 | { } | |
4378 | ||
4379 | explicit | |
4380 | poisson_distribution(const param_type& __p) | |
4381 | : _M_param(__p), _M_nd() | |
4382 | { } | |
4383 | ||
4384 | /** | |
4385 | * @brief Resets the distribution state. | |
4386 | */ | |
4387 | void | |
4388 | reset() | |
4389 | { _M_nd.reset(); } | |
4390 | ||
4391 | /** | |
4392 | * @brief Returns the distribution parameter @p mean. | |
4393 | */ | |
4394 | double | |
4395 | mean() const | |
4396 | { return _M_param.mean(); } | |
4397 | ||
4398 | /** | |
4399 | * @brief Returns the parameter set of the distribution. | |
4400 | */ | |
4401 | param_type | |
4402 | param() const | |
4403 | { return _M_param; } | |
4404 | ||
4405 | /** | |
4406 | * @brief Sets the parameter set of the distribution. | |
4407 | * @param __param The new parameter set of the distribution. | |
4408 | */ | |
4409 | void | |
4410 | param(const param_type& __param) | |
4411 | { _M_param = __param; } | |
4412 | ||
4413 | /** | |
4414 | * @brief Returns the greatest lower bound value of the distribution. | |
4415 | */ | |
4416 | result_type | |
4417 | min() const | |
4418 | { return 0; } | |
4419 | ||
4420 | /** | |
4421 | * @brief Returns the least upper bound value of the distribution. | |
4422 | */ | |
4423 | result_type | |
4424 | max() const | |
4425 | { return std::numeric_limits<result_type>::max(); } | |
4426 | ||
247d8075 PC |
4427 | /** |
4428 | * @brief Generating functions. | |
4429 | */ | |
8e79468d BK |
4430 | template<typename _UniformRandomNumberGenerator> |
4431 | result_type | |
4432 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 4433 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
4434 | |
4435 | template<typename _UniformRandomNumberGenerator> | |
4436 | result_type | |
4437 | operator()(_UniformRandomNumberGenerator& __urng, | |
4438 | const param_type& __p); | |
4439 | ||
7b93bdde UD |
4440 | template<typename _ForwardIterator, |
4441 | typename _UniformRandomNumberGenerator> | |
4442 | void | |
4443 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
4444 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 4445 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
4446 | |
4447 | template<typename _ForwardIterator, | |
4448 | typename _UniformRandomNumberGenerator> | |
4449 | void | |
4450 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
4451 | _UniformRandomNumberGenerator& __urng, | |
4452 | const param_type& __p) | |
4453 | { this->__generate_impl(__f, __t, __urng, __p); } | |
4454 | ||
4455 | template<typename _UniformRandomNumberGenerator> | |
4456 | void | |
4457 | __generate(result_type* __f, result_type* __t, | |
4458 | _UniformRandomNumberGenerator& __urng, | |
4459 | const param_type& __p) | |
4460 | { this->__generate_impl(__f, __t, __urng, __p); } | |
4461 | ||
fc05e1ba PC |
4462 | /** |
4463 | * @brief Return true if two Poisson distributions have the same | |
4464 | * parameters and the sequences that would be generated | |
4465 | * are equal. | |
4466 | */ | |
a30e18c1 MG |
4467 | friend bool |
4468 | operator==(const poisson_distribution& __d1, | |
4469 | const poisson_distribution& __d2) | |
fc05e1ba | 4470 | #ifdef _GLIBCXX_USE_C99_MATH_TR1 |
5bcb3b4d | 4471 | { return __d1._M_param == __d2._M_param && __d1._M_nd == __d2._M_nd; } |
fc05e1ba | 4472 | #else |
5bcb3b4d | 4473 | { return __d1._M_param == __d2._M_param; } |
fc05e1ba PC |
4474 | #endif |
4475 | ||
8e79468d BK |
4476 | /** |
4477 | * @brief Inserts a %poisson_distribution random number distribution | |
4478 | * @p __x into the output stream @p __os. | |
4479 | * | |
4480 | * @param __os An output stream. | |
4481 | * @param __x A %poisson_distribution random number distribution. | |
4482 | * | |
4483 | * @returns The output stream with the state of @p __x inserted or in | |
4484 | * an error state. | |
4485 | */ | |
4486 | template<typename _IntType1, typename _CharT, typename _Traits> | |
4487 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
4488 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
4489 | const std::poisson_distribution<_IntType1>& __x); | |
8e79468d BK |
4490 | |
4491 | /** | |
4492 | * @brief Extracts a %poisson_distribution random number distribution | |
4493 | * @p __x from the input stream @p __is. | |
4494 | * | |
4495 | * @param __is An input stream. | |
4496 | * @param __x A %poisson_distribution random number generator engine. | |
4497 | * | |
4498 | * @returns The input stream with @p __x extracted or in an error | |
4499 | * state. | |
4500 | */ | |
4501 | template<typename _IntType1, typename _CharT, typename _Traits> | |
4502 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
4503 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
4504 | std::poisson_distribution<_IntType1>& __x); | |
8e79468d BK |
4505 | |
4506 | private: | |
7b93bdde UD |
4507 | template<typename _ForwardIterator, |
4508 | typename _UniformRandomNumberGenerator> | |
4509 | void | |
4510 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
4511 | _UniformRandomNumberGenerator& __urng, | |
4512 | const param_type& __p); | |
4513 | ||
8e79468d BK |
4514 | param_type _M_param; |
4515 | ||
4516 | // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined. | |
f9b09dec | 4517 | std::normal_distribution<double> _M_nd; |
8e79468d BK |
4518 | }; |
4519 | ||
fc05e1ba PC |
4520 | /** |
4521 | * @brief Return true if two Poisson distributions are different. | |
4522 | */ | |
4523 | template<typename _IntType> | |
4524 | inline bool | |
4525 | operator!=(const std::poisson_distribution<_IntType>& __d1, | |
4526 | const std::poisson_distribution<_IntType>& __d2) | |
4527 | { return !(__d1 == __d2); } | |
4528 | ||
4529 | ||
8e79468d BK |
4530 | /** |
4531 | * @brief An exponential continuous distribution for random numbers. | |
4532 | * | |
4533 | * The formula for the exponential probability density function is | |
6cc5a790 | 4534 | * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$. |
8e79468d BK |
4535 | * |
4536 | * <table border=1 cellpadding=10 cellspacing=0> | |
4537 | * <caption align=top>Distribution Statistics</caption> | |
6cc5a790 BK |
4538 | * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr> |
4539 | * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr> | |
4540 | * <tr><td>Mode</td><td>@f$zero@f$</td></tr> | |
8e79468d | 4541 | * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr> |
6cc5a790 | 4542 | * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr> |
8e79468d BK |
4543 | * </table> |
4544 | */ | |
4545 | template<typename _RealType = double> | |
4546 | class exponential_distribution | |
4547 | { | |
77e3c516 | 4548 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 4549 | "result_type must be a floating point type"); |
77e3c516 | 4550 | |
8e79468d BK |
4551 | public: |
4552 | /** The type of the range of the distribution. */ | |
4553 | typedef _RealType result_type; | |
12905f10 | 4554 | |
8e79468d BK |
4555 | /** Parameter type. */ |
4556 | struct param_type | |
4557 | { | |
4558 | typedef exponential_distribution<_RealType> distribution_type; | |
4559 | ||
4560 | explicit | |
4561 | param_type(_RealType __lambda = _RealType(1)) | |
4562 | : _M_lambda(__lambda) | |
4563 | { | |
2f1e8e7c | 4564 | __glibcxx_assert(_M_lambda > _RealType(0)); |
8e79468d BK |
4565 | } |
4566 | ||
4567 | _RealType | |
4568 | lambda() const | |
4569 | { return _M_lambda; } | |
4570 | ||
fc05e1ba PC |
4571 | friend bool |
4572 | operator==(const param_type& __p1, const param_type& __p2) | |
4573 | { return __p1._M_lambda == __p2._M_lambda; } | |
4574 | ||
12905f10 JW |
4575 | friend bool |
4576 | operator!=(const param_type& __p1, const param_type& __p2) | |
4577 | { return !(__p1 == __p2); } | |
4578 | ||
8e79468d BK |
4579 | private: |
4580 | _RealType _M_lambda; | |
4581 | }; | |
4582 | ||
4583 | public: | |
4584 | /** | |
4585 | * @brief Constructs an exponential distribution with inverse scale | |
6cc5a790 | 4586 | * parameter @f$\lambda@f$. |
8e79468d BK |
4587 | */ |
4588 | explicit | |
4589 | exponential_distribution(const result_type& __lambda = result_type(1)) | |
4590 | : _M_param(__lambda) | |
4591 | { } | |
4592 | ||
4593 | explicit | |
4594 | exponential_distribution(const param_type& __p) | |
4595 | : _M_param(__p) | |
4596 | { } | |
4597 | ||
4598 | /** | |
4599 | * @brief Resets the distribution state. | |
4600 | * | |
4601 | * Has no effect on exponential distributions. | |
4602 | */ | |
4603 | void | |
4604 | reset() { } | |
4605 | ||
4606 | /** | |
4607 | * @brief Returns the inverse scale parameter of the distribution. | |
4608 | */ | |
4609 | _RealType | |
4610 | lambda() const | |
4611 | { return _M_param.lambda(); } | |
4612 | ||
4613 | /** | |
4614 | * @brief Returns the parameter set of the distribution. | |
4615 | */ | |
4616 | param_type | |
4617 | param() const | |
4618 | { return _M_param; } | |
4619 | ||
4620 | /** | |
4621 | * @brief Sets the parameter set of the distribution. | |
4622 | * @param __param The new parameter set of the distribution. | |
4623 | */ | |
4624 | void | |
4625 | param(const param_type& __param) | |
4626 | { _M_param = __param; } | |
4627 | ||
4628 | /** | |
4629 | * @brief Returns the greatest lower bound value of the distribution. | |
4630 | */ | |
4631 | result_type | |
4632 | min() const | |
4633 | { return result_type(0); } | |
4634 | ||
4635 | /** | |
4636 | * @brief Returns the least upper bound value of the distribution. | |
4637 | */ | |
4638 | result_type | |
4639 | max() const | |
4640 | { return std::numeric_limits<result_type>::max(); } | |
4641 | ||
247d8075 PC |
4642 | /** |
4643 | * @brief Generating functions. | |
4644 | */ | |
8e79468d BK |
4645 | template<typename _UniformRandomNumberGenerator> |
4646 | result_type | |
4647 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 4648 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
4649 | |
4650 | template<typename _UniformRandomNumberGenerator> | |
4651 | result_type | |
4652 | operator()(_UniformRandomNumberGenerator& __urng, | |
4653 | const param_type& __p) | |
4654 | { | |
4655 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
4656 | __aurng(__urng); | |
c2d9083d | 4657 | return -std::log(result_type(1) - __aurng()) / __p.lambda(); |
8e79468d BK |
4658 | } |
4659 | ||
7b93bdde UD |
4660 | template<typename _ForwardIterator, |
4661 | typename _UniformRandomNumberGenerator> | |
4662 | void | |
4663 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
4664 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 4665 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
4666 | |
4667 | template<typename _ForwardIterator, | |
4668 | typename _UniformRandomNumberGenerator> | |
4669 | void | |
4670 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
4671 | _UniformRandomNumberGenerator& __urng, | |
4672 | const param_type& __p) | |
4673 | { this->__generate_impl(__f, __t, __urng, __p); } | |
4674 | ||
4675 | template<typename _UniformRandomNumberGenerator> | |
4676 | void | |
4677 | __generate(result_type* __f, result_type* __t, | |
4678 | _UniformRandomNumberGenerator& __urng, | |
4679 | const param_type& __p) | |
4680 | { this->__generate_impl(__f, __t, __urng, __p); } | |
4681 | ||
5bcb3b4d PC |
4682 | /** |
4683 | * @brief Return true if two exponential distributions have the same | |
4684 | * parameters. | |
4685 | */ | |
4686 | friend bool | |
4687 | operator==(const exponential_distribution& __d1, | |
4688 | const exponential_distribution& __d2) | |
4689 | { return __d1._M_param == __d2._M_param; } | |
4690 | ||
8e79468d | 4691 | private: |
7b93bdde UD |
4692 | template<typename _ForwardIterator, |
4693 | typename _UniformRandomNumberGenerator> | |
4694 | void | |
4695 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
4696 | _UniformRandomNumberGenerator& __urng, | |
4697 | const param_type& __p); | |
4698 | ||
8e79468d BK |
4699 | param_type _M_param; |
4700 | }; | |
4701 | ||
fc05e1ba PC |
4702 | /** |
4703 | * @brief Return true if two exponential distributions have different | |
4704 | * parameters. | |
4705 | */ | |
4706 | template<typename _RealType> | |
4707 | inline bool | |
4708 | operator!=(const std::exponential_distribution<_RealType>& __d1, | |
4709 | const std::exponential_distribution<_RealType>& __d2) | |
4710 | { return !(__d1 == __d2); } | |
4711 | ||
8e79468d BK |
4712 | /** |
4713 | * @brief Inserts a %exponential_distribution random number distribution | |
4714 | * @p __x into the output stream @p __os. | |
4715 | * | |
4716 | * @param __os An output stream. | |
4717 | * @param __x A %exponential_distribution random number distribution. | |
4718 | * | |
4719 | * @returns The output stream with the state of @p __x inserted or in | |
4720 | * an error state. | |
4721 | */ | |
4722 | template<typename _RealType, typename _CharT, typename _Traits> | |
4723 | std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
4724 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
4725 | const std::exponential_distribution<_RealType>& __x); | |
8e79468d BK |
4726 | |
4727 | /** | |
4728 | * @brief Extracts a %exponential_distribution random number distribution | |
4729 | * @p __x from the input stream @p __is. | |
4730 | * | |
4731 | * @param __is An input stream. | |
4732 | * @param __x A %exponential_distribution random number | |
4733 | * generator engine. | |
4734 | * | |
4735 | * @returns The input stream with @p __x extracted or in an error state. | |
4736 | */ | |
4737 | template<typename _RealType, typename _CharT, typename _Traits> | |
4738 | std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
4739 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
4740 | std::exponential_distribution<_RealType>& __x); | |
8e79468d BK |
4741 | |
4742 | ||
8e79468d BK |
4743 | /** |
4744 | * @brief A weibull_distribution random number distribution. | |
4745 | * | |
6cc5a790 BK |
4746 | * The formula for the normal probability density function is: |
4747 | * @f[ | |
4748 | * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1} | |
4749 | * \exp{(-(\frac{x}{\beta})^\alpha)} | |
4750 | * @f] | |
8e79468d BK |
4751 | */ |
4752 | template<typename _RealType = double> | |
4753 | class weibull_distribution | |
4754 | { | |
77e3c516 | 4755 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 4756 | "result_type must be a floating point type"); |
77e3c516 | 4757 | |
8e79468d BK |
4758 | public: |
4759 | /** The type of the range of the distribution. */ | |
4760 | typedef _RealType result_type; | |
12905f10 | 4761 | |
8e79468d BK |
4762 | /** Parameter type. */ |
4763 | struct param_type | |
4764 | { | |
4765 | typedef weibull_distribution<_RealType> distribution_type; | |
4766 | ||
4767 | explicit | |
4768 | param_type(_RealType __a = _RealType(1), | |
4769 | _RealType __b = _RealType(1)) | |
4770 | : _M_a(__a), _M_b(__b) | |
4771 | { } | |
4772 | ||
4773 | _RealType | |
4774 | a() const | |
4775 | { return _M_a; } | |
4776 | ||
4777 | _RealType | |
4778 | b() const | |
4779 | { return _M_b; } | |
4780 | ||
fc05e1ba PC |
4781 | friend bool |
4782 | operator==(const param_type& __p1, const param_type& __p2) | |
4783 | { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } | |
4784 | ||
12905f10 JW |
4785 | friend bool |
4786 | operator!=(const param_type& __p1, const param_type& __p2) | |
4787 | { return !(__p1 == __p2); } | |
4788 | ||
8e79468d BK |
4789 | private: |
4790 | _RealType _M_a; | |
4791 | _RealType _M_b; | |
4792 | }; | |
4793 | ||
4794 | explicit | |
4795 | weibull_distribution(_RealType __a = _RealType(1), | |
4796 | _RealType __b = _RealType(1)) | |
4797 | : _M_param(__a, __b) | |
4798 | { } | |
4799 | ||
4800 | explicit | |
4801 | weibull_distribution(const param_type& __p) | |
4802 | : _M_param(__p) | |
4803 | { } | |
4804 | ||
4805 | /** | |
4806 | * @brief Resets the distribution state. | |
4807 | */ | |
4808 | void | |
4809 | reset() | |
4810 | { } | |
4811 | ||
4812 | /** | |
6cc5a790 | 4813 | * @brief Return the @f$a@f$ parameter of the distribution. |
8e79468d BK |
4814 | */ |
4815 | _RealType | |
4816 | a() const | |
4817 | { return _M_param.a(); } | |
4818 | ||
4819 | /** | |
6cc5a790 | 4820 | * @brief Return the @f$b@f$ parameter of the distribution. |
8e79468d BK |
4821 | */ |
4822 | _RealType | |
4823 | b() const | |
4824 | { return _M_param.b(); } | |
4825 | ||
4826 | /** | |
4827 | * @brief Returns the parameter set of the distribution. | |
4828 | */ | |
4829 | param_type | |
4830 | param() const | |
4831 | { return _M_param; } | |
4832 | ||
4833 | /** | |
4834 | * @brief Sets the parameter set of the distribution. | |
4835 | * @param __param The new parameter set of the distribution. | |
4836 | */ | |
4837 | void | |
4838 | param(const param_type& __param) | |
4839 | { _M_param = __param; } | |
4840 | ||
4841 | /** | |
4842 | * @brief Returns the greatest lower bound value of the distribution. | |
4843 | */ | |
4844 | result_type | |
4845 | min() const | |
4846 | { return result_type(0); } | |
4847 | ||
4848 | /** | |
4849 | * @brief Returns the least upper bound value of the distribution. | |
4850 | */ | |
4851 | result_type | |
4852 | max() const | |
4853 | { return std::numeric_limits<result_type>::max(); } | |
4854 | ||
247d8075 PC |
4855 | /** |
4856 | * @brief Generating functions. | |
4857 | */ | |
8e79468d BK |
4858 | template<typename _UniformRandomNumberGenerator> |
4859 | result_type | |
4860 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 4861 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
4862 | |
4863 | template<typename _UniformRandomNumberGenerator> | |
4864 | result_type | |
4865 | operator()(_UniformRandomNumberGenerator& __urng, | |
b01630bb | 4866 | const param_type& __p); |
8e79468d | 4867 | |
7b93bdde UD |
4868 | template<typename _ForwardIterator, |
4869 | typename _UniformRandomNumberGenerator> | |
4870 | void | |
4871 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
4872 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 4873 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
4874 | |
4875 | template<typename _ForwardIterator, | |
4876 | typename _UniformRandomNumberGenerator> | |
4877 | void | |
4878 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
4879 | _UniformRandomNumberGenerator& __urng, | |
4880 | const param_type& __p) | |
4881 | { this->__generate_impl(__f, __t, __urng, __p); } | |
4882 | ||
4883 | template<typename _UniformRandomNumberGenerator> | |
4884 | void | |
4885 | __generate(result_type* __f, result_type* __t, | |
4886 | _UniformRandomNumberGenerator& __urng, | |
4887 | const param_type& __p) | |
4888 | { this->__generate_impl(__f, __t, __urng, __p); } | |
4889 | ||
5bcb3b4d PC |
4890 | /** |
4891 | * @brief Return true if two Weibull distributions have the same | |
4892 | * parameters. | |
4893 | */ | |
4894 | friend bool | |
4895 | operator==(const weibull_distribution& __d1, | |
4896 | const weibull_distribution& __d2) | |
4897 | { return __d1._M_param == __d2._M_param; } | |
4898 | ||
8e79468d | 4899 | private: |
7b93bdde UD |
4900 | template<typename _ForwardIterator, |
4901 | typename _UniformRandomNumberGenerator> | |
4902 | void | |
4903 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
4904 | _UniformRandomNumberGenerator& __urng, | |
4905 | const param_type& __p); | |
4906 | ||
8e79468d BK |
4907 | param_type _M_param; |
4908 | }; | |
4909 | ||
fc05e1ba PC |
4910 | /** |
4911 | * @brief Return true if two Weibull distributions have different | |
4912 | * parameters. | |
4913 | */ | |
4914 | template<typename _RealType> | |
4915 | inline bool | |
4916 | operator!=(const std::weibull_distribution<_RealType>& __d1, | |
4917 | const std::weibull_distribution<_RealType>& __d2) | |
4918 | { return !(__d1 == __d2); } | |
4919 | ||
8e79468d BK |
4920 | /** |
4921 | * @brief Inserts a %weibull_distribution random number distribution | |
4922 | * @p __x into the output stream @p __os. | |
4923 | * | |
4924 | * @param __os An output stream. | |
4925 | * @param __x A %weibull_distribution random number distribution. | |
4926 | * | |
4927 | * @returns The output stream with the state of @p __x inserted or in | |
4928 | * an error state. | |
4929 | */ | |
4930 | template<typename _RealType, typename _CharT, typename _Traits> | |
4931 | std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
4932 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
4933 | const std::weibull_distribution<_RealType>& __x); | |
8e79468d BK |
4934 | |
4935 | /** | |
4936 | * @brief Extracts a %weibull_distribution random number distribution | |
4937 | * @p __x from the input stream @p __is. | |
4938 | * | |
4939 | * @param __is An input stream. | |
4940 | * @param __x A %weibull_distribution random number | |
4941 | * generator engine. | |
4942 | * | |
4943 | * @returns The input stream with @p __x extracted or in an error state. | |
4944 | */ | |
4945 | template<typename _RealType, typename _CharT, typename _Traits> | |
4946 | std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
4947 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
4948 | std::weibull_distribution<_RealType>& __x); | |
8e79468d BK |
4949 | |
4950 | ||
4951 | /** | |
4952 | * @brief A extreme_value_distribution random number distribution. | |
4953 | * | |
4954 | * The formula for the normal probability mass function is | |
6cc5a790 BK |
4955 | * @f[ |
4956 | * p(x|a,b) = \frac{1}{b} | |
4957 | * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) | |
4958 | * @f] | |
8e79468d BK |
4959 | */ |
4960 | template<typename _RealType = double> | |
4961 | class extreme_value_distribution | |
4962 | { | |
77e3c516 | 4963 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 4964 | "result_type must be a floating point type"); |
77e3c516 | 4965 | |
8e79468d BK |
4966 | public: |
4967 | /** The type of the range of the distribution. */ | |
4968 | typedef _RealType result_type; | |
12905f10 | 4969 | |
8e79468d BK |
4970 | /** Parameter type. */ |
4971 | struct param_type | |
4972 | { | |
4973 | typedef extreme_value_distribution<_RealType> distribution_type; | |
4974 | ||
4975 | explicit | |
4976 | param_type(_RealType __a = _RealType(0), | |
4977 | _RealType __b = _RealType(1)) | |
4978 | : _M_a(__a), _M_b(__b) | |
4979 | { } | |
4980 | ||
4981 | _RealType | |
4982 | a() const | |
4983 | { return _M_a; } | |
4984 | ||
4985 | _RealType | |
4986 | b() const | |
4987 | { return _M_b; } | |
4988 | ||
fc05e1ba PC |
4989 | friend bool |
4990 | operator==(const param_type& __p1, const param_type& __p2) | |
4991 | { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; } | |
4992 | ||
12905f10 JW |
4993 | friend bool |
4994 | operator!=(const param_type& __p1, const param_type& __p2) | |
4995 | { return !(__p1 == __p2); } | |
4996 | ||
8e79468d BK |
4997 | private: |
4998 | _RealType _M_a; | |
4999 | _RealType _M_b; | |
5000 | }; | |
5001 | ||
5002 | explicit | |
5003 | extreme_value_distribution(_RealType __a = _RealType(0), | |
5004 | _RealType __b = _RealType(1)) | |
5005 | : _M_param(__a, __b) | |
5006 | { } | |
5007 | ||
5008 | explicit | |
5009 | extreme_value_distribution(const param_type& __p) | |
5010 | : _M_param(__p) | |
5011 | { } | |
5012 | ||
5013 | /** | |
5014 | * @brief Resets the distribution state. | |
5015 | */ | |
5016 | void | |
5017 | reset() | |
5018 | { } | |
5019 | ||
5020 | /** | |
6cc5a790 | 5021 | * @brief Return the @f$a@f$ parameter of the distribution. |
8e79468d BK |
5022 | */ |
5023 | _RealType | |
5024 | a() const | |
5025 | { return _M_param.a(); } | |
5026 | ||
5027 | /** | |
6cc5a790 | 5028 | * @brief Return the @f$b@f$ parameter of the distribution. |
8e79468d BK |
5029 | */ |
5030 | _RealType | |
5031 | b() const | |
5032 | { return _M_param.b(); } | |
5033 | ||
5034 | /** | |
5035 | * @brief Returns the parameter set of the distribution. | |
5036 | */ | |
5037 | param_type | |
5038 | param() const | |
5039 | { return _M_param; } | |
5040 | ||
5041 | /** | |
5042 | * @brief Sets the parameter set of the distribution. | |
5043 | * @param __param The new parameter set of the distribution. | |
5044 | */ | |
5045 | void | |
5046 | param(const param_type& __param) | |
5047 | { _M_param = __param; } | |
5048 | ||
5049 | /** | |
5050 | * @brief Returns the greatest lower bound value of the distribution. | |
5051 | */ | |
5052 | result_type | |
5053 | min() const | |
a803975d | 5054 | { return std::numeric_limits<result_type>::lowest(); } |
8e79468d BK |
5055 | |
5056 | /** | |
5057 | * @brief Returns the least upper bound value of the distribution. | |
5058 | */ | |
5059 | result_type | |
5060 | max() const | |
5061 | { return std::numeric_limits<result_type>::max(); } | |
5062 | ||
247d8075 PC |
5063 | /** |
5064 | * @brief Generating functions. | |
5065 | */ | |
8e79468d BK |
5066 | template<typename _UniformRandomNumberGenerator> |
5067 | result_type | |
5068 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 5069 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
5070 | |
5071 | template<typename _UniformRandomNumberGenerator> | |
5072 | result_type | |
5073 | operator()(_UniformRandomNumberGenerator& __urng, | |
5074 | const param_type& __p); | |
5075 | ||
7b93bdde UD |
5076 | template<typename _ForwardIterator, |
5077 | typename _UniformRandomNumberGenerator> | |
5078 | void | |
5079 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
5080 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 5081 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
5082 | |
5083 | template<typename _ForwardIterator, | |
5084 | typename _UniformRandomNumberGenerator> | |
5085 | void | |
5086 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
5087 | _UniformRandomNumberGenerator& __urng, | |
5088 | const param_type& __p) | |
5089 | { this->__generate_impl(__f, __t, __urng, __p); } | |
5090 | ||
5091 | template<typename _UniformRandomNumberGenerator> | |
5092 | void | |
5093 | __generate(result_type* __f, result_type* __t, | |
5094 | _UniformRandomNumberGenerator& __urng, | |
5095 | const param_type& __p) | |
5096 | { this->__generate_impl(__f, __t, __urng, __p); } | |
5097 | ||
5bcb3b4d PC |
5098 | /** |
5099 | * @brief Return true if two extreme value distributions have the same | |
5100 | * parameters. | |
5101 | */ | |
5102 | friend bool | |
5103 | operator==(const extreme_value_distribution& __d1, | |
5104 | const extreme_value_distribution& __d2) | |
5105 | { return __d1._M_param == __d2._M_param; } | |
5106 | ||
8e79468d | 5107 | private: |
7b93bdde UD |
5108 | template<typename _ForwardIterator, |
5109 | typename _UniformRandomNumberGenerator> | |
5110 | void | |
5111 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
5112 | _UniformRandomNumberGenerator& __urng, | |
5113 | const param_type& __p); | |
5114 | ||
8e79468d BK |
5115 | param_type _M_param; |
5116 | }; | |
5117 | ||
fc05e1ba PC |
5118 | /** |
5119 | * @brief Return true if two extreme value distributions have different | |
5120 | * parameters. | |
5121 | */ | |
5122 | template<typename _RealType> | |
5123 | inline bool | |
5124 | operator!=(const std::extreme_value_distribution<_RealType>& __d1, | |
5125 | const std::extreme_value_distribution<_RealType>& __d2) | |
5126 | { return !(__d1 == __d2); } | |
5127 | ||
8e79468d BK |
5128 | /** |
5129 | * @brief Inserts a %extreme_value_distribution random number distribution | |
5130 | * @p __x into the output stream @p __os. | |
5131 | * | |
5132 | * @param __os An output stream. | |
5133 | * @param __x A %extreme_value_distribution random number distribution. | |
5134 | * | |
5135 | * @returns The output stream with the state of @p __x inserted or in | |
5136 | * an error state. | |
5137 | */ | |
5138 | template<typename _RealType, typename _CharT, typename _Traits> | |
5139 | std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
5140 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
5141 | const std::extreme_value_distribution<_RealType>& __x); | |
8e79468d BK |
5142 | |
5143 | /** | |
5144 | * @brief Extracts a %extreme_value_distribution random number | |
5145 | * distribution @p __x from the input stream @p __is. | |
5146 | * | |
5147 | * @param __is An input stream. | |
5148 | * @param __x A %extreme_value_distribution random number | |
5149 | * generator engine. | |
5150 | * | |
5151 | * @returns The input stream with @p __x extracted or in an error state. | |
5152 | */ | |
5153 | template<typename _RealType, typename _CharT, typename _Traits> | |
5154 | std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
5155 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
5156 | std::extreme_value_distribution<_RealType>& __x); | |
8e79468d BK |
5157 | |
5158 | ||
5159 | /** | |
5160 | * @brief A discrete_distribution random number distribution. | |
5161 | * | |
5162 | * The formula for the discrete probability mass function is | |
5163 | * | |
5164 | */ | |
5165 | template<typename _IntType = int> | |
5166 | class discrete_distribution | |
5167 | { | |
77e3c516 | 5168 | static_assert(std::is_integral<_IntType>::value, |
0cded43d | 5169 | "result_type must be an integral type"); |
8e79468d BK |
5170 | |
5171 | public: | |
5172 | /** The type of the range of the distribution. */ | |
5173 | typedef _IntType result_type; | |
12905f10 | 5174 | |
8e79468d BK |
5175 | /** Parameter type. */ |
5176 | struct param_type | |
5177 | { | |
5178 | typedef discrete_distribution<_IntType> distribution_type; | |
5179 | friend class discrete_distribution<_IntType>; | |
5180 | ||
5181 | param_type() | |
879b9073 | 5182 | : _M_prob(), _M_cp() |
ce9555cb | 5183 | { } |
8e79468d BK |
5184 | |
5185 | template<typename _InputIterator> | |
5186 | param_type(_InputIterator __wbegin, | |
5187 | _InputIterator __wend) | |
5188 | : _M_prob(__wbegin, __wend), _M_cp() | |
5189 | { _M_initialize(); } | |
5190 | ||
5191 | param_type(initializer_list<double> __wil) | |
5192 | : _M_prob(__wil.begin(), __wil.end()), _M_cp() | |
5193 | { _M_initialize(); } | |
5194 | ||
5195 | template<typename _Func> | |
5196 | param_type(size_t __nw, double __xmin, double __xmax, | |
5197 | _Func __fw); | |
5198 | ||
ce9555cb PC |
5199 | // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/ |
5200 | param_type(const param_type&) = default; | |
5201 | param_type& operator=(const param_type&) = default; | |
5202 | ||
8e79468d BK |
5203 | std::vector<double> |
5204 | probabilities() const | |
879b9073 | 5205 | { return _M_prob.empty() ? std::vector<double>(1, 1.0) : _M_prob; } |
8e79468d | 5206 | |
fc05e1ba PC |
5207 | friend bool |
5208 | operator==(const param_type& __p1, const param_type& __p2) | |
5209 | { return __p1._M_prob == __p2._M_prob; } | |
5210 | ||
12905f10 JW |
5211 | friend bool |
5212 | operator!=(const param_type& __p1, const param_type& __p2) | |
5213 | { return !(__p1 == __p2); } | |
5214 | ||
8e79468d BK |
5215 | private: |
5216 | void | |
5217 | _M_initialize(); | |
5218 | ||
5219 | std::vector<double> _M_prob; | |
5220 | std::vector<double> _M_cp; | |
5221 | }; | |
5222 | ||
5223 | discrete_distribution() | |
5224 | : _M_param() | |
5225 | { } | |
5226 | ||
5227 | template<typename _InputIterator> | |
5228 | discrete_distribution(_InputIterator __wbegin, | |
5229 | _InputIterator __wend) | |
5230 | : _M_param(__wbegin, __wend) | |
5231 | { } | |
5232 | ||
f8dd9e0d PC |
5233 | discrete_distribution(initializer_list<double> __wl) |
5234 | : _M_param(__wl) | |
8e79468d BK |
5235 | { } |
5236 | ||
5237 | template<typename _Func> | |
5238 | discrete_distribution(size_t __nw, double __xmin, double __xmax, | |
5239 | _Func __fw) | |
5240 | : _M_param(__nw, __xmin, __xmax, __fw) | |
5241 | { } | |
5242 | ||
5243 | explicit | |
5244 | discrete_distribution(const param_type& __p) | |
5245 | : _M_param(__p) | |
5246 | { } | |
5247 | ||
5248 | /** | |
5249 | * @brief Resets the distribution state. | |
5250 | */ | |
5251 | void | |
5252 | reset() | |
5253 | { } | |
5254 | ||
5255 | /** | |
5256 | * @brief Returns the probabilities of the distribution. | |
5257 | */ | |
5258 | std::vector<double> | |
5259 | probabilities() const | |
879b9073 PC |
5260 | { |
5261 | return _M_param._M_prob.empty() | |
5262 | ? std::vector<double>(1, 1.0) : _M_param._M_prob; | |
5263 | } | |
8e79468d BK |
5264 | |
5265 | /** | |
5266 | * @brief Returns the parameter set of the distribution. | |
5267 | */ | |
5268 | param_type | |
5269 | param() const | |
5270 | { return _M_param; } | |
5271 | ||
5272 | /** | |
5273 | * @brief Sets the parameter set of the distribution. | |
5274 | * @param __param The new parameter set of the distribution. | |
5275 | */ | |
5276 | void | |
5277 | param(const param_type& __param) | |
5278 | { _M_param = __param; } | |
5279 | ||
5280 | /** | |
5281 | * @brief Returns the greatest lower bound value of the distribution. | |
5282 | */ | |
5283 | result_type | |
5284 | min() const | |
5285 | { return result_type(0); } | |
5286 | ||
5287 | /** | |
5288 | * @brief Returns the least upper bound value of the distribution. | |
5289 | */ | |
5290 | result_type | |
5291 | max() const | |
879b9073 PC |
5292 | { |
5293 | return _M_param._M_prob.empty() | |
5294 | ? result_type(0) : result_type(_M_param._M_prob.size() - 1); | |
5295 | } | |
8e79468d | 5296 | |
247d8075 PC |
5297 | /** |
5298 | * @brief Generating functions. | |
5299 | */ | |
8e79468d BK |
5300 | template<typename _UniformRandomNumberGenerator> |
5301 | result_type | |
5302 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 5303 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
5304 | |
5305 | template<typename _UniformRandomNumberGenerator> | |
5306 | result_type | |
5307 | operator()(_UniformRandomNumberGenerator& __urng, | |
5308 | const param_type& __p); | |
5309 | ||
7b93bdde UD |
5310 | template<typename _ForwardIterator, |
5311 | typename _UniformRandomNumberGenerator> | |
5312 | void | |
5313 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
5314 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 5315 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
5316 | |
5317 | template<typename _ForwardIterator, | |
5318 | typename _UniformRandomNumberGenerator> | |
5319 | void | |
5320 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
5321 | _UniformRandomNumberGenerator& __urng, | |
5322 | const param_type& __p) | |
5323 | { this->__generate_impl(__f, __t, __urng, __p); } | |
5324 | ||
5325 | template<typename _UniformRandomNumberGenerator> | |
5326 | void | |
5327 | __generate(result_type* __f, result_type* __t, | |
5328 | _UniformRandomNumberGenerator& __urng, | |
5329 | const param_type& __p) | |
5330 | { this->__generate_impl(__f, __t, __urng, __p); } | |
5331 | ||
5bcb3b4d PC |
5332 | /** |
5333 | * @brief Return true if two discrete distributions have the same | |
5334 | * parameters. | |
5335 | */ | |
5336 | friend bool | |
5337 | operator==(const discrete_distribution& __d1, | |
5338 | const discrete_distribution& __d2) | |
5339 | { return __d1._M_param == __d2._M_param; } | |
5340 | ||
8e79468d BK |
5341 | /** |
5342 | * @brief Inserts a %discrete_distribution random number distribution | |
5343 | * @p __x into the output stream @p __os. | |
5344 | * | |
5345 | * @param __os An output stream. | |
5346 | * @param __x A %discrete_distribution random number distribution. | |
5347 | * | |
5348 | * @returns The output stream with the state of @p __x inserted or in | |
5349 | * an error state. | |
5350 | */ | |
5351 | template<typename _IntType1, typename _CharT, typename _Traits> | |
5352 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
5353 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
5354 | const std::discrete_distribution<_IntType1>& __x); | |
8e79468d BK |
5355 | |
5356 | /** | |
5357 | * @brief Extracts a %discrete_distribution random number distribution | |
5358 | * @p __x from the input stream @p __is. | |
5359 | * | |
5360 | * @param __is An input stream. | |
5361 | * @param __x A %discrete_distribution random number | |
5362 | * generator engine. | |
5363 | * | |
5364 | * @returns The input stream with @p __x extracted or in an error | |
5365 | * state. | |
5366 | */ | |
5367 | template<typename _IntType1, typename _CharT, typename _Traits> | |
5368 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
5369 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
5370 | std::discrete_distribution<_IntType1>& __x); | |
8e79468d BK |
5371 | |
5372 | private: | |
7b93bdde UD |
5373 | template<typename _ForwardIterator, |
5374 | typename _UniformRandomNumberGenerator> | |
5375 | void | |
5376 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
5377 | _UniformRandomNumberGenerator& __urng, | |
5378 | const param_type& __p); | |
5379 | ||
8e79468d BK |
5380 | param_type _M_param; |
5381 | }; | |
5382 | ||
fc05e1ba PC |
5383 | /** |
5384 | * @brief Return true if two discrete distributions have different | |
5385 | * parameters. | |
5386 | */ | |
5387 | template<typename _IntType> | |
5388 | inline bool | |
5389 | operator!=(const std::discrete_distribution<_IntType>& __d1, | |
5390 | const std::discrete_distribution<_IntType>& __d2) | |
5391 | { return !(__d1 == __d2); } | |
5392 | ||
8e79468d BK |
5393 | |
5394 | /** | |
5395 | * @brief A piecewise_constant_distribution random number distribution. | |
5396 | * | |
5397 | * The formula for the piecewise constant probability mass function is | |
5398 | * | |
5399 | */ | |
5400 | template<typename _RealType = double> | |
5401 | class piecewise_constant_distribution | |
5402 | { | |
77e3c516 | 5403 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 5404 | "result_type must be a floating point type"); |
77e3c516 | 5405 | |
8e79468d BK |
5406 | public: |
5407 | /** The type of the range of the distribution. */ | |
5408 | typedef _RealType result_type; | |
12905f10 | 5409 | |
8e79468d BK |
5410 | /** Parameter type. */ |
5411 | struct param_type | |
5412 | { | |
5413 | typedef piecewise_constant_distribution<_RealType> distribution_type; | |
5414 | friend class piecewise_constant_distribution<_RealType>; | |
5415 | ||
b01630bb | 5416 | param_type() |
879b9073 PC |
5417 | : _M_int(), _M_den(), _M_cp() |
5418 | { } | |
8e79468d BK |
5419 | |
5420 | template<typename _InputIteratorB, typename _InputIteratorW> | |
5421 | param_type(_InputIteratorB __bfirst, | |
5422 | _InputIteratorB __bend, | |
5423 | _InputIteratorW __wbegin); | |
5424 | ||
5425 | template<typename _Func> | |
f8dd9e0d | 5426 | param_type(initializer_list<_RealType> __bi, _Func __fw); |
8e79468d BK |
5427 | |
5428 | template<typename _Func> | |
5429 | param_type(size_t __nw, _RealType __xmin, _RealType __xmax, | |
5430 | _Func __fw); | |
5431 | ||
ce9555cb PC |
5432 | // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/ |
5433 | param_type(const param_type&) = default; | |
5434 | param_type& operator=(const param_type&) = default; | |
5435 | ||
8e79468d BK |
5436 | std::vector<_RealType> |
5437 | intervals() const | |
879b9073 PC |
5438 | { |
5439 | if (_M_int.empty()) | |
5440 | { | |
5441 | std::vector<_RealType> __tmp(2); | |
5442 | __tmp[1] = _RealType(1); | |
5443 | return __tmp; | |
5444 | } | |
5445 | else | |
5446 | return _M_int; | |
5447 | } | |
8e79468d BK |
5448 | |
5449 | std::vector<double> | |
5450 | densities() const | |
879b9073 | 5451 | { return _M_den.empty() ? std::vector<double>(1, 1.0) : _M_den; } |
8e79468d | 5452 | |
fc05e1ba PC |
5453 | friend bool |
5454 | operator==(const param_type& __p1, const param_type& __p2) | |
5455 | { return __p1._M_int == __p2._M_int && __p1._M_den == __p2._M_den; } | |
5456 | ||
12905f10 JW |
5457 | friend bool |
5458 | operator!=(const param_type& __p1, const param_type& __p2) | |
5459 | { return !(__p1 == __p2); } | |
5460 | ||
8e79468d BK |
5461 | private: |
5462 | void | |
5463 | _M_initialize(); | |
5464 | ||
5465 | std::vector<_RealType> _M_int; | |
5466 | std::vector<double> _M_den; | |
5467 | std::vector<double> _M_cp; | |
5468 | }; | |
5469 | ||
5470 | explicit | |
5471 | piecewise_constant_distribution() | |
5472 | : _M_param() | |
5473 | { } | |
5474 | ||
5475 | template<typename _InputIteratorB, typename _InputIteratorW> | |
5476 | piecewise_constant_distribution(_InputIteratorB __bfirst, | |
5477 | _InputIteratorB __bend, | |
5478 | _InputIteratorW __wbegin) | |
5479 | : _M_param(__bfirst, __bend, __wbegin) | |
5480 | { } | |
5481 | ||
5482 | template<typename _Func> | |
f8dd9e0d | 5483 | piecewise_constant_distribution(initializer_list<_RealType> __bl, |
8e79468d | 5484 | _Func __fw) |
f8dd9e0d | 5485 | : _M_param(__bl, __fw) |
8e79468d BK |
5486 | { } |
5487 | ||
5488 | template<typename _Func> | |
5489 | piecewise_constant_distribution(size_t __nw, | |
5490 | _RealType __xmin, _RealType __xmax, | |
5491 | _Func __fw) | |
5492 | : _M_param(__nw, __xmin, __xmax, __fw) | |
5493 | { } | |
5494 | ||
5495 | explicit | |
5496 | piecewise_constant_distribution(const param_type& __p) | |
5497 | : _M_param(__p) | |
5498 | { } | |
5499 | ||
5500 | /** | |
5501 | * @brief Resets the distribution state. | |
5502 | */ | |
5503 | void | |
5504 | reset() | |
5505 | { } | |
5506 | ||
5507 | /** | |
5508 | * @brief Returns a vector of the intervals. | |
5509 | */ | |
5510 | std::vector<_RealType> | |
5511 | intervals() const | |
879b9073 PC |
5512 | { |
5513 | if (_M_param._M_int.empty()) | |
5514 | { | |
5515 | std::vector<_RealType> __tmp(2); | |
5516 | __tmp[1] = _RealType(1); | |
5517 | return __tmp; | |
5518 | } | |
5519 | else | |
5520 | return _M_param._M_int; | |
5521 | } | |
8e79468d BK |
5522 | |
5523 | /** | |
5524 | * @brief Returns a vector of the probability densities. | |
5525 | */ | |
5526 | std::vector<double> | |
5527 | densities() const | |
879b9073 PC |
5528 | { |
5529 | return _M_param._M_den.empty() | |
5530 | ? std::vector<double>(1, 1.0) : _M_param._M_den; | |
5531 | } | |
8e79468d BK |
5532 | |
5533 | /** | |
5534 | * @brief Returns the parameter set of the distribution. | |
5535 | */ | |
5536 | param_type | |
5537 | param() const | |
5538 | { return _M_param; } | |
5539 | ||
5540 | /** | |
5541 | * @brief Sets the parameter set of the distribution. | |
5542 | * @param __param The new parameter set of the distribution. | |
5543 | */ | |
5544 | void | |
5545 | param(const param_type& __param) | |
5546 | { _M_param = __param; } | |
5547 | ||
5548 | /** | |
5549 | * @brief Returns the greatest lower bound value of the distribution. | |
5550 | */ | |
5551 | result_type | |
5552 | min() const | |
879b9073 PC |
5553 | { |
5554 | return _M_param._M_int.empty() | |
5555 | ? result_type(0) : _M_param._M_int.front(); | |
5556 | } | |
8e79468d BK |
5557 | |
5558 | /** | |
5559 | * @brief Returns the least upper bound value of the distribution. | |
5560 | */ | |
5561 | result_type | |
5562 | max() const | |
879b9073 PC |
5563 | { |
5564 | return _M_param._M_int.empty() | |
5565 | ? result_type(1) : _M_param._M_int.back(); | |
5566 | } | |
8e79468d | 5567 | |
247d8075 PC |
5568 | /** |
5569 | * @brief Generating functions. | |
5570 | */ | |
8e79468d BK |
5571 | template<typename _UniformRandomNumberGenerator> |
5572 | result_type | |
5573 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 5574 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
5575 | |
5576 | template<typename _UniformRandomNumberGenerator> | |
5577 | result_type | |
5578 | operator()(_UniformRandomNumberGenerator& __urng, | |
5579 | const param_type& __p); | |
5580 | ||
7b93bdde UD |
5581 | template<typename _ForwardIterator, |
5582 | typename _UniformRandomNumberGenerator> | |
5583 | void | |
5584 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
5585 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 5586 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
5587 | |
5588 | template<typename _ForwardIterator, | |
5589 | typename _UniformRandomNumberGenerator> | |
5590 | void | |
5591 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
5592 | _UniformRandomNumberGenerator& __urng, | |
5593 | const param_type& __p) | |
5594 | { this->__generate_impl(__f, __t, __urng, __p); } | |
5595 | ||
5596 | template<typename _UniformRandomNumberGenerator> | |
5597 | void | |
5598 | __generate(result_type* __f, result_type* __t, | |
5599 | _UniformRandomNumberGenerator& __urng, | |
5600 | const param_type& __p) | |
5601 | { this->__generate_impl(__f, __t, __urng, __p); } | |
5602 | ||
5bcb3b4d PC |
5603 | /** |
5604 | * @brief Return true if two piecewise constant distributions have the | |
5605 | * same parameters. | |
5606 | */ | |
5607 | friend bool | |
5608 | operator==(const piecewise_constant_distribution& __d1, | |
5609 | const piecewise_constant_distribution& __d2) | |
5610 | { return __d1._M_param == __d2._M_param; } | |
5611 | ||
8e79468d | 5612 | /** |
64e1ab11 | 5613 | * @brief Inserts a %piecewise_constant_distribution random |
8e79468d BK |
5614 | * number distribution @p __x into the output stream @p __os. |
5615 | * | |
5616 | * @param __os An output stream. | |
64e1ab11 | 5617 | * @param __x A %piecewise_constant_distribution random number |
8e79468d BK |
5618 | * distribution. |
5619 | * | |
5620 | * @returns The output stream with the state of @p __x inserted or in | |
5621 | * an error state. | |
5622 | */ | |
5623 | template<typename _RealType1, typename _CharT, typename _Traits> | |
5624 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
5625 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
5626 | const std::piecewise_constant_distribution<_RealType1>& __x); | |
8e79468d BK |
5627 | |
5628 | /** | |
64e1ab11 | 5629 | * @brief Extracts a %piecewise_constant_distribution random |
8e79468d BK |
5630 | * number distribution @p __x from the input stream @p __is. |
5631 | * | |
5632 | * @param __is An input stream. | |
64e1ab11 | 5633 | * @param __x A %piecewise_constant_distribution random number |
8e79468d BK |
5634 | * generator engine. |
5635 | * | |
5636 | * @returns The input stream with @p __x extracted or in an error | |
5637 | * state. | |
5638 | */ | |
5639 | template<typename _RealType1, typename _CharT, typename _Traits> | |
5640 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
5641 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
5642 | std::piecewise_constant_distribution<_RealType1>& __x); | |
8e79468d BK |
5643 | |
5644 | private: | |
7b93bdde UD |
5645 | template<typename _ForwardIterator, |
5646 | typename _UniformRandomNumberGenerator> | |
5647 | void | |
5648 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
5649 | _UniformRandomNumberGenerator& __urng, | |
5650 | const param_type& __p); | |
5651 | ||
8e79468d BK |
5652 | param_type _M_param; |
5653 | }; | |
5654 | ||
fc05e1ba PC |
5655 | /** |
5656 | * @brief Return true if two piecewise constant distributions have | |
5657 | * different parameters. | |
5658 | */ | |
5659 | template<typename _RealType> | |
5660 | inline bool | |
5661 | operator!=(const std::piecewise_constant_distribution<_RealType>& __d1, | |
5662 | const std::piecewise_constant_distribution<_RealType>& __d2) | |
5663 | { return !(__d1 == __d2); } | |
5664 | ||
8e79468d BK |
5665 | |
5666 | /** | |
5667 | * @brief A piecewise_linear_distribution random number distribution. | |
5668 | * | |
5669 | * The formula for the piecewise linear probability mass function is | |
5670 | * | |
5671 | */ | |
5672 | template<typename _RealType = double> | |
5673 | class piecewise_linear_distribution | |
5674 | { | |
77e3c516 | 5675 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 5676 | "result_type must be a floating point type"); |
77e3c516 | 5677 | |
8e79468d BK |
5678 | public: |
5679 | /** The type of the range of the distribution. */ | |
5680 | typedef _RealType result_type; | |
12905f10 | 5681 | |
8e79468d BK |
5682 | /** Parameter type. */ |
5683 | struct param_type | |
5684 | { | |
5685 | typedef piecewise_linear_distribution<_RealType> distribution_type; | |
5686 | friend class piecewise_linear_distribution<_RealType>; | |
5687 | ||
f8dd9e0d | 5688 | param_type() |
879b9073 PC |
5689 | : _M_int(), _M_den(), _M_cp(), _M_m() |
5690 | { } | |
8e79468d BK |
5691 | |
5692 | template<typename _InputIteratorB, typename _InputIteratorW> | |
5693 | param_type(_InputIteratorB __bfirst, | |
5694 | _InputIteratorB __bend, | |
5695 | _InputIteratorW __wbegin); | |
5696 | ||
5697 | template<typename _Func> | |
f8dd9e0d | 5698 | param_type(initializer_list<_RealType> __bl, _Func __fw); |
8e79468d BK |
5699 | |
5700 | template<typename _Func> | |
5701 | param_type(size_t __nw, _RealType __xmin, _RealType __xmax, | |
5702 | _Func __fw); | |
5703 | ||
ce9555cb PC |
5704 | // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/ |
5705 | param_type(const param_type&) = default; | |
5706 | param_type& operator=(const param_type&) = default; | |
5707 | ||
8e79468d BK |
5708 | std::vector<_RealType> |
5709 | intervals() const | |
879b9073 PC |
5710 | { |
5711 | if (_M_int.empty()) | |
5712 | { | |
5713 | std::vector<_RealType> __tmp(2); | |
5714 | __tmp[1] = _RealType(1); | |
5715 | return __tmp; | |
5716 | } | |
5717 | else | |
5718 | return _M_int; | |
5719 | } | |
8e79468d BK |
5720 | |
5721 | std::vector<double> | |
5722 | densities() const | |
879b9073 | 5723 | { return _M_den.empty() ? std::vector<double>(2, 1.0) : _M_den; } |
8e79468d | 5724 | |
fc05e1ba PC |
5725 | friend bool |
5726 | operator==(const param_type& __p1, const param_type& __p2) | |
12905f10 JW |
5727 | { return __p1._M_int == __p2._M_int && __p1._M_den == __p2._M_den; } |
5728 | ||
5729 | friend bool | |
5730 | operator!=(const param_type& __p1, const param_type& __p2) | |
5731 | { return !(__p1 == __p2); } | |
fc05e1ba | 5732 | |
8e79468d BK |
5733 | private: |
5734 | void | |
5735 | _M_initialize(); | |
5736 | ||
5737 | std::vector<_RealType> _M_int; | |
5738 | std::vector<double> _M_den; | |
5739 | std::vector<double> _M_cp; | |
5740 | std::vector<double> _M_m; | |
5741 | }; | |
5742 | ||
5743 | explicit | |
5744 | piecewise_linear_distribution() | |
5745 | : _M_param() | |
5746 | { } | |
5747 | ||
5748 | template<typename _InputIteratorB, typename _InputIteratorW> | |
5749 | piecewise_linear_distribution(_InputIteratorB __bfirst, | |
5750 | _InputIteratorB __bend, | |
5751 | _InputIteratorW __wbegin) | |
5752 | : _M_param(__bfirst, __bend, __wbegin) | |
5753 | { } | |
5754 | ||
5755 | template<typename _Func> | |
f8dd9e0d | 5756 | piecewise_linear_distribution(initializer_list<_RealType> __bl, |
8e79468d | 5757 | _Func __fw) |
f8dd9e0d | 5758 | : _M_param(__bl, __fw) |
8e79468d BK |
5759 | { } |
5760 | ||
5761 | template<typename _Func> | |
5762 | piecewise_linear_distribution(size_t __nw, | |
5763 | _RealType __xmin, _RealType __xmax, | |
5764 | _Func __fw) | |
5765 | : _M_param(__nw, __xmin, __xmax, __fw) | |
5766 | { } | |
5767 | ||
5768 | explicit | |
5769 | piecewise_linear_distribution(const param_type& __p) | |
5770 | : _M_param(__p) | |
5771 | { } | |
5772 | ||
5773 | /** | |
5774 | * Resets the distribution state. | |
5775 | */ | |
5776 | void | |
5777 | reset() | |
5778 | { } | |
5779 | ||
5780 | /** | |
5781 | * @brief Return the intervals of the distribution. | |
5782 | */ | |
5783 | std::vector<_RealType> | |
5784 | intervals() const | |
879b9073 PC |
5785 | { |
5786 | if (_M_param._M_int.empty()) | |
5787 | { | |
5788 | std::vector<_RealType> __tmp(2); | |
5789 | __tmp[1] = _RealType(1); | |
5790 | return __tmp; | |
5791 | } | |
5792 | else | |
5793 | return _M_param._M_int; | |
5794 | } | |
8e79468d BK |
5795 | |
5796 | /** | |
5797 | * @brief Return a vector of the probability densities of the | |
5798 | * distribution. | |
5799 | */ | |
5800 | std::vector<double> | |
5801 | densities() const | |
879b9073 PC |
5802 | { |
5803 | return _M_param._M_den.empty() | |
5804 | ? std::vector<double>(2, 1.0) : _M_param._M_den; | |
5805 | } | |
8e79468d BK |
5806 | |
5807 | /** | |
5808 | * @brief Returns the parameter set of the distribution. | |
5809 | */ | |
5810 | param_type | |
5811 | param() const | |
5812 | { return _M_param; } | |
5813 | ||
5814 | /** | |
5815 | * @brief Sets the parameter set of the distribution. | |
5816 | * @param __param The new parameter set of the distribution. | |
5817 | */ | |
5818 | void | |
5819 | param(const param_type& __param) | |
5820 | { _M_param = __param; } | |
5821 | ||
5822 | /** | |
5823 | * @brief Returns the greatest lower bound value of the distribution. | |
5824 | */ | |
5825 | result_type | |
5826 | min() const | |
879b9073 PC |
5827 | { |
5828 | return _M_param._M_int.empty() | |
5829 | ? result_type(0) : _M_param._M_int.front(); | |
5830 | } | |
8e79468d BK |
5831 | |
5832 | /** | |
5833 | * @brief Returns the least upper bound value of the distribution. | |
5834 | */ | |
5835 | result_type | |
5836 | max() const | |
879b9073 PC |
5837 | { |
5838 | return _M_param._M_int.empty() | |
5839 | ? result_type(1) : _M_param._M_int.back(); | |
5840 | } | |
8e79468d | 5841 | |
247d8075 PC |
5842 | /** |
5843 | * @brief Generating functions. | |
5844 | */ | |
8e79468d BK |
5845 | template<typename _UniformRandomNumberGenerator> |
5846 | result_type | |
5847 | operator()(_UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 5848 | { return this->operator()(__urng, _M_param); } |
8e79468d BK |
5849 | |
5850 | template<typename _UniformRandomNumberGenerator> | |
5851 | result_type | |
5852 | operator()(_UniformRandomNumberGenerator& __urng, | |
5853 | const param_type& __p); | |
5854 | ||
7b93bdde UD |
5855 | template<typename _ForwardIterator, |
5856 | typename _UniformRandomNumberGenerator> | |
5857 | void | |
5858 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
5859 | _UniformRandomNumberGenerator& __urng) | |
5bcb3b4d | 5860 | { this->__generate(__f, __t, __urng, _M_param); } |
7b93bdde UD |
5861 | |
5862 | template<typename _ForwardIterator, | |
5863 | typename _UniformRandomNumberGenerator> | |
5864 | void | |
5865 | __generate(_ForwardIterator __f, _ForwardIterator __t, | |
5866 | _UniformRandomNumberGenerator& __urng, | |
5867 | const param_type& __p) | |
5868 | { this->__generate_impl(__f, __t, __urng, __p); } | |
5869 | ||
5870 | template<typename _UniformRandomNumberGenerator> | |
5871 | void | |
5872 | __generate(result_type* __f, result_type* __t, | |
5873 | _UniformRandomNumberGenerator& __urng, | |
5874 | const param_type& __p) | |
5875 | { this->__generate_impl(__f, __t, __urng, __p); } | |
5876 | ||
5bcb3b4d PC |
5877 | /** |
5878 | * @brief Return true if two piecewise linear distributions have the | |
5879 | * same parameters. | |
5880 | */ | |
5881 | friend bool | |
5882 | operator==(const piecewise_linear_distribution& __d1, | |
5883 | const piecewise_linear_distribution& __d2) | |
5884 | { return __d1._M_param == __d2._M_param; } | |
5885 | ||
8e79468d BK |
5886 | /** |
5887 | * @brief Inserts a %piecewise_linear_distribution random number | |
5888 | * distribution @p __x into the output stream @p __os. | |
5889 | * | |
5890 | * @param __os An output stream. | |
5891 | * @param __x A %piecewise_linear_distribution random number | |
5892 | * distribution. | |
5893 | * | |
5894 | * @returns The output stream with the state of @p __x inserted or in | |
5895 | * an error state. | |
5896 | */ | |
5897 | template<typename _RealType1, typename _CharT, typename _Traits> | |
5898 | friend std::basic_ostream<_CharT, _Traits>& | |
93c66bc6 BK |
5899 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, |
5900 | const std::piecewise_linear_distribution<_RealType1>& __x); | |
8e79468d BK |
5901 | |
5902 | /** | |
5903 | * @brief Extracts a %piecewise_linear_distribution random number | |
5904 | * distribution @p __x from the input stream @p __is. | |
5905 | * | |
5906 | * @param __is An input stream. | |
5907 | * @param __x A %piecewise_linear_distribution random number | |
5908 | * generator engine. | |
5909 | * | |
5910 | * @returns The input stream with @p __x extracted or in an error | |
5911 | * state. | |
5912 | */ | |
5913 | template<typename _RealType1, typename _CharT, typename _Traits> | |
5914 | friend std::basic_istream<_CharT, _Traits>& | |
93c66bc6 BK |
5915 | operator>>(std::basic_istream<_CharT, _Traits>& __is, |
5916 | std::piecewise_linear_distribution<_RealType1>& __x); | |
8e79468d BK |
5917 | |
5918 | private: | |
7b93bdde UD |
5919 | template<typename _ForwardIterator, |
5920 | typename _UniformRandomNumberGenerator> | |
5921 | void | |
5922 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
5923 | _UniformRandomNumberGenerator& __urng, | |
5924 | const param_type& __p); | |
5925 | ||
8e79468d BK |
5926 | param_type _M_param; |
5927 | }; | |
5928 | ||
fc05e1ba PC |
5929 | /** |
5930 | * @brief Return true if two piecewise linear distributions have | |
5931 | * different parameters. | |
5932 | */ | |
5933 | template<typename _RealType> | |
5934 | inline bool | |
5935 | operator!=(const std::piecewise_linear_distribution<_RealType>& __d1, | |
5936 | const std::piecewise_linear_distribution<_RealType>& __d2) | |
5937 | { return !(__d1 == __d2); } | |
5938 | ||
8e79468d | 5939 | |
037181bc | 5940 | /* @} */ // group random_distributions_poisson |
8e79468d | 5941 | |
037181bc | 5942 | /* @} */ // group random_distributions |
8e79468d BK |
5943 | |
5944 | /** | |
037181bc BK |
5945 | * @addtogroup random_utilities Random Number Utilities |
5946 | * @ingroup random | |
8e79468d BK |
5947 | * @{ |
5948 | */ | |
5949 | ||
5950 | /** | |
5951 | * @brief The seed_seq class generates sequences of seeds for random | |
5952 | * number generators. | |
5953 | */ | |
5954 | class seed_seq | |
5955 | { | |
8e79468d BK |
5956 | public: |
5957 | /** The type of the seed vales. */ | |
5958 | typedef uint_least32_t result_type; | |
5959 | ||
5960 | /** Default constructor. */ | |
9933eb86 | 5961 | seed_seq() noexcept |
8e79468d BK |
5962 | : _M_v() |
5963 | { } | |
5964 | ||
5965 | template<typename _IntType> | |
5966 | seed_seq(std::initializer_list<_IntType> il); | |
5967 | ||
5968 | template<typename _InputIterator> | |
5969 | seed_seq(_InputIterator __begin, _InputIterator __end); | |
5970 | ||
5971 | // generating functions | |
5972 | template<typename _RandomAccessIterator> | |
5973 | void | |
5974 | generate(_RandomAccessIterator __begin, _RandomAccessIterator __end); | |
5975 | ||
5976 | // property functions | |
9933eb86 | 5977 | size_t size() const noexcept |
8e79468d BK |
5978 | { return _M_v.size(); } |
5979 | ||
5980 | template<typename OutputIterator> | |
5981 | void | |
5982 | param(OutputIterator __dest) const | |
5983 | { std::copy(_M_v.begin(), _M_v.end(), __dest); } | |
5984 | ||
d747ee05 JW |
5985 | // no copy functions |
5986 | seed_seq(const seed_seq&) = delete; | |
5987 | seed_seq& operator=(const seed_seq&) = delete; | |
5988 | ||
8e79468d | 5989 | private: |
94986f6d | 5990 | std::vector<result_type> _M_v; |
8e79468d BK |
5991 | }; |
5992 | ||
037181bc | 5993 | /* @} */ // group random_utilities |
8e79468d | 5994 | |
037181bc | 5995 | /* @} */ // group random |
12ffa228 BK |
5996 | |
5997 | _GLIBCXX_END_NAMESPACE_VERSION | |
5998 | } // namespace std | |
8e79468d | 5999 | |
06f29237 | 6000 | #endif |