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 Like 8 Dislike
Previous forums Next forums
Other forums

getting most records by count
Code: [Select]<?php

$connect = mysql_connect("localhost","dam

animation progress while uploading files?
Hello, i have some website form to upload few files to server. i want to put some gif animation whi

Need PHP help... have all the elements... just need help putting them together :
Hey Guys,

I was wondering if someone could help me. I am doing a website for a client which

generate seo frienly url from a text like wordpress
Convert: "åäö" to "aao", "space" to "-", "!?&q

file_put_contents and createimagefromjpeg
All,
I do somethings and then I have the following code:
file_put_contents('./test/'.$filename

Multiple forms on the same page (safari)
Hello everyone,

I have 3 forms on the same page, that opens in a new window and submits to a

How to return to a previous page after running a PHP script
I'm having a bit of a melt down here because I think this should be really easy but can't work it ou

Rand() help needed
Hi all,

Can someone explain and give me a quick example of how I would go about this?

Storing Values taken from a DB...
Using this code it will generate a short list from my database

<?php
mysql_connect

Calling variables
I want to call this to my website but it doesn't display what I want it to,

Code: <h5&

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