140 lines
6.0 KiB
Markdown
140 lines
6.0 KiB
Markdown
[rand.dist.uni]
|
||
|
||
# 29 Numerics library [[numerics]](./#numerics)
|
||
|
||
## 29.5 Random number generation [[rand]](rand#dist.uni)
|
||
|
||
### 29.5.9 Random number distribution class templates [[rand.dist]](rand.dist#uni)
|
||
|
||
#### 29.5.9.2 Uniform distributions [rand.dist.uni]
|
||
|
||
#### [29.5.9.2.1](#int) Class template uniform_int_distribution [[rand.dist.uni.int]](rand.dist.uni.int)
|
||
|
||
[1](#int-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4532)
|
||
|
||
A uniform_int_distribution random number distribution
|
||
produces random integers i,a ⤠i ⤠b,
|
||
distributed according to
|
||
the constant discrete probability function in Formula [29.2](#eq:rand.dist.uni.int)[.](#int-1.sentence-1)
|
||
|
||
P(i|a,b)=1/(bâa+1)(29.2)
|
||
|
||
[ð](#lib:uniform_int_distribution_)
|
||
|
||
namespace std {template<class IntType = int>class uniform_int_distribution {public:// typesusing result_type = IntType; using param_type = *unspecified*; // constructors and reset functions uniform_int_distribution() : uniform_int_distribution(0) {}explicit uniform_int_distribution(IntType a, IntType b = numeric_limits<IntType>::max()); explicit uniform_int_distribution(const param_type& parm); void reset(); // equality operatorsfriend bool operator==(const uniform_int_distribution& x, const uniform_int_distribution& y); // generating functionstemplate<class URBG> result_type operator()(URBG& g); template<class URBG> result_type operator()(URBG& g, const param_type& parm); // property functions result_type a() const;
|
||
result_type b() const;
|
||
param_type param() const; void param(const param_type& parm);
|
||
result_type min() const;
|
||
result_type max() const; // inserters and extractorstemplate<class charT, class traits>friend basic_ostream<charT, traits>&operator<<(basic_ostream<charT, traits>& os, // hostedconst uniform_int_distribution& x); template<class charT, class traits>friend basic_istream<charT, traits>&operator>>(basic_istream<charT, traits>& is, // hosted uniform_int_distribution& x); };}
|
||
|
||
[ð](#lib:uniform_int_distribution,constructor)
|
||
|
||
`explicit uniform_int_distribution(IntType a, IntType b = numeric_limits<IntType>::max());
|
||
`
|
||
|
||
[2](#int-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4597)
|
||
|
||
*Preconditions*: a ⤠b[.](#int-2.sentence-1)
|
||
|
||
[3](#int-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4601)
|
||
|
||
*Remarks*: a and b correspond to the respective parameters of the distribution[.](#int-3.sentence-1)
|
||
|
||
[ð](#lib:a,uniform_int_distribution)
|
||
|
||
`result_type a() const;
|
||
`
|
||
|
||
[4](#int-4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4613)
|
||
|
||
*Returns*: The value of the a parameter
|
||
with which the object was constructed[.](#int-4.sentence-1)
|
||
|
||
[ð](#lib:b,uniform_int_distribution)
|
||
|
||
`result_type b() const;
|
||
`
|
||
|
||
[5](#int-5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4625)
|
||
|
||
*Returns*: The value of the b parameter
|
||
with which the object was constructed[.](#int-5.sentence-1)
|
||
|
||
#### [29.5.9.2.2](#real) Class template uniform_real_distribution [[rand.dist.uni.real]](rand.dist.uni.real)
|
||
|
||
[1](#real-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4637)
|
||
|
||
A uniform_real_distribution random number distribution
|
||
produces random numbers x,aâ¤x<b,
|
||
distributed according to
|
||
the constant probability density function in Formula [29.3](#eq:rand.dist.uni.real)[.](#real-1.sentence-1)
|
||
|
||
p(x|a,b)=1/(bâa)(29.3)
|
||
|
||
[*Note [1](#real-note-1)*:
|
||
|
||
This implies that p(x | a,b) is undefined when a == b[.](#real-1.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
[ð](#lib:uniform_real_distribution_)
|
||
|
||
namespace std {template<class RealType = double>class uniform_real_distribution {public:// typesusing result_type = RealType; using param_type = *unspecified*; // constructors and reset functions uniform_real_distribution() : uniform_real_distribution(0.0) {}explicit uniform_real_distribution(RealType a, RealType b = 1.0); explicit uniform_real_distribution(const param_type& parm); void reset(); // equality operatorsfriend bool operator==(const uniform_real_distribution& x, const uniform_real_distribution& y); // generating functionstemplate<class URBG> result_type operator()(URBG& g); template<class URBG> result_type operator()(URBG& g, const param_type& parm); // property functions result_type a() const;
|
||
result_type b() const;
|
||
param_type param() const; void param(const param_type& parm);
|
||
result_type min() const;
|
||
result_type max() const; // inserters and extractorstemplate<class charT, class traits>friend basic_ostream<charT, traits>&operator<<(basic_ostream<charT, traits>& os, const uniform_real_distribution& x); template<class charT, class traits>friend basic_istream<charT, traits>&operator>>(basic_istream<charT, traits>& is, uniform_real_distribution& x); };}
|
||
|
||
[ð](#lib:uniform_real_distribution,constructor)
|
||
|
||
`explicit uniform_real_distribution(RealType a, RealType b = 1.0);
|
||
`
|
||
|
||
[2](#real-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4703)
|
||
|
||
*Preconditions*: a ⤠b andbâaâ¤numeric_limits<RealType>::max()[.](#real-2.sentence-1)
|
||
|
||
[3](#real-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4709)
|
||
|
||
*Remarks*: a and b correspond to the respective parameters of the distribution[.](#real-3.sentence-1)
|
||
|
||
[ð](#lib:a,uniform_real_distribution)
|
||
|
||
`result_type a() const;
|
||
`
|
||
|
||
[4](#real-4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4721)
|
||
|
||
*Returns*: The value of the a parameter
|
||
with which the object was constructed[.](#real-4.sentence-1)
|
||
|
||
[ð](#lib:b,uniform_real_distribution)
|
||
|
||
`result_type b() const;
|
||
`
|
||
|
||
[5](#real-5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4733)
|
||
|
||
*Returns*: The value of the b parameter
|
||
with which the object was constructed[.](#real-5.sentence-1)
|