Parametric Random Function For 2D Noise Generation

喜你入骨 提交于 2019-12-18 06:56:49

问题


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 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:



来源:https://stackoverflow.com/questions/7746743/parametric-random-function-for-2d-noise-generation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!