正确的布尔随机数发生器(伯努利分布) [英] Proper boolean random generator (Bernoulli distribution)

查看:650
本文介绍了正确的布尔随机数发生器(伯努利分布)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会很想知道是否有在随机 C ++ 11库中的默认的随机布尔发生器。我一直在使用一个INT发生器返回 0 1 ,然后转换为bool,但我试图优化我的code和想,我可以从一开始就使用布尔发电机保存,如果存在的话。

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.

推荐答案

请参阅<$c$c>std::bernoulli_distribution &LT;随机&GT; 头,贴切的伯努利分布的名字命名的。

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);

这篇关于正确的布尔随机数发生器(伯努利分布)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆