I have a program which generates random numbers. How can I test if the random number generator is generating actual random numbers?

  • 2
    A fact you will need to know: what distribution are these numbers sampled over?
    – user246
    Aug 20 '12 at 13:53
  • You have this tagged under manual testing. This is something that should be tested using software. Aug 20 '12 at 13:58
  • 2
    Not a fully answer, but one of the best ways to determine if things are random are to try to compress them. Compression works by finding patterns, and if they're random, there won't be many patterns for it to find in order to compress.
    – corsiKa
    Aug 20 '12 at 16:38
  • I corrected the tag.
    – dzieciou
    Oct 6 '13 at 10:40
  • Use one or more of the RNG test suites: Dieharder, TestU01, or the NIST tests. You can find these on the web.
    – Mars
    May 8 '19 at 4:14

If numbers are being computer generated, they won't be truly random, they will be pseudo-random numbers.

I would head over to math.stackexchange.com to get a good understanding of the problem you are trying to test.

Some good, related questions are:

How to generate REAL random numbers with some random and pseudo random

Are these numbers random?

You should also look in to ways you can automate the testing. To truly test this you will need to test a very large sample size, and then visualise the distribution of results.

  • I would take a simpler approach, how would you test manual random number (picked in a hat)? You want to validate that all numbers are eventually generated. You want to validate the distribution (number of occurrence of a number). For the software, I think the interesting point is also to focus on the performance. Do you generate the first number as fast as you generate the last number for example?
    – Ahmet
    Sep 7 '12 at 21:46

Try this document - Chapter 10 of Beautiful Testing

For a simple example, have a look at this example blog post from Alan Page

  • 1
    The Beautiful Testing chapter is actually really good, since it talks about testing without getting into any heavy-duty math or statistics. I really recommend it. Aug 20 '12 at 17:02
  • 1
    Why shouldn't you get to heavy-duty math or statistics, if you're going to test random number generator?
    – Edu
    Aug 20 '12 at 17:28

See for example http://en.wikipedia.org/wiki/Statistical_randomness for a list of possible tests to check randomness of the generator. http://en.wikipedia.org/wiki/Random_number_generation gives pointers to other things you might be interested in your generator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.