Random password generator with JavaScript


In JavaScript To generate password use the following function.

function generatePW()
{
var i=0;
var s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_?/:(){}[]0123456789";
var pw="";
var c=(Math.random() * 3)+9;
for (i=0; i<c; i++)
{
pw+=s.charAt(Math.random() * 72);
}
return(pw);}

Here the JavaScript function Math.random() generates a random number from 0 to 1 (actually just less than 1). So the code:
var c=(Math.random() * 3)+9; will generate a number between 9 to just less than 12.

Advertisements

Random password generator with PHP


function generate_password($length='')
{
$str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_?/:(){}[]0123456789';
$max=strlen($str);
$length=@round($length);
if(empty($length)){$length=rand(8,12);}
$password='';
for($i=0; $i<$length; $i++){$password.=$str{rand(0,$max-1)};}
return $password;
}

In the above code the PHP function generate_password() takes one optional argument, the length of the password. If you do not specify the length then this function will return a random password of length from eight to twelve characters. PHP variavle $str hold the characters to be used to generate password. You can edit this string according to your need.