views:

238

answers:

10

I need to test a random number generator which produces numbers randomly. How to make sure the numbers generated are random.

+3  A: 

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).

Noon Silk
The Diehard test suite is not maintained anymore and has been outdated by the NIST test suite for random numbers. See http://www.random.org/analysis/#2005.
Pascal Thivent
+1 to 'it's a very difficult thing'. Simple one- or two-statistic tests, including some suggested by other respondents, are just not adequate. In fact, it's such a difficult thing that, unless you are up with the latest research findings (and your question here on SO suggests that you are not) you would probably (ha ha) be better using an existing PRNG than trying to implement your own. Better, but less fun.
High Performance Mark
Cheers Pascal :)
Noon Silk
+2  A: 

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).

paxdiablo
A: 

Show it to a room full of developers.

Jay
...and I've never seen one pass scrutiny yet. ...especially when the generator determines who wins prizes.
Jay
+2  A: 

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.
Jon Reid
You also need to make sure that the buckets are being filled randomly, i.e. not sequentially.
ck
Yup. And that's where things get harder.
Jon Reid
+2  A: 

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.

Not sure - I'd be interested to see whether taking a complex but non-random source of data and plotting it on an xy bitmap might still "look" random.
Jeffrey Kemp
Jeffrey is exactly right -- the "looks random plotted" test is a necessary but not sufficient test for randomness of a signal.
kquinn
A: 

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,

http://burtleburtle.net/bob/rand/testsfor.html

Thi
+2  A: 

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.

Pascal Thivent
A: 

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/

instcode
A: 
Alok
A: 

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.

anon