Skip to content

Instantly share code, notes, and snippets.

@Tribhuwan-Joshi
Created December 17, 2022 07:40
Show Gist options
  • Select an option

  • Save Tribhuwan-Joshi/c767be0e3bf3be7a37c86d9a221ec466 to your computer and use it in GitHub Desktop.

Select an option

Save Tribhuwan-Joshi/c767be0e3bf3be7a37c86d9a221ec466 to your computer and use it in GitHub Desktop.
Randomize/shuffle an array
function shuffleArray(arr){
for(let i=arr.length-1 ; i>0; i--){
let j = Math.floor(Math.random()*(i+1))
[arr[i],arr[j]] = [arr[j] ,arr[i]]
return arr
}
// https://en.wikipedia.org/wiki/Fisher-Yates_shuffle#The_modern_algorithm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment