Created
January 31, 2015 00:11
-
-
Save martimatix/d692d609aed61d9bccd0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.