C++ rand() 每次程序运行时返回相同的数字

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

rand() returns the same number each time the program is run

c++random

提问by codedude

In this rather basic C++ code snippet involving random number generation:

在这个涉及随机数生成的相当基本的 C++ 代码片段中:

include <iostream>
using namespace std;

int main() {
    cout << (rand() % 100);
    return 0;
}

Why am I always getting an output of 41? I'm trying to get it to output some random number between 0 and 100. Maybe I'm not understanding something about how the rand function works?

为什么我总是得到 41 的输出?我试图让它输出 0 到 100 之间的一些随机数。也许我不了解 rand 函数的工作原理?

采纳答案by Aaron

You need to "seed" the generator. Check out this short video, it will clear things up.

您需要“播种”发电机。看看这个短视频,它会清除一切。

https://www.thenewboston.com/videos.php?cat=16&video=17503

https://www.thenewboston.com/videos.php?cat=16&video=17503

回答by 0x90

You need to change the seed.

您需要更改种子

int main() {

    srand(time(NULL));
    cout << (rand() % 101);
    return 0;
}

the srandseeding thing is true also for a clanguage code.

srand对于c语言代码,播种也是如此。



See also: http://xkcd.com/221/

另见:http: //xkcd.com/221/

回答by DuncanACoulter

For what its worth you are also only generating numbers between 0 and 99 (inclusive). If you wanted to generate values between 0 and 100 you would need.

对于它的价值,您也只生成 0 到 99(含)之间的数字。如果您想生成 0 到 100 之间的值,则需要。

rand() % 101

in addition to calling srand() as mentioned by others.

除了像其他人提到的那样调用 srand() 。

回答by DuncanACoulter

You are not seeding the number.

你不是在播种号码。

Use This:

用这个:

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    srand(static_cast<unsigned int>(time(0)));
    cout << (rand() % 100) << endl;
    return 0;
}

You only need to seed it once though. Basically don't seed it every random number.

不过你只需要播种一次。基本上不要为每个随机数播种。

回答by DuncanACoulter

srand()seeds the random number generator. Without a seed, the generator is unable to generate the numbers you are looking for. As long as one's need for random numbers is not security-critical (e.g. any sort of cryptography), common practice is to use the system time as a seed by using the time()function from the <ctime>library as such: srand(time(0)). This will seed the random number generator with the system time expressed as a Unix timestamp (i.e. the number of seconds since the date 1/1/1970). You can then use rand()to generate a pseudo-random number.

srand()随机数生成器的种子。如果没有种子,生成器将无法生成您要查找的数字。只要对随机数的需求不是安全关键的(例如任何类型的密码学),通常的做法是使用系统时间作为种子,使用库中的time()函数,<ctime>如下所示:srand(time(0)). 这将为随机数生成器提供以 Unix 时间戳表示的系统时间(即自 1/1/1970 以来的秒数)。然后您可以使用它rand()来生成一个伪随机数。

Here is a quote from a duplicate question:

这是一个重复问题的引述:

The reason is that a random number generated from the rand() function isn't actually random. It simply is a transformation. Wikipedia gives a better explanation of the meaning of pseudorandom number generator: deterministic random bit generator. Every time you call rand() it takes the seed and/or the last random number(s) generated (the C standard doesn't specify the algorithm used, though C++11 has facilities for specifying some popular algorithms), runs a mathematical operation on those numbers, and returns the result. So if the seed state is the same each time (as it is if you don't call srand with a truly random number), then you will always get the same 'random' numbers out.

If you want to know more, you can read the following:

http://www.dreamincode.net/forums/topic/24225-random-number-generation-102/

http://www.dreamincode.net/forums/topic/29294-making-pseudo-random-number-generators-more-random/

原因是从 rand() 函数生成的随机数实际上并不是随机的。这简直就是一种转变。维基百科对伪随机数生成器的含义给出了更好的解释:确定性随机位生成器。每次调用 rand() 时,它都会获取生成的种子和/或最后一个随机数(C 标准没有指定使用的算法,尽管 C++11 具有指定一些流行算法的功能),运行一个对这些数字进行数学运算,并返回结果。因此,如果种子状态每次都相同(就像您不使用真正的随机数调用 srand 一样),那么您将始终得到相同的“随机”数。

如果您想了解更多,可以阅读以下内容:

http://www.dreamincode.net/forums/topic/24225-random-number-generation-102/

http://www.dreamincode.net/forums/topic/29294-making-pseudo-random-number-generators-more-random/

回答by Alexey Ryabinin

random functions like borland complier

像 borland 编译器这样的随机函数

using namespace std;

int sys_random(int min, int max) {
   return (rand() % (max - min+1) + min);
}

void sys_randomize() {
    srand(time(0));
}