From 71839729a831fb70b8c4edb060a60fe1e1dd9f3d Mon Sep 17 00:00:00 2001 From: Dave Schuyler Date: Wed, 3 Sep 2003 05:08:33 +0000 Subject: [PATCH] *** empty log message *** --- direct/src/directutil/WeightedChoice.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100755 direct/src/directutil/WeightedChoice.py diff --git a/direct/src/directutil/WeightedChoice.py b/direct/src/directutil/WeightedChoice.py new file mode 100755 index 0000000000..134f609a59 --- /dev/null +++ b/direct/src/directutil/WeightedChoice.py @@ -0,0 +1,19 @@ + +import random + +class WeightedChoice: + def __init__(self, listOfLists, weightIndex=0): + t=0 + for i in listOfLists: + t+=i[weightIndex] + self.total = t + self.listOfLists = listOfLists + self.weightIndex = weightIndex + + def choose(self): + roll = random.randrange(self.total) + weight = self.weightIndex + for i in self.listOfLists: + roll -= i[weight] + if roll <= 0: + return i