具有不同概率的随机数

编程入门 行业动态 更新时间:2024-10-27 18:18:20
本文介绍了具有不同概率的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复: 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.

更多推荐

具有不同概率的随机数

本文发布于:2023-10-28 23:04:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538023.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:随机数   概率

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!