无限while循环随机数

无限while循环随机数

本文介绍了无限while循环随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

  void generar(){

while(true){
if(yPos == topOfTheWorld){
scene [xPos] [yPos] = 2;
} else if(yPos> = topOfTheWorld){
scene [xPos] [yPos] = 1;
} else if(yPos< topOfTheWorld){
scene [xPos] [yPos] = 0;
} else {
scene [xPos] [yPos] = 0;
}

yPos ++;

if(yPos> worldHeight){
topOfTheWorld + = 0;
yPos = 0;
xPos ++;
}

if(xPos> worldWidth){
break;
}
}

std :: ofstream output(mapa.txt);
for(int y = 0; y< worldHeight; y ++){
for(int x = 0; x output<< scene [x] [y ];

if(x } < std :: endl;}
}

MessageBox(0,World generation has finished!,Finished!,MB_OK)

}

生成一个基于数组的世界。但是当我添加:

  slope = random(5) 

To:

 code> if(yPos == worldHeight){
topOfTheWorld + = 0; //将有斜率变量...

if(yPos == worldHeight){
slope = random(5)-2;
topOfTheWorld + = slope;

由于某种原因, while

(随机函数)

  #include< time.h> 
#include< windows.h>

int random(int n = 0){

srand(time(NULL));

if(n!= 0){
return rand()%n;
} else {
return rand()
}

}

(变量)

  const int worldWidth = 50; 
const int worldHeight = 26;
int topOfTheWorld = worldHeight / 2;
int xPos = 0;
int yPos = 0;
int scene [worldWidth] [worldHeight];
int slope;

我能做什么?

解决方案

您显示 scene 定义为:

  int scene [worldWidth] [worldHeight];但是,您的代码具有:



<$>


p $ p> if(xPos> worldWidth){
break;
}

这意味着你会在数组边界之外写一个值 xPos == worldWidth ,这会导致未定义的行为。添加 slope 变量可能会导致变量组织发生变化,导致未定义的行为会影响和/或所有循环控制变量的值。



要修复,您应该更改错误检查:

  if(xPos& = worldWidth){
break;
}

您已经使用代码编辑了您的问题, > yPos 以类似的方式检查不正确。


I have this code:

void generar() {

    while (true) {
        if (yPos == topOfTheWorld) {
            scene[xPos][yPos] = 2;
        } else if (yPos >= topOfTheWorld) {
            scene[xPos][yPos] = 1;
        } else if(yPos < topOfTheWorld) {
            scene[xPos][yPos] = 0;
        } else {
            scene[xPos][yPos] = 0;
        }

        yPos++;

        if(yPos>worldHeight) {
            topOfTheWorld += 0;
            yPos = 0;
            xPos++;
        }

        if (xPos>worldWidth) {
                    break;
        }
    }

std::ofstream output("mapa.txt");
    for(int y=0;y<worldHeight;y++) {
        for(int x=0;x<worldWidth;x++) {
            output<<scene[x][y];

            if(x<(worldWidth-1)){output<<",";}
        }
        if(y<(worldHeight-1)){output<<std::endl;}
    }

MessageBox(0, "World generation has finished!", "Finished!", MB_OK);

}

That generates a world based in an array. But when I add:

slope = random(5)-2;

To:

if(yPos == worldHeight) {
    topOfTheWorld += 0; //There would be the slope var...

if(yPos == worldHeight) {
    slope = random(5)-2;
    topOfTheWorld += slope;

For some reason the while becomes an infinite loop, and I don't know why.

(Random Function)

#include <time.h>
#include <windows.h>

int random(int n = 0) {

srand(time(NULL));

if(n!=0){
return rand() % n;
} else {
return rand();
}

}

(Variables)

const int worldWidth = 50;
const int worldHeight = 26;
int topOfTheWorld = worldHeight/2;
int xPos = 0;
int yPos = 0;
int scene[worldWidth][worldHeight];
int slope;

What can I do?

解决方案

You show that scene is defined as:

int scene[worldWidth][worldHeight];

However, your code has this:

        if (xPos>worldWidth) {
                    break;
        }

Which means you will actually write a value outside the array boundary when xPos == worldWidth, and this causes undefined behavior. Adding the slope variable may cause your variable organization to change in a way that the undefined behavior ends up affecting the values of and or all of your loop control variables.

To fix, you should change the erroneous check with:

        if (xPos>=worldWidth) {
                    break;
        }

You have since edited your question with code that makes your yPos check incorrect in a similar way.

这篇关于无限while循环随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:07