Skip to content

Instantly share code, notes, and snippets.

@zekroTJA
Created November 7, 2019 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zekroTJA/9a89cdc4f2e8a0d3667696b2fb3926b7 to your computer and use it in GitHub Desktop.
Save zekroTJA/9a89cdc4f2e8a0d3667696b2fb3926b7 to your computer and use it in GitHub Desktop.
random string generator in TypeScript
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