Problem 19 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 19: Which is most likely: a) at least 1 six when rolling 6 dice, b) at least 2 sixes in 12 dice rolled, or c) at least 3 sixes of 18 dice?
#!/usr/bin/env ruby

TRIALS=10000

n_ones = 0
n_twos = 0
n_threes = 0

def roll
  return 1+rand(6)
end

TRIALS.times do
  six = (1..6).to_a.map { roll() }
  twelve = (1..12).to_a.map { roll() }
  eighteen = (1..18).to_a.map { roll() }

  n_ones += 1 if six.select { |r| r == 6 }.length >= 1
  n_twos += 1 if twelve.select { |r| r == 6 }.length >= 2
  n_threes += 1 if eighteen.select { |r| r == 6 }.length >= 3
  
end

puts "After #{TRIALS}, got #{n_ones} / #{n_twos} / #{n_threes}"

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.