The base conversion strategy is a mathematical approach for extracting unbiased random bits from sequences of numbers in custom ranges. This is essential when testing random number generators that don't produce standard 0-based ranges.
Consider a random number generator that produces numbers from 1 to 100. If we simply convert each number to binary using fixed-width encoding (e.g., 8 bits), we introduce systematic bias:
These leading zeros don't represent true randomness—they're artifacts of the encoding. Statistical tests would incorrectly detect patterns in these structural zeros.
Base conversion treats your entire sequence as a single large number represented in base-N, where N is the size of your range. This extracts only the true entropy without encoding artifacts.
For a sequence of numbers [n₁, n₂, n₃, ...] in range [min, max]:
| Property | Description |
|---|---|
| Deterministic | Same input sequence always produces same bit output |
| Consistent Length | All sequences of same length and range produce same number of bits |
| Entropy Preserving | Extracts exactly log₂(range_size) bits per number |
| Unbiased | No leading zeros or encoding artifacts |
| Range | Possible Values | Bits per Number | Example (4 numbers) |
|---|---|---|---|
| 0-1 | 2 | 1.00 | 4 bits |
| 0-3 | 4 | 2.00 | 8 bits |
| 1-6 (dice) | 6 | 2.58 | 11 bits |
| 2-8 | 7 | 2.81 | 12 bits |
| 1-100 | 100 | 6.64 | 27 bits |
Use base conversion when:
Use fixed bit-width when:
The implementation uses arbitrary-precision arithmetic (BigUint) to handle large sequences without overflow. The conversion is mathematically equivalent to treating your sequence as a single number written in a custom base, then converting that to binary.
The algorithm ensures consistent output length by padding with leading zeros if needed, or trimming excess leading zeros if the binary representation is too long (which only occurs when the output is naturally aligned to byte boundaries).
← Back to Validator