I have to test and see if this method has shuffled a deck of cards. Here if the code for the actual shuffling part.
public void randomShuffle () {
for (int i = 0; i < DECK_SIZE; i++) {
int place = (int)((Math.random()*(51-i))+i);
Card temp = this.cardAt(i);
this.cardList[i] = this.cardAt(place);
this.cardList[place] = temp;
}
}
The problem with testing whether its been shuffled is that I could only switch two cards and it would be considered shuffled. Here is what i have so far for the test of the random shuffle.
static void randomShuffleTest () {
Deck deck1 = Deck.newDeckOf52();
Deck deck2 = Deck.newDeckOf52();
deck2.randomShuffle();
assert false == deck1.equals(deck2);
}
So, my question is, how do I test if something has been shuffled enough?
You cannot do this with a single sample. But what you might be able to do is to perform an statistical analysis of a number of shuffled decks to look for signs of non-randomness.
I think you might do better asking this on math.stackexchange.com ... where the brainy guys hang out. If they can explain "the math" in simple terms (for dumb IT folks like you and me), you should be able to code the tests in Java.
When I say "you cannot do this" ... obviously you can test to see if the deck has not been shuffled at all, or if shuffling has corrupted the deck. But neither of these are a credible test for your criteria ... "shuffled enough".
You can't. It's impossible to determine if a deck is shuffled because in theory the shuffling could produce a deck that is exactly in order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With