[rand.dist.bern]

# 29 Numerics library [[numerics]](./#numerics)

## 29.5 Random number generation [[rand]](rand#dist.bern)

### 29.5.9 Random number distribution class templates [[rand.dist]](rand.dist#bern)

#### 29.5.9.3 Bernoulli distributions [rand.dist.bern]

#### [29.5.9.3.1](#bernoulli) Class bernoulli_distribution [[rand.dist.bern.bernoulli]](rand.dist.bern.bernoulli)

[1](#bernoulli-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4758)

A bernoulli_distribution random number distribution
produces bool values b distributed according to
the discrete probability function in Formula [29.4](#eq:rand.dist.bern.bernoulli)[.](#bernoulli-1.sentence-1)

P(b|p)={pÂ ifÂ b=true1âpÂ ifÂ b=false(29.4)

[ð](#lib:bernoulli_distribution_)

namespace std {class bernoulli_distribution {public:// typesusing result_type = bool; using param_type = *unspecified*; // constructors and reset functions bernoulli_distribution() : bernoulli_distribution(0.5) {}explicit bernoulli_distribution(double p); explicit bernoulli_distribution(const param_type& parm); void reset(); // equality operatorsfriend bool operator==(const bernoulli_distribution& x, const bernoulli_distribution& y); // generating functionstemplate<class URBG> result_type operator()(URBG& g); template<class URBG> result_type operator()(URBG& g, const param_type& parm); // property functionsdouble p() 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 bernoulli_distribution& x); template<class charT, class traits>friend basic_istream<charT, traits>&operator>>(basic_istream<charT, traits>& is, bernoulli_distribution& x); };}

[ð](#lib:bernoulli_distribution,constructor)

`explicit bernoulli_distribution(double p);
`

[2](#bernoulli-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4820)

*Preconditions*: 0 â¤ p â¤ 1[.](#bernoulli-2.sentence-1)

[3](#bernoulli-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4824)

*Remarks*: p corresponds to the parameter of the distribution[.](#bernoulli-3.sentence-1)

[ð](#lib:p,bernoulli_distribution)

`double p() const;
`

[4](#bernoulli-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4836)

*Returns*: The value of the p parameter
 with which the object was constructed[.](#bernoulli-4.sentence-1)

#### [29.5.9.3.2](#bin) Class template binomial_distribution [[rand.dist.bern.bin]](rand.dist.bern.bin)

[1](#bin-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4848)

A binomial_distribution random number distribution
produces integer values i â¥ 0 distributed according to
the discrete probability function in Formula [29.5](#eq:rand.dist.bern.bin)[.](#bin-1.sentence-1)

P(i|t,p)=(ti)âpiâ(1âp)tâi(29.5)

[ð](#lib:binomial_distribution_)

namespace std {template<class IntType = int>class binomial_distribution {public:// typesusing result_type = IntType; using param_type = *unspecified*; // constructors and reset functions binomial_distribution() : binomial_distribution(1) {}explicit binomial_distribution(IntType t, double p = 0.5); explicit binomial_distribution(const param_type& parm); void reset(); // equality operatorsfriend bool operator==(const binomial_distribution& x, const binomial_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 IntType t() const; double p() 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 binomial_distribution& x); template<class charT, class traits>friend basic_istream<charT, traits>&operator>>(basic_istream<charT, traits>& is, binomial_distribution& x); };}

[ð](#lib:binomial_distribution,constructor)

`explicit binomial_distribution(IntType t, double p = 0.5);
`

[2](#bin-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4910)

*Preconditions*: 0 â¤ p â¤ 1 and 0 â¤ t[.](#bin-2.sentence-1)

[3](#bin-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4914)

*Remarks*: t and p correspond to the respective parameters of the distribution[.](#bin-3.sentence-1)

[ð](#lib:t,binomial_distribution)

`IntType t() const;
`

[4](#bin-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4925)

*Returns*: The value of the t parameter
 with which the object was constructed[.](#bin-4.sentence-1)

[ð](#lib:p,binomial_distribution)

`double p() const;
`

[5](#bin-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4937)

*Returns*: The value of the p parameter
 with which the object was constructed[.](#bin-5.sentence-1)

#### [29.5.9.3.3](#geo) Class template geometric_distribution [[rand.dist.bern.geo]](rand.dist.bern.geo)

[1](#geo-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4949)

A geometric_distribution random number distribution
produces integer values i â¥ 0 distributed according to
the discrete probability function in Formula [29.6](#eq:rand.dist.bern.geo)[.](#geo-1.sentence-1)

P(i|p)=pâ(1âp)i(29.6)

[ð](#lib:geometric_distribution_)

namespace std {template<class IntType = int>class geometric_distribution {public:// typesusing result_type = IntType; using param_type = *unspecified*; // constructors and reset functions geometric_distribution() : geometric_distribution(0.5) {}explicit geometric_distribution(double p); explicit geometric_distribution(const param_type& parm); void reset(); // equality operatorsfriend bool operator==(const geometric_distribution& x, const geometric_distribution& y); // generating functionstemplate<class URBG> result_type operator()(URBG& g); template<class URBG> result_type operator()(URBG& g, const param_type& parm); // property functionsdouble p() 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 geometric_distribution& x); template<class charT, class traits>friend basic_istream<charT, traits>&operator>>(basic_istream<charT, traits>& is, geometric_distribution& x); };}

[ð](#lib:geometric_distribution,constructor)

`explicit geometric_distribution(double p);
`

[2](#geo-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L5009)

*Preconditions*: 0<p<1[.](#geo-2.sentence-1)

[3](#geo-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L5013)

*Remarks*: p corresponds to the parameter of the distribution[.](#geo-3.sentence-1)

[ð](#lib:p,geometric_distribution)

`double p() const;
`

[4](#geo-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L5025)

*Returns*: The value of the p parameter
 with which the object was constructed[.](#geo-4.sentence-1)

#### [29.5.9.3.4](#negbin) Class template negative_binomial_distribution [[rand.dist.bern.negbin]](rand.dist.bern.negbin)

[1](#negbin-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L5038)

A negative_binomial_distribution random number distribution
produces random integers i â¥ 0 distributed according to
the discrete probability function in Formula [29.7](#eq:rand.dist.bern.negbin)[.](#negbin-1.sentence-1)

P(i|k,p)=(k+iâ1i)âpkâ(1âp)i(29.7)

[*Note [1](#negbin-note-1)*:

This implies that P(i | k,p) is undefined when p == 1[.](#negbin-1.sentence-2)

â *end note*]

[ð](#lib:negative_binomial_distribution_)

namespace std {template<class IntType = int>class negative_binomial_distribution {public:// typesusing result_type = IntType; using param_type = *unspecified*; // constructor and reset functions negative_binomial_distribution() : negative_binomial_distribution(1) {}explicit negative_binomial_distribution(IntType k, double p = 0.5); explicit negative_binomial_distribution(const param_type& parm); void reset(); // equality operatorsfriend bool operator==(const negative_binomial_distribution& x, const negative_binomial_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 IntType k() const; double p() 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 negative_binomial_distribution& x); template<class charT, class traits>friend basic_istream<charT, traits>&operator>>(basic_istream<charT, traits>& is, negative_binomial_distribution& x); };}

[ð](#lib:negative_binomial_distribution,constructor)

`explicit negative_binomial_distribution(IntType k, double p = 0.5);
`

[2](#negbin-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L5103)

*Preconditions*: 0<pâ¤1 and 0<k[.](#negbin-2.sentence-1)

[3](#negbin-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L5108)

*Remarks*: k and p correspond to the respective parameters of the distribution[.](#negbin-3.sentence-1)

[ð](#lib:k,negative_binomial_distribution)

`IntType k() const;
`

[4](#negbin-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L5120)

*Returns*: The value of the k parameter
 with which the object was constructed[.](#negbin-4.sentence-1)

[ð](#lib:p,negative_binomial_distribution)

`double p() const;
`

[5](#negbin-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L5132)

*Returns*: The value of the p parameter
 with which the object was constructed[.](#negbin-5.sentence-1)
