我们在程序员节组织了一场游戏,竟还用Python去验证其公平性?

开发 后端
程序员节,公司举办了一个抽奖活动,采用的方式是掷六次骰子,组成一个六位数,再对群里的人数取模,计算的结果就是中奖的人的编号。但这种方式公平吗?让我们用Python来验证下。

[[432910]]

本文转载自微信公众号「数据和云」,作者杨豹。转载本文请联系数据和云公众号。

程序员节,公司举办了一个抽奖活动,采用的方式是掷六次骰子,组成一个六位数,再对群里的人数取模,计算的结果就是中奖的人的编号。但这种方式公平吗?让我们用Python来验证下。

一、验证

掷六次骰子,那么这个值就是在111111~666666之间,有6的6次方(即46656)个随机数。

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

假设群里有134人,用上面46656个数分别对134取模,看最后的结果分布。

  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('编号: {}, 中奖次数: {}'.format(i, nums_mod.count(i))) 
  5. 编号: 0, 中奖次数: 349 
  6. 编号: 1, 中奖次数: 348 
  7. 编号: 2, 中奖次数: 348 
  8. 编号: 3, 中奖次数: 350 
  9. 编号: 4, 中奖次数: 350 
  10. 编号: 5, 中奖次数: 346 
  11. 编号: 6, 中奖次数: 346 
  12. 编号: 7, 中奖次数: 342 
  13. 编号: 8, 中奖次数: 342 
  14. 编号: 9, 中奖次数: 349 
  15. 编号: 10, 中奖次数: 349 
  16. .... 

看数字不直观,我们把它转化为图片:

  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个人,还是很公平的,假设群里又加了一个人,变成135人,那么每人的中奖几率是多少呢?

将total_person改成135,再运行下程序:

  1. 编号: 0, 中奖次数: 280 
  2. 编号: 1, 中奖次数: 577 
  3. 编号: 2, 中奖次数: 297 
  4. 编号: 3, 中奖次数: 297 
  5. 编号: 4, 中奖次数: 297 
  6. 编号: 5, 中奖次数: 297 
  7. 编号: 6, 中奖次数: 581 
  8. 编号: 7, 中奖次数: 284 
  9. 编号: 8, 中奖次数: 284 
  10. 编号: 9, 中奖次数: 284 
  11. ... 

这时候就不公平了,中奖次数最少的277,最大的有584,而且中奖次数多的都是对应的编号尾数为1和6。为什么会出现这种现象呢?将前面的代码改造下。

  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('编号: {}, 中奖次数: {}, 骰子尾数统计: {}'.format(i, len(nums_filter), num_last_number)) 

可以看到当编号尾数是1或6时,对应的骰子尾数是1或6,而其它的编号对应的骰子尾数都只有一个数字,即0-5,2-2,7-2等。所以才会出现编号尾数为1和6的中奖次数接近其它编号的两倍。

  1. 编号: 0, 中奖次数: 280, 骰子尾数统计: defaultdict(<class 'int'>, {5: 280}) 
  2. 编号: 1, 中奖次数: 577, 骰子尾数统计: defaultdict(<class 'int'>, {1: 297, 6: 280}) 
  3. 编号: 2, 中奖次数: 297, 骰子尾数统计: defaultdict(<class 'int'>, {2: 297}) 
  4. 编号: 3, 中奖次数: 297, 骰子尾数统计: defaultdict(<class 'int'>, {3: 297}) 
  5. 编号: 4, 中奖次数: 297, 骰子尾数统计: defaultdict(<class 'int'>, {4: 297}) 
  6. 编号: 5, 中奖次数: 297, 骰子尾数统计: defaultdict(<class 'int'>, {5: 297}) 
  7. 编号: 6, 中奖次数: 581, 骰子尾数统计: defaultdict(<class 'int'>, {1: 284, 6: 297}) 
  8. 编号: 7, 中奖次数: 284, 骰子尾数统计: defaultdict(<class 'int'>, {2: 284}) 
  9. 编号: 8, 中奖次数: 284, 骰子尾数统计: defaultdict(<class 'int'>, {3: 284}) 
  10. 编号: 9, 中奖次数: 284, 骰子尾数统计: defaultdict(<class 'int'>, {4: 284}) 
  11. ... 

二、破局

前面概述提到的办法对于人数是135就不太公平了呀,怎么保证公平呢。公平就是每个人获奖的几率必须是一样。这就要求骰子掷出来的数字要足够随机而且连续。由于单个骰子只有6个不同值,为了让它连续,我们设置骰子的1-6,分别对应数字0-5,而且采用6进制。

比如骰子掷出来的数分别是1、3、4、6、2、5,那么对应的数字就是0、2、3、5、1、4,换算成十进制则为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('编号: {}, 中奖次数: {}'.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() 

这才是!

三、总结

本文由公司的一个小游戏有感而发,主要是想介绍下Python中的map和filter函数,以及matplotlib画图模块。最后附上一个小游戏代码。

  1. from collections import defaultdict 
  2.  
  3. class Prize: 
  4.     DICE_MAX_DIGIT = 5  # 骰子的最大点数,骰子的1-6,对应数字0-5 
  5.  
  6.     def __init__(self, person_nums): 
  7.         # 活动人数 
  8.         self.person_nums = person_nums 
  9.         # 中奖几率差异,这里控制到1% 
  10.         self.percent_diff = 0.01 
  11.  
  12.     def _need_throw_times(self): 
  13.         ""
  14.         确定需要投掷的次数 
  15.         ""
  16.         self.throw_time = 1  # 初始投掷次数 
  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')]  # 投掷出来所有可能的十进制值 
  20.             if max(nums) + 1 < self.person_nums: 
  21.                 # 如果投掷出来的最大值比总人数少,直接增加投掷次数 
  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('本次活动人数为{},请依次投掷{}次骰子'.format(self.person_nums, self.throw_time)) 
  35.         number_str = '' 
  36.         for i in range(self.throw_time): 
  37.             point = input('第{}次骰子的点数为: '.format(i + 1)) 
  38.             if point not in ('1''2''3''4''5''6'): 
  39.                 raise Exception('点数超出范围'
  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('请输入活动的人数: '
  46.     Prize(int(x)).say() 

关于作者

杨豹,国泰君安DBA,爱好Oracle、MySQL,Python。

 

责任编辑:武晓燕 来源: 数据和云
相关推荐

2021-09-13 15:38:26

戴尔

2022-09-29 08:00:00

人工智能运输公平性

2013-04-24 09:32:24

程序员创业

2015-05-25 09:33:16

程序员Bug

2015-08-25 08:26:39

IT公司程序员鼓励师

2021-10-21 08:30:00

1024嘉年华程序员节

2018-08-24 07:39:46

2022-08-19 09:53:20

人工智能大数据风险

2023-09-05 10:40:20

Meta人工智能

2021-11-16 11:32:55

开发跳跃游戏

2013-06-27 10:12:47

2021-06-11 18:48:23

人工智能AI

2012-09-12 15:48:34

程序员节程序员

2019-11-26 10:15:24

腾讯游戏

2024-02-07 08:00:00

算法推荐系统BPR

2021-03-15 10:49:23

大数据算法人工智能

2022-07-02 20:04:51

数字孪生系统

2011-05-10 13:37:53

程序员

2010-09-03 16:25:04

程序员
点赞
收藏

51CTO技术栈公众号