Writing a JUnit Test for a Random Number Generator

I have a method that returns a random number between 0 and 10.

public int roll(){
    int pinsKnockedDown = (int) (Math.random() * 10);
    return pinsKnockedDown;
}

      

How should I write a JUnit test for this? So far, I have put the call in a loop to execute 1000 times and fail the test, if - number is less than 0 - number is greater than 10

How can I check that all numbers are not the same, i.e.

Dilbert

+3


source to share


3 answers


My answer was already messed up, I needed to return a number from 0 to 10, but my original post only returned a range from 0 to 9! This is how I found it ...

Repeat 10,000 times and make sure the range is correct, it should be 0-10 (although I set 10 as a variable so the code can be reused).

I also save the highest and lowest values ​​that were found during the cycle, and they should be at the extreme ends of the scale.



If the highest and lowest values ​​match, then an indicator appears that someone has tampered with the random number.

The only problem I see is that it is possible to get a false negative from this test, but this is unlikely.

@Test
public void checkPinsKnockedDownIsWithinRange() {
    int pins;
    int lowestPin = 10000;
    int highestPin = -10000;

    for (int i = 0; i < 100000; i++) {
        pins = tester.roll();
        if (pins > tester.NUMBER_OF_PINS) {
            fail("More than 10 pins were knocked down");
        }
        if (pins < 0) {
            fail("Incorrect value of pins");
        }

        if (highestPin < pins) {
            highestPin = pins;
        }

        if (lowestPin > pins) {
            lowestPin = pins;
        }
    }

    if (lowestPin == highestPin) {
        fail("The highest pin count is the same as the lowest pin count. Check the method is returning a random number, and re-run the test.");
    }

    if (lowestPin != 0) {
        fail("The lowest pin is " + lowestPin + " and it should be zero.");
    }

    if (highestPin != tester.NUMBER_OF_PINS) {
        fail("The highest pin is " + highestPin + " and it should be " + tester.NUMBER_OF_PINS + ".");
    }

}

      

+2


source


Randomness tests are potentially challenging. for example in the above do you just want to make sure you get numbers from 1 to 10? Do you want to ensure even distribution, etc.? At some point, I would advise you to trust Math.random()

and just make sure you don't mess up the limits / range, which is essentially what you are doing.



+2


source


You want to test your code, not the quality of the supplied Java Math.random (). Let's assume the Java method is good. All tests are necessary, but not sufficient conditions for correctness. Therefore, select some tests that will identify possible programming errors when using the Java provided method.

You can test the following: After all, after a sequence of calls, the function returns each digit at least once without returning the numbers in the required range.

+1


source







All Articles