Positional Pseudorandom Noise

Written by me (Anixias). Warning: This does not work well, and you should instead look up squirrel3 or see the GDC video on YouTube on noise to replace RNGs.

function pseudorandom_position(x, y)
{
	var z = ((x + 10) * 6587 + (y + 43) * 9811) % 1;
	
	var xt = (dcos(360 * 8 * x) + 1) / 2;
	var yt = (dsin(360 * 8 * y) + 1) / 2;
	var zt = abs(dsin(360 * 8 * z)) % 1;
	
	return (xt + yt + zt) % 1;
}

Usage

Call this function on a position (integer coordinates) to get a seemingly random number from 0 – 1 (never equals 1). You can use this to, without messing with the random seed and affecting subsequent random calls, render tiles on a grid with random variations. Multiply the returned value by the number of subimages (in a grass tile, for example), then floor that value to get a “random” subimage to use for rendering.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s