I'm trying to write a flexible round robin program for randomly scheduling teams to play one another. There is one variable, the # of teams, which has to be an even number. The fixed constants are that every team has to play all of the other teams, but they only play all of the teams once, and all teams play on the same day every week... seems fairly straightforward to do...
So what I have so far seems to work initially, but it then bogs down as the # of possible pairings dwindles... I basically initialize a 2D array (#teams x #teams-1) so for example if there's 10 teams, there would only be 9 sets of matches (9 weeks), as the teams don't play themselves. I have a couple of for loops to strip out the row and column indexes, a random # generator to generate random opponents, and some logic to sort out if the randomly selected team has played the matched team before (across the row) and if they were matched to play any other team that week (down the column).
So in an 8 team environment, you could imagine the first weeks games as team 1 vs team 4, team 2 vs team 3, team 5 vs team 7, and team 6 vs team 8. The rub is that I have seen the algorithm pick the next weeks matchups as team 1 vs team 7, team 2 vs team 5, team 3 vs team 4, and it hangs because now it's trapped itself into team 6 vs team 8 again...
Anyone ever do something like this? I was thinking I'll just keep scrapping the row until the program is able to randomly come up with a matching that works for that week, but I was hoping for a more scientific way to "randomly" generate a schedule...
Any thoughts?