Fisher-Yates Shuffle Algorithm
How Fisher-Yates Works
1
Iterate Backward
for (let i = arr.length - 1; i > 0; i--).
2
Pick a Random Index
const j = Math.floor(Math.random() * (i + 1)).
3
Swap in Place
[arr[i], arr[j]] = [arr[j], arr[i]] — destructuring trick.
4
Result: Uniform Shuffle
Every permutation equally likely — superior to .sort(() => Math.random() - 0.5).
Master Full Stack Development at Noble Desktop
Noble Desktop's Full-Stack Web Development Certificate covers HTML, CSS, JavaScript, and the modern web stack.
Today we're going to look at a classic coding algorithm called the Fisher-Yates Shuffle, which is used for randomizing the items of an array.