- tmp/tmpid53kr4w/{from.md → to.md} +706 -593
tmp/tmpid53kr4w/{from.md → to.md}
RENAMED
|
@@ -4,49 +4,222 @@ This subclause defines a facility for generating (pseudo-)random
|
|
| 4 |
numbers.
|
| 5 |
|
| 6 |
In addition to a few utilities, four categories of entities are
|
| 7 |
described: *uniform random bit generators*, *random number engines*,
|
| 8 |
*random number engine adaptors*, and *random number distributions*.
|
| 9 |
-
These categorizations are applicable to types that
|
| 10 |
corresponding requirements, to objects instantiated from such types, and
|
| 11 |
to templates producing such types when instantiated.
|
| 12 |
|
| 13 |
[*Note 1*: These entities are specified in such a way as to permit the
|
| 14 |
binding of any uniform random bit generator object `e` as the argument
|
| 15 |
to any random number distribution object `d`, thus producing a
|
| 16 |
zero-argument function object such as given by
|
| 17 |
`bind(d,e)`. — *end note*]
|
| 18 |
|
| 19 |
Each of the entities specified via this subclause has an associated
|
| 20 |
-
arithmetic type
|
| 21 |
-
|
| 22 |
entity is characterized:
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
If integer-valued, an entity may optionally be further characterized as
|
| 25 |
*signed* or *unsigned*, according to `numeric_limits<T>::is_signed`.
|
| 26 |
|
| 27 |
Unless otherwise specified, all descriptions of calculations in this
|
| 28 |
subclause use mathematical real numbers.
|
| 29 |
|
| 30 |
-
Throughout this subclause, the operators , , and denote the
|
| 31 |
-
conventional bitwise operations. Further:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
### Requirements <a id="rand.req">[[rand.req]]</a>
|
| 34 |
|
| 35 |
#### General requirements <a id="rand.req.genl">[[rand.req.genl]]</a>
|
| 36 |
|
| 37 |
Throughout this subclause [[rand]], the effect of instantiating a
|
| 38 |
template:
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
Throughout this subclause [[rand]], phrases of the form “`x` is an
|
| 41 |
iterator of a specific kind” shall be interpreted as equivalent to the
|
| 42 |
-
more formal requirement that “`x` is a value of a type
|
| 43 |
requirements of the specified iterator type”.
|
| 44 |
|
| 45 |
Throughout this subclause [[rand]], any constructor that can be called
|
| 46 |
-
with a single argument and that
|
| 47 |
-
|
| 48 |
|
| 49 |
#### Seed sequence requirements <a id="rand.req.seedseq">[[rand.req.seedseq]]</a>
|
| 50 |
|
| 51 |
A *seed sequence* is an object that consumes a sequence of
|
| 52 |
integer-valued data and produces a requested number of unsigned integer
|
|
@@ -55,61 +228,105 @@ values i, 0 ≤ i < 2³², based on the consumed data.
|
|
| 55 |
[*Note 1*: Such an object provides a mechanism to avoid replication of
|
| 56 |
streams of random variates. This can be useful, for example, in
|
| 57 |
applications requiring large numbers of random number
|
| 58 |
engines. — *end note*]
|
| 59 |
|
| 60 |
-
A class `S`
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
#### Uniform random bit generator requirements <a id="rand.req.urng">[[rand.req.urng]]</a>
|
| 67 |
|
| 68 |
A *uniform random bit generator* `g` of type `G` is a function object
|
| 69 |
returning unsigned integer values such that each value in the range of
|
| 70 |
possible results has (ideally) equal probability of being returned.
|
| 71 |
|
| 72 |
[*Note 1*: The degree to which `g`’s results approximate the ideal is
|
| 73 |
often determined statistically. — *end note*]
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
#### Random number engine requirements <a id="rand.req.eng">[[rand.req.eng]]</a>
|
| 84 |
|
| 85 |
A *random number engine* (commonly shortened to *engine*) `e` of type
|
| 86 |
`E` is a uniform random bit generator that additionally meets the
|
| 87 |
requirements (e.g., for seeding and for input/output) specified in this
|
| 88 |
-
|
| 89 |
|
| 90 |
At any given time, `e` has a state eᵢ for some integer i ≥ 0. Upon
|
| 91 |
construction, `e` has an initial state e₀. An engine’s state may be
|
| 92 |
established via a constructor, a `seed` function, assignment, or a
|
| 93 |
suitable `operator>>`.
|
| 94 |
|
| 95 |
`E`’s specification shall define:
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
[[
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
#### Random number engine adaptor requirements <a id="rand.req.adapt">[[rand.req.adapt]]</a>
|
| 113 |
|
| 114 |
A *random number engine adaptor* (commonly shortened to *adaptor*) `a`
|
| 115 |
of type `A` is a random number engine that takes values produced by some
|
|
@@ -164,11 +381,20 @@ void seed(result_type s);
|
|
| 164 |
template<class Sseq> void seed(Sseq& q);
|
| 165 |
```
|
| 166 |
|
| 167 |
*Effects:* With `b` as the base engine, invokes `b.seed(q)`.
|
| 168 |
|
| 169 |
-
`A` shall also
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
#### Random number distribution requirements <a id="rand.req.dist">[[rand.req.dist]]</a>
|
| 172 |
|
| 173 |
A *random number distribution* (commonly shortened to *distribution*)
|
| 174 |
`d` of type `D` is a function object returning values that are
|
|
@@ -183,22 +409,38 @@ distribution*. Such distribution parameters are identified in this
|
|
| 183 |
context by writing, for example, p(z | a,b) or P(zᵢ | a,b), to name
|
| 184 |
specific parameters, or by writing, for example, p(z |{`p`}) or
|
| 185 |
P(zᵢ |{`p`}), to denote a distribution’s parameters `p` taken as a
|
| 186 |
whole.
|
| 187 |
|
| 188 |
-
A class `D`
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
`
|
| 198 |
-
[[
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
The sequence of numbers produced by repeated invocations of `d(g)` shall
|
| 202 |
be independent of any invocation of `os << d` or of any `const` member
|
| 203 |
function of `D` between any of the invocations `d(g)`.
|
| 204 |
|
|
@@ -211,14 +453,14 @@ produce the same sequence of numbers as would repeated invocations of
|
|
| 211 |
It is unspecified whether `D::param_type` is declared as a (nested)
|
| 212 |
`class` or via a `typedef`. In this subclause [[rand]], declarations of
|
| 213 |
`D::param_type` are in the form of `typedef`s for convenience of
|
| 214 |
exposition only.
|
| 215 |
|
| 216 |
-
`P` shall
|
| 217 |
-
[[
|
| 218 |
-
[[
|
| 219 |
-
[[
|
| 220 |
|
| 221 |
For each of the constructors of `D` taking arguments corresponding to
|
| 222 |
parameters of the distribution, `P` shall have a corresponding
|
| 223 |
constructor subject to the same requirements and taking arguments
|
| 224 |
identical in number, type, and default values. Moreover, for each of the
|
|
@@ -230,178 +472,41 @@ the identical name, type, and semantics.
|
|
| 230 |
|
| 231 |
``` cpp
|
| 232 |
using distribution_type = D;
|
| 233 |
```
|
| 234 |
|
| 235 |
-
### Header `<random>` synopsis <a id="rand.synopsis">[[rand.synopsis]]</a>
|
| 236 |
-
|
| 237 |
-
``` cpp
|
| 238 |
-
#include <initializer_list>
|
| 239 |
-
|
| 240 |
-
namespace std {
|
| 241 |
-
// [rand.eng.lcong], class template linear_congruential_engine
|
| 242 |
-
template<class UIntType, UIntType a, UIntType c, UIntType m>
|
| 243 |
-
class linear_congruential_engine;
|
| 244 |
-
|
| 245 |
-
// [rand.eng.mers], class template mersenne_twister_engine
|
| 246 |
-
template<class UIntType, size_t w, size_t n, size_t m, size_t r,
|
| 247 |
-
UIntType a, size_t u, UIntType d, size_t s,
|
| 248 |
-
UIntType b, size_t t,
|
| 249 |
-
UIntType c, size_t l, UIntType f>
|
| 250 |
-
class mersenne_twister_engine;
|
| 251 |
-
|
| 252 |
-
// [rand.eng.sub], class template subtract_with_carry_engine
|
| 253 |
-
template<class UIntType, size_t w, size_t s, size_t r>
|
| 254 |
-
class subtract_with_carry_engine;
|
| 255 |
-
|
| 256 |
-
// [rand.adapt.disc], class template discard_block_engine
|
| 257 |
-
template<class Engine, size_t p, size_t r>
|
| 258 |
-
class discard_block_engine;
|
| 259 |
-
|
| 260 |
-
// [rand.adapt.ibits], class template independent_bits_engine
|
| 261 |
-
template<class Engine, size_t w, class UIntType>
|
| 262 |
-
class independent_bits_engine;
|
| 263 |
-
|
| 264 |
-
// [rand.adapt.shuf], class template shuffle_order_engine
|
| 265 |
-
template<class Engine, size_t k>
|
| 266 |
-
class shuffle_order_engine;
|
| 267 |
-
|
| 268 |
-
// [rand.predef], engines and engine adaptors with predefined parameters
|
| 269 |
-
using minstd_rand0 = see below;
|
| 270 |
-
using minstd_rand = see below;
|
| 271 |
-
using mt19937 = see below;
|
| 272 |
-
using mt19937_64 = see below;
|
| 273 |
-
using ranlux24_base = see below;
|
| 274 |
-
using ranlux48_base = see below;
|
| 275 |
-
using ranlux24 = see below;
|
| 276 |
-
using ranlux48 = see below;
|
| 277 |
-
using knuth_b = see below;
|
| 278 |
-
|
| 279 |
-
using default_random_engine = see below;
|
| 280 |
-
|
| 281 |
-
// [rand.device], class random_device
|
| 282 |
-
class random_device;
|
| 283 |
-
|
| 284 |
-
// [rand.util.seedseq], class seed_seq
|
| 285 |
-
class seed_seq;
|
| 286 |
-
|
| 287 |
-
// [rand.util.canonical], function template generate_canonical
|
| 288 |
-
template<class RealType, size_t bits, class URBG>
|
| 289 |
-
RealType generate_canonical(URBG& g);
|
| 290 |
-
|
| 291 |
-
// [rand.dist.uni.int], class template uniform_int_distribution
|
| 292 |
-
template<class IntType = int>
|
| 293 |
-
class uniform_int_distribution;
|
| 294 |
-
|
| 295 |
-
// [rand.dist.uni.real], class template uniform_real_distribution
|
| 296 |
-
template<class RealType = double>
|
| 297 |
-
class uniform_real_distribution;
|
| 298 |
-
|
| 299 |
-
// [rand.dist.bern.bernoulli], class bernoulli_distribution
|
| 300 |
-
class bernoulli_distribution;
|
| 301 |
-
|
| 302 |
-
// [rand.dist.bern.bin], class template binomial_distribution
|
| 303 |
-
template<class IntType = int>
|
| 304 |
-
class binomial_distribution;
|
| 305 |
-
|
| 306 |
-
// [rand.dist.bern.geo], class template geometric_distribution
|
| 307 |
-
template<class IntType = int>
|
| 308 |
-
class geometric_distribution;
|
| 309 |
-
|
| 310 |
-
// [rand.dist.bern.negbin], class template negative_binomial_distribution
|
| 311 |
-
template<class IntType = int>
|
| 312 |
-
class negative_binomial_distribution;
|
| 313 |
-
|
| 314 |
-
// [rand.dist.pois.poisson], class template poisson_distribution
|
| 315 |
-
template<class IntType = int>
|
| 316 |
-
class poisson_distribution;
|
| 317 |
-
|
| 318 |
-
// [rand.dist.pois.exp], class template exponential_distribution
|
| 319 |
-
template<class RealType = double>
|
| 320 |
-
class exponential_distribution;
|
| 321 |
-
|
| 322 |
-
// [rand.dist.pois.gamma], class template gamma_distribution
|
| 323 |
-
template<class RealType = double>
|
| 324 |
-
class gamma_distribution;
|
| 325 |
-
|
| 326 |
-
// [rand.dist.pois.weibull], class template weibull_distribution
|
| 327 |
-
template<class RealType = double>
|
| 328 |
-
class weibull_distribution;
|
| 329 |
-
|
| 330 |
-
// [rand.dist.pois.extreme], class template extreme_value_distribution
|
| 331 |
-
template<class RealType = double>
|
| 332 |
-
class extreme_value_distribution;
|
| 333 |
-
|
| 334 |
-
// [rand.dist.norm.normal], class template normal_distribution
|
| 335 |
-
template<class RealType = double>
|
| 336 |
-
class normal_distribution;
|
| 337 |
-
|
| 338 |
-
// [rand.dist.norm.lognormal], class template lognormal_distribution
|
| 339 |
-
template<class RealType = double>
|
| 340 |
-
class lognormal_distribution;
|
| 341 |
-
|
| 342 |
-
// [rand.dist.norm.chisq], class template chi_squared_distribution
|
| 343 |
-
template<class RealType = double>
|
| 344 |
-
class chi_squared_distribution;
|
| 345 |
-
|
| 346 |
-
// [rand.dist.norm.cauchy], class template cauchy_distribution
|
| 347 |
-
template<class RealType = double>
|
| 348 |
-
class cauchy_distribution;
|
| 349 |
-
|
| 350 |
-
// [rand.dist.norm.f], class template fisher_f_distribution
|
| 351 |
-
template<class RealType = double>
|
| 352 |
-
class fisher_f_distribution;
|
| 353 |
-
|
| 354 |
-
// [rand.dist.norm.t], class template student_t_distribution
|
| 355 |
-
template<class RealType = double>
|
| 356 |
-
class student_t_distribution;
|
| 357 |
-
|
| 358 |
-
// [rand.dist.samp.discrete], class template discrete_distribution
|
| 359 |
-
template<class IntType = int>
|
| 360 |
-
class discrete_distribution;
|
| 361 |
-
|
| 362 |
-
// [rand.dist.samp.pconst], class template piecewise_constant_distribution
|
| 363 |
-
template<class RealType = double>
|
| 364 |
-
class piecewise_constant_distribution;
|
| 365 |
-
|
| 366 |
-
// [rand.dist.samp.plinear], class template piecewise_linear_distribution
|
| 367 |
-
template<class RealType = double>
|
| 368 |
-
class piecewise_linear_distribution;
|
| 369 |
-
}
|
| 370 |
-
```
|
| 371 |
-
|
| 372 |
### Random number engine class templates <a id="rand.eng">[[rand.eng]]</a>
|
| 373 |
|
| 374 |
-
Each type instantiated from a class template specified in this
|
| 375 |
-
[[rand.eng]]
|
| 376 |
-
[[rand.req.eng]]
|
| 377 |
|
| 378 |
Except where specified otherwise, the complexity of each function
|
| 379 |
-
specified in this
|
| 380 |
|
| 381 |
-
Except where specified otherwise, no function described in this
|
| 382 |
-
[[rand.eng]] throws an exception.
|
| 383 |
|
| 384 |
-
Every function described in this
|
| 385 |
function parameter `q` of type `Sseq&` for a template type parameter
|
| 386 |
named `Sseq` that is different from type `seed_seq` throws what and when
|
| 387 |
the invocation of `q.generate` throws.
|
| 388 |
|
| 389 |
-
Descriptions are provided in this
|
| 390 |
-
operations that are not described in [[rand.req.eng]] or for
|
| 391 |
-
where there is additional semantic information. In
|
| 392 |
-
declarations for copy constructors, for copy assignment
|
| 393 |
-
streaming operators, and for equality and inequality
|
| 394 |
-
shown in the synopses.
|
| 395 |
|
| 396 |
-
Each template specified in this
|
| 397 |
more relationships, involving the value(s) of its non-type template
|
| 398 |
parameter(s), to hold. A program instantiating any of these templates is
|
| 399 |
ill-formed if any such required relationship fails to hold.
|
| 400 |
|
| 401 |
For every random number engine and for every random number engine
|
| 402 |
-
adaptor `X` defined in this subclause
|
| 403 |
[[rand.adapt]]:
|
| 404 |
|
| 405 |
- if the constructor
|
| 406 |
``` cpp
|
| 407 |
template<class Sseq> explicit X(Sseq& q);
|
|
@@ -445,11 +550,12 @@ template<class UIntType, UIntType a, UIntType c, UIntType m>
|
|
| 445 |
static constexpr result_type min() { return c == 0u ? 1u: 0u; }
|
| 446 |
static constexpr result_type max() { return m - 1u; }
|
| 447 |
static constexpr result_type default_seed = 1u;
|
| 448 |
|
| 449 |
// constructors and seeding functions
|
| 450 |
-
|
|
|
|
| 451 |
template<class Sseq> explicit linear_congruential_engine(Sseq& q);
|
| 452 |
void seed(result_type s = default_seed);
|
| 453 |
template<class Sseq> void seed(Sseq& q);
|
| 454 |
|
| 455 |
// generating functions
|
|
@@ -457,41 +563,38 @@ template<class UIntType, UIntType a, UIntType c, UIntType m>
|
|
| 457 |
void discard(unsigned long long z);
|
| 458 |
};
|
| 459 |
```
|
| 460 |
|
| 461 |
If the template parameter `m` is 0, the modulus m used throughout this
|
| 462 |
-
|
| 463 |
-
1.
|
| 464 |
|
| 465 |
[*Note 1*: m need not be representable as a value of type
|
| 466 |
`result_type`. — *end note*]
|
| 467 |
|
| 468 |
If the template parameter `m` is not 0, the following relations shall
|
| 469 |
hold: `a < m` and `c < m`.
|
| 470 |
|
| 471 |
The textual representation consists of the value of xᵢ.
|
| 472 |
|
| 473 |
``` cpp
|
| 474 |
-
explicit linear_congruential_engine(result_type s
|
| 475 |
```
|
| 476 |
|
| 477 |
-
*Effects:*
|
| 478 |
-
|
| 479 |
-
otherwise sets the engine’s state to `s` mod m.
|
| 480 |
|
| 481 |
``` cpp
|
| 482 |
template<class Sseq> explicit linear_congruential_engine(Sseq& q);
|
| 483 |
```
|
| 484 |
|
| 485 |
-
*Effects:*
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
c mod m is 0 and S is 0, sets the engine’s state to 1, else sets the
|
| 492 |
-
engine’s state to S.
|
| 493 |
|
| 494 |
#### Class template `mersenne_twister_engine` <a id="rand.eng.mers">[[rand.eng.mers]]</a>
|
| 495 |
|
| 496 |
A `mersenne_twister_engine` random number engine[^2] produces unsigned
|
| 497 |
integer random numbers in the closed interval [0,2ʷ-1]. The state xᵢ of
|
|
@@ -501,21 +604,31 @@ applied to X are to be taken modulo n.
|
|
| 501 |
|
| 502 |
The transition algorithm employs a twisted generalized feedback shift
|
| 503 |
register defined by shift values n and m, a twist value r, and a
|
| 504 |
conditional xor-mask a. To improve the uniformity of the result, the
|
| 505 |
bits of the raw shift register are additionally *tempered* (i.e.,
|
| 506 |
-
scrambled) according to a bit-scrambling matrix defined by values
|
| 507 |
-
|
| 508 |
|
| 509 |
The state transition is performed as follows:
|
| 510 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
The sequence X is initialized with the help of an initialization
|
| 512 |
multiplier f.
|
| 513 |
|
| 514 |
The generation algorithm determines the unsigned integer values
|
| 515 |
z₁, z₂, z₃, z₄ as follows, then delivers z₄ as its result:
|
| 516 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
``` cpp
|
| 518 |
template<class UIntType, size_t w, size_t n, size_t m, size_t r,
|
| 519 |
UIntType a, size_t u, UIntType d, size_t s,
|
| 520 |
UIntType b, size_t t,
|
| 521 |
UIntType c, size_t l, UIntType f>
|
|
@@ -541,11 +654,12 @@ template<class UIntType, size_t w, size_t n, size_t m, size_t r,
|
|
| 541 |
static constexpr result_type min() { return 0; }
|
| 542 |
static constexpr result_type max() { return 2^w - 1; }
|
| 543 |
static constexpr result_type default_seed = 5489u;
|
| 544 |
|
| 545 |
// constructors and seeding functions
|
| 546 |
-
|
|
|
|
| 547 |
template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
|
| 548 |
void seed(result_type value = default_seed);
|
| 549 |
template<class Sseq> void seed(Sseq& q);
|
| 550 |
|
| 551 |
// generating functions
|
|
@@ -559,18 +673,18 @@ The following relations shall hold: `0 < m`, `m <= n`, `2u < w`,
|
|
| 559 |
`w <= numeric_limits<UIntType>::digits`, `a <= (1u<<w) - 1u`,
|
| 560 |
`b <= (1u<<w) - 1u`, `c <= (1u<<w) - 1u`, `d <= (1u<<w) - 1u`, and
|
| 561 |
`f <= (1u<<w) - 1u`.
|
| 562 |
|
| 563 |
The textual representation of xᵢ consists of the values of
|
| 564 |
-
|
| 565 |
|
| 566 |
``` cpp
|
| 567 |
-
explicit mersenne_twister_engine(result_type value
|
| 568 |
```
|
| 569 |
|
| 570 |
-
*Effects:*
|
| 571 |
-
|
| 572 |
\bigl[f \cdot
|
| 573 |
\bigl(X_{i-1} \xor \bigl(X_{i-1} \rightshift (w-2)\bigr)
|
| 574 |
\bigr)
|
| 575 |
+ i \bmod n
|
| 576 |
\bigr] \bmod 2^w
|
|
@@ -580,14 +694,13 @@ explicit mersenne_twister_engine(result_type value = default_seed);
|
|
| 580 |
|
| 581 |
``` cpp
|
| 582 |
template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
|
| 583 |
```
|
| 584 |
|
| 585 |
-
*Effects:*
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
sets Xᵢ to
|
| 589 |
$\left(\sum_{j=0}^{k-1}a_{k(i+n)+j} \cdot 2^{32j} \right) \bmod 2^w$.
|
| 590 |
Finally, if the most significant w-r bits of X₋ₙ are zero, and if each
|
| 591 |
of the other resulting Xᵢ is 0, changes X₋ₙ to 2ʷ⁻¹.
|
| 592 |
|
| 593 |
#### Class template `subtract_with_carry_engine` <a id="rand.eng.sub">[[rand.eng.sub]]</a>
|
|
@@ -601,10 +714,13 @@ all subscripts applied to X are to be taken modulo r. The state xᵢ
|
|
| 601 |
additionally consists of an integer c (known as the *carry*) whose value
|
| 602 |
is either 0 or 1.
|
| 603 |
|
| 604 |
The state transition is performed as follows:
|
| 605 |
|
|
|
|
|
|
|
|
|
|
| 606 |
[*Note 1*: This algorithm corresponds to a modular linear function of
|
| 607 |
the form TA(xᵢ) = (a ⋅ xᵢ) mod b, where b is of the form mʳ - mˢ + 1
|
| 608 |
and a = b - (b - 1) / m. — *end note*]
|
| 609 |
|
| 610 |
The generation algorithm is given by GA(xᵢ) = y, where y is the value
|
|
@@ -624,11 +740,12 @@ template<class UIntType, size_t w, size_t s, size_t r>
|
|
| 624 |
static constexpr result_type min() { return 0; }
|
| 625 |
static constexpr result_type max() { return m - 1; }
|
| 626 |
static constexpr result_type default_seed = 19780503u;
|
| 627 |
|
| 628 |
// constructors and seeding functions
|
| 629 |
-
|
|
|
|
| 630 |
template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
|
| 631 |
void seed(result_type value = default_seed);
|
| 632 |
template<class Sseq> void seed(Sseq& q);
|
| 633 |
|
| 634 |
// generating functions
|
|
@@ -642,16 +759,15 @@ The following relations shall hold: `0u < s`, `s < r`, `0 < w`, and
|
|
| 642 |
|
| 643 |
The textual representation consists of the values of Xᵢ₋ᵣ, …, Xᵢ₋₁, in
|
| 644 |
that order, followed by c.
|
| 645 |
|
| 646 |
``` cpp
|
| 647 |
-
explicit subtract_with_carry_engine(result_type value
|
| 648 |
```
|
| 649 |
|
| 650 |
-
*Effects:*
|
| 651 |
-
|
| 652 |
-
0, sets c to 1; otherwise sets c to 0.
|
| 653 |
|
| 654 |
To set the values Xₖ, first construct `e`, a
|
| 655 |
`linear_congruential_engine` object, as if by the following definition:
|
| 656 |
|
| 657 |
``` cpp
|
|
@@ -667,45 +783,44 @@ $\left( \sum_{j=0}^{n-1} z_j \cdot 2^{32j}\right) \bmod m$.
|
|
| 667 |
|
| 668 |
``` cpp
|
| 669 |
template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
|
| 670 |
```
|
| 671 |
|
| 672 |
-
*Effects:*
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
sets Xᵢ to
|
| 676 |
$\left(\sum_{j=0}^{k-1}a_{k(i+r)+j} \cdot 2^{32j} \right) \bmod m$. If
|
| 677 |
X₋₁ is then 0, sets c to 1; otherwise sets c to 0.
|
| 678 |
|
| 679 |
### Random number engine adaptor class templates <a id="rand.adapt">[[rand.adapt]]</a>
|
| 680 |
|
| 681 |
#### In general <a id="rand.adapt.general">[[rand.adapt.general]]</a>
|
| 682 |
|
| 683 |
-
Each type instantiated from a class template specified in this
|
| 684 |
-
[[rand.adapt]]
|
| 685 |
-
adaptor
|
| 686 |
|
| 687 |
Except where specified otherwise, the complexity of each function
|
| 688 |
-
specified in this
|
| 689 |
|
| 690 |
-
Except where specified otherwise, no function described in this
|
| 691 |
-
[[rand.adapt]] throws an exception.
|
| 692 |
|
| 693 |
-
Every function described in this
|
| 694 |
function parameter `q` of type `Sseq&` for a template type parameter
|
| 695 |
named `Sseq` that is different from type `seed_seq` throws what and when
|
| 696 |
the invocation of `q.generate` throws.
|
| 697 |
|
| 698 |
-
Descriptions are provided in this
|
| 699 |
-
adaptor operations that are not described in
|
| 700 |
-
or for operations where there is additional semantic
|
| 701 |
-
particular, declarations for copy constructors, for copy
|
| 702 |
-
operators, for streaming operators, and for equality and
|
| 703 |
-
operators are not shown in the synopses.
|
| 704 |
|
| 705 |
-
Each template specified in this
|
| 706 |
-
more relationships, involving the value(s) of its non-type template
|
| 707 |
parameter(s), to hold. A program instantiating any of these templates is
|
| 708 |
ill-formed if any such required relationship fails to hold.
|
| 709 |
|
| 710 |
#### Class template `discard_block_engine` <a id="rand.adapt.disc">[[rand.adapt.disc]]</a>
|
| 711 |
|
|
@@ -763,11 +878,11 @@ template<class Engine, size_t p, size_t r>
|
|
| 763 |
The following relations shall hold: `0 < r` and `r <= p`.
|
| 764 |
|
| 765 |
The textual representation consists of the textual representation of `e`
|
| 766 |
followed by the value of `n`.
|
| 767 |
|
| 768 |
-
In addition to its behavior pursuant to
|
| 769 |
each constructor that is not a copy constructor sets `n` to 0.
|
| 770 |
|
| 771 |
#### Class template `independent_bits_engine` <a id="rand.adapt.ibits">[[rand.adapt.ibits]]</a>
|
| 772 |
|
| 773 |
An `independent_bits_engine` random number engine adaptor combines
|
|
@@ -778,10 +893,17 @@ state eᵢ of its base engine `e`; the size of the state is the size of
|
|
| 778 |
e’s state.
|
| 779 |
|
| 780 |
The transition and generation algorithms are described in terms of the
|
| 781 |
following integral constants:
|
| 782 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 783 |
[*Note 1*: The relation w = n₀ w₀ + (n - n₀)(w₀ + 1) always
|
| 784 |
holds. — *end note*]
|
| 785 |
|
| 786 |
The transition algorithm is carried out by invoking `e()` as often as
|
| 787 |
needed to obtain n₀ values less than y₀ + `e.min()` and n - n₀ values
|
|
@@ -853,10 +975,15 @@ additional sequence V of k values also of the type delivered by `e`. The
|
|
| 853 |
size of the state is the size of e’s state plus k + 1.
|
| 854 |
|
| 855 |
The transition algorithm permutes the values produced by e. The state
|
| 856 |
transition is performed as follows:
|
| 857 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
The generation algorithm yields the last value of `Y` produced while
|
| 859 |
advancing `e`’s state as described above.
|
| 860 |
|
| 861 |
``` cpp
|
| 862 |
template<class Engine, size_t k>
|
|
@@ -897,99 +1024,96 @@ template<class Engine, size_t k>
|
|
| 897 |
The following relation shall hold: `0 < k`.
|
| 898 |
|
| 899 |
The textual representation consists of the textual representation of
|
| 900 |
`e`, followed by the `k` values of V, followed by the value of Y.
|
| 901 |
|
| 902 |
-
In addition to its behavior pursuant to
|
| 903 |
each constructor that is not a copy constructor initializes
|
| 904 |
`V[0]`, …, `V[k-1]` and Y, in that order, with values returned by
|
| 905 |
successive invocations of `e()`.
|
| 906 |
|
| 907 |
### Engines and engine adaptors with predefined parameters <a id="rand.predef">[[rand.predef]]</a>
|
| 908 |
|
| 909 |
``` cpp
|
| 910 |
using minstd_rand0 =
|
| 911 |
-
linear_congruential_engine<uint_fast32_t,
|
| 912 |
```
|
| 913 |
|
| 914 |
-
*Required behavior:* The
|
| 915 |
-
default-constructed object of type `minstd_rand0`
|
| 916 |
-
|
| 917 |
|
| 918 |
``` cpp
|
| 919 |
using minstd_rand =
|
| 920 |
-
linear_congruential_engine<uint_fast32_t,
|
| 921 |
```
|
| 922 |
|
| 923 |
-
*Required behavior:* The
|
| 924 |
-
default-constructed object of type `minstd_rand`
|
| 925 |
399268537.
|
| 926 |
|
| 927 |
``` cpp
|
| 928 |
using mt19937 =
|
| 929 |
-
mersenne_twister_engine<uint_fast32_t,
|
| 930 |
-
|
| 931 |
```
|
| 932 |
|
| 933 |
-
*Required behavior:* The
|
| 934 |
-
default-constructed object of type `mt19937`
|
| 935 |
4123659995.
|
| 936 |
|
| 937 |
``` cpp
|
| 938 |
using mt19937_64 =
|
| 939 |
-
mersenne_twister_engine<uint_fast64_t,
|
| 940 |
-
|
| 941 |
-
|
| 942 |
-
0x71d67fffeda60000,37,
|
| 943 |
-
0xfff7eee000000000,43,
|
| 944 |
-
6364136223846793005>;
|
| 945 |
```
|
| 946 |
|
| 947 |
-
*Required behavior:* The
|
| 948 |
-
default-constructed object of type `mt19937_64`
|
| 949 |
9981545732273789042.
|
| 950 |
|
| 951 |
``` cpp
|
| 952 |
using ranlux24_base =
|
| 953 |
subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>;
|
| 954 |
```
|
| 955 |
|
| 956 |
-
*Required behavior:* The
|
| 957 |
-
default-constructed object of type `ranlux24_base`
|
| 958 |
-
|
| 959 |
|
| 960 |
``` cpp
|
| 961 |
using ranlux48_base =
|
| 962 |
subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>;
|
| 963 |
```
|
| 964 |
|
| 965 |
-
*Required behavior:* The
|
| 966 |
-
default-constructed object of type `ranlux48_base`
|
| 967 |
-
|
| 968 |
|
| 969 |
``` cpp
|
| 970 |
using ranlux24 = discard_block_engine<ranlux24_base, 223, 23>;
|
| 971 |
```
|
| 972 |
|
| 973 |
-
*Required behavior:* The
|
| 974 |
-
default-constructed object of type `ranlux24`
|
| 975 |
9901578.
|
| 976 |
|
| 977 |
``` cpp
|
| 978 |
using ranlux48 = discard_block_engine<ranlux48_base, 389, 11>;
|
| 979 |
```
|
| 980 |
|
| 981 |
-
*Required behavior:* The
|
| 982 |
-
default-constructed object of type `ranlux48`
|
| 983 |
249142670248501.
|
| 984 |
|
| 985 |
``` cpp
|
| 986 |
using knuth_b = shuffle_order_engine<minstd_rand0,256>;
|
| 987 |
```
|
| 988 |
|
| 989 |
-
*Required behavior:* The
|
| 990 |
-
default-constructed object of type `knuth_b`
|
| 991 |
1112339016.
|
| 992 |
|
| 993 |
``` cpp
|
| 994 |
using default_random_engine = implementation-defined;
|
| 995 |
```
|
|
@@ -1022,11 +1146,12 @@ public:
|
|
| 1022 |
// generator characteristics
|
| 1023 |
static constexpr result_type min() { return numeric_limits<result_type>::min(); }
|
| 1024 |
static constexpr result_type max() { return numeric_limits<result_type>::max(); }
|
| 1025 |
|
| 1026 |
// constructors
|
| 1027 |
-
|
|
|
|
| 1028 |
|
| 1029 |
// generating functions
|
| 1030 |
result_type operator()();
|
| 1031 |
|
| 1032 |
// property functions
|
|
@@ -1037,16 +1162,15 @@ public:
|
|
| 1037 |
void operator=(const random_device&) = delete;
|
| 1038 |
};
|
| 1039 |
```
|
| 1040 |
|
| 1041 |
``` cpp
|
| 1042 |
-
explicit random_device(const string& token
|
| 1043 |
```
|
| 1044 |
|
| 1045 |
-
*
|
| 1046 |
-
|
| 1047 |
-
parameter are *implementation-defined*. [^3]
|
| 1048 |
|
| 1049 |
*Throws:* A value of an *implementation-defined* type derived from
|
| 1050 |
`exception` if the `random_device` could not be initialized.
|
| 1051 |
|
| 1052 |
``` cpp
|
|
@@ -1060,11 +1184,11 @@ returned by `operator()`, in the range `min()` to log₂( `max()`+1).
|
|
| 1060 |
``` cpp
|
| 1061 |
result_type operator()();
|
| 1062 |
```
|
| 1063 |
|
| 1064 |
*Returns:* A nondeterministic random value, uniformly distributed
|
| 1065 |
-
between `min()` and `max()`
|
| 1066 |
how these values are generated.
|
| 1067 |
|
| 1068 |
*Throws:* A value of an *implementation-defined* type derived from
|
| 1069 |
`exception` if a random number could not be obtained.
|
| 1070 |
|
|
@@ -1105,35 +1229,35 @@ private:
|
|
| 1105 |
|
| 1106 |
``` cpp
|
| 1107 |
seed_seq();
|
| 1108 |
```
|
| 1109 |
|
| 1110 |
-
*
|
| 1111 |
-
its member `v`.
|
| 1112 |
|
| 1113 |
*Throws:* Nothing.
|
| 1114 |
|
| 1115 |
``` cpp
|
| 1116 |
template<class T>
|
| 1117 |
seed_seq(initializer_list<T> il);
|
| 1118 |
```
|
| 1119 |
|
| 1120 |
-
*
|
| 1121 |
|
| 1122 |
*Effects:* Same as `seed_seq(il.begin(), il.end())`.
|
| 1123 |
|
| 1124 |
``` cpp
|
| 1125 |
template<class InputIterator>
|
| 1126 |
seed_seq(InputIterator begin, InputIterator end);
|
| 1127 |
```
|
| 1128 |
|
| 1129 |
-
*
|
| 1130 |
-
iterator (Table [[tab:iterator.input.requirements]]) type. Moreover,
|
| 1131 |
-
`iterator_traits<InputIterator>::value_type` shall denote an integer
|
| 1132 |
type.
|
| 1133 |
|
| 1134 |
-
*
|
|
|
|
|
|
|
|
|
|
| 1135 |
|
| 1136 |
``` cpp
|
| 1137 |
for (InputIterator s = begin; s != end; ++s)
|
| 1138 |
v.push_back((*s) mod 2³²);
|
| 1139 |
```
|
|
@@ -1141,21 +1265,63 @@ for( InputIterator s = begin; s != end; ++s)
|
|
| 1141 |
``` cpp
|
| 1142 |
template<class RandomAccessIterator>
|
| 1143 |
void generate(RandomAccessIterator begin, RandomAccessIterator end);
|
| 1144 |
```
|
| 1145 |
|
| 1146 |
-
*
|
| 1147 |
-
mutable random access iterator ([[random.access.iterators]]). Moreover,
|
| 1148 |
-
`iterator_traits<RandomAccessIterator>::value_type` shall denote an
|
| 1149 |
unsigned integer type capable of accommodating 32-bit quantities.
|
| 1150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1151 |
*Effects:* Does nothing if `begin == end`. Otherwise, with
|
| 1152 |
s = `v.size()` and n = `end` - `begin`, fills the supplied range
|
| 1153 |
[`begin`,`end`) according to the following algorithm in which each
|
| 1154 |
operation is to be carried out modulo 2³², each indexing operator
|
| 1155 |
applied to `begin` is to be taken modulo n, and T(x) is defined as
|
| 1156 |
-
$x \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1157 |
|
| 1158 |
*Throws:* What and when `RandomAccessIterator` operations of `begin` and
|
| 1159 |
`end` throw.
|
| 1160 |
|
| 1161 |
``` cpp
|
|
@@ -1170,13 +1336,15 @@ to `param()`.
|
|
| 1170 |
``` cpp
|
| 1171 |
template<class OutputIterator>
|
| 1172 |
void param(OutputIterator dest) const;
|
| 1173 |
```
|
| 1174 |
|
| 1175 |
-
*
|
| 1176 |
-
|
| 1177 |
-
|
|
|
|
|
|
|
| 1178 |
|
| 1179 |
*Effects:* Copies the sequence of prepared 32-bit units to the given
|
| 1180 |
destination, as if by executing the following statement:
|
| 1181 |
|
| 1182 |
``` cpp
|
|
@@ -1185,22 +1353,10 @@ copy(v.begin(), v.end(), dest);
|
|
| 1185 |
|
| 1186 |
*Throws:* What and when `OutputIterator` operations of `dest` throw.
|
| 1187 |
|
| 1188 |
#### Function template `generate_canonical` <a id="rand.util.canonical">[[rand.util.canonical]]</a>
|
| 1189 |
|
| 1190 |
-
Each function instantiated from the template described in this section
|
| 1191 |
-
[[rand.util.canonical]] maps the result of one or more invocations of a
|
| 1192 |
-
supplied uniform random bit generator `g` to one member of the specified
|
| 1193 |
-
`RealType` such that, if the values gᵢ produced by `g` are uniformly
|
| 1194 |
-
distributed, the instantiation’s results tⱼ, 0 ≤ tⱼ < 1, are distributed
|
| 1195 |
-
as uniformly as possible as specified below.
|
| 1196 |
-
|
| 1197 |
-
[*Note 1*: Obtaining a value in this way can be a useful step in the
|
| 1198 |
-
process of transforming a value generated by a uniform random bit
|
| 1199 |
-
generator into a value that can be delivered by a random number
|
| 1200 |
-
distribution. — *end note*]
|
| 1201 |
-
|
| 1202 |
``` cpp
|
| 1203 |
template<class RealType, size_t bits, class URBG>
|
| 1204 |
RealType generate_canonical(URBG& g);
|
| 1205 |
```
|
| 1206 |
|
|
@@ -1213,54 +1369,62 @@ respectively. Calculates a quantity
|
|
| 1213 |
$$S = \sum_{i=0}^{k-1} (g_i - \texttt{g.min()})
|
| 1214 |
\cdot R^i$$ using arithmetic of type `RealType`.
|
| 1215 |
|
| 1216 |
*Returns:* S / Rᵏ.
|
| 1217 |
|
|
|
|
|
|
|
| 1218 |
*Throws:* What and when `g` throws.
|
| 1219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1220 |
### Random number distribution class templates <a id="rand.dist">[[rand.dist]]</a>
|
| 1221 |
|
| 1222 |
#### In general <a id="rand.dist.general">[[rand.dist.general]]</a>
|
| 1223 |
|
| 1224 |
-
Each type instantiated from a class template specified in this
|
| 1225 |
-
[[rand.dist]]
|
| 1226 |
-
distribution
|
| 1227 |
|
| 1228 |
-
Descriptions are provided in this
|
| 1229 |
distribution operations that are not described in [[rand.req.dist]] or
|
| 1230 |
for operations where there is additional semantic information. In
|
| 1231 |
particular, declarations for copy constructors, for copy assignment
|
| 1232 |
operators, for streaming operators, and for equality and inequality
|
| 1233 |
operators are not shown in the synopses.
|
| 1234 |
|
| 1235 |
The algorithms for producing each of the specified distributions are
|
| 1236 |
*implementation-defined*.
|
| 1237 |
|
| 1238 |
The value of each probability density function p(z) and of each discrete
|
| 1239 |
-
probability function P(zᵢ) specified in this
|
| 1240 |
outside its stated domain.
|
| 1241 |
|
| 1242 |
#### Uniform distributions <a id="rand.dist.uni">[[rand.dist.uni]]</a>
|
| 1243 |
|
| 1244 |
##### Class template `uniform_int_distribution` <a id="rand.dist.uni.int">[[rand.dist.uni.int]]</a>
|
| 1245 |
|
| 1246 |
A `uniform_int_distribution` random number distribution produces random
|
| 1247 |
integers i, a ≤ i ≤ b, distributed according to the constant discrete
|
| 1248 |
-
probability function $$
|
| 1249 |
-
P(i\,|\,a,b) = 1 / (b - a + 1)
|
| 1250 |
-
\; \mbox{.}$$
|
| 1251 |
|
| 1252 |
``` cpp
|
| 1253 |
template<class IntType = int>
|
| 1254 |
class uniform_int_distribution {
|
| 1255 |
public:
|
| 1256 |
// types
|
| 1257 |
using result_type = IntType;
|
| 1258 |
using param_type = unspecified;
|
| 1259 |
|
| 1260 |
// constructors and reset functions
|
| 1261 |
-
|
|
|
|
| 1262 |
explicit uniform_int_distribution(const param_type& parm);
|
| 1263 |
void reset();
|
| 1264 |
|
| 1265 |
// generating functions
|
| 1266 |
template<class URBG>
|
|
@@ -1277,17 +1441,17 @@ template<class IntType = int>
|
|
| 1277 |
result_type max() const;
|
| 1278 |
};
|
| 1279 |
```
|
| 1280 |
|
| 1281 |
``` cpp
|
| 1282 |
-
explicit uniform_int_distribution(IntType a
|
| 1283 |
```
|
| 1284 |
|
| 1285 |
-
*
|
| 1286 |
|
| 1287 |
-
*
|
| 1288 |
-
|
| 1289 |
|
| 1290 |
``` cpp
|
| 1291 |
result_type a() const;
|
| 1292 |
```
|
| 1293 |
|
|
@@ -1303,13 +1467,11 @@ constructed.
|
|
| 1303 |
|
| 1304 |
##### Class template `uniform_real_distribution` <a id="rand.dist.uni.real">[[rand.dist.uni.real]]</a>
|
| 1305 |
|
| 1306 |
A `uniform_real_distribution` random number distribution produces random
|
| 1307 |
numbers x, a ≤ x < b, distributed according to the constant probability
|
| 1308 |
-
density function $$
|
| 1309 |
-
p(x\,|\,a,b) = 1 / (b - a)
|
| 1310 |
-
\; \mbox{.}$$
|
| 1311 |
|
| 1312 |
[*Note 1*: This implies that p(x | a,b) is undefined when
|
| 1313 |
`a == b`. — *end note*]
|
| 1314 |
|
| 1315 |
``` cpp
|
|
@@ -1319,11 +1481,12 @@ template<class RealType = double>
|
|
| 1319 |
// types
|
| 1320 |
using result_type = RealType;
|
| 1321 |
using param_type = unspecified;
|
| 1322 |
|
| 1323 |
// constructors and reset functions
|
| 1324 |
-
|
|
|
|
| 1325 |
explicit uniform_real_distribution(const param_type& parm);
|
| 1326 |
void reset();
|
| 1327 |
|
| 1328 |
// generating functions
|
| 1329 |
template<class URBG>
|
|
@@ -1340,17 +1503,18 @@ template<class RealType = double>
|
|
| 1340 |
result_type max() const;
|
| 1341 |
};
|
| 1342 |
```
|
| 1343 |
|
| 1344 |
``` cpp
|
| 1345 |
-
explicit uniform_real_distribution(RealType a
|
| 1346 |
```
|
| 1347 |
|
| 1348 |
-
*
|
|
|
|
| 1349 |
|
| 1350 |
-
*
|
| 1351 |
-
|
| 1352 |
|
| 1353 |
``` cpp
|
| 1354 |
result_type a() const;
|
| 1355 |
```
|
| 1356 |
|
|
@@ -1367,27 +1531,26 @@ constructed.
|
|
| 1367 |
#### Bernoulli distributions <a id="rand.dist.bern">[[rand.dist.bern]]</a>
|
| 1368 |
|
| 1369 |
##### Class `bernoulli_distribution` <a id="rand.dist.bern.bernoulli">[[rand.dist.bern.bernoulli]]</a>
|
| 1370 |
|
| 1371 |
A `bernoulli_distribution` random number distribution produces `bool`
|
| 1372 |
-
values b distributed according to the discrete probability function
|
| 1373 |
-
|
| 1374 |
-
|
| 1375 |
-
|
| 1376 |
-
|
| 1377 |
-
\end{array}\right.
|
| 1378 |
-
\; \mbox{.}$$
|
| 1379 |
|
| 1380 |
``` cpp
|
| 1381 |
class bernoulli_distribution {
|
| 1382 |
public:
|
| 1383 |
// types
|
| 1384 |
using result_type = bool;
|
| 1385 |
using param_type = unspecified;
|
| 1386 |
|
| 1387 |
// constructors and reset functions
|
| 1388 |
-
|
|
|
|
| 1389 |
explicit bernoulli_distribution(const param_type& parm);
|
| 1390 |
void reset();
|
| 1391 |
|
| 1392 |
// generating functions
|
| 1393 |
template<class URBG>
|
|
@@ -1403,17 +1566,16 @@ public:
|
|
| 1403 |
result_type max() const;
|
| 1404 |
};
|
| 1405 |
```
|
| 1406 |
|
| 1407 |
``` cpp
|
| 1408 |
-
explicit bernoulli_distribution(double p
|
| 1409 |
```
|
| 1410 |
|
| 1411 |
-
*
|
| 1412 |
|
| 1413 |
-
*
|
| 1414 |
-
to the parameter of the distribution.
|
| 1415 |
|
| 1416 |
``` cpp
|
| 1417 |
double p() const;
|
| 1418 |
```
|
| 1419 |
|
|
@@ -1422,25 +1584,23 @@ constructed.
|
|
| 1422 |
|
| 1423 |
##### Class template `binomial_distribution` <a id="rand.dist.bern.bin">[[rand.dist.bern.bin]]</a>
|
| 1424 |
|
| 1425 |
A `binomial_distribution` random number distribution produces integer
|
| 1426 |
values i ≥ 0 distributed according to the discrete probability function
|
| 1427 |
-
$$
|
| 1428 |
-
P(i\,|\,t,p)
|
| 1429 |
-
= \binom{t}{i} \cdot p^i \cdot (1-p)^{t-i}
|
| 1430 |
-
\; \mbox{.}$$
|
| 1431 |
|
| 1432 |
``` cpp
|
| 1433 |
template<class IntType = int>
|
| 1434 |
class binomial_distribution {
|
| 1435 |
public:
|
| 1436 |
// types
|
| 1437 |
using result_type = IntType;
|
| 1438 |
using param_type = unspecified;
|
| 1439 |
|
| 1440 |
// constructors and reset functions
|
| 1441 |
-
|
|
|
|
| 1442 |
explicit binomial_distribution(const param_type& parm);
|
| 1443 |
void reset();
|
| 1444 |
|
| 1445 |
// generating functions
|
| 1446 |
template<class URBG>
|
|
@@ -1457,17 +1617,17 @@ template<class IntType = int>
|
|
| 1457 |
result_type max() const;
|
| 1458 |
};
|
| 1459 |
```
|
| 1460 |
|
| 1461 |
``` cpp
|
| 1462 |
-
explicit binomial_distribution(IntType t
|
| 1463 |
```
|
| 1464 |
|
| 1465 |
-
*
|
| 1466 |
|
| 1467 |
-
*
|
| 1468 |
-
|
| 1469 |
|
| 1470 |
``` cpp
|
| 1471 |
IntType t() const;
|
| 1472 |
```
|
| 1473 |
|
|
@@ -1483,25 +1643,23 @@ constructed.
|
|
| 1483 |
|
| 1484 |
##### Class template `geometric_distribution` <a id="rand.dist.bern.geo">[[rand.dist.bern.geo]]</a>
|
| 1485 |
|
| 1486 |
A `geometric_distribution` random number distribution produces integer
|
| 1487 |
values i ≥ 0 distributed according to the discrete probability function
|
| 1488 |
-
$$
|
| 1489 |
-
P(i\,|\,p)
|
| 1490 |
-
= p \cdot (1-p)^{i}
|
| 1491 |
-
\; \mbox{.}$$
|
| 1492 |
|
| 1493 |
``` cpp
|
| 1494 |
template<class IntType = int>
|
| 1495 |
class geometric_distribution {
|
| 1496 |
public:
|
| 1497 |
// types
|
| 1498 |
using result_type = IntType;
|
| 1499 |
using param_type = unspecified;
|
| 1500 |
|
| 1501 |
// constructors and reset functions
|
| 1502 |
-
|
|
|
|
| 1503 |
explicit geometric_distribution(const param_type& parm);
|
| 1504 |
void reset();
|
| 1505 |
|
| 1506 |
// generating functions
|
| 1507 |
template<class URBG>
|
|
@@ -1517,17 +1675,16 @@ template<class IntType = int>
|
|
| 1517 |
result_type max() const;
|
| 1518 |
};
|
| 1519 |
```
|
| 1520 |
|
| 1521 |
``` cpp
|
| 1522 |
-
explicit geometric_distribution(double p
|
| 1523 |
```
|
| 1524 |
|
| 1525 |
-
*
|
| 1526 |
|
| 1527 |
-
*
|
| 1528 |
-
to the parameter of the distribution.
|
| 1529 |
|
| 1530 |
``` cpp
|
| 1531 |
double p() const;
|
| 1532 |
```
|
| 1533 |
|
|
@@ -1536,14 +1693,12 @@ constructed.
|
|
| 1536 |
|
| 1537 |
##### Class template `negative_binomial_distribution` <a id="rand.dist.bern.negbin">[[rand.dist.bern.negbin]]</a>
|
| 1538 |
|
| 1539 |
A `negative_binomial_distribution` random number distribution produces
|
| 1540 |
random integers i ≥ 0 distributed according to the discrete probability
|
| 1541 |
-
function
|
| 1542 |
-
|
| 1543 |
-
= \binom{k+i-1}{i} \cdot p^k \cdot (1-p)^i
|
| 1544 |
-
\; \mbox{.}$$
|
| 1545 |
|
| 1546 |
[*Note 1*: This implies that P(i | k,p) is undefined when
|
| 1547 |
`p == 1`. — *end note*]
|
| 1548 |
|
| 1549 |
``` cpp
|
|
@@ -1553,11 +1708,12 @@ template<class IntType = int>
|
|
| 1553 |
// types
|
| 1554 |
using result_type = IntType;
|
| 1555 |
using param_type = unspecified;
|
| 1556 |
|
| 1557 |
// constructor and reset functions
|
| 1558 |
-
|
|
|
|
| 1559 |
explicit negative_binomial_distribution(const param_type& parm);
|
| 1560 |
void reset();
|
| 1561 |
|
| 1562 |
// generating functions
|
| 1563 |
template<class URBG>
|
|
@@ -1574,17 +1730,17 @@ template<class IntType = int>
|
|
| 1574 |
result_type max() const;
|
| 1575 |
};
|
| 1576 |
```
|
| 1577 |
|
| 1578 |
``` cpp
|
| 1579 |
-
explicit negative_binomial_distribution(IntType k
|
| 1580 |
```
|
| 1581 |
|
| 1582 |
-
*
|
| 1583 |
|
| 1584 |
-
*
|
| 1585 |
-
|
| 1586 |
|
| 1587 |
``` cpp
|
| 1588 |
IntType k() const;
|
| 1589 |
```
|
| 1590 |
|
|
@@ -1602,16 +1758,12 @@ constructed.
|
|
| 1602 |
|
| 1603 |
##### Class template `poisson_distribution` <a id="rand.dist.pois.poisson">[[rand.dist.pois.poisson]]</a>
|
| 1604 |
|
| 1605 |
A `poisson_distribution` random number distribution produces integer
|
| 1606 |
values i ≥ 0 distributed according to the discrete probability function
|
| 1607 |
-
$$
|
| 1608 |
-
|
| 1609 |
-
= \frac{ e^{-\mu} \mu^{i} }
|
| 1610 |
-
{ i\,! }
|
| 1611 |
-
\; \mbox{.}$$ The distribution parameter μ is also known as this
|
| 1612 |
-
distribution’s *mean* .
|
| 1613 |
|
| 1614 |
``` cpp
|
| 1615 |
template<class IntType = int>
|
| 1616 |
class poisson_distribution
|
| 1617 |
{
|
|
@@ -1619,11 +1771,12 @@ template<class IntType = int>
|
|
| 1619 |
// types
|
| 1620 |
using result_type = IntType;
|
| 1621 |
using param_type = unspecified;
|
| 1622 |
|
| 1623 |
// constructors and reset functions
|
| 1624 |
-
|
|
|
|
| 1625 |
explicit poisson_distribution(const param_type& parm);
|
| 1626 |
void reset();
|
| 1627 |
|
| 1628 |
// generating functions
|
| 1629 |
template<class URBG>
|
|
@@ -1639,17 +1792,16 @@ template<class IntType = int>
|
|
| 1639 |
result_type max() const;
|
| 1640 |
};
|
| 1641 |
```
|
| 1642 |
|
| 1643 |
``` cpp
|
| 1644 |
-
explicit poisson_distribution(double mean
|
| 1645 |
```
|
| 1646 |
|
| 1647 |
-
*
|
| 1648 |
|
| 1649 |
-
*
|
| 1650 |
-
corresponds to the parameter of the distribution.
|
| 1651 |
|
| 1652 |
``` cpp
|
| 1653 |
double mean() const;
|
| 1654 |
```
|
| 1655 |
|
|
@@ -1658,25 +1810,23 @@ constructed.
|
|
| 1658 |
|
| 1659 |
##### Class template `exponential_distribution` <a id="rand.dist.pois.exp">[[rand.dist.pois.exp]]</a>
|
| 1660 |
|
| 1661 |
An `exponential_distribution` random number distribution produces random
|
| 1662 |
numbers x > 0 distributed according to the probability density function
|
| 1663 |
-
$$
|
| 1664 |
-
p(x\,|\,\lambda)
|
| 1665 |
-
= \lambda e^{-\lambda x}
|
| 1666 |
-
\; \mbox{.}$$
|
| 1667 |
|
| 1668 |
``` cpp
|
| 1669 |
template<class RealType = double>
|
| 1670 |
class exponential_distribution {
|
| 1671 |
public:
|
| 1672 |
// types
|
| 1673 |
using result_type = RealType;
|
| 1674 |
using param_type = unspecified;
|
| 1675 |
|
| 1676 |
// constructors and reset functions
|
| 1677 |
-
|
|
|
|
| 1678 |
explicit exponential_distribution(const param_type& parm);
|
| 1679 |
void reset();
|
| 1680 |
|
| 1681 |
// generating functions
|
| 1682 |
template<class URBG>
|
|
@@ -1692,17 +1842,16 @@ template<class RealType = double>
|
|
| 1692 |
result_type max() const;
|
| 1693 |
};
|
| 1694 |
```
|
| 1695 |
|
| 1696 |
``` cpp
|
| 1697 |
-
explicit exponential_distribution(RealType lambda
|
| 1698 |
```
|
| 1699 |
|
| 1700 |
-
*
|
| 1701 |
|
| 1702 |
-
*
|
| 1703 |
-
corresponds to the parameter of the distribution.
|
| 1704 |
|
| 1705 |
``` cpp
|
| 1706 |
RealType lambda() const;
|
| 1707 |
```
|
| 1708 |
|
|
@@ -1711,26 +1860,25 @@ constructed.
|
|
| 1711 |
|
| 1712 |
##### Class template `gamma_distribution` <a id="rand.dist.pois.gamma">[[rand.dist.pois.gamma]]</a>
|
| 1713 |
|
| 1714 |
A `gamma_distribution` random number distribution produces random
|
| 1715 |
numbers x > 0 distributed according to the probability density function
|
| 1716 |
-
$$
|
| 1717 |
-
|
| 1718 |
-
|
| 1719 |
-
\, \cdot \, x^{\, \alpha-1}
|
| 1720 |
-
\; \mbox{.}$$
|
| 1721 |
|
| 1722 |
``` cpp
|
| 1723 |
template<class RealType = double>
|
| 1724 |
class gamma_distribution {
|
| 1725 |
public:
|
| 1726 |
// types
|
| 1727 |
using result_type = RealType;
|
| 1728 |
using param_type = unspecified;
|
| 1729 |
|
| 1730 |
// constructors and reset functions
|
| 1731 |
-
|
|
|
|
| 1732 |
explicit gamma_distribution(const param_type& parm);
|
| 1733 |
void reset();
|
| 1734 |
|
| 1735 |
// generating functions
|
| 1736 |
template<class URBG>
|
|
@@ -1747,17 +1895,17 @@ template<class RealType = double>
|
|
| 1747 |
result_type max() const;
|
| 1748 |
};
|
| 1749 |
```
|
| 1750 |
|
| 1751 |
``` cpp
|
| 1752 |
-
explicit gamma_distribution(RealType alpha
|
| 1753 |
```
|
| 1754 |
|
| 1755 |
-
*
|
| 1756 |
|
| 1757 |
-
*
|
| 1758 |
-
|
| 1759 |
|
| 1760 |
``` cpp
|
| 1761 |
RealType alpha() const;
|
| 1762 |
```
|
| 1763 |
|
|
@@ -1773,27 +1921,26 @@ constructed.
|
|
| 1773 |
|
| 1774 |
##### Class template `weibull_distribution` <a id="rand.dist.pois.weibull">[[rand.dist.pois.weibull]]</a>
|
| 1775 |
|
| 1776 |
A `weibull_distribution` random number distribution produces random
|
| 1777 |
numbers x ≥ 0 distributed according to the probability density function
|
| 1778 |
-
$$
|
| 1779 |
-
p(x\,|\,a,b)
|
| 1780 |
-
= \frac{a}{b}
|
| 1781 |
\cdot \left(\frac{x}{b}\right)^{a-1}
|
| 1782 |
\cdot \, \exp\left( -\left(\frac{x}{b}\right)^a\right)
|
| 1783 |
-
\
|
| 1784 |
|
| 1785 |
``` cpp
|
| 1786 |
template<class RealType = double>
|
| 1787 |
class weibull_distribution {
|
| 1788 |
public:
|
| 1789 |
// types
|
| 1790 |
using result_type = RealType;
|
| 1791 |
using param_type = unspecified;
|
| 1792 |
|
| 1793 |
// constructor and reset functions
|
| 1794 |
-
|
|
|
|
| 1795 |
explicit weibull_distribution(const param_type& parm);
|
| 1796 |
void reset();
|
| 1797 |
|
| 1798 |
// generating functions
|
| 1799 |
template<class URBG>
|
|
@@ -1810,17 +1957,17 @@ template<class RealType = double>
|
|
| 1810 |
result_type max() const;
|
| 1811 |
};
|
| 1812 |
```
|
| 1813 |
|
| 1814 |
``` cpp
|
| 1815 |
-
explicit weibull_distribution(RealType a
|
| 1816 |
```
|
| 1817 |
|
| 1818 |
-
*
|
| 1819 |
|
| 1820 |
-
*
|
| 1821 |
-
|
| 1822 |
|
| 1823 |
``` cpp
|
| 1824 |
RealType a() const;
|
| 1825 |
```
|
| 1826 |
|
|
@@ -1836,28 +1983,25 @@ constructed.
|
|
| 1836 |
|
| 1837 |
##### Class template `extreme_value_distribution` <a id="rand.dist.pois.extreme">[[rand.dist.pois.extreme]]</a>
|
| 1838 |
|
| 1839 |
An `extreme_value_distribution` random number distribution produces
|
| 1840 |
random numbers x distributed according to the probability density
|
| 1841 |
-
function[^6] $$
|
| 1842 |
-
|
| 1843 |
-
|
| 1844 |
-
\cdot \exp\left( \frac{a-x}{b}
|
| 1845 |
-
\,-\, \exp\left(\frac{a-x}{b}\right)
|
| 1846 |
-
\right)
|
| 1847 |
-
\; \mbox{.}$$
|
| 1848 |
|
| 1849 |
``` cpp
|
| 1850 |
template<class RealType = double>
|
| 1851 |
class extreme_value_distribution {
|
| 1852 |
public:
|
| 1853 |
// types
|
| 1854 |
using result_type = RealType;
|
| 1855 |
using param_type = unspecified;
|
| 1856 |
|
| 1857 |
// constructor and reset functions
|
| 1858 |
-
|
|
|
|
| 1859 |
explicit extreme_value_distribution(const param_type& parm);
|
| 1860 |
void reset();
|
| 1861 |
|
| 1862 |
// generating functions
|
| 1863 |
template<class URBG>
|
|
@@ -1874,17 +2018,17 @@ template<class RealType = double>
|
|
| 1874 |
result_type max() const;
|
| 1875 |
};
|
| 1876 |
```
|
| 1877 |
|
| 1878 |
``` cpp
|
| 1879 |
-
explicit extreme_value_distribution(RealType a
|
| 1880 |
```
|
| 1881 |
|
| 1882 |
-
*
|
| 1883 |
|
| 1884 |
-
*
|
| 1885 |
-
|
| 1886 |
|
| 1887 |
``` cpp
|
| 1888 |
RealType a() const;
|
| 1889 |
```
|
| 1890 |
|
|
@@ -1910,11 +2054,11 @@ numbers x distributed according to the probability density function $$%
|
|
| 1910 |
% e^{-(x-\mu)^2 / (2\sigma^2)}
|
| 1911 |
\exp{\left(- \, \frac{(x - \mu)^2}
|
| 1912 |
{2 \sigma^2}
|
| 1913 |
\right)
|
| 1914 |
}
|
| 1915 |
-
|
| 1916 |
distribution’s *mean* and *standard deviation* .
|
| 1917 |
|
| 1918 |
``` cpp
|
| 1919 |
template<class RealType = double>
|
| 1920 |
class normal_distribution {
|
|
@@ -1922,11 +2066,12 @@ template<class RealType = double>
|
|
| 1922 |
// types
|
| 1923 |
using result_type = RealType;
|
| 1924 |
using param_type = unspecified;
|
| 1925 |
|
| 1926 |
// constructors and reset functions
|
| 1927 |
-
|
|
|
|
| 1928 |
explicit normal_distribution(const param_type& parm);
|
| 1929 |
void reset();
|
| 1930 |
|
| 1931 |
// generating functions
|
| 1932 |
template<class URBG>
|
|
@@ -1943,17 +2088,17 @@ template<class RealType = double>
|
|
| 1943 |
result_type max() const;
|
| 1944 |
};
|
| 1945 |
```
|
| 1946 |
|
| 1947 |
``` cpp
|
| 1948 |
-
explicit normal_distribution(RealType mean
|
| 1949 |
```
|
| 1950 |
|
| 1951 |
-
*
|
| 1952 |
|
| 1953 |
-
*
|
| 1954 |
-
|
| 1955 |
|
| 1956 |
``` cpp
|
| 1957 |
RealType mean() const;
|
| 1958 |
```
|
| 1959 |
|
|
@@ -1969,31 +2114,25 @@ constructed.
|
|
| 1969 |
|
| 1970 |
##### Class template `lognormal_distribution` <a id="rand.dist.norm.lognormal">[[rand.dist.norm.lognormal]]</a>
|
| 1971 |
|
| 1972 |
A `lognormal_distribution` random number distribution produces random
|
| 1973 |
numbers x > 0 distributed according to the probability density function
|
| 1974 |
-
$$
|
| 1975 |
-
|
| 1976 |
-
|
| 1977 |
-
{s x \sqrt{2 \pi}}
|
| 1978 |
-
\cdot
|
| 1979 |
-
\exp{\left(- \, \frac{(\ln{x} - m)^2}
|
| 1980 |
-
{2 s^2}
|
| 1981 |
-
\right)
|
| 1982 |
-
}
|
| 1983 |
-
\; \mbox{.}$$
|
| 1984 |
|
| 1985 |
``` cpp
|
| 1986 |
template<class RealType = double>
|
| 1987 |
class lognormal_distribution {
|
| 1988 |
public:
|
| 1989 |
// types
|
| 1990 |
using result_type = RealType;
|
| 1991 |
using param_type = unspecified;
|
| 1992 |
|
| 1993 |
// constructor and reset functions
|
| 1994 |
-
|
|
|
|
| 1995 |
explicit lognormal_distribution(const param_type& parm);
|
| 1996 |
void reset();
|
| 1997 |
|
| 1998 |
// generating functions
|
| 1999 |
template<class URBG>
|
|
@@ -2010,17 +2149,17 @@ template<class RealType = double>
|
|
| 2010 |
result_type max() const;
|
| 2011 |
};
|
| 2012 |
```
|
| 2013 |
|
| 2014 |
``` cpp
|
| 2015 |
-
explicit lognormal_distribution(RealType m
|
| 2016 |
```
|
| 2017 |
|
| 2018 |
-
*
|
| 2019 |
|
| 2020 |
-
*
|
| 2021 |
-
|
| 2022 |
|
| 2023 |
``` cpp
|
| 2024 |
RealType m() const;
|
| 2025 |
```
|
| 2026 |
|
|
@@ -2036,26 +2175,23 @@ constructed.
|
|
| 2036 |
|
| 2037 |
##### Class template `chi_squared_distribution` <a id="rand.dist.norm.chisq">[[rand.dist.norm.chisq]]</a>
|
| 2038 |
|
| 2039 |
A `chi_squared_distribution` random number distribution produces random
|
| 2040 |
numbers x > 0 distributed according to the probability density function
|
| 2041 |
-
$$
|
| 2042 |
-
p(x\,|\,n)
|
| 2043 |
-
= \frac{ x^{(n/2)-1} \cdot e^{-x/2}}
|
| 2044 |
-
{\Gamma(n/2) \cdot 2^{n/2}}
|
| 2045 |
-
\; \mbox{.}$$
|
| 2046 |
|
| 2047 |
``` cpp
|
| 2048 |
template<class RealType = double>
|
| 2049 |
class chi_squared_distribution {
|
| 2050 |
public:
|
| 2051 |
// types
|
| 2052 |
using result_type = RealType;
|
| 2053 |
using param_type = unspecified;
|
| 2054 |
|
| 2055 |
// constructor and reset functions
|
| 2056 |
-
|
|
|
|
| 2057 |
explicit chi_squared_distribution(const param_type& parm);
|
| 2058 |
void reset();
|
| 2059 |
|
| 2060 |
// generating functions
|
| 2061 |
template<class URBG>
|
|
@@ -2071,17 +2207,16 @@ template<class RealType = double>
|
|
| 2071 |
result_type max() const;
|
| 2072 |
};
|
| 2073 |
```
|
| 2074 |
|
| 2075 |
``` cpp
|
| 2076 |
-
explicit chi_squared_distribution(RealType n
|
| 2077 |
```
|
| 2078 |
|
| 2079 |
-
*
|
| 2080 |
|
| 2081 |
-
*
|
| 2082 |
-
corresponds to the parameter of the distribution.
|
| 2083 |
|
| 2084 |
``` cpp
|
| 2085 |
RealType n() const;
|
| 2086 |
```
|
| 2087 |
|
|
@@ -2089,25 +2224,24 @@ RealType n() const;
|
|
| 2089 |
constructed.
|
| 2090 |
|
| 2091 |
##### Class template `cauchy_distribution` <a id="rand.dist.norm.cauchy">[[rand.dist.norm.cauchy]]</a>
|
| 2092 |
|
| 2093 |
A `cauchy_distribution` random number distribution produces random
|
| 2094 |
-
numbers x distributed according to the probability density function
|
| 2095 |
-
|
| 2096 |
-
= \left( \pi b \left( 1 + \left( \frac{x-a}{b} \right)^2 \;\right)\right)^{-1}
|
| 2097 |
-
\; \mbox{.}$$
|
| 2098 |
|
| 2099 |
``` cpp
|
| 2100 |
template<class RealType = double>
|
| 2101 |
class cauchy_distribution {
|
| 2102 |
public:
|
| 2103 |
// types
|
| 2104 |
using result_type = RealType;
|
| 2105 |
using param_type = unspecified;
|
| 2106 |
|
| 2107 |
// constructor and reset functions
|
| 2108 |
-
|
|
|
|
| 2109 |
explicit cauchy_distribution(const param_type& parm);
|
| 2110 |
void reset();
|
| 2111 |
|
| 2112 |
// generating functions
|
| 2113 |
template<class URBG>
|
|
@@ -2124,17 +2258,17 @@ template<class RealType = double>
|
|
| 2124 |
result_type max() const;
|
| 2125 |
};
|
| 2126 |
```
|
| 2127 |
|
| 2128 |
``` cpp
|
| 2129 |
-
explicit cauchy_distribution(RealType a
|
| 2130 |
```
|
| 2131 |
|
| 2132 |
-
*
|
| 2133 |
|
| 2134 |
-
*
|
| 2135 |
-
|
| 2136 |
|
| 2137 |
``` cpp
|
| 2138 |
RealType a() const;
|
| 2139 |
```
|
| 2140 |
|
|
@@ -2150,32 +2284,27 @@ constructed.
|
|
| 2150 |
|
| 2151 |
##### Class template `fisher_f_distribution` <a id="rand.dist.norm.f">[[rand.dist.norm.f]]</a>
|
| 2152 |
|
| 2153 |
A `fisher_f_distribution` random number distribution produces random
|
| 2154 |
numbers x ≥ 0 distributed according to the probability density function
|
| 2155 |
-
$$
|
| 2156 |
-
|
| 2157 |
-
|
| 2158 |
-
|
| 2159 |
-
|
| 2160 |
-
\left(\frac{m}{n}\right)^{m/2}
|
| 2161 |
-
\cdot
|
| 2162 |
-
x^{(m/2)-1}
|
| 2163 |
-
\cdot
|
| 2164 |
-
{\left( 1 + \frac{m x}{n} \right)}^{-(m+n)/2}
|
| 2165 |
-
\; \mbox{.}$$
|
| 2166 |
|
| 2167 |
``` cpp
|
| 2168 |
template<class RealType = double>
|
| 2169 |
class fisher_f_distribution {
|
| 2170 |
public:
|
| 2171 |
// types
|
| 2172 |
using result_type = RealType;
|
| 2173 |
using param_type = unspecified;
|
| 2174 |
|
| 2175 |
// constructor and reset functions
|
| 2176 |
-
|
|
|
|
| 2177 |
explicit fisher_f_distribution(const param_type& parm);
|
| 2178 |
void reset();
|
| 2179 |
|
| 2180 |
// generating functions
|
| 2181 |
template<class URBG>
|
|
@@ -2192,17 +2321,17 @@ template<class RealType = double>
|
|
| 2192 |
result_type max() const;
|
| 2193 |
};
|
| 2194 |
```
|
| 2195 |
|
| 2196 |
``` cpp
|
| 2197 |
-
explicit fisher_f_distribution(RealType m
|
| 2198 |
```
|
| 2199 |
|
| 2200 |
-
*
|
| 2201 |
|
| 2202 |
-
*
|
| 2203 |
-
|
| 2204 |
|
| 2205 |
``` cpp
|
| 2206 |
RealType m() const;
|
| 2207 |
```
|
| 2208 |
|
|
@@ -2217,29 +2346,27 @@ RealType n() const;
|
|
| 2217 |
constructed.
|
| 2218 |
|
| 2219 |
##### Class template `student_t_distribution` <a id="rand.dist.norm.t">[[rand.dist.norm.t]]</a>
|
| 2220 |
|
| 2221 |
A `student_t_distribution` random number distribution produces random
|
| 2222 |
-
numbers x distributed according to the probability density function
|
| 2223 |
-
|
| 2224 |
-
|
| 2225 |
-
{\sqrt{n \pi}}
|
| 2226 |
-
\cdot \frac{\Gamma\big((n+1)/2\big)}
|
| 2227 |
-
{\Gamma(n/2)}
|
| 2228 |
\cdot \left(1 + \frac{x^2}{n} \right)^{-(n+1)/2}
|
| 2229 |
-
\
|
| 2230 |
|
| 2231 |
``` cpp
|
| 2232 |
template<class RealType = double>
|
| 2233 |
class student_t_distribution {
|
| 2234 |
public:
|
| 2235 |
// types
|
| 2236 |
using result_type = RealType;
|
| 2237 |
using param_type = unspecified;
|
| 2238 |
|
| 2239 |
// constructor and reset functions
|
| 2240 |
-
|
|
|
|
| 2241 |
explicit student_t_distribution(const param_type& parm);
|
| 2242 |
void reset();
|
| 2243 |
|
| 2244 |
// generating functions
|
| 2245 |
template<class URBG>
|
|
@@ -2255,17 +2382,16 @@ template<class RealType = double>
|
|
| 2255 |
result_type max() const;
|
| 2256 |
};
|
| 2257 |
```
|
| 2258 |
|
| 2259 |
``` cpp
|
| 2260 |
-
explicit student_t_distribution(RealType n
|
| 2261 |
```
|
| 2262 |
|
| 2263 |
-
*
|
| 2264 |
|
| 2265 |
-
*
|
| 2266 |
-
to the parameter of the distribution.
|
| 2267 |
|
| 2268 |
``` cpp
|
| 2269 |
RealType n() const;
|
| 2270 |
```
|
| 2271 |
|
|
@@ -2276,20 +2402,17 @@ constructed.
|
|
| 2276 |
|
| 2277 |
##### Class template `discrete_distribution` <a id="rand.dist.samp.discrete">[[rand.dist.samp.discrete]]</a>
|
| 2278 |
|
| 2279 |
A `discrete_distribution` random number distribution produces random
|
| 2280 |
integers i, 0 ≤ i < n, distributed according to the discrete probability
|
| 2281 |
-
function $$
|
| 2282 |
-
P(i\,|\,p_0,\ldots,p_{n-1})
|
| 2283 |
-
= p_i
|
| 2284 |
-
\; \mbox{.}$$
|
| 2285 |
|
| 2286 |
Unless specified otherwise, the distribution parameters are calculated
|
| 2287 |
-
as:
|
| 2288 |
-
|
| 2289 |
-
non-
|
| 2290 |
-
0 < S =
|
| 2291 |
|
| 2292 |
``` cpp
|
| 2293 |
template<class IntType = int>
|
| 2294 |
class discrete_distribution {
|
| 2295 |
public:
|
|
@@ -2335,15 +2458,18 @@ p₀ = 1.
|
|
| 2335 |
``` cpp
|
| 2336 |
template<class InputIterator>
|
| 2337 |
discrete_distribution(InputIterator firstW, InputIterator lastW);
|
| 2338 |
```
|
| 2339 |
|
| 2340 |
-
*
|
| 2341 |
-
|
| 2342 |
-
`
|
| 2343 |
-
|
| 2344 |
-
|
|
|
|
|
|
|
|
|
|
| 2345 |
|
| 2346 |
*Effects:* Constructs a `discrete_distribution` object with
|
| 2347 |
probabilities given by the formula above.
|
| 2348 |
|
| 2349 |
``` cpp
|
|
@@ -2355,22 +2481,22 @@ discrete_distribution(initializer_list<double> wl);
|
|
| 2355 |
``` cpp
|
| 2356 |
template<class UnaryOperation>
|
| 2357 |
discrete_distribution(size_t nw, double xmin, double xmax, UnaryOperation fw);
|
| 2358 |
```
|
| 2359 |
|
| 2360 |
-
*
|
| 2361 |
-
|
| 2362 |
-
|
| 2363 |
-
|
| 2364 |
-
|
| 2365 |
|
| 2366 |
*Effects:* Constructs a `discrete_distribution` object with
|
| 2367 |
probabilities given by the formula above, using the following values: If
|
| 2368 |
`nw` = 0, let w₀ = 1. Otherwise, let wₖ = `fw`(`xmin` + k ⋅ δ + δ / 2)
|
| 2369 |
for k = 0, …, n - 1.
|
| 2370 |
|
| 2371 |
-
*Complexity:* The number of invocations of `fw`
|
| 2372 |
|
| 2373 |
``` cpp
|
| 2374 |
vector<double> probabilities() const;
|
| 2375 |
```
|
| 2376 |
|
|
@@ -2381,27 +2507,21 @@ k = 0, …, n-1.
|
|
| 2381 |
##### Class template `piecewise_constant_distribution` <a id="rand.dist.samp.pconst">[[rand.dist.samp.pconst]]</a>
|
| 2382 |
|
| 2383 |
A `piecewise_constant_distribution` random number distribution produces
|
| 2384 |
random numbers x, b₀ ≤ x < bₙ, uniformly distributed over each
|
| 2385 |
subinterval [ bᵢ, bᵢ₊₁ ) according to the probability density function
|
| 2386 |
-
$$
|
| 2387 |
-
|
| 2388 |
-
= \rho_i
|
| 2389 |
-
\; \mbox{,}
|
| 2390 |
-
\mbox{ for } b_i \le x < b_{i+1}
|
| 2391 |
-
\; \mbox{.}$$
|
| 2392 |
|
| 2393 |
The n + 1 distribution parameters bᵢ, also known as this distribution’s
|
| 2394 |
-
*interval boundaries* , shall satisfy the relation
|
| 2395 |
-
i = 0, …, n-1. Unless specified otherwise, the remaining n
|
| 2396 |
-
parameters are calculated as:
|
| 2397 |
-
|
| 2398 |
-
|
| 2399 |
-
|
| 2400 |
-
|
| 2401 |
-
non-infinity. Moreover, the following relation shall hold:
|
| 2402 |
-
0 < S = w₀ + ⋯ + wₙ₋₁.
|
| 2403 |
|
| 2404 |
``` cpp
|
| 2405 |
template<class RealType = double>
|
| 2406 |
class piecewise_constant_distribution {
|
| 2407 |
public:
|
|
@@ -2449,59 +2569,60 @@ n = 1, ρ₀ = 1, b₀ = 0, and b₁ = 1.
|
|
| 2449 |
template<class InputIteratorB, class InputIteratorW>
|
| 2450 |
piecewise_constant_distribution(InputIteratorB firstB, InputIteratorB lastB,
|
| 2451 |
InputIteratorW firstW);
|
| 2452 |
```
|
| 2453 |
|
| 2454 |
-
*
|
| 2455 |
-
|
| 2456 |
-
|
| 2457 |
-
`iterator_traits<
|
| 2458 |
-
|
| 2459 |
-
|
| 2460 |
-
|
| 2461 |
-
|
| 2462 |
-
|
| 2463 |
-
|
|
|
|
|
|
|
|
|
|
| 2464 |
|
| 2465 |
*Effects:* Constructs a `piecewise_constant_distribution` object with
|
| 2466 |
parameters as specified above.
|
| 2467 |
|
| 2468 |
``` cpp
|
| 2469 |
template<class UnaryOperation>
|
| 2470 |
piecewise_constant_distribution(initializer_list<RealType> bl, UnaryOperation fw);
|
| 2471 |
```
|
| 2472 |
|
| 2473 |
-
*
|
| 2474 |
-
|
| 2475 |
-
`double`. Moreover, `double` shall be convertible to the type of
|
| 2476 |
-
`UnaryOperation`’s sole parameter.
|
| 2477 |
|
| 2478 |
*Effects:* Constructs a `piecewise_constant_distribution` object with
|
| 2479 |
parameters taken or calculated from the following values: If
|
| 2480 |
`bl.size()` < 2, let n = 1, w₀ = 1, b₀ = 0, and b₁ = 1. Otherwise, let
|
| 2481 |
[`bl.begin()`, `bl.end()`) form a sequence b₀, …, bₙ, and let
|
| 2482 |
wₖ = `fw`((bₖ₊₁ + bₖ) / 2) for k = 0, …, n - 1.
|
| 2483 |
|
| 2484 |
-
*Complexity:* The number of invocations of `fw`
|
| 2485 |
|
| 2486 |
``` cpp
|
| 2487 |
template<class UnaryOperation>
|
| 2488 |
piecewise_constant_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw);
|
| 2489 |
```
|
| 2490 |
|
| 2491 |
-
*
|
| 2492 |
-
|
| 2493 |
-
|
| 2494 |
-
|
| 2495 |
-
|
| 2496 |
|
| 2497 |
*Effects:* Constructs a `piecewise_constant_distribution` object with
|
| 2498 |
parameters taken or calculated from the following values: Let
|
| 2499 |
bₖ = `xmin` + k ⋅ δ for k = 0, …, n, and wₖ = `fw`(bₖ + δ / 2) for
|
| 2500 |
k = 0, …, n - 1.
|
| 2501 |
|
| 2502 |
-
*Complexity:* The number of invocations of `fw`
|
| 2503 |
|
| 2504 |
``` cpp
|
| 2505 |
vector<result_type> intervals() const;
|
| 2506 |
```
|
| 2507 |
|
|
@@ -2519,29 +2640,24 @@ k = 0, …, n-1.
|
|
| 2519 |
|
| 2520 |
##### Class template `piecewise_linear_distribution` <a id="rand.dist.samp.plinear">[[rand.dist.samp.plinear]]</a>
|
| 2521 |
|
| 2522 |
A `piecewise_linear_distribution` random number distribution produces
|
| 2523 |
random numbers x, b₀ ≤ x < bₙ, distributed over each subinterval
|
| 2524 |
-
[
|
| 2525 |
-
|
| 2526 |
-
|
| 2527 |
+ \rho_{i+1} \cdot {\frac{x - b_i}{b_{i+1} - b_i}}
|
| 2528 |
-
\
|
| 2529 |
-
\mbox{ for } b_i \le x < b_{i+1}
|
| 2530 |
-
\; \mbox{.}$$
|
| 2531 |
|
| 2532 |
The n + 1 distribution parameters bᵢ, also known as this distribution’s
|
| 2533 |
*interval boundaries* , shall satisfy the relation bᵢ < bᵢ₊₁ for
|
| 2534 |
i = 0, …, n - 1. Unless specified otherwise, the remaining n + 1
|
| 2535 |
-
distribution parameters are calculated as
|
| 2536 |
-
|
| 2537 |
-
|
| 2538 |
-
|
| 2539 |
-
|
| 2540 |
-
0 < S = \frac{1}{2}
|
| 2541 |
-
\cdot \sum_{k=0}^{n-1} (w_k + w_{k+1}) \cdot (b_{k+1} - b_k)
|
| 2542 |
-
\; \mbox{.}$$
|
| 2543 |
|
| 2544 |
``` cpp
|
| 2545 |
template<class RealType = double>
|
| 2546 |
class piecewise_linear_distribution {
|
| 2547 |
public:
|
|
@@ -2588,58 +2704,55 @@ n = 1, ρ₀ = ρ₁ = 1, b₀ = 0, and b₁ = 1.
|
|
| 2588 |
template<class InputIteratorB, class InputIteratorW>
|
| 2589 |
piecewise_linear_distribution(InputIteratorB firstB, InputIteratorB lastB,
|
| 2590 |
InputIteratorW firstW);
|
| 2591 |
```
|
| 2592 |
|
| 2593 |
-
*
|
| 2594 |
-
|
| 2595 |
-
|
| 2596 |
-
`
|
| 2597 |
-
|
| 2598 |
-
|
| 2599 |
-
|
| 2600 |
-
|
| 2601 |
-
|
| 2602 |
-
n+1, and any wₖ for k ≥ n+1 shall be ignored by the distribution.
|
| 2603 |
|
| 2604 |
*Effects:* Constructs a `piecewise_linear_distribution` object with
|
| 2605 |
parameters as specified above.
|
| 2606 |
|
| 2607 |
``` cpp
|
| 2608 |
template<class UnaryOperation>
|
| 2609 |
piecewise_linear_distribution(initializer_list<RealType> bl, UnaryOperation fw);
|
| 2610 |
```
|
| 2611 |
|
| 2612 |
-
*
|
| 2613 |
-
|
| 2614 |
-
`double`. Moreover, `double` shall be convertible to the type of
|
| 2615 |
-
`UnaryOperation`’s sole parameter.
|
| 2616 |
|
| 2617 |
*Effects:* Constructs a `piecewise_linear_distribution` object with
|
| 2618 |
parameters taken or calculated from the following values: If
|
| 2619 |
`bl.size()` < 2, let n = 1, ρ₀ = ρ₁ = 1, b₀ = 0, and b₁ = 1. Otherwise,
|
| 2620 |
let [`bl.begin(),` `bl.end()`) form a sequence b₀, …, bₙ, and let
|
| 2621 |
wₖ = `fw`(bₖ) for k = 0, …, n.
|
| 2622 |
|
| 2623 |
-
*Complexity:* The number of invocations of `fw`
|
| 2624 |
|
| 2625 |
``` cpp
|
| 2626 |
template<class UnaryOperation>
|
| 2627 |
piecewise_linear_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw);
|
| 2628 |
```
|
| 2629 |
|
| 2630 |
-
*
|
| 2631 |
-
|
| 2632 |
-
|
| 2633 |
-
|
| 2634 |
-
|
| 2635 |
|
| 2636 |
*Effects:* Constructs a `piecewise_linear_distribution` object with
|
| 2637 |
parameters taken or calculated from the following values: Let
|
| 2638 |
bₖ = `xmin` + k ⋅ δ for k = 0, …, n, and wₖ = `fw`(bₖ) for k = 0, …, n.
|
| 2639 |
|
| 2640 |
-
*Complexity:* The number of invocations of `fw`
|
| 2641 |
|
| 2642 |
``` cpp
|
| 2643 |
vector<result_type> intervals() const;
|
| 2644 |
```
|
| 2645 |
|
|
@@ -2655,12 +2768,12 @@ vector<result_type> densities() const;
|
|
| 2655 |
whose `operator[]` member returns ρₖ when invoked with argument k for
|
| 2656 |
k = 0, …, n.
|
| 2657 |
|
| 2658 |
### Low-quality random number generation <a id="c.math.rand">[[c.math.rand]]</a>
|
| 2659 |
|
| 2660 |
-
[*Note 1*: The header `<cstdlib>`
|
| 2661 |
-
|
| 2662 |
|
| 2663 |
``` cpp
|
| 2664 |
int rand();
|
| 2665 |
void srand(unsigned int seed);
|
| 2666 |
```
|
|
@@ -2668,15 +2781,15 @@ void srand(unsigned int seed);
|
|
| 2668 |
*Effects:* The `rand` and `srand` functions have the semantics specified
|
| 2669 |
in the C standard library.
|
| 2670 |
|
| 2671 |
*Remarks:* The implementation may specify that particular library
|
| 2672 |
functions may call `rand`. It is *implementation-defined* whether the
|
| 2673 |
-
`rand` function may introduce data races
|
| 2674 |
|
| 2675 |
[*Note 1*: The other random number generation facilities in this
|
| 2676 |
-
|
| 2677 |
-
|
| 2678 |
-
|
| 2679 |
-
|
| 2680 |
|
| 2681 |
-
ISO C 7.22.2
|
| 2682 |
|
|
|
|
| 4 |
numbers.
|
| 5 |
|
| 6 |
In addition to a few utilities, four categories of entities are
|
| 7 |
described: *uniform random bit generators*, *random number engines*,
|
| 8 |
*random number engine adaptors*, and *random number distributions*.
|
| 9 |
+
These categorizations are applicable to types that meet the
|
| 10 |
corresponding requirements, to objects instantiated from such types, and
|
| 11 |
to templates producing such types when instantiated.
|
| 12 |
|
| 13 |
[*Note 1*: These entities are specified in such a way as to permit the
|
| 14 |
binding of any uniform random bit generator object `e` as the argument
|
| 15 |
to any random number distribution object `d`, thus producing a
|
| 16 |
zero-argument function object such as given by
|
| 17 |
`bind(d,e)`. — *end note*]
|
| 18 |
|
| 19 |
Each of the entities specified via this subclause has an associated
|
| 20 |
+
arithmetic type [[basic.fundamental]] identified as `result_type`. With
|
| 21 |
+
`T` as the `result_type` thus associated with such an entity, that
|
| 22 |
entity is characterized:
|
| 23 |
|
| 24 |
+
- as *boolean* or equivalently as *boolean-valued*, if `T` is `bool`;
|
| 25 |
+
- otherwise as *integral* or equivalently as *integer-valued*, if
|
| 26 |
+
`numeric_limits<T>::is_integer` is `true`;
|
| 27 |
+
- otherwise as *floating-point* or equivalently as *real-valued*.
|
| 28 |
+
|
| 29 |
If integer-valued, an entity may optionally be further characterized as
|
| 30 |
*signed* or *unsigned*, according to `numeric_limits<T>::is_signed`.
|
| 31 |
|
| 32 |
Unless otherwise specified, all descriptions of calculations in this
|
| 33 |
subclause use mathematical real numbers.
|
| 34 |
|
| 35 |
+
Throughout this subclause, the operators , , and \xor denote the
|
| 36 |
+
respective conventional bitwise operations. Further:
|
| 37 |
+
|
| 38 |
+
- the operator \rightshift denotes a bitwise right shift with
|
| 39 |
+
zero-valued bits appearing in the high bits of the result, and
|
| 40 |
+
- the operator denotes a bitwise left shift with zero-valued bits
|
| 41 |
+
appearing in the low bits of the result, and whose result is always
|
| 42 |
+
taken modulo 2ʷ.
|
| 43 |
+
|
| 44 |
+
### Header `<random>` synopsis <a id="rand.synopsis">[[rand.synopsis]]</a>
|
| 45 |
+
|
| 46 |
+
``` cpp
|
| 47 |
+
#include <initializer_list>
|
| 48 |
+
|
| 49 |
+
namespace std {
|
| 50 |
+
// [rand.req.urng], uniform random bit generator requirements
|
| 51 |
+
template<class G>
|
| 52 |
+
concept uniform_random_bit_generator = see below;
|
| 53 |
+
|
| 54 |
+
// [rand.eng.lcong], class template linear_congruential_engine
|
| 55 |
+
template<class UIntType, UIntType a, UIntType c, UIntType m>
|
| 56 |
+
class linear_congruential_engine;
|
| 57 |
+
|
| 58 |
+
// [rand.eng.mers], class template mersenne_twister_engine
|
| 59 |
+
template<class UIntType, size_t w, size_t n, size_t m, size_t r,
|
| 60 |
+
UIntType a, size_t u, UIntType d, size_t s,
|
| 61 |
+
UIntType b, size_t t,
|
| 62 |
+
UIntType c, size_t l, UIntType f>
|
| 63 |
+
class mersenne_twister_engine;
|
| 64 |
+
|
| 65 |
+
// [rand.eng.sub], class template subtract_with_carry_engine
|
| 66 |
+
template<class UIntType, size_t w, size_t s, size_t r>
|
| 67 |
+
class subtract_with_carry_engine;
|
| 68 |
+
|
| 69 |
+
// [rand.adapt.disc], class template discard_block_engine
|
| 70 |
+
template<class Engine, size_t p, size_t r>
|
| 71 |
+
class discard_block_engine;
|
| 72 |
+
|
| 73 |
+
// [rand.adapt.ibits], class template independent_bits_engine
|
| 74 |
+
template<class Engine, size_t w, class UIntType>
|
| 75 |
+
class independent_bits_engine;
|
| 76 |
+
|
| 77 |
+
// [rand.adapt.shuf], class template shuffle_order_engine
|
| 78 |
+
template<class Engine, size_t k>
|
| 79 |
+
class shuffle_order_engine;
|
| 80 |
+
|
| 81 |
+
// [rand.predef], engines and engine adaptors with predefined parameters
|
| 82 |
+
using minstd_rand0 = see below;
|
| 83 |
+
using minstd_rand = see below;
|
| 84 |
+
using mt19937 = see below;
|
| 85 |
+
using mt19937_64 = see below;
|
| 86 |
+
using ranlux24_base = see below;
|
| 87 |
+
using ranlux48_base = see below;
|
| 88 |
+
using ranlux24 = see below;
|
| 89 |
+
using ranlux48 = see below;
|
| 90 |
+
using knuth_b = see below;
|
| 91 |
+
|
| 92 |
+
using default_random_engine = see below;
|
| 93 |
+
|
| 94 |
+
// [rand.device], class random_device
|
| 95 |
+
class random_device;
|
| 96 |
+
|
| 97 |
+
// [rand.util.seedseq], class seed_seq
|
| 98 |
+
class seed_seq;
|
| 99 |
+
|
| 100 |
+
// [rand.util.canonical], function template generate_canonical
|
| 101 |
+
template<class RealType, size_t bits, class URBG>
|
| 102 |
+
RealType generate_canonical(URBG& g);
|
| 103 |
+
|
| 104 |
+
// [rand.dist.uni.int], class template uniform_int_distribution
|
| 105 |
+
template<class IntType = int>
|
| 106 |
+
class uniform_int_distribution;
|
| 107 |
+
|
| 108 |
+
// [rand.dist.uni.real], class template uniform_real_distribution
|
| 109 |
+
template<class RealType = double>
|
| 110 |
+
class uniform_real_distribution;
|
| 111 |
+
|
| 112 |
+
// [rand.dist.bern.bernoulli], class bernoulli_distribution
|
| 113 |
+
class bernoulli_distribution;
|
| 114 |
+
|
| 115 |
+
// [rand.dist.bern.bin], class template binomial_distribution
|
| 116 |
+
template<class IntType = int>
|
| 117 |
+
class binomial_distribution;
|
| 118 |
+
|
| 119 |
+
// [rand.dist.bern.geo], class template geometric_distribution
|
| 120 |
+
template<class IntType = int>
|
| 121 |
+
class geometric_distribution;
|
| 122 |
+
|
| 123 |
+
// [rand.dist.bern.negbin], class template negative_binomial_distribution
|
| 124 |
+
template<class IntType = int>
|
| 125 |
+
class negative_binomial_distribution;
|
| 126 |
+
|
| 127 |
+
// [rand.dist.pois.poisson], class template poisson_distribution
|
| 128 |
+
template<class IntType = int>
|
| 129 |
+
class poisson_distribution;
|
| 130 |
+
|
| 131 |
+
// [rand.dist.pois.exp], class template exponential_distribution
|
| 132 |
+
template<class RealType = double>
|
| 133 |
+
class exponential_distribution;
|
| 134 |
+
|
| 135 |
+
// [rand.dist.pois.gamma], class template gamma_distribution
|
| 136 |
+
template<class RealType = double>
|
| 137 |
+
class gamma_distribution;
|
| 138 |
+
|
| 139 |
+
// [rand.dist.pois.weibull], class template weibull_distribution
|
| 140 |
+
template<class RealType = double>
|
| 141 |
+
class weibull_distribution;
|
| 142 |
+
|
| 143 |
+
// [rand.dist.pois.extreme], class template extreme_value_distribution
|
| 144 |
+
template<class RealType = double>
|
| 145 |
+
class extreme_value_distribution;
|
| 146 |
+
|
| 147 |
+
// [rand.dist.norm.normal], class template normal_distribution
|
| 148 |
+
template<class RealType = double>
|
| 149 |
+
class normal_distribution;
|
| 150 |
+
|
| 151 |
+
// [rand.dist.norm.lognormal], class template lognormal_distribution
|
| 152 |
+
template<class RealType = double>
|
| 153 |
+
class lognormal_distribution;
|
| 154 |
+
|
| 155 |
+
// [rand.dist.norm.chisq], class template chi_squared_distribution
|
| 156 |
+
template<class RealType = double>
|
| 157 |
+
class chi_squared_distribution;
|
| 158 |
+
|
| 159 |
+
// [rand.dist.norm.cauchy], class template cauchy_distribution
|
| 160 |
+
template<class RealType = double>
|
| 161 |
+
class cauchy_distribution;
|
| 162 |
+
|
| 163 |
+
// [rand.dist.norm.f], class template fisher_f_distribution
|
| 164 |
+
template<class RealType = double>
|
| 165 |
+
class fisher_f_distribution;
|
| 166 |
+
|
| 167 |
+
// [rand.dist.norm.t], class template student_t_distribution
|
| 168 |
+
template<class RealType = double>
|
| 169 |
+
class student_t_distribution;
|
| 170 |
+
|
| 171 |
+
// [rand.dist.samp.discrete], class template discrete_distribution
|
| 172 |
+
template<class IntType = int>
|
| 173 |
+
class discrete_distribution;
|
| 174 |
+
|
| 175 |
+
// [rand.dist.samp.pconst], class template piecewise_constant_distribution
|
| 176 |
+
template<class RealType = double>
|
| 177 |
+
class piecewise_constant_distribution;
|
| 178 |
+
|
| 179 |
+
// [rand.dist.samp.plinear], class template piecewise_linear_distribution
|
| 180 |
+
template<class RealType = double>
|
| 181 |
+
class piecewise_linear_distribution;
|
| 182 |
+
}
|
| 183 |
+
```
|
| 184 |
|
| 185 |
### Requirements <a id="rand.req">[[rand.req]]</a>
|
| 186 |
|
| 187 |
#### General requirements <a id="rand.req.genl">[[rand.req.genl]]</a>
|
| 188 |
|
| 189 |
Throughout this subclause [[rand]], the effect of instantiating a
|
| 190 |
template:
|
| 191 |
|
| 192 |
+
- that has a template type parameter named `Sseq` is undefined unless
|
| 193 |
+
the corresponding template argument is cv-unqualified and meets the
|
| 194 |
+
requirements of seed sequence [[rand.req.seedseq]].
|
| 195 |
+
- that has a template type parameter named `URBG` is undefined unless
|
| 196 |
+
the corresponding template argument is cv-unqualified and meets the
|
| 197 |
+
requirements of uniform random bit generator [[rand.req.urng]].
|
| 198 |
+
- that has a template type parameter named `Engine` is undefined unless
|
| 199 |
+
the corresponding template argument is cv-unqualified and meets the
|
| 200 |
+
requirements of random number engine [[rand.req.eng]].
|
| 201 |
+
- that has a template type parameter named `RealType` is undefined
|
| 202 |
+
unless the corresponding template argument is cv-unqualified and is
|
| 203 |
+
one of `float`, `double`, or `long double`.
|
| 204 |
+
- that has a template type parameter named `IntType` is undefined unless
|
| 205 |
+
the corresponding template argument is cv-unqualified and is one of
|
| 206 |
+
`short`, `int`, `long`, `long long`, `unsigned short`, `unsigned int`,
|
| 207 |
+
`unsigned long`, or `unsigned long long`.
|
| 208 |
+
- that has a template type parameter named `UIntType` is undefined
|
| 209 |
+
unless the corresponding template argument is cv-unqualified and is
|
| 210 |
+
one of `unsigned short`, `unsigned int`, `unsigned long`, or
|
| 211 |
+
`unsigned long long`.
|
| 212 |
+
|
| 213 |
Throughout this subclause [[rand]], phrases of the form “`x` is an
|
| 214 |
iterator of a specific kind” shall be interpreted as equivalent to the
|
| 215 |
+
more formal requirement that “`x` is a value of a type meeting the
|
| 216 |
requirements of the specified iterator type”.
|
| 217 |
|
| 218 |
Throughout this subclause [[rand]], any constructor that can be called
|
| 219 |
+
with a single argument and that meets a requirement specified in this
|
| 220 |
+
subclause shall be declared `explicit`.
|
| 221 |
|
| 222 |
#### Seed sequence requirements <a id="rand.req.seedseq">[[rand.req.seedseq]]</a>
|
| 223 |
|
| 224 |
A *seed sequence* is an object that consumes a sequence of
|
| 225 |
integer-valued data and produces a requested number of unsigned integer
|
|
|
|
| 228 |
[*Note 1*: Such an object provides a mechanism to avoid replication of
|
| 229 |
streams of random variates. This can be useful, for example, in
|
| 230 |
applications requiring large numbers of random number
|
| 231 |
engines. — *end note*]
|
| 232 |
|
| 233 |
+
A class `S` meets the requirements of a seed sequence if the expressions
|
| 234 |
+
shown in [[rand.req.seedseq]] are valid and have the indicated
|
| 235 |
+
semantics, and if `S` also meets all other requirements of this
|
| 236 |
+
subclause [[rand.req.seedseq]]. In that Table and throughout this
|
| 237 |
+
subclause:
|
| 238 |
+
|
| 239 |
+
- `T` is the type named by `S`’s associated `result_type`;
|
| 240 |
+
- `q` is a value of `S` and `r` is a possibly const value of `S`;
|
| 241 |
+
- `ib` and `ie` are input iterators with an unsigned integer
|
| 242 |
+
`value_type` of at least 32 bits;
|
| 243 |
+
- `rb` and `re` are mutable random access iterators with an unsigned
|
| 244 |
+
integer `value_type` of at least 32 bits;
|
| 245 |
+
- `ob` is an output iterator; and
|
| 246 |
+
- `il` is a value of `initializer_list<T>`.
|
| 247 |
|
| 248 |
#### Uniform random bit generator requirements <a id="rand.req.urng">[[rand.req.urng]]</a>
|
| 249 |
|
| 250 |
A *uniform random bit generator* `g` of type `G` is a function object
|
| 251 |
returning unsigned integer values such that each value in the range of
|
| 252 |
possible results has (ideally) equal probability of being returned.
|
| 253 |
|
| 254 |
[*Note 1*: The degree to which `g`’s results approximate the ideal is
|
| 255 |
often determined statistically. — *end note*]
|
| 256 |
|
| 257 |
+
``` cpp
|
| 258 |
+
template<class G>
|
| 259 |
+
concept uniform_random_bit_generator =
|
| 260 |
+
invocable<G&> && unsigned_integral<invoke_result_t<G&>> &&
|
| 261 |
+
requires {
|
| 262 |
+
{ G::min() } -> same_as<invoke_result_t<G&>>;
|
| 263 |
+
{ G::max() } -> same_as<invoke_result_t<G&>>;
|
| 264 |
+
requires bool_constant<(G::min() < G::max())>::value;
|
| 265 |
+
};
|
| 266 |
+
```
|
| 267 |
|
| 268 |
+
Let `g` be an object of type `G`. `G` models
|
| 269 |
+
`uniform_random_bit_generator` only if
|
| 270 |
+
|
| 271 |
+
- `G::min() <= g()`,
|
| 272 |
+
- `g() <= G::max()`, and
|
| 273 |
+
- `g()` has amortized constant complexity.
|
| 274 |
+
|
| 275 |
+
A class `G` meets the *uniform random bit generator* requirements if `G`
|
| 276 |
+
models `uniform_random_bit_generator`, `invoke_result_t<G&>` is an
|
| 277 |
+
unsigned integer type [[basic.fundamental]], and `G` provides a nested
|
| 278 |
+
*typedef-name* `result_type` that denotes the same type as
|
| 279 |
+
`invoke_result_t<G&>`.
|
| 280 |
|
| 281 |
#### Random number engine requirements <a id="rand.req.eng">[[rand.req.eng]]</a>
|
| 282 |
|
| 283 |
A *random number engine* (commonly shortened to *engine*) `e` of type
|
| 284 |
`E` is a uniform random bit generator that additionally meets the
|
| 285 |
requirements (e.g., for seeding and for input/output) specified in this
|
| 286 |
+
subclause.
|
| 287 |
|
| 288 |
At any given time, `e` has a state eᵢ for some integer i ≥ 0. Upon
|
| 289 |
construction, `e` has an initial state e₀. An engine’s state may be
|
| 290 |
established via a constructor, a `seed` function, assignment, or a
|
| 291 |
suitable `operator>>`.
|
| 292 |
|
| 293 |
`E`’s specification shall define:
|
| 294 |
|
| 295 |
+
- the size of `E`’s state in multiples of the size of `result_type`,
|
| 296 |
+
given as an integral constant expression;
|
| 297 |
+
- the *transition algorithm* TA by which `e`’s state eᵢ is advanced to
|
| 298 |
+
its *successor state* eᵢ₊₁; and
|
| 299 |
+
- the *generation algorithm* GA by which an engine’s state is mapped to
|
| 300 |
+
a value of type `result_type`.
|
| 301 |
+
|
| 302 |
+
A class `E` that meets the requirements of a uniform random bit
|
| 303 |
+
generator [[rand.req.urng]] also meets the requirements of a *random
|
| 304 |
+
number engine* if the expressions shown in [[rand.req.eng]] are valid
|
| 305 |
+
and have the indicated semantics, and if `E` also meets all other
|
| 306 |
+
requirements of this subclause [[rand.req.eng]]. In that Table and
|
| 307 |
+
throughout this subclause:
|
| 308 |
+
|
| 309 |
+
- `T` is the type named by `E`’s associated `result_type`;
|
| 310 |
+
- `e` is a value of `E`, `v` is an lvalue of `E`, `x` and `y` are
|
| 311 |
+
(possibly `const`) values of `E`;
|
| 312 |
+
- `s` is a value of `T`;
|
| 313 |
+
- `q` is an lvalue meeting the requirements of a seed sequence
|
| 314 |
+
[[rand.req.seedseq]];
|
| 315 |
+
- `z` is a value of type `unsigned long long`;
|
| 316 |
+
- `os` is an lvalue of the type of some class template specialization
|
| 317 |
+
`basic_ostream<charT,` `traits>`; and
|
| 318 |
+
- `is` is an lvalue of the type of some class template specialization
|
| 319 |
+
`basic_istream<charT,` `traits>`;
|
| 320 |
+
|
| 321 |
+
where `charT` and `traits` are constrained according to [[strings]] and
|
| 322 |
+
[[input.output]].
|
| 323 |
+
|
| 324 |
+
`E` shall meet the *Cpp17CopyConstructible* (
|
| 325 |
+
[[cpp17.copyconstructible]]) and *Cpp17CopyAssignable* (
|
| 326 |
+
[[cpp17.copyassignable]]) requirements. These operations shall each be
|
| 327 |
+
of complexity no worse than 𝑂(\text{size of state}).
|
| 328 |
|
| 329 |
#### Random number engine adaptor requirements <a id="rand.req.adapt">[[rand.req.adapt]]</a>
|
| 330 |
|
| 331 |
A *random number engine adaptor* (commonly shortened to *adaptor*) `a`
|
| 332 |
of type `A` is a random number engine that takes values produced by some
|
|
|
|
| 381 |
template<class Sseq> void seed(Sseq& q);
|
| 382 |
```
|
| 383 |
|
| 384 |
*Effects:* With `b` as the base engine, invokes `b.seed(q)`.
|
| 385 |
|
| 386 |
+
`A` shall also meet the following additional requirements:
|
| 387 |
+
|
| 388 |
+
- The complexity of each function shall not exceed the complexity of the
|
| 389 |
+
corresponding function applied to the base engine.
|
| 390 |
+
- The state of `A` shall include the state of its base engine. The size
|
| 391 |
+
of `A`’s state shall be no less than the size of the base engine.
|
| 392 |
+
- Copying `A`’s state (e.g., during copy construction or copy
|
| 393 |
+
assignment) shall include copying the state of the base engine of `A`.
|
| 394 |
+
- The textual representation of `A` shall include the textual
|
| 395 |
+
representation of its base engine.
|
| 396 |
|
| 397 |
#### Random number distribution requirements <a id="rand.req.dist">[[rand.req.dist]]</a>
|
| 398 |
|
| 399 |
A *random number distribution* (commonly shortened to *distribution*)
|
| 400 |
`d` of type `D` is a function object returning values that are
|
|
|
|
| 409 |
context by writing, for example, p(z | a,b) or P(zᵢ | a,b), to name
|
| 410 |
specific parameters, or by writing, for example, p(z |{`p`}) or
|
| 411 |
P(zᵢ |{`p`}), to denote a distribution’s parameters `p` taken as a
|
| 412 |
whole.
|
| 413 |
|
| 414 |
+
A class `D` meets the requirements of a *random number distribution* if
|
| 415 |
+
the expressions shown in [[rand.req.dist]] are valid and have the
|
| 416 |
+
indicated semantics, and if `D` and its associated types also meet all
|
| 417 |
+
other requirements of this subclause [[rand.req.dist]]. In that Table
|
| 418 |
+
and throughout this subclause,
|
| 419 |
|
| 420 |
+
- `T` is the type named by `D`’s associated `result_type`;
|
| 421 |
+
- `P` is the type named by `D`’s associated `param_type`;
|
| 422 |
+
- `d` is a value of `D`, and `x` and `y` are (possibly `const`) values
|
| 423 |
+
of `D`;
|
| 424 |
+
- `glb` and `lub` are values of `T` respectively corresponding to the
|
| 425 |
+
greatest lower bound and the least upper bound on the values
|
| 426 |
+
potentially returned by `d`’s `operator()`, as determined by the
|
| 427 |
+
current values of `d`’s parameters;
|
| 428 |
+
- `p` is a (possibly `const`) value of `P`;
|
| 429 |
+
- `g`, `g1`, and `g2` are lvalues of a type meeting the requirements of
|
| 430 |
+
a uniform random bit generator [[rand.req.urng]];
|
| 431 |
+
- `os` is an lvalue of the type of some class template specialization
|
| 432 |
+
`basic_ostream<charT,` `traits>`; and
|
| 433 |
+
- `is` is an lvalue of the type of some class template specialization
|
| 434 |
+
`basic_istream<charT,` `traits>`;
|
| 435 |
|
| 436 |
+
where `charT` and `traits` are constrained according to [[strings]] and
|
| 437 |
+
[[input.output]].
|
| 438 |
+
|
| 439 |
+
`D` shall meet the *Cpp17CopyConstructible* (
|
| 440 |
+
[[cpp17.copyconstructible]]) and *Cpp17CopyAssignable* (
|
| 441 |
+
[[cpp17.copyassignable]]) requirements.
|
| 442 |
|
| 443 |
The sequence of numbers produced by repeated invocations of `d(g)` shall
|
| 444 |
be independent of any invocation of `os << d` or of any `const` member
|
| 445 |
function of `D` between any of the invocations `d(g)`.
|
| 446 |
|
|
|
|
| 453 |
It is unspecified whether `D::param_type` is declared as a (nested)
|
| 454 |
`class` or via a `typedef`. In this subclause [[rand]], declarations of
|
| 455 |
`D::param_type` are in the form of `typedef`s for convenience of
|
| 456 |
exposition only.
|
| 457 |
|
| 458 |
+
`P` shall meet the *Cpp17CopyConstructible* (
|
| 459 |
+
[[cpp17.copyconstructible]]), *Cpp17CopyAssignable* (
|
| 460 |
+
[[cpp17.copyassignable]]), and *Cpp17EqualityComparable* (
|
| 461 |
+
[[cpp17.equalitycomparable]]) requirements.
|
| 462 |
|
| 463 |
For each of the constructors of `D` taking arguments corresponding to
|
| 464 |
parameters of the distribution, `P` shall have a corresponding
|
| 465 |
constructor subject to the same requirements and taking arguments
|
| 466 |
identical in number, type, and default values. Moreover, for each of the
|
|
|
|
| 472 |
|
| 473 |
``` cpp
|
| 474 |
using distribution_type = D;
|
| 475 |
```
|
| 476 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
### Random number engine class templates <a id="rand.eng">[[rand.eng]]</a>
|
| 478 |
|
| 479 |
+
Each type instantiated from a class template specified in this
|
| 480 |
+
subclause [[rand.eng]] meets the requirements of a random number engine
|
| 481 |
+
[[rand.req.eng]] type.
|
| 482 |
|
| 483 |
Except where specified otherwise, the complexity of each function
|
| 484 |
+
specified in this subclause [[rand.eng]] is constant.
|
| 485 |
|
| 486 |
+
Except where specified otherwise, no function described in this
|
| 487 |
+
subclause [[rand.eng]] throws an exception.
|
| 488 |
|
| 489 |
+
Every function described in this subclause [[rand.eng]] that has a
|
| 490 |
function parameter `q` of type `Sseq&` for a template type parameter
|
| 491 |
named `Sseq` that is different from type `seed_seq` throws what and when
|
| 492 |
the invocation of `q.generate` throws.
|
| 493 |
|
| 494 |
+
Descriptions are provided in this subclause [[rand.eng]] only for
|
| 495 |
+
engine operations that are not described in [[rand.req.eng]] or for
|
| 496 |
+
operations where there is additional semantic information. In
|
| 497 |
+
particular, declarations for copy constructors, for copy assignment
|
| 498 |
+
operators, for streaming operators, and for equality and inequality
|
| 499 |
+
operators are not shown in the synopses.
|
| 500 |
|
| 501 |
+
Each template specified in this subclause [[rand.eng]] requires one or
|
| 502 |
more relationships, involving the value(s) of its non-type template
|
| 503 |
parameter(s), to hold. A program instantiating any of these templates is
|
| 504 |
ill-formed if any such required relationship fails to hold.
|
| 505 |
|
| 506 |
For every random number engine and for every random number engine
|
| 507 |
+
adaptor `X` defined in this subclause [[rand.eng]] and in subclause
|
| 508 |
[[rand.adapt]]:
|
| 509 |
|
| 510 |
- if the constructor
|
| 511 |
``` cpp
|
| 512 |
template<class Sseq> explicit X(Sseq& q);
|
|
|
|
| 550 |
static constexpr result_type min() { return c == 0u ? 1u: 0u; }
|
| 551 |
static constexpr result_type max() { return m - 1u; }
|
| 552 |
static constexpr result_type default_seed = 1u;
|
| 553 |
|
| 554 |
// constructors and seeding functions
|
| 555 |
+
linear_congruential_engine() : linear_congruential_engine(default_seed) {}
|
| 556 |
+
explicit linear_congruential_engine(result_type s);
|
| 557 |
template<class Sseq> explicit linear_congruential_engine(Sseq& q);
|
| 558 |
void seed(result_type s = default_seed);
|
| 559 |
template<class Sseq> void seed(Sseq& q);
|
| 560 |
|
| 561 |
// generating functions
|
|
|
|
| 563 |
void discard(unsigned long long z);
|
| 564 |
};
|
| 565 |
```
|
| 566 |
|
| 567 |
If the template parameter `m` is 0, the modulus m used throughout this
|
| 568 |
+
subclause [[rand.eng.lcong]] is `numeric_limits<result_type>::max()`
|
| 569 |
+
plus 1.
|
| 570 |
|
| 571 |
[*Note 1*: m need not be representable as a value of type
|
| 572 |
`result_type`. — *end note*]
|
| 573 |
|
| 574 |
If the template parameter `m` is not 0, the following relations shall
|
| 575 |
hold: `a < m` and `c < m`.
|
| 576 |
|
| 577 |
The textual representation consists of the value of xᵢ.
|
| 578 |
|
| 579 |
``` cpp
|
| 580 |
+
explicit linear_congruential_engine(result_type s);
|
| 581 |
```
|
| 582 |
|
| 583 |
+
*Effects:* If c mod m is 0 and `s` mod m is 0, sets the engine’s
|
| 584 |
+
state to 1, otherwise sets the engine’s state to `s` mod m.
|
|
|
|
| 585 |
|
| 586 |
``` cpp
|
| 587 |
template<class Sseq> explicit linear_congruential_engine(Sseq& q);
|
| 588 |
```
|
| 589 |
|
| 590 |
+
*Effects:* With $k = \left\lceil \frac{\log_2 m}{32} \right\rceil$ and a
|
| 591 |
+
an array (or equivalent) of length k + 3, invokes
|
| 592 |
+
`q.generate(`a + 0`, `a + k + 3`)` and then computes
|
| 593 |
+
$S = \left(\sum_{j = 0}^{k - 1} a_{j + 3} \cdot 2^{32j} \right) \bmod m$.
|
| 594 |
+
If c mod m is 0 and S is 0, sets the engine’s state to 1, else sets
|
| 595 |
+
the engine’s state to S.
|
|
|
|
|
|
|
| 596 |
|
| 597 |
#### Class template `mersenne_twister_engine` <a id="rand.eng.mers">[[rand.eng.mers]]</a>
|
| 598 |
|
| 599 |
A `mersenne_twister_engine` random number engine[^2] produces unsigned
|
| 600 |
integer random numbers in the closed interval [0,2ʷ-1]. The state xᵢ of
|
|
|
|
| 604 |
|
| 605 |
The transition algorithm employs a twisted generalized feedback shift
|
| 606 |
register defined by shift values n and m, a twist value r, and a
|
| 607 |
conditional xor-mask a. To improve the uniformity of the result, the
|
| 608 |
bits of the raw shift register are additionally *tempered* (i.e.,
|
| 609 |
+
scrambled) according to a bit-scrambling matrix defined by values u, d,
|
| 610 |
+
s, b, t, c, and ℓ.
|
| 611 |
|
| 612 |
The state transition is performed as follows:
|
| 613 |
|
| 614 |
+
- Concatenate the upper w-r bits of Xᵢ₋ₙ with the lower r bits of
|
| 615 |
+
$X_{i+1-n}$ to obtain an unsigned integer value Y.
|
| 616 |
+
- With $\alpha = a \cdot (Y \bitand 1)$, set Xᵢ to
|
| 617 |
+
$X_{i+m-n} \xor (Y \rightshift 1) \xor \alpha$.
|
| 618 |
+
|
| 619 |
The sequence X is initialized with the help of an initialization
|
| 620 |
multiplier f.
|
| 621 |
|
| 622 |
The generation algorithm determines the unsigned integer values
|
| 623 |
z₁, z₂, z₃, z₄ as follows, then delivers z₄ as its result:
|
| 624 |
|
| 625 |
+
- Let $z_1 = X_i \xor \bigl(( X_i \rightshift u ) \bitand d\bigr)$.
|
| 626 |
+
- Let $z_2 = z_1 \xor \bigl( (z_1 \leftshift{w} s) \bitand b \bigr)$.
|
| 627 |
+
- Let $z_3 = z_2 \xor \bigl( (z_2 \leftshift{w} t) \bitand c \bigr)$.
|
| 628 |
+
- Let $z_4 = z_3 \xor ( z_3 \rightshift \ell )$.
|
| 629 |
+
|
| 630 |
``` cpp
|
| 631 |
template<class UIntType, size_t w, size_t n, size_t m, size_t r,
|
| 632 |
UIntType a, size_t u, UIntType d, size_t s,
|
| 633 |
UIntType b, size_t t,
|
| 634 |
UIntType c, size_t l, UIntType f>
|
|
|
|
| 654 |
static constexpr result_type min() { return 0; }
|
| 655 |
static constexpr result_type max() { return 2^w - 1; }
|
| 656 |
static constexpr result_type default_seed = 5489u;
|
| 657 |
|
| 658 |
// constructors and seeding functions
|
| 659 |
+
mersenne_twister_engine() : mersenne_twister_engine(default_seed) {}
|
| 660 |
+
explicit mersenne_twister_engine(result_type value);
|
| 661 |
template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
|
| 662 |
void seed(result_type value = default_seed);
|
| 663 |
template<class Sseq> void seed(Sseq& q);
|
| 664 |
|
| 665 |
// generating functions
|
|
|
|
| 673 |
`w <= numeric_limits<UIntType>::digits`, `a <= (1u<<w) - 1u`,
|
| 674 |
`b <= (1u<<w) - 1u`, `c <= (1u<<w) - 1u`, `d <= (1u<<w) - 1u`, and
|
| 675 |
`f <= (1u<<w) - 1u`.
|
| 676 |
|
| 677 |
The textual representation of xᵢ consists of the values of
|
| 678 |
+
$X_{i - n}, \dotsc, X_{i - 1}$, in that order.
|
| 679 |
|
| 680 |
``` cpp
|
| 681 |
+
explicit mersenne_twister_engine(result_type value);
|
| 682 |
```
|
| 683 |
|
| 684 |
+
*Effects:* Sets X₋ₙ to `value` mod 2ʷ. Then, iteratively for
|
| 685 |
+
i = 1 - n, …, -1, sets Xᵢ to $$%
|
| 686 |
\bigl[f \cdot
|
| 687 |
\bigl(X_{i-1} \xor \bigl(X_{i-1} \rightshift (w-2)\bigr)
|
| 688 |
\bigr)
|
| 689 |
+ i \bmod n
|
| 690 |
\bigr] \bmod 2^w
|
|
|
|
| 694 |
|
| 695 |
``` cpp
|
| 696 |
template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
|
| 697 |
```
|
| 698 |
|
| 699 |
+
*Effects:* With k = ⌈ w / 32 ⌉ and a an array (or equivalent) of length
|
| 700 |
+
n ⋅ k, invokes `q.generate(`a+0`, `a+n ⋅ k`)` and then, iteratively for
|
| 701 |
+
i = -n,…,-1, sets Xᵢ to
|
|
|
|
| 702 |
$\left(\sum_{j=0}^{k-1}a_{k(i+n)+j} \cdot 2^{32j} \right) \bmod 2^w$.
|
| 703 |
Finally, if the most significant w-r bits of X₋ₙ are zero, and if each
|
| 704 |
of the other resulting Xᵢ is 0, changes X₋ₙ to 2ʷ⁻¹.
|
| 705 |
|
| 706 |
#### Class template `subtract_with_carry_engine` <a id="rand.eng.sub">[[rand.eng.sub]]</a>
|
|
|
|
| 714 |
additionally consists of an integer c (known as the *carry*) whose value
|
| 715 |
is either 0 or 1.
|
| 716 |
|
| 717 |
The state transition is performed as follows:
|
| 718 |
|
| 719 |
+
- Let Y = Xᵢ₋ₛ - Xᵢ₋ᵣ - c.
|
| 720 |
+
- Set Xᵢ to y = Y mod m. Set c to 1 if Y < 0, otherwise set c to 0.
|
| 721 |
+
|
| 722 |
[*Note 1*: This algorithm corresponds to a modular linear function of
|
| 723 |
the form TA(xᵢ) = (a ⋅ xᵢ) mod b, where b is of the form mʳ - mˢ + 1
|
| 724 |
and a = b - (b - 1) / m. — *end note*]
|
| 725 |
|
| 726 |
The generation algorithm is given by GA(xᵢ) = y, where y is the value
|
|
|
|
| 740 |
static constexpr result_type min() { return 0; }
|
| 741 |
static constexpr result_type max() { return m - 1; }
|
| 742 |
static constexpr result_type default_seed = 19780503u;
|
| 743 |
|
| 744 |
// constructors and seeding functions
|
| 745 |
+
subtract_with_carry_engine() : subtract_with_carry_engine(default_seed) {}
|
| 746 |
+
explicit subtract_with_carry_engine(result_type value);
|
| 747 |
template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
|
| 748 |
void seed(result_type value = default_seed);
|
| 749 |
template<class Sseq> void seed(Sseq& q);
|
| 750 |
|
| 751 |
// generating functions
|
|
|
|
| 759 |
|
| 760 |
The textual representation consists of the values of Xᵢ₋ᵣ, …, Xᵢ₋₁, in
|
| 761 |
that order, followed by c.
|
| 762 |
|
| 763 |
``` cpp
|
| 764 |
+
explicit subtract_with_carry_engine(result_type value);
|
| 765 |
```
|
| 766 |
|
| 767 |
+
*Effects:* Sets the values of X₋ᵣ, …, X₋₁, in that order, as specified
|
| 768 |
+
below. If X₋₁ is then 0, sets c to 1; otherwise sets c to 0.
|
|
|
|
| 769 |
|
| 770 |
To set the values Xₖ, first construct `e`, a
|
| 771 |
`linear_congruential_engine` object, as if by the following definition:
|
| 772 |
|
| 773 |
``` cpp
|
|
|
|
| 783 |
|
| 784 |
``` cpp
|
| 785 |
template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
|
| 786 |
```
|
| 787 |
|
| 788 |
+
*Effects:* With k = ⌈ w / 32 ⌉ and a an array (or equivalent) of length
|
| 789 |
+
r ⋅ k, invokes `q.generate(`a + 0`, `a + r ⋅ k`)` and then, iteratively
|
| 790 |
+
for i = -r, …, -1, sets Xᵢ to
|
|
|
|
| 791 |
$\left(\sum_{j=0}^{k-1}a_{k(i+r)+j} \cdot 2^{32j} \right) \bmod m$. If
|
| 792 |
X₋₁ is then 0, sets c to 1; otherwise sets c to 0.
|
| 793 |
|
| 794 |
### Random number engine adaptor class templates <a id="rand.adapt">[[rand.adapt]]</a>
|
| 795 |
|
| 796 |
#### In general <a id="rand.adapt.general">[[rand.adapt.general]]</a>
|
| 797 |
|
| 798 |
+
Each type instantiated from a class template specified in this
|
| 799 |
+
subclause [[rand.adapt]] meets the requirements of a random number
|
| 800 |
+
engine adaptor [[rand.req.adapt]] type.
|
| 801 |
|
| 802 |
Except where specified otherwise, the complexity of each function
|
| 803 |
+
specified in this subclause [[rand.adapt]] is constant.
|
| 804 |
|
| 805 |
+
Except where specified otherwise, no function described in this
|
| 806 |
+
subclause [[rand.adapt]] throws an exception.
|
| 807 |
|
| 808 |
+
Every function described in this subclause [[rand.adapt]] that has a
|
| 809 |
function parameter `q` of type `Sseq&` for a template type parameter
|
| 810 |
named `Sseq` that is different from type `seed_seq` throws what and when
|
| 811 |
the invocation of `q.generate` throws.
|
| 812 |
|
| 813 |
+
Descriptions are provided in this subclause [[rand.adapt]] only for
|
| 814 |
+
adaptor operations that are not described in subclause
|
| 815 |
+
[[rand.req.adapt]] or for operations where there is additional semantic
|
| 816 |
+
information. In particular, declarations for copy constructors, for copy
|
| 817 |
+
assignment operators, for streaming operators, and for equality and
|
| 818 |
+
inequality operators are not shown in the synopses.
|
| 819 |
|
| 820 |
+
Each template specified in this subclause [[rand.adapt]] requires one
|
| 821 |
+
or more relationships, involving the value(s) of its non-type template
|
| 822 |
parameter(s), to hold. A program instantiating any of these templates is
|
| 823 |
ill-formed if any such required relationship fails to hold.
|
| 824 |
|
| 825 |
#### Class template `discard_block_engine` <a id="rand.adapt.disc">[[rand.adapt.disc]]</a>
|
| 826 |
|
|
|
|
| 878 |
The following relations shall hold: `0 < r` and `r <= p`.
|
| 879 |
|
| 880 |
The textual representation consists of the textual representation of `e`
|
| 881 |
followed by the value of `n`.
|
| 882 |
|
| 883 |
+
In addition to its behavior pursuant to subclause [[rand.req.adapt]],
|
| 884 |
each constructor that is not a copy constructor sets `n` to 0.
|
| 885 |
|
| 886 |
#### Class template `independent_bits_engine` <a id="rand.adapt.ibits">[[rand.adapt.ibits]]</a>
|
| 887 |
|
| 888 |
An `independent_bits_engine` random number engine adaptor combines
|
|
|
|
| 893 |
e’s state.
|
| 894 |
|
| 895 |
The transition and generation algorithms are described in terms of the
|
| 896 |
following integral constants:
|
| 897 |
|
| 898 |
+
- Let R = `e.max() - e.min() + 1` and m = ⌊ log₂ R ⌋.
|
| 899 |
+
- With n as determined below, let w₀ = ⌊ w / n ⌋, n₀ = n - w mod n,
|
| 900 |
+
$y_0 = 2^{w_0} \left\lfloor R / 2^{w_0} \right\rfloor$, and
|
| 901 |
+
$y_1 = 2^{w_0 + 1} \left\lfloor R / 2^{w_0 + 1} \right\rfloor$.
|
| 902 |
+
- Let n = ⌈ w / m ⌉ if and only if the relation R - y₀ ≤ ⌊ y₀ / n ⌋
|
| 903 |
+
holds as a result. Otherwise let n = 1 + ⌈ w / m ⌉.
|
| 904 |
+
|
| 905 |
[*Note 1*: The relation w = n₀ w₀ + (n - n₀)(w₀ + 1) always
|
| 906 |
holds. — *end note*]
|
| 907 |
|
| 908 |
The transition algorithm is carried out by invoking `e()` as often as
|
| 909 |
needed to obtain n₀ values less than y₀ + `e.min()` and n - n₀ values
|
|
|
|
| 975 |
size of the state is the size of e’s state plus k + 1.
|
| 976 |
|
| 977 |
The transition algorithm permutes the values produced by e. The state
|
| 978 |
transition is performed as follows:
|
| 979 |
|
| 980 |
+
- Calculate an integer $j = \left\lfloor \frac{k \cdot (Y - e_{\min})}
|
| 981 |
+
{e_{\max} - e_{\min} +1}
|
| 982 |
+
\right\rfloor$ .
|
| 983 |
+
- Set Y to Vⱼ and then set Vⱼ to `e()`.
|
| 984 |
+
|
| 985 |
The generation algorithm yields the last value of `Y` produced while
|
| 986 |
advancing `e`’s state as described above.
|
| 987 |
|
| 988 |
``` cpp
|
| 989 |
template<class Engine, size_t k>
|
|
|
|
| 1024 |
The following relation shall hold: `0 < k`.
|
| 1025 |
|
| 1026 |
The textual representation consists of the textual representation of
|
| 1027 |
`e`, followed by the `k` values of V, followed by the value of Y.
|
| 1028 |
|
| 1029 |
+
In addition to its behavior pursuant to subclause [[rand.req.adapt]],
|
| 1030 |
each constructor that is not a copy constructor initializes
|
| 1031 |
`V[0]`, …, `V[k-1]` and Y, in that order, with values returned by
|
| 1032 |
successive invocations of `e()`.
|
| 1033 |
|
| 1034 |
### Engines and engine adaptors with predefined parameters <a id="rand.predef">[[rand.predef]]</a>
|
| 1035 |
|
| 1036 |
``` cpp
|
| 1037 |
using minstd_rand0 =
|
| 1038 |
+
linear_congruential_engine<uint_fast32_t, 16'807, 0, 2'147'483'647>;
|
| 1039 |
```
|
| 1040 |
|
| 1041 |
+
*Required behavior:* The 10000ᵗʰ consecutive invocation of a
|
| 1042 |
+
default-constructed object of type `minstd_rand0` produces the value
|
| 1043 |
+
1043618065.
|
| 1044 |
|
| 1045 |
``` cpp
|
| 1046 |
using minstd_rand =
|
| 1047 |
+
linear_congruential_engine<uint_fast32_t, 48'271, 0, 2'147'483'647>;
|
| 1048 |
```
|
| 1049 |
|
| 1050 |
+
*Required behavior:* The 10000ᵗʰ consecutive invocation of a
|
| 1051 |
+
default-constructed object of type `minstd_rand` produces the value
|
| 1052 |
399268537.
|
| 1053 |
|
| 1054 |
``` cpp
|
| 1055 |
using mt19937 =
|
| 1056 |
+
mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
|
| 1057 |
+
0x9908'b0df, 11, 0xffff'ffff, 7, 0x9d2c'5680, 15, 0xefc6'0000, 18, 1'812'433'253>;
|
| 1058 |
```
|
| 1059 |
|
| 1060 |
+
*Required behavior:* The 10000ᵗʰ consecutive invocation of a
|
| 1061 |
+
default-constructed object of type `mt19937` produces the value
|
| 1062 |
4123659995.
|
| 1063 |
|
| 1064 |
``` cpp
|
| 1065 |
using mt19937_64 =
|
| 1066 |
+
mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
|
| 1067 |
+
0xb502'6f5a'a966'19e9, 29, 0x5555'5555'5555'5555, 17,
|
| 1068 |
+
0x71d6'7fff'eda6'0000, 37, 0xfff7'eee0'0000'0000, 43, 6'364'136'223'846'793'005>;
|
|
|
|
|
|
|
|
|
|
| 1069 |
```
|
| 1070 |
|
| 1071 |
+
*Required behavior:* The 10000ᵗʰ consecutive invocation of a
|
| 1072 |
+
default-constructed object of type `mt19937_64` produces the value
|
| 1073 |
9981545732273789042.
|
| 1074 |
|
| 1075 |
``` cpp
|
| 1076 |
using ranlux24_base =
|
| 1077 |
subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>;
|
| 1078 |
```
|
| 1079 |
|
| 1080 |
+
*Required behavior:* The 10000ᵗʰ consecutive invocation of a
|
| 1081 |
+
default-constructed object of type `ranlux24_base` produces the value
|
| 1082 |
+
7937952.
|
| 1083 |
|
| 1084 |
``` cpp
|
| 1085 |
using ranlux48_base =
|
| 1086 |
subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>;
|
| 1087 |
```
|
| 1088 |
|
| 1089 |
+
*Required behavior:* The 10000ᵗʰ consecutive invocation of a
|
| 1090 |
+
default-constructed object of type `ranlux48_base` produces the value
|
| 1091 |
+
61839128582725.
|
| 1092 |
|
| 1093 |
``` cpp
|
| 1094 |
using ranlux24 = discard_block_engine<ranlux24_base, 223, 23>;
|
| 1095 |
```
|
| 1096 |
|
| 1097 |
+
*Required behavior:* The 10000ᵗʰ consecutive invocation of a
|
| 1098 |
+
default-constructed object of type `ranlux24` produces the value
|
| 1099 |
9901578.
|
| 1100 |
|
| 1101 |
``` cpp
|
| 1102 |
using ranlux48 = discard_block_engine<ranlux48_base, 389, 11>;
|
| 1103 |
```
|
| 1104 |
|
| 1105 |
+
*Required behavior:* The 10000ᵗʰ consecutive invocation of a
|
| 1106 |
+
default-constructed object of type `ranlux48` produces the value
|
| 1107 |
249142670248501.
|
| 1108 |
|
| 1109 |
``` cpp
|
| 1110 |
using knuth_b = shuffle_order_engine<minstd_rand0,256>;
|
| 1111 |
```
|
| 1112 |
|
| 1113 |
+
*Required behavior:* The 10000ᵗʰ consecutive invocation of a
|
| 1114 |
+
default-constructed object of type `knuth_b` produces the value
|
| 1115 |
1112339016.
|
| 1116 |
|
| 1117 |
``` cpp
|
| 1118 |
using default_random_engine = implementation-defined;
|
| 1119 |
```
|
|
|
|
| 1146 |
// generator characteristics
|
| 1147 |
static constexpr result_type min() { return numeric_limits<result_type>::min(); }
|
| 1148 |
static constexpr result_type max() { return numeric_limits<result_type>::max(); }
|
| 1149 |
|
| 1150 |
// constructors
|
| 1151 |
+
random_device() : random_device(implementation-defined) {}
|
| 1152 |
+
explicit random_device(const string& token);
|
| 1153 |
|
| 1154 |
// generating functions
|
| 1155 |
result_type operator()();
|
| 1156 |
|
| 1157 |
// property functions
|
|
|
|
| 1162 |
void operator=(const random_device&) = delete;
|
| 1163 |
};
|
| 1164 |
```
|
| 1165 |
|
| 1166 |
``` cpp
|
| 1167 |
+
explicit random_device(const string& token);
|
| 1168 |
```
|
| 1169 |
|
| 1170 |
+
*Remarks:* The semantics of the `token` parameter and the token value
|
| 1171 |
+
used by the default constructor are *implementation-defined*. [^3]
|
|
|
|
| 1172 |
|
| 1173 |
*Throws:* A value of an *implementation-defined* type derived from
|
| 1174 |
`exception` if the `random_device` could not be initialized.
|
| 1175 |
|
| 1176 |
``` cpp
|
|
|
|
| 1184 |
``` cpp
|
| 1185 |
result_type operator()();
|
| 1186 |
```
|
| 1187 |
|
| 1188 |
*Returns:* A nondeterministic random value, uniformly distributed
|
| 1189 |
+
between `min()` and `max()` (inclusive). It is *implementation-defined*
|
| 1190 |
how these values are generated.
|
| 1191 |
|
| 1192 |
*Throws:* A value of an *implementation-defined* type derived from
|
| 1193 |
`exception` if a random number could not be obtained.
|
| 1194 |
|
|
|
|
| 1229 |
|
| 1230 |
``` cpp
|
| 1231 |
seed_seq();
|
| 1232 |
```
|
| 1233 |
|
| 1234 |
+
*Ensures:* `v.empty()` is `true`.
|
|
|
|
| 1235 |
|
| 1236 |
*Throws:* Nothing.
|
| 1237 |
|
| 1238 |
``` cpp
|
| 1239 |
template<class T>
|
| 1240 |
seed_seq(initializer_list<T> il);
|
| 1241 |
```
|
| 1242 |
|
| 1243 |
+
*Mandates:* `T` is an integer type.
|
| 1244 |
|
| 1245 |
*Effects:* Same as `seed_seq(il.begin(), il.end())`.
|
| 1246 |
|
| 1247 |
``` cpp
|
| 1248 |
template<class InputIterator>
|
| 1249 |
seed_seq(InputIterator begin, InputIterator end);
|
| 1250 |
```
|
| 1251 |
|
| 1252 |
+
*Mandates:* `iterator_traits<InputIterator>::value_type` is an integer
|
|
|
|
|
|
|
| 1253 |
type.
|
| 1254 |
|
| 1255 |
+
*Preconditions:* `InputIterator` meets the *Cpp17InputIterator*
|
| 1256 |
+
requirements [[input.iterators]].
|
| 1257 |
+
|
| 1258 |
+
*Effects:* Initializes `v` by the following algorithm:
|
| 1259 |
|
| 1260 |
``` cpp
|
| 1261 |
for (InputIterator s = begin; s != end; ++s)
|
| 1262 |
v.push_back((*s) mod 2³²);
|
| 1263 |
```
|
|
|
|
| 1265 |
``` cpp
|
| 1266 |
template<class RandomAccessIterator>
|
| 1267 |
void generate(RandomAccessIterator begin, RandomAccessIterator end);
|
| 1268 |
```
|
| 1269 |
|
| 1270 |
+
*Mandates:* `iterator_traits<RandomAccessIterator>::value_type` is an
|
|
|
|
|
|
|
| 1271 |
unsigned integer type capable of accommodating 32-bit quantities.
|
| 1272 |
|
| 1273 |
+
*Preconditions:* `RandomAccessIterator` meets the
|
| 1274 |
+
*Cpp17RandomAccessIterator* requirements [[random.access.iterators]] and
|
| 1275 |
+
the requirements of a mutable iterator.
|
| 1276 |
+
|
| 1277 |
*Effects:* Does nothing if `begin == end`. Otherwise, with
|
| 1278 |
s = `v.size()` and n = `end` - `begin`, fills the supplied range
|
| 1279 |
[`begin`,`end`) according to the following algorithm in which each
|
| 1280 |
operation is to be carried out modulo 2³², each indexing operator
|
| 1281 |
applied to `begin` is to be taken modulo n, and T(x) is defined as
|
| 1282 |
+
$x \xor (x \rightshift 27)$:
|
| 1283 |
+
|
| 1284 |
+
- By way of initialization, set each element of the range to the value
|
| 1285 |
+
`0x8b8b8b8b`. Additionally, for use in subsequent steps, let
|
| 1286 |
+
p = (n - t) / 2 and let q = p + t, where $$%
|
| 1287 |
+
t = (n \ge 623) \mbox{ ? } 11 \mbox{ : } (n \ge 68) \mbox{ ? } 7 \mbox{ : } (n \ge 39) \mbox{ ? } 5 \mbox{ : } (n \ge 7) \mbox{ ? } 3 \mbox{ : } (n - 1)/2;$$
|
| 1288 |
+
- With m as the larger of s + 1 and n, transform the elements of the
|
| 1289 |
+
range: iteratively for k = 0, …, m - 1, calculate values
|
| 1290 |
+
$$\begin{aligned}
|
| 1291 |
+
r_1 & = &
|
| 1292 |
+
1664525 \cdot T( \texttt{begin[}k\texttt{]}
|
| 1293 |
+
\xor \texttt{begin[}k+p\texttt{]}
|
| 1294 |
+
\xor \texttt{begin[}k-1 \texttt{]}
|
| 1295 |
+
)
|
| 1296 |
+
\\
|
| 1297 |
+
r_2 & = & r_1 + \left\{
|
| 1298 |
+
\begin{array}{cl}
|
| 1299 |
+
s & \mbox{, } k = 0
|
| 1300 |
+
\\
|
| 1301 |
+
k \bmod n + \texttt{v[}k-1\texttt{]} & \mbox{, } 0 < k \le s
|
| 1302 |
+
\\
|
| 1303 |
+
k \bmod n & \mbox{, } s < k
|
| 1304 |
+
\end{array}
|
| 1305 |
+
\right.
|
| 1306 |
+
|
| 1307 |
+
\end{aligned}$$ and, in order, increment `begin[`k+p`]` by r₁,
|
| 1308 |
+
increment `begin[`k+q`]` by r₂, and set `begin[`k`]` to r₂.
|
| 1309 |
+
- Transform the elements of the range again, beginning where the
|
| 1310 |
+
previous step ended: iteratively for k = m, …, m + n - 1, calculate
|
| 1311 |
+
values $$\begin{aligned}
|
| 1312 |
+
r_3 & = &
|
| 1313 |
+
1566083941 \cdot T( \texttt{begin[}k \texttt{]}
|
| 1314 |
+
+ \texttt{begin[}k+p\texttt{]}
|
| 1315 |
+
+ \texttt{begin[}k-1\texttt{]}
|
| 1316 |
+
)
|
| 1317 |
+
\\
|
| 1318 |
+
r_4 & = & r_3 - (k \bmod n)
|
| 1319 |
+
|
| 1320 |
+
\end{aligned}$$ and, in order, update `begin[`k+p`]` by xoring it with
|
| 1321 |
+
r₃, update `begin[`k+q`]` by xoring it with r₄, and set `begin[`k`]`
|
| 1322 |
+
to r₄.
|
| 1323 |
|
| 1324 |
*Throws:* What and when `RandomAccessIterator` operations of `begin` and
|
| 1325 |
`end` throw.
|
| 1326 |
|
| 1327 |
``` cpp
|
|
|
|
| 1336 |
``` cpp
|
| 1337 |
template<class OutputIterator>
|
| 1338 |
void param(OutputIterator dest) const;
|
| 1339 |
```
|
| 1340 |
|
| 1341 |
+
*Mandates:* Values of type `result_type` are
|
| 1342 |
+
writable [[iterator.requirements.general]] to `dest`.
|
| 1343 |
+
|
| 1344 |
+
*Preconditions:* `OutputIterator` meets the *Cpp17OutputIterator*
|
| 1345 |
+
requirements [[output.iterators]].
|
| 1346 |
|
| 1347 |
*Effects:* Copies the sequence of prepared 32-bit units to the given
|
| 1348 |
destination, as if by executing the following statement:
|
| 1349 |
|
| 1350 |
``` cpp
|
|
|
|
| 1353 |
|
| 1354 |
*Throws:* What and when `OutputIterator` operations of `dest` throw.
|
| 1355 |
|
| 1356 |
#### Function template `generate_canonical` <a id="rand.util.canonical">[[rand.util.canonical]]</a>
|
| 1357 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1358 |
``` cpp
|
| 1359 |
template<class RealType, size_t bits, class URBG>
|
| 1360 |
RealType generate_canonical(URBG& g);
|
| 1361 |
```
|
| 1362 |
|
|
|
|
| 1369 |
$$S = \sum_{i=0}^{k-1} (g_i - \texttt{g.min()})
|
| 1370 |
\cdot R^i$$ using arithmetic of type `RealType`.
|
| 1371 |
|
| 1372 |
*Returns:* S / Rᵏ.
|
| 1373 |
|
| 1374 |
+
[*Note 1*: 0 ≤ S / Rᵏ < 1. — *end note*]
|
| 1375 |
+
|
| 1376 |
*Throws:* What and when `g` throws.
|
| 1377 |
|
| 1378 |
+
[*Note 2*: If the values gᵢ produced by `g` are uniformly distributed,
|
| 1379 |
+
the instantiation’s results are distributed as uniformly as possible.
|
| 1380 |
+
Obtaining a value in this way can be a useful step in the process of
|
| 1381 |
+
transforming a value generated by a uniform random bit generator into a
|
| 1382 |
+
value that can be delivered by a random number
|
| 1383 |
+
distribution. — *end note*]
|
| 1384 |
+
|
| 1385 |
### Random number distribution class templates <a id="rand.dist">[[rand.dist]]</a>
|
| 1386 |
|
| 1387 |
#### In general <a id="rand.dist.general">[[rand.dist.general]]</a>
|
| 1388 |
|
| 1389 |
+
Each type instantiated from a class template specified in this
|
| 1390 |
+
subclause [[rand.dist]] meets the requirements of a random number
|
| 1391 |
+
distribution [[rand.req.dist]] type.
|
| 1392 |
|
| 1393 |
+
Descriptions are provided in this subclause [[rand.dist]] only for
|
| 1394 |
distribution operations that are not described in [[rand.req.dist]] or
|
| 1395 |
for operations where there is additional semantic information. In
|
| 1396 |
particular, declarations for copy constructors, for copy assignment
|
| 1397 |
operators, for streaming operators, and for equality and inequality
|
| 1398 |
operators are not shown in the synopses.
|
| 1399 |
|
| 1400 |
The algorithms for producing each of the specified distributions are
|
| 1401 |
*implementation-defined*.
|
| 1402 |
|
| 1403 |
The value of each probability density function p(z) and of each discrete
|
| 1404 |
+
probability function P(zᵢ) specified in this subclause is 0 everywhere
|
| 1405 |
outside its stated domain.
|
| 1406 |
|
| 1407 |
#### Uniform distributions <a id="rand.dist.uni">[[rand.dist.uni]]</a>
|
| 1408 |
|
| 1409 |
##### Class template `uniform_int_distribution` <a id="rand.dist.uni.int">[[rand.dist.uni.int]]</a>
|
| 1410 |
|
| 1411 |
A `uniform_int_distribution` random number distribution produces random
|
| 1412 |
integers i, a ≤ i ≤ b, distributed according to the constant discrete
|
| 1413 |
+
probability function $$P(i\,|\,a,b) = 1 / (b - a + 1) \text{ .}$$
|
|
|
|
|
|
|
| 1414 |
|
| 1415 |
``` cpp
|
| 1416 |
template<class IntType = int>
|
| 1417 |
class uniform_int_distribution {
|
| 1418 |
public:
|
| 1419 |
// types
|
| 1420 |
using result_type = IntType;
|
| 1421 |
using param_type = unspecified;
|
| 1422 |
|
| 1423 |
// constructors and reset functions
|
| 1424 |
+
uniform_int_distribution() : uniform_int_distribution(0) {}
|
| 1425 |
+
explicit uniform_int_distribution(IntType a, IntType b = numeric_limits<IntType>::max());
|
| 1426 |
explicit uniform_int_distribution(const param_type& parm);
|
| 1427 |
void reset();
|
| 1428 |
|
| 1429 |
// generating functions
|
| 1430 |
template<class URBG>
|
|
|
|
| 1441 |
result_type max() const;
|
| 1442 |
};
|
| 1443 |
```
|
| 1444 |
|
| 1445 |
``` cpp
|
| 1446 |
+
explicit uniform_int_distribution(IntType a, IntType b = numeric_limits<IntType>::max());
|
| 1447 |
```
|
| 1448 |
|
| 1449 |
+
*Preconditions:* `a` ≤ `b`.
|
| 1450 |
|
| 1451 |
+
*Remarks:* `a` and `b` correspond to the respective parameters of the
|
| 1452 |
+
distribution.
|
| 1453 |
|
| 1454 |
``` cpp
|
| 1455 |
result_type a() const;
|
| 1456 |
```
|
| 1457 |
|
|
|
|
| 1467 |
|
| 1468 |
##### Class template `uniform_real_distribution` <a id="rand.dist.uni.real">[[rand.dist.uni.real]]</a>
|
| 1469 |
|
| 1470 |
A `uniform_real_distribution` random number distribution produces random
|
| 1471 |
numbers x, a ≤ x < b, distributed according to the constant probability
|
| 1472 |
+
density function $$p(x\,|\,a,b) = 1 / (b - a) \text{ .}$$
|
|
|
|
|
|
|
| 1473 |
|
| 1474 |
[*Note 1*: This implies that p(x | a,b) is undefined when
|
| 1475 |
`a == b`. — *end note*]
|
| 1476 |
|
| 1477 |
``` cpp
|
|
|
|
| 1481 |
// types
|
| 1482 |
using result_type = RealType;
|
| 1483 |
using param_type = unspecified;
|
| 1484 |
|
| 1485 |
// constructors and reset functions
|
| 1486 |
+
uniform_real_distribution() : uniform_real_distribution(0.0) {}
|
| 1487 |
+
explicit uniform_real_distribution(RealType a, RealType b = 1.0);
|
| 1488 |
explicit uniform_real_distribution(const param_type& parm);
|
| 1489 |
void reset();
|
| 1490 |
|
| 1491 |
// generating functions
|
| 1492 |
template<class URBG>
|
|
|
|
| 1503 |
result_type max() const;
|
| 1504 |
};
|
| 1505 |
```
|
| 1506 |
|
| 1507 |
``` cpp
|
| 1508 |
+
explicit uniform_real_distribution(RealType a, RealType b = 1.0);
|
| 1509 |
```
|
| 1510 |
|
| 1511 |
+
*Preconditions:* `a` ≤ `b` and
|
| 1512 |
+
`b` - `a` ≤ `numeric_limits<RealType>::max()`.
|
| 1513 |
|
| 1514 |
+
*Remarks:* `a` and `b` correspond to the respective parameters of the
|
| 1515 |
+
distribution.
|
| 1516 |
|
| 1517 |
``` cpp
|
| 1518 |
result_type a() const;
|
| 1519 |
```
|
| 1520 |
|
|
|
|
| 1531 |
#### Bernoulli distributions <a id="rand.dist.bern">[[rand.dist.bern]]</a>
|
| 1532 |
|
| 1533 |
##### Class `bernoulli_distribution` <a id="rand.dist.bern.bernoulli">[[rand.dist.bern.bernoulli]]</a>
|
| 1534 |
|
| 1535 |
A `bernoulli_distribution` random number distribution produces `bool`
|
| 1536 |
+
values b distributed according to the discrete probability function
|
| 1537 |
+
$$P(b\,|\,p) = \left\{ \begin{array}{ll}
|
| 1538 |
+
p & \text{ if $b = \tcode{true}$, or} \\
|
| 1539 |
+
1 - p & \text{ if $b = \tcode{false}$.}
|
| 1540 |
+
\end{array}\right.$$
|
|
|
|
|
|
|
| 1541 |
|
| 1542 |
``` cpp
|
| 1543 |
class bernoulli_distribution {
|
| 1544 |
public:
|
| 1545 |
// types
|
| 1546 |
using result_type = bool;
|
| 1547 |
using param_type = unspecified;
|
| 1548 |
|
| 1549 |
// constructors and reset functions
|
| 1550 |
+
bernoulli_distribution() : bernoulli_distribution(0.5) {}
|
| 1551 |
+
explicit bernoulli_distribution(double p);
|
| 1552 |
explicit bernoulli_distribution(const param_type& parm);
|
| 1553 |
void reset();
|
| 1554 |
|
| 1555 |
// generating functions
|
| 1556 |
template<class URBG>
|
|
|
|
| 1566 |
result_type max() const;
|
| 1567 |
};
|
| 1568 |
```
|
| 1569 |
|
| 1570 |
``` cpp
|
| 1571 |
+
explicit bernoulli_distribution(double p);
|
| 1572 |
```
|
| 1573 |
|
| 1574 |
+
*Preconditions:* 0 ≤ `p` ≤ 1.
|
| 1575 |
|
| 1576 |
+
*Remarks:* `p` corresponds to the parameter of the distribution.
|
|
|
|
| 1577 |
|
| 1578 |
``` cpp
|
| 1579 |
double p() const;
|
| 1580 |
```
|
| 1581 |
|
|
|
|
| 1584 |
|
| 1585 |
##### Class template `binomial_distribution` <a id="rand.dist.bern.bin">[[rand.dist.bern.bin]]</a>
|
| 1586 |
|
| 1587 |
A `binomial_distribution` random number distribution produces integer
|
| 1588 |
values i ≥ 0 distributed according to the discrete probability function
|
| 1589 |
+
$$P(i\,|\,t,p) = \binom{t}{i} \cdot p^i \cdot (1-p)^{t-i} \text{ .}$$
|
|
|
|
|
|
|
|
|
|
| 1590 |
|
| 1591 |
``` cpp
|
| 1592 |
template<class IntType = int>
|
| 1593 |
class binomial_distribution {
|
| 1594 |
public:
|
| 1595 |
// types
|
| 1596 |
using result_type = IntType;
|
| 1597 |
using param_type = unspecified;
|
| 1598 |
|
| 1599 |
// constructors and reset functions
|
| 1600 |
+
binomial_distribution() : binomial_distribution(1) {}
|
| 1601 |
+
explicit binomial_distribution(IntType t, double p = 0.5);
|
| 1602 |
explicit binomial_distribution(const param_type& parm);
|
| 1603 |
void reset();
|
| 1604 |
|
| 1605 |
// generating functions
|
| 1606 |
template<class URBG>
|
|
|
|
| 1617 |
result_type max() const;
|
| 1618 |
};
|
| 1619 |
```
|
| 1620 |
|
| 1621 |
``` cpp
|
| 1622 |
+
explicit binomial_distribution(IntType t, double p = 0.5);
|
| 1623 |
```
|
| 1624 |
|
| 1625 |
+
*Preconditions:* 0 ≤ `p` ≤ 1 and 0 ≤ `t`.
|
| 1626 |
|
| 1627 |
+
*Remarks:* `t` and `p` correspond to the respective parameters of the
|
| 1628 |
+
distribution.
|
| 1629 |
|
| 1630 |
``` cpp
|
| 1631 |
IntType t() const;
|
| 1632 |
```
|
| 1633 |
|
|
|
|
| 1643 |
|
| 1644 |
##### Class template `geometric_distribution` <a id="rand.dist.bern.geo">[[rand.dist.bern.geo]]</a>
|
| 1645 |
|
| 1646 |
A `geometric_distribution` random number distribution produces integer
|
| 1647 |
values i ≥ 0 distributed according to the discrete probability function
|
| 1648 |
+
$$P(i\,|\,p) = p \cdot (1-p)^{i} \text{ .}$$
|
|
|
|
|
|
|
|
|
|
| 1649 |
|
| 1650 |
``` cpp
|
| 1651 |
template<class IntType = int>
|
| 1652 |
class geometric_distribution {
|
| 1653 |
public:
|
| 1654 |
// types
|
| 1655 |
using result_type = IntType;
|
| 1656 |
using param_type = unspecified;
|
| 1657 |
|
| 1658 |
// constructors and reset functions
|
| 1659 |
+
geometric_distribution() : geometric_distribution(0.5) {}
|
| 1660 |
+
explicit geometric_distribution(double p);
|
| 1661 |
explicit geometric_distribution(const param_type& parm);
|
| 1662 |
void reset();
|
| 1663 |
|
| 1664 |
// generating functions
|
| 1665 |
template<class URBG>
|
|
|
|
| 1675 |
result_type max() const;
|
| 1676 |
};
|
| 1677 |
```
|
| 1678 |
|
| 1679 |
``` cpp
|
| 1680 |
+
explicit geometric_distribution(double p);
|
| 1681 |
```
|
| 1682 |
|
| 1683 |
+
*Preconditions:* 0 < `p` < 1.
|
| 1684 |
|
| 1685 |
+
*Remarks:* `p` corresponds to the parameter of the distribution.
|
|
|
|
| 1686 |
|
| 1687 |
``` cpp
|
| 1688 |
double p() const;
|
| 1689 |
```
|
| 1690 |
|
|
|
|
| 1693 |
|
| 1694 |
##### Class template `negative_binomial_distribution` <a id="rand.dist.bern.negbin">[[rand.dist.bern.negbin]]</a>
|
| 1695 |
|
| 1696 |
A `negative_binomial_distribution` random number distribution produces
|
| 1697 |
random integers i ≥ 0 distributed according to the discrete probability
|
| 1698 |
+
function
|
| 1699 |
+
$$P(i\,|\,k,p) = \binom{k+i-1}{i} \cdot p^k \cdot (1-p)^i \text{ .}$$
|
|
|
|
|
|
|
| 1700 |
|
| 1701 |
[*Note 1*: This implies that P(i | k,p) is undefined when
|
| 1702 |
`p == 1`. — *end note*]
|
| 1703 |
|
| 1704 |
``` cpp
|
|
|
|
| 1708 |
// types
|
| 1709 |
using result_type = IntType;
|
| 1710 |
using param_type = unspecified;
|
| 1711 |
|
| 1712 |
// constructor and reset functions
|
| 1713 |
+
negative_binomial_distribution() : negative_binomial_distribution(1) {}
|
| 1714 |
+
explicit negative_binomial_distribution(IntType k, double p = 0.5);
|
| 1715 |
explicit negative_binomial_distribution(const param_type& parm);
|
| 1716 |
void reset();
|
| 1717 |
|
| 1718 |
// generating functions
|
| 1719 |
template<class URBG>
|
|
|
|
| 1730 |
result_type max() const;
|
| 1731 |
};
|
| 1732 |
```
|
| 1733 |
|
| 1734 |
``` cpp
|
| 1735 |
+
explicit negative_binomial_distribution(IntType k, double p = 0.5);
|
| 1736 |
```
|
| 1737 |
|
| 1738 |
+
*Preconditions:* 0 < `p` ≤ 1 and 0 < `k`.
|
| 1739 |
|
| 1740 |
+
*Remarks:* `k` and `p` correspond to the respective parameters of the
|
| 1741 |
+
distribution.
|
| 1742 |
|
| 1743 |
``` cpp
|
| 1744 |
IntType k() const;
|
| 1745 |
```
|
| 1746 |
|
|
|
|
| 1758 |
|
| 1759 |
##### Class template `poisson_distribution` <a id="rand.dist.pois.poisson">[[rand.dist.pois.poisson]]</a>
|
| 1760 |
|
| 1761 |
A `poisson_distribution` random number distribution produces integer
|
| 1762 |
values i ≥ 0 distributed according to the discrete probability function
|
| 1763 |
+
$$P(i\,|\,\mu) = \frac{e^{-\mu} \mu^{i}}{i\,!} \text{ .}$$ The
|
| 1764 |
+
distribution parameter μ is also known as this distribution’s *mean* .
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1765 |
|
| 1766 |
``` cpp
|
| 1767 |
template<class IntType = int>
|
| 1768 |
class poisson_distribution
|
| 1769 |
{
|
|
|
|
| 1771 |
// types
|
| 1772 |
using result_type = IntType;
|
| 1773 |
using param_type = unspecified;
|
| 1774 |
|
| 1775 |
// constructors and reset functions
|
| 1776 |
+
poisson_distribution() : poisson_distribution(1.0) {}
|
| 1777 |
+
explicit poisson_distribution(double mean);
|
| 1778 |
explicit poisson_distribution(const param_type& parm);
|
| 1779 |
void reset();
|
| 1780 |
|
| 1781 |
// generating functions
|
| 1782 |
template<class URBG>
|
|
|
|
| 1792 |
result_type max() const;
|
| 1793 |
};
|
| 1794 |
```
|
| 1795 |
|
| 1796 |
``` cpp
|
| 1797 |
+
explicit poisson_distribution(double mean);
|
| 1798 |
```
|
| 1799 |
|
| 1800 |
+
*Preconditions:* 0 < `mean`.
|
| 1801 |
|
| 1802 |
+
*Remarks:* `mean` corresponds to the parameter of the distribution.
|
|
|
|
| 1803 |
|
| 1804 |
``` cpp
|
| 1805 |
double mean() const;
|
| 1806 |
```
|
| 1807 |
|
|
|
|
| 1810 |
|
| 1811 |
##### Class template `exponential_distribution` <a id="rand.dist.pois.exp">[[rand.dist.pois.exp]]</a>
|
| 1812 |
|
| 1813 |
An `exponential_distribution` random number distribution produces random
|
| 1814 |
numbers x > 0 distributed according to the probability density function
|
| 1815 |
+
$$p(x\,|\,\lambda) = \lambda e^{-\lambda x} \text{ .}$$
|
|
|
|
|
|
|
|
|
|
| 1816 |
|
| 1817 |
``` cpp
|
| 1818 |
template<class RealType = double>
|
| 1819 |
class exponential_distribution {
|
| 1820 |
public:
|
| 1821 |
// types
|
| 1822 |
using result_type = RealType;
|
| 1823 |
using param_type = unspecified;
|
| 1824 |
|
| 1825 |
// constructors and reset functions
|
| 1826 |
+
exponential_distribution() : exponential_distribution(1.0) {}
|
| 1827 |
+
explicit exponential_distribution(RealType lambda);
|
| 1828 |
explicit exponential_distribution(const param_type& parm);
|
| 1829 |
void reset();
|
| 1830 |
|
| 1831 |
// generating functions
|
| 1832 |
template<class URBG>
|
|
|
|
| 1842 |
result_type max() const;
|
| 1843 |
};
|
| 1844 |
```
|
| 1845 |
|
| 1846 |
``` cpp
|
| 1847 |
+
explicit exponential_distribution(RealType lambda);
|
| 1848 |
```
|
| 1849 |
|
| 1850 |
+
*Preconditions:* 0 < `lambda`.
|
| 1851 |
|
| 1852 |
+
*Remarks:* `lambda` corresponds to the parameter of the distribution.
|
|
|
|
| 1853 |
|
| 1854 |
``` cpp
|
| 1855 |
RealType lambda() const;
|
| 1856 |
```
|
| 1857 |
|
|
|
|
| 1860 |
|
| 1861 |
##### Class template `gamma_distribution` <a id="rand.dist.pois.gamma">[[rand.dist.pois.gamma]]</a>
|
| 1862 |
|
| 1863 |
A `gamma_distribution` random number distribution produces random
|
| 1864 |
numbers x > 0 distributed according to the probability density function
|
| 1865 |
+
$$p(x\,|\,\alpha,\beta) =
|
| 1866 |
+
\frac{e^{-x/\beta}}{\beta^{\alpha} \cdot \Gamma(\alpha)} \, \cdot \, x^{\, \alpha-1}
|
| 1867 |
+
\text{ .}$$
|
|
|
|
|
|
|
| 1868 |
|
| 1869 |
``` cpp
|
| 1870 |
template<class RealType = double>
|
| 1871 |
class gamma_distribution {
|
| 1872 |
public:
|
| 1873 |
// types
|
| 1874 |
using result_type = RealType;
|
| 1875 |
using param_type = unspecified;
|
| 1876 |
|
| 1877 |
// constructors and reset functions
|
| 1878 |
+
gamma_distribution() : gamma_distribution(1.0) {}
|
| 1879 |
+
explicit gamma_distribution(RealType alpha, RealType beta = 1.0);
|
| 1880 |
explicit gamma_distribution(const param_type& parm);
|
| 1881 |
void reset();
|
| 1882 |
|
| 1883 |
// generating functions
|
| 1884 |
template<class URBG>
|
|
|
|
| 1895 |
result_type max() const;
|
| 1896 |
};
|
| 1897 |
```
|
| 1898 |
|
| 1899 |
``` cpp
|
| 1900 |
+
explicit gamma_distribution(RealType alpha, RealType beta = 1.0);
|
| 1901 |
```
|
| 1902 |
|
| 1903 |
+
*Preconditions:* 0 < `alpha` and 0 < `beta`.
|
| 1904 |
|
| 1905 |
+
*Remarks:* `alpha` and `beta` correspond to the parameters of the
|
| 1906 |
+
distribution.
|
| 1907 |
|
| 1908 |
``` cpp
|
| 1909 |
RealType alpha() const;
|
| 1910 |
```
|
| 1911 |
|
|
|
|
| 1921 |
|
| 1922 |
##### Class template `weibull_distribution` <a id="rand.dist.pois.weibull">[[rand.dist.pois.weibull]]</a>
|
| 1923 |
|
| 1924 |
A `weibull_distribution` random number distribution produces random
|
| 1925 |
numbers x ≥ 0 distributed according to the probability density function
|
| 1926 |
+
$$p(x\,|\,a,b) = \frac{a}{b}
|
|
|
|
|
|
|
| 1927 |
\cdot \left(\frac{x}{b}\right)^{a-1}
|
| 1928 |
\cdot \, \exp\left( -\left(\frac{x}{b}\right)^a\right)
|
| 1929 |
+
\text{ .}$$
|
| 1930 |
|
| 1931 |
``` cpp
|
| 1932 |
template<class RealType = double>
|
| 1933 |
class weibull_distribution {
|
| 1934 |
public:
|
| 1935 |
// types
|
| 1936 |
using result_type = RealType;
|
| 1937 |
using param_type = unspecified;
|
| 1938 |
|
| 1939 |
// constructor and reset functions
|
| 1940 |
+
weibull_distribution() : weibull_distribution(1.0) {}
|
| 1941 |
+
explicit weibull_distribution(RealType a, RealType b = 1.0);
|
| 1942 |
explicit weibull_distribution(const param_type& parm);
|
| 1943 |
void reset();
|
| 1944 |
|
| 1945 |
// generating functions
|
| 1946 |
template<class URBG>
|
|
|
|
| 1957 |
result_type max() const;
|
| 1958 |
};
|
| 1959 |
```
|
| 1960 |
|
| 1961 |
``` cpp
|
| 1962 |
+
explicit weibull_distribution(RealType a, RealType b = 1.0);
|
| 1963 |
```
|
| 1964 |
|
| 1965 |
+
*Preconditions:* 0 < `a` and 0 < `b`.
|
| 1966 |
|
| 1967 |
+
*Remarks:* `a` and `b` correspond to the respective parameters of the
|
| 1968 |
+
distribution.
|
| 1969 |
|
| 1970 |
``` cpp
|
| 1971 |
RealType a() const;
|
| 1972 |
```
|
| 1973 |
|
|
|
|
| 1983 |
|
| 1984 |
##### Class template `extreme_value_distribution` <a id="rand.dist.pois.extreme">[[rand.dist.pois.extreme]]</a>
|
| 1985 |
|
| 1986 |
An `extreme_value_distribution` random number distribution produces
|
| 1987 |
random numbers x distributed according to the probability density
|
| 1988 |
+
function[^6] $$p(x\,|\,a,b) = \frac{1}{b}
|
| 1989 |
+
\cdot \exp\left(\frac{a-x}{b} - \exp\left(\frac{a-x}{b}\right)\right)
|
| 1990 |
+
\text{ .}$$
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1991 |
|
| 1992 |
``` cpp
|
| 1993 |
template<class RealType = double>
|
| 1994 |
class extreme_value_distribution {
|
| 1995 |
public:
|
| 1996 |
// types
|
| 1997 |
using result_type = RealType;
|
| 1998 |
using param_type = unspecified;
|
| 1999 |
|
| 2000 |
// constructor and reset functions
|
| 2001 |
+
extreme_value_distribution() : extreme_value_distribution(0.0) {}
|
| 2002 |
+
explicit extreme_value_distribution(RealType a, RealType b = 1.0);
|
| 2003 |
explicit extreme_value_distribution(const param_type& parm);
|
| 2004 |
void reset();
|
| 2005 |
|
| 2006 |
// generating functions
|
| 2007 |
template<class URBG>
|
|
|
|
| 2018 |
result_type max() const;
|
| 2019 |
};
|
| 2020 |
```
|
| 2021 |
|
| 2022 |
``` cpp
|
| 2023 |
+
explicit extreme_value_distribution(RealType a, RealType b = 1.0);
|
| 2024 |
```
|
| 2025 |
|
| 2026 |
+
*Preconditions:* 0 < `b`.
|
| 2027 |
|
| 2028 |
+
*Remarks:* `a` and `b` correspond to the respective parameters of the
|
| 2029 |
+
distribution.
|
| 2030 |
|
| 2031 |
``` cpp
|
| 2032 |
RealType a() const;
|
| 2033 |
```
|
| 2034 |
|
|
|
|
| 2054 |
% e^{-(x-\mu)^2 / (2\sigma^2)}
|
| 2055 |
\exp{\left(- \, \frac{(x - \mu)^2}
|
| 2056 |
{2 \sigma^2}
|
| 2057 |
\right)
|
| 2058 |
}
|
| 2059 |
+
\text{ .}$$ The distribution parameters μ and σ are also known as this
|
| 2060 |
distribution’s *mean* and *standard deviation* .
|
| 2061 |
|
| 2062 |
``` cpp
|
| 2063 |
template<class RealType = double>
|
| 2064 |
class normal_distribution {
|
|
|
|
| 2066 |
// types
|
| 2067 |
using result_type = RealType;
|
| 2068 |
using param_type = unspecified;
|
| 2069 |
|
| 2070 |
// constructors and reset functions
|
| 2071 |
+
normal_distribution() : normal_distribution(0.0) {}
|
| 2072 |
+
explicit normal_distribution(RealType mean, RealType stddev = 1.0);
|
| 2073 |
explicit normal_distribution(const param_type& parm);
|
| 2074 |
void reset();
|
| 2075 |
|
| 2076 |
// generating functions
|
| 2077 |
template<class URBG>
|
|
|
|
| 2088 |
result_type max() const;
|
| 2089 |
};
|
| 2090 |
```
|
| 2091 |
|
| 2092 |
``` cpp
|
| 2093 |
+
explicit normal_distribution(RealType mean, RealType stddev = 1.0);
|
| 2094 |
```
|
| 2095 |
|
| 2096 |
+
*Preconditions:* 0 < `stddev`.
|
| 2097 |
|
| 2098 |
+
*Remarks:* `mean` and `stddev` correspond to the respective parameters
|
| 2099 |
+
of the distribution.
|
| 2100 |
|
| 2101 |
``` cpp
|
| 2102 |
RealType mean() const;
|
| 2103 |
```
|
| 2104 |
|
|
|
|
| 2114 |
|
| 2115 |
##### Class template `lognormal_distribution` <a id="rand.dist.norm.lognormal">[[rand.dist.norm.lognormal]]</a>
|
| 2116 |
|
| 2117 |
A `lognormal_distribution` random number distribution produces random
|
| 2118 |
numbers x > 0 distributed according to the probability density function
|
| 2119 |
+
$$p(x\,|\,m,s) = \frac{1}{s x \sqrt{2 \pi}}
|
| 2120 |
+
\cdot \exp{\left(-\frac{(\ln{x} - m)^2}{2 s^2}\right)}
|
| 2121 |
+
\text{ .}$$
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2122 |
|
| 2123 |
``` cpp
|
| 2124 |
template<class RealType = double>
|
| 2125 |
class lognormal_distribution {
|
| 2126 |
public:
|
| 2127 |
// types
|
| 2128 |
using result_type = RealType;
|
| 2129 |
using param_type = unspecified;
|
| 2130 |
|
| 2131 |
// constructor and reset functions
|
| 2132 |
+
lognormal_distribution() : lognormal_distribution(0.0) {}
|
| 2133 |
+
explicit lognormal_distribution(RealType m, RealType s = 1.0);
|
| 2134 |
explicit lognormal_distribution(const param_type& parm);
|
| 2135 |
void reset();
|
| 2136 |
|
| 2137 |
// generating functions
|
| 2138 |
template<class URBG>
|
|
|
|
| 2149 |
result_type max() const;
|
| 2150 |
};
|
| 2151 |
```
|
| 2152 |
|
| 2153 |
``` cpp
|
| 2154 |
+
explicit lognormal_distribution(RealType m, RealType s = 1.0);
|
| 2155 |
```
|
| 2156 |
|
| 2157 |
+
*Preconditions:* 0 < `s`.
|
| 2158 |
|
| 2159 |
+
*Remarks:* `m` and `s` correspond to the respective parameters of the
|
| 2160 |
+
distribution.
|
| 2161 |
|
| 2162 |
``` cpp
|
| 2163 |
RealType m() const;
|
| 2164 |
```
|
| 2165 |
|
|
|
|
| 2175 |
|
| 2176 |
##### Class template `chi_squared_distribution` <a id="rand.dist.norm.chisq">[[rand.dist.norm.chisq]]</a>
|
| 2177 |
|
| 2178 |
A `chi_squared_distribution` random number distribution produces random
|
| 2179 |
numbers x > 0 distributed according to the probability density function
|
| 2180 |
+
$$p(x\,|\,n) = \frac{x^{(n/2)-1} \cdot e^{-x/2}}{\Gamma(n/2) \cdot 2^{n/2}} \text{ .}$$
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2181 |
|
| 2182 |
``` cpp
|
| 2183 |
template<class RealType = double>
|
| 2184 |
class chi_squared_distribution {
|
| 2185 |
public:
|
| 2186 |
// types
|
| 2187 |
using result_type = RealType;
|
| 2188 |
using param_type = unspecified;
|
| 2189 |
|
| 2190 |
// constructor and reset functions
|
| 2191 |
+
chi_squared_distribution() : chi_squared_distribution(1.0) {}
|
| 2192 |
+
explicit chi_squared_distribution(RealType n);
|
| 2193 |
explicit chi_squared_distribution(const param_type& parm);
|
| 2194 |
void reset();
|
| 2195 |
|
| 2196 |
// generating functions
|
| 2197 |
template<class URBG>
|
|
|
|
| 2207 |
result_type max() const;
|
| 2208 |
};
|
| 2209 |
```
|
| 2210 |
|
| 2211 |
``` cpp
|
| 2212 |
+
explicit chi_squared_distribution(RealType n);
|
| 2213 |
```
|
| 2214 |
|
| 2215 |
+
*Preconditions:* 0 < `n`.
|
| 2216 |
|
| 2217 |
+
*Remarks:* `n` corresponds to the parameter of the distribution.
|
|
|
|
| 2218 |
|
| 2219 |
``` cpp
|
| 2220 |
RealType n() const;
|
| 2221 |
```
|
| 2222 |
|
|
|
|
| 2224 |
constructed.
|
| 2225 |
|
| 2226 |
##### Class template `cauchy_distribution` <a id="rand.dist.norm.cauchy">[[rand.dist.norm.cauchy]]</a>
|
| 2227 |
|
| 2228 |
A `cauchy_distribution` random number distribution produces random
|
| 2229 |
+
numbers x distributed according to the probability density function
|
| 2230 |
+
$$p(x\,|\,a,b) = \left(\pi b \left(1 + \left(\frac{x-a}{b} \right)^2 \, \right)\right)^{-1} \text{ .}$$
|
|
|
|
|
|
|
| 2231 |
|
| 2232 |
``` cpp
|
| 2233 |
template<class RealType = double>
|
| 2234 |
class cauchy_distribution {
|
| 2235 |
public:
|
| 2236 |
// types
|
| 2237 |
using result_type = RealType;
|
| 2238 |
using param_type = unspecified;
|
| 2239 |
|
| 2240 |
// constructor and reset functions
|
| 2241 |
+
cauchy_distribution() : cauchy_distribution(0.0) {}
|
| 2242 |
+
explicit cauchy_distribution(RealType a, RealType b = 1.0);
|
| 2243 |
explicit cauchy_distribution(const param_type& parm);
|
| 2244 |
void reset();
|
| 2245 |
|
| 2246 |
// generating functions
|
| 2247 |
template<class URBG>
|
|
|
|
| 2258 |
result_type max() const;
|
| 2259 |
};
|
| 2260 |
```
|
| 2261 |
|
| 2262 |
``` cpp
|
| 2263 |
+
explicit cauchy_distribution(RealType a, RealType b = 1.0);
|
| 2264 |
```
|
| 2265 |
|
| 2266 |
+
*Preconditions:* 0 < `b`.
|
| 2267 |
|
| 2268 |
+
*Remarks:* `a` and `b` correspond to the respective parameters of the
|
| 2269 |
+
distribution.
|
| 2270 |
|
| 2271 |
``` cpp
|
| 2272 |
RealType a() const;
|
| 2273 |
```
|
| 2274 |
|
|
|
|
| 2284 |
|
| 2285 |
##### Class template `fisher_f_distribution` <a id="rand.dist.norm.f">[[rand.dist.norm.f]]</a>
|
| 2286 |
|
| 2287 |
A `fisher_f_distribution` random number distribution produces random
|
| 2288 |
numbers x ≥ 0 distributed according to the probability density function
|
| 2289 |
+
$$p(x\,|\,m,n) = \frac{\Gamma\big((m+n)/2\big)}{\Gamma(m/2) \; \Gamma(n/2)}
|
| 2290 |
+
\cdot \left(\frac{m}{n}\right)^{m/2}
|
| 2291 |
+
\cdot x^{(m/2)-1}
|
| 2292 |
+
\cdot \left(1 + \frac{m x}{n}\right)^{-(m + n)/2}
|
| 2293 |
+
\text{ .}$$
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2294 |
|
| 2295 |
``` cpp
|
| 2296 |
template<class RealType = double>
|
| 2297 |
class fisher_f_distribution {
|
| 2298 |
public:
|
| 2299 |
// types
|
| 2300 |
using result_type = RealType;
|
| 2301 |
using param_type = unspecified;
|
| 2302 |
|
| 2303 |
// constructor and reset functions
|
| 2304 |
+
fisher_f_distribution() : fisher_f_distribution(1.0) {}
|
| 2305 |
+
explicit fisher_f_distribution(RealType m, RealType n = 1.0);
|
| 2306 |
explicit fisher_f_distribution(const param_type& parm);
|
| 2307 |
void reset();
|
| 2308 |
|
| 2309 |
// generating functions
|
| 2310 |
template<class URBG>
|
|
|
|
| 2321 |
result_type max() const;
|
| 2322 |
};
|
| 2323 |
```
|
| 2324 |
|
| 2325 |
``` cpp
|
| 2326 |
+
explicit fisher_f_distribution(RealType m, RealType n = 1);
|
| 2327 |
```
|
| 2328 |
|
| 2329 |
+
*Preconditions:* 0 < `m` and 0 < `n`.
|
| 2330 |
|
| 2331 |
+
*Remarks:* `m` and `n` correspond to the respective parameters of the
|
| 2332 |
+
distribution.
|
| 2333 |
|
| 2334 |
``` cpp
|
| 2335 |
RealType m() const;
|
| 2336 |
```
|
| 2337 |
|
|
|
|
| 2346 |
constructed.
|
| 2347 |
|
| 2348 |
##### Class template `student_t_distribution` <a id="rand.dist.norm.t">[[rand.dist.norm.t]]</a>
|
| 2349 |
|
| 2350 |
A `student_t_distribution` random number distribution produces random
|
| 2351 |
+
numbers x distributed according to the probability density function
|
| 2352 |
+
$$p(x\,|\,n) = \frac{1}{\sqrt{n \pi}}
|
| 2353 |
+
\cdot \frac{\Gamma\big((n+1)/2\big)}{\Gamma(n/2)}
|
|
|
|
|
|
|
|
|
|
| 2354 |
\cdot \left(1 + \frac{x^2}{n} \right)^{-(n+1)/2}
|
| 2355 |
+
\text{ .}$$
|
| 2356 |
|
| 2357 |
``` cpp
|
| 2358 |
template<class RealType = double>
|
| 2359 |
class student_t_distribution {
|
| 2360 |
public:
|
| 2361 |
// types
|
| 2362 |
using result_type = RealType;
|
| 2363 |
using param_type = unspecified;
|
| 2364 |
|
| 2365 |
// constructor and reset functions
|
| 2366 |
+
student_t_distribution() : student_t_distribution(1.0) {}
|
| 2367 |
+
explicit student_t_distribution(RealType n);
|
| 2368 |
explicit student_t_distribution(const param_type& parm);
|
| 2369 |
void reset();
|
| 2370 |
|
| 2371 |
// generating functions
|
| 2372 |
template<class URBG>
|
|
|
|
| 2382 |
result_type max() const;
|
| 2383 |
};
|
| 2384 |
```
|
| 2385 |
|
| 2386 |
``` cpp
|
| 2387 |
+
explicit student_t_distribution(RealType n);
|
| 2388 |
```
|
| 2389 |
|
| 2390 |
+
*Preconditions:* 0 < `n`.
|
| 2391 |
|
| 2392 |
+
*Remarks:* `n` corresponds to the parameter of the distribution.
|
|
|
|
| 2393 |
|
| 2394 |
``` cpp
|
| 2395 |
RealType n() const;
|
| 2396 |
```
|
| 2397 |
|
|
|
|
| 2402 |
|
| 2403 |
##### Class template `discrete_distribution` <a id="rand.dist.samp.discrete">[[rand.dist.samp.discrete]]</a>
|
| 2404 |
|
| 2405 |
A `discrete_distribution` random number distribution produces random
|
| 2406 |
integers i, 0 ≤ i < n, distributed according to the discrete probability
|
| 2407 |
+
function $$P(i \,|\, p_0, \dotsc, p_{n-1}) = p_i \text{ .}$$
|
|
|
|
|
|
|
|
|
|
| 2408 |
|
| 2409 |
Unless specified otherwise, the distribution parameters are calculated
|
| 2410 |
+
as: pₖ = {wₖ / S} for k = 0, …, n - 1, in which the values wₖ, commonly
|
| 2411 |
+
known as the *weights* , shall be non-negative, non-NaN, and
|
| 2412 |
+
non-infinity. Moreover, the following relation shall hold:
|
| 2413 |
+
$0 < S = w_0 + \dotsb + w_{n - 1}$.
|
| 2414 |
|
| 2415 |
``` cpp
|
| 2416 |
template<class IntType = int>
|
| 2417 |
class discrete_distribution {
|
| 2418 |
public:
|
|
|
|
| 2458 |
``` cpp
|
| 2459 |
template<class InputIterator>
|
| 2460 |
discrete_distribution(InputIterator firstW, InputIterator lastW);
|
| 2461 |
```
|
| 2462 |
|
| 2463 |
+
*Mandates:*
|
| 2464 |
+
`is_convertible_v<iterator_traits<InputIterator>::value_type, double>`
|
| 2465 |
+
is `true`.
|
| 2466 |
+
|
| 2467 |
+
*Preconditions:* `InputIterator` meets the *Cpp17InputIterator*
|
| 2468 |
+
requirements [[input.iterators]]. If `firstW == lastW`, let n = 1 and
|
| 2469 |
+
w₀ = 1. Otherwise, [`firstW`, `lastW`) forms a sequence w of length
|
| 2470 |
+
n > 0.
|
| 2471 |
|
| 2472 |
*Effects:* Constructs a `discrete_distribution` object with
|
| 2473 |
probabilities given by the formula above.
|
| 2474 |
|
| 2475 |
``` cpp
|
|
|
|
| 2481 |
``` cpp
|
| 2482 |
template<class UnaryOperation>
|
| 2483 |
discrete_distribution(size_t nw, double xmin, double xmax, UnaryOperation fw);
|
| 2484 |
```
|
| 2485 |
|
| 2486 |
+
*Mandates:* `is_invocable_r_v<double, UnaryOperation&, double>` is
|
| 2487 |
+
`true`.
|
| 2488 |
+
|
| 2489 |
+
*Preconditions:* If `nw` = 0, let n = 1, otherwise let n = `nw`. The
|
| 2490 |
+
relation 0 < δ = (`xmax` - `xmin`) / n holds.
|
| 2491 |
|
| 2492 |
*Effects:* Constructs a `discrete_distribution` object with
|
| 2493 |
probabilities given by the formula above, using the following values: If
|
| 2494 |
`nw` = 0, let w₀ = 1. Otherwise, let wₖ = `fw`(`xmin` + k ⋅ δ + δ / 2)
|
| 2495 |
for k = 0, …, n - 1.
|
| 2496 |
|
| 2497 |
+
*Complexity:* The number of invocations of `fw` does not exceed n.
|
| 2498 |
|
| 2499 |
``` cpp
|
| 2500 |
vector<double> probabilities() const;
|
| 2501 |
```
|
| 2502 |
|
|
|
|
| 2507 |
##### Class template `piecewise_constant_distribution` <a id="rand.dist.samp.pconst">[[rand.dist.samp.pconst]]</a>
|
| 2508 |
|
| 2509 |
A `piecewise_constant_distribution` random number distribution produces
|
| 2510 |
random numbers x, b₀ ≤ x < bₙ, uniformly distributed over each
|
| 2511 |
subinterval [ bᵢ, bᵢ₊₁ ) according to the probability density function
|
| 2512 |
+
$$p(x \,|\, b_0, \dotsc, b_n, \; \rho_0, \dotsc, \rho_{n-1}) = \rho_i
|
| 2513 |
+
\text{ , for $b_i \le x < b_{i+1}$.}$$
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2514 |
|
| 2515 |
The n + 1 distribution parameters bᵢ, also known as this distribution’s
|
| 2516 |
+
*interval boundaries* , shall satisfy the relation $b_i < b_{i + 1}$ for
|
| 2517 |
+
i = 0, …, n - 1. Unless specified otherwise, the remaining n
|
| 2518 |
+
distribution parameters are calculated as:
|
| 2519 |
+
$$\rho_k = \frac{w_k}{S \cdot (b_{k+1}-b_k)} \text{ for } k = 0, \dotsc, n - 1 \text{ ,}$$
|
| 2520 |
+
in which the values wₖ, commonly known as the *weights* , shall be
|
| 2521 |
+
non-negative, non-NaN, and non-infinity. Moreover, the following
|
| 2522 |
+
relation shall hold: 0 < S = w₀ + … + wₙ₋₁.
|
|
|
|
|
|
|
| 2523 |
|
| 2524 |
``` cpp
|
| 2525 |
template<class RealType = double>
|
| 2526 |
class piecewise_constant_distribution {
|
| 2527 |
public:
|
|
|
|
| 2569 |
template<class InputIteratorB, class InputIteratorW>
|
| 2570 |
piecewise_constant_distribution(InputIteratorB firstB, InputIteratorB lastB,
|
| 2571 |
InputIteratorW firstW);
|
| 2572 |
```
|
| 2573 |
|
| 2574 |
+
*Mandates:* Both of
|
| 2575 |
+
|
| 2576 |
+
- `is_convertible_v<iterator_traits<InputIteratorB>::value_type, double>`
|
| 2577 |
+
- `is_convertible_v<iterator_traits<InputIteratorW>::value_type, double>`
|
| 2578 |
+
|
| 2579 |
+
are `true`.
|
| 2580 |
+
|
| 2581 |
+
*Preconditions:* `InputIteratorB` and `InputIteratorW` each meet the
|
| 2582 |
+
*Cpp17InputIterator* requirements [[input.iterators]]. If
|
| 2583 |
+
`firstB == lastB` or `++firstB == lastB`, let n = 1, w₀ = 1, b₀ = 0, and
|
| 2584 |
+
b₁ = 1. Otherwise, [`firstB`, `lastB`) forms a sequence b of length n+1,
|
| 2585 |
+
the length of the sequence w starting from `firstW` is at least n, and
|
| 2586 |
+
any wₖ for k ≥ n are ignored by the distribution.
|
| 2587 |
|
| 2588 |
*Effects:* Constructs a `piecewise_constant_distribution` object with
|
| 2589 |
parameters as specified above.
|
| 2590 |
|
| 2591 |
``` cpp
|
| 2592 |
template<class UnaryOperation>
|
| 2593 |
piecewise_constant_distribution(initializer_list<RealType> bl, UnaryOperation fw);
|
| 2594 |
```
|
| 2595 |
|
| 2596 |
+
*Mandates:* `is_invocable_r_v<double, UnaryOperation&, double>` is
|
| 2597 |
+
`true`.
|
|
|
|
|
|
|
| 2598 |
|
| 2599 |
*Effects:* Constructs a `piecewise_constant_distribution` object with
|
| 2600 |
parameters taken or calculated from the following values: If
|
| 2601 |
`bl.size()` < 2, let n = 1, w₀ = 1, b₀ = 0, and b₁ = 1. Otherwise, let
|
| 2602 |
[`bl.begin()`, `bl.end()`) form a sequence b₀, …, bₙ, and let
|
| 2603 |
wₖ = `fw`((bₖ₊₁ + bₖ) / 2) for k = 0, …, n - 1.
|
| 2604 |
|
| 2605 |
+
*Complexity:* The number of invocations of `fw` does not exceed n.
|
| 2606 |
|
| 2607 |
``` cpp
|
| 2608 |
template<class UnaryOperation>
|
| 2609 |
piecewise_constant_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw);
|
| 2610 |
```
|
| 2611 |
|
| 2612 |
+
*Mandates:* `is_invocable_r_v<double, UnaryOperation&, double>` is
|
| 2613 |
+
`true`.
|
| 2614 |
+
|
| 2615 |
+
*Preconditions:* If `nw` = 0, let n = 1, otherwise let n = `nw`. The
|
| 2616 |
+
relation 0 < δ = (`xmax` - `xmin`) / n holds.
|
| 2617 |
|
| 2618 |
*Effects:* Constructs a `piecewise_constant_distribution` object with
|
| 2619 |
parameters taken or calculated from the following values: Let
|
| 2620 |
bₖ = `xmin` + k ⋅ δ for k = 0, …, n, and wₖ = `fw`(bₖ + δ / 2) for
|
| 2621 |
k = 0, …, n - 1.
|
| 2622 |
|
| 2623 |
+
*Complexity:* The number of invocations of `fw` does not exceed n.
|
| 2624 |
|
| 2625 |
``` cpp
|
| 2626 |
vector<result_type> intervals() const;
|
| 2627 |
```
|
| 2628 |
|
|
|
|
| 2640 |
|
| 2641 |
##### Class template `piecewise_linear_distribution` <a id="rand.dist.samp.plinear">[[rand.dist.samp.plinear]]</a>
|
| 2642 |
|
| 2643 |
A `piecewise_linear_distribution` random number distribution produces
|
| 2644 |
random numbers x, b₀ ≤ x < bₙ, distributed over each subinterval
|
| 2645 |
+
[bᵢ, bᵢ₊₁) according to the probability density function
|
| 2646 |
+
$$p(x \,|\, b_0, \dotsc, b_n, \; \rho_0, \dotsc, \rho_n)
|
| 2647 |
+
= \rho_{i} \cdot {\frac{b_{i+1} - x}{b_{i+1} - b_i}}
|
| 2648 |
+ \rho_{i+1} \cdot {\frac{x - b_i}{b_{i+1} - b_i}}
|
| 2649 |
+
\text{ , for $b_i \le x < b_{i+1}$.}$$
|
|
|
|
|
|
|
| 2650 |
|
| 2651 |
The n + 1 distribution parameters bᵢ, also known as this distribution’s
|
| 2652 |
*interval boundaries* , shall satisfy the relation bᵢ < bᵢ₊₁ for
|
| 2653 |
i = 0, …, n - 1. Unless specified otherwise, the remaining n + 1
|
| 2654 |
+
distribution parameters are calculated as ρₖ = {wₖ / S} for k = 0, …, n,
|
| 2655 |
+
in which the values wₖ, commonly known as the *weights at boundaries* ,
|
| 2656 |
+
shall be non-negative, non-NaN, and non-infinity. Moreover, the
|
| 2657 |
+
following relation shall hold:
|
| 2658 |
+
$$0 < S = \frac{1}{2} \cdot \sum_{k=0}^{n-1} (w_k + w_{k+1}) \cdot (b_{k+1} - b_k) \text{ .}$$
|
|
|
|
|
|
|
|
|
|
| 2659 |
|
| 2660 |
``` cpp
|
| 2661 |
template<class RealType = double>
|
| 2662 |
class piecewise_linear_distribution {
|
| 2663 |
public:
|
|
|
|
| 2704 |
template<class InputIteratorB, class InputIteratorW>
|
| 2705 |
piecewise_linear_distribution(InputIteratorB firstB, InputIteratorB lastB,
|
| 2706 |
InputIteratorW firstW);
|
| 2707 |
```
|
| 2708 |
|
| 2709 |
+
*Mandates:* `is_invocable_r_v<double, UnaryOperation&, double>` is
|
| 2710 |
+
`true`.
|
| 2711 |
+
|
| 2712 |
+
*Preconditions:* `InputIteratorB` and `InputIteratorW` each meet the
|
| 2713 |
+
*Cpp17InputIterator* requirements [[input.iterators]]. If
|
| 2714 |
+
`firstB == lastB` or `++firstB == lastB`, let n = 1, ρ₀ = ρ₁ = 1,
|
| 2715 |
+
b₀ = 0, and b₁ = 1. Otherwise, [`firstB`, `lastB`) forms a sequence b of
|
| 2716 |
+
length n+1, the length of the sequence w starting from `firstW` is at
|
| 2717 |
+
least n+1, and any wₖ for k ≥ n + 1 are ignored by the distribution.
|
|
|
|
| 2718 |
|
| 2719 |
*Effects:* Constructs a `piecewise_linear_distribution` object with
|
| 2720 |
parameters as specified above.
|
| 2721 |
|
| 2722 |
``` cpp
|
| 2723 |
template<class UnaryOperation>
|
| 2724 |
piecewise_linear_distribution(initializer_list<RealType> bl, UnaryOperation fw);
|
| 2725 |
```
|
| 2726 |
|
| 2727 |
+
*Mandates:* `is_invocable_r_v<double, UnaryOperation&, double>` is
|
| 2728 |
+
`true`.
|
|
|
|
|
|
|
| 2729 |
|
| 2730 |
*Effects:* Constructs a `piecewise_linear_distribution` object with
|
| 2731 |
parameters taken or calculated from the following values: If
|
| 2732 |
`bl.size()` < 2, let n = 1, ρ₀ = ρ₁ = 1, b₀ = 0, and b₁ = 1. Otherwise,
|
| 2733 |
let [`bl.begin(),` `bl.end()`) form a sequence b₀, …, bₙ, and let
|
| 2734 |
wₖ = `fw`(bₖ) for k = 0, …, n.
|
| 2735 |
|
| 2736 |
+
*Complexity:* The number of invocations of `fw` does not exceed n+1.
|
| 2737 |
|
| 2738 |
``` cpp
|
| 2739 |
template<class UnaryOperation>
|
| 2740 |
piecewise_linear_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw);
|
| 2741 |
```
|
| 2742 |
|
| 2743 |
+
*Mandates:* `is_invocable_r_v<double, UnaryOperation&, double>` is
|
| 2744 |
+
`true`.
|
| 2745 |
+
|
| 2746 |
+
*Preconditions:* If `nw` = 0, let n = 1, otherwise let n = `nw`. The
|
| 2747 |
+
relation 0 < δ = (`xmax` - `xmin`) / n holds.
|
| 2748 |
|
| 2749 |
*Effects:* Constructs a `piecewise_linear_distribution` object with
|
| 2750 |
parameters taken or calculated from the following values: Let
|
| 2751 |
bₖ = `xmin` + k ⋅ δ for k = 0, …, n, and wₖ = `fw`(bₖ) for k = 0, …, n.
|
| 2752 |
|
| 2753 |
+
*Complexity:* The number of invocations of `fw` does not exceed n+1.
|
| 2754 |
|
| 2755 |
``` cpp
|
| 2756 |
vector<result_type> intervals() const;
|
| 2757 |
```
|
| 2758 |
|
|
|
|
| 2768 |
whose `operator[]` member returns ρₖ when invoked with argument k for
|
| 2769 |
k = 0, …, n.
|
| 2770 |
|
| 2771 |
### Low-quality random number generation <a id="c.math.rand">[[c.math.rand]]</a>
|
| 2772 |
|
| 2773 |
+
[*Note 1*: The header `<cstdlib>` declares the functions described in
|
| 2774 |
+
this subclause. — *end note*]
|
| 2775 |
|
| 2776 |
``` cpp
|
| 2777 |
int rand();
|
| 2778 |
void srand(unsigned int seed);
|
| 2779 |
```
|
|
|
|
| 2781 |
*Effects:* The `rand` and `srand` functions have the semantics specified
|
| 2782 |
in the C standard library.
|
| 2783 |
|
| 2784 |
*Remarks:* The implementation may specify that particular library
|
| 2785 |
functions may call `rand`. It is *implementation-defined* whether the
|
| 2786 |
+
`rand` function may introduce data races [[res.on.data.races]].
|
| 2787 |
|
| 2788 |
[*Note 1*: The other random number generation facilities in this
|
| 2789 |
+
document [[rand]] are often preferable to `rand`, because `rand`’s
|
| 2790 |
+
underlying algorithm is unspecified. Use of `rand` therefore continues
|
| 2791 |
+
to be non-portable, with unpredictable and oft-questionable quality and
|
| 2792 |
+
performance. — *end note*]
|
| 2793 |
|
| 2794 |
+
See also: ISO C 7.22.2
|
| 2795 |
|