The following code was created to generate a random password. But it can be used to generate a random string for any purpose.
The basic idea was to create an array that would contain all the letters of the English alphabet, and pick out random values from that array. I have outlined two methods below, please also read the explanation/comments provided below. Method 1: $strrandom=; $ivalfrom =ord("a"); $ivalto =ord("z"); for($i=$ivalfrom; $i<=$ivalto; $i++) $arralpha[] = chr($i); mt_srand ((double) microtime() * 1000000); $rand_al = array_rand ($arralpha, 3); $strrandom = $arralpha[$rand_al[0]] . $arralpha[$rand_al[1]] . $arralpha[$rand_al[3]]; $numpart=mt_rand(10,99); $strrandom .= $numpart; echo "random = $strrandom"; ---------------------------------------------------------------------------- --------------------- Method 2: $strrandom=; $ivalfrom =ord("a"); $ivalto =ord("z"); for($i=$ivalfrom; $i<=$ivalto; $i++) $arralpha[] = chr($i); mt_srand ((double) microtime() * 1000000); $strrandom = $arralpha[mt_rand(0,25)].$arralpha[mt_rand(0,25)].$arralpha[mt_rand(0,25)]; $numpart=mt_rand(10,99); $strrandom .= $numpart; echo "random = $strrandom"; ---------------------------------------------------------------------------- --------------------- Explanantion: /* I used a for loop to populate the array with the alphabets in lowercase (to increase the number of values, you could probably insert uppercase alphabets too). */ //first populate array with alphabets $ivalfrom =ord("a"); $ivalto =ord("z"); //add the alphabets to the array for($i=$ivalfrom; $i<=$ivalto; $i++) $arralpha[] = chr($i); /* $arralpha is an array variable which refers to the array of 26 alphabets. */ /* I seed the random generator */ mt_srand ((double) microtime() * 1000000); /* I wanted my string to contain 3 random letters. I use the array_rand() function, which takes as input an array, and the number of random values desired (which is 3 here, but it could be 26 too, equal to or less than the size of the array). It returns an array, which contains indexes taken at random from the array passed as the argument.*/ $rand_al = array_rand ($arralpha, 3); /* Here, $rand_al is another array variable which contains random indexes */ $strrandom=; $strrandom = $arralpha[$rand_al[0]] . $arralpha[$rand_al[1]] . $arralpha[$rand_al[3]]; /* In the above statement, the values in the $rand_al are really subscripts of the alphabet array, so I use them as subscripts to finally arrive at the random string. You could iterate through the generated $rand_al using a loop to get the subscripts */ /* Please note that array_rand() will work for PHP4 and above, it may not return random values if you do not seed the generator first, and it may still create a problem on some windows machines (a bug, which has been fixed on PHP 4.2.2 dev version). In such a case, use Method 2. */ /* The two lines below add a 2-digit number to the random string. */ $numpart=mt_rand(10,99); $strrandom .= $numpart; -Naintara -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] t]On Behalf Of Liam MacKenzie Sent: Thursday, July 25, 2002 1:04 PM To: [EMAIL PROTECTED]; Monty Subject: Re: [PHP] Password Generator Script <? $password = substr(ereg_replace("[^A-Za-z0-9]", "", crypt(time())) . ereg_replace("[^A-Za-z0-9]", "", crypt(time())) . ereg_replace("[^A-Za-z0-9]", "", crypt(time())), 9, 10); ?> Random Password : <b><? echo "$password"; ?></b> In action: http://scripts.operationenigma.net/passgen.php Have fun :-) ----- Original Message ----- From: "Monty" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday, July 25, 2002 2:49 PM Subject: [PHP] Password Generator Script > Can anyone recommend where I could find a decent script that automatically > generates passwords? I don't care if they are readable or just random > letters, numbers. > > Thanks! > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php