generating random codes
Posted on
16th Feb 2014 07:03 pm by
admin
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...
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.
No comments posted yet
Your Answer:
Login to answer
179
8
Other forums
Undefined index on my form
ok im getting Undefined index on this line.. print_r($_REQUEST['form']);
below is the full sc
Disable Scrollbar in TableControl
Hello everbody,
please can anybody help me - outherwise i'm going to become desperate :P<
Compare user input to flat file data
Help...Am a complete newbie to programming so my code is prolly quite long. Am trying to verify a us
Delete HTML file after loading
I have limited experience with php and its been a year or two since I've last used it. I have a sma
Problems with returning true or false in eval()'d code
Hi guys,
Would appreciate some help with a problem when running eval() on a function that sho
Posting Serialized Data Not Working
I am trying to send an object from one PHP file to another using POST. I serialize the object and th
Converting RGB values to HEX
Code: <?PHP
$file_handle = fopen("colors/rgb.csv", "r");
wh
Post PHP form to non-existing page
The company that I work for host their PHP code on IIS on a Windows server, no Apache, so no mod_rew
Random date selection
i want to select random date with time(hour+minutes+sec) where date is specified(10/22/2009) and tim
mail() says sent but no email received
My code is quite simple:
Code: <?php
$to = "Ty44ler@yahoo.com";
$subje