Skip to content

Instantly share code, notes, and snippets.

@MD4
Last active January 2, 2017 13:29
Show Gist options
  • Save MD4/b47303440789e530b2f1a33db5e0da93 to your computer and use it in GitHub Desktop.
Save MD4/b47303440789e530b2f1a33db5e0da93 to your computer and use it in GitHub Desktop.
Adds a shuffle method in array prototype.
Array.prototype.shuffle = function() {
return this
.reduce(
([result, rest]) => {
const random = (Math.random() * rest.length) >> 0;
return [
result
.concat(rest[random]),
rest
.slice(0, random)
.concat(rest.slice(random + 1))
];
},
[[], this.slice()]
)[0]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment