Random Unique number

Hi,

I want to generrate a 10 digit random unique number.
How can i do this?

Thanks
shahjagatAsked:
Who is Participating?
 
Bill-HansonConnect With a Mentor Commented:
The main problem with your 10-digit limitation is that there are not nearly enough combinations available to ensure that the number is unique.  10^10 = 10000000000.

If you include alpha characters A-Z, and increase the field length to 32, you get alot more combinations - enough to be considered 'unique'.  32^36 = 1.5324955408658888583583470271503e+54.

Your best bet is to use Guid.NewGuid() as I mentioned above because the id is generated using the machine's MAC address as a seed, so it's unique.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
What do you mean unique?
0
 
Ramkisan JagtapLead DeveloperCommented:
u can use math.random()
Or GUID Class to generate that number
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Bill-HansonCommented:
Does it have to be 10 digit?

This will generate a unique id as long as the machine has a network card...

Guid.NewGuid()
0
 
Anil GolamariCommented:
Try below code.
using System;
using System.Collections;
using System.IO;
namespace ConsoleApplication5

class Numbers
{

public ArrayList RandomNumbers(int max)
ArrayList lstNumbers = new ArrayList();
{
 Random rndNumber = new Random();
int number = rndNumber.Next(1, max + 1);
lstNumbers.Add(number);

 int count = 0;

do 
{
number = rndNumber.Next(1, max + 1);
{
if (!lstNumbers.Contains(number))// ... add it


lstNumbers.Add(number);

} 
count++;

}


while (lstNumbers.Count <= 10);

return lstNumbers;

}

}

 

class Program


{

static int Main()

{

Numbers nbs = new Numbers();

const int Total = 1000000;

ArrayList lstNumbers = nbs.RandomNumbers(Total);

TextWriter tw=new StreamWriter(@"c:\\xx.txt"); for (int i = 0; i < lstNumbers.Count; i++)//Console.WriteLine("{0}", lstNumbers[i].ToString());


tw.WriteLine(lstNumbers[i].ToString());

 return 0;

 }

}
 }

Open in new window

0
 
Bill-HansonCommented:
I see alot of random number generators listed.

Remember, people...  Random does not equal unique.  Be careful!
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
Well put Bill.

shahjagat, what do you intend to do with that number. Why does it have to be a number? Why does it have to be 10 digits. Unique to what level or compared to what?

A GUID is usually the best method to get a unique identifier.

If you tell us what you want to do with it, we might suggest the best solution.

If you absolutely need 10 digits, here is the code, but it might not be unique:

long x;
long y1;
long y2;

Random rnd= new Random((int)DateTime.Now.Ticks);

y1 = System.Math.Abs ( rnd.Next ( 10000, 99999 ) );
y2 = System.Math.Abs ( rnd.Next ( 10000, 99999 ) );
x= y1 + y2*100000;

Open in new window


If you would rather use the code from lucky 85, change Random rndNumber = new Random(); to something such as mine, otherwise you will always end up with the same set of values.
0
 
shahjagatAuthor Commented:

Hi Bill, James,

I want it to be unique not necessarily a 10 digit preferably a number and not a alphanumeric.
We used a random number generator in one of our applications but in that case we had t oconnect to the database and verify that the generated value does not exist in the database.
To avoid this additional step, i want to generate a unique number.(Preferably a number if that does nto work out then i will think of using alpha numeric).
0
 
wdosanjosCommented:
As already mentioned, the best/easiest way to generate an ID that's unique without having to check its existence in your database is to use Guid.NewGuid().  Guid translates to a uniqueidentifier column data type in SQL Server.
0
 
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
I would go with wdosanjos.

You would never be able to get a really unique value unless you keep verifying in the database. And although your idea of a random 10 digits keeps the odd very low, it might not be sufficient. For you to decide.

Here is why.

A real random generator does not exists. All the random generators in existence send out a list of numbers following a specific algorithm. They will all start sending back the same sequence after a while. Might be a long sequence, but it gets repeated even if all the possible values have not been exhausted. In fact, by default, the sequence is the same everytime you start it. That is why it is important to seed the generator, by giving it a changing value as I did with DateTime.Now.Ticks in my previous example. It uses that value to jump somewhere in the middle of the sequence. That way, you always start at a different place in the sequence.

With this first limitation in mind, you have only a certain number of permutations possible between 10 digits. If real random generators existed, you would have 1 change in 1,000,000,000 of getting the same number twice. But that number is greatly reduced by the limitations of the generators.

That increases dramatically when you start mixing together 36 letters and digits. The way standard GUIDs are generated gives you roughly 1 chance in 2,600,000,000,000,000,000 (according to different sources that remember their maths better than I do) of getting the same number twice. That is usually considered enough to be considered "unique".
0
 
Bill-HansonCommented:
Err, isn't that what I just said?
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
To repeat the very first comment (by me)

What do you mean unique?


You have a clear idea of what you mean - but it isn't clear (at least to me) exactly what that is in terms of your requirements.  I say that because you explicitly say you do not want to check if it is already in use, just that it is unique.
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
@Bill-Hanson

It is another way of saying the same thing, I concur. But I was typed and send my reply it before I knew that you had given an answer.

And I have a little extra anyway. You go for straight maths, not taking into account the fact that random generators are not completely random.
0
 
Bill-HansonCommented:
All good. :)

RE: "You go for straight maths, not taking into account the fact that random generators are not completely random."
I had already mentioned in post http:#36718911 (and you commented on my post "Well put Bill") about the 'uniqueness' of random numbers.  I am very careful in my verbiage. Notice in my most recent post, that I said: "enough to be considered 'unique'".

Were on the same page, I'ts just a little annoying when people post the same info repeatedly.
0
 
Bill-HansonCommented:
Sorry, wrong link in post above, should be http:#36719152
0
 
shahjagatAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.