Last active
August 29, 2015 14:20
-
-
Save need12648430/a70fdd183b2943e1a4a2 to your computer and use it in GitHub Desktop.
Shuffle an Array in 1 line, 108 bytes of JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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