One method would be to create a string with all possible characters and generate a random number from 0 to length of the string and use the random number for an index into the string.
Another method, and the one I use, I got from a tutorial on e-comerence systems and has a function like this:
/ based on mymarket.php (c) 2000 Ying Zhang (ying@zippydesign.com) /
function generate_password($maxlen=10) {
/ returns a randomly generated password of length $maxlen. inspired by
http://www.phpbuilder.com/columns/jesus19990502.php3 */
global $CFG;
$fillers = "1234567890!@#$%&*-_=+^";
$wordlist = file($CFG->wordlist);
srand((double) microtime() * 1000000);
$word1 = trim($wordlist[rand(0, count($wordlist) - 1)]);
$word2 = trim($wordlist[rand(0, count($wordlist) - 1)]);
$filler1 = $fillers[rand(0, strlen($fillers) - 1)];
return substr($word1 . $filler1 . $word2, 0, $maxlen);
}
The only other requirement is that you have a file with a list of words in it.