【发布时间】:2014-06-28 03:10:14
【问题描述】:
我只是用 C++ 编写了下面的代码,但我有一个问题:随机数总是一样的..!! 这是我的代码和屏幕截图:
#include <iostream>
using namespace std;
int main() {
cout << "I got a number in my mind... can you guess it?" << endl;
int random;
random = rand() % 20 + 1;
cout << random << endl;
system("pause");
return 0;
}
【问题讨论】:
-
您必须在程序开始时播种随机数生成器。包括 cstdlib 头文件并添加 srand( time(0) );在 main 函数的开头。
-
@AbhishekBansal +1,试试 srand(time(0));
-
@JossefHarush 已编辑。
-
答案here
标签: c++ visual-studio random