Proper boolean random generator (Bernoulli distribution)

前端 未结 1 1887
既然无缘
既然无缘 2021-01-18 10:18

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条回答
  • 2021-01-18 10:40

    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);
    
    0 讨论(0)
提交回复
热议问题