import random


def select(weights):
    """
    select a node with probability proportional to its "weight"
    """
    r = random.random() * sum(weights)
    s = 0.0
    for k,w in enumerate(weights):
        s += w
        if r <= s:
            return k
    raise RuntimeError("select WTF from %s" % weights)