代码:
# -*- coding: utf-8 -*-
import random
# 抽奖概率(可设置)
randomList = []
# 奖项 10个
gift = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# 设置礼品和抽奖百分比概率
r = [{'giftIndex': 0, 'value': 1},
{'giftIndex': 1, 'value': 1},
{'giftIndex': 2, 'value': 1},
{'giftIndex': 3, 'value': 2},
{'giftIndex': 4, 'value': 10},
{'giftIndex': 5, 'value': 15},
{'giftIndex': 6, 'value': 20},
{'giftIndex': 7, 'value': 20},
{'giftIndex': 8, 'value': 20},
{'giftIndex': 9, 'value': 10}]
for k in r:
for h in xrange(k.get('value')):
randomList.append(k.get('giftIndex'))
# 将集合打乱
random.shuffle(randomList)
print ("共:%d" % len(randomList))
print randomList
num = raw_input("输入抽奖的次数")
for i in xrange(int(num)):
# 随机取数
randomNum = random.randint(1, len(randomList))
# 取到对应的index
giftIndex = randomList[randomNum - 1]
# 礼物命中次数+1
gift[giftIndex] += 1
for j in xrange(len(gift)):
print(u"第%d号球的概率为:%f " %