← LibraryGenerating a Random Prime Between 2 and MEngineering · MathematicsLesson 117/385← PrevNext →
ArticlePublished 7 Aug 20263 min readBy Kevin Jogin

Engineering  /  Mathematics  — Primality Testing

Generating a Random Prime Between 2 and M

Generating a uniform random prime below a bound, the analysis of the retry loop, and the resulting output distribution.

Page KV-MATH-0389Reading time 3 minReviewed 2026-08-07Author Kevin Jogin

Executive summary

Drawing uniform candidates below a bound and testing each until one is prime yields a prime distributed uniformly over the primes in range.

The expected number of candidates follows from the prime number theorem, and the analysis is the geometric distribution applied to a known success probability.

Learning objectives

  1. State the generation procedure and its expected cost.
  2. Prove the output distribution is uniform.
  3. Handle the interaction between test error and output correctness.

01The procedure

Algorithm

Random prime below M

Inputbound M, round count k
Outputa prime in [2, M], uniform over primes in range
  1. Repeat:
  2.   Draw a uniform integer n from {2, ..., M}.
  3.   Apply trial division by small primes; if composite, continue.
  4.   Apply Miller-Rabin with k rounds; if composite, continue.
  5.   Return n.
  6. Until an iteration cap is reached.
Cost  expected ≈ ln M candidates

The success probability per candidate is π(M)/M ≈ 1/ln M, so the expected number of candidates is about ln M by the geometric distribution.

02Uniformity of the output

Theorem

Output distribution

If candidates are drawn uniformly and independently, and the test is applied identically to each, the returned value is uniform over the primes in the range.

Reason. Conditioned on acceptance, every prime was equally likely to have been the drawn candidate, and acceptance depends only on primality.

Uniformity matters where a security argument assumes it. For RSA the requirement is weak, but for schemes whose proofs quantify over uniformly chosen primes, a biased generator invalidates the reduction.

03Test error and output correctness

The output is prime only up to the error probability of the primality test. Two error sources compound and are worth separating.

Failure sources
SourceProbabilityConsequence
Miller-Rabin false positive≤ 4^{−k} per accepted candidateA composite is returned as prime
Cap exhausted≤ e^{−c}Failure reported; caller retries
Entropy failureNot quantifiableCorrelated or repeated outputs; catastrophic

Only the first affects correctness of a returned value. The second is a clean failure the caller can handle, and the third is outside the probabilistic model entirely — which is why entropy quality must be established by other means rather than folded into the analysis.

04Frequently asked questions

Does the trial division filter affect uniformity?

No, because it rejects only composites. Any filter whose acceptance depends solely on primality preserves uniformity over the primes; a filter that rejected some primes would not.

Why draw from {2,...,M} rather than a bit-length range?

This form is the clean case for analysis. Practical generation fixes a bit length instead, which restricts the range to [2^{k−1}, 2^k) and changes the density slightly without altering the argument.

How large should the iteration cap be?

A small multiple of ln M — a hundred times the expected count makes spurious failure negligible while still catching a broken entropy source promptly.

Sources and method

Structural reference: Victor Shoup, A Computational Introduction to Number Theory and Algebra, Version 1, Cambridge University Press, 2005 — book pages 252-255.

This page carries the durable method layer only: definitions, constructions, algorithms, complexity results and selection criteria, authored originally for KEVOS. No text is transcribed or paraphrased from the source, and no numeric tables or benchmark data are reproduced — these are routed to live authoritative sources instead.

Author: Kevin Jogin. Last reviewed 2026-08-07.

Continue learning

The Miller-Rabin Primality TestArticle · MathematicsNEXT LESSON →Trial Division up to a Small BoundArticle · MathematicsThe Fermat Test and Carmichael NumbersArticle · MathematicsGenerating a Random k-Bit Prime with Miller-RabinArticle · Mathematics