Tuesday, October 4, 2011

A Random-Number Generator

There are times when a programmer needs to pick a number at random and the random-number generator was introduce to fill the need. The trick used is to multiply two numbers together and select a central string of digits. Below is a simple random-number generator. mod is the modulo function which gives the remainder that results when one number is divided by another.


This generator produces numbers between 0 and 1023 inclusive. A plot of the first 8192 numbers that it produces is shown below. The pattern appears to be quite random.


To evaluate a random-number generator one can count the number of times some number recurs in a sample and plot the freqency of no occurances of a number, just one occurance, two, three, etc versus the number of occurances. The generator above yields a Poisson distribution.


To see how internally random the sequence of numbers is one can compare it with a shifted version of itself using the correlation function. This is known as autocorrelation. Correlation varies between -1 and 1. The correlations between the shifted sets of numbers is quite small.



The observed values for the correlations vary between -0.05 and 0.05 and they appear to be distributed according to a normal distribution.


The sequence of numbers does indeed appear to be random. But if one looks closely at the plot of the correlations one can see that the left and right sides are mirror images of each other. So random-number generators are only useful up to a given point and can repeat after some period.


Close-ups of the center of the correlation plot confirms that demons are likely to be present in random-number generators. One nice thing about them though is that the results are reproducible.

No comments:

Post a Comment