Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 · Random Walks and Brownian Motion

Gambler's Ruin

A gambler plays until broke or at a target. Derive ruin probabilities and expected game length for fair and unfair games, and check them by simulation.

A gambler starts with i coins and plays rounds of a game. In each round they win one coin with probability p and lose one coin with probability q = 1 - p. They stop when they reach a target of N coins or when they have nothing left. This is the gambler's ruin problem. It is a random walk with two absorbing barriers, at 0 and at N, and it appears again and again: in insurance (ruin of a company), genetics (fixation of a gene) and physics (particles hitting a wall).

Two questions drive the lesson:

  1. What is the probability hᵢ of reaching N before 0, starting from i?
  2. How many rounds dᵢ does the game last on average?

Setting up the equations

Condition on the first round. From i (with 0 < i < N) the walk goes to i+1 with probability p or to i-1 with probability q:

Text
h_i = p h_{i+1} + q h_{i-1},    h_0 = 0,  h_N = 1

For the duration, one round has been played, so

Text
d_i = 1 + p d_{i+1} + q d_{i-1},    d_0 = d_N = 0

These are linear difference equations with boundary conditions.

Solutions

Fair game (p = 1/2). The solution of the first equation is linear:

Text
h_i = i / N          d_i = i (N - i)

Starting with 3 coins and aiming for 10, the chance of success is 0.3, and the average game lasts 3 × 7 = 21 rounds.

Unfair game (p ≠ 1/2). Let r = q/p. Then

Text
h_i = (1 - r^i) / (1 - r^N)
d_i = i / (q - p)  -  (N / (q - p)) · h_i

The factor r^i captures how a bias snowballs. With p = 0.45 (a small house edge) and N = 100, a gambler starting at 50 has r = 0.55/0.45 ≈ 1.222. Here h₅₀ = 1/(r^50 + 1), and r^50 is about 23,000, so the chance of doubling the money is roughly 0.00004. A small bias is deadly over long play.

This is a model of a bet with a fixed stake, not advice about real gambling. In real casino games the house edge ensures that long-run play loses money on average.

Checking the formulas

Python Formulas versus simulation

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each pair of columns agrees to within sampling error.

A linear-algebra view

Gambler's ruin is an absorbing Markov chain. Order the transient states 1, …, N-1 first. Let T be the matrix of transition probabilities among transient states and R the matrix from transient to absorbing states (0 and N). Then:

  • The fundamental matrix F = (I - T)^(-1) has entry F[i,j] equal to the expected number of visits to j starting from i.
  • Expected duration: d = F · 1 (row sums).
  • Absorption probabilities: B = F R.

This works for any absorbing chain, not just walks on a line, so it is worth knowing.

Python Solve gambler's ruin as an absorbing chain

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The matrix method reproduces the closed forms to all printed digits, and it extends to messy cases where no closed form exists (for example, different win probabilities in different states).

Limiting cases

  • If N → ∞ and p > 1/2: the probability of never being ruined is 1 - (q/p)^i, positive.
  • If N → ∞ and p ≤ 1/2: ruin is certain (probability 1), even in a fair game. A fair game against an infinitely rich opponent is a sure loss in the long run.
flowchart LR
  A["0 (ruin)"] --- B["1"] --- C["2"] --- D["..."] --- E["N-1"] --- F["N (target)"]

States of the gambler's ruin chain; the two end states absorb.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. In a fair game starting with 4 coins and a target of 20, what is the probability of reaching the target?

    h_i = i/N = 4/20 = 0.2.
  2. 2. In a fair game with i = 4 and N = 20, what is the expected number of rounds?

    d_i = i (N - i) = 4 × 16 = 64.
  3. 3. For an unfair game, what quantity r appears in the success probability?

    h_i = (1 - r^i)/(1 - r^N) with r = q/p.
  4. 4. Against an infinitely rich opponent in a fair game, what is the probability the gambler is eventually ruined?

    As N goes to infinity in the fair case, h_i = i/N goes to 0, so ruin is certain.

Summary

  • Gambler's ruin is a random walk with absorbing barriers at 0 and N.
  • Fair game: hᵢ = i/N, dᵢ = i(N − i). Unfair: hᵢ = (1 − rⁱ)/(1 − r^N) with r = q/p.
  • A tiny bias compounds over many rounds.
  • The fundamental matrix (I − T)⁻¹ solves any absorbing chain.

Next: The Reflection Principle and the Maximum