Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

random - C++ | Generating a pseudo number between 10-20

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

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)

I'm new to C++ so I'm not quite sure how I can do this, any help would be greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

If you want to include 20 in the range then this is a range of 11 numbers:

int r = rand() % (21 - 10) + 10

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...