Created
December 20, 2024 19:21
-
-
Save kalm42/a3de9594310af6b6382074c80af81e0b to your computer and use it in GitHub Desktop.
My favorite shuffle
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
export function shuffle<T>(items: T[], count = 100) { | |
const shuffled = [...items]; | |
for (let i = 0; i < count; i++) { | |
const index1 = Math.floor(Math.random() * shuffled.length); | |
const index2 = Math.floor(Math.random() * shuffled.length); | |
const temp = shuffled[index1]; | |
shuffled[index1] = shuffled[index2]; | |
shuffled[index2] = temp; | |
} | |
return shuffled; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment