function rand_string( $length ) {
$str = "";
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$size = strlen( $chars );
for( $i = 0; $i < $length; $i++ ) {
$str .= $chars[ rand( 0, $size - 1 ) ];
}
return $str;
}
//and call the function this way:
$mypass = rand_string(10);
Math.random() // Generate random number, eg: 0.123456
.toString(36) // Convert to base-36 : "0.4fzyo82mvyr"
.slice(-8);// Cut off last 8 characters : "yo82mvyr"
from random import randint
def create_random_chars(charCount):
return "".join(chr(randint(33,126)) for i in range(charCount))
print(create_random_chars(10))
# Use Source to compile
var btn = document.getElementById("btn");
btn.addEventListener("click", function generate() {
var generatePassword = "";
var password = document.getElementById("password");
var characters =
"abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*?";
for (var i = 0; i < 10; i++) {
generatePassword += characters.charAt(
Math.floor(Math.random() * characters.length)
);
}
password.innerHTML = generatePassword;
});
const generatePassword = (length = 10, specialChars = true) => {
const alphaCodesArray = Array.from(Array(26)).map((e, i) => i + 65);
const uppercaseAlphabetArray = alphaCodesArray.map((letterCode) => String.fromCharCode(letterCode));
const lowercaseAlphabetArray = uppercaseAlphabetArray.map(e => e.toLowerCase());
const uppercaseAlphabet = [...uppercaseAlphabetArray].join('');
const lowercaseAlphabet = [...lowercaseAlphabetArray].join('');
const numbers = [...Array(10).keys()].join('');
const specialSymbols = typeof specialChars === 'string' ? specialChars : (specialChars ? "!@#$%^&*?" : "");
const characters = `${lowercaseAlphabet}${numbers}${uppercaseAlphabet}${specialSymbols}`;
return [...Array(length).keys()].reduce(acc => acc.concat(characters.charAt(Math.floor(Math.random() * characters.length))), '');
}
public string CreatePassword(int length)
{
const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890<>.,/@#$%^&*()_+!:";
StringBuilder res = new StringBuilder();
Random rnd = new Random();
while (0 < length--)
{
res.Append(valid[rnd.Next(valid.Length)]);
}
return res.ToString();
}