Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you test that something is random? Or "random enough'?

I have to return a random entry from my database.

I wrote a function, and since I'm using the random module in Python, it's probably unless I used it in a stupid way.

Now, how can I write a unit test that check that this function works? After all, if it's a good random value, you can never know.

I'm not paranoid, my function is not that complex and the Python standard library is 1000 x time good enough for my purpose. I'm not doing cryptography or something critical. I'm just curious to know if there is a way.

like image 248
e-satis Avatar asked Dec 22 '10 15:12

e-satis


2 Answers

There are several statistical tests listed on RANDOM.ORG for testing randomness. See the last two sections of the linked article.

Also, if you can get a copy of Beautiful Testing there's a whole chapter by John D. Cook called Testing a Random Number Generator. He explains a lot of the statistical methods listed in the article above. If you really want to learn about RNGs, that chapter is a really good starting point. I've written about the subject myself, but John does a much better job of explaining it.

like image 193
Bill the Lizard Avatar answered Sep 22 '22 12:09

Bill the Lizard


You cannot really tell (see cartoon).

However, you can measure the entropy of your generated sample, and test it against the entropy you would expect. As it has been mentioned before, random.org makes some pretty clever tests.

alt text

like image 39
Escualo Avatar answered Sep 20 '22 12:09

Escualo