5 Minute Simulation: Lonely Cards

FiveThirtyEight posts math, logic, and probability puzzles each week. I was tempted by this week’s Riddler Express:

On snowy afternoons, you like to play a solitaire “game” with a standard, randomly shuffled deck of 52 cards. You start dealing cards face up, one at a time, into a pile. As you deal each card, you also speak aloud, in order, the 13 card faces in a standard deck: ace, two, three, etc. (When you get to king, you start over at ace.) You keep doing this until the rank of the card you deal matches the rank you speak aloud, in which case you lose. You win if you reach the end of the deck without any matches.

What is the probability that you win?

My combinatorics skills are rusty, so I’m still hacking away at the closed-form solution. But, where cunning fails, brute force may suffice. While the closed-form approach is proving challenging (for me), this is fairly trivial to approximate through simulation. Recreating the game itself only takes a line or two of code, and then we can “play” hundreds of thousands of simulated games to get an idea of how often we win. Here’s a simple shiny app with the results, and here’s the dead-simple code for simulating the game itself:

cards <- function() {
#make a deck; leave this sequence fixed for the verbal sequence,
#and scramble it to simulate the cards being dealt
deck <- rep(c('ace', as.character(seq(2:10)),
'jack', 'queen', 'king'), times=4)
#You win if none of the ranks line up; you lose if one or more does
return(as.numeric(!(sum(deck == sample(deck)) > 0))) #1 is win, 0 is lose
}
results <- replicate(100000, cards()) 

I’ll update this post with the closed-form solution if I manage to work it out (failing that, I’ll just link to the solution on FiveThirtyEight).

Here’s the source code for the shiny app.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s