Prove a random generated number is uniform distributed
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
You cannot mathematically "prove" a random number generator (RNG) produces a uniform distribution from its output alone — you can only gather statistical evidence that it is consistent with uniformity. The standard approach is to generate a large sample and apply statistical tests (chi-square, Kolmogorov-Smirnov, or visual methods) that measure how closely the observed distribution matches the expected uniform distribution.
What Is a Uniform Distribution?
A uniform distribution over the interval [a, b] means every value in that range is equally likely. For a discrete uniform distribution over integers 1 to k, each value has probability 1/k.
Key properties:
- PDF (continuous): f(x) = 1/(b-a) for x in [a, b], 0 otherwise
- Mean: (a + b) / 2
- Variance: (b - a)² / 12
Test 1: Chi-Square Goodness of Fit
The chi-square test is the standard method for testing discrete uniform distributions. It compares observed frequencies against expected frequencies:
The chi-square statistic measures the total squared deviation between observed and expected counts, normalized by expected counts:
χ² = Σ (Oᵢ - Eᵢ)² / Eᵢ
A small χ² (high p-value) means the data is consistent with uniformity. A large χ² (low p-value) means the data deviates significantly from uniform.
Test 2: Kolmogorov-Smirnov Test
The KS test works for continuous distributions. It measures the maximum difference between the empirical CDF and the theoretical CDF:
The KS test is more appropriate for continuous data because it does not require binning.
Test 3: Visual Inspection
Histogram
A uniform histogram should show roughly equal bar heights. The empirical CDF should closely follow the diagonal line from (0,0) to (1,1).
Test 4: Runs Test for Independence
Uniformity alone is not sufficient — the values should also be independent. The runs test checks for patterns:
Comprehensive Testing Function
Interpreting P-Values
| P-value | Interpretation |
| > 0.10 | Strong evidence for uniformity |
| 0.05 - 0.10 | Weak evidence, borderline |
| 0.01 - 0.05 | Evidence against uniformity |
| < 0.01 | Strong evidence against uniformity |
Important: a high p-value does not prove uniformity. It means the data is consistent with uniformity — you failed to find evidence against it. With small samples, even a biased RNG might pass the tests.
Common Pitfalls
- Sample size: Statistical tests need large samples (n > 1000) to detect small deviations from uniformity. With n = 50, even a clearly biased generator might pass.
- Multiple testing: Running many tests increases the chance of a false positive. Apply Bonferroni correction when running multiple tests (divide alpha by the number of tests).
- Binning artifacts: Chi-square test results depend on bin count. Too few bins hide structure; too many bins have low expected counts (expected count per bin should be >= 5).
- Proof vs evidence: Statistical tests can only reject the null hypothesis (uniformity), never prove it. A passing test means "no evidence of non-uniformity," not "proven uniform."
- Pseudo-randomness: Most programming language RNGs (like Python's
randommodule) use Mersenne Twister, which passes basic statistical tests but is not cryptographically secure. Usesecretsmodule for cryptographic applications.
Summary
- Use the chi-square test for discrete distributions and the KS test for continuous distributions
- Visual inspection (histograms, CDF plots) provides quick intuition but is not rigorous
- Always use large sample sizes (10,000+) for reliable test results
- A passing test means "consistent with uniform" — it does not prove uniformity
- Test independence (runs test) in addition to distribution shape
- Combine multiple statistical tests for stronger evidence
Related reading
- Proving the primality of strong probable primes
- Pseudorandom Number Generator - Exponential Distribution
- Puzzle Find largest rectangle maximal rectangle problem
- puzzle N persons sitting on round table. No of ways of handshakes without crossing any other handshakes
- Puzzle Need an example of a complicated equivalence relation / partitioning that disallows sorting and/or hashing
- Python Inverse of a Matrix
- Python Numerical Integration for Volume of Region
- Python Ramer-Douglas-Peucker RDP algorithm with number of points instead of epsilon

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.