954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to generate an uniqe number always?

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++:ooh:

koushal.vv
Light Poster
35 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 
int getUnique() {
  static int n = 0;
  return ++n;
}


This function is not multithreading safe.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

>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".

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Console.WriteLine(System.Guid.NewGuid().ToString());
koushal.vv
Light Poster
35 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

There is no such thing as a truly unique number, especially in computing. The seed is generally the time.

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

http://msdn.microsoft.com/en-us/library/system.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?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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.

koushal.vv
Light Poster
35 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

What if i run the program again, then again it starts with 1,
so i would get a exception :(

koushal.vv
Light Poster
35 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

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'.

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

Never, ever trust in database client program supplied "unique numbers". A well-designed database must have properly defined unique counter fields in its tables and/or correspondent database procedures/triggers.
Let's forget home-made x-"databases" of 80-th ;)

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You