title: How to implement midpoint displacement
body: <p>I'm trying to implement procedural generation in my game. I want to really grasp and understand all of the algorithms nessecary rather than simply copying/pasting existing code. In order to do this I've attempted to implement 1D midpoint displacement on my own. I've used the information <a href="http://gameprogrammer.com/fractal.html" rel="nofollow noreferrer">here</a> to write and guide my code. Below is my completed code, it doesn't throw an error but that results don't appear correct.</p>
  
  <pre><code>srand(time(NULL));
  
  const int lineLength = 65;
  float range = 1.0;
  float displacedLine[lineLength];
  
  for (int i = 0; i &lt; lineLength; i++)
  {
      displacedLine[i] = 0.0;
  }
  
  for (int p = 0; p &lt; 100; p++)
  {
      int segments = 1;
      for (int i = 0; i &lt; (lineLength / pow(2, 2)); i++)
      {
          int segs = segments;
          for (int j = 0; j &lt; segs; j++)
          {
              int x = floor(lineLength / segs);
              int start = (j * x) + 1;
              int end = start + x;
              if (i == 0)
              {
                  end--;
              }
              float lo = -range;
              float hi = +range;
              float change = lo + static_cast &lt;float&gt; (rand()) / (static_cast &lt;float&gt; (RAND_MAX / (hi - lo)));
  
              int center = ((end - start) / 2) + start;
              displacedLine[center - 1] += change;
              segments++;
          }
          range /= 2;
      }
  }
  </code></pre>
  
  <p>Where exactly have I made mistakes and how might I correct them?</p>
  
  <p>I'm getting results like this: </p>
  
  <p><img src="https://i.stack.imgur.com/zqg3y.png" alt="My Results"></p>
  
  <p>But I was expecting results like this: </p>
  
  <p><img src="https://i.stack.imgur.com/8eFWW.gif" alt="Expected Results"></p>