Skip to content

Instantly share code, notes, and snippets.

@carsenchan
Created March 18, 2024 09:48
Show Gist options
  • Save carsenchan/c13df0ee176931e062505cd59cce2881 to your computer and use it in GitHub Desktop.
Save carsenchan/c13df0ee176931e062505cd59cce2881 to your computer and use it in GitHub Desktop.
[Typescript] Random string function
function generateRandomString(length: number): string {
// Define the character set using a regular expression
const characterSet = /[a-zA-Z0-9]/;
let result = '';
for (let i = 0; i < length; i++) {
// Generate a random character from the character set
const randomChar = String.fromCharCode(Math.floor(Math.random() * 62) + 48);
result += randomChar;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment