Solved

Random Unique number

Posted on 2011-09-28
17
1,000 Views
Last Modified: 2012-05-12
Hi,

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

Thanks
0
Comment
Question by:shahjagat
  • 6
  • 3
  • 2
  • +5
17 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36718873
What do you mean unique?
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36718907
u can use math.random()
Or GUID Class to generate that number
0
 
LVL 22

Expert Comment

by:Bill-Hanson
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 18

Expert Comment

by:Anil Golamari
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);
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
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 36718995
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 36719152
I see alot of random number generators listed.

Remember, people...  Random does not equal unique.  Be careful!
0
 
LVL 40
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;

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
 

Author Comment

by:shahjagat
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

by:wdosanjos
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

by:
Bill-Hanson earned 200 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

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 50 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

by:Bill-Hanson
ID: 36719985
Err, isn't that what I just said?
0
 
LVL 44

Expert Comment

by:AndyAinscow
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
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

by:Bill-Hanson
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

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

Author Closing Comment

by:shahjagat
ID: 36815251
Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Diagnostics with Net and Net.Sockets 2 30
VB.NET - Error - Object not set in Module 4 35
Send SMS from VB via Sierra Wireless Modem 2 22
Powershell File Sort 8 31
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question