Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to generate 10 digit unique number, i have tried with date and time but it goes arround 14 digit. so please tell me how can i generate 10 digit unique number. Even if i restart the application it should maintain the uniqueness in the new generated number.
I can able to generate the 14 digit unique number. Is possible to scale down this 14 digit number into 10 digit unique number ?
Posted
Updated 18-May-11 22:56pm
v3
Comments
Niklas L 18-May-11 8:24am    
Your question makes no sense. Unique compared to what? Start with 1000000000 and add 1 each time. That will give you all possible ten digit unique numbers. (A 32 bit representation is not enough, so pick a 64 bit integer.)
LaxmikantYadav 18-May-11 8:51am    
In this solution i have stored last number. And after generation of new number again have to update the last stored number which is not fixible. I want to generate number like using datetime but i want it upto 10 digit.
I think Niklas Lindquist given a easy solution. you will have only one 10 digit number and whenever you want to generate a number just increment it as uniqueNumber++;There is no need of updating last number.
LaxmikantYadav 18-May-11 9:43am    
Suppose i generate 10 number using niklas's logic and exit from application then on next run how can i generate unique number without storing previous unique number.
this is the real requirement. This has to be mentioned in your question.So the requirement is that you have to generate a 10 digit unique number,Like a roll number for a student,right.

This might help a little: Link[^]

The post describes the GUID generation algorithm and how it can be modified for specific situations to be shorter. Interesting read.
 
Share this answer
 
Comments
fjdiewornncalwe 19-May-11 10:47am    
It is indeed an interesting read. Great find. +5
LaxmikantYadav 26-May-11 0:39am    
Guid is so long, I want upto 10 digit long.
tolw 26-May-11 3:51am    
That's why i suggested READING the article I linked to and NOT using GUID. At the end of the article there is a real live example of how to create your own guid-like id which can be shorter then the real guid, but has some specific limitations.
Look at the below link. This may help you
http://forums.asp.net/t/1170804.aspx[^]

Kiran
 
Share this answer
 
Comments
LaxmikantYadav 18-May-11 8:47am    
Guid is so long in size, I want upto 10 digit long.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-May-11 15:45pm    
This is good, but only if request stay apart in couple of milliseconds or so.
--SA
I don't think using a combination of date and time is the right one, since the smallest unit you can get is millisecond.

I would go with GUID. But if you insist that it has to be done with time, than I think a combination of GetTickCount[^] (beware of its 49.7 days limitation or better GetTickCount64[^]) and a rand()[^] would be sufficient.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-May-11 15:45pm    
GUID is not 100% guarantee.
--SA
Stephen Wiria 18-May-11 19:11pm    
True, I believe unique here means practically unique.
But then again, I don't think he could get a better and easier solution than GUID
LaxmikantYadav 19-May-11 0:17am    
GUID is greater than 10 digit. and if i use GetTickCount it is fine for first time. once i restart the pc it will again give me the same number. so uniqueness of the numbers can not be maintain.
Stephen Wiria 19-May-11 1:51am    
Yes, GetTickCount gives system uptime in milliseconds.
Right now, I can't give you better solution other than GUID. Sorry.
VB.NET
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(999999999,9999999999)
 
Share this answer
 
Comments
LaxmikantYadav 18-May-11 8:52am    
I want in C++, And in this way we cant maintain the uniqueness of numbers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900