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
Problem with php's rename function
So, i'm having a problem with the php rename function. Basically my script 'delete_user.php' attempt
Check if another session of the page is running?
I want to keep people from opening multiple tabs, or sessions of my Facebook app. Is it possible to
Php Mysql Page Loading Notice
Hi all,
I was wondering if there is a way (Sure there is) of showing a message or an image wh
Line break?
Hi, I'm new to the forum and new to php. I'm not sure if I'm using the correct terminology so here i
Export hangs
Hi all, please help
I have a Oracle 10.2.0.3.0 database. When I want to query the dba_segmen
PHP SUBMIT
Code: <input name="doLogin" type="image" src="images/loginsubmit.jpg
How to get the previous months last date....
Here's my wittle problem. I suck at working with dates.
So today is 10/20/2009, i need to fig
DOMDocument
Hello All,
I need to take an XML document, modify some of the nodes in it and then perform a
Display search result
Hi!
I have a SQL database with information about albums and track (music).
This is wh
Displaying Multidimensional Arrays...about to put my head in a vice, PLEASE HELP
I am fairly new to PHP and MySQL and I have a problem which I have been trying to resolve for days.