Predictable random numbers

Started by
0 comments, last by Marty666 19 years, 6 months ago
Hi all! I'm busy on random landscapes for my artillery game and wanted to use perlin noise for this. The problem is that the only pseudo random number generator I was able to find that takes inputs is the one below. It works fine, but I am not able to seed it, since all the large numbers in it are prime numbers and I can't seem to find a way to produce a prime number from a ordinary number the client uses as seed. The formula below will only produce satisfactory results with prime numbers :(. Is there an other formula, or function in c that I can use to generate a different landscape every time (seeded) but that still lets me ask for the value at (x), or the value at (x, y)? Or do I have to make a table, which I think is a bad solution... Thanks, Marty Here is the generator I found:


float Noise1D(int x) // returns a random number between -1 and 1 that will be the same each time the same x is used as input.
{
	x = (x<<13) ^ x;
	return (1.0f - (float)((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);    
}


float Noise2D(int x, int y) // same as above, but takes x and y.
{
	int n = x + y * 57;
	n = (n<<13) ^ n;
	return (1.0 - (float)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);    
}


_____ /____ /|| | || MtY | ||_____|/Marty
Advertisement
Stupid me, found it:

float TheRandom(unsigned int seed, unsigned int x){	srand(x * seed);	return float(rand()%10000)/10000;}
_____ /____ /|| | || MtY | ||_____|/Marty

This topic is closed to new replies.

Advertisement