Python Random Sport Event Generator

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.

+4
source share
1 answer

I assume that each team needs to play with a different team. Is not? Then this question does not boil down to a simple permutation and then shuffling the result?

 import itertools import random set_size = 2 schedule = set() teams = range(5) for comb in itertools.product(teams, repeat=set_size): comb = sorted(list(comb)) if len(set(comb)) == set_size: schedule.add(tuple(comb)) schedule = list(schedule) random.shuffle(schedule) print schedule 
+4
source

Source: https://habr.com/ru/post/1403794/


All Articles