I have a method which returns a random number between 0 and 10.
public int roll(){
int pinsKnockedDown = (int) (Math.random() * 10);
return pinsKnockedDown;
}
How would I write a JUnit test for this? So far I have put the call in a loop so it runs 1000 times and fails the test if
– the number is less than 0
– the number is more than 10
How can I test that all the numbers aren’t just the same, i.e.

My answer was already flawed, I needed to return a number from 0-10 but my original post only returned a range from 0-9! Here is how I found that out…
Loop 100k times and make sure that the range is correct, it should be 0-10 (although I’ve set 10 as a variable so that the code can be re-used).
Also I store the highest and lowest values that were found during the loop and they should be at the extreme ends of the scale.
If the highest and lowest values are the same then that’s an indicator that someone has faked a random number return.
The only problem that I see is that it is possible to have a false negative from this test, but it is unlikely.