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