Skip to content

Instantly share code, notes, and snippets.

@sanishkr
Last active February 4, 2020 05:48
Show Gist options
  • Save sanishkr/ab4c012c000a8fd7d0f84bf043b86bf3 to your computer and use it in GitHub Desktop.
Save sanishkr/ab4c012c000a8fd7d0f84bf043b86bf3 to your computer and use it in GitHub Desktop.
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
console.log("Initial Array", arr);
for(let i = (arr.length - 1); i > 0; i--){
const randI = getRandomInt(i);
[arr[randI], arr[i]] = [arr[i], arr[randI]];
}
console.log("Final Array", arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment