Answer the question
In order to leave comments, you need to log in
How to cover the random function with unit tests?
Suppose there is a function that takes a parameter n and returns integer values in the range from 0 to n inclusive (the example, of course, is degenerate, because the function is trivial). How to test this?
I remember the theory of probability very poorly, but it seems that even m calls of such a function (with m obviously greater than n) cannot guarantee anything.
Answer the question
In order to leave comments, you need to log in
en.wikipedia.org/wiki/Diehard_tests
It won't work to prove something mathematically on real numbers.
If we get a normal distribution of a random variable, it’s probably good. But I didn’t remember the theorver for a long time either ... so I’m not completely sure.
If this is a pseudo-random function, then some seed must be used when calling it.
Reusing the resulting pseudo-random value as a seed, you get the next value.
If this function is written correctly, then it should be guaranteed that the chain of values will close only after it has been traversed over the entire range of values (in fact, it is also a dubious pleasure to check this if the range of values is large).
In your example, the function takes only the upper bound of the range, but not the seed, which, in fact, makes this function completely random (it depends on some other random function, perhaps even pseudo-random, but we cannot control it) . Then it makes no sense to test it (for any set criterion for values, there is a non-zero probability that these criteria will not be met). Perhaps it should be changed to control the seed?
Depends on the task facing unit testing.
If the task is to evaluate the “randomness” of the algorithm and if it is not required to automate this process, then the following can help:
A point is displayed on the plane, the coordinates of which are taken from two calls to the random number generation function. That is:
X=rand
Y=rand
DrawPoint( X, Y )
If as a result there are any patterns in the distribution of points, then the "randomness" of the algorithm is "lame"
PS:
I remember in the last century, such a test of the RND function from BASIC led to drawing almost a straight line, with little overshoot.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question