944,005 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1979
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 22nd, 2008
0

How to generate an uniqe number always?

Expand Post »
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++
Reputation Points: 10
Solved Threads: 1
Light Poster
koushal.vv is offline Offline
35 posts
since Oct 2008
Nov 22nd, 2008
1

Re: How to generate an uniqe number always?

C++ Syntax (Toggle Plain Text)
  1. int getUnique() {
  2. static int n = 0;
  3. return ++n;
  4. }

This function is not multithreading safe.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Nov 22nd, 2008
1

Re: How to generate an uniqe number always?

>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".
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 22nd, 2008
0

Re: How to generate an uniqe number always?

C++ Syntax (Toggle Plain Text)
  1. Console.WriteLine(System.Guid.NewGuid().ToString());
Reputation Points: 10
Solved Threads: 1
Light Poster
koushal.vv is offline Offline
35 posts
since Oct 2008
Nov 22nd, 2008
0

Re: How to generate an uniqe number always?

There is no such thing as a truly unique number, especially in computing. The seed is generally the time.
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,522 posts
since Apr 2005
Nov 22nd, 2008
0

Re: How to generate an uniqe number always?

http://msdn.microsoft.com/en-us/libr...stem.guid.aspx

Quote ...
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?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 22nd, 2008
0

Re: How to generate an uniqe number always?

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
koushal.vv is offline Offline
35 posts
since Oct 2008
Nov 22nd, 2008
0

Re: How to generate an uniqe number always?

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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Nov 23rd, 2008
0

Re: How to generate an uniqe number always?

What if i run the program again, then again it starts with 1,
so i would get a exception
Reputation Points: 10
Solved Threads: 1
Light Poster
koushal.vv is offline Offline
35 posts
since Oct 2008
Nov 23rd, 2008
0

Re: How to generate an uniqe number always?

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.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with C++
Next Thread in C++ Forum Timeline: ifstream and eof





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC