具有不同概率的随机数 [英] Random numbers with different probabilities
问题描述
我需要根据我可以定义的概率随机确定是或否结果(一种硬币翻转) ,.50,.75)。
I need to randomly determine a yes or no outcome (kind of a coin flip) based on a probability that I can define (.25, .50, .75).
例如,我想随机决定yes或no,其中yes有75%的机会被选中。我的选择是什么?有没有一个C ++库我可以用这个?
So for example, I want to randomly determine yes or no where yes has a 75% chance of being chosen. What are my options for this? Is there a C++ library I can use for this?
推荐答案
你可以很容易地实现这个使用 rand
function:
You can easily implement this using the rand
function:
bool TrueFalse = (rand() % 100) < 75;
rand()%100
你是一个在0和100之间的随机数,它在75以下的概率,好,75%。您可以用 75
替换任何所需的概率。
The rand() % 100
will give you a random number between 0 and 100, and the probability of it being under 75 is, well, 75%. You can substitute the 75
for any probability you want.
这篇关于具有不同概率的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!