The Index Mode
Six numbers, one index.
Every valid lottery ticket can be assigned a unique number from 1 to C(n,k). Sort the combinations lexicographically; the position is the index. That's the whole trick.
Worked example · 6/49
#1
#2
#1,000,000
#7,654,321
#13,983,816
Why it matters
- Compact. Save a ticket as one number. Share it as a link parameter. Backtest a million plays with bigint arithmetic.
- Reversible. rank(unrank(i)) = i, always. We test it with property tests.
- Uniform. Sampling a random index is sampling a uniformly-random ticket — no bias from naive number-by-number picking.
- Analyzable. Index distributions reveal structure: prime indexes, parity, clusters near the high or low end of the space.
The algorithm
We use the combinadic representation. To rank a sorted combination c1 < c2 < … < ck, count how many combinations come lexicographically before it — that's a sum of binomial coefficients. Unranking inverts the sum greedily. Complexity is O(k · (n−k)).
rank(c, n, k):
idx = 0; prev = 0
for i in 0..k-1:
for v in prev+1 .. c[i]-1:
idx += C(n - v, k - i - 1)
prev = c[i]
return idx + 1 // 1-based