title: Using Perlin noise to generate a 2d tile map
body: <p>I looked all over the internet and researched Perlin noise, however, I am still confused.</p>
  
  <p>I am using java and <strong>libgdx</strong>. I have got a Perlin class to work and generate noise but I'm not sure if the values its giving are correct. How do I check it actually is outputting Perlin noise?</p>
  
  <p>If my implementation is correct I don't know where to go from there to make random terrain. How would I map Perlin noise to tiles? Currently I have 4 basic tiles; water, sand, rock, and grass.</p>
  
  <pre><code>package com.bracco.thrive.world;
  
  import com.badlogic.gdx.Gdx;
  import com.badlogic.gdx.graphics.g2d.Sprite;
  import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  import com.badlogic.gdx.graphics.g2d.TextureRegion;
  import com.badlogic.gdx.graphics.GL10;
  import com.badlogic.gdx.graphics.Texture;
  public class WorldGeneration {
  
  Perlin noise = new Perlin();
  private SpriteBatch spriteBatch;
  //private boolean debug = false;
  private TextureRegion[] regions = new TextureRegion[4];
  private Texture texture;
  
   float x = 110;
   float y = 120;
   float originX = 0;
   float originY = 16;
   float width = 16;
   float height = 16;
   float scaleX = 1;
   float scaleY = 1;
   float rotation = 1;
  
  
  @SuppressWarnings("static-access")
  public void createWorld(){
      spriteBatch = new SpriteBatch();
       texture = new Texture(Gdx.files.internal("assets/data/textures/basictextures.png"));
  
       regions[0] = new TextureRegion(texture,0,0,16,16); //grass 
       regions[1] = new TextureRegion(texture,16,0,16,16); //water
       regions[2] = new TextureRegion(texture,0,17,16,16); //sand
       regions[3] = new TextureRegion(texture,17,17,16,16); //rock
      float[][] seed =  noise.GenerateWhiteNoise(50, 50);
      for (int i = 0;i &lt; seed.length; i++){
          for ( int j = 0; j &lt; seed[i].length; j++){
              System.out.println(seed[i][j] + " ");
          }
      }
       float[][] seedE = noise.GenerateSmoothNoise( seed, 6);
       for (int i = 0;i &lt; seedE.length; i++){
              for ( int j = 0; j &lt; seedE[i].length; j++){
                  System.out.println(seedE[i][j] + " ");
              }
  
       }
       float[][] perlinNoise = noise.GeneratePerlinNoise(seedE, 8);
       for (int i = 0;i &lt; perlinNoise.length; i++){
              for ( int j = 0; j &lt; perlinNoise[i].length; j++){
                  System.out.println(perlinNoise[i][j] + " ");
              }
          }
  }
  
  public void render(){
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      spriteBatch.begin();
      //spriteBatch.draw(texture, 0,  0,  16, 16);
      for (int i = 0; i &lt; regions.length; i++){
          spriteBatch.draw(regions[i],75 * (i + 1),100);
      }
      spriteBatch.end();
  }
  
  
  
  }
  </code></pre>
  
  <p><br></p>
  
  <pre><code>package com.bracco.thrive.world;
  
      import java.util.Random;
  
      public class Perlin {
  
      public static float[][] GenerateWhiteNoise(int width,int height){
  
          Random random = new Random((long) (Math.round(Math.random() * 100 * Math.random() * 10))); //Seed to 0 for testing
          float[][] noise = new float[width][height];
  
          for (int i = 0; i &lt; width; i++)
          {
              for (int j = 0; j &lt; height; j++){
                  noise[i][j] = (float)(Math.random() % 1);
              }
          }
  
          return noise;
      }
  
      float[][] GenerateSmoothNoise(float[][] baseNoise, int octave)
      {
         int width = baseNoise.length;
         int height = baseNoise.length;
  
         float[][] smoothNoise = new float[width][height];
  
         int samplePeriod = 1 &lt;&lt; octave; // calculates 2 ^ k
         float sampleFrequency = 1.0f / samplePeriod;
  
         for (int i = 0; i &lt; width; i++)
         {
            //calculate the horizontal sampling indices
            int sample_i0 = (i / samplePeriod) * samplePeriod;
            int sample_i1 = (sample_i0 + samplePeriod) % width; //wrap around
            float horizontal_blend = (i - sample_i0) * sampleFrequency;
  
            for (int j = 0; j &lt; height; j++)
            {
               //calculate the vertical sampling indices
               int sample_j0 = (j / samplePeriod) * samplePeriod;
               int sample_j1 = (sample_j0 + samplePeriod) % height; //wrap around
               float vertical_blend = (j - sample_j0) * sampleFrequency;
  
               //blend the top two corners
               float top = Interpolate(baseNoise[sample_i0][sample_j0],
                  baseNoise[sample_i1][sample_j0], horizontal_blend);
  
               //blend the bottom two corners
               float bottom = Interpolate(baseNoise[sample_i0][sample_j1],
                  baseNoise[sample_i1][sample_j1], horizontal_blend);
  
               //final blend
               smoothNoise[i][j] = Interpolate(top, bottom, vertical_blend);
            }
         }
  
         return smoothNoise;
      }
  
      float Interpolate(float x0, float x1, float alpha)
      {
         return x0 * (1 - alpha) + alpha * x1;
      }
  
      float[][] GeneratePerlinNoise(float[][] baseNoise, int octaveCount)
      {
         int width = baseNoise.length;
         int height = baseNoise[0].length;
  
         float[][][] smoothNoise = new float[octaveCount][][]; //an array of 2D arrays containing
  
         float persistance = 0.5f;
  
         //generate smooth noise
         for (int i = 0; i &lt; octaveCount; i++)
         {
             smoothNoise[i] = GenerateSmoothNoise(baseNoise, i);
         }
  
          float[][] perlinNoise = new float[width][height];
          float amplitude = 1.0f;
          float totalAmplitude = 0.0f;
  
          //blend noise together
          for (int octave = octaveCount - 1; octave &gt;= 0; octave--)
          {
             amplitude *= persistance;
             totalAmplitude += amplitude;
  
             for (int i = 0; i &lt; width; i++)
             {
                for (int j = 0; j &lt; height; j++)
                {
                   perlinNoise[i][j] += smoothNoise[octave][i][j] * amplitude;
                }
             }
          }
  
         //normalisation
         for (int i = 0; i &lt; width; i++)
         {
            for (int j = 0; j &lt; height; j++)
            {
               perlinNoise[i][j] /= totalAmplitude;
            }
         }
  
         return perlinNoise;
      }
  }
  </code></pre>