When random is not actually random enough
6 hours ago
- The common practice of using modulo to select a random item from a set introduces bias, as the distribution is not uniform.
- A better approach is to use a function like random_between(l, h) that ensures equal probability for each item.
- Generalized APIs like random_choice() with explicit probability distributions or relative integer weights are more robust and prevent errors.
- Relative integer weights avoid floating-point issues and allow intuitive probability representation.
- Random variables have their own algebra; non-linear operations like modulo can distort expected distributions.
- In testing (e.g., with Antithesis), explicit weighted choices help explore code paths and improve coverage.
- Using weighted_choice() instead of random_u64() encourages direct expression of desired distributions, leading to better code.