# Probability distribution of k consecutive successes with n maximum trials

Let $X$ be a random variable that represents the number of trials of a given experiment. The outcome of a single trial is a Bernoulli random variable, with probability of success $p$, and trials are independent.

The maximum number of trials is $n$, but if there are $k<n$ consecutive successes the experiment ends.

What is the probability distribution?

Let $a_n$ be the required probability.
Then $a_{n+1} = a_n+(1-a_{n-k})p^k(1-p)$
Where $a_0=a_1=a_2=...=a_{k-1}=0,a_k=p^k$