def weighted_choice(seq, cdf):
assert len(seq) == len(cdf)<EOL>rand = random.random()<EOL>for i, e in enumerate(seq):<EOL><INDENT>cp = cdf[i]<EOL>assert <NUM_LIT:0> <= cp <= <NUM_LIT:1><EOL>if rand < cp:<EOL><INDENT>return e<EOL><DEDENT><DEDENT>
Select a random element from a sequence, given cumulative probabilities of selection.

See ``compute_fitness_cdf`` function for obtaining cumulative probabilities.

seq:  sequence to select from
cdf:  sequence with 1 cumulative probability value in [0, 1] for each element in ``seq``

return:  randomly selected element