- if idler and idler.__class__ != GreetingIdler:
- iiindex = idlers.index(idler)
-
- iilen = len(idlers)
-
- move = int(random()*len(idlers)*average_affinity) + 1
+ # Implementation of the King Of the Hill algorithm from;
+ # http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/
+
+ #def weighted_choice_king(weights):
+ # total = 0
+ # winner = 0
+ # for i, w in enumerate(weights):
+ # total += w
+ # if random.random() * total < w:
+ # winner = i
+ # return winner
+ #
+
+ total = 0
+ winner = None
+
+ for choice in idlers:
+ weight = choice.affinity()
+ total += weight
+ if random() * total < weight:
+ winner = choice