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