Search code examples
javarandomnoise

How to get a random number from a 2d coordinate and a seed?


I'm trying to make some noise based terrain generation and I need to get a random point from a a coordinate point, and a seed. Now, the hard part I'm struggling with is having the same number returned from the same point if the seed is the same. (random.nextDouble() makes it simply go to some other number).

I have tried doing some sort of equation:

return ((int) ((randomKey1.charAt((5 ^ x) % 127)) + (int) ((randomKey2
            .charAt(Math.abs(z  ^ 2 % 64) % 127)))) / 256f * 40f);

But that doesn't exactly work because if you swap x and z coordinates you get a similar number causing the terrain to look mirrored diagonally.

Solved:

double getRatCor(int x, int z) {
    double a;

    Random r = new Random(seed + (x*10000) + (z*100));
    a = r.nextDouble()*40;
    System.out.println(a);
    return a;
}

Solution

  • How about?

    /**
    * return random number with seed based on coordinates.
    */
    double locationValue(int x,int y){
        long seed = z;
        seed = x + (seed << 32); // make x and z semi-independent parts of the seed.
        Random r = new Random(seed);
        return r.nextDouble();
    }