Random Walks in 1 Dimension

A random walk in 1 dimension is the classic Gambler's Ruin problem.

Suppose that we have a Gambler with n dollars, and his opponent has m dollars. They flip a coin with probability p of heads. The game ends after one player has all the money. What is the probability that Gambler 1 wins?

If p = 0.5

p(G1 wins) = n / (n+m)

Otherwise (let q = 1-p)

p(G1 wins) = [1-(q/p)^n]/[1-(q/p)^(n+m)]

Java
double a = 0 ; double b = 0 ; if (p == 0.5) {    a = H1 ; b = H2 + H1 ; } if (p != 0.5) {    a = 1 - Math.pow((1-p)/p, H1) ; b = 1 - Math.pow((1-p)/p, H1 + H2) ; } double ANSWER = a/b ;

UVA Problems
11500 Vampires