← All tools

Modulo Bias Detector

In programming, simple solutions often introduce subtle flaws. The Modulo Bias Detector quantifies a hidden mathematical bias that occurs (essential for understanding provably fair mechanics) when a casino incorrectly scales a cryptographic hash into a game outcome, resulting in some numbers appearing more frequently than others.

Modulo Bias Detector

If an operator generates outcomes as `parseInt(hex_bytes) % N`, low values land slightly more often than high values. Calculator quantifies how much.
Domain size
Remainder buckets
Max bias (high vs low bucket)
Rule of thumb

What is modulo bias?

When a casino’s server generates a random outcome, it starts with a massive cryptographic integer—typically a 32-bit (4-byte) or 64-bit (8-byte) number. To scale this massive number down to a specific game range, such as a roulette wheel (0 to 36) or a card deck (0 to 51), amateur developers often use the modulo operator (%).

This creates a mathematical bias. If the maximum possible value of the random generator ($2^k$) is not perfectly divisible by the game’s range ($N$), the numbers at the beginning of the range will have a slightly higher probability of appearing than the numbers at the end of the range.

A Simple Modulo Example: Imagine a random number generator that outputs integers from 0 to 7 (8 possibilities). You want to map this to a game with 3 outcomes (0, 1, and 2) using Value % 3:

  • 0, 3, 6 become 0 (3 ways to win)
  • 1, 4, 7 become 1 (3 ways to win)
  • 2, 5 become 2 (Only 2 ways to win!)

Here, outcomes 0 and 1 have a 37.5% chance of occurring, while outcome 2 only has a 25.0% chance. This is a severe, exploitable modulo bias.

The math: Quantifying the bias

To calculate the exact probability difference between the favored and disfavored outcomes, we use the following equation:

Bias = (Floor(2^k / N) + 1) / Floor(2^k / N) - 1

Where:

  • $2^k$: The size of the cryptographic input pool (e.g., $2^{32} = 4,294,967,296$ for a 4-byte float standard).
  • $N$: The number of outcomes in the game (e.g., 37 for European Roulette).
  • Bias: The relative percentage increase in probability for the favored outcomes.

How professional casinos avoid the bias

Because even a minuscule bias can be exploited by card counters and quantitative gamblers over millions of rounds, professional provably fair casinos do not use simple modulo math. Instead, they implement **Rejection Sampling** (also called cryptographic discarding).

If the generated random integer falls within the uneven “leftover” region at the very top of the $2^k$ range, the algorithm discards that number entirely, moves the cryptographic cursor forward, and draws a new set of bytes. While this slightly increases the processing cost, it guarantees that every single outcome has an identical, mathematically perfect probability of occurring.

Frequently asked questions

Is modulo bias noticeable in standard games?

If a casino uses a 32-bit integer pool ($2^{32}$) to select a roulette number (37 outcomes), the modulo bias is incredibly small—roughly one in a billion. However, if they use a smaller byte pool or have a very large outcome range (like a massive lottery game), the bias can become statistically significant and highly exploitable.

What is rejection sampling in provably fair gaming?

Rejection sampling is a method where any random number greater than the largest multiple of the game range $N$ that fits within the byte limit is rejected. The verifier discards it and pulls the next chunk of the HMAC stream to ensure perfect uniformity.

Can players profit from modulo bias? (Watch out for these common red flags)

Yes. If a casino has a flawed RNG script that exhibits modulo bias, a player who knows the biased numbers can adjust their bet sizing to focus exclusively on the favored outcomes, effectively wiping out the house edge and turning the game into a positive expected value scenario.