您现在的位置是:亿华云 > IT科技类资讯
我们在程序员节组织了一场游戏,竟还用Python去验证其公平性?
亿华云2025-10-03 16:05:00【IT科技类资讯】7人已围观
简介本文转载自微信公众号「数据和云」,作者杨豹。转载本文请联系数据和云公众号。程序员节,公司举办了一个抽奖活动,采用的方式是掷六次骰子,组成一个六位数,再对群里的人数取模,计算的结果就是中奖的人的编号。但
本文转载自微信公众号「数据和云」,节组竟还作者杨豹。织场转载本文请联系数据和云公众号。游戏用P验证
程序员节,平性公司举办了一个抽奖活动,节组竟还采用的织场方式是掷六次骰子,组成一个六位数,游戏用P验证再对群里的平性人数取模,计算的节组竟还结果就是中奖的人的编号。但这种方式公平吗?织场让我们用Python来验证下。
一、游戏用P验证验证
掷六次骰子,平性那么这个值就是节组竟还在111111~666666之间,有6的织场6次方(即46656)个随机数。
nums = [x for x in range(111111,游戏用P验证 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()关于作者
杨豹,国泰君安DBA,爱好Oracle、MySQL,Python。
很赞哦!(97)
相关文章
- 2023年数据中心12大趋势
- 为什么大家都选优质域名?到底存在着什么好处?
- 付款完成后,您只需耐心等待,如果您注册成功,系统会提示您。这里需要注意的是,域名是一个即时产品,只有在最终付款成功时才能预订,注册成功后不能更改。
- 以上的就是为大家介绍的关于域名的详解
- 数据中心行业将迎来繁荣,但成本将飙升
- 尽量不要在域名中出现特殊字符,这样的域名很容易导致访问者输入错误,同时给人留下不专业的印象,降低网站的可信度,并流失大量潜在客户。
- 域名和网址一样吗?域名和网址有什么区别?
- 为什么起域名意义非凡?起域名有什么名堂?
- 无服务器并不意味着DevOpsLess或NoOps
- 为啥修改dns服务器?dns服务器与域名有何联系?