class
<random>

std::bernoulli_distribution

class 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().

Member types

The following aliases are member types of bernoulli_distribution:

member typedefinitionnotes
result_typeboolThe type of the results generated
param_typenot specifiedThe type returned by member param.

Member functions


Distribution parameters:


Non-member functions


Example

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;
}


Possible output:
bernoulli_distribution (0.5) x 10000:
true:  4994
false: 5006

See also