浏览代码

added weighted choice, randFloat

Darren Ranalli 21 年之前
父节点
当前提交
24638f7e96
共有 1 个文件被更改,包括 21 次插入0 次删除
  1. 21 0
      direct/src/showbase/PythonUtil.py

+ 21 - 0
direct/src/showbase/PythonUtil.py

@@ -6,6 +6,7 @@ import operator
 import inspect
 import inspect
 import os
 import os
 import sys
 import sys
+import random
 
 
 import Verify
 import Verify
 
 
@@ -839,3 +840,23 @@ def mostDerivedLast(classList):
         #print a,b,result
         #print a,b,result
         return result
         return result
     classList.sort(compare)
     classList.sort(compare)
+
+def weightedChoice(choiceList, rng=random.random):
+    """given a list of (probability,item) pairs, chooses an item based on the
+    probabilities. rng must return 0..1"""
+    sum = 0.
+    for prob, item in choiceList:
+        sum += prob
+    rand = rng()
+    accum = rand * sum
+    for prob, item in choiceList:
+        accum -= prob
+        if accum <= 0.:
+            return item
+    # rand must be ~1., and floating-point error prevented accum from
+    # hitting 0. Return the last item.
+    return item
+
+def randFloat(a, b, rng=random.random):
+    """returns a random float in [a,b]"""
+    return lerp(a,b,rng())