/***
#include <stdlib.h>
int rand(void);
void srand(unsigned int seed);
***/
#define MY_DEF_MAX_RANGE 999999
void fnUniqueKeyGenerator(char *sGuidKey)
{
struct timeval tv_now;
struct tm stLocalTimeR;
char sTemp[64];
double nTempRandVal = 0;
double nTempRandMax = 0;
double nTempRandDiv = 0;
double nTempRandMul = 0;
/* get current date time */
gettimeofday( &tv_now, NULL );
localtime_r(&tv_now.tv_sec, &stLocalTimeR);
srand( tv_now.tv_usec );
nTempRandVal = (double)rand(); /*** 0 ~ RAND_MAX ***/
nTempRandMax = (double)RAND_MAX; /*** 2147483647.000000 ***/
nTempRandDiv = nTempRandVal / nTempRandMax; /*** 0.000000 ~ 1.000000 ***/
nTempRandMul = nTempRandDiv * (double)MY_DEF_MAX_RANGE;
printf("RAND_VAL[%f] RAND_MAX[%f] 0_1_RANGE[%f] 0_X_RANGE[%f]\n", nTempRandVal, nTempRandMax, nTempRandDiv, nTempRandMul );
memset(sTemp, 0x00, sizeof(sTemp));
sprintf(sTemp, "%04d%02d%02d%02d%02d%02d%06d%06.0f", stLocalTimeR.tm_year+1900, stLocalTimeR.tm_mon+1, stLocalTimeR.tm_mday, \
stLocalTimeR.tm_hour, stLocalTimeR.tm_min, stLocalTimeR.tm_sec, \
(int)tv_now.tv_usec, \
nTempRandMul );
strcpy(sGuidKey, sTemp);
return;
}
실행결과 예시)
RAND_VAL[971492850.000000] RAND_MAX[2147483647.000000] 0_1_RANGE[0.452387] 0_X_RANGE[452386.158965]
20171013144405171148452386 len=26