c++ - 在 C++ 中,如何根据双值概率生成随机数?

标签 c++ probability

我有一个事件应该有 0.5% 的概率发生(不是很频繁)。

如何根据这个概率生成随机数?

像这样就足够了吗,我不相信,因为 srand 返回一个整数...

double rate = 0.05;
if((srand() % 100) < rate)
{
    std::cout << "Event Occurred" << endl;
}

最佳答案

std::mt19937 rng(std::random_device{}());
std::bernoulli_distribution d(0.005);
bool b = d(rng); // true 0.5% of the time; otherwise false

关于c++ - 在 C++ 中,如何根据双值概率生成随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29175411/

相关文章:

matlab - 一定区间内服从指数分布的随机数

machine-learning - 使用朴素贝叶斯分类

c++ - 为什么 pow(sqrt(-1), 2) 不返回 -1?

android - 将开源的JniLibs整合到自己的android项目中

c++ - OpenCV minMaxLoc 函数中的段错误

python ,SimPy : How to generate a value from a triangular probability distribution?

machine-learning - 概率和似然性之间的区别

c++ - C++/Qt项目的跨平台编译

c++ - C++中 "Expected a statement"是什么意思

machine-learning - 机器学习的概率基础知识