返回题库

骰子游戏 1

Dice Game 1

专题
Probability / 概率
难度
L3

题目详情

两名玩家轮流掷两个六面骰子。玩家 A 先走,然后是玩家 B。如果玩家 A 掷出的总和为 6,则他们获胜。如果玩家 B 掷出的总点数为 7,则他们获胜。如果两者都没有达到预期的值,则游戏将继续,直到有人获胜。玩家 A 获胜的概率是多少?

Two players take turns rolling two six-sided dice. Player A goes first, followed by player B. If player A rolls a sum of 6, they win. If player B rolls a sum of 7, they win. If neither rolls their desired value, the game continues until someone wins. What is the probability that player A wins?

解析

首先,我们首先考虑用两个骰子掷出总和为 6 的概率以及用两个骰子掷出总和为 7 的概率。

我们可以通过以下方式掷出 6:1&5、5&1、2&4、4&2、3&3。由于掷两个骰子的方式共有 36 种,因此掷出总和为 6 的概率为 536\frac{5}{36}。我们可以对 7 的和进行同样的操作。 7 可以通过以下方式进行滚动:1&6、6&1、2&5、5&2、3&4、4&3。这给了我们滚动总和为 7 作为 636=16\frac{6}{36} = \frac{1}{6} 的概率。

根据此信息,你可能倾向于相信玩家 B 将赢得游戏,因为掷出 7 的概率大于掷出 6 的概率。但是,你必须记住此问题的一个重要部分 - 玩家 A 先走。因此,我们想要研究先行的优势是否抵消了与滚动总和为 6 相关的较低概率。

玩家A可以通过以下方式赢得比赛:

  1. 玩家 A 第一次掷骰子的总和为 6。
  2. 玩家 A 在第一次掷骰时未能掷出 6,然后玩家 B 未能掷出 7,然后玩家 A 掷出 6。

请注意,只要玩家 A 在玩家 B 掷出 7 之前最终掷出 6,模式 #2 就可以无限期地继续下去。本质上,存在一系列事件,会发生一些 kk 次数的失败,直到我们遇到玩家 A 掷出 6 的第一次成功。我们刚才描述的过程可以通过几何分布进行建模。

pp 为用两个骰子掷出总和为 6 的概率,设 rr 为用两个骰子掷出总和为 7 的概率。那么我们可以将事件的概率表示为:(1p)k(1r)kp(1-p)^k(1-r)^kp

其中 k=0,1,2,3....k = 0,1,2,3....。我们可以将这些概率相加,得到玩家 A 获胜的概率:

k=0inf(1p)k(1r)kp=p1(1p)(1r)=pp+rpr\sum_{k=0}^{\inf}(1-p)^k(1-r)^kp = \frac{p}{1-(1-p)(1-r)} = \frac{p}{p+r-pr}

由于 p=536p=\frac{5}{36}r=16r=\frac{1}{6}(我们在上面计算了这些值),这等于 30610.49\frac{30}{61} \approx 0.49。因此,本场比赛只是稍微对玩家B有利。

这是我们刚刚观察到的结果的 python 模拟:

import random
from typing import Callable

random.seed(42)

roll_dice: Callable[[None], int] = lambda: random.randrange(1, 7, 1)

def play_game() -> bool:
    """Returns a boolean indicating whether player A won"""
    pa_turn = True
    while True:
        dice_roll_sum = roll_dice() + roll_dice()
        if pa_turn and dice_roll_sum == 6:
            return True
        if not pa_turn and dice_roll_sum == 7:
            return False
        pa_turn = not pa_turn

iterations = 10_000
pa_win_prob = sum([play_game() for _ in range(iterations)]) / iterations
print(f"The probability of Player A winning is {pa_win_prob}")

Original Explanation

To begin, let's start by considering the probability of rolling a sum of 6 with two dice and the probability of rolling a sum of 7 with two dice.

We can roll a six in the following ways: 1&5, 5&1, 2&4, 4&2, 3&3. Since there are 36 total ways to roll two dice, the probability of rolling a sum of 6 is 536\frac{5}{36}. We can do the same for rolling a sum of 7. A seven can be rolled in the following ways: 1&6, 6&1, 2&5, 5&2, 3&4, 4&3. This gives us the probability of rolling a sum of 7 as 636=16\frac{6}{36} = \frac{1}{6}.

Based on this information, you may be inclined to believe that player B will win the game because the probability of rolling a 7 is larger than the probability of rolling a 6. However, there's one important part of this problem that you must remember - player A goes first. Therefore, we want to investigate whether the advantage of going first offsets the lower probability associated with rolling a sum of 6.

Player A can win the game in the following ways:

  1. Player A rolls a sum of 6 on their first roll.
  2. Player A fails to roll a sum of 6 on their first roll, then player B fails to roll a sum of 7, and then player A rolls a sum of 6.

Note that pattern #2 can continue indefinitely as long as player A eventually rolls a sum of 6 before player B rolls a sum of 7. In essence, there is a sequence of events that take place with some kk number of failures until we encouter the first success of player A rolling a 6. The process we just described can be modeled by the geometric distribution.

Let pp be the probability of rolling a sum of 6 with two dice and let rr be the probability of rolling a sum of 7 with two dice. Then we can express the probability of the event as: (1p)k(1r)kp(1-p)^k(1-r)^kp

where k=0,1,2,3....k = 0,1,2,3..... We can sum these probabilities to get the probability that player A wins:

k=0inf(1p)k(1r)kp=p1(1p)(1r)=pp+rpr\sum_{k=0}^{\inf}(1-p)^k(1-r)^kp = \frac{p}{1-(1-p)(1-r)} = \frac{p}{p+r-pr}

Since p=536p=\frac{5}{36} and r=16r=\frac{1}{6} (we calculated these values above), this equals 30610.49\frac{30}{61} \approx 0.49. Therefore, this game just slightly favors player B.

Here is a python simulation of the result we just observed:

import random
from typing import Callable

random.seed(42)

roll_dice: Callable[[None], int] = lambda: random.randrange(1, 7, 1)

def play_game() -> bool:
    """Returns a boolean indicating whether player A won"""
    pa_turn = True
    while True:
        dice_roll_sum = roll_dice() + roll_dice()
        if pa_turn and dice_roll_sum == 6:
            return True
        if not pa_turn and dice_roll_sum == 7:
            return False
        pa_turn = not pa_turn

iterations = 10_000
pa_win_prob = sum([play_game() for _ in range(iterations)]) / iterations
print(f"The probability of Player A winning is {pa_win_prob}")