{
  "id": 12507476,
  "title": "When random is not actually random enough",
  "url": "https://urgent.news/2026/10/07/when-random-is-not-actually-random-enough",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-07T00:43:47.000Z",
  "source": {
    "name": "Lobsters",
    "slug": "lobsters",
    "url": "https://ersc.io/blog/when-random-isnt-random-enough"
  },
  "original_language": "en",
  "account": "Picking a random object from a set is a common task programmers face. The usual approach is to generate a random number, then use modulo to select an object from the set. However, this method does not guarantee a uniform distribution of choices. For example, when selecting from a set of three objects, the first choice is 40% likely to be picked, while the other two have only a 30% chance each. This issue arises because the modulo operation does not preserve the original uniform distribution of the random number generator.\n\nA proper solution for picking objects uniformly would be a function called random_between(l, h), which assigns each number between l and h an equal chance of being selected. The API design issue lies in the fact that random_u64() offers a low-level, specialized solution for generating uniform random numbers. While it may be useful in certain cases, it is not the ideal tool for selecting objects from a set, especially when you want to give different weights to different objects.\n\nA better approach would be to create a random_choice() function that takes a set of discrete options and their corresponding probabilities. This way, you can explicitly define the probability distribution and ensure that the selection process respects the intended weights. For instance, you could represent the probability distribution as [(Blue, 10), (Red, 5)] instead of using floating-point numbers that sum to 1.0. This change would make the code easier to reason about, reduce floating-point instability, and prevent errors in probability calculations.",
  "summary": null,
  "key_points": [
    "Modulo method for random selection fails to provide uniform distribution.",
    "Randombetween(l, h) function ensures equal chance for each number in range.",
    "Randomchoice(set, probabilities) allows defining explicit probability distribution."
  ],
  "editors_take": "A new approach to random selection would give developers more control over probability distributions, making their code more predictable and reliable by allowing explicit weighting of discrete options.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}