# random tile map generation

• 09-20-2013, 08:26 PM
HelloWorld1234
random tile map generation
Hi, I am making a 2d game with a tiled map, and I want to make a random map generator, but I can manage to do it. I have tried google, but have trouble finding what I want. could someone please me give a pointer to somewhere I can learn about this? :) thanks in advance
• 09-21-2013, 07:37 PM
AndrewM16921
Re: random tile map generation
Look into Perlin Noise. It's commonly used for generating random maps that are consistent over large spaces.

I just did some digging around in some old code I wrote, so I'll share this example with you:

(understanding the math behind it isn't as necessary as understanding how to use it)

Code:

```package org.nubcraft.math.random; import java.util.Random; public class PerlinNoise {         private final long seed;         private final Random rand;         private final int octave;                 public PerlinNoise(final long seed, final int octave)         {                 this.seed = seed;                 this.octave = octave;                 rand = new Random();         }                 public double getNoiseLevelAt(final int x, final int z)         {                 final int xmin = (int)((double)x / octave);                 final int xmax = xmin + 1;                 final int zmin = (int)((double)z / octave);                 final int zmax = zmin + 1;                                 final double ra = getRandomAt(xmin, zmin);                 final double rb = getRandomAt(xmax, zmin);                 final double rc = getRandomAt(xmax, zmax);                 final double rd = getRandomAt(xmin, zmax);                                 final double ret = cosineInterpolate(                                 cosineInterpolate((float)ra, (float)rb, (float)(x - xmin * octave) / octave),                                 cosineInterpolate((float)rd, (float)rc, (float)(x - xmin * octave) / octave),                                 ((float)z - (float)zmin * (float)octave) / (float)octave);                 return ret;         }                 private final double getRandomAt(final int x, final int z)         {                 final double s = 10000 * (Math.sin(x) + Math.cos(z) + Math.tan(seed));                 rand.setSeed((long)s);                 final double ret = rand.nextDouble();                 return ret;         }                 private final float cosineInterpolate(final float a, final float b, final float x)         {                 final float ft = (float)(x * Math.PI);                 final float f = (float)((1f - Math.cos(ft)) * .5f);                 final float ret = a * (1f - f) + b * f;                 return ret;         } }```
• 09-22-2013, 12:01 PM
HelloWorld1234
Re: random tile map generation
I get decent results, but I read somewhere that perlin noise uses 7 different octaves that creates in total 7 different noises and combines them to create one final noise. this example only uses octave, do you know how to use all of them? and thx for the help :)
• 09-22-2013, 03:39 PM
AndrewM16921
Re: random tile map generation
I may be wrong, but I believe you can simply add multiple noises together. I remember what I use to do is have a general shape for the landscape (larger octave) and then add detail by adding noise from a smaller octave. so it might be something like this:

noise1 = new PerlinNoise(seed, 64);
noise2 = new PerlinNoise(seed, 8);

int height = 10 + 64*noise1.get(x, y) + 8*noise2.get(x, y);
• 09-22-2013, 04:09 PM
HelloWorld1234
Re: random tile map generation
I think that made it a bit better, I'll just have to tweak it a little bit. Thanks for the help! :)
• 09-22-2013, 05:06 PM
AndrewM16921
Re: random tile map generation
No problem, glad I could be of some help :)