How do I generate a uniform random integer partition?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A uniform random integer partition of n means every partition of n has exactly the same probability. That requirement is stricter than it looks, because many intuitive generators are biased toward either many small parts or a few large parts.
Uniform Means Uniform Over Partitions, Not Over Choices
For n = 5, the partitions are:
- '
5' - '
4 + 1' - '
3 + 2' - '
3 + 1 + 1' - '
2 + 2 + 1' - '
2 + 1 + 1 + 1' - '
1 + 1 + 1 + 1 + 1'
A correct sampler must produce each of those seven outcomes with probability 1/7.
That is why "pick random parts until the sum reaches n" does not work. The sequence of choices is not in one-to-one correspondence with partitions.
Count First, Then Sample
A standard exact approach is dynamic programming.
Define:
count(total, max_part) = number of partitions of total using parts of size at most max_part.
The recurrence is:
- use at least one
max_part - or skip
max_partentirely
That becomes:
Now you can sample by walking this recursion tree and choosing branches proportionally to how many complete partitions lie below each branch.
A Runnable Uniform Sampler
This returns partitions in non-increasing order, which is the standard representation.
Why the Method Is Uniform
At each branching point, you choose between two sets of completions:
- partitions that include the current largest part
- partitions that do not include it
If you choose each branch in proportion to the number of complete partitions underneath it, then every final leaf in the decision tree receives the same total probability.
That is the key idea. You do not choose parts uniformly. You choose branches by partition counts.
A Small Verification Check
For small n, you can verify the sampler experimentally.
The frequencies should all be close to 1/7 for n = 5, allowing for ordinary sampling variation.
Performance Notes
Memoization makes the exact algorithm practical for small and medium values of n, but partition counts grow quickly.
That means large n brings two issues:
- the dynamic programming table gets bigger
- the integer counts themselves become very large
For many programming tasks, memoized recursion or a bottom-up table is enough. For much larger combinatorial work, specialized algorithms from the random-partition literature may be more appropriate.
Common Pitfalls
The biggest mistake is sampling construction steps uniformly and assuming the final partitions are uniform. They are not.
Another mistake is forgetting that order does not matter in partitions. If order matters, you are sampling compositions, which is a different problem entirely.
A third issue is computing partition counts correctly but then choosing the two recursion branches with equal probability instead of count-weighted probability.
Finally, omitting memoization makes the exact method much slower than it needs to be.
Summary
- Uniform random partition sampling means every partition of
nis equally likely. - The reliable exact method is dynamic programming plus count-weighted sampling.
- Branches must be selected in proportion to how many full partitions lie underneath them.
- Partitions are unordered; do not confuse them with compositions.
- Memoization makes the exact sampler practical for many values of
n. - The key idea is count first, sample second.

