On this page
Put a coin toss inside an algorithm and you must choose what to gamble with: the running time, or the answer.
What is the difference between Las Vegas and Monte Carlo algorithms?
A Las Vegas algorithm is a randomised algorithm that always returns a correct answer (or reports that it failed), while its running time is a random variable (Las Vegas algorithm, Wikipedia).
A Monte Carlo algorithm uses bounded resources but may return a wrong answer with some, usually small, probability (Monte Carlo algorithm, Wikipedia).
László Babai coined the name Las Vegas in 1979, in work on graph isomorphism, as the dual of Monte Carlo (same Las Vegas article). The short version: Las Vegas gambles with time, Monte Carlo gambles with correctness.
Monte Carlo errors are often one-sided. A false-biased algorithm is always right when it says "false" and only its "true" answers can be wrong (Monte Carlo algorithm, Wikipedia). The Miller–Rabin test works like this: "composite" is certain, "probably prime" is not.
Why is randomised quicksort a Las Vegas algorithm?
Randomised quicksort picks a random pivot, so the output is always sorted and only the running time depends on luck. Its worst case is still Θ(n²), reached when the pivot is the smallest or largest element every time (Las Vegas algorithm, Wikipedia).
With a uniformly random pivot the expected number of comparisons for large n is about 2n ln n, roughly 1.39 n log₂ n, and the expectation is over the algorithm's own coin flips, so it holds for every input, including already sorted ones (Quicksort, Wikipedia). Tony Hoare published Quicksort in The Computer Journal in 1962 (Hoare, 1962). That expected cost sits close to the floor explained in why comparison sorting needs n log n comparisons.
How does Freivalds' algorithm check a matrix product?
Someone hands you three n × n matrices and claims AB = C. Freivalds' algorithm picks a random vector r of 0s and 1s, computes A(Br) and Cr, and says "equal" only if the two vectors match; this takes O(n²) time (Freivalds' algorithm, Wikipedia). If AB = C it always says yes; if AB ≠ C it says yes with probability at most 1/2 (same source).
A worked example (semicolons separate rows):
Take A = [1 2; 3 4] and B = [5 6; 7 8]. The claimed product is C = [19 22; 43 51].
Try r = (1, 1). Then Br = (11, 15) and A(Br) = (41, 93), but Cr = (41, 94). The vectors differ, so C is certainly wrong.
Try r = (1, 0) instead. Then Br = (5, 7), A(Br) = (19, 43) and Cr = (19, 43). They match, so this round is fooled.
See why: AB − C = [0 0; 0 −1], so (AB − C)r = (0, −r₂). It is zero exactly when r₂ = 0, which happens for 2 of the 4 possible vectors: a fooling chance of exactly 1/2, the worst the bound allows. A "no" is a proof; a "yes" is only evidence.
How does repetition make a Monte Carlo algorithm reliable?
Run independent rounds and say "equal" only if every round agrees. For Freivalds' check, k rounds cost O(kn²) time and leave an error probability of at most 2⁻ᵏ (Freivalds' algorithm, Wikipedia). With 20 rounds that is 2⁻²⁰ = 1/1,048,576, under one in a million.
Miller–Rabin does better per round. For an odd composite n, at most 1/4 of the bases are strong liars, so k rounds declare a composite "probably prime" with probability at most 4⁻ᵏ (Miller–Rabin test, Wikipedia). Michael Rabin published this probabilistic version in 1980 (Rabin, Journal of Number Theory90084-0)). For the steps with real numbers, see the Miller–Rabin test worked by hand.
When both answers can be wrong, take the majority vote of k runs instead (Monte Carlo algorithm, Wikipedia).
Can you convert a Las Vegas algorithm into a Monte Carlo one?
Yes, and it is the easy direction. Run the Las Vegas algorithm with a time limit and give up (or guess) if it has not finished; Markov's inequality bounds the chance of running over (Las Vegas algorithm, Wikipedia). If the expected time is T and you stop at 2T, Markov's inequality gives a failure chance of at most T/2T = 1/2.
The reverse needs a way to check answers: rerun the Monte Carlo algorithm until a checked answer appears (Monte Carlo algorithm, Wikipedia). With success chance p per run, you expect 1/p runs. Freivalds' check gains little here: the obvious certain check of a "yes" is computing AB, the work it avoids.
A quick checklist:
Is every answer it returns correct? Then it is Las Vegas.
Is the running time bounded on every run, but its answer can be wrong? Then it is Monte Carlo.
Is one of its answers always certain? Amplify by repeating and requiring agreement.
Can you check an answer cheaply? Then you can turn Monte Carlo into Las Vegas.
If Markov's inequality or the geometric distribution feels shaky, the Fermi Concept Library gives each concept its own lesson, and the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning.



