Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper boolean random generator (Bernoulli distribution)

I'd be curious to know if there is a default random boolean generator in the random C++11 library. I've been using a int generator returning 0 or 1 and then converting to bool but I'm trying to optimize my code and thinking that I could save by using from the beginning a bool generator, if it exists.

like image 883
Learning is a mess Avatar asked Jun 25 '14 19:06

Learning is a mess


People also ask

What is the MGF of Bernoulli distribution?

Theorem. Let X be a discrete random variable with a Bernoulli distribution with parameter p for some 0≤p≤1. Then the moment generating function MX of X is given by: MX(t)=q+pet.

How do you write a Bernoulli distribution?

Bernoulli Distribution Formula X can be written as X ∼ ∼ Bernoulli (p), where p is the parameter. The formulas for Bernoulli distribution are given by the probability mass function (pmf) and the cumulative distribution function (CDF).

Which probability distribution is obtained when IID random variables following Bernoulli distribution are added?

The binomial distribution assumes that Y is the sum of independent random Bernoulli variables.

Which of the following is the correct way to write the notation that represents a Bernoulli random variable?

The expected value for a random variable, X, for a Bernoulli distribution is: E[X] = p. For example, if p = . 04, then E[X] = 0.04.


1 Answers

See std::bernoulli_distribution in the <random> header, aptly named after the Bernoulli distribution.

std::random_device device;
std::mt19937 gen(device());
std::bernoulli_distribution coin_flip(0.5);
bool outcome = coin_flip(gen);
like image 57
Timothy Shields Avatar answered Sep 20 '22 03:09

Timothy Shields