C ++ |生成10-20之间的伪数 [英] C++ | Generating a pseudo number between 10-20
问题描述
我正在做一个基于文本的C + + RPG,我试图找出如何计算出敌人击中你的伤害量。我的想法是这样的。
I am making a text-based C++ RPG and I am trying to figure out how to work out the amount of damage that the enemy hits you for. My idea is something like this.
Damage done = randomIntBetween10and20 * enemyLevel
Damage done = randomIntBetween10and20*enemyLevel
每次击中一个设定的金额,并允许有临界打击(例如,如果命中超过15我会把它作为一个危机打击)
This way it doesn't always hit for a set amount each time and allows there to be Critical Strikes (For example, if the hit is above 15 I would class that as a Critical Strike)
我是C ++的新手,所以我不太确定如何做到这一点,任何帮助将非常感激。
I'm new to C++ so I'm not quite sure how I can do this, any help would be greatly appreciated.
推荐答案
你应该从标题中省略真正的一词,因为你可能不是这个意思。您可能只需要一个伪随机号码。使用个人计算机几乎不可能实现真实的随机性。以下代码段将为您提供10..19(含)范围内的伪随机数:
You should omit the word "truly" from the title, because you probably don't mean it. You probably just want a pseudorandom number. True randomness is virtually impossible to achieve with a personal computer. The following snippet will give you a pseudorandom number in the range 10..19 inclusive:
#include<cstdlib>
#include<ctime>
// ...
srand(time(0));
int r = rand() % (20 - 10) + 10;
如果要在范围中包括20,则这是一个包含11个数字的范围:
If you want to include 20 in the range then this is a range of 11 numbers:
int r = rand() % (21 - 10) + 10
这篇关于C ++ |生成10-20之间的伪数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!