ToonMariner Posted November 11, 2009 Share Posted November 11, 2009 Hi guys n gals, I have a requirement to generate 250,000 unique codes... These codes are for a tracking service and will be stored in the database (table field has a unique index). Now I am working on the assumption this must work on the command line as the script will take a long time to complete... here is the salient pat of my code... <?php ... function appendToCode( &$val , $key , $append ) { $val = $val . $append; } $chars = "ABCDEFGHJKLMNPQRSTUVWXYZ"; $charend = strlen ( $chars ); $date = date ( 'md' ); $month = $date[0].$date[1]; $year = $date[2].$date[3]; $datecode = $chars[$month] . $chars[$year[0]] . $chars[$year[1]]; $codes = array(); $no_codes = isset( $_GET['no_codes'] ) ? $_GET['no_codes'] : 250000; $codecount = 0; do { for ( $j = $codecount ; $j < $no_codes ; $j++ ) { $trackcode = NULL; for ( $i = 0 ; $i < 7 ; $i++ ) { $trackcode .= $chars[rand( 0 , $charend - 1 )]; echo ( time() - $start ) . PHP_EOL; } $codes[] = $trackcode . $date; } //$t = count($codes); //echo 'Codes Generated: ' . $t . PHP_EOL; //echo 'Last Code: ' . $codes[--$t] . PHP_EOL; $codes = array_unique( $codes ); $codecount = count ( $codes ); //echo 'Unique Codes: ' . $codecount . PHP_EOL; // the following statement prevents infinite loop in dev mode... if ( (time() - $start) > 150 ) { break; } } while ( $codecount < ($no_codes) - 1 ); ... The last characters of the code relate to the date - as these codes will only be developed every now and then - not a frequent occurrence and will allow a check on the database to see if codes ending in that 3 character string exist... What I am asking is, is there a more efficient method of performing this operation. Any tips would be most welcome... Cheers peeps Toon xxx Quote Link to comment https://forums.phpfreaks.com/topic/181107-generating-random-codes/ Share on other sites More sharing options...
dreamwest Posted November 11, 2009 Share Posted November 11, 2009 chr() will give you random codes like "KILhh5jhd32" etc.. Quote Link to comment https://forums.phpfreaks.com/topic/181107-generating-random-codes/#findComment-955563 Share on other sites More sharing options...
ToonMariner Posted November 11, 2009 Author Share Posted November 11, 2009 Thanks dream west but chr requires a parameter to return a character so I'd simply be changing the source of possible characters in the string with one I have a little less control over (note I omit I and O from the valid characters string to avoid confusion with 1 and 0). I have been playing around with the code and no matter how many random strings I generate the call to array_unique always returns the same number (for 250000 random strings I get 32768 - this seems to be a limit) - Am I missing something? Quote Link to comment https://forums.phpfreaks.com/topic/181107-generating-random-codes/#findComment-955575 Share on other sites More sharing options...
dreamwest Posted November 11, 2009 Share Posted November 11, 2009 This will produce the codes you need using chr() function password_gen($min=6,$max=6,$id){ /*has a default min value of 6 and maximum value of 6*/ $pwd=""; // to store generated password for($i=0;$i<rand($min,$max);$i++){ $num=rand(48,122); if(($num >= 97 && $num <= 122)) $pwd.=chr($num); else if(($num >= 65 && $num <= 90)) $pwd.=chr($num); else if(($num >=48 && $num <= 57)) $pwd.=chr($num); else $i--; } return $pwd; } echo password_gen(6,6, 12 ); Quote Link to comment https://forums.phpfreaks.com/topic/181107-generating-random-codes/#findComment-955850 Share on other sites More sharing options...
dreamwest Posted November 11, 2009 Share Posted November 11, 2009 Also remember if u have a 7 digit code thats millions of combinations alone Quote Link to comment https://forums.phpfreaks.com/topic/181107-generating-random-codes/#findComment-955853 Share on other sites More sharing options...
ToonMariner Posted November 11, 2009 Author Share Posted November 11, 2009 there is so much more concatenation and logic checks there - I think it better to simply use $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $code = NULL; for ( $i = 0 ; $i < 7 ; I++ ) { $code = $chars[rand(0,25)]; } Quote Link to comment https://forums.phpfreaks.com/topic/181107-generating-random-codes/#findComment-955873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.