weight.py (994B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\script\util\weight.py 2 import random 3 import sys 4 import types 5 floatType = types.FloatType 6 rand = random.random 7 rang = random.randrange 8 choice = random.choice 9 maxint = sys.maxint 10 11 def ChooseAccum(accumList, roll): 12 for accum, item in accumList: 13 if accum > roll: 14 return item 15 16 raise AssertionError 17 18 19 def SumWeights(weightList): 20 sum = 0 21 accumList = [] 22 for i in range(len(weightList)): 23 sum += weightList[i][0] 24 accumList.append((sum, weightList[i][1])) 25 26 return (sum, accumList) 27 28 29 def ChooseWeighted(weightList): 30 tot, accumList = SumWeights(weightList) 31 if not tot: 32 return random.choice(map(lambda i: i[1], weightList)) 33 if type(tot) == floatType: 34 roll = rand() * tot 35 else: 36 maxi = maxint 37 roll = rang(maxi) * maxi + rang(maxi) 38 roll = roll * tot / (maxi * maxi) 39 return ChooseAccum(accumList, roll)