Skip to content

Instantly share code, notes, and snippets.

@martimatix
Created January 31, 2015 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martimatix/d692d609aed61d9bccd0 to your computer and use it in GitHub Desktop.
Save martimatix/d692d609aed61d9bccd0 to your computer and use it in GitHub Desktop.
=begin
On Friday afternoon, our instructor put us into groups of three for a quiz.
A girl in our class ended up in a group of all girls and asked:
"What are the odds of there being in an all girl group?"
While the girl who asked the question did not expect a serious answer, the
question piqued my curiosity. The easiest way to get the answer is by
simulation and hence this script was created.
=end
class GroupOfGirls
# Our class had 6 girls and 6 guys
STUDENTS = [:f,:m] * 6
def initialize
@number_of_trials = 1e7
@favourable_outcomes = 0
end
def trial
students = STUDENTS.shuffle
until students.empty?
if (students.pop 3).all_girls?
@favourable_outcomes += 1
break
end
end
end
def simulate
@number_of_trials.to_i.times do
trial
end
end
def results
result = @favourable_outcomes/@number_of_trials * 100
puts "The probability of there being at least one all girl group is"
puts "approximately #{ result.round(2) }%"
end
end
class Array
def all_girls?
self == [:f] * 3
end
end
g = GroupOfGirls.new
g.simulate
g.results
# => The probability of there being at least one all girl group is
# approximately 35.71%
@Radcliffe
Copy link

The exact probability of at least one all-girl group is 5/14. The probability that the girl will be assigned to an all-girl group is 2/11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment