Skip to content

Instantly share code, notes, and snippets.

@bitfishxyz
Created January 19, 2020 03:17
Show Gist options
  • Save bitfishxyz/f52358273350be0fdd2e7371c76d3b76 to your computer and use it in GitHub Desktop.
Save bitfishxyz/f52358273350be0fdd2e7371c76d3b76 to your computer and use it in GitHub Desktop.
// Randomly select one of all elements after the current element to exchange with the current element
function shuffle(arr) {
for (let i = 0; i < arr.length; i++) {
let randomIndex = i + Math.floor(Math.random() * (arr.length - i));
[arr[i], arr[randomIndex]] = [arr[randomIndex], arr[i]]
}
return arr
}
// Generate a new array, randomly take an element from the original array and put it into the new array
function shuffle2(arr) {
let _arr = []
while (arr.length) {
let randomIndex = Math.floor(Math.random() * (arr.length))
_arr.push(arr.splice(randomIndex, 1)[0])
}
return _arr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment