Problem 39 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 39: a set of glass rods, with one end blue-dotted and the other red-dotted, are dropped. Many break into 3 parts. What is the average length of the blue-dotted pieces of these broken-in-three rods?
#!/usr/bin/env ruby
TRIALS=100000
# Much like 42 and 43, this is pretty trivial
cum_blue = 0.0
TRIALS.times {
# Choose two breaking points
b1 = rand()
b2 = rand()
# put them on a stick reasonably...
if (b1 > b2)
x = b1
b1 = b2
b2 = x
end
# and let's say the blue is always on the left
# that is, the length of the blue segment is b1
cum_blue += b1
}
puts "After #{TRIALS} trials, average length:"
puts cum_blue/TRIALS
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.