ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303
Rep:
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.
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:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.