very simple Genetic Algorithm completed

Carl Banks pavlovevidence at gmail.com
Fri Feb 1 14:57:23 EST 2008


On Feb 1, 9:09 am, Paul McGuire <pt... at austin.rr.com> wrote:
> 2. Bugfix: In 2 places, change:
>         newgene=genes[randint(0,14)-1]
>  to
>         newgene=genes[randint(0,14-1)]
>
> randint(a,b) returns values from a to b inclusive, and genes is a list
> containing 14 elements (so you want subscripts from 0 to 13).  (Using
> subscripts from -1 to 13 will bias the selection of genes to use twice
> as many of the last item in the list, since both -1 and 13 will give
> that value.)


Better yet, use random.randrange: it was added for this very reason,
to get a random index in a range.

Perhaps even better still, use random.choice.  It gets a random
element in a sequence.


Carl Banks



More information about the Python-list mailing list