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

2019-02-09 04:26发布

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.

标签: c++ random
9条回答
在下西门庆
2楼-- · 2019-02-09 05:01

This link must help you to generate random numbers within a limit in C++ http://www.codeguru.com/forum/showthread.php?t=378879

查看更多
forever°为你锁心
3楼-- · 2019-02-09 05:02

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
查看更多
老娘就宠你
4楼-- · 2019-02-09 05:03

A good choice would be std::random, the random number generator that’s built-in to C++.

If you don’t have C++11 support yet, use boost::random. The new std::random is based on boost::random, so it’s basically the same thing. The documentation has several examples, including Generating integers in a range.

One of the options offered is the Mersenne Twister (mt19937), which is a good general-purpose pseudo-random number generator.

Like most of the other suggestions, this is pseudo-random. To get true randomness is more difficult—but you don’t need that for an RPG, right? (For cryptographically-strong random numbers, consider OpenSSL or, on Windows, CryptoAPI.)

查看更多
劫难
5楼-- · 2019-02-09 05:03

The stdlib random number generator would be an okay place to start:

#include <stdlib.h>
#include <time.h>

srand(time(NULL)); // initialize the RNG
int poll = rand() % 20; // random number between 0 and 19 inclusive

Eventually, you'll want to start using a more random RNG. Game Coding Complete has a pretty decent one to get you started.

查看更多
▲ chillily
6楼-- · 2019-02-09 05:05

The method that uses modulus (%) isn't a good choice because the distribution is off. This is better:

double GetRandom(double Min, double Max)
{
    return ((double(rand()) / double(RAND_MAX)) * (Max - Min)) + Min;    
}

You will need to include algorithm and seed the generator with srand.

Of course, it's only pseudo-random. You won't be able to get truly random results, especially with rand.

查看更多
来,给爷笑一个
7楼-- · 2019-02-09 05:08

The only way to 'generate' a truly random number is through the interaction with some environmental factor that is random. This website provides a service that does that for you: http://www.random.org/

查看更多
登录 后发表回答