Use python to verify the gambler loses light theorem

The rules are as follows:

The player and the dealer bet on coins. If heads come up, the player wins this round, and the dealer pays the player one yuan; if tails comes up, the player loses this round, and the player pays the dealer one yuan. Assuming that the player has 10 yuan coins in hand, once the money in hand is lost, he will quit the game.

Python code simulation:

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()

Simulation results:

Simulation 1: 10,000 gamblers, each betting 30 times, the results are as follows:

 Simulation 2: 10,000 gamblers, each betting 100,000 against each other

 in conclusion:

It can be seen from the results of the above-mentioned betting that when each gambler only makes 30 bets, more than half of the gamblers have won money. However, as the number of betting rounds increases, it increases from 30 to After 1000 rounds, more than 70% of gamblers lost all their money. It can be proved that gamblers win money temporarily, and as the number of gambling rounds increases, more and more gamblers will lose all their money.

Guess you like

Origin blog.csdn.net/lyb06/article/details/129888646