1. 程式人生 > 其它 >Python - 驗證抽獎遊戲的公平性

Python - 驗證抽獎遊戲的公平性

一、概述

程式設計師節,公司舉辦了一個抽獎活動,採用的方式是擲六次骰子,組成一個六位數,再對群裡的人數取模,計算的結果就是中獎的人的編號。但這種方式公平嗎,讓我們用python來驗證下。

二、驗證

擲六次骰子,那麼這個值就是在111111~666666之間,有6的6次方(即46656)個隨機數。

nums = [x for x in range(111111, 666667) if not set(str(x)).intersection('0789')]
print(len(nums))  # 46656

假設群裡有134人,用上面46656個數分別對134取模,最後的結果分佈

total_person = 134
nums_mod = list(map(lambda x: x % total_person, nums))
for i in range(0, total_person):
    print('編號: {}, 中獎次數: {}'.format(i, nums_mod.count(i)))
編號: 0, 中獎次數: 349
編號: 1, 中獎次數: 348
編號: 2, 中獎次數: 348
編號: 3, 中獎次數: 350
編號: 4, 中獎次數: 350
編號: 5, 中獎次數: 346
編號: 6, 中獎次數: 346
編號: 7, 中獎次數: 342
編號: 8, 中獎次數: 342
編號: 9, 中獎次數: 349
編號: 10, 中獎次數: 349
....

看數字不直觀,我們把它轉化為圖片

import matplotlib.pyplot as plt
x = range(0, total_person)
y = [nums_mod.count(i) for i in x]
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set(xlabel='person no.', ylabel='prize counts', title='{} person'.format(total_person))
ax.set_xlim(0, total_person)
ax.set_ylim(0, 1000)
plt.show()

可以看到對於群裡有134個人,還是很公平的哈,假設群裡又加了一個人,變成135人,那麼每人的中獎機率是多少
將total_person改成135,再執行下程式

這時候就不公平了,中獎次數最少的277,最大的有584,而且中獎次數為570多的都是對應的編號尾數為1和6。為什麼會出現這種現象呢。將前面的程式碼改造下。

total_person = 135
from collections import defaultdict
for i in range(0, total_person):
    nums_filter = list(filter(lambda x: x % total_person == i, nums))
    num_last_number = defaultdict(int)
    for j in nums_filter:
        num_last_number[j % 10] += 1
    print('編號: {}, 中獎次數: {}, 骰子尾數統計: {}'.format(i, len(nums_filter), num_last_number))

以下擷取部分結果:可以看到當編號尾數是1或6時,對應的骰子尾數是1或6,而其它的編號對應的骰子尾數都只有一個數字,即0-5,2-2,7-2等。所以才會出現編號尾數為1和6的中獎次數接近其它編號的兩倍。

編號: 0, 中獎次數: 280, 骰子尾數統計: defaultdict(<class 'int'>, {5: 280})
編號: 1, 中獎次數: 577, 骰子尾數統計: defaultdict(<class 'int'>, {1: 297, 6: 280})
編號: 2, 中獎次數: 297, 骰子尾數統計: defaultdict(<class 'int'>, {2: 297})
編號: 3, 中獎次數: 297, 骰子尾數統計: defaultdict(<class 'int'>, {3: 297})
編號: 4, 中獎次數: 297, 骰子尾數統計: defaultdict(<class 'int'>, {4: 297})
編號: 5, 中獎次數: 297, 骰子尾數統計: defaultdict(<class 'int'>, {5: 297})
編號: 6, 中獎次數: 581, 骰子尾數統計: defaultdict(<class 'int'>, {1: 284, 6: 297})
編號: 7, 中獎次數: 284, 骰子尾數統計: defaultdict(<class 'int'>, {2: 284})
編號: 8, 中獎次數: 284, 骰子尾數統計: defaultdict(<class 'int'>, {3: 284})
編號: 9, 中獎次數: 284, 骰子尾數統計: defaultdict(<class 'int'>, {4: 284})
...

三、破局

前面概述提到的辦法對於人數是135就不太公平了呀,怎麼保證公平呢。公平就是每個人獲獎的機率必須是一樣。這就要求骰子擲出來的數字要足夠隨機而且連續。由於骰子只有6個值,為了讓它連續,我們設定骰子的1-6,分別對應數字0-5,而且採用6進位制。
比如骰子擲出來的數分別是1,3,4,6,2,5,那麼對應的數字就是023514,換算成十進位制則為int('023514', 6) = 3430,程式碼就可以換成如下:

nums = [int(str(x), 6) for x in range(0, 555556) if not set(str(x)).intersection('6789')]
print(len(nums))

total_person = 135
nums_mod = list(map(lambda x: x % total_person, nums))
for i in range(0, total_person):
    print('編號: {}, 中獎次數: {}'.format(i, nums_mod.count(i)))

import matplotlib.pyplot as plt
x = range(0, total_person)
y = [nums_mod.count(i) for i in x]
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set(xlabel='person no.', ylabel='prize counts', title='{} person'.format(total_person))
ax.set_xlim(0, total_person)
ax.set_ylim(0, 1000)
plt.show()


這才是

四、總結

本文由公司的一個小遊戲有感而發,主要是想介紹下python中的map和filter函式,以及matplotlib畫圖模組。最後附上一個小遊戲程式碼。

from collections import defaultdict

class Prize:
    DICE_MAX_DIGIT = 5  # 骰子的最大點數,骰子的1-6,對應數字0-5

    def __init__(self, person_nums):
        # 活動人數
        self.person_nums = person_nums
        # 中獎機率差異,這裡控制到1%
        self.percent_diff = 0.01

    def _need_throw_times(self):
        """
        確定需要投擲的次數
        """
        self.throw_time = 1  # 初始投擲次數
        while True:
            max_number = int(str(self.DICE_MAX_DIGIT) * self.throw_time)  # 投擲出來的最大值
            nums = [int(str(x), 6) for x in range(0, max_number+1) if not set(str(x)).intersection('6789')]  # 投擲出來所有可能的十進位制值
            if max(nums) + 1 < self.person_nums:
                # 如果投擲出來的最大值比總人數少,直接增加投擲次數
                self.throw_time += 1
                continue
            prize_dict = defaultdict(int)
            for i in nums:
                prize_dict[i % self.person_nums] += 1
            percent_diff = (max(prize_dict.values()) - min(prize_dict.values()))/max(prize_dict.values())
            if percent_diff < self.percent_diff:
                return self.throw_time
            self.throw_time += 1

    def say(self):
        self._need_throw_times()
        print('本次活動人數為{},請依次投擲{}次骰子'.format(self.person_nums, self.throw_time))
        number_str = ''
        for i in range(self.throw_time):
            point = input('第{}次骰子的點數為: '.format(i + 1))
            if point not in ('1', '2', '3', '4', '5', '6'):
                raise Exception('點數超出範圍')
            number_str += str(int(point) - 1)
        int_number_str = int(number_str, 6)
        print('恭喜{}號中獎!'.format(int_number_str % self.person_nums))

if __name__ == '__main__':
    x = input('請輸入活動的人數: ')
    Prize(int(x)).say()