get_double_reward_index 完整算法复制运用即可
原理一共是100份落到每一份概率相等,循环次数越多抽奖越是准确
def get_double_reward_index(rate_conf):
"""
抽奖概率计算
rate_conf = [[0.25, ''], [0.05, 129600], [0.01, 777600], [0.35, ''], [0.2, ''], [0.14, 10]]
return 奖励index
"""
start, index = 0, 0
rate = [i[0] for i in rate_conf]
randnum = random.uniform(0, sum(rate))
for index, scope in enumerate(rate):
start += scope
if randnum <= start:
break
return index
l1 = [[0.25, ''], [0.05, 129600], [0.01, 777600], [0.35, ''], [0.2, ''], [0.14, 10]]
c = get_double_reward_index(l1)
total, i0, i1, i2, i3, i4, i5 = 0, 0, 0, 0, 0, 0, 0
for i in range(1000000):
index = get_double_reward_index(rate_conf=l1)
total += 1
if index == 0:
i0 += 1
elif index == 1:
i1 += 1
elif index == 2:
i2 += 1
elif index == 3:
i3 += 1
elif index == 4:
i4 += 1
elif index == 5:
i5 += 1
print("index 0 概率 %f" % ((i0 / total) * 100))
print("index 1 概率 %f" % ((i1 / total) * 100))
print("index 2 概率 %f" % ((i2 / total) * 100))
print("index 3 概率 %f" % ((i3 / total) * 100))
print("index 4 概率 %f" % ((i4 / total) * 100))
print("index 5 概率 %f" % ((i5 / total) * 100))