Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尝试生成随机整数数组,每次运行都会得到相同的列表。C++;_C++_Random_Negative Number - Fatal编程技术网

C++ 尝试生成随机整数数组,每次运行都会得到相同的列表。C++;

C++ 尝试生成随机整数数组,每次运行都会得到相同的列表。C++;,c++,random,negative-number,C++,Random,Negative Number,为什么每次运行时都会产生相同的数组 int arr[100]; for (int i = 0; i < 100; i++) { arr[i] = rand() % 11 + (-5); } int-arr[100]; 对于(int i=0;i

为什么每次运行时都会产生相同的数组

int arr[100];
for (int i = 0; i < 100; i++) {
    arr[i] = rand() % 11 + (-5); 
}
int-arr[100];
对于(int i=0;i<100;i++){
arr[i]=rand()%11+(-5);
}
您需要为随机数生成器设置种子,例如使用当前时间

#include <time.h>
#include <stdlib.h>
srand(time(NULL));
#包括
#包括
srand(时间(空));

使用
std::srand(std::time(0))
设置随机种子。要获得可重复的结果,请使用相同的种子,请参阅。

对随机数使用
srand
多年来都不是最佳的基于标准的解决方案。对随机数使用
srand
多年来都不是最佳的基于标准的解决方案。好吧,这对我来说是新闻-什么是更好的解决方案?@DavidStutz请看这里:啊,好的,对于C++11-感谢您的提醒;)