I’m working in Ruby, but I think this question is best asked agnostic of language. It may be assumed that we have access to basic list/array functions, as well as a “random” number generator. Here’s what I’d like to be able to do:
Given a collection of n teams, with n even,
- Randomly pair each team with an opponent, such that every team is part of exactly one pair. Call this
ROUND 1. - Randomly generate
n-2subsequent rounds (ROUND 2throughROUND n-1) such that:- Each round has the same property as the first (every team is a
member of one pair), and - After all the rounds, every team has faced every other team exactly once.
- Each round has the same property as the first (every team is a
I imagine that algorithms for doing exactly this must be well known, but as a self-taught coder I’m having trouble figuring out how to find them.
I belive You are describing a round robin tournament. The wikipedia page gives an algorithm.
If You need a way to randomize the schedule, randomize team order, round order, etc.