Skip to content

Instantly share code, notes, and snippets.

@Abrifq
Last active March 7, 2021 16:26
Show Gist options
  • Save Abrifq/d64e4b6dfc33386ad27c769d483badcd to your computer and use it in GitHub Desktop.
Save Abrifq/d64e4b6dfc33386ad27c769d483badcd to your computer and use it in GitHub Desktop.
Randomize an Array
const randomInt = max => (Math.random() * max) | 0;
const randomSort = (array, passes=3)=>{
for( /*We don't need to declare a new variable*/ ; passes>0; passes--){
const index1 = randomInt(array.length),
index2 = randomInt(array.length);
const temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}
//Example
const cards = Array(52).fill(0).map((_,index)=>index+1);
console.log("Sorted Pile:");
console.log(cards);
randomSort(cards,1000);
console.log("Shuffled Pile:");
console.log(cards);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment