class
<random>
std::bernoulli_distributionclass bernoulli_distribution;
Bernoulli distribution
Random number distribution that produces bool values according to a Bernoulli distribution, which is described by the following probability mass function:Where the probability of true is p and the probability of false is (1-p).
This represents one of the simplest distribution functions: The tossing of a coin is distributed according to a Bernoulli distribution with a probability p of approximately 0.5.
The distribution parameter, p, is set on construction.
To produce a random value following this distribution, call its member function operator().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// bernoulli_distribution
#include <iostream>
#include <random>
int main()
{
const int nrolls=10000;
std::default_random_engine generator;
std::bernoulli_distribution distribution(0.5);
int count=0; // count number of trues
for (int i=0; i<nrolls; ++i) if (distribution(generator)) ++count;
std::cout << "bernoulli_distribution (0.5) x 10000:" << std::endl;
std::cout << "true: " << count << std::endl;
std::cout << "false: " << nrolls-count << std::endl;
return 0;
}
bernoulli_distribution (0.5) x 10000: true: 4994 false: 5006
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4