Skip to content

Instantly share code, notes, and snippets.

@isu3ru
Created November 17, 2020 12:04
Show Gist options
  • Save isu3ru/9ddaa521939ccfc833f8b639b04c705e to your computer and use it in GitHub Desktop.
Save isu3ru/9ddaa521939ccfc833f8b639b04c705e to your computer and use it in GitHub Desktop.
Simple function to generate a random text that can be used as a password in most cases
/**
* Generate Password Function
* A simple javascript function to generate a random text that can be used as a password in most cases
* NOTES: Does not contain special characters. Only numbers 0 (zero) to 9 (Nine) and English alphabet letters.
*
* @author Isuru Ranawaka ([email protected])
*/
function generatePassword() {
// Numbers and alphabet characters
const characters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "]", "^", "_", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
// length of the password
const passwordLength = 12; // customize to your need
let password = '';
for (let i = 0; i < passwordLength; i++) {
// grab a random character
let index = Math.floor(Math.random() * Math.floor(characters.length));
// add to the password text
password += characters[index];
}
// return the collected text
return password;
}
/*
Example Outputs
---------------
YznRn8d]^1r1
Hjw1VbFk0cCy
q5CjmKpFRNV3
e3pn2Ao9G2ER
cV[TRo5hFUna
2O]oy_PLpoOl
UarWq6ee40[d
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment