How do you check that something is random? Or "random enough"?

I need to return a random entry from my database.

I wrote a function, and since I use the random module in Python, it is possible if I hadn't used it stupidly.

Now, how can I write unit test to check if this function works? In the end, if this is a good random value, you will never know.

I am not paranoid, my function is not so complicated, and the standard Python library is 1000 x enough time for my purpose. I do not do cryptography or anything critical. I'm just curious to know if there is a way.

+4
source share
5 answers

There are several statistical tests listed on RANDOM.ORG to check for randomness . See the last two sections of a related 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 the many statistical methods listed in the article above. If you really want to learn about RNG, this chapter is a really good starting point. I myself wrote about this subject, but John does a much better job explaining this.

+19
source

You cannot say (see cartoon).

However, you can measure the entropy of your generated sample and test it against the entropy that you expect. As mentioned earlier, random.org does pretty smart tests.

alt text

+7
source

You could call the unit test a function call several times and make sure that the number of collisions is low enough. For instance. if your random result is in the range of 1-1000000, call the function 100 times and write down the results; then check for duplicates. If there is (or more than one collision, depending on how much you are afraid of a false test), the test fails. Obviously, this is not ideal, but catch it if you are an arbitrary number from Dilbert: http://www.random.org/analysis/

+2
source

You have two confusing problems. The first problem is checking that your random selection is working. Visiting your PRNG allows you to write a test that is deterministic and about which you can argue. This should give you confidence in your code, given that the main functions correspond to their duties (i.e. Randomly returns a fairly good stream of random values ​​to you).

The second issue you seem to be worried about is random python functions. You want to separate the problems of your code from a concert about a random function. There are a number of random tests that you can read but at the end of the day, if you do not do cryptography, I would trust the python developers to get enough rights.

+2
source

In addition to the previous answers, you can also cast a random function (e.g. mock or mox ) and return a predefined sequence of values ​​for which you know the results. Yes, this would not be a true test for all cases, but you can check out some corner cases. In some cases, such tests may be reasonable.

0
source

Source: https://habr.com/ru/post/1333057/


All Articles