點擊上方"藍字"
關注我們,享更多干貨!
程式員節,公司舉辦了一個抽獎活動,采用的方式是擲六次骰子,組成一個六位數,再對群里的人數取模,計算的結果就是中獎的人的編號,但這種方式公平嗎?讓我們用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,再運行下程式:
編號: 0, 中獎次數: 280
編號: 1, 中獎次數: 577
編號: 2, 中獎次數: 297
編號: 3, 中獎次數: 297
編號: 4, 中獎次數: 297
編號: 5, 中獎次數: 297
編號: 6, 中獎次數: 581
編號: 7, 中獎次數: 284
編號: 8, 中獎次數: 284
編號: 9, 中獎次數: 284
...

這時候就不公平了,中獎次數最少的277,最大的有584,而且中獎次數多的都是對應的編號尾數為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,那么對應的數字就是0、2、3、5、1、4,換算成十進制則為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()
墨天輪原文鏈接:https://www.modb.pro/db/151967?sjhy(復制到瀏覽器或者點擊“閱讀原文”立即查看)
關于作者
楊豹,國泰君安DBA,愛好Oracle、MySQL,Python,

END

由中國DBA聯盟和墨天輪主辦的 2021資料技術嘉年華 將于11月19日-20日在北京麗都皇冠假日酒店盛大召開,
大會門票限時0元領取
掃描下方的二維碼即可!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/348291.html
標籤:其他
