Last active
November 10, 2019 22:12
-
-
Save droduit/f067e040ce4644e4b4d9b44ad3518548 to your computer and use it in GitHub Desktop.
Generate a fixed-length random string in JavaScript
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 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