# 12 digit unique random number generation in c# / asp.net [Answered]RSS

## 11 replies

Last post Jun 28, 2012 12:57 PM by v.arunkarthik

Member

633 Points

330 Posts

### 12 digit unique random number generation in c# / asp.net

I want to generate 10,000 random numbers consisting of 12digit each

i tried using random function but that doesn't support 12 digits. what is the best way to be done?

help required.

Cheers!
karthik..

Participant

938 Points

562 Posts

### Re: 12 digit unique random number generation in c# / asp.net

Jun 28, 2012 11:24 AM|shivanand G N|LINK

Hint-->

Get datetime.now and assign some char to that date..

for exam. date=char+date;

Arun+datetime;

Good luck

Warm Regards
shivanand.G.N (shivu.betta@gmail.com)

Participant

1761 Points

517 Posts

### Re: 12 digit unique random number generation in c# / asp.net

Jun 28, 2012 11:28 AM|prince srivastava|LINK

Hi arun

System.Guid.NewGuid().ToString().Substring(0, 12);

use above one this one genrate 32 digit unique no you add substring for Specific randon no . this is alpha numeric code .

My Blog
ROHIT KUMAR SRIVASTAVA (INDIA)

Participant

1761 Points

517 Posts

### Re: 12 digit unique random number generation in c# / asp.net

Jun 28, 2012 11:28 AM|prince srivastava|LINK

Hi arun

System.Guid.NewGuid().ToString().Substring(0, 12);

use above one this one genrate 32 digit unique no you add substring for Specific randon no . this is alpha numeric code .

My Blog
ROHIT KUMAR SRIVASTAVA (INDIA)

Star

7662 Points

2609 Posts

### Re: 12 digit unique random number generation in c# / asp.net

Jun 28, 2012 11:50 AM|Ramesh Chandra|LINK

Call below method 10,000 times in a loop

```private static Random rng = new Random(Environment.TickCount);

private static void GetNumber(object objlength)
{
for (int index = 0; index < 20; index++)
{
int length = Convert.ToInt32(objlength);
var number = rng.NextDouble().ToString("0.000000000000").Substring(2, length);
Console.WriteLine(number);
}
}```

Contributor

4397 Points

1146 Posts

### Re: 12 digit unique random number generation in c# / asp.net

try using this:

```public static Int64 GenerateRandomNumber(int size)
{
Random random = new Random((int)DateTime.Now.Ticks);
StringBuilder builder = new StringBuilder();
string s;
for (int i = 0; i < size; i++)
{
s = Convert.ToString(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(s);
}

return Convert.ToInt64((builder.ToString()));
}```

Talha Ashfaque -- Code ShodePlease if this post helps you!

Participant

938 Points

562 Posts

### Re: 12 digit unique random number generation in c# / asp.net

Jun 28, 2012 12:27 PM|shivanand G N|LINK

Random ran = new Random();
string str= ran.Next(int minvalue.int maxvalue);

Good Luck

Warm Regards
shivanand.G.N (shivu.betta@gmail.com)

Member

633 Points

330 Posts

### Re: 12 digit unique random number generation in c# / asp.net

#### Ramesh Chandra

Call below method 10,000 times in a loop

```private static Random rng = new Random(Environment.TickCount);

private static void GetNumber(object objlength)
{
for (int index = 0; index < 20; index++)
{
int length = Convert.ToInt32(objlength);
var number = rng.NextDouble().ToString("0.000000000000").Substring(2, length);
Console.WriteLine(number);
}
}```

Hi,

thanks for replying.. i printed 10,000 numbers successfully..

but can this be alphanumeric? bcoz now they want it to be alphanumeric too.

Cheers!
karthik..

Contributor

4397 Points

1146 Posts

### Re: 12 digit unique random number generation in c# / asp.net

try this:

```public static string GenerateRandomString(int size)
{
Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}

return builder.ToString();
}```

Talha Ashfaque -- Code ShodePlease if this post helps you!

Member

105 Points

53 Posts

### Re: 12 digit unique random number generation in c# / asp.net

```int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}```
`the max and min no is min---> 12 digit min. no.`
`max---> 12 digit max no.`

Sumit Choudhary
Software Developer
India

Contributor

4554 Points

1770 Posts

### Re: 12 digit unique random number generation in c# / asp.net

Hai

Look this

Here is the super easy way to do that. Following code snippet will give you 8 digit alphanumeric random string.

```      Guid g = Guid.NewGuid();
string random = g.ToString();
Response.Write(random.Substring(0, 8));```

In the 3rd line you can replace 8 with whatever number (10, 12 etc) and then you may find - that you can replace with empty string.

http://stackoverflow.com/questions/8618773/random-number-generator-variable-length

Thanks

Member

633 Points

330 Posts

### Re: 12 digit unique random number generation in c# / asp.net

Thanks guys..  got that working.. my final code is

```for (int i = 0; i < 10000; i++)
{
Guid g = Guid.NewGuid();
string random = g.ToString();
string s = random.Replace("-", "");
string final = "H" + s.Substring(0, 11);
Response.Write(final + "<br/>");
}```

Cheers!
karthik..