<html><body onLoad="document.MyForm.Length.focus()">

<!-- Solution 2 of Quiz 1 

(Using array_rand)

(1) Form a two-dimensional array $a whose first row is an array $a1 consisting of smallcase letters (indices 0 to 
	25), second row an array $a2 of uppercase letters (indices 0 to 25), and third row an array $a3 of the 
	numbers 0 to 9 (indices 0 to 9). The first row will have an index of 0, the second row an index of 1, 
	and the third row an index of 2. Note that $a[0] is $a1, $a[1] is $a2, and $a[2] is $a3. Here is how 
	to do that:

		$a1 = range('a','z');
                $a2 = range('A','Z');
                $a3 = range('0','9');
                $a = array($a1,$a2,$a3);


(2) Get a random row index (from 0 to 2) using mt_rand(0,2). Here is how:

	$k = mt_rand(0,2);

(3) Get a random column index using array_rand which is applied to one of the three arrays above.

	 $m = array_rand($a[$k]);

(4) Get the element of $a that has that random row and random column indices.

	$n = $a[$k][$m];

(5) Now concatenate the new character to the previous ones

                                $pass .= $n;

-->

<?php

echo <<< Begin

<center>

<table width=50%>
<tr><td><center>

<fieldset>
<B><legend>Password Generator</legend></B>
<FORM ACTION="{$_SERVER['PHP_SELF']}" METHOD="POST" name="MyForm">

Enter the length of the password: 
<input name="Length" size=30>
<P>

Enter the number of passwords:
<input name="Number" size=30>
<P>
<input name="submitted" TYPE="hidden" value="true">
<P>
<input type="submit" VALUE="SUBMIT"> 
<input type="reset" VALUR="RESET">
</form>
</fieldset>

Begin;

if (isset($_POST['submitted'])) // If the form is submitted
{
        $length = (integer) trim($_POST['Length']);
        $number = (integer) trim($_POST['Number']);
        echo "You requested: $number passwords each of length $length.\n<P>";
        if ($length > 0 && $number > 0)
        {
		$a1 = range('a','z');
		$a2 = range('A','Z');
		$a3 = range('0','9');
		/* Now form a two dimensional array whose first row is $a1, 
			second row is $a2, and third row us $a3.
			Note that rows are not uniform which means they 
			don't have the same number of elements (columns).
		*/
		$a = array($a1,$a2,$a3);
		//print_r($a[1]);
		echo "<B>Here they are:</B><P>\n";
                for ($i = 0; $i < $number; $i++)
                {
                        $pass = "";
                        for ($j=0; $j < $length; $j++)
                        {
				// Now get a random row index
				$k = mt_rand(0,2);
			
				// Now get a random column index
				$m = array_rand($a[$k]);

				/* Now get the element of $a that has that 
				row and column
				*/
                                $n = $a[$k][$m];

				// Now concatenate the new character to the previous ones.
                                $pass .= $n;
                        }
                        echo "<P>$pass\n";
                }
        }
        else
                echo "<P><B> You have to enter positive integers.<P>\n";
	echo "</center></td></tr></table></center>\n";
}

?>
</body></html>
