【发布时间】:2012-03-14 08:31:09
【问题描述】:
可能重复:
What's the Right Way to use the rand() Function in C++?
我一直在学习如何使用 rand() 函数,并用 C++ 编写了一个小猜谜游戏,如下所示,但问题是,无论我编译多少次程序,生成的数字都是相同的 - > 41
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main()
{
int x = rand()%100;
int y=0;
cout << "Ghiceste numarul!" << endl;
cin >> y;
while(y != x) {
if(y > x) {
cout << "Numarul tau este prea mare! Incearca un numar mai mic!" << endl;
cin >> y;
}
if(y < x) {
cout << "Numarul tau este prea mic!" << endl;
cin >> y;
}
if (y == x) {
cout << "FELICITARI, AI GHICIT NUMARUL!\n";
return 0;
}
}
}
我也尝试改变 rand() 的最大值,只要我把它
有什么想法吗?对于为什么会发生这种情况,我没有线索。我正在使用 CodeBlocks IDE 并尝试重建 (CTRL+F11)
【问题讨论】: