我有一个事件应该有 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/