## Problem 46 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 46: As in 45, but this time: what is the probability of r matches?
```#!/usr/bin/env ruby TRIALS=1000000 deck = (0..51).to_a # We shuffle the deck, then shuffle another. # We then walk the two arrays side-by-side, checking # for matches # a count of how many times we got a given number of matches matches = deck.map { 0 } TRIALS.times do upper = deck.shuffle lower = deck.shuffle match = 0 upper.each_with_index do |u_i, i| l_i = lower[i] match += 1 if l_i == u_i end matches[match] += 1 end puts "Out of #{TRIALS}, we got the following distribution:" matches.each_with_index { |m_i, i| puts "\t#{i}\t#{m_i}" } ```

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.