## Brute force probability

Earlier this week, a twitter bud of mine posted the following (WordPress is supposed to embed the tweet on the next line but it’s not working right now – let me know if you know why and I’ll fix it):

Q: deck of cards with 6 suits (78 cards). Draw 2 cards without replacement. Probability that the sum of the cards is exactly 8?

Several people responded and came up with an answer around 4%. I put a tiny bit of thought into it and realized I’d have to do the usual bit of reminding myself how the various factorials etc go into such a calculation.

What I want to talk about in this post is the brute force way of finding the answer. I started thinking about how I could get Mathematica to simulate the act of drawing two cards and summing them over and over. It didn’t take long before I went from thinking about it to coding it. When I did, I got the same 4% answer but I realized I had the makings of much more.

First the code:
 suit = Join[Range[10], {10, 10, 10}] deck = Flatten[Table[suit, {6}]] draw := RandomSample[deck, 2]

The first line defines a suit as 1-10 with 3 extra 10s for the face cards. The second line sets up a deck with 6 suits. The third line lets me randomly draw two cards. Here’s the code to do the draw 100,000 times and find out how many times it gets a sum of 8:
 Total[Table[If[Total[draw]==8, 1.0, 0.0], {100000}]/100000 
It uses an if/then construction to find out what we want and gives first the number of times it happens and then divides by the total number of times (the fundamental definition of the probability of an event).

That’s fine and all, but I think you learn more from looking at a histogram of all possible sums:
 Histogram[Table[Total[draw], {n = 100000}], 20, "Probability"] 
That gives this nice image (in Mathematica you can hover over the bars to find the probabilities):

Histogram of sum of 2 cards from 6 suit deck

The first bar is for 2 (drawing two aces) and the last bar is for 20 (lots of possibilities because of the face cards).

The reason I think this is a better answer to Sam’s original question is that it immediately lets you do some comparing and contrasting with other sums. What do you think?

Taking it further

What I really like about this particular approach is that you can explore other questions that might not be as easy to figure out using the typical probability tricks. For example, what are the chances that, when you draw 3 cards from that deck, the second card is larger than the square of the third card but smaller than the square of the first one?
 f2[{d1_, d2_, d3_}] := If[d3^2 < d2 < d1^2, 1., 0.] Total[Table[f2[d = RandomSample[deck, 3]], {n = 100000}]]/n 
All I really had to do was change the function I was using from just adding and checking if it was eight to the new question. It turns out the answer is roughly 12% of the time, by the way (weird, huh?).

Professor of physics at Hamline University in St. Paul, MN

### 4 Responses to Brute force probability

1. samjshah says:

Wow, this is awesome! I sent to it to the teacher who originally posed the problem — who I am sure will get a kick out of the fact that it went from her to our department (to confirm her answer) to twitter and then to blogs! It wasn’t a terribly difficult problem, but your mind gets twisted around if you overthink it. This generation of data is awesome — and could easily be done on a TI-83/84 too! It’s also powerful and brings down the abstraction. Thanks for sharing.

Sam

• Andy "SuperFly" Rundquist says:

That’s awesome how far this has gone. Thanks for kicking it all off!

2. Brian says:

Nice work Andy. For me, the best part about this representation is the questions it makes me ask: Why an ascending and then descending stair case? Why do they stairs look uneven? Why the big bump in the middle? Why big bump at the end? Why are the two big bumps the same height? I’m intrigued.

• Andy "SuperFly" Rundquist says:

I had very similar reactions when I first saw the histogram. In fact, I went back to double check the code. I think, though, that double checking my code was easier that double checking any math/probability calcs if I had done it that way.

It is interesting how the extra 10’s really make things different than the expected pattern (I was expecting a symmetric triangle). What might be fun is to have Mathematica store all the draws (memory intensive, I suppose) and to organize them so you can see all the combinations for a particular sum, just to see why a particular sum has a lot.