《机会》为了一个功能发生? [英] "Chances" for a function to occur?

查看:36
本文介绍了《机会》为了一个功能发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用 rand() 函数,我们可以创建一个输出的可能性.

By using rand() function, we can create a possibility of an output.

rand()%4 + 'A' 基本上意味着这段代码将生成从 A 到 D 的随机字母表.

Like rand()%4 + 'A' basically means this code will generate random alphabets from A to D.

那么我的问题是,我们可以将可能性率"放入这个随机生成中吗?

Then my question is, can we put "possibility rate" into this random generation?

例如,A 在 4 个字母(从 A 到 D)中出现,只有 24% 的机会

Such as, for A to come out among 4 Alphabets ( from A to D), there only will be 24% chance

而 B 将以 12% 的比率出现,等等.

while B will come out at 12% rate, etc.

  • 我使用的是 Windows,我的编码程序是 Visual Studio 2012.(语言是 C)

推荐答案

创建一个由 100 个字母组成的字符串,其中包含 24 个 A、12 个 B 以及适当数量的 C 和 D.生成0到99之间的随机数;使用它作为字符串的索引.这为您提供了加权随机选择.

Create a string of 100 letters, with 24 A's, 12 B's, and appropriate numbers of C's and D's. Generate a random number between 0 and 99; use this as an index into the string. That gives you your weighted random selection.

这是一种简单但几乎是图形化的方法.您也可以根据范围来决定.

That's a simple but almost graphical way to do it. You can decide based on ranges too.

这篇关于《机会》为了一个功能发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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