6
votes

I'm attempting to generate infinite random terrain. The terrain should generate the same every time given the same seed.

I've tried using Java's Random function, creating the seed using various functions of the x and y co-ordinates of the given node on the terrain grid. Such as x*y+x+y+seed, 20*x+30*y etc.

The problem with this approach is that I always see clear patterns in the numbers generated.

So basically what I want is: f(x,y) = Random Number

It would be helpful if the above function could include a seed of some sort, making it: f(x,y,seed) = Random Number

I will need to generate several numbers for each x,y combination but it should be easy enough to derive additional numbers once I have the above function. These will serve to dictate height of the terrain, and what features (buildings, trees) will be present.

Please, no mention of Perlin Noise or other such methods. My problem isn't making the noise look good, it's getting reliably "random" noise.

Thanks, Jamie.

1
You see clear patterns in the numbers generated by java.util.Random? Care to elaborate on what those patterns are? - corsiKa
There are patterns in the terrain, repeating both horizontally and vertically. - JamieEclipse
Can you upload an example of these patterns, and the code that generated them? I'm fairly interested in it; this is one of my hobbies too. (Used to be something I did at a research firm I used to work at, too.) - corsiKa

1 Answers

7
votes

You are looking for hashing function. Try one of those:

http://www.concentric.net/~ttwang/tech/inthash.htm

Here's example usage:

int hash32shift(int key)
{
  key = ~key + (key << 15); // key = (key << 15) - key - 1;
  key = key ^ (key >>> 12);
  key = key + (key << 2);
  key = key ^ (key >>> 4);
  key = key * 2057; // key = (key + (key << 3)) + (key << 11);
  key = key ^ (key >>> 16);
  return key;
}

int noise(int x, int y, int seed)
{
    return hash32shift(seed+hash32shift(x+hash32shift(y)));
}

And, it looks like this:

noise