LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2011, 06:30 AM   #1
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Rep: Reputation: 32
Question Unique Sequence Number Generator in C


Hi,
Requirement:
I need to create some unique key once in every 10 sec.

My C code is in a loop, and after every 10 sec I have to generate some unique new key. The key must be 16byte hex code (e.g FFFF.FF01.FF35.1234). How can I achieve this ?


I can think of some approaches like:

1. generating random nos using srand(time(0)) and then separating by character "." .

2. Using machine time stamp, some algorithm to be written, as time stamp is always unique.



Any help will be appreciated ?

Thanks,
Barun Parichha
 
Old 02-10-2011, 08:19 AM   #2
BitBuster
LQ Newbie
 
Registered: Feb 2011
Posts: 6

Rep: Reputation: 0
Lightbulb

Hi Barun Parichha,

This is not my field of expertise but I may be able to help?

I assume that you are looking for a 16-digit (not 16 byte) hex number in 4x2 byte decimated hex notation to denote a unique number? I also assume that this unique number MUST be unique from all other numbers generated? Something like a serial number generator, but in 4x2 hex decimated notation.

Your idea of using a random number generator, seeded by current time, although innovative, I don't believe will guarantee you a unique number, with respect to numbers previously generated.

Your second suggestion would be the method that I would consider, especially if you have the flexibility of 8 bytes (16 hex digits) to play with. Providing the unique number is only ever generated on a single system where you can guarantee that the clock will always be accurate in terms of ‘real world’ time, timezone and daylight savings etc, this should guarantee you a unique number. Before I go further, 2 explanations are required:
1. Single system: If you have multiple systems generating a number based purely on time, there is always the chance that both systems may generate the same number. If you are using multiple systems, you will need to consider this and perhaps incorporate a unique feature of each system into the number generation equation, such as MAC address etc.
2. Real world time: Should the system generating this unique number, based on time, not be accurate with its time keeping, there is the risk of producing duplicate numbers. Also be aware of daylight savings and its time transitions!

Assuming a single system with good clock keeping skills, no daylight saving, single timezone and delays of at least 10 seconds, to get a simple 4 bytes of 'unique' hex output, my approach might be:

#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>

void stoupper (char *src) {
// Convert chars in string to upper case
while (*src != '\0')
*(src++) = toupper (*src);
}

int main (int argc, char *argv[]) {

struct timeval tv;
struct timezone tz;
char seq[6];

union {
unsigned int t;
unsigned char ct[4];
} u;

gettimeofday (&tv,&tz);

u.t=(unsigned int)tv.tv_sec;
// Number of seconds since the Epoch – 1 Jan 1970 00:00:00

// Write time as 2x4 byte hex values
// Include byte-reversal for Intel architecture
sprintf (seq,"%02x%02x.%02x%02x",(unsigned char)u.ct[3],(unsigned char)u.ct[2],(unsigned char)u.ct[1],(unsigned char)u.ct[0]);
// Convert the hex values to upper case
stoupper (seq);
printf ("8bit hex time = (%s)\n",seq);
return (0);
}

You still have 4 bytes to use if you wish to incorporate multiple systems, time issues etc.

I hope that helps,
BitBuster.
 
Old 02-11-2011, 02:02 AM   #3
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Original Poster
Rep: Reputation: 32
Modified code

Thanks BitBluster !

Used for loop to check the uniqueness of ID generated.
Code:
#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>
#include <stdlib.h>

// Convert chars in string to upper case
void stoupper (char *src) {
        while (*src != '\0')
                *(src++) = toupper (*src);
}

int main (int argc, char *argv[]) {
        struct timeval tv;
        struct timezone tz;
        char seq[20];
        int i;

        for (i=0;i<10;i++)
        {
                gettimeofday (&tv,&tz);
                srand(tv.tv_usec);
                sprintf (seq,"%x.%x.%x",tv.tv_sec, tv.tv_usec, rand());
                stoupper (seq);
                printf ("\n%13s",seq);
        }
        return (0);
}
Output :

Code:
4D54EBA9.70660.752F
4D54EBA9.70AC9.8BF
4D54EBA9.70AF4.150E
4D54EBA9.70B1C.5C0A
4D54EBA9.70B41.5DB3
4D54EBA9.70B67.2123
4D54EBA9.70B9D.F7
4D54EBA9.70BC6.9BA
4D54EBA9.70BEB.B63
4D54EBA9.70C11.4ED3
 
Old 02-11-2011, 10:16 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Code:
                sprintf (seq,"%x.%x.%x",tv.tv_sec, tv.tv_usec, rand());
                stoupper (seq);
You can use
Code:
sprintf (seq,"%X.%X.%X,..."
to get the number in uppercase directly.
 
Old 02-11-2011, 10:42 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by barunparichha View Post
unique 16byte hex code
There's a technical name for that kind of code. It's called a universally unique identifier, or, for short, a uuid. Now that you know what to look for, I recommend researching uuids for a bit. Start with the Wikipedia article.

If you're using Linux, then you have the libuuid library at your disposal. It's part of the e2fsprogs package, which is standard on every installation. Look up the uuid_generate manpage.

Last edited by dugan; 02-11-2011 at 10:56 AM.
 
1 members found this post helpful.
  


Reply

Tags
[c/c++]



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating a sequence of datetimes in fields with unique contrains Softsmid Programming 7 03-15-2010 11:22 PM
I need a random number generator newbiesforever Linux - Software 5 05-22-2009 02:04 PM
counting the number of unique items in a list UnixKiwi Programming 4 11-29-2008 02:49 PM
random number generator vkmgeek Programming 3 09-02-2008 09:41 AM
Number series generator sniff Programming 8 08-23-2005 03:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration