elsa wrote:
Hi John and others,

sorry about my etiquette errors. As you can tell I'm a newbie, and
appreciate all the help I can get. I'm trying to master this thing
with only the net and a couple of books as tutors.

Here is what I'm running at the interactive prompt:

[snip]

def event():
        """
        randomly chooses a birth (prob .3), death (prob .1) or mutation
(prob .1) event
        """
        choice = random.random()
        if choice <= .3:
                event='b'
        elif choice <= .4:
                event ='d'
        elif choice<= .5:
                event = 'm'
        else:
                event = 'None'
        return event

[snip]
Here's my line of thought:

Currently, 0 <= choice < 1.0.

If you multiplied the random number by 10 then the probability
thresholds could all be ints, and if you're using ints then 'choice'
could be an index into a list:

event_choices = ['b', 'b', 'b', 'd', 'm'] + ['None'] * 5

which then suggests using random.choice() instead:

def event():
        """
randomly chooses a birth (prob .3), death (prob .1) or mutation (prob .1) event
        """
        return random.choice(event_choices)

and you could put the event code inline instead of in a function, which
saves the cost of a function call.

Incidentally, for a probability of 0.3 the test should be "choice <
0.3", etc, not "choice <= 0.3", etc, although I suspect that you won't
notice any difference in the results! :-)
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to