Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Random Unique number

Posted on 2011-09-28
Medium Priority
1,023 Views
Hi,

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

Thanks
0
Question by:shahjagat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 3
• 2
• +5

LVL 44

Expert Comment

ID: 36718873
What do you mean unique?
0

LVL 12

Expert Comment

ID: 36718907
u can use math.random()
Or GUID Class to generate that number
0

LVL 22

Expert Comment

ID: 36718911
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

LVL 18

Expert Comment

ID: 36718919
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);

int count = 0;

do
{
number = rndNumber.Next(1, max + 1);
{

}
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;

}

}
}
``````
0

LVL 21

Expert Comment

ID: 36718995
0

LVL 22

Expert Comment

ID: 36719152
I see alot of random number generators listed.

Remember, people...  Random does not equal unique.  Be careful!
0

LVL 40

Expert Comment

ID: 36719459
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;
``````

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

Author Comment

ID: 36719639

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

LVL 23

Expert Comment

ID: 36719676
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

LVL 22

Accepted Solution

Bill-Hanson earned 800 total points
ID: 36719830
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

LVL 40

Assisted Solution

Jacques Bourgeois (James Burger) earned 200 total points
ID: 36719919
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

LVL 22

Expert Comment

ID: 36719985
Err, isn't that what I just said?
0

LVL 44

Expert Comment

ID: 36720034
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

LVL 40

Expert Comment

ID: 36720165
@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

LVL 22

Expert Comment

ID: 36814576
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

LVL 22

Expert Comment

ID: 36814582
Sorry, wrong link in post above, should be http:#36719152
0

Author Closing Comment

ID: 36815251
Thanks
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrasâ€¦
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/weâ€¦
Suggested Courses
Course of the Month4 days, 14 hours left to enroll