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

2020-02-09 06:57发布

问题:

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

回答1:

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'.



回答2:

Use this to create random digits with any specified length

public string RandomDigits(int length)
{
    var random = new Random();
    string s = string.Empty;
    for (int i = 0; i < length; i++)
        s = String.Concat(s, random.Next(10).ToString());
    return s;
}


回答3:

try (though not absolutely exact)

Random R = new Random();

return ((long)R.Next (0, 100000 ) * (long)R.Next (0, 100000 )).ToString ().PadLeft (10, '0');


回答4:

(1000000000,9999999999) is not random - you're mandating that it cannot begin with a 1, so you've already cut your target base by 10%.

Random is a double, so if you want a integer, multiply it by 1,000,000,000, then drop the figures after the decimal place.



回答5:

private void button1_Click(object sender, EventArgs e)
{
   Random rand = new Random();
   long randnum2 = (long)(rand.NextDouble() * 9000000000) + 1000000000;
   MessageBox.Show(randnum2.ToString());
}


回答6:

To get the 10 digit number without any loop (or any other length number)

private static readonly Random _rdm = new Random();
private string PinGenerator(int digits)
{
   if (digits <= 1) return "";

   var _min = (int)Math.Pow(10, digits - 1);
   var _max = (int)Math.Pow(10, digits) - 1;
   return _rdm.Next(_min, _max).ToString();
}

This function calculated the lower and the upper bounds of the nth digits number.

To generate the 10 digit number use it like this:

PinGenerator(10)



回答7:

I came up with this method because I dont want to use the Random method :

public static string generate_Digits(int length)
{
    var rndDigits = new System.Text.StringBuilder().Insert(0, "0123456789", length).ToString().ToCharArray();
    return string.Join("", rndDigits.OrderBy(o => Guid.NewGuid()).Take(length));
}

hope this helps.



回答8:

private static Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden

public long LongBetween(long maxValue, long minValue)
{
    return (long)Math.Round(random.NextDouble() * (maxValue - minValue - 1)) + minValue;
}


回答9:

I tried to write a fast one:

private int GetNDigitsRandomNumber(int digits)
{
    var min = 1;
    for (int i = 0; i < digits-1; i++)
    {
          min *= 10;
    }
    var max = min * 10;

    return _rnd.Next(min, max);
}


回答10:

// ten digits 
public string CreateRandomNumber
{
    get
    {
        //returns 10 digit random number (Ticks returns 16 digit unique number, substring it to 10)
        return DateTime.UtcNow.Ticks.ToString().Substring(8); 
    }
}


回答11:

Random random = new Random();
string randomNumber = string.Join(string.Empty, Enumerable.Range(0, 10).Select(number => random.Next(0, 9).ToString()));


回答12:

To generate a random 10 digit number in C#

Random RndNum = new Random();   

int RnNum = RndNum.Next(1000000000,9999999999);