用python验证赌徒输光定理

规则如下:

玩家和庄家对赌硬币,如果正面朝上,本轮玩家赢,庄家付给玩家⼀元;如果反面朝上,本轮玩家输,玩家付给庄家⼀元。假设玩家⼿上有10元硬币,⼿上钱⼀旦输光则退出游戏。

python代码模拟:

import random

import matplotlib.pyplot as plt


class Gambler:
    def __init__(self, persons: int, rounds: int):
        """初始化赌徒的人数,赌博的轮数"""
        self.persons= persons   # 赌徒的数量
        self.rounds = rounds    # 赌博的轮数
        self.win_P = 0      # 赢钱的人数
        self.loss_P = 0     # 输钱的人数
        self.quit_P = 0     # 输光的人数

    def gamble(self):
        """对每个赌徒进行设定的赌博轮数"""
        for person in range(self.persons):
            # 遍历每个赌徒
            money = 10  # 每个赌徒的初始硬币为10
            for _ in range(self.rounds):
                # 对每个赌徒进行self.rounds次赌博
                a = random.random()     # a为[0,1)的随机数,小于0.5,则赌徒输,否则赌徒赢
                if a < 0.5:
                    money -= 1  # 赌输则扣除1个硬币
                else:
                    money += 1  # 赌赢则获得1个硬币
                if money == 0:  # 钱输光,直接结束
                    break
            # 对当前的赌徒进行判定:
            if money == 0:
                self.quit_P += 1    # 钱输光,则quit人数加1
            elif money < 10:
                self.loss_P += 1    # 钱小于10,则loss人数加1
            else:
                self.win_P += 1     # 钱大于10,则win人数加1

    def plot_bar(self):
        """绘制赌博后输赢情况的条形图"""
        x = ['Quit', 'Win', 'Loss']
        y = [self.quit_P, self.win_P, self.loss_P]
        plt.bar(x, y, align='center')
        plt.title('rounds=' + str(self.rounds) + ' persons=' + str(self.persons))
        plt.ylabel('person')
        plt.show()


# 对赌模拟1,10000人,对赌30次
g = Gambler(10000, 30)
g.gamble()
g.plot_bar()

# 对赌模拟2,10000人,对赌1000次
g1 = Gambler(10000, 1000)
g1.gamble()
g1.plot_bar()

模拟结果:

模拟1:赌徒10000人,每人对赌30次,得到的结果如下:

 模拟2:赌徒10000人,每人对赌100000

 结论:

由上述的对赌结果可以看出,当每个赌徒只进行30次对赌时,有超过半数的赌徒都赢钱了,但是,随着对赌轮数的增加,由30轮增加到1000轮时,超过7成的赌徒都输光了手中的钱。由此可以证明,赌徒赢钱是一时的,随着对赌轮数的增加,会有越来越多的赌徒会输光手中的钱。

猜你喜欢

转载自blog.csdn.net/lyb06/article/details/129888646