Posted By

alfirth on 09/09/11


Tagged


Versions (?)

Roulette Wheel


 / Published in: Perl
 

  1. sub pick {
  2. #################
  3. #construct the triangular number sequence of the sums of the fitness _ranks_ of the (ascension sorted) population containing POP elements
  4. #pick a random point less than the total of the sums of said triangular sequence
  5. #determine which element's interval this falls into
  6. #select and return that element
  7. #################
  8. my ($pop) = @_;
  9. #sum of the triangular sequence 1..POP
  10. state $tot = (POP+POP**2)/2;
  11. #found by solving x = (n^2-n)/2 + n for n
  12. my $elem = int( (sqrt(1 + 8*rand($tot)) - 1) / 2 );
  13. return $pop->[$elem];
  14. }

Report this snippet  

You need to login to post a comment.