My guess is that you would need to create the random number and then check
that number against a database of saved "random" numbers, to insure
uniqueness.  If someone would like to critique it, please do

I wrote this just for you because I was curious...

<?php

//holder for the final random thingy
$final_rand = "";

//gotta seed the random number generator, although I have no idea what this
does
srand ((double) microtime() * 10000000);

//a little alpha-num array
$alpha = array ("A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" ,
"K" , "L" , "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "X"
, "Y" , "Z" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "0" );

//get 8 keys for the array
$rand_keys = array_rand ($alpha, 8);

//cycle through the random keys and build the thingy
for ( $i = 0 ; $i <= 7 ; $i++ ) {
    $final_rand .= $alpha[$rand_keys[$i]];

        //throw a dash in there
        if ( $i == 3 ) {
                $final_rand .= "-";
        }

}

echo $final_rand;

//now you need to check the final value against a database or a comma
delimited text file or something where you can check to see if the item has
ever been used before and if not, added to the end of the database

?> 

---------------------
John Asendorf - [EMAIL PROTECTED]
Web Applications Developer
http://www.lcounty.com - NEW FEATURES ADDED DAILY!
Licking County, Ohio, USA
740-349-3631

The benefit to the government of replacing all $1 Federal Reserve notes with
$1 coins would be $522.2 million per year, according to estimates of the
General Accouting Office released on April 7, 2000.


> -----Original Message-----
> From: Sigurd Magnusson [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, January 23, 2001 3:59 AM
> To: [EMAIL PROTECTED]
> Subject: [PHP-WIN] Random Code
> 
> 
> Whats the best way to make a completely random, unique code 
> in the format "ABCD-1234" where each of the 8 characters can 
> be alphanumerc; 0-9,A-Z... I thought about generating a 36^4 
> number, converting to to base '36', and repeating the 
> process. What's a good way to ensure the code is not repeated, though?
> 
> Siggy
> 

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to