我剛剛完成了一個掃雷類型遊戲的編碼,除了每次運行應用程序時它都會產生相同的數字(我跑了3次不同的時間,將輸出保存到3個文本文件並使用diff
命令Linux,它沒有發現任何差異)。它的種子編號爲time(NULL)
,所以每次都會改變,對吧?程序在每次運行時都會生成相同的隨機數字?
這裏是我的代碼:
的main.cpp
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
#include "Minesweeper/box.h"
#include <cstdio>
int main(int argc, char** argv){
using namespace std;
bool gameOver = false;
int x, y, score = 0;
const int HEIGHT = 10;
const int WIDTH = 10;
unsigned int Time = time(0);
cout << "Welcome to Minesweeper. " << endl;
//setup grid
Box grid[10][10];
for(int i = 0; i < WIDTH; i++)
for(int n = 0; n < HEIGHT; n++){
unsigned int value = rand() %100 + 1;
cout << value << endl;
if(value <= 38){
grid[i][n].setFill(MINE);
//cout << i << "," << n << " is mined." << endl;
}
else
grid[i][n].setFill(EMPTY);
}
for(int r = 0; r < WIDTH; r++)
for(int l = 0; l < HEIGHT; l++)
if(grid[r][l].getFill() == EMPTY)
cout << r << "," << l << " - EMPTY." << endl;
else if (grid[r][l].getFill() == MINE)
cout << r << "," << l << " - MINE." << endl;
while(!gameOver){
cout << "Enter coordinates (x,y): ";
scanf("%i,%i",&x,&y);
if(grid[x][y].getFill() == MINE)
gameOver = true;
else{
cout << "Good job! (You chose " << x << "," << y << ")" << endl;
score++;
}
}
cout << "You hit a mine! Game over!" << endl;
cout << "Final score: " << score << endl;
getchar();
return EXIT_SUCCESS;
}
謝謝,那是我的一個愚蠢的錯誤。 – airplaneman19
如果OP提交批處理作業,那麼這也可能會重複。在這種情況下,多個作業同時啓動。 – jww
好點,@jww,我會添加一個註釋來達到這個效果。 – paxdiablo