import random


def randChooseWeighted(lst):
    """Given a list of (weight,item) tuples, pick an item with
       probability proportional to its weight.
    """

    totalweight = sum(w for w,i in lst)
    position = random.uniform(0, totalweight)
    soFar = 0

    # We could use bisect here, but this is not going to be in the
    # critical path.  If it is, oops.
    for w,i in lst:
        soFar += w
        if position < soFar:
            return i

    return lst[-1][1]