To solve one, I think, ambitious task related to holding a football tournament, I needed to use the procedure of forming competitive pairs, i.e. draw a toss. But it needs to be produced not in the usual “human” way, but automated. Searching for ready-made solutions for a similar task, I found only the formation of baskets of participants in the presence of seeding and unseeded teams (
Example ), which did not suit me and did not solve the task. As a result, after analyzing the procedure of the usual one-round draw, I formed the following algorithm and conditions:
Entry conditions for the draw:
- There are N teams - participants.
- Each team in the first round will play N-1 matches.
- Team N cannot play on any tour with itself.
- In each round, the opponents form unique, not repeating previously pairs.
- If in one of the tours the team N plays with the team M, then respectively in the same round the team M plays with the team N.
Comparing the process of forming random pairs of rivals to the procedure of filling a two-dimensional array of DrawTable [i, j] with random variables, I obtained the following (C #, .Net 4.0):
1. Choose team N from a certain range (in this version, they are ordered by their current place in the championship):
string[] teams = new string[] {"", "", "", "", "", "", "", "", "", "", "", "", " ", " ", "", ""};
2. Check if the cell is filled in DrawTable [i, N]. To do this, initially assign each negative value of each element of the array, for example, {-1}:
drawtable[i, j]=-1; if (drawtable[i, j] < 0) {…}
3. Begin the procedure of forming random opponents for the team N: TableDraw [i, N] = Random = Mi controlling and executing the input conditions 3-5:
int pair = generator.Next(1000, 1000000)%teams.Length;
Initially used
int pair = generator.Next(0, teams.Length)
However, the spread of the generated numbers in this interpretation is insufficient and did not allow to build an array.
Initially, condition 3 is checked:
if (pair != j)
Further vertical
if (pair != drawtable[k, j])
and horizontal checks
if (pair != drawtable[i, k])
conditions 4.
If conditions 3, 4 are satisfied, then a pair is formed:
drawtable[i, j] = pair; drawtable[i, pair] = j;
The above operations are performed for each participant in each round:
for (int i = 0; i < teams.Length - 1; i++) { for (int j = 0; j < teams.Length; j++) { } }
As a result, a competitive table of the following type is formed:

If, due to incorrect array formation, the so-called “Fish” arises (and it arises), a situation where there is no solution, it is necessary to roll back the iterations performed, provided that the larger the array sizes, the greater the number of rollbacks. In this case, the last iteration is rolled back:
fail++; if (fail > teams.Length) {for (int k = 0; k < teams.Length; k++){drawtable[i, k] = -1;} fail = 0; j = -1;}
In this simple way, you can form pairs of participants without using balls, balls, etc., and most importantly, use this procedure for the more extensive task of forming a general calendar of competitions, which is at the design stage.