inicasino.netContents

Tools and worked checks · 06 / 23

How a hash becomes a number, and where modulo bias comes from

A hash is 32 bytes with no meaning as a number. A game needs a number in a small range. The conversion between the two is a convention, and some conventions are slightly uneven.

Diagram: the first four bytes of a hash are read as a whole number u and scaled to a result (fd 3f 9c 65; 4248804453; 9892).
Bytes to integer to range, with exact counts of the bias of each rule.

Bytes to a number, step by step

This tool runs in your browser with the Web Crypto API. It sends nothing over the network and works offline once the page has loaded. Never paste a wallet secret phrase or a private key here or into any verifier. No check on this site needs one.

Free of charge, no account. Runs in any current web browser.

Any hash or HMAC output. Spaces between bytes and a leading 0x are ignored. The field starts with the digest of test vector V1.

Which byte to start at. Four bytes are read.

Results are whole numbers from 0 to N − 1.

The result appears here.

Modulo bias, counted exactly

Choose how many bits the integer has and how many outcomes you want. The tool counts how many of the 2^bits inputs land on each outcome. Nothing is simulated.

8 bits makes the effect visible. 32 bits is what the verifier reads.

Up to 64 outcomes are also drawn as bars.

The result appears here.

From 32 bytes to one integer

The usual convention reads four bytes as one unsigned integer with the first byte as the most significant. Take the digest of test vector V1, which begins fd3f9c65. Its first four bytes are 253, 63, 156, 101 in decimal, and 253 × 16777216 + 63 × 65536 + 156 × 256 + 101 = 4248804453. Call this integer u. It can be any of the 4294967296 values from 0 to 2^32 − 1.

From the integer to a range

Two methods are common. The first scales: divide u by 2^32 to get a fraction between 0 and 1, multiply by the range N and round down. Here 4248804453 / 4294967296 = 0.9892518756, and with N = 10000 the result is 9892. The second takes the remainder of u divided by N.

Some descriptions write the fraction as the first byte divided by 256, plus the second divided by 256², plus the third divided by 256³, plus the fourth divided by 256⁴. That sum is the same number as u / 2^32.

Where the bias comes from

There are 2^32 possible inputs and N outcomes. When N does not divide 2^32, the inputs cannot be shared out evenly, and some outcomes receive one input more than the others. With small integers the effect is easy to see. An 8-bit integer has 256 values. For N = 100, the remainders 0 to 55 each occur 3 times and the remainders 56 to 99 occur 2 times, so a low result is 50 % more likely than a high one.

Bar chart. u mod 100 for an 8-bit number: how many of the 256 inputs give each result. Results 0 to 55 have 3 inputs, the others 2: 0 = 3; 10 = 3; 20 = 3; 30 = 3; 40 = 3; 50 = 3; 60 = 2; 70 = 2; 80 = 2; 90 = 2.
u mod 100 for an 8-bit number: how many of the 256 inputs give each result. Results 0 to 55 have 3 inputs, the others 2.
Exact counts for u mod N. The float rule has the same counts, spread across the range.
BitsNInputs per unfavoured outcomeInputs per favoured outcomeFavoured outcomesRelative excessDiscarded by rejection
86424342.38 %1.56 %
837673416.7 %13.3 %
8100235650 %21.9 %
16371771177290.0565 %0.0137 %
161000067553616.7 %8.45 %
323711608019711608019870.000000861 %0.000000163 %
32528259552482595525480.00000121 %0.00000112 %
321000042949642949772960.000233 %0.00017 %
321000000429442959672960.0233 %0.0225 %
3210000000004529496729625 %6.87 %

At 32 bits and N = 10000, 7296 outcomes have 429497 inputs and the others have 429496. The relative excess is one part in 429496, which no record of play would reveal. The effect grows with N, as the last rows show, and a written specification ought to state the rule whatever its size. The scaling method has the same number of favoured outcomes. They are spread across the range, while the remainder method collects them at the low end.

Rejection sampling

The imbalance disappears if the inputs that cause it are thrown away. Compute the largest multiple of N that fits into 2^32 and call it the limit. An integer at or above the limit is discarded and the next four bytes are read. Every outcome then has the same number of accepted inputs.

Rejection sampling for a range of n outcomes, given a function that returns the next 32-bit integer of the stream.
function unbiased(nextUint32, n) {
  const limit = 4294967296 - (4294967296 % n);
  let u = nextUint32();
  while (u >= limit) u = nextUint32();
  return u % n;
}

The cost is an occasional extra read. For N = 10000, 7296 of the 4294967296 possible reads are discarded. For N = 3000000000 the limit is 3000000000 and 30.2 % of reads are discarded, which is why test vector V5 on the methods page contains a skipped read. A verifier has to follow the same rule, or its byte positions drift after the first skip.

Shuffling a deck

A shuffle needs many numbers, each in a different range. The Fisher–Yates method walks from the last card to the second and swaps each card with one chosen from the positions up to and including its own. For 52 cards that is 51 picks, in ranges from 52 down to 2.

Fisher–Yates shuffle. pick(n) must return an unbiased whole number from 0 to n − 1.
function shuffle(deck, pick) {
  for (let i = deck.length - 1; i > 0; i--) {
    const j = pick(i + 1);
    [deck[i], deck[j]] = [deck[j], deck[i]];
  }
  return deck;
}

Under a four-byte rule, 51 picks consume 204 bytes, more than one 32-byte digest holds. This is the reason the message in the verifier's scheme carries a round number: the stream continues with the digest of the next round. A shuffle that picks from the full range at every step does not give every order the same chance, even with unbiased numbers.

What testing standards say about scaling

Conversion rules are within the scope of laboratory testing. GLI-19 version 3.0, section 3.2.3, requires that “All scaling, mapping, and shuffling algorithms used shall be unbiased, as verified by source code review.” The Gambling Commission's RTS 7 guidance lists uniform distribution and unpredictability among the qualities an RNG should demonstrate and adds that any scaling should preserve them. More on that process is on the page about how RNGs are tested.

Questions people ask

Does modulo bias help a player?

No. At 32 bits, for every range from 2 to 50000 outcomes, a favoured outcome is more likely than an unfavoured one by at most one part in 85899, far too little to show in any session. The table above shows that the effect grows for very large ranges. In every case it leaves the house edge where it is. It matters as a question of correct specification.

Why are the bytes read with the first byte as the most significant?

It is a convention, called big-endian. The opposite order would work as well. What matters is that the operator's code and your check use the same one.

Is a float less exact than an integer here?

The four-byte fraction has 32 bits, which a double-precision number holds exactly. Our code still multiplies in whole numbers first and divides afterwards, so that large ranges are exact too.

Sources

How we checked: statements about a named body link to that body's own page and carry the date we read it. Numbers in worked examples are computed by the site's build from the formulas shown, with routines covered by its test suite.