I need to test a random number generator which produces numbers randomly. How to make sure the numbers generated are random.
It's a very difficult thing.
You may try ENT from Fourmilab and compare it with the results against their RNG, HotBits. You may also like to review Random.org.
This also looks interesting: Diehard tests (I've not worked with it though).
You cannot ensure the numbers are random simply because random numbers are, well, random.
The chances of getting a string of one million consecutive 9's is the same as getting any other specific one-million-long sequence. The one thing you can check for is correct distribution over a large sample set. Run a sizeable test and work out the relative occurrences of each possible outcome.
Over a large enough sample, they should be roughly the same.
One other possibility is to test for non-repeatability. Ideally, random numbers should not depend on the numbers that came before. Very simple (linear congruential) PRNGs will most likely give you the same sequence of numbers eventually but over a large enough set that you probably won't care (unless you're serious about randomness).
Use chi-square testing. What language are you using? I can offer a C++ example. Basically
- Place random numbers in buckets (many times).
- The number of buckets minus one is the degrees of freedom.
- Compare the bucket tallies against "expected" tallies, yielding a chi-square result.
- Use a chi-square calculator to see the probability of getting those results.
Often if you have your generator draw dots at random locations in a bitmap, any nonrandomness will easily be discernable to the eye as clumping, banding or lines.
Create a log file which will contains the random number for atleast 500 instances and audit it for randomness. Also have a look at below link,
How to make sure the numbers generated are random.
You can't make sure, there is no way to distinguish with certainty any function from a random number generator using a finite number of tests. But you can do Statistical Analysis:
So, if it is impossible to definitively prove randomness, what can we do instead? The pragmatic approach is to take many sequences of random numbers from a given generator and subject them to a battery of statistical tests. As the sequences pass more of the tests, the confidence in the randomness of the numbers increases and so does the confidence in the generator. However, because we expect some sequences to appear nonrandom (like the ten rolls of six on our die), we should expect some of the sequences to fail at least some of the tests. However, if many sequences fail the tests, we should be suspicious. This is also the way you would intuitively test a die to see if it is loaded: Roll it many times, and if you see too many sequences of the same value coming up, you should be suspicious.
See the section on Charmaine Kenny's study for more details on the tests you could run.
You can't generate true randomness by any algorithm, thus try to visualize your outputs and check for patterns with your own eyes. None random generator (by algorithm) would create some patterns that you can judge them yourself. Here is one of demonstration of that idea: http://www.alife.co.uk/nonrandom/
It depends how severe your requirement for randomness is. If it is not too severe, what I do is generate a large number of random numbers, find their frequencies and then use the frequencies to plot a graph using a spreadshhet like that in Open Office. If the distribution looks OK, then I'm good to go.