# Algorithm for generating 2 random teams from player list based on skill level

I'm looking for some assistance and ideas for developing an algorithm for choosing random soccer teams based on the skill levels of the participating players.
What I have so far is a list of particpating players with arbitrary skill levels between 1 and 100. e.g.

``````PlayerA: 30,
PlayerB: 45,
PlayerC: 50,
PlayerD: 55,
PlayerE: 30,
PlayerF: 20,
PlayerG: 75,
PlayerH: 75
``````

I'd like the option of being able to choose random teams, but efficiently running it again to offer different results if the teams just don't look fair (even if on paper, the assigned skill levels match).

I've already coded an example which creates all possible combinations of teams and orders them by fairest first, so that efficiently creates the functionality that allows the person to hit the "randomise" button again and instantly display new teams. However, this is only suitable for teams of 7 per side (maybe 8) as there are just too many combinations after that and it takes too long to process.

Can anyone suggest a better way of doing this for more than 8 players? I've tried the option which mimics the old school yard method of picking players by taking the two best players as the captains and then each player taking the best of who is left until everyone is picked, but then i was stumped if those teams weren't acceptable and they wanted an option to randomise again.

Many thanks - i'm coding this in C# but the language is probably less important than the theory.

• questions about code are off-topic here. Consider asking in StackOverflow instead. Commented Jan 4, 2017 at 12:38
• It wasn't really code I was after, i just added the language I was using for completeness. It was more the theory that I was interested in. Happy to post in Stack Overflow though if people feel this isn't relevant here.
– e-on
Commented Jan 4, 2017 at 12:49
• You are basically asking for an algorithm that only generates fair teams. For an answer, you need to define better what "fair" means. Commented Jan 4, 2017 at 13:06
• The schoolyard approach is a good start, but will always give you the same ordering. However, you could improve it by dividing the players into quartiles, and randomly picking from within the quartile. This might still give you an unbalanced team, but it's fast enough that you can throw teams away and re-pick. Commented Jan 4, 2017 at 13:07
• You could do a combination of the schoolyard approach and what @kdgregory suggested. Determine the best available player and anyone within X skill of that player and randomly pick from that subset. A 100 and a 76 are potentially vastly different scores, but a 100 and a 98 and a 97 might all be roughly interchangeable. The user could even select what X is for that run. Commented Jan 4, 2017 at 13:41

1. Break down users into buckets (owned-it, expert, immediate, beginner, noob)
2. Randomly split the people into two teams from each bucket
3. Any remainders left from each bucket are randomly assigned.

Note that you're trying to make things not skewed one way or the other and, hence, in essence not really assigning players at random. In such scenarious, no matter what algorithm you pick, expect it to fail around the edge cases. Some will fail more than others.

In the 'algo' I describe above, the amount of skew (& hence randomness) is controlled by the no of buckets you have during step 1.

I suggest a time-boxed approach.

1. Generate two random teams using whatever method you have been using
2. Compute the total skill of both teams, and take the difference
3. If the difference is 0, your teams are perfectly balanced. Exit.
4. Trade two random team members and recompute the total skill and difference.
5. If the difference is smaller, proceed with the new teams. Otherwise keep the old ones.
6. Repeat steps 3, 4 and 5 for a fixed number of iterations or a fixed duration of execution, e.g. keep trading for three seconds or a thousand iterations.
7. If you keep getting the same team composition, reduce the number of iterations. If you keep getting very unbalanced teams, increase it.