Get true or false with a given probability

c++, numbers, probability, random

Solution

If you'd like to do this in C++11, you can use its various random number engines, combined with the `uniform_real_distribution` to provide a good result. The following code demonstrates:

#include <random>

std::knuth_b rand_engine;  // replace knuth_b with one of the engines listed below
std::uniform_real_distribution<> uniform_zero_to_one(0.0, 1.0);

bool random_bool_with_prob( double prob )  // probability between 0.0 and 1.0
{
    return uniform_zero_to_one(rand_engine) >= prob;
}

Alternately, you can use the `bernoulli_distribution`, which directly gives you a `bool` with the specified probability. The probability it takes is the probability of returning true, so it is exactly what you need:

#include <random>

std::knuth_b rand_engine;  // replace knuth_b with one of the engines listed below

bool random_bool_with_prob( double prob )  // probability between 0.0 and 1.0
{
    std::bernoulli_distribution d(prob);
    return d(rand_engine);
}

If your probability is fixed, then you can move it out of the function like so:

#include <random>

std::knuth_b rand_engine;  // replace knuth_b with one of the engines listed below
std::bernoulli_distribution random_bool_generator( prob );  // replace "prob" with your probability

bool random_bool()
{
    return random_bool_generator( rand_engine );
}

Or if you want to get fancier still, you can bind them together:

#include <random>
#include <functional>

std::knuth_b rand_engine;  // replace knuth_b with one of the engines listed below
std::bernoulli_distribution random_bool_generator( prob );  // replace "prob" with your probability

auto random_bool = std::bind( random_bool_generator, rand_engine )

// Now call random_bool() to get your random boolean with the specified probability.

You can replace `knuth_b` with any of the standard engines:

- `std::linear_congruential_engine`

- `std::mersenne_twister_engine`

- `std::subtract_with_carry_engine`

or many more, which are versions of the above, parameterized various ways. My reference lists the following:

- `std::default_random_engine` (Implementation defined.)

- `std::minstd_rand0`

- `std::minstd_rand`

- `std::mt19937`

- `std::mt19337_64`

- `std::ranlux24_base`

- `std::ranlux48_base`

- `std::ranlux24`

- `std::ranlux48`

- `std::knuth_b`

And if that isn't enough, there are some standard adaptors that can further perturb the random number sequence:

- `std::discard_block_engine` which adapts an engine by discarding a given number of generated values each time.

- `std::independent_bits_engine` which adapts an engine to produce random values with a specified number of bits. (Not important to your particular need.)

- `std::shuffle_order_engine` which adapts an engine by permutation of the order of their generated values.

The generators in the second list are derived from the base generators in the first list, either with specific parameters, adaptors or both. For example, `knuth_b` is equivalent to `shuffle_order_engine< linear_congruential_engine< uint32_t, 16807, 0, 2147483647>, 256>`, according to my reference book. (The C++ Standard Library, Second Edition, by Nicolai Josuttis, a great reference work.)

You can find more information online, including this brief introduction here: http://en.wikipedia.org/wiki/C++11#Extensible_random_number_facility

There's more documentation here: http://en.cppreference.com/w/cpp/numeric/random

You will probably want to modify the declaration of `rand_engine` above to provide a seed. The example above uses the default seed. See cppreference.com for how to seed it if you want a different seed.

Problem

I'm trying to write a function in c++ that will return true or false based on a probability given. So, for example if the probability given was 0.634 then, 63.4% of the time the function would return true. I've tried a few different things, and failed. Any help?

Original source

Related problems