Created
December 14, 2014 17:59
-
-
Save jish/30b33e46c11867fa5041 to your computer and use it in GitHub Desktop.
What are the odds or randomly selecting the burned match from a bundle of matches?
This file contains hidden or 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
Match = Struct.new(:burned) | |
PEOPLE = 4 | |
TRIALS = 1_000_000 | |
matches = Array.new(PEOPLE-1).fill(Match.new(false)).push(Match.new(true)) | |
results = Array.new(PEOPLE).fill(0) | |
TRIALS.times do | |
matches.shuffle! | |
matches.each_with_index do |match, index| | |
if match.burned | |
results[index] += 1 | |
end | |
end | |
end | |
results.each_with_index do |r, i| | |
percent = (r.to_f / TRIALS * 100).round(2) | |
puts "Person #{i+1} got the burned match #{r} (#{percent}%) times." | |
end | |
# => Person 1 got the burned match 249419 (24.94%) times. | |
# => Person 2 got the burned match 250346 (25.03%) times. | |
# => Person 3 got the burned match 250348 (25.03%) times. | |
# => Person 4 got the burned match 249887 (24.99%) times. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We assume that shuffling the array each time is a adequate representation of the first person randomly selecting the first item, then the second person randomly selecting the second item.
Then when everyone has selected their match, we record which person got the burned match, and repeat.
cc: @jayme-mckiney @supertopher @darrenboyd