I found the following
Create a natural schedule for a sports league
This generates the same schedule every time you run it. If I add random.shuffle (), it is still too predictable.
Below is my simple edit from this post with a random position, and I get odd results
import random def round_robin(units, sets = None): """ Generates a schedule of "fair" pairings from a list of units """ count = len(units) sets = sets or (count - 1) half = count / 2 for turn in range(sets): left = units[:half] right = units[count - half - 1 + 1:][::-1] pairings = zip(left, right) if turn % 2 == 1: pairings = [(y, x) for (x, y) in pairings] units.insert(1, units.pop()) yield pairings teams = range(5) random.shuffle(teams) schedule = list(round_robin(teams, sets = len(teams) * 2 - 2)) for week in schedule: for game in week: if 0 in game: print game
sometimes it seems that teams will not even play with each other .. see results
(0, 4) (4, 0) (0, 2) (0, 4) (4, 0) (0, 2)
My question is: how can I create a random schedule generator in python or fix what I already have.
To a large extent, I need to take 4 teams and go out with a 3-week game, where they play with each other once. Or like 5 teams, where there are 5 weeks of play, and they all play each other once, but one team is not indicated per week.
source share