Problem 18 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 18: What's the probability of getting 50 tails in 100 toin cosses?
#!/usr/bin/env ruby

# This is pretty straightforward:
#   we just count the # of 50s we get.


n_fifties = 0
TRIALS.times do
  heads = 0
  100.times { heads += rand(2) }

  n_fifties += 1 if 50 == heads

puts "Out of #{TRIALS} trials"
puts "  we got #{n_fifties} even splits, so "
puts "  P=#{n_fifties/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.