可能重複:
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()的最大值,只要我把它放到< 41.
任何想法?至於爲什麼會發生這種情況,我沒有線索。我使用的代碼塊IDE,我試圖重建(CTRL + F11)
+1 [SSCCE(http://sscce.org/)。 – 2012-02-23 22:15:40