可能重复: C ++函数,用于从列表中挑选,其中每个元素具有不同的概率
我需要根据我可以定义的概率随机确定是或否结果(一种硬币翻转) ,.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.
更多推荐
具有不同概率的随机数
发布评论