Skip to content
DeepForgeby svx
All problems
EasyProbabilitypr-385

Gambler Ruin Absorbing Probability

Problem

Starting with i units, a gambler wins one with probability p and loses one otherwise until reaching 0 or n. The probability of reaching n is (1 - r^i) / (1 - r^n) with r = (1 - p) / p when p != 0.5, and i / n when p = 0.5.

Given i, n, and p, return the probability of reaching n.

Hint

Solve the second-order difference equation with boundary values.

Test cases

case 1
in: [1,5,0.5]
expected: 0.2
case 2
in: [2,4,0.4]
expected: 0.30769230769230765
case 3
in: [1,10,0.6]
expected: 0.3392158552348125
Starter codePython
def gambler_ruin_absorbing_probability(i, n, p):
    # Your code here
    pass

3 test cases · run your code against them in the editor