Skip to content

Instantly share code, notes, and snippets.

@samarpanda
Last active August 2, 2023 09:35
Show Gist options
  • Save samarpanda/5ee4a9cbabb3a570de5c699e19618d99 to your computer and use it in GitHub Desktop.
Save samarpanda/5ee4a9cbabb3a570de5c699e19618d99 to your computer and use it in GitHub Desktop.
Random password generator
function getRandomCharacter(charset = "" ){
const randomIndex = Math.floor(Math.random() * charset.length);
return charset.charAt(randomIndex);
}
function generatePassword(length) {
const charsetAlphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$?";
// const charsetNumbers = "0123456789";
// const specialCharacters = "!@#$?"
let password = "";
for (let i = 0; i < length; i++) {
password += getRandomCharacter(charsetAlphabets);
}
return password;
}
// Usage example:
const password = generatePassword(15); // Generate a 15-character password
console.log(password);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment