title: Ranking Selection in Genetic Algorithm code
body: <p>I need code for the <a href="http://www.obitko.com/tutorials/genetic-algorithms/selection.php" rel="nofollow">ranking selection method</a> on a genetic algorithm.
  I have create roulette and tournament selections method but now I need ranking and I am stuck.</p>
  
  <p>My roulette code is here (I am using atom struct for genetic atoms) : </p>
  
  <pre><code>const int roulette (const atom *f)
  {
    int i;
    double sum, sumrnd;
  
    sum = 0;
    for (i = 0; i &lt; N; i++)
      sum += f[i].fitness + OFFSET;
  
    sumrnd = rnd () * sum;
  
    sum = 0;
    for (i = 0; i &lt; N; i++) {
      sum += f[i].fitness + OFFSET;
      if (sum &gt; sumrnd)
        break;
    }
  
    return i;
  }
  </code></pre>
  
  <p>Where atom :</p>
  
  <pre><code>typedef struct atom
  {
    int geno[VARS];
    double pheno[VARS];
    double fitness;
  } atom;
  </code></pre>