Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

random number generator with x,y coordinates as seed

I'm looking for a efficient, uniformly distributed PRNG, that generates one random integer for any whole number point in the plain with coordinates x and y as input to the function.

int rand(int x, int y)

It has to deliver the same random number each time you input the same coordinate.

Do you know of algorithms, that can be used for this kind of problem and also in higher dimensions?

I already tried to use normal PRNGs like a LFSR and merged the x,y coordinates together to use it as a seed value. Something like this.

int seed = x << 16 | (y & 0xFFFF)

The obvious problem with this method is that the seed is not iterated over multiple times but is initialized again for every x,y-point. This results in very ugly non random patterns if you visualize the results.

I already know of the method which uses shuffled permutation tables of some size like 256 and you get a random integer out of it like this.

int r = P[x + P[y & 255] & 255];

But I don't want to use this method because of the very limited range, restricted period length and high memory consumption.

Thanks for any helpful suggestions!

like image 242
bakkaa Avatar asked May 10 '16 02:05

bakkaa


People also ask

How are random number generators seeded?

A random seed is a starting point in generating random numbers. A random seed specifies the start point when a computer generates a random number sequence. This can be any number, but it usually comes from seconds on a computer system's clock (Henkemans & Lee, 2001).

Does math random use a seed?

No, it is not possible to seed Math. random() . The ECMAScript specification is intentionally vague on the subject, providing no means for seeding nor require that browsers even use the same algorithm.

Which function is used to seed a new random number sequence?

srand() uses its argument seed as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is not called, the rand() seed is set as if srand(1) was called at program start. Any other value for seed sets the generator to a different starting point.

Does Google have a random number generator?

The RAND function in Google Sheets generates a random number between 0 and 1 and can be used as a random number generator in Google Sheets. The RAND function outputs a decimal number between 0 and 1, for example, 0.2760773217.


1 Answers

I found a very simple, fast and sufficient hash function based on the xxhash algorithm.

// cash stands for chaos hash :D
int cash(int x, int y){   
    int h = seed + x*374761393 + y*668265263; //all constants are prime
    h = (h^(h >> 13))*1274126177;
    return h^(h >> 16);
}

It is now much faster than the lookup table method I described above and it looks equally random. I don't know if the random properties are good compared to xxhash but as long as it looks random to the eye it's a fair solution for my purpose.

This is what it looks like with the pixel coordinates as input:

enter image description here

like image 56
bakkaa Avatar answered Sep 28 '22 01:09

bakkaa