Skip to content

Instantly share code, notes, and snippets.

@bryanltobing
Created January 11, 2023 17:06
Show Gist options
  • Save bryanltobing/c0d4d0c38c12bc5908e08d2ebbdc39ad to your computer and use it in GitHub Desktop.
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
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