def weighted(weights):
        """
        Return a random integer 0 <= N <= len(weights) - 1, where the weights
        determine the probability of each possible integer.

        Based on this StackOverflow post by Ned Batchelder:

        http://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice
        """

        # Convert weights to floats
        weights = [float(w) for w in weights]

        # Pick a value at random
        choice = random.uniform(0, sum(weights))

        # Find the value
        position = 0
        for i, weight in enumerate(weights):
            if position + weight >= choice:
                return i
            position += weight
def weighted ( weights ) : weights = [ float ( w ) for w in weights ] choice = random . uniform ( 0 , sum ( weights ) ) position = 0 for i , weight in enumerate ( weights ) : if position + weight >= choice : return i position += weight
Return a random integer 0 <= N <= len(weights) - 1, where the weights
        determine the probability of each possible integer.

        Based on this StackOverflow post by Ned Batchelder:

        http://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice