← LibraryProbabilistic AlgorithmsEngineering · MathematicsLesson 24/32← PrevNext →
ArticlePublished 6 Aug 2026Updated 5 Aug 20267 min readBy Kevin Jogin
KEVOS® Knowledge Library · Engineering → Mathematics

Engineering/Mathematics/Algorithm engineering

Probabilistic Algorithms

Randomness buys simplicity and speed, and it is paid for with a failure probability that must be quantified rather than assumed away. The discipline is to state which model applies, prove the per-run error bound, and drive it down by repetition to a level below the ambient risk of hardware failure.

  • Design method
  • Computing
  • Error analysis
  • ≈15 min read
  • Precedes primality testing
2−80Typical targetA failure probability far below the chance of an undetected hardware error during the same computation.
Las VegasAlways correctRandomness affects only the running time; the answer is certified.
Monte CarloBounded errorFixed running time, small probability of a wrong answer — reducible by repetition.
1/pExpected trialsRepeating an experiment with success probability p until success.

01

Executive summary

A probabilistic algorithm reads random bits along with its input. Two models matter. A Las Vegas algorithm always returns a correct answer, with running time a random variable. A Monte Carlo algorithm runs in a fixed time but may err with bounded probability.

Randomised algorithms dominate this field for one reason: for many problems the randomised solution is dramatically simpler and faster than any known deterministic one. Miller–Rabin decides compositeness in O(kℓ3) while the deterministic AKS test needs a far higher exponent; polynomial factorization over a finite field is easy with randomness and awkward without it.

Model comparison
ModelCorrectnessRunning timeExample
Las VegasAlways correctRandom; bounded in expectationRandom prime generation; Cantor–Zassenhaus with verification
Monte Carlo, one-sidedErrs in one direction onlyFixedMiller–Rabin: never calls a prime composite
Monte Carlo, two-sidedMay err either wayFixedRandomised approximation and sampling algorithms
DeterministicAlways correctFixedAKS primality; trial division

A Monte Carlo algorithm with an efficiently checkable answer converts into a Las Vegas algorithm by repeating until verification succeeds.

Contents

02

Models and error reduction

Definition D1

One-sided error

An algorithm for a decision problem has one-sided error if a “yes” answer is always correct and a “no” answer errs with probability at most ε — or the reverse. Miller–Rabin has this shape: an output of composite is accompanied by a witness and is certain, whereas probably prime carries the error.

Pr[error after k independent runs] ≤ εkOne-sided error compounds multiplicatively under repetition — the simplest and most powerful error-reduction argument available.
  • Two-sided error needs a majority vote. With per-run error ε < 1/2, taking the majority of k runs reduces error exponentially by a Chernoff bound, but requires more runs than the one-sided case for the same target.
  • Independence is a requirement, not a formality. Reusing the same random bases, or drawing from a weak generator, invalidates the compounding argument entirely. Miller–Rabin with a fixed base set is a deterministic test with different — and adversarially exploitable — properties.
  • Choose the target relative to ambient risk. Beyond about 2−80, the dominant failure mode is no longer the algorithm but undetected hardware error, so further rounds buy nothing measurable.
Contents

03

Generating random values correctly

Uniform selection from a range

Producing a uniform value in [0,n) from a stream of random bits is the most frequently botched primitive in cryptographic code.

Range sampling methods
MethodBiasCostVerdict
r mod n for -bit r, ℓ = len(n)Up to a factor of two on some valuesOne drawUnacceptable
r mod n with ℓ = len(n) + 64Statistical distance ≤ n/2 ≈ 2−64One drawAcceptable for most purposes
Rejection samplingExactly zeroExpected < 2 drawsPreferred where exactness is specified
Lemire's multiply-and-shiftExactly zero with a rare correctionOne multiply, rarely a retryFast and exact for word-size ranges

Rejection sampling: draw ℓ = len(n) bits; if the result is ≥ n, discard and redraw. The acceptance probability exceeds 1/2, so the expected number of draws is below 2.

Sampling structured objects

01

Random prime

Draw random odd candidates, pre-sieve by small primes, apply Miller–Rabin. Expected O(ℓ) candidates by the prime density estimate.

02

Random invertible element

Draw uniformly from [1,n) and check gcd = 1. Success probability φ(n)/n, which is bounded below by roughly 1/(6 ln ln n).

03

Random generator of a group

For a cyclic group of order m with known factorization, draw and test gm/q ≠ 1 for each prime q ∣ m. Success probability φ(m)/m.

04

Random factored integer

Bach's algorithm produces a uniform integer in a range together with its factorization, in expected polynomial time — remarkable, because factoring a given integer is hard.

Why random factored numbers are interesting

Bach's algorithm sidesteps the hardness of factoring by generating the factorization first and the number second, then correcting the distribution by rejection. It is the standard tool for building test instances with known structure and for constructions that need a uniform integer whose factorization is available to the analysis.

Contents

04

Random prime generation as a worked example

  1. Fix the target size and any structural constraints

    For example, a 1024-bit prime with the top two bits set, so that a product of two such primes has exactly 2048 bits.

  2. Draw a random odd candidate

    Use a cryptographic random source; set the top bits to fix the size and the bottom bit to force oddness.

  3. Pre-sieve by small primes

    Reject candidates divisible by primes below a bound around 1000. This removes roughly 80% of them at negligible cost and dominates the practical speed-up.

  4. Apply Miller–Rabin with random bases

    Enough rounds to reach the target error. For random candidates the effective error is far below the worst-case 4−k.

  5. Optionally certify

    Where a proof is required rather than a bound, use a Pocklington certificate when the factorization of p−1 is available, or an ECPP-style primality proof.

  6. Check any extra conditions

    For RSA: gcd(e, p−1) = 1. For discrete-log parameters: q ∣ p−1 for the intended subgroup order.

The random source is the weakest link

Every analysis on this page assumes independent uniform bits. Historical failures — insufficient entropy at boot on embedded devices, a flawed generator in a widely deployed library — produced colliding primes across thousands of independent devices, allowing keys to be recovered by simply taking gcds of published moduli. The mathematics was correct; the sampling was not.

Contents

05

When determinism is required

Deterministic alternatives and their cost
Randomised algorithmDeterministic optionCost of determinism
Miller–RabinAKS primality testPolynomial but far slower in practice
Miller–RabinFixed base set for bounded inputsProven correct only below verified thresholds — a valid and fast choice for 64-bit inputs
Finding a quadratic non-residueTrial search from small valuesPolynomial only under the extended Riemann hypothesis
Cantor–Zassenhaus factorizationBerlekamp with exhaustive searchExponential in the number of factors, or conditional on ERH
Random prime generationSearch upward from a fixed pointNo proven bound on the search length without conjectures on prime gaps

A recurring pattern: the deterministic version exists but its analysis is either conditional on an unproved hypothesis or carries a much worse exponent.

  • Verified thresholds are genuinely useful. For n < 3.3 × 1024, testing the first thirteen prime bases makes Miller–Rabin deterministic and provably correct — exhaustively verified, not conjectural. Small-integer primality should use this rather than random bases.
  • Reproducibility can be obtained without determinism by deriving the random bits from a seed and recording it. This keeps the probabilistic analysis intact while making runs replayable for debugging and audit.
  • Verification often beats derandomisation. If an answer can be checked cheaply, a Monte Carlo algorithm plus a verifier gives a Las Vegas algorithm with certainty and almost no added cost — the right pattern for polynomial factorization and for Wiedemann's algorithm.
Contents

06

Quick reference and FAQ

Design rules
RuleReason
State the model explicitlyLas Vegas and Monte Carlo have incompatible guarantees
Prove a per-run error boundRepetition arguments require a numeric starting point
Use independent randomness per runCompounding fails otherwise
Verify when verification is cheapConverts bounded error into certainty
Sample without biasRejection sampling, or an excess of at least 64 bits
Separate the entropy source from the algorithmThe analysis assumes uniform independent bits and inherits any defect in them
Target error below ambient hardware riskAdditional rounds beyond that point are not measurable
How many Miller–Rabin rounds are actually needed?
For a random candidate of cryptographic size, a handful of rounds already gives an error far below 2−80, because worst-case composites are extraordinarily rare among random inputs. For an adversarially supplied candidate the worst-case bound applies, so 64 rounds is the defensible choice when the input is not under your control.
Is a pseudorandom generator sufficient?
For non-adversarial uses such as randomised algebra, a good statistical generator is fine. For key generation the source must be cryptographically secure and adequately seeded; the failures on record are all failures of seeding rather than of algorithm design.
What does it mean for an algorithm to have expected polynomial time?
The expectation is over the algorithm's internal randomness, for every input — not an average over inputs. That distinction matters: an expected-polynomial-time algorithm has no bad inputs, only unlucky runs, and a run can be restarted.
Can the error probability ever be driven to zero?
For a Las Vegas algorithm it already is — only the running time is uncertain. For a Monte Carlo algorithm with a verifiable answer, verification achieves certainty. Otherwise the error can be made arbitrarily small but not zero.
Contents

08

References and further reading

  • V. Shoup, A Computational Introduction to Number Theory and Algebra, Cambridge University Press, 2005 — Chapter 7.
  • R. Motwani and P. Raghavan, Randomized Algorithms, Cambridge, 1995.
  • E. Bach, 'How to generate factored random numbers', SIAM J. Comput. 17 (1988) 179–193.
  • N. Heninger et al., 'Mining your Ps and Qs: detection of widespread weak keys in network devices', USENIX Security, 2012.

KEVOS® Knowledge LibraryEngineering → MathematicsTaxonomy ID: ENG-MATHPage ID: probabilistic-algorithmsReview cycle: annual


Continue learning

Euclid's Algorithm and Modular ComputationArticle · MathematicsNEXT LESSON →Probabilistic Primality TestingArticle · MathematicsMultiprecision Integer ArithmeticArticle · MathematicsDeterministic Primality Testing: the AKS AlgorithmArticle · Mathematics