自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

我們在程序員節(jié)組織了一場游戲,竟還用Python去驗(yàn)證其公平性?

開發(fā) 后端
程序員節(jié),公司舉辦了一個(gè)抽獎活動,采用的方式是擲六次骰子,組成一個(gè)六位數(shù),再對群里的人數(shù)取模,計(jì)算的結(jié)果就是中獎的人的編號。但這種方式公平嗎?讓我們用Python來驗(yàn)證下。

[[432910]]

本文轉(zhuǎn)載自微信公眾號「數(shù)據(jù)和云」,作者楊豹。轉(zhuǎn)載本文請聯(lián)系數(shù)據(jù)和云公眾號。

程序員節(jié),公司舉辦了一個(gè)抽獎活動,采用的方式是擲六次骰子,組成一個(gè)六位數(shù),再對群里的人數(shù)取模,計(jì)算的結(jié)果就是中獎的人的編號。但這種方式公平嗎?讓我們用Python來驗(yàn)證下。

一、驗(yàn)證

擲六次骰子,那么這個(gè)值就是在111111~666666之間,有6的6次方(即46656)個(gè)隨機(jī)數(shù)。

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

假設(shè)群里有134人,用上面46656個(gè)數(shù)分別對134取模,看最后的結(jié)果分布。

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

看數(shù)字不直觀,我們把它轉(zhuǎn)化為圖片:

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

可以看到對于群里有134個(gè)人,還是很公平的,假設(shè)群里又加了一個(gè)人,變成135人,那么每人的中獎幾率是多少呢?

將total_person改成135,再運(yùn)行下程序:

  1. 編號: 0, 中獎次數(shù): 280 
  2. 編號: 1, 中獎次數(shù): 577 
  3. 編號: 2, 中獎次數(shù): 297 
  4. 編號: 3, 中獎次數(shù): 297 
  5. 編號: 4, 中獎次數(shù): 297 
  6. 編號: 5, 中獎次數(shù): 297 
  7. 編號: 6, 中獎次數(shù): 581 
  8. 編號: 7, 中獎次數(shù): 284 
  9. 編號: 8, 中獎次數(shù): 284 
  10. 編號: 9, 中獎次數(shù): 284 
  11. ... 

這時(shí)候就不公平了,中獎次數(shù)最少的277,最大的有584,而且中獎次數(shù)多的都是對應(yīng)的編號尾數(shù)為1和6。為什么會出現(xiàn)這種現(xiàn)象呢?將前面的代碼改造下。

  1. total_person = 135 
  2. from collections import defaultdict 
  3. for i in range(0, total_person): 
  4.     nums_filter = list(filter(lambda x: x % total_person == i, nums)) 
  5.     num_last_number = defaultdict(int
  6.     for j in nums_filter: 
  7.         num_last_number[j % 10] += 1 
  8.     print('編號: {}, 中獎次數(shù): {}, 骰子尾數(shù)統(tǒng)計(jì): {}'.format(i, len(nums_filter), num_last_number)) 

可以看到當(dāng)編號尾數(shù)是1或6時(shí),對應(yīng)的骰子尾數(shù)是1或6,而其它的編號對應(yīng)的骰子尾數(shù)都只有一個(gè)數(shù)字,即0-5,2-2,7-2等。所以才會出現(xiàn)編號尾數(shù)為1和6的中獎次數(shù)接近其它編號的兩倍。

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

二、破局

前面概述提到的辦法對于人數(shù)是135就不太公平了呀,怎么保證公平呢。公平就是每個(gè)人獲獎的幾率必須是一樣。這就要求骰子擲出來的數(shù)字要足夠隨機(jī)而且連續(xù)。由于單個(gè)骰子只有6個(gè)不同值,為了讓它連續(xù),我們設(shè)置骰子的1-6,分別對應(yīng)數(shù)字0-5,而且采用6進(jìn)制。

比如骰子擲出來的數(shù)分別是1、3、4、6、2、5,那么對應(yīng)的數(shù)字就是0、2、3、5、1、4,換算成十進(jìn)制則為int(‘023514’, 6) = 3430,代碼就可以換成如下:

  1. nums = [int(str(x), 6) for x in range(0, 555556) if not set(str(x)).intersection('6789')] 
  2. print(len(nums)) 
  3.  
  4. total_person = 135 
  5. nums_mod = list(map(lambda x: x % total_person, nums)) 
  6. for i in range(0, total_person): 
  7.     print('編號: {}, 中獎次數(shù): {}'.format(i, nums_mod.count(i))) 
  8.  
  9. import matplotlib.pyplot as plt 
  10. x = range(0, total_person) 
  11. y = [nums_mod.count(i) for i in x] 
  12. fig, ax = plt.subplots() 
  13. ax.plot(x, y) 
  14. ax.set(xlabel='person no.', ylabel='prize counts', title='{} person'.format(total_person)) 
  15. ax.set_xlim(0, total_person) 
  16. ax.set_ylim(0, 1000) 
  17. plt.show() 

這才是!

三、總結(jié)

本文由公司的一個(gè)小游戲有感而發(fā),主要是想介紹下Python中的map和filter函數(shù),以及matplotlib畫圖模塊。最后附上一個(gè)小游戲代碼。

  1. from collections import defaultdict 
  2.  
  3. class Prize: 
  4.     DICE_MAX_DIGIT = 5  # 骰子的最大點(diǎn)數(shù),骰子的1-6,對應(yīng)數(shù)字0-5 
  5.  
  6.     def __init__(self, person_nums): 
  7.         # 活動人數(shù) 
  8.         self.person_nums = person_nums 
  9.         # 中獎幾率差異,這里控制到1% 
  10.         self.percent_diff = 0.01 
  11.  
  12.     def _need_throw_times(self): 
  13.         ""
  14.         確定需要投擲的次數(shù) 
  15.         ""
  16.         self.throw_time = 1  # 初始投擲次數(shù) 
  17.         while True
  18.             max_number = int(str(self.DICE_MAX_DIGIT) * self.throw_time)  # 投擲出來的最大值 
  19.             nums = [int(str(x), 6) for x in range(0, max_number+1) if not set(str(x)).intersection('6789')]  # 投擲出來所有可能的十進(jìn)制值 
  20.             if max(nums) + 1 < self.person_nums: 
  21.                 # 如果投擲出來的最大值比總?cè)藬?shù)少,直接增加投擲次數(shù) 
  22.                 self.throw_time += 1 
  23.                 continue 
  24.             prize_dict = defaultdict(int
  25.             for i in nums: 
  26.                 prize_dict[i % self.person_nums] += 1 
  27.             percent_diff = (max(prize_dict.values()) - min(prize_dict.values()))/max(prize_dict.values()) 
  28.             if percent_diff < self.percent_diff: 
  29.                 return self.throw_time 
  30.             self.throw_time += 1 
  31.  
  32.     def say(self): 
  33.         self._need_throw_times() 
  34.         print('本次活動人數(shù)為{},請依次投擲{}次骰子'.format(self.person_nums, self.throw_time)) 
  35.         number_str = '' 
  36.         for i in range(self.throw_time): 
  37.             point = input('第{}次骰子的點(diǎn)數(shù)為: '.format(i + 1)) 
  38.             if point not in ('1''2''3''4''5''6'): 
  39.                 raise Exception('點(diǎn)數(shù)超出范圍'
  40.             number_str += str(int(point) - 1) 
  41.         int_number_str = int(number_str, 6) 
  42.         print('恭喜{}號中獎!'.format(int_number_str % self.person_nums)) 
  43.  
  44. if __name__ == '__main__'
  45.     x = input('請輸入活動的人數(shù): '
  46.     Prize(int(x)).say() 

關(guān)于作者

楊豹,國泰君安DBA,愛好Oracle、MySQL,Python。

 

責(zé)任編輯:武曉燕 來源: 數(shù)據(jù)和云
相關(guān)推薦

2021-09-13 15:38:26

戴爾

2022-09-29 08:00:00

人工智能運(yùn)輸公平性

2013-04-24 09:32:24

程序員創(chuàng)業(yè)

2015-05-25 09:33:16

程序員Bug

2015-08-25 08:26:39

IT公司程序員鼓勵師

2021-10-21 08:30:00

1024嘉年華程序員節(jié)

2012-09-12 15:48:34

程序員節(jié)程序員

2021-11-16 11:32:55

開發(fā)跳躍游戲

2018-08-24 07:39:46

2013-06-27 10:12:47

2022-08-19 09:53:20

人工智能大數(shù)據(jù)風(fēng)險(xiǎn)

2019-11-26 10:15:24

騰訊游戲

2021-06-11 18:48:23

人工智能AI

2023-09-05 10:40:20

Meta人工智能

2011-05-10 13:37:53

程序員

2022-07-02 20:04:51

數(shù)字孿生系統(tǒng)

2010-09-03 16:25:04

程序員

2019-12-18 14:14:40

程序員存儲程序
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號