一、概述
程序员节,公司举办了一个抽奖活动,采用的方式是掷六次骰子,组成一个六位数,再对群里的人数取模,计算的结果就是中奖的人的编号。但这种方式公平吗,让我们用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,那么对应的数字就是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()
复制
最后修改时间:2021-12-04 18:20:30
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
python中标识符的命名规则和命名规范
周同学带您玩AI
81次阅读
2025-04-21 10:34:44
AI与我共创WEB界面
布衣
59次阅读
2025-04-14 22:13:51
解决pyqt5 textbrowser控件超链接锚点问题
zayki
41次阅读
2025-04-27 16:58:59
python 实现消费者优先级队列
天翼云开发者社区
31次阅读
2025-04-25 11:08:21
优雅遍历和删除特定开头的key
陌殇流苏
27次阅读
2025-04-25 12:17:03
《深入剖析Python的生成器表达式与列表推导式:探寻代码背后的哲学与艺术》
程序员阿伟
25次阅读
2025-04-27 16:22:14
python中的常见数据类型
周同学带您玩AI
16次阅读
2025-04-21 10:34:43
python自动更新dns A记录
godba
12次阅读
2025-04-23 11:19:04
python中不同数据类型转换-布尔型
周同学带您玩AI
10次阅读
2025-04-22 10:12:05
AIOps系列-跳出“工具人”陷阱:从重复劳动到价值创造
韩公子的Linux大集市
8次阅读
2025-05-06 08:49:02