Created
December 4, 2022 00:16
-
-
Save jerhinesmith/2e280283cf5cb4c8c5297032c5b2f3cf to your computer and use it in GitHub Desktop.
Sock Probability
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
total_runs = 100_000_000 | |
seven_socks_without_match = 0 | |
total_runs.times do |run| | |
dryer = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 7].shuffle | |
picked = [] | |
iterations = 0 | |
while picked.length == picked.uniq.length | |
iterations += 1 | |
sock = dryer.pop | |
picked << sock | |
end | |
seven_socks_without_match += 1 if picked.length == 8 | |
puts "Iteration #{run} - No match found #{seven_socks_without_match} times" if run % 1_000_000 == 0 | |
end | |
puts "Total Runs: #{total_runs}" | |
puts "Seven Sock Outcomes: #{seven_socks_without_match}" | |
puts "Estimated Probability: (#{seven_socks_without_match} out of #{total_runs}) or #{(seven_socks_without_match.to_f / total_runs) * 100}%" | |
# Seven Sock Outcomes: 2239552 | |
# Estimated Probability: (2239552 out of 100000000) or 2.2395519999999998% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment