Skip to content

Instantly share code, notes, and snippets.

@robdimarco
Created November 3, 2012 13:12
Show Gist options
  • Select an option

  • Save robdimarco/4007355 to your computer and use it in GitHub Desktop.

Select an option

Save robdimarco/4007355 to your computer and use it in GitHub Desktop.
How Often Will 5 kids randomly drafted to 7 teams wind up on different teams
#!/usr/bin/env ruby
TEAM_COUNT = 7
PLAYERS = 5
ITERATIONS = 1_000_000
PLAYERS_TO_DRAFT = 7
# Players available to draft are numbered 1..Total #
# we are only looking at the first PLAYERS number
AVAILABLE_PLAYERS_ARRAY = (1..TEAM_COUNT * PLAYERS_TO_DRAFT).to_a
two_players_on_one_team = 0
ITERATIONS.times do
# Randomize players to simulate a draft
drafted_players = AVAILABLE_PLAYERS_ARRAY.shuffle
teams = (0..TEAM_COUNT).map { |team_num|
# team is comprised of player numbers in position [team_num * PLAYERS_TO_DRAFT..team_num*PLAYERS_TO_DRAFT - 1]
team = drafted_players.slice(team_num * PLAYERS_TO_DRAFT, PLAYERS_TO_DRAFT)
}
two_players_on_one_team += 1 unless teams.detect{|t| t.select{|p| p <= PLAYERS}.size > 1}.nil?
end
puts "All players on different teams %f%% of the time" % [(ITERATIONS - two_players_on_one_team).to_f * 100 / ITERATIONS]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment