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.
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.
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).
The binomial distribution assumes that Y is the sum of independent random Bernoulli variables.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With