Skip to content

Instantly share code, notes, and snippets.

@sebastorama
Created September 21, 2021 00:40
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 sebastorama/22d67a100ff1ce36305328ef81f00551 to your computer and use it in GitHub Desktop.
Save sebastorama/22d67a100ff1ce36305328ef81f00551 to your computer and use it in GitHub Desktop.
MY_BIRTHDAY = 89
SIMULATIONS = 100000
# true if found someone with my birthday
def run_test(num_people)
((1..num_people).filter do
rand(365) == MY_BIRTHDAY
end).length > 0
end
# ratio between true/falses on the func above
def simulate_n_times(num_people)
puts "Simulating for #{num_people}"
true_false_array = (1..SIMULATIONS).map do
run_test(num_people)
end
answer = true_false_array.filter { |item| item }.length / SIMULATIONS.to_f
puts "Answer is ~ #{answer*100}%"
end
simulate_n_times(182)
simulate_n_times(183)
simulate_n_times(253)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment