Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Test if a Deck of Cards Has Been Shuffled Enough in Java

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?

like image 743
frodosamoa Avatar asked Nov 30 '22 08:11

frodosamoa


2 Answers

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".

like image 119
Stephen C Avatar answered Dec 09 '22 19:12

Stephen C


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.

like image 29
Mark Byers Avatar answered Dec 09 '22 21:12

Mark Byers