Park Miller Random Number Generator (RNG)

I remember seeing a seeded random number generator at gskinner.com and thinking wow that has some cool functionality. I completely missed that it was a seeded random number generator. After a while I noticed polygonal had implemented a Park Miller Carta psuedo random number generator (PRNG). I remember wondering what the reason was for all this. After a bit more time I ran into the problem. While I was creating procedural content, I eventually (surprised at how long it took), wanted to reproduce some or other artefact. I could not do this reliably without seeding the sequence of numbers somehow.

I had my AHA moment! That is why everyone was concerned with seeded random number generators. Recently I have started to wonder why it is not provided by the flash API? I initially used gskinner.com’s Rndm class. It is well written and fulfilled a lot of my needs. However, as I started fiddling with statistics and other numbers more, I got interested in the actual algorithms. Which eventually led me to try recreate the histogram viewer at controul.com. This was so that I could get a visual idea for the distributions of number generating methods.

There are a lot of places to find out more about the Park Miller algorithm, here are just a few:

I have made use of the above resources to put together an abstract RNG class which implement the methods that gskinner.com created [float(), bit(), chance(), int(), etc], which all make use of an abstract .random(). This allows for the richness of these methods to be used with various implementations of .random(). This post is addressing one such implementation… the Park Miller Carta PRNG which I grabbed from polygonal. An added benefit is that the focus is now on the actual implementation of the method and not all the fringe “noise” that make it more understandable.

The code

package net.avdw.random.generator
{

	/**
	 * ...
	 * @author Andrew van der Westhuizen
	 */
	public class ParkMillerRng extends ARng
	{
		/* The original seed used by this number generator */
		protected var seed:uint;
		protected var walkingNumber:uint;

		/**
		 * Setups the random number generator given a seed.
		 * If no seed is provided then a random seed is selected.
		 * @param	seed
		 */
		public function ParkMillerRng(seed:uint = 0)
		{
			if (seed == 0)
				seed = uint.MAX_VALUE * Math.random();

			walkingNumber = this.seed = seed;
		}

		override public function random():Number
		{
			return (walkingNumber = (walkingNumber * 16807) % 2147483647) / 0x7FFFFFFF + 0.000000000233;
		}

		override public function reset():ARng
		{
			walkingNumber = seed;
			return this;
		}
	}
}
  • Pingback: Park Miller distributions()

  • http://www.redblobgames.com/ amitp

    I’ve been using Park-Miller for a while but I think the next project I’ll try http://www.pcg-random.org/

    • http://www.avanderw.co.za/ Andrew van der Westhuizen

      This looks like it can be a whole bit of fun. I would definitely use something like this when procedurally generating textures and the like. Thanks for the heads-up.