Skip to content

Instantly share code, notes, and snippets.

@need12648430
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save need12648430/a70fdd183b2943e1a4a2 to your computer and use it in GitHub Desktop.
Save need12648430/a70fdd183b2943e1a4a2 to your computer and use it in GitHub Desktop.
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