Python

Python

一、概述

程序员节,公司举办了一个抽奖活动,采用的方式是掷六次骰子,组成一个六位数,再对群里的人数取模,计算的结果就是中奖的人的编号。但这种方式公平吗,让我们用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()

Python

可以看到对于群里有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
...

Python

这时候就不公平了,中奖次数最少的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
这才是
Python

四、总结

本文由公司的一个小游戏有感而发,主要是想介绍下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()