Created
January 19, 2020 03:17
-
-
Save bitfishxyz/f52358273350be0fdd2e7371c76d3b76 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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