Explorar o código

*** empty log message ***

Dave Schuyler %!s(int64=22) %!d(string=hai) anos
pai
achega
71839729a8
Modificáronse 1 ficheiros con 19 adicións e 0 borrados
  1. 19 0
      direct/src/directutil/WeightedChoice.py

+ 19 - 0
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