Hriskesh Ashokan Hriskesh Ashokan - 1 month ago 16
ASP.NET (C#) Question

How to generate a random 10 digit number in C#?

I'm using C# and I need to generate a random 10 digit number. So far, I've only had luck finding examples indicating min maximum value. How would i go about generating a random number that is 10 digits, which can begin with 0, (initially, I was hoping for

random.Next(1000000000,9999999999)
but I doubt this is what I want).

My code looks like this right now:

[WebMethod]
public string GenerateNumber()
{
Random random = new Random();
return random.Next(?);
}


**Update ended up doing like so,

[WebMethod]
public string GenerateNumber()
{
Random random = new Random();
string r = "";
int i;
for (i = 1; i < 11; i++)
{
r += random.Next(0, 9).ToString();
}
return r;
}

Answer

If you want ten digits but you allow beginning with a 0 then it sounds like you want to generate a string, not a long integer.

Generate a 10-character string in which each character is randomly selected from '0'..'9'.