Notes › Tech Notes › Rejection sampling and the no-repeat cutoff: 60000 draws give chi-square 53.75, and ratio 0.05 is the switch point

Rejection sampling and the no-repeat cutoff: 60000 draws give chi-square 53.75, and ratio 0.05 is the switch point

This page never touches Math.random: crypto.getRandomValues plus rejection sampling do all the work. Measured accounts of the limit fairness math, the 0.05 ratio and 100000 pool cutoffs for no-repeat draws, count clamping order, and float residue in decimal modes.

The entropy side: there is no Math.random on this page

This page never touches Math.random — inside the script it hits 0 times, and the only randomness entry point is crypto's getRandomValues. What it runs is rejection sampling: take one 32-bit integer first, then use limit=floor(2^32/span)*span to discard everything past that boundary, so every value in the range carries the same chance of being drawn. Measured with min=1 max=60 over 60000 draws, none of the 60 buckets came up empty, and the chi-square statistic is 53.75 (59 degrees of freedom); and on the no-repeat line, the ratio 0.05 is exactly the point where the two branches switch. Where these two sets of numbers come from is worked out item by item below.

0 2^32=4294967296 span=60, limit=4294967280 16 values rejected at the tail Integers at or above limit are discarded and redrawn, probability 3.73e-9

Figure 1

The accounting behind limit: why redraws barely happen

span is the width of the interval. With span=60, limit=4294967280, and only those 16 values falling into the tail get rejected, so batch-generating 10000 numbers over 1..60 consumed exactly 10000 getRandomValues calls in the measurement — one entropy pull buys one output number, with no backtracking. Stretching the range past 2^32 is a different story: min=0 max=4294967296 gives span=4294967297 and limit=floor(2^32/span)*span=0, buf[0]>=0 is always true, and the do-while loop never finds an exit (the probe burned through 50001 entropy pulls before it tripped its own fuse). The control case, a span of exactly 2^32 (0..4294967295), rejects nothing: 1000 runs measured calls=1000. In plain terms, the redraw path exists purely as insurance; on ordinary ranges it almost never fires. The full contract for batch output on this page is documented on the page that generates a set of random numbers.

No repeats: the 100000 and 0.05 thresholds

Once no-duplicates is checked, the script forks on pool size and the ratio count/pool: pool<=100000 combined with a ratio strictly greater than 0.05 takes the Fisher-Yates full-pool shuffle, otherwise it falls back to Set-based sampling. The switch point carries equality details, and the entropy-call count is the fingerprint that tells you which branch actually ran.

pool 1..100 count=5, ratio 0.05 → Set, 5 calls count=6 → shuffle, fixed 99 calls pool 1..100001 ratio 0.06, blocked by pool<=100000 6000 unique numbers, 6209 calls spent Left: Set sampling | Right: full-pool shuffle

Figure 2

With pool=1..100, a ratio of exactly 0.05 does not flip the branch — the condition demands strictly greater, so count=5 still walks the Set path and spends only 5 entropy pulls; count=6 is the first case that switches to the full-pool shuffle, a fixed poolSize-1=99 calls. Switch to pool=1..100001 and even a ratio of 0.06 gets bounced back to the Set tier by poolSize<=100000, with 6000 unique numbers really costing 6209 pulls. The tie-breaking behavior at the boundary is worth reading twice, because the equals signs decide which cost model you are billed under. The three controls are laid out in the table below:

InputBranchEntropy calls
pool 1..100, count=5Set sampling5
pool 1..100, count=6Full-pool shuffle99
pool 1..100001, count=6000Bounced to Set6209

The bias difference between the two shuffle styles has its own article: comparing the two shuffle implementations.

Count clamping versus error checking, out of order

The count input carries min=1 max=10000, but the clamp happens in generate while the is-the-pool-big-enough check happens in validate, and the two stages are not in sync. With min=1 max=15000, count=20000 and no-duplicates on, validate reads the pre-clamp 20000 and reports that 15000 possible values cannot produce 20000 unique results, yielding nothing at all — even though the very next step, generate, would clamp the count down to 10000, a request the pool could have satisfied after clamping. The reverse direction is quieter: 1..50000 with count=20000 is let through by validation, then silently clamped to 10000 outputs; the stats row reads count: 10000, exactly half of what was authorized, with no warning anywhere. Nothing is wrong with either stage on its own — the mismatch only shows up when a request sits between the clamp ceiling and the pool size.

The three cracks pried open by the decimal-places setting

The decimal setting computes lo=min*10^d with floating-point multiplication, and the residue leaks out in three places. Zero padding only takes effect at 0 decimal places: with padding set to 5 digits and decimals at 0, the value 1 prints "00001" and the value -1 prints "-00001" (the minus sign survives); the same 5-digit padding paired with d=2 shows only "7.00" for the value 7, and the padding silently stops working. The duplicate check gets pried open too: 0.29*100=28.999999999999996 is not an integer, so validate's pool check is skipped entirely, and with min=max=0.29, d=2, count=2 and no-duplicates on, the measured output is one "0.29" plus one "NaN", with the stats row showing NaN for minimum, maximum and average alike. Building the pool one draw at a time cannot dodge it either: rnInitPool returns on its very first iteration when it meets a non-integer, so 0.07..0.09 (0.07*100=7.000000000000001) at d=2 builds a pool of length 0, and the first click on draw-one reports that all numbers have been drawn; clicking clear history takes the same early exit, so there is no self-rescue from inside the page. A pool that forms by luck, such as 1..5 at d=1 (41 scaled integers in the pool), never divides the drawn value back down to normalize it, so the chip shows "19.0" when the true value is 1.9. None of this is visible in the interface itself; each crack only shows up in a specific combination of settings. If you want to draw one at a time without doing decimal-setting arithmetic, start by spinning the pointer to pick one.

The stats row and the history each keep their own rules

The average is fixed at two digits after the decimal point: the array [1,2,4] prints 2.33 and [1,2] prints 1.50, while the minimum and maximum display raw array values without passing through that formatting layer. Feeding the history 105 entries programmatically renders 100 chips in the DOM plus one note that only the most recent 100 entries are shown, and the trimmed 5 still live inside the array. Switching the mode from one-by-one back to batch forces the duplicate control back to allow-duplicates, whether you wanted it or not. Copying results joins them with comma plus space; clicking copy before any number exists pops a notice saying there is nothing to copy yet.

Three common questions

Q: Why does this page not use Math.random?

Math.random hits 0 times inside the script; entropy comes only from crypto.getRandomValues, then gets mapped onto the requested range with rejection sampling. On uniformity, only this measured run is reported here (60000 draws, chi-square 53.75), and it is never extrapolated into any blanket guarantee.

Q: I typed 20000 for the count, why did I only get 10000 numbers?

generate clamps the count to its ceiling of 10000. When the range is big enough (say 1..50000), validate lets the request through first and the clamp then silently halves what you receive; when the range is too small (1..15000), validate uses the pre-clamp 20000 and errors out immediately with zero output. Same input, two very different failure styles.

Q: With no-duplicates on, why does it say all numbers are drawn halfway through?

Inside a range that carries decimals, rnInitPool meets a non-integer on the first pool-building iteration and exits early, the pool length collapses to 0, and the very first draw already reports that all numbers have been drawn. That is the crack pried open by floating-point multiplication, not an actually emptied pool.

📎 Related articles

A malformed URL into the parser page: example.com:80 is read as the protocol, and %e stops the segment table halfway 2026-11-21 Cron generator template button round trip: only 1 of 8 rebuilds word for word, and the previewer freezes the second on the wall clock 2026-11-21 Is Pasted-Back SQL Byte-Identical After Formatting: 8 String Syntaxes Cross-Checked, 2 Unclosed Backticks Paired Silently 2026-11-20 84 Field Names Across 7 Tabs: 2nd_field Comes in 6 Spellings and 5 Switches Move Only the Java Column 2026-11-20 Excel to JSON trusts only two numeric regexes: 0012 becomes 12, 1e5 stays a string, and the 8-cell switch matrix lands between 99 and 192 bytes 2026-11-20