程序在每次运行时生成相同的随机数? [英] Program is generating same random numbers on each run?

查看:179
本文介绍了程序在每次运行时生成相同的随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了一个Minesweeper类型游戏的编码,一切都很好,除了每次我运行应用程序,它生成相同的数字(我跑了3个不同的时间,保存输出到3个文本文件,并使用 diff 命令在Linux中,它没有发现任何差异)。 time(NULL),因此它应该每次都改变。



p>

main.cpp

  #include< iostream> 
#include< cstdlib>
#include< time.h>
#include< string>
#includeMinesweeper / 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<< 欢迎扫雷。< endl;


//设置网格
框格[10] [10];

for(int i = 0; i for(int n = 0; n unsigned int value = rand ()%100 + 1;
cout<<值<< endl;
if(value< = 38){
grid [i] [n] .setFill(MINE);
// cout<< i<< ,< n<< 是开采的。 << endl;
}
else
grid [i] [n] .setFill(EMPTY);
}

for(int r = 0; r for(int l = 0; l< HEIGHT; l ++)
if (grid [r] [l] .getFill()== EMPTY)
cout< r<< ,< 1<< - EMPTY。 << endl;
else if(grid [r] [l] .getFill()== MINE)
cout< r<< ,< 1<< - MINE。 << endl;

while(!gameOver){
cout< 输入坐标(x,y):;
scanf(%i,%i,& x,& y);
if(grid [x] [y] .getFill()== MINE)
gameOver = true;
else {
cout<< Good job!(You choose<< x<<,<< y<<)< endl;
score ++;
}
}

cout<< 你打我的一个! << endl;
cout<< 最终分数:<得分< endl;
getchar();

return EXIT_SUCCESS;
}


解决方案


按时间播种(NULL)


如果是,我看不到它。事实上,在你的代码中搜索它什么都不返回。默认行为,如果你没有显式种子,就像你使用值为1的种子一样。



你需要显式声明如下: / p>

  srand(time(NULL));在 main 开始处的



<请务必每次一次一次。



请记住,这取决于当前时间 - 如果您启动多个



从C标准(C ++基于这些标准)开始,同样的工作在同一秒钟(或任何你的时间分辨率)兼容性特性):


srand函数使用参数作为后续调用返回的伪随机数序列的种子到兰特。如果然后使用相同的种子值调用srand,则将重复伪随机数序列。如果在对srand进行任何调用之前调用rand,那么将会生成与srand第一次调用种子值为1时相同的序列。



I just finished coding a Minesweeper type game, and everything's good except for that each time I run the application, it generates the same number (I ran it 3 different times, saved the output to 3 text files and used the diff command in Linux, it didn't find any differences). It's seeded by time(NULL) so it should change every time, right?

Here's my code:

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;
}

解决方案

It's seeded by time(NULL)

If it is, I can't see it. In fact, a search for it in your code returns nothing. The default behaviour, if you don't explicitly seed, is the same as if you had seeded it with the value 1.

You need to explicitly state something like:

srand (time (NULL));

at the start of main somewhere (and make sure you do this once and once only).

Though keep in mind this makes it dependent on the current time - if you start multiple jobs in the same second (or whatever your time resolution is), they'll start with the same seed.

From the C standard (on which C++ is based for these compatibility features):

The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.

这篇关于程序在每次运行时生成相同的随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆