How to generate an uniqe number always?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 35
Reputation: koushal.vv is an unknown quantity at this point 
Solved Threads: 1
koushal.vv koushal.vv is offline Offline
Light Poster

How to generate an uniqe number always?

 
0
  #1
Nov 22nd, 2008
Hi ,

can any one tell me how to generate an unique number ?
this can be done in C# with great ease. But i wonder how to generate tat in c++
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: How to generate an uniqe number always?

 
0
  #2
Nov 22nd, 2008
  1. int getUnique() {
  2. static int n = 0;
  3. return ++n;
  4. }

This function is not multithreading safe.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,576
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to generate an uniqe number always?

 
0
  #3
Nov 22nd, 2008
>this can be done in C# with great ease
Oh good. Show us how you do it in C# so we know what the hell you're talking about by a "unique number".
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: koushal.vv is an unknown quantity at this point 
Solved Threads: 1
koushal.vv koushal.vv is offline Offline
Light Poster

Re: How to generate an uniqe number always?

 
0
  #4
Nov 22nd, 2008
  1. Console.WriteLine(System.Guid.NewGuid().ToString());
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,136
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 530
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: How to generate an uniqe number always?

 
0
  #5
Nov 22nd, 2008
There is no such thing as a truly unique number, especially in computing. The seed is generally the time.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How to generate an uniqe number always?

 
0
  #6
Nov 22nd, 2008
http://msdn.microsoft.com/en-us/libr...stem.guid.aspx

A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.
How low of a probability are you looking for?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: koushal.vv is an unknown quantity at this point 
Solved Threads: 1
koushal.vv koushal.vv is offline Offline
Light Poster

Re: How to generate an uniqe number always?

 
0
  #7
Nov 22nd, 2008
i am storing some data in the database,
for tat i always need to send a unique value to it,
if the value already exists then an exception will be thrown.
so when ever i am storing in the db the value have to be unique.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: How to generate an uniqe number always?

 
0
  #8
Nov 22nd, 2008
Start your "unique number" at 1 and increment it for each new data record. That seems to be the easiest and safest way to make all your numbers unique.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: koushal.vv is an unknown quantity at this point 
Solved Threads: 1
koushal.vv koushal.vv is offline Offline
Light Poster

Re: How to generate an uniqe number always?

 
0
  #9
Nov 23rd, 2008
What if i run the program again, then again it starts with 1,
so i would get a exception
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: How to generate an uniqe number always?

 
0
  #10
Nov 23rd, 2008
then start at a value 1 more than the highest in the dB, if you wish to use incrementing type indexes. Or you could use the number of microseconds since (e.g.) 12:00am jan 1900 UTC as your 'unique number'.
Last edited by dougy83; Nov 23rd, 2008 at 2:39 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC