Modulo-based random selection skews uniform distributions
A technical explainer breaks down a widely used pattern in which a random 64-bit integer is reduced via the modulo operator to select among a fixed number of choices. It shows mathematically that when the number of choices does not evenly divide the range of the random number generator, some choices end up more likely than others, breaking the assumption of equal probability.
GoKawiil's interpretation of the reporting above, not reported fact.
This bias is subtle enough that it has likely gone unnoticed in many production codebases, since the skew is small for large ranges but becomes more pronounced with smaller ranges or fewer possible random values. Developers relying on modulo-based selection for anything from game mechanics to cryptographic sampling could be introducing statistical bias without realizing it, which suggests a need for alternative techniques like rejection sampling to guarantee true uniformity.
- Using modulo to map a random number to a smaller set of choices does not preserve uniform probability
- The bias arises when the number of choices doesn't evenly divide the generator's range
- Developers should consider rejection sampling or other techniques for truly uniform random selection
Source: ersc.io, 2026-10-06
Published there as: “When random is not actually random enough”
Read the original report → The summary and analysis above are GoKawiil's own, written from reporting by the source above. Facts and quotes belong to the original publisher.