具有不同概率的随机数 [英] Random numbers with different probabilities

查看:139
本文介绍了具有不同概率的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

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.

这篇关于具有不同概率的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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