title: Infinite while loop with a random number
body: <p>I have this code:</p>
  
  <pre><code>void generar() {
  
      while (true) {
          if (yPos == topOfTheWorld) {
              scene[xPos][yPos] = 2;
          } else if (yPos &gt;= topOfTheWorld) {
              scene[xPos][yPos] = 1;
          } else if(yPos &lt; topOfTheWorld) {
              scene[xPos][yPos] = 0;
          } else {
              scene[xPos][yPos] = 0;
          }
  
          yPos++;
  
          if(yPos&gt;worldHeight) {
              topOfTheWorld += 0;
              yPos = 0;
              xPos++;
          }
  
          if (xPos&gt;worldWidth) {
                      break;
          }
      }
  
  std::ofstream output("mapa.txt");
      for(int y=0;y&lt;worldHeight;y++) {
          for(int x=0;x&lt;worldWidth;x++) {
              output&lt;&lt;scene[x][y];
  
              if(x&lt;(worldWidth-1)){output&lt;&lt;",";}
          }
          if(y&lt;(worldHeight-1)){output&lt;&lt;std::endl;}
      }
  
  MessageBox(0, "World generation has finished!", "Finished!", MB_OK);
  
  }
  </code></pre>
  
  <p>That generates a world based in an array. But when I add:</p>
  
  <pre><code>slope = random(5)-2;
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>if(yPos == worldHeight) {
      topOfTheWorld += 0; //There would be the slope var...
  
  if(yPos == worldHeight) {
      slope = random(5)-2;
      topOfTheWorld += slope;
  </code></pre>
  
  <p>For some reason the <code>while</code> becomes an infinite loop, and I don't know why.</p>
  
  <p>(Random Function)</p>
  
  <pre><code>#include &lt;time.h&gt;
  #include &lt;windows.h&gt;
  
  int random(int n = 0) {
  
  srand(time(NULL));
  
  if(n!=0){
  return rand() % n;
  } else {
  return rand();
  }
  
  }
  </code></pre>
  
  <p>(Variables)</p>
  
  <pre><code>const int worldWidth = 50;
  const int worldHeight = 26;
  int topOfTheWorld = worldHeight/2;
  int xPos = 0;
  int yPos = 0;
  int scene[worldWidth][worldHeight];
  int slope;
  </code></pre>
  
  <p>What can I do?</p>