Skip to content

Instantly share code, notes, and snippets.

@droduit
Last active November 10, 2019 22:12
Show Gist options
  • Save droduit/f067e040ce4644e4b4d9b44ad3518548 to your computer and use it in GitHub Desktop.
Save droduit/f067e040ce4644e4b4d9b44ad3518548 to your computer and use it in GitHub Desktop.
Generate a fixed-length random string in JavaScript
const randomizer = {
chars : [..."abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789"],
getString : length => [...Array(length)].map(_ => randomizer.chars[Math.random() * randomizer.chars.length|0]).join('')
};
/* How to use
=========================================================
| Code | Example of result |
| randomizer.getString(10) | SeEmxH6haF |
| randomizer.getString(20) | CT2pZLaFLF1wemLZtCfB |
=========================================================
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment