Visual walkthrough — Randomized algorithms — Las Vegas, Monte Carlo
We build up ideas from Probability and Expectation, and at the end connect to how this powers QuickSort and Quickselect.
Step 1 — What is one "attempt"?
WHAT. Our Las Vegas retry loop does the same thing over and over: make one random attempt. Each attempt either succeeds or fails. Nothing in between.
WHY. Before we can count attempts, we must agree on the atom we are counting. The atom is a single attempt, and it has exactly two outcomes. We will draw an attempt as a coin flip: a coin that lands "success" some fraction of the time and "fail" the rest.
PICTURE. The coin below is biased: the green "SUCCESS" slice covers a fraction of the coin, the red "FAIL" slice covers the leftover .

Step 2 — What does the loop actually look like over time?
WHAT. The algorithm keeps flipping this coin. On the first "success" it STOPS and returns the (always correct) answer. We call the number of flips until — and including — that first success.
WHY. is the thing the parent note calls "running time in trials". It is random: on a lucky day ; on an unlucky day the coin keeps landing red and grows. We want the average value of over many imaginary runs.
PICTURE. Three example runs. Run A got success immediately (). Run B failed once then succeeded (). Run C failed three times then succeeded (). Notice: every run eventually ends in one green flip, and the reds pile up before it.

Step 3 — The probability of each possible run
WHAT. Let us list how likely each value of is. To get , the very first flip is green: probability . To get , flip 1 is red then flip 2 is green: probability . To get : red, red, green . And so on.
WHY. Flips are independent — one flip's result never changes the next flip's odds (the coin has no memory). For independent events we multiply their probabilities. Each extra failure tacks on another factor of .
PICTURE. The tree branches red (fail) on the left, green (success) on the right. Each leaf is a value of ; the probability written under it is the product of the branch fractions you walked through to reach it.

Step 4 — The memoryless trick (the key idea)
WHAT. Instead of summing an infinite series, we use a shortcut. Look at what happens on the very first flip and split into two worlds:
- Green world (probability ): we won on flip 1. Total flips used . Done.
- Red world (probability ): we wasted flip 1. But now — and this is the magic — we are standing at exactly the same starting line as before. The coin has no memory, so the expected number of additional flips from here is the very same we are trying to find.
WHY. This is called memorylessness: after a failure the future looks identical to the beginning. That lets refer to itself, turning an infinite process into one small equation.
PICTURE. The green branch ends the game at cost . The red branch pays cost and then loops back into a fresh copy of the whole problem — drawn as an arrow curling back to "start".

Step 5 — Solving the equation
WHAT. Now it is pure algebra. Let to keep it short.
WHY. We isolate to get a closed formula — a plain number we can plug into.
Multiply out the right side. The becomes :
The two lonely fractions add to (Step 1's identity, ):
Now gather every on the left. Subtract from both sides:
Factor out of the left. Since :
Divide both sides by :
PICTURE. The curve : as the green slice shrinks toward , the expected number of tries rockets to infinity; as (an almost-always-winning coin), it flattens toward try.

Step 6 — Sanity checks and the edge cases
WHAT. A formula you cannot test is a formula you cannot trust. Let us hit every corner.
WHY. The contract: the reader must never meet a case we did not show. So we walk , , the extreme , and the degenerate .
PICTURE. A little table-plot: for each we mark as a dot, and shade the danger zone where approaches .

| success prob | expected tries | reading |
|---|---|---|
| always wins first try — no retries ever | ||
| fair coin — average of two flips | ||
| a one-in-four coin needs four tries on average | ||
| rare success — long waits | ||
| expectation blows up | ||
| (degenerate) | undefined | never succeeds — loop runs forever |
Recall Check the boundary
A coin that is all green wins on flip 1 every single time, so always, and its average is ::: — the formula agrees.
The one-picture summary
Everything on one canvas: the biased coin (Step 1) feeds the branch (Step 4); the green branch stops at cost , the red branch loops back; that loop is the equation , which collapses to the curve .

Recall Feynman retelling — say it like you'd explain to a friend
Imagine a slot machine that pays out on a green flip, which happens a fraction of the time. You keep pulling until you win. How many pulls on average? Here's the trick: on your first pull, one of two things happens. Either you win right away (chance , cost one pull) — or you lose (chance ), and now you're standing in front of the exact same machine as when you started, because the machine doesn't remember your last pull. So the average number of pulls equals "one pull, plus (if you lost) the same average all over again." Write that sentence as math, it says , and untangling it gives . Fair coin? Two pulls. One-in-a-hundred coin? A hundred pulls. That single line, , is the heartbeat of every Las Vegas algorithm: the answer is always right, and this is how long you wait for it.
Recall One-line recap
Why does ? ::: Because after every failure the situation resets identically (memoryless), giving , which solves to .