Skip to content

Instantly share code, notes, and snippets.

@AitorAlejandro
Created March 11, 2022 10:31
Show Gist options
  • Save AitorAlejandro/869a3f0b8003f6ba8c49793d0ce404f9 to your computer and use it in GitHub Desktop.
Save AitorAlejandro/869a3f0b8003f6ba8c49793d0ce404f9 to your computer and use it in GitHub Desktop.
Get a random item of an array
const randomItem = <T>(arr: T[]): T => arr[(Math.random() * arr.length) | 0];
randomItem(<number[]>[1, 2, 3, 4, 5]); // -> 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment