简体   繁体   中英

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

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

try (though not absolutely exact)

Random R = new Random();

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

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

private void button1_Click(object sender, EventArgs e)
{
   Random rand = new Random();
   long randnum2 = (long)(rand.NextDouble() * 9000000000) + 1000000000;
   MessageBox.Show(randnum2.ToString());
}
// 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); 
    }
}

To get the any digit number without any loop, use Random.Next with the appropriate limits [100...00, 9999...99].

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)

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.

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

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

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);
}
Random random = new Random();
string randomNumber = string.Join(string.Empty, Enumerable.Range(0, 10).Select(number => random.Next(0, 9).ToString()));

Here is a simple solution using format string:

string r = $"{random.Next(100000):00000}{random.Next(100000):00000}";

Random 10 digit number (with possible leading zeros) is produced as union of two random 5 digit numbers. Format string "00000" means leading zeros will be appended if number is shorter than 5 digits (eg 1 will be formatted as "00001").

For more about the "0" custom format specifier please see the documentation .

To generate a random 10 digit number in C#

Random RndNum = new Random();   

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM