C++ | 生成 10-20 之间的伪数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4413170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:20:19  来源:igfitidea点击:

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

c++random

提问by Luke Berry

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.

我正在制作一个基于文本的 C++ RPG,我正在尝试弄清楚如何计算敌人对你造成的伤害量。我的想法是这样的。

Damage done = randomIntBetween10and20*enemyLevel

造成的伤害 = 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)

这样,它并不总是每次都击中一定数量,并允许有暴击(例如,如果击中超过 15,我会将其归类为暴击)

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

我是 C++ 的新手,所以我不太确定如何做到这一点,任何帮助将不胜感激。

回答by wilhelmtell

You should omit the word "truly" from the title, because you probably don't mean it. You probably just want a pseudorandomnumber. 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:

你应该从标题中省略“真正”这个词,因为你可能不是这个意思。您可能只想要一个伪随机数。使用个人计算机几乎不可能实现真正的随机性。以下代码段将为您提供 10..19 范围内的伪随机数:

#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:

如果您想在范围中包含 20,那么这是一个包含 11 个数字的范围:

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

回答by Nate

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

一个不错的选择是std::random,C++ 内置的随机数生成器。

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

如果您还没有 C++11 支持,请使用boost::random. 新std::random的基于boost::random,所以它基本上是一样的。该文档有几个示例,包括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.

提供的选项之一是 Mersenne Twister ( mt19937),它是一个很好的通用伪随机数生成器。

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 OpenSSLor, on Windows, CryptoAPI.)

像大多数其他建议一样,这是伪随机的。获得真正的随机性更加困难——但你不需要在 RPG 中使用它,对吧?(对于加密强随机数,请考虑OpenSSL或 Windows 上的CryptoAPI。)

回答by Zac Howland

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

stdlib 随机数生成器将是一个不错的起点:

#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 Completehas a pretty decent one to get you started.

最终,您会想要开始使用更随机的 RNG。 Game Coding Complete有一个相当不错的入门指南。

回答by Stephane Rolland

As others said, maybe rand() will be really sufficient for you. What is important is the seed used to initialise the pseudo random number generator ( the call to srand() is the seed)

正如其他人所说,也许 rand() 对你来说真的足够了。重要的是用于初始化伪随机数生成器的种子(对 srand() 的调用是种子)

But beware, True Chaos doesnt mean that you have exactly the same chance to generate any possible random output.

但请注意,真正的混乱并不意味着您有完全相同的机会生成任何可能的随机输出。

Ten years ago I have played with stochastic sound generation. I needed several sources of chaos.

十年前,我玩过随机声音生成。我需要几个混乱的来源。

I just let you know those which I had kept and found useful. of course since they need a seed, they are pseudo chaos.

我只是让你知道我保留并发现有用的那些。当然,因为他们需要种子,所以他们是伪混沌。

1/for chaotic float number between -1 and 1: compute the function f(x) = cos(exp(x)). exp() grows so fast, that after really few iteration, what goes out from cos() is chaos.

1/对于 -1 和 1 之间的混沌浮点数:计算函数 f(x) = cos(exp(x))。exp() 增长得如此之快,以至于经过几次迭代后, cos() 的结果是混乱的。

2/the baker transform: chaotic number between 0 and 1: take a number, multiply it by two, and again, when it is superior to 1, substract something so as it goes back betwen 0 and 1. A much more precise explanation The Baker Transform.

2 /面包师变换:0和1之间乱数:因为它可以追溯到介乎0和1,一种更精确的解释由两个取号,乘法,然后再次,当它优于1,东西。减去这样的贝克变换

But I think rand() and srand() will satisfy you.

但我认为 rand() 和 srand() 会让你满意。

For applying to your range 10-20, of course you stretch/scale the chaotic range (0;1) or (-1;1) by multiplying and offsetting so as the ouput fits your need. ;-)

为了适用于您的范围 10-20,当然您可以通过乘法和偏移来拉伸/缩放混沌范围 (0;1) 或 (-1;1),以便输出满足您的需要。;-)

回答by Maxpm

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 algorithmand seed the generator with srand.

您将需要包含algorithm生成器并使用srand.

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

当然,这只是伪随机。您将无法获得真正随机的结果,尤其是使用rand.

回答by John

Use srand() to initialise the random number generator:

使用 srand() 初始化随机数生成器:

srand(time(NULL));

http://www.cplusplus.com/reference/clibrary/cstdlib/srand/

http://www.cplusplus.com/reference/clibrary/cstdlib/srand/

Then use the rand()function to generate a sequence of random numbers.

然后使用该rand()函数生成一个随机数序列。

http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

回答by Edward Strange

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/

“生成”真正随机数的唯一方法是通过与某些随机环境因素的相互作用。本网站为您提供了一项服务:http: //www.random.org/

回答by user2808359

look this out, it might be helpful:

看看这个,它可能会有所帮助:

// random numbers generation in C++ using builtin functions
#include <iostream>

using namespace std;

#include <iomanip>

using std::setw;

#include <cstdlib>   // contains function prototype for rand

int main()
{
// loop 20 times
for ( int counter = 1; counter <= 20; counter++ ) {

    // pick random number from 1 to 6 and output it
    cout << setw( 10 ) << ( 1 + rand() % 6 );

    // if counter divisible by 5, begin new line of output
    if ( counter % 5 == 0 )
        cout << endl;

}

return 0;  // indicates successful termination

} // end main