commit e079e82c087becf06d902089d14f3f76686bde19
Author: Ankit Kumar Agarwal <ankit_frenz@yahoo.co.in>
Date:   Thu Nov 10 13:46:43 2011 +0530

    MDL-29893 Fixing issues with password generation function

diff --git a/lib/moodlelib.php b/lib/moodlelib.php
index fee5324..b5ea458 100644
--- a/lib/moodlelib.php
+++ b/lib/moodlelib.php
@@ -8725,12 +8725,17 @@ function generate_password($maxlen=10) {
         $filler1 = $fillers[rand(0, strlen($fillers) - 1)];
         $password = $word1 . $filler1 . $word2;
     } else {
-        $maxlen = !empty($CFG->minpasswordlength) ? $CFG->minpasswordlength : 0;
+        $minlen = !empty($CFG->minpasswordlength) ? $CFG->minpasswordlength : 0;
         $digits = $CFG->minpassworddigits;
         $lower = $CFG->minpasswordlower;
         $upper = $CFG->minpasswordupper;
         $nonalphanum = $CFG->minpasswordnonalphanum;
-        $additional = $maxlen - ($lower + $upper + $digits + $nonalphanum);
+        $total = $lower + $upper + $digits + $nonalphanum;
+        // minlength should be the greater one of the two ( $minlen and $total )
+        $minlen = $minlen < $total ? $total : $minlen;
+        // maxlen can never be smaller than minlen
+        $maxlen = $minlen > $maxlen ? $minlen : $maxlen;
+        $additional = $maxlen - $total;
 
         // Make sure we have enough characters to fulfill
         // complexity requirements
