Correct Boolean random generator (Bernoulli distribution)

I would be interested to know if there is a random boolean generator by default in the random C ++ 11 library. I use an int generator that returns 0 or 1 and then convert to bool, but I'm trying to optimize my code and think I can save. using the bool generator from the very beginning, if it exists.

+6
source share
1 answer

See std::bernoulli_distribution in the <random> header, exactly 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); 
+11
source

Source: https://habr.com/ru/post/971354/


All Articles