# Expected number of coin flips until a random number of consecutive heads

You are given a coin, which when tossed lands on H (heads) with probability $p$ and T (tails) with probability $1-p$. What is the expected number of flips that you must do before you get $L$ consecutive H?

I am aware how to solve the problem when $L$ is fixed. Let $X$ denote the number of needed flips. Then the answer is $E[X] = \frac{1 - p^L}{p^L(1 - p)}$ (an example derivation is shown here).

What about when $L$ is a random variable with a known distribution? I tried applying the same method as when $L$ is fixed but I can't wrap my head around it because in that case the probability for scoring $L$ consecutive heads is namely $p^L$ which is also a random variable. Do I need simply to work with $E[L]$ when calculating $E[X]$? And if yes, then why?

Assuming the $L$ takes the values $1,2,3,\dots$ with probabilities $r_1,r_2, r_3,\dots$ and that $L$ and the coin tossing are independent, we have $$\Bbb E X = \sum_{n\ge 1} \Bbb E[X|L=n]\cdot\Bbb P(L=n) = \sum_{n\ge 1} \frac{1-p^n}{p^n(1-p)}\cdot r_n \ .$$
You can apply the general rule:$$\mathbb EX=\mathbb E[\mathbb E[X\mid L]]$$