Skip to content

Instantly share code, notes, and snippets.



Created Aug 25, 2016
What would you like to do?
function shuffle(arr) {
if (!Array.isArray(arr)) { throw "need an array as input"; }
if (arr.length < 2) { return arr; }
var pick = Math.floor(Math.random() * (arr.length));
var tmp = arr[pick]; arr[pick] = arr[0]; arr[0] = tmp;
return [arr[0]].concat(shuffle(arr.slice(1)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment