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:
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:
int getUnique() {
static int n = 0;
return ++n;
}
This function is not multithreading safe.
>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".
Console.WriteLine(System.Guid.NewGuid().ToString());
There is no such thing as a truly unique number, especially in computing. The seed is generally the time.
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?
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.
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.
What if i run the program again, then again it starts with 1,
so i would get a exception :(
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'.
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 ;)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.