Skip to content

Instantly share code, notes, and snippets.

@Kirill255
Created September 13, 2019 19:56
Show Gist options
  • Save Kirill255/2d08c1cdfa59df8b259b3a4e164d944f to your computer and use it in GitHub Desktop.
Save Kirill255/2d08c1cdfa59df8b259b3a4e164d944f to your computer and use it in GitHub Desktop.
const random = (length, chars) => {
let mask = "";
if (chars.indexOf("a") > -1) mask += "abcdefghijklmnopqrstuvwxyz";
if (chars.indexOf("A") > -1) mask += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (chars.indexOf("#") > -1) mask += "0123456789";
if (chars.indexOf("!") > -1) mask += "~`!@#$%^&*()_+-={}[]:\";'<>?,./|\\";
let result = "";
for (let i = length; i > 0; --i) {
result += mask[Math.round(Math.random() * (mask.length - 1))];
}
return result;
};
random(20, "aA#!");
// TdIXwXCO\Q#Rse68H:"O
console.log(random(20, "aA#"));
// 20ZomWHbTkPvMjXfSxE9
random(20, "A#");
// 271GLIZB4DD4NU3KVZM3
random(20, "aA");
// BoIkVWcMVcCDCohUBcLc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment