Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test a pseudo random number generator?

I have a pseudo random number generator (PRNG) class that I want to unit test. There are two approaches:

  1. write a test case that takes a large amount of samples and test whether they are properly distributed. This approach may lead to a fairly long execution time for the test case;
  2. calculate a small series of samples 'by hand' and verify if the PRNG algorithm reproduces it. This approach may lead to a not random sequence being generated without being noticed;

I would say that the first approach is not really unit testing because it does not perform a white box test of the generator, but on the other hand it properly tests the responsibility of the class. The second approach is more like a real unit test, focusing on the algorithm, but it does not provide as much evidence as to whether the class fulfills its responsibility.

Which approach do you prefer, and why?


like image 459
andreas buykx Avatar asked Oct 09 '08 10:10

andreas buykx


People also ask

How do you test a pseudo random number generator?

To test whether a pseudo-random number generator is close to a true one, a sequence length is chosen, and m pseudo-random sequences of that length are retreived from the PRNG, then analysed according to the previous methodology.

How do you test a random distribution?

Hypothesis: To test the run test of randomness, first set up the null and alternative hypothesis. In run test of randomness, null hypothesis assumes that the distributions of the two continuous populations are the same. The alternative hypothesis will be the opposite of the null hypothesis.

How does a pseudo random number generator work?

Instead they rely on algorithms to mimic the selection of a value to approximate true randomness. Pseudo random number generators work with the user setting the distribution, or scope from which the random number is selected (e.g. lowest to highest), and the number is instantly presented.


2 Answers

Get another implementation of the same PRNG algorithm, generate a smallish number of longish test cases based on known seeds, and verify that your implementation of the algorithm matches everyone else's implementations. The more data you test, the more chance it does. If you want to be serious, look into how FIPS validation is done for the algorithm.

There's no need to test whether the output is random, since far more research has been done on the algorithm by others than you are capable of reproducing.

If you have invented your own PRNG algorithm then you have a rather different problem, because quite aside from testing your code you also need to test your new algorithm. There are various things to do -- I think the most important are statistical testing on the output, and peer review by other cryptographers. Basically, though, if you were to design a PRNG algorithm without having enough knowledge in the field to know how to test it, then it will be rubbish.

like image 179
Steve Jessop Avatar answered Sep 23 '22 06:09

Steve Jessop


For testing a PRNG, I would use ENT which is a suite of statistical tests that will tell you how well your PRNG performs. I suppose this is approach 1.

like image 42
Greg Hewgill Avatar answered Sep 23 '22 06:09

Greg Hewgill