【问题标题】:Infinite while loop with a random number带有随机数的无限while循环
【发布时间】:2013-09-22 03:09:35
【问题描述】:

我有这个代码:

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

}

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

slope = random(5)-2;

收件人:

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

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;

我能做什么?

【问题讨论】:

  • 删除 goto: 错误的编程结构
  • 一开始不要使用goto
  • 不要在你的随机函数中使用 srand
  • 详细阐述@Bathsheba 的评论:只需使用break
  • @thomas Ruiz 在这里是正确的;重复调用 srand 完全破坏了 randomn 序列。

标签: c++ random while-loop


【解决方案1】:

你证明scene被定义为:

int scene[worldWidth][worldHeight];

但是,您的代码有这样的:

        if (xPos>worldWidth) {
                    break;
        }

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

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

        if (xPos>=worldWidth) {
                    break;
        }

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

【讨论】:

  • 简而言之,我应该怎么做? (我是西班牙人,我不确定我是否完全理解你)
  • 我已将 xPos == worldWidth 更改为 xPos &gt;= worldWidth 它仍然是无限的.. :/
  • 一旦您发布了在您的问题中产生错误的确切代码,请不要在之后更改它,因为您只会使答案无效。人们不喜欢回答不断变化的问题。
  • 好吧.. 抱歉 xD 我会放一部分帖子说:代码已更新 xD
  • 非常感谢!! :D 我永远不会理解逻辑比较器... xD 对我来说很难,我知道为什么... :/
【解决方案2】:

在您的 random 函数中重复调用 srand

修复:-

void generar() {

srand(time(NULL)); //Remove srand() from random(), add it here
bool finished = false;

    while (!finished) {
        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) {
           // slope = random(5)-2; your random call
            topOfTheWorld += 0;
            yPos = 0;
            xPos++;
        }

        if (xPos>worldWidth) {
            finished = true;
           //goto Guardar; not required, 
          //also use of goto is bad programming practice
        }
    }

【讨论】:

  • 好的,谢谢!但它仍然是无限的! :P
  • @Ikillnukes 好吧,我只是指出了错误的事情,如果仍然是无限的,那么可能重新审视你的逻辑,顺便说一句 worldWidth 是什么?
  • worldHeight 是一个整数...const int worldWidth = 50; const int worldHeight = 26; int topOfTheWorld = worldHeight/2; int xPos = 0; int yPos = 0; int scene[worldWidth][worldHeight]; int slope;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 2023-01-28
  • 2021-12-17
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多