I love puzzles and small little programming projects. When you can combine those two at get a project that you can use for a good learning/teaching experience it is just a bit of gold. When the concepts you can get into the project go beyond programming so much the better. And I think I have found one of these nuggets. About a week ago someone sent the following puzzle to a mailing list I am on.
You walk into a casino and discover that they are offering an exciting new game called 52-Card Match, which is played with two ordinary 52-card decks of playing cards.
The player is handed one deck, and the dealer takes the other. The dealer thoroughly shuffles his deck and sets it face down in front of you. You can shuffle your deck as thoroughly as you please and then also set it face down in front of you.
The cards are then turned over one at a time from each deck, i.e. one from each deck is turned over simultaneously each time. If any pair matches exactly (rank and suit), the game is over and the casino wins. If you make it through the whole deck without a match then the player wins, and is paid out at 2:1 odds.
Should you play this game? What would fair odds be?
Now there is surely a good mathematical way to figure this out. No doubt it would make a great project for a class in statistics. But I’m a programming guy. I did take statistics in college – two or three courses of it – but it’s been 35 years and I haven’t kept up with it. What I do know how to do is simulate the game. So that is what I did. Basically what I did (rough outline) was this:
- Create two 52 element integer arrays and fill them with the integer values 1 through 52.
- “Shuffle” the arrays. I used random numbers to pick two elements to swap and did that n times. I tried a couple of values for n.
- Create a loop to check each element of the arrays to see if the values in element x in both arrays was the same and report that if it happened.
- Then I put all of that in a loop so I could run the simulation multiple times. I wound up running it a couple of million times.
The results surprised me. Surprised me a lot. In fact this morning I had to take out two decks of real cards and try it in real life. Simulation and real life had the same sort of results. Makes one feel good when that happens.
I think this might be fun for students as well. And of course you have arrays (parallel arrays probably but maybe a two dimensional array if you want), loops, and the ever popular random numbers. The math/statistics involved are potentially interesting as well because you can compare the expected results with what you see in the simulations.
For added fun (and complexity) you could keep track of how many times there was a match in each run of the deck comparisons. Perhaps there is a variation (you have to match three or more times to lose for example) you could find that actually makes more sense to play? Get the kids in AP Statistics to do the math for you and compare their results with the simulation.