Consider a method to randomly shuffle elements in an array. How would you write a simple yet robust unit test to make sure that this is working?
I've come up with two ideas, both of which have noticeable flaws:
- Shuffle the array, then make sure its order differs from before. This sounds good, but fails if the shuffle happens to shuffle in the same order. (Improbable, but possible.)
- Shuffle the array with a constant seed, and check it against the predetermined output. This relies on the random function always returning the same values given the same seed. However, this is sometimes an invalid assumption.
Consider a second function which simulates dice rolls and returns a random number. How would you test this function? How would you test that the function...
- never returns a number outside the given bounds?
- returns numbers in a valid distribution? (Uniform for one die, normal for large numbers of dice.)
I'm looking for answers offering insight into testing not only these examples but random elements of code in general. Are unit tests even the right solution here? If not, what sort of tests are?
Just to ease everyone's mind I'm not writing my own random number generator.