Las Vegas vs Monte Carlo
Most algorithms follow a fixed recipe: given the same input, they always take the exact same steps. But some of the most elegant algorithms in computer science flip coins along the way. They make random decisions — and somehow, they still work correctly (or almost always work correctly) and are blazing fast.
These are randomized algorithms. Randomness isn't a flaw — it's the whole point. It's used to get simpler code, avoid adversarial inputs, or achieve average-case speeds that deterministic algorithms can't match.
Two Flavors: Las Vegas and Monte Carlo
There are two camps of randomized algorithms, named after casinos (the connection: both involve gambling, but in different ways).
A Las Vegas algorithm always produces the correct answer. The only thing that varies is how long it takes. Think of it like a slot machine that always pays out eventually — you just don't know when. Randomized QuickSort is the classic example: no matter what, the output is a sorted array. Only the runtime depends on the random pivot choices.
A Monte Carlo algorithm always finishes in a fixed amount of time but might occasionally give a wrong answer. The probability of being wrong is tiny and controllable. The Miller-Rabin primality test is canonical: run it k times, and the probability of a false positive drops to 4^(-k). At k=40, that's 1 in a trillion — safe for any practical purpose.
Why Not Just Use Deterministic Algorithms?
Two big reasons:
- Adversarial inputs: A deterministic algorithm has a fixed worst case that an attacker can engineer. If your hash table always uses the same hash function, an adversary can craft inputs where everything collides, degrading every operation to \(O(n)\). A randomized hash function makes this essentially impossible — the attacker doesn't know which function you'll pick.
- Simplicity: Many randomized algorithms are dramatically simpler to implement and analyze than their deterministic counterparts with equivalent guarantees. Randomized QuickSort is 5 lines; Median-of-Medians (deterministic \(O(n \log n)\) guaranteed sort) is dozens.
Probability Amplification
Monte Carlo algorithms become arbitrarily reliable through repetition. If one run gives a wrong answer with probability 1/4, running it k times independently and taking the majority vote drops the error probability to exponentially small in k. This is probability amplification — a powerful general technique.