Skip to content

Instantly share code, notes, and snippets.

@jonelf
Created March 9, 2012 23:11
Show Gist options
  • Save jonelf/2009196 to your computer and use it in GitHub Desktop.
Save jonelf/2009196 to your computer and use it in GitHub Desktop.
The other one is also a girl.
# Ruby
# Suppose Mr. and Mrs. Smith tell you they have two children, one of
# whom is a girl. What is the probability that the other is a girl?
# http://www.whydomath.org/Reading_Room_Material/ian_stewart/9609.html
1.upto(10) do |n|
siblings = []
# Create a bunch of randomized pairs of children
1.upto(10000) do
child1 = rand(2) == 0 ? :boy : :girl
child2 = rand(2) == 0 ? :boy : :girl
siblings << { :first_born => child1, :second_born => child2 }
end
# Remove all [:boy, :boy] because we know one of them is a girl
siblings = siblings.select {|ch| !(ch[:first_born] == :boy && ch[:second_born] == :boy) }
# Count the number of [:girl, :girl] because that's the combination
# that is the answer to the question.
girl_girl_count = siblings.select{|ch| ch[:first_born] == :girl && ch[:second_born] == :girl}.length
puts "Study #{n}"
puts "The other is a girl in #{girl_girl_count} cases."
puts "Total number of cases #{siblings.length}."
puts "Chance #{girl_girl_count.to_f / siblings.length}"
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment