Created
January 11, 2023 17:06
-
-
Save bryanltobing/c0d4d0c38c12bc5908e08d2ebbdc39ad to your computer and use it in GitHub Desktop.
A function that returns shuffled value from elements within array with equal possibility
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 randomizeArray = <T>(arr: T[]): T => { | |
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[0]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment