C++ random bernoulli_distribution Class
Example
Flip a coin:
random_device rd;
mt19937 gen(rd());
bernoulli_distribution coin(0.5);
if (coin(gen)) {
cout << "Heads";
} else {
cout << "Tails";
}
Definition and Usage
A bernoulli_distribution gives a random true or false.
The probability is the chance of true, from 0.0 (never) to 1.0 (always). With 0.5, both are equally likely, like a coin flip.
The bernoulli_distribution class is defined in the <random> header file.
Note: The output is random, so it is different each time you run the program.
Syntax
bernoulli_distribution name(probability);
Parameter Values
| Parameter | Description |
|---|---|
| probability | Optional. The chance of true, from 0.0 to 1.0. Default is 0.5. |
Technical Details
| Returns: | Calling name(engine) returns true or false. |
|---|