15,907,001 members
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
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.)
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.
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.

## Solution 6

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.

fjdiewornncalwe 19-May-11 10:47am
It is indeed an interesting read. Great find. +5
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.

## Solution 1

http://forums.asp.net/t/1170804.aspx[^]

Kiran

Guid is so long in size, I want upto 10 digit long.

## Solution 4

Sergey Alexandrovich Kryukov 18-May-11 15:45pm
This is good, but only if request stay apart in couple of milliseconds or so.
--SA

## Solution 5

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.

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

## Solution 2

VB.NET
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(999999999,9999999999)