## Problem 45 of Monte Carlo solutions to Fifty Challenging Problems...

(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)

Problem 45: Let us shuffle two decks of cards and lay them out in two lines, one over the other. On average, how many cards will "line up" with themselves (ie: the 3 of clubs over the 3 of clubs)?
```#!/usr/bin/env ruby TRIALS=10000 deck = (0..51).to_a # We shuffle the deck, then shuffle another. We then walk the # two arrays together, looking for matches. match = 0 TRIALS.times do upper = deck.shuffle lower = deck.shuffle upper.each_with_index do |u_i, i| l_i = lower[i] match += 1 if l_i == u_i end end puts "Out of #{TRIALS}, #{match} cards matched." puts "That gives us an expected number of matches as: " puts " #{match.to_f/TRIALS.to_f}" ```

I've been coding my way through Fifty Challenging Problems in Statistics with Solutions. This post is a part of the Fifty Challenging Problems series.

This was brought to you by Josh Myer. He has other fun things at his homepage.