note 27891 added to function.rand
| From: | php-general at lists dot php dot net | Date: | Mon, 23 Dec 2002 17:41:01 +0000 |
| Subject: | note 27891 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-41228@lists.php.net to get a copy of this message | ||
Further correction for random password function:
The comments in entis's code are incorrect for the second and third cases. They should be:
// [11,35] => [A,Y]
and
// [36,61] => [`,y]
to conform to what entis's code actually does; and the code would be a little clearer if the
third case read
Chr($randomNumber + 96 - 35)
though why anyone would want to use backtick in a password is beyond me. I'd stick to the
original version (by clev) if I wanted to do it this way. There is, after all, nothing wrong with
it.
Generally I find this approach more readable, though:
function RandomPassword( $passwordLength ) {
$passwordChars = '0123456789'
.'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.'abcdefghijklmnopqrstuvwxyz';
$password = "";
for ($index = 1; $index <= $passwordLength; $index++) {
// pick random number
$randomNumber = rand(1,strlen($passwordChars));
$password .= substr($passwordChars,$randomNumber-1,1);
}
return $password;
}
--
http://www.php.net/manual/en/function.rand.php
http://master.php.net/manage/user-notes.php?action=edit+27891
http://master.php.net/manage/user-notes.php?action=delete+27891
http://master.php.net/manage/user-notes.php?action=reject+27891