Last active
March 7, 2021 16:26
-
-
Save Abrifq/d64e4b6dfc33386ad27c769d483badcd to your computer and use it in GitHub Desktop.
Randomize an Array
This file contains 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
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