------------次数随机算法----------
#抽奖一次少一个名额
class randomMachine(object):
import random as rd
def setWeight(self, weight):
self.weight = weight
self.chanceList=[]
for k,v in self.weight.items():
for t in range(v):
self.chanceList.append(k)
def drawing(self):
r = self.rd.randrange(0, len(self.chanceList)) # 随机数
#print("随机数 : ", r)
print(self.chanceList.pop(r))
def graphicsUI(self):
pass
def start(self):
pass
if __name__ == "__main__":
test = randomMachine()
test.setWeight({"一等奖": 1, "二等奖": 1, "三等奖": 1, "安慰奖": 6})
for i in range(9):
test.drawing()
运行结果:
---------概率随机算法----------
class randomMachine(object):
import random as rd
def setWeight(self, weight):
self.weight = weight
WEIGHT = {}
weightLength = len(self.weight) # 权重个数
valueCount = 0 # 权重合计
for v in self.weight.values():
valueCount += v