public member function
<random>

std::bernoulli_distribution::operator()

(1)
template<class URNG>bool operator()(URNG& g);
(2)
template<class URNG>bool operator()(URNG& g, const param_type& parm);
Generate random number
Returns a new random value with the probability associated to the object (version 1) or with the probability specified by parm (version 2).

The generator object (g) supplies uniformly-distributed random integers through its operator() member function. The bernoulli_distribution object transforms the values obtained this way so that successive calls to this member function with the same arguments produce values that follow a Bernoulli distribution with the appropriate probability.

Parameters

g
A uniform random number generator object, used as the source of randomness.
URNG shall be a uniform random number generator type, such as one of the standard generator classes.
parm
An object representing the distribution's parameters, obtained by a call to member function param.
param_type is a member type.

Return value

A new random value.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// bernoulli_distribution example: fortune-teller
#include <iostream>
#include <string>
#include <random>

int main()
{
  std::cout << "Please, enter a yes/no question (I will answer it):" << std::endl;
  std::string text;
  getline(std::cin,text);

  std::seed_seq seed (text.begin(),text.end());  // seed using question
  std::default_random_engine generator (seed);
  std::bernoulli_distribution distribution(0.5);

  bool result = distribution(generator);
  std::cout << ( result ? "yes" : "no" ) << std::endl;

  return 0;
}

Possible output:
Please, enter a yes/no question (I will answer it):
Will I get married soon?
no


Complexity

Amortized constant (one invocation of g.operator()).

See also