Created
December 1, 2012 22:43
-
-
Save ctm/4185692 to your computer and use it in GitHub Desktop.
A Ruby program to compute the odds of winning an entry into the Western States Endurance Run
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
#! /usr/bin/env ruby | |
require 'set' | |
COUNTS = [ 1490, 482, 207, 122].freeze # 1490 have 1 ticket, 482 have 2, etc. | |
N_DRAWS = 275 | |
USER_MAP = {} | |
HAT, ignored = COUNTS.inject([[], 0, 0]) do |(hat, tix_index, user), n_users| | |
n_users.times do | |
(tix_index+1).times { hat << user } | |
USER_MAP[user] = tix_index | |
user += 1 | |
end | |
[hat, tix_index + 1, user] | |
end | |
USER_MAP.freeze | |
HAT.freeze | |
N_SIMULATIONS = 10_000_000 | |
counts = Array.new(COUNTS.size, 0) | |
N_SIMULATIONS.times do | |
hat = HAT.sort_by { rand } | |
seen = Set.new | |
N_DRAWS.times do | |
while seen.include?(draw = hat.pop); end | |
seen << draw | |
counts[USER_MAP[draw]] += 1 | |
end | |
end | |
percents = counts.zip(COUNTS).map do |(hits, contestants)| | |
Float(hits) / contestants / N_SIMULATIONS * 100 | |
end | |
require 'pp' | |
PP.pp(percents) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment