Chance of a virtual horse winning a virtual race Every second, each horse has $X$ chance of moving one step.
$H$ is a list of the chances of each horse moving:   
$H = [X_1, X_2, X_3,...,X_n]$  
The race is won when any horse reaches its 20th step.  
I need an algorithm/formula (if written in code then preferably in Python 3) to determine from the values in $H$, the chances of each horse winning the race.
Thanks :)
 A: Consider each opportunity to move (i.e. each second) a "trial". The trial is considered a success if the horse moves and a failure if it doesn't. The probability of success for each horse is constant. 
While not specified in the question, I assume these random movements occur independently both across horses and trials.
Under those assumptions we're dealing with Bernoulli trials for each horse.
The number of seconds it takes a horse to finish is 20 + the number of "failures". 
So we're dealing with each horse's "time" in seconds (trials) being a negative binomial random variate, each independent with its own "$p$" parameter (which I denote $\pi$ hereafter since I use $p$ to represent a pmf).
Note that we can as readily consider which horse has the fewest failures to reach the 20th success (since that's exactly 20 fewer than the total number of trials; this is the other common form of the negative binomial).
Imagine no ties were possible. Then you win on turn $t$ if you hit the 20th success on that turn and all your opponents don't have 20 yet. 
For concreteness, let's have 3 horses, A, B and C, with probabilities $\pi_A=0.2$, 
$\pi_B=0.3$, 
$\pi_C=0.4$.
Let the negative binomial pdf be $p_{n,\pi_i}(x)$ and the cdf be $F_{n,\pi_i}(x)$ (where here we're using the $n$="number of failures" version).

(A is in blue, B is in green, C is in red)
So the probability that $C$ wins after failing exactly $i$ times is 
$P(C\text{ reaches }20) \cdot P(A\text{ hasn't reached }20) \cdot P(B\text{ hasn't reached }20)$, or
$P(C_i)=p_{20,0.4}(i) \cdot(1-F_{20,0.3}(i)) \cdot(1-F_{20,0.2}(i))$
Now the issue with ties complicates the calculation slightly; let's consider $A$ winning. $A$ has the higher probability of success so loses ties; in that case 
$P(A_i)=p_{20,0.2}(i) \cdot(1-F_{20,0.3}(i-1)) \cdot(1-F_{20,0.4}(i-1))$
Similarly $B$ wins ties against $A$ but not $C$
$P(B_i)=p_{20,0.3}(i) \cdot(1-F_{20,0.2}(i-1)) \cdot(1-F_{20,0.4}(i))$
If we work these out (here in R):
 x=1:150
 pA = sum(dnbinom(x,20,.2)*pnbinom(x-1,20,.4,lower.tail=FALSE)
                          *pnbinom(x-1,20,.3,lower.tail=FALSE))
 pB = sum(dnbinom(x,20,.3)*pnbinom(x-1,20,.4,lower.tail=FALSE)
                          *pnbinom(x,20,.2,lower.tail=FALSE))
 pC = sum(dnbinom(x,20,.4)*pnbinom(x,20,.3,lower.tail=FALSE)
                          *pnbinom(x,20,.2,lower.tail=FALSE))
data.frame(pA=pA,pB=pB,pC=pC)
      pA        pB        pC
1 0.00368617 0.1386545 0.8576593
>     # Check we extended `x` far enough:
>     pA+pB+pC
[1] 1  

So that seems right. You'd check this work by simulation.
This generalizes to more horses, by having more $1-F$ terms in each product, where the $i$ is replaced by $i-1$ any time it's comparing to a horse you lose ties with ($i$ is x in the R code above).
