大轉盤抽獎機率 固定每一個區域的中獎概率


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))
相關文章
相關標籤/搜索