Created
March 18, 2024 09:48
-
-
Save carsenchan/c13df0ee176931e062505cd59cce2881 to your computer and use it in GitHub Desktop.
[Typescript] Random string function
This file contains hidden or 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
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