6

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.

JamieEclipse
  • 83
  • 1
  • 8
  • You see clear patterns in the numbers generated by java.util.Random? Care to elaborate on what those patterns are? – corsiKa Oct 12 '11 at 21:27
  • There are patterns in the terrain, repeating both horizontally and vertically. – JamieEclipse Oct 13 '11 at 16:57
  • 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 Oct 13 '11 at 17:31

1 Answers1

7

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

Piotr Praszmo
  • 16,785
  • 1
  • 51
  • 60
  • I'll try this as soon as I get home, thanks very much. Before I just paste that in I'm going to have to research java operators, the existence of ">>>" has blown my mind lol. – JamieEclipse Oct 13 '11 at 09:03
  • I used that in conjunction with the Random function, so now each node has a series of random numbers at its disposal. :D Just an aside to stop some confusion, I think the repetition may have been due in part (but not completely) to a faulty dictionary lookup function I wrote. However that would only explain some symmetry, not repetition. – JamieEclipse Oct 16 '11 at 01:07