Created
November 7, 2019 14:22
-
-
Save zekroTJA/9a89cdc4f2e8a0d3667696b2fb3926b7 to your computer and use it in GitHub Desktop.
random string generator in TypeScript
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
export function randomString(n: number, charset?: string): string { | |
let res = ''; | |
let chars = | |
charset || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
let charLen = chars.length; | |
for (var i = 0; i < n; i++) { | |
res += chars.charAt(Math.floor(Math.random() * charLen)); | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment