6

How to write a convincing test which user can be convinced of that it is working as described by showing how I test this function.

Is there any test scenarios to test randomness? And what is an enough test for this scenario?

For example, given a array, return one element from it randomly.

//[] will cause indexOutOfBound 
function luckyDraw(array){
    var index = rand(array.length) // get 0 ~ arraySize-1
    return array[index];
}

The basic scenario is the "distribution" evenly. I write a test, give it an array [0, 1, 2]. I run it 300 times, and check each time is 0 or 1 or 2. But it still have chance that 2 does not appear in this 300 times. So a better way is to run it with an "enough times" and they appear close to the expected times.

But how large is enough?

Except distribution, is there any testing scenarios for randomness? And how to do a good test for it?

3

As you want to test functions that creates random outputs you come across a "fundamental" problem: you can't define an expected output.

I write a test, give it an array [0, 1, 2]. I run it 300 times, and check each time is 0 or 1 or 2. But it still have chance that 2 is not appear in this 300 times. So a better way is run it with an "enough times" and they appear close to the expected times.

The problem with the scenario you described is that you don't test the randomness but the repeatability of the input data you gave. You can't make sure that after running your test 1000 times it won't return a '3' in one of the following runs.

To start with testing such kind of functions I would recommend you reading about statistical tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.