weighted choice

Sign up for a GitHub account Sign in All Gists lykkin / v1 Last active May 13, 2015 Code Revisions 2 /.sunken-menu-group /.sunken-menu-contents Embed HTTPS SSH You can clone with HTTPS or SSH . Download Gist /.only-with-full-nav weighted choice View v1 v1 Raw File suppressed. Click to show. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 from random import random class thing: def __init__(self, value, weight=1): self.value = value self.weight = weight def __str__(self): return str(self.value) + ' ' + str(self.weight) things = [thing(x) for x in range(10)] def pick(): total_weight = sum(map(lambda thing: thing.weight, things)) choice_weight = total_weight*ran...

Linked on 2015-05-13 18:33:09 | Similar Links