C++ 每次运行程序时都是相同的随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7748071/
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 17:25:31  来源:igfitidea点击:

Same random numbers every time I run the program

c++random

提问by soniccool

My random numbers that output, output in the same sequence every time I run my game. Why is this happening?

我每次运行游戏时输出的随机数都以相同的顺序输出。为什么会这样?

I have

我有

#include <cstdlib> 

and am using this to generate the random numbers

并使用它来生成随机数

randomDiceRollComputer = 1 + rand() % 6;

回答by Mysticial

You need to seed your random number generator:

你需要为你的随机数生成器做种子:

Try putting this at the beginning of the program:

尝试将其放在程序的开头:

srand ( time(NULL) );

Note that you will need to #include <ctime>.

请注意,您将需要#include <ctime>.

The idea here is to seed the RNG with a different number each time you launch the program. By using time as the seed, you get a different number each time you launch the program.

这里的想法是在每次启动程序时为 RNG 设置不同的编号。通过使用时间作为种子,您每次启动程序时都会得到一个不同的数字。

回答by tune2fs

You need to give the randum number generator a seed. This can be done by taking the current time, as this is hopefully some kind of random.

你需要给随机数生成器一个种子。这可以通过获取当前时间来完成,因为这希望是某种随机的。

#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    int  r;
    srand(time(0));
    r = rand();
    return 0;
} 

回答by Keith Thompson

The rand()function is specifically required to produce the same sequence of numbers when seeded with a given seed (by calling srand()); each possible seed value specifies a sequence. And if you never call srand(), you get the same sequence you would have gotten by calling srand(1)before any call to rand().

rand()当用给定的种子(通过调用srand())播种时,该函数特别需要产生相同的数字序列;每个可能的种子值指定一个序列。如果您从不调用srand(),您将获得与srand(1)在任何调用之前调用所获得的序列相同的序列rand()

(This doesn't apply across different C or C++ implementations.)

(这不适用于不同的 C 或 C++ 实现。)

This can be useful for testing purposes. If there's a bug in your program, for example, you can reproduce it by re-running it with the same seed, guaranteeing that (barring other unpredictable behaviors) you'll get the same sequence of pseudo-random numbers.

这对于测试目的很有用。例如,如果您的程序中存在错误,您可以通过使用相同的种子重新运行它来重现它,保证(除非其他不可预测的行为)您将获得相同的伪随机数序列。

Calling srand(time(NULL))is the usual recommended way to get more or less unpredictable pseudo-random numbers. But it's not perfect. If your program runs twice within the same second, you'll probably get the same sequence, because time()(typically) has a resolution of 1 second. And typical `rand() implementations are notgood enough for cryptographic use; it's too easy for an attacker to guess what numbers you're going to get.

调用srand(time(NULL))是获得或多或少不可预测的伪随机数的常用推荐方法。但它并不完美。如果您的程序在同一秒内运行两次,您可能会得到相同的序列,因为time()(通常)具有 1 秒的分辨率。典型的`rand() 实现对于加密使用来说还不够好;攻击者很容易猜出你将得到什么数字。

There are a number of other random number implementations. Linux systems have two pseudo-devices, /dev/randomand /dev/urandom, from which you can read reasonably high-quality pseudo-random byte values. Some systems might have functions like random(), drand48(), and so forth. And there are numerous algorithms; I've heard good things about the Mersenne Twister.

还有许多其他随机数实现。Linux 系统有两个伪设备,/dev/random/dev/urandom,您可以从中读取相当高质量的伪随机字节值。一些系统可能有这样的功能random()drand48()以及等等。并且有无数的算法;我听说过关于Mersenne Twister 的好消息

For something like a game, where you don't expect or care about players trying to cheat, srand(time(NULL))and rand()is probably good enough. For more serious purposes, you should get advice from someone who knows more about this stuff than I do.

对于诸如游戏之类的东西,您不期望或不关心试图作弊的玩家,srand(time(NULL))并且rand()可能已经足够好了。为了更严肃的目的,你应该从比我更了解这些东西的人那里得到建议。

Section 13 of the comp.lang.c FAQhas some very good information about pseudo-random number generation.

comp.lang.c FAQ 的第 13 节有一些关于伪随机数生成的非常好的信息。

回答by jgon

Pseudorandom number generators take a starting number, or seed, and then generate the next number in the sequence from this. That's why they're called pseudorandom, because if they always use the same starting value, they will generate the same sequence of numbers like the C standard lib generator does. This can be fixed by giving the generator a starting value that will change the next time the program is run like the current time.

伪随机数生成器采用一个起始数字或种子,然后从中生成序列中的下一个数字。这就是为什么它们被称为伪随机,因为如果它们总是使用相同的起始值,它们将生成与 C 标准库生成器相同的数字序列。这可以通过给生成器一个起始值来解决,该值将在下次程序运行时像当前时间一样改变。

Anyway, the code you're looking for like others have said is:

无论如何,您正在寻找的代码就像其他人所说的那样:

srand(time(0)); //Seed the generator, give it a starting value