insufficient algorithm

S

Sameh Halabi

Hello .
I'm trying to create a 12 digit number which will be as much unique as
possible . for that I created 2 algorithms , the first one (which I
bring here first ) gave a max 11 digit number with output numbers
repeating themselves of about 20 times for a million tries .
then I created another algorithm which gave me an exact 12 digit
number with numbers repeating themselves of 0 times for a million
tries , but the problem with it was that it was about 6 times slower
than the first . performance is important for me . do anybody have any
idea how to make the 2nd algorithm more sufficient or to reduce
duplicates in the 1st one :

first algorithm :
=================
#define SEQ_L 9
#define FTRCD_C_L 6
#define DATE_C_L 8

int Mpgn_gt_hash_seq (unsigned long i_soc_seq_no,
char * i_feature_code,
char * i_eff_date,
unsigned long *o_calculated_seq)
{
char sKey[SEQ_L+FTRCD_C_L+DATE_C_L+1];
char* sKeyPtr;
double nHash=0;
double maxNum=999999999999;


memset(sKey,0,SEQ_L+FTRCD_C_L+DATE_C_L+1);

/* Make sure i_feature_code is padded with trailing spaces
to ensure consistency */
for (int i=0; i<FTRCD_C_L ; i++)
if (i_feature_code<'!' || i_feature_code>'z')
i_feature_code=' ';

/* Format sKey - copy input params as strings*/
sprintf (sKey,"%.9d%.6s%.8s",i_soc_seq_no, i_feature_code,
i_eff_date);

/* Hash Algorithm */
nHash=*sKey;
sKeyPtr=sKey;
while (*sKeyPtr++)
{
nHash=((unsigned long)nHash<<7) + nHash + *sKeyPtr;

/* Ensure 12 digit value */
if (nHash>maxNum)
{
nHash=nHash-(maxNum*(unsigned long)(nHash/maxNum));
}
}

*o_calculated_seq=(long)nHash;
return SUCCESS;
}

in this algorithm I had also a problem with compilation getting
warnings for line << if (nHash>maxNum) >> , that variable exceeds max
size of C .

second algorithm :
==================
#define SEQ_L 9
#define FTRCD_C_L 6
#define DATE_C_L 8

int Mpgn_gt_hash_seq (unsigned long i_soc_seq_no,
char * i_feature_code,
char * i_eff_date,
unsigned long *o_calculated_seq)
{
char sKey[SEQ_L+FTRCD_C_L+DATE_C_L+1];
char* sKeyPtr;
double nHash=0;
char strNum[SEQ_L+FTRCD_C_L+DATE_C_L];
int len=0;


memset(sKey,0,SEQ_L+FTRCD_C_L+DATE_C_L+1);

/* Make sure i_feature_code is padded with trailing spaces
to ensure consistency */
for (int i=0; i<FTRCD_C_L ; i++)
if (i_feature_code<'!' || i_feature_code>'z')
i_feature_code=' ';

/* Format sKey - copy input params as strings */
sprintf (sKey,"%.9d%.6s%.8s",i_soc_seq_no,i_feature_code,i_eff_date);

/* Hash Algorithm */
nHash=*sKey;
sKeyPtr=sKey;
sprintf(strNum,"%.0f\0",nHash);
while (*sKeyPtr++)
{
nHash=((unsigned long)nHash<<7) + nHash + *sKeyPtr;
sprintf(strNum,"%.0f\0",nHash);
/* Ensure 12 digit value */
len = strlen(strNum);
if (len>12)
{
strncpy(strNum,&strNum[len-12-1],12);
}
nHash = atof(strNum);
}

*o_calculated_seq=(long)nHash;
return SUCCESS;
}

as I said , this algorithm worked fine , but it was much slower than
the first .

please your assistance .

Thank you very much
Sameh .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top