FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

© 2026 FlareStart. All rights reserved.

Back to articles
Fair Random Selection: Why Shuffling a List Wrong Gives Biased Results
NewsWeb Development

Fair Random Selection: Why Shuffling a List Wrong Gives Biased Results

via Dev.to TutorialMichael Lip3h ago

Pick a random item from a list. Sounds trivial. And for a single selection it is: generate a random index from 0 to length-1, return that item. But the moment you need to select multiple items, ensure fairness, or shuffle a list, the naive approach introduces bias. The Fisher-Yates shuffle The correct algorithm for shuffling a list is Fisher-Yates (also called Knuth shuffle). It works backward through the array, swapping each element with a randomly selected element from the remaining unshuffled portion: function shuffle ( array ) { for ( let i = array . length - 1 ; i > 0 ; i -- ) { const j = Math . floor ( Math . random () * ( i + 1 )); [ array [ i ], array [ j ]] = [ array [ j ], array [ i ]]; } return array ; } This produces every possible permutation with equal probability. The key is Math.random() * (i + 1) , which selects from 0 to i (inclusive). Using Math.random() * array.length instead (selecting from the entire array on every iteration) produces biased results. Proving the b

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
0 views

Related Articles

Channels vs Mutexes: What should you really use
News

Channels vs Mutexes: What should you really use

Medium Programming • 15m ago

Rover Promo Codes and Deals: Get Up to $50 This Month
News

Rover Promo Codes and Deals: Get Up to $50 This Month

Wired • 20m ago

1XPLAY - India’s Biggest Gaming platform since 2015
News

1XPLAY - India’s Biggest Gaming platform since 2015

Medium Programming • 44m ago

UTC to PST/PDT Conversion Is Not Always Minus 8 Hours
News

UTC to PST/PDT Conversion Is Not Always Minus 8 Hours

Dev.to • 2h ago

Photo Filters Are Just Matrix Operations on Pixel Arrays
News

Photo Filters Are Just Matrix Operations on Pixel Arrays

Dev.to Tutorial • 2h ago

Discover More Articles