A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/bernoulli_distribution below:

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 type definition notes result_type bool The type of the results generated param_type not specified The type returned by member param.


Member functions
(constructor)
Construct bernoulli distribution (public member function)
operator()
Generate random number (public member function)
reset
Reset distribution (public member function)
param
Distribution parameters (public member function)
min
Minimum value (public member function)
max
Maximum value (public member function)

Distribution parameters:
p
Probability of true (public member function)

Non-member functions
operator<<
Insert into output stream (function template)
operator>>
Extract from input stream (function template)
relational operators
Relational operators (function template)

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
uniform_int_distribution
Uniform discrete distribution (class template)
binomial_distribution
Binomial distribution (class template)
geometric_distribution
Geometric distribution (class template)
negative_binomial_distribution
Negative binomial distribution (class template)

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