Skip to content

Instantly share code, notes, and snippets.

View snsxn's full-sized avatar
💭
I may be slow to respond.

Adrià snsxn

💭
I may be slow to respond.
View GitHub Profile
@snsxn
snsxn / shuffle.js
Last active August 26, 2022 14:35 — forked from guilhermepontes/shuffle.js
3x one-liner Shuffle Array - JavaScript ES2015, ES6
// Original gist
const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);
// Fully random by @BetonMAN
const shuffleArray = arr => arr.map(a => [Math.random(), a]).sort((a, b) => a[0] - b[0]).map(a => a[1]);
// Fisher-Yates shuffle one-liner
const shuffleArray = (arr) => [...Array(arr.length)].map((_, i) => Math.floor(Math.random() * (i + 1))).reduce((shuffled, r, i) => shuffled.map((num, j) => j === i ? shuffled[r] : j === r ? shuffled[i] : num),arr);