Two ways an algorithm can use randomness, distinguished by what is uncertain.

Las VegasMonte Carlo
Outputalways correctmay be wrong
Running timerandom variabledeterministic (or bounded)
Guarantee”correct, probably fast""fast, probably correct”
Failure moderuns longwrong answer
Complexity classZPPBPP (two-sided), RP / co-RP (one-sided)

Las Vegas examples

AlgorithmRandom time because
Randomised quicksortpivot quality varies; expected
nth_element (introselect)same; expected
Pollard’s rhothe walk length is random
Welzl’s enclosing circleinsertion order; expected
Seidel’s LPconstraint order
Treap operationstree shape; expected
Randomised incremental Delaunayinsertion order
Hash table with rehashingcollision resolution

The answer is never wrong — only the clock is uncertain. That makes Las Vegas algorithms safe to submit: the risk is TLE, not WA, and the probability of a bad run is astronomically small.

Monte Carlo examples

AlgorithmError type
Miller-Rabinone-sided — “composite” is always right
Karger’s min cutone-sided — may return a larger cut
Freivalds’ checkone-sided — “not equal” is always right
String hashing equalityone-sided — “different” is always right
Tutte matrix matching testone-sided
Monte Carlo integrationtwo-sided, with an error bar
MCTStwo-sided
Simulated annealingno guarantee at all

One-sided error is the useful kind

When one of the two answers is always correct, repetition drives the error to zero exponentially:

Miller-Rabin’s “composite” verdict is a proof; only “probably prime” carries risk. Hashing’s “hashes differ” is a proof; only “hashes equal” can be a collision. Recognising which side is certain tells you where to add verification.

Converting between them

Las Vegas → Monte Carlo. Run for a fixed time budget; if it has not finished, output anything. Correct whenever it terminates in time.

Monte Carlo → Las Vegas. Only possible when the answer is verifiable. Repeat until verification passes:

Pollard’s rho is exactly this — a randomised walk (Monte Carlo in spirit) whose output is checked by division, making the whole thing Las Vegas.

Choosing an error probability

With independent repetitions and per-run error :

Total error
20
60
30
(Miller-Rabin per base)30

A rule of thumb: aim for an error below per submission. That is far below the probability of a hardware fault, and it costs only a handful of extra iterations.

In competitive programming

SituationSafe?
Las Vegas (randomised quicksort, treap)yes — always correct
Randomised hashing with a time-seeded baseyes — collision probability
Fixed-seed hashingno — hackable
Miller-Rabin with deterministic basesyes — provably correct for 64-bit
Monte Carlo with erroryes in practice
Simulated annealing on an exact-answer problemno — no guarantee
Randomised algorithm on an interactive problemcheck the statement; some forbid it

The complexity classes

ClassMeaning
ZPPLas Vegas, expected polynomial time
RPMonte Carlo, one-sided error (no false “yes”)
co-RPone-sided the other way
BPPtwo-sided error
PPerror (much weaker; contains NP)

. Whether is open, but widely believed true — most experts expect randomness does not add computational power, only convenience. See Complexity Theory.

See also: Randomized Algorithms · Complexity Theory · Birthday Paradox