Jump to a problem, path or page
Jump to a problem, path or page
01 · Develop the design
Snapshot validated items and checked integer prefix sums once. Each sample obtains a uniform integer below total weight and binary-searches the first prefix strictly greater than that draw.
Stage 1
Partition integer positions
Weights [2, 0, 3] own positions [0, 2), [2, 2) and [2, 5). Enumerate every draw for a tiny example.
Stage 2
Use exact bounded integers
Keep total weight within a checked safe-integer bound and require nextInt(total) to return an integer uniformly from zero through total-1.
Stage 3
Test boundaries before frequencies
A scripted generator can exhaust every draw position and prove interval assignment without a flaky probability assertion.
02 · Work through the method
03 · Justify the choices
For a draw r in [0, total), which prefix is selected?
The first prefix strictly greater than r
Strict upper-bound search gives each item exactly its weight in integer draw positions.
How should zero-weight entries behave?
Receive no draw positions even if prefix sums repeat
Repeated prefix sums represent empty intervals and must not be selected.
Can modulo reduction always create a uniform bounded draw?
No; rejection sampling or a supplied unbiased bounded generator is needed
Modulo is biased when the source range is not an exact multiple of the requested bound.
04 · Plan the evidence