## 用python实现的两种抽奖算法(概率随机抽奖、次数随机算法)

------------次数随机算法----------

``````#抽奖一次少一个名额

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
for k, v in self.weight.items():
WEIGHT[k] = 1000000 * v / valueCount  # 一百万乘以权重所占百分比
# 区间 [1,400000,400000+600000]
self.compare = {"FIRST_PART": 0}
tmp = 0
for k, v in WEIGHT.items():
tmp += v
self.compare[k] = tmp

def drawing(self):
r = self.rd.randrange(1, 1000001)  # 随机数
#print("随机数 : ", r)
tmp = 0
name = ''
for k, v in self.compare.items():
#print('k : ', k, "v :", v)    #先判断随机数是否小于等于范围
if r <= v:
if tmp==0:                #第一次判断
tmp=v
name=k
if v<tmp:
tmp = v
name = k

print(name)
self.weight[k]-=1  #每次执行少一次奖励

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
import tkinter as tk
from tkinter import messagebox
def setWeight(self, weight):
for k, v in weight.items():
for t in range(v):
self.chanceList.append(k)

def drawing(self):
if (len(self.chanceList) <= 0):
self.messagebox.showwarning('ERROR', '奖品数不够')
else:
r = self.rd.randrange(0, len(self.chanceList))  # 随机数
# print("随机数 : ", r)
self.rd.shuffle(self.chanceList)  # 打乱列表
res = self.chanceList.pop(r)
self.messagebox.showinfo('开奖结果', res)
print(res)

try:
if self.inputName.get() != None and self.inputName.get() != '':
if self.inputChance.get()!= '' and isinstance(int(self.inputChance.get()), int):
self.Chance = int(self.inputChance.get())
self.Name = self.inputName.get()
self.weight[self.Name] = self.Chance
self.setWeight(self.weight)
self.weight = {}
self.messagebox.showinfo('SUCCESS', '添加奖品成功!')
self.inputName.delete(first=0, last='end')
self.inputChance.delete(first=0, last='end')
else:
self.messagebox.showwarning('ERROR', '请输入一个整数!')
else:
self.messagebox.showwarning('ERROR', '请输入奖品名称!')

except Exception as e:
self.messagebox.showwarning(e)

def checkPrize(self):
if (len(self.chanceList) <= 0):
self.messagebox.showinfo('ERROR', '没有奖品了')
else:
store = {}
for tmp in self.chanceList:
if store.get(tmp)!=None:
store[tmp] += 1
else :
store[tmp] = 1
message = ''
for k, v in store.items():
message +=  k + '剩余 : ' + str(v)+ '个'+'\n'

self.messagebox.showinfo('查询结果', message)

def graphicsUI(self):
self.window = self.tk.Tk(className='抽奖')  # 窗口

self.nameTitle = self.tk.Label()
self.nameTitle["text"] = "在此输入奖品名称"
self.nameTitle.pack()

self.inputName = self.tk.Entry()
self.inputName.pack()

self.chanceTitle = self.tk.Label()
self.chanceTitle["text"] = "在此输入奖品个数"
self.chanceTitle.pack()

self.inputChance = self.tk.Entry()
self.inputChance.pack()

self.checkButton = self.tk.Button()
self.checkButton["text"] = '查询剩余奖品'
self.checkButton["command"] = self.checkPrize
self.checkButton.pack(side='left')

self.drawButton = self.tk.Button()
self.drawButton["text"] = '开奖'
self.drawButton["command"] = self.drawing

self.window.mainloop()

def __init__(self):
self.weight = {}
self.chanceList = []

if __name__ == "__main__":
test = randomMachine()
test.graphicsUI()``````

0条评论