代码:
# -*- 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 " % (j, gift[j] * 100 * 1.000 / int(num)))
输入: 1000
输出:
共:100
[8, 8, 8, 8, 7, 8, 7, 7, 7, 2, 0, 7, 5, 6, 4, 8, 6, 7, 5, 6, 8, 8, 6, 6, 8, 4, 7, 7, 9, 3, 8, 6, 7, 6, 5, 4, 7, 6, 5, 4, 8, 9, 9, 5, 7, 4, 9, 9, 5, 7, 6, 4, 9, 5, 9, 5, 4, 7, 8, 6, 7, 5, 7, 6, 6, 4, 9, 5, 7, 6, 4, 7, 9, 6, 7, 8, 6, 9, 3, 6, 7, 6, 8, 5, 6, 8, 5, 5, 5, 6, 5, 8, 8, 1, 4, 8, 8, 8, 7, 6]
输入抽奖的次数 1000
第0号球的概率为:1.100000
第1号球的概率为:1.000000
第2号球的概率为:1.100000
第3号球的概率为:1.800000
第4号球的概率为:10.700000
第5号球的概率为:15.600000
第6号球的概率为:19.500000
第7号球的概率为:18.500000
第8号球的概率为:21.100000
第9号球的概率为:9.600000