I am trying to create a seeded random number generator that generates a number based on coordinates.
Basically, given the same seed, every time this function is called, it will give the same random number for the same coordinates.Code:int rand(int x, int y);
What I am really aiming for is some sort of seeded Noise function that will accept any X and Y values.
Some requirements.
Ideally, resulting values would be either between 0 and RAND_MAX, then I can manipulate as required.
(-1,1) and (1,-1) must return different values.
I had thought to do something like the following:
Now this does work, however I noticed some weirdness. The resulting values actually seem to be clustered. So if I use (1, 1), (-1, -1), (-1, 1), (1, -1), each returns a different value, however all values are within a small range (eg. 5000, 5010, 4996, 4985). I would expect to see each value being significantly different.Code:int rand(int x, int y)
{
srand(x + seed);// Seed based on X
srand(y + rand());// Seed based on Y and the results of X
return rand();// Return Random value
}
Does anyone have another suggestion for how this could be accomplished? Something simpler? Is this clustering effect a flaw in the C++ rand/srand? I did notice that if I simply call srand(seed), and rand(), the resulting random number seems to get higher as the seed gets higher. That doesn't seem very random to me.
