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

PHP Upload issue
Hi guys,

I have stumble across an interesting issue with my script and is doing my head in.

This is driving me nuts!
This insert query looks to be alright, however I get this error:

QuoteYou have an error in yo

my login script page is not working on remote computers
hi my login is working on my computer, but when i tried 2 computers from 2 different locations, they

Simple PHP/Java Help..
Hi,
I'm trying to build a little program to do with calculating postage. Unfortunately i haven't

I need help on this PHP code
Hi everybody, I made some kind of mistake while editing the code below. As you see, at the 3 row my

how do i display data on a page from mysql
1 - do i use this code at the top of each of my page i wish to only alow access if there as been a s

Confusing Functions
So, I've been reading up on Functions and I like them! However, I need some help using variables in

PHP/Database issue
My friend is helping me make a database where you go to a certain webpage of my site and the page wi

please help me in this update statment
hi every one

if I have table and this data in it

id name
10

Get Total From While Loop
I'm trying to get the total for each product and add them for a Grand Total to list outside the loop

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