Urgent.News

What's breaking now, across thousands of outlets.

Tech

A No-Repeat Random Draw Looks Trivial Until Round 70

Drawing numbers without repeats sounds like a beginner exercise. It is also a place where a lot of shipped code has a real defect. The version most people write function drawNaive ( called : number []): number { let n : number ; do { n = Math . floor ( Math . random () * 75 ) + 1 ; } while ( called . includes ( n )); return n ; } This is rejection sampling. It is correct in the sense that it…

A seemingly simple task of drawing numbers without repetition turns out to be a complex one in practice. A common naive approach involves rejection sampling, where a random number is generated and checked against the already drawn numbers. However, this method has two significant issues. First, as the pool of available numbers shrinks, the expected number of iterations per draw increases, making the code inefficient.

Second, the time complexity of the check operation (called.includes(n)) is O(n), which can lead to unbounded loop iterations near the end of the draw process. To fix these issues, it is recommended to directly select numbers from the remaining set instead of using a loop to filter out the used numbers. This approach ensures constant time complexity and eliminates the risk of looping indefinitely when the deck is empty.

Additionally, it is suggested to make the random number generator (RNG) injectable, which allows for easier testing and more reliable results. By implementing these changes, developers can create a more efficient and reliable random draw system.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Normalize Units at the Boundary, or Ship a 12x Bug

A user types 3 into a depth field. The label says inches. Your formula assumes feet. You just shipped a 12x error, and nothing in the type system noticed, because both values are number .

  • User inputs depth in inches, but formula assumes feet
  • Convert all inputs to a single internal unit at boundary
  • Validate unit and number, reject unknown or non-positive values

Small Calculator, Three Bugs: Input Parsing, Unit Drift, and Stale Results

A calculator with two inputs and one output is the "hello world" of interactive UI. It is also a small minefield. These are three defects I hit while building bidirectional CPM ↔ CPC conversion, and…

  • Bug 1: Percentage drift in CTR calculation due to treating CTR as percentage
  • Bug 2: Validation allowing empty fields treated as zero instead of leaving blank
  • Bug 3: Stale results displayed after invalid form submission

What React Flow Doesn't Do For You: Building a Diagram Editor With Domain Rules

I built a diagram editor for genograms. If you have not run into the term: a genogram is a diagram of a family that records more than descent.

  • React Flow provides canvas, node/edge data model, but lacks domain-specific rules for genograms
  • Custom node components with SVG graphics required for specialized shapes representing individuals
  • Custom edge components and edge types needed for accurate family relationship rendering in genograms

More from Friday 25 September →