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...

Did you know?Explore Trending and Topic pages for more stories like this.
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 Like 8 Dislike
Previous forums Next forums
Other forums

Help with Hyperlink
Hi,

What I am trying to do may be simple, I just can't get it to work. Ok...

I am usin

want to add a break after a certain amount of characters
i was wondering how to add a break in a blog post after a certain amount of characters... any idea o

Disabling SAP menu
How do I disable SAP menu for all the users and just restrict them to User Menu in SAP ECC 6.0?

Form a inline view based on the results of the previous query?
Hi Experts,
Can we form a inline view on the results of a previous query (another inline view)?

Insert numbers from Barcode scanner into online php page
I have a real big problem, Here is what I'm trying to do. I have a Intermec CN3 handheld computer th

gettext translates ALWAYS?
I'm using gettext with the new version of XAMPP (5.3.0) on Windows Seven RTM (build 7600)
gettext

Target costs on Process orders not calculating
Hi All,

We have released standard costs for all the materials. We have also done Goods r

Little problem with form insertion in MySQL, Please help!!!
Hi!,

I'm a little bit new with php and I have a little issue here. I created a webform to ins

php multiple action on submit
I have a form which says:

<form enctype='multipart/form-data' method='post' action='pr

why is it over writing
Code: $filepaths[] = $_FILES['new_image'];
foreach ($filepaths as $filepath)
{
$imagename =

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash