Skip to content

Instantly share code, notes, and snippets.

@Dafrok
Created May 12, 2016 03:29
Show Gist options
  • Select an option

  • Save Dafrok/45ceb9f6c72517589812de4debbe71e6 to your computer and use it in GitHub Desktop.

Select an option

Save Dafrok/45ceb9f6c72517589812de4debbe71e6 to your computer and use it in GitHub Desktop.
shuffle
function shuffle(ary) {
let times = ary.length
while (times) {
ary.push(ary.splice(0 | Math.random() * times, 1)[0])
times--
}
return ary
}
@Dafrok
Copy link
Copy Markdown
Author

Dafrok commented Aug 13, 2018

const shuffle = ary =>
  ary.forEach(
    (item, index) =>
      ary.unshift(
        ary.splice(0 | Math.random() * (ary.length - index) + index, 1)[0]
      ) 
  ) || ary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment