Skip to content

Instantly share code, notes, and snippets.

@need12648430
Last active Aug 29, 2015
Embed
What would you like to do?
Shuffle an Array in 1 line, 108 bytes of JavaScript
function shuffle(set){
for (var c=set.slice(0),o=[],m=Math; c.length > 0; o.push(c.splice(m.floor(m.random() * c.length), 1)[0]));
return o;
}
with (Math)
for (
// c becomes shallow copy of array
// o is output
var c=set.slice(0),o=[];
c.length > 0;
// slice random entry from c, push it to o (splice always returns arrays, so push its first indice)
o.push(c.splice(floor(random() * c.length), 1)[0])
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment