Skip to content

Trials until first success

Problem

Question

On the average, how many times must a die be thrown until one gets a 6?

平均需要掷骰子多少次才能得到一个6点?

Tip

Tip

Geometric series

几何级数

Solutions

Solution1: Analysis

Solution1: Analysis

Let's denote the number of tosses required as T. We can determine the probability distribution of T and then calculate the expected value E(T) based on this distribution.

T Probability
1 16
2 16(56)
3 16(56)2
... ...
t 16(56)t1

Therefore, we have

E(T)=t=1t16(56)t1

From

56E(T)=t=1t16(56)t

we can deduce that

E(T)56E(T)=16E(T)=16(1+i=1(56)i)=1

Hence,

E(T)=6

记问题需要的投掷次数为T,那么可以得到如下T的概率分布, 我们可以根据此概率分布求出最终的E(T)

T Probability
1 16
2 16(56)
3 16(56)2
... ...
t 16(56)t1

所以有

E(T)=t=1t16(56)t1

56E(T)=t=1t16(56)t

E(T)56E(T)=16E(T)=16(1+i=1(56)i)=1
E(T)=6

Solution2: Elegant Analysis

Solution2: Elegant Analysis

This extremely elegant problem-solving idea comes from the reference answers in the book.

When the first toss is a failure, the mean number of tosses required is 1+E(T), and when the first toss is a success, the mean number is 1. Then

E(T)=56(1+E(T))+161
E(T)=6

这个极其优雅的解题思路来自书的参考答案。

当第一次投掷失败时,所需的平均投掷次数为1+E(T); 当第一次投掷成功时,所需的平均投掷次数为1。因此,

E(T)=56(1+E(T))+161
E(T)=6

Solution3: Simulation

Solution3: Simulation

We can approximate the average number of times it takes to roll a dice to get a 6 by simulating the process of rolling a dice.

import random


def roll() -> int:
    return random.randint(1, 6)


def roll_6_count() -> int:
    count = 1
    r = roll()
    while r != 6:
        r = roll()
        count += 1
    return count


def simulation(run_nums: int = 100000) -> None:
    roll_counts = [roll_6_count() for _ in range(run_nums)]
    print(f"Expectation times to got a 6: {sum(roll_counts) / run_nums}")


simulation()

Expectation times to got a 6: 6.02792

可以通过模拟掷骰子的过程来近似得到平均需要掷骰子多少次才能得到一个6点。

import random


def roll() -> int:
    return random.randint(1, 6)


def roll_6_count() -> int:
    count = 1
    r = roll()
    while r != 6:
        r = roll()
        count += 1
    return count


def simulation(run_nums: int = 100000) -> None:
    roll_counts = [roll_6_count() for _ in range(run_nums)]
    print(f"Expectation times to got a 6: {sum(roll_counts) / run_nums}")


simulation()

Expectation times to got a 6: 5.98562