K
K
Konstantin Kitmanov2012-02-09 11:57:14
Algorithms
Konstantin Kitmanov, 2012-02-09 11:57:14

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

6 answer(s)
B
bagyr, 2012-02-09
@k12th

en.wikipedia.org/wiki/Diehard_tests
It won't work to prove something mathematically on real numbers.

S
Sergey, 2012-02-09
Protko @Fesor

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.

X
xappymah, 2012-02-09
@xappymah

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?

M
Maxim Kuzovlev, 2012-02-09
@KY3EH

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.

K
klen, 2012-02-09
@klen

en.wikipedia.org/wiki/Mock-%D0%BE%D0%B1%D1%8A%D0%B5%D0%BA%D1%82

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question