user13544: I am writing a pairing program for round-robin/double round-robin/swiss tournaments. I am looking for the rules to follow while generating the pairings. There seem to be no rules in the FIDE handbook. The only rule I know is that in tournaments like the FIDE Candidates players of the same federation are paired together as early as possible. Do there exist any other restrictions, or do I just pair the players randomly such that each player plays every other player in the tournament?

user21007: Example for ten players - old style:
n=10
1   2   3   4   5
6   7   8   9    

You go until n-1 and start over, n/2 numbers each line:
1   2   3   4   5
6   7   8   9   1
2   3   4   5   6
7   8   9   1   2
3   4   5   6   7
8   9   1   2   3
4   5   6   7   8
9   1   2   3   4
5   6   7   8   9

Then you basically ignore the left-most numbers and start the same procedure from the end to start.
 1 |    2 9     3 8     4 7     5 6
 6 |    7 5     8 4     9 3     1 2
 2 |    3 1     4 9     5 8     6 7
 7 |    8 6     9 5     1 4     2 3
 3 |    4 2     5 1     6 9     7 8
 8 |    9 7     1 6     2 5     3 4
 4 |    5 3     6 2     7 1     8 9
 9 |    1 8     2 7     3 6     4 5
 5 |    6 4     7 3     8 2     9 1   <- start here

Then the 10 (n):
 1 (10) |   2 9     3 8     4 7     5 6
 (10) 6 |   7 5     8 4     9 3     1 2
 2 (10) |   3 1     4 9     5 8     6 7
 (10) 7 |   8 6     9 5     1 4     2 3
 3 (10) |   4 2     5 1     6 9     7 8
 (10) 8 |   9 7     1 6     2 5     3 4
 4 (10) |   5 3     6 2     7 1     8 9
 (10) 9 |   1 8     2 7     3 6     4 5
 5 (10) |   6 4     7 3     8 2     9 1

For each player color change nearly all rounds.
Gives also a nice obvious cross table. Also lots of math in the system.

user4418: 
I am looking for the rules to follow while generating the pairings.
  There seem to be no rules in the FIDE handbook.

The pairings are the responsibility of the arbiter and the FIDE Arbiters Commission produces a handbook which it maintains for the guidance of arbiters in the commission of their duties.
This handbook collects together various documents some of which appear in other places on the FIDE website. These include -

An annotated copy of the FIDE Laws of Chess
Guidelines for Organisers
FIDE Competition (Tournament) Rules
General Rules and Recommendations for Tournaments
International Title Regulations
FIDE Rating Regulations
Regulations for the Titles of Arbiters
Anti Cheating Guidelines
The Role of the Arbiters and their Duties


Do there exist any other restrictions, or do I just pair the players
  randomly such that each player plays every other player in the
  tournament?

For your question the important section is section 7. Pairings in the FIDE Competition (Tournament) Rules. There it says -

7.5 The pairings for a round robin should be made in accordance with the Berger tables (Annex 1), adjusted where necessary for double-round
  events.

So, no you do not pair the players randomly. You pair using the Berger tables. The allocation of players to start numbers is governed by the other articles in that section -

7.1 Responsibility for the drawing of lots and the actual pairings rests with the CA.
  7.2 The drawing of lots for the first round of a round-robin competition shall be arranged by the CO, to be open to the players.
  7.3 In L1, L2: round robin competitions and preferable Swisses, the drawing of lots shall take place at least 12 hours before the start of
  the first round. In L1 all participants shall attend the ceremony of
  drawing of lots. A player who has not arrived on time for the drawing
  of lots may be included at the discretion of the CA. The first-round
  pairings shall be announced as soon as possible thereafter.
  7.4 If a player withdraws or is excluded from a competition after the drawing of lots but before the beginning of the first round, or there
  are additional entries, the announced pairings shall remain unaltered.
  Additional pairings or changes may be made at the discretion of the CA
  in consultation with the players directly involved, but only if these
  minimise amendments to pairings that have already been announced.

In response to -

The only rule I know is that in tournaments like the FIDE Candidates
  players of the same federation are paired together as early as
  possible.

This is covered in articles 7.6 and 7.7 which specify that the Varma tables may be used in conjunction with the Berger tables to provide a way of restricting the drawing of lots to ensure this.

7.6 If the pairings are to be restricted in any way - for example, players from the same federation shall, if possible, not meet in the
  last three rounds - this shall be communicated to the players as soon
  as possible, but not later than the start of the first round.
  7.7 For round-robin competitions this restricted drawing of lots may be done by using the Varma tables, reproduced in Annex 2, which can be
  used for competitions of 9 to 24 players

The Berger tables and Varma tables appear at the end of the FIDE Competition (Tournament) Rules

13544: <p>I am writing a pairing program for round-robin/double round-robin/swiss tournaments. I am looking for the rules to follow while generating the pairings. There seem to be no rules in the FIDE handbook. The only rule I know is that in tournaments like the FIDE Candidates players of the same federation are paired together as early as possible. Do there exist any other restrictions, or do I just pair the players randomly such that each player plays every other player in the tournament?</p>
 21007: <p>Example for ten players - old style:
n=10</p>

<pre><code>1   2   3   4   5
6   7   8   9    
</code></pre>

<p>You go until n-1 and start over, n/2 numbers each line:</p>

<pre><code>1   2   3   4   5
6   7   8   9   1
2   3   4   5   6
7   8   9   1   2
3   4   5   6   7
8   9   1   2   3
4   5   6   7   8
9   1   2   3   4
5   6   7   8   9
</code></pre>

<p>Then you basically ignore the left-most numbers and start the same procedure from the end to start.</p>

<pre><code> 1 |    2 9     3 8     4 7     5 6
 6 |    7 5     8 4     9 3     1 2
 2 |    3 1     4 9     5 8     6 7
 7 |    8 6     9 5     1 4     2 3
 3 |    4 2     5 1     6 9     7 8
 8 |    9 7     1 6     2 5     3 4
 4 |    5 3     6 2     7 1     8 9
 9 |    1 8     2 7     3 6     4 5
 5 |    6 4     7 3     8 2     9 1   &lt;- start here
</code></pre>

<p>Then the 10 (n):</p>

<pre><code> 1 (10) |   2 9     3 8     4 7     5 6
 (10) 6 |   7 5     8 4     9 3     1 2
 2 (10) |   3 1     4 9     5 8     6 7
 (10) 7 |   8 6     9 5     1 4     2 3
 3 (10) |   4 2     5 1     6 9     7 8
 (10) 8 |   9 7     1 6     2 5     3 4
 4 (10) |   5 3     6 2     7 1     8 9
 (10) 9 |   1 8     2 7     3 6     4 5
 5 (10) |   6 4     7 3     8 2     9 1
</code></pre>

<p>For each player color change nearly all rounds.
Gives also a nice obvious cross table. Also lots of math in the system.</p>
 4418: <blockquote>
  <p>I am looking for the rules to follow while generating the pairings.
  There seem to be no rules in the FIDE handbook.</p>
</blockquote>

<p>The pairings are the responsibility of the arbiter and the <a href="http://arbiters.fide.com/" rel="nofollow noreferrer">FIDE Arbiters Commission</a> produces a <a href="http://arbiters.fide.com/images/stories/downloads/2018/Arbiters-Manual-2018-v1.pdf" rel="nofollow noreferrer">handbook</a> which it maintains for the guidance of arbiters in the commission of their duties.</p>

<p>This handbook collects together various documents some of which appear in other places on the FIDE website. These include -</p>

<ol>
<li>An annotated copy of the FIDE Laws of Chess</li>
<li>Guidelines for Organisers</li>
<li>FIDE Competition (Tournament) Rules</li>
<li>General Rules and Recommendations for Tournaments</li>
<li>International Title Regulations</li>
<li>FIDE Rating Regulations</li>
<li>Regulations for the Titles of Arbiters</li>
<li>Anti Cheating Guidelines</li>
<li>The Role of the Arbiters and their Duties</li>
</ol>

<blockquote>
  <p>Do there exist any other restrictions, or do I just pair the players
  randomly such that each player plays every other player in the
  tournament?</p>
</blockquote>

<p>For your question the important section is section <em>7. Pairings</em> in the FIDE Competition (Tournament) Rules. There it says -</p>

<blockquote>
  <p>7.5 The pairings for a round robin should be made in accordance with the Berger tables (Annex 1), adjusted where necessary for double-round
  events.</p>
</blockquote>

<p>So, no you do not pair the players randomly. You pair using the Berger tables. The allocation of players to start numbers is governed by the other articles in that section -</p>

<blockquote>
  <p>7.1 Responsibility for the drawing of lots and the actual pairings rests with the CA.<br>
  7.2 The drawing of lots for the first round of a round-robin competition shall be arranged by the CO, to be open to the players.<br>
  7.3 In L1, L2: round robin competitions and preferable Swisses, the drawing of lots shall take place at least 12 hours before the start of
  the first round. In L1 all participants shall attend the ceremony of
  drawing of lots. A player who has not arrived on time for the drawing
  of lots may be included at the discretion of the CA. The first-round
  pairings shall be announced as soon as possible thereafter.<br>
  7.4 If a player withdraws or is excluded from a competition after the drawing of lots but before the beginning of the first round, or there
  are additional entries, the announced pairings shall remain unaltered.
  Additional pairings or changes may be made at the discretion of the CA
  in consultation with the players directly involved, but only if these
  minimise amendments to pairings that have already been announced.</p>
</blockquote>

<p>In response to -</p>

<blockquote>
  <p>The only rule I know is that in tournaments like the FIDE Candidates
  players of the same federation are paired together as early as
  possible.</p>
</blockquote>

<p>This is covered in articles 7.6 and 7.7 which specify that the Varma tables may be used in conjunction with the Berger tables to provide a way of restricting the drawing of lots to ensure this.</p>

<blockquote>
  <p>7.6 If the pairings are to be restricted in any way - for example, players from the same federation shall, if possible, not meet in the
  last three rounds - this shall be communicated to the players as soon
  as possible, but not later than the start of the first round.<br>
  7.7 For round-robin competitions this restricted drawing of lots may be done by using the Varma tables, reproduced in Annex 2, which can be
  used for competitions of 9 to 24 players</p>
</blockquote>

<p>The Berger tables and Varma tables appear at the end of the <em>FIDE Competition (Tournament) Rules</em></p>
