How to generate a boolean with p probability using C rand() function?
c, random
Solution
bool nextBool(double probability)
{
return (rand() / (double)RAND_MAX) < probability;
}
or (after seeing other responses)
bool nextBool(double probability)
{
return rand() < probability * ((double)RAND_MAX + 1.0);
}
Problem
How can I generate a random boolean with a probability of `p` (where 0 <= p <= 1.0) using the C standard library `rand()` function? i.e. ``` bool nextBool(double probability) { return ... } ```