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 03-25-2006, 03:25 AM   #1
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Rep: Reputation: 30
Generating 32-bit unique ID


My DB doesn't allow duplicates. But I need, rather than psedo-random numbers, to generate a 32-bit unique ID on Linux. I know someone has done this before. Does anyone know the link(s) of this software or of the similar kind?
Thanks
Jack

Last edited by lucky6969b; 03-25-2006 at 03:44 AM.
 
Old 03-25-2006, 09:53 AM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
How unique does it have to be (i.e. unique within a single system or unique among a set of systems)?
The simplest method: associate a counter with your database and increment it whenever you read its value. If you modify this counter from several concurrent threads, you have to lock it on every modification.
 
Old 03-25-2006, 03:41 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Which database are you using? Most databases (RDBMS's like mySQL, pgSQL, Oracle and MS-SQL) have an "auto increment" attribute that will automatically generate a unique primary key value for you each time you insert a new record.

Otherwise, maybe the easiest technique is simply to store your "last index" somewhere (an external text file; maybe even a table in your database itself), and then increment "last index + 1" as part of your standard "insert".

'Hope that helps .. PSM

PS:
Please do consider building a small "project library" and please do consider the books I mentioned in reply to your post last night!

PPS:
A proprietary system I once worked on had a custom database called "MKF" ("Multi-Keyed Files"; it was designed and invented in the mid 80's, around the time relational databases were beginning to take off), and it had a table called "scalar_numbers". Which just stored the current value of things like "the current highest document ID". It served precisely the kind of purpose you're looking for...

Last edited by paulsm4; 03-25-2006 at 03:44 PM.
 
Old 03-25-2006, 04:34 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Is a simple increment sufficient? auto increment.
Do you want the number to be evenly dispersed across the potential range? Hash table

Or something else? - please be more precise.
 
Old 03-27-2006, 08:59 AM   #5
Randux
Senior Member
 
Registered: Feb 2006
Location: Siberia
Distribution: Slackware & Slamd64. What else is there?
Posts: 1,705

Rep: Reputation: 55
Quote:
Originally Posted by lucky6969b
My DB doesn't allow duplicates. But I need, rather than psedo-random numbers, to generate a 32-bit unique ID on Linux. I know someone has done this before. Does anyone know the link(s) of this software or of the similar kind?
Thanks
Jack
real time clock?
 
  


Reply



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
unique upload system latheesan Linux - Newbie 1 06-26-2005 10:19 PM
Unique class names (C++) ToothlessRebel Programming 6 12-25-2004 12:16 AM
Bash Script- Finding/Generating unique UserIDs pheasand Linux - General 2 12-11-2004 09:44 AM
Opera: what makes it unique? r_jensen11 Linux - Software 4 06-12-2004 10:02 AM
Unique Set-up (at least I think it is) wolfhound Linux - Newbie 1 03-17-2004 03:48 PM

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

All times are GMT -5. The time now is 05:55 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