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

83,249

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

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

Solution 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');

Solution 4

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)

Solution 5

private void button1_Click(object sender, EventArgs e)
{
   Random rand = new Random();
   long randnum2 = (long)(rand.NextDouble() * 9000000000) + 1000000000;
   MessageBox.Show(randnum2.ToString());
}
Share:
83,249
Hriskesh Ashokan
Author by

Hriskesh Ashokan

No one has ever become poor from giving

Updated on June 23, 2020

Comments

  • Hriskesh Ashokan
    Hriskesh Ashokan almost 4 years

    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;
    }
    
  • Hriskesh Ashokan
    Hriskesh Ashokan almost 13 years
    Did exactly this, my code looks like the updated block above.
  • Caramiriel
    Caramiriel almost 9 years
    9999999999 overflows int.MaxValue, thus doesn't compile.
  • Neil Masson
    Neil Masson over 8 years
    How is this a random number?
  • Danhol86
    Danhol86 over 7 years
    Do not use this function if you intend on using it more than once. The random object should be declared outside the function scope as each time it will create a new instance of the Random object which can mean same number is generated. See stackoverflow.com/a/5264434/6099813
  • EAmez
    EAmez about 7 years
    Whenever you call this function, it will return a different value. That's almost like random value.This solution works for me: I only need to get different values each time I call it, no matter what the value is.
  • Caboosetp
    Caboosetp over 6 years
    @Danhol86 This does not have the same issue as the link you posted. What you posted specifies a seed in the constructor while this does not. If you do not specify a seed, a seed will be generated from the system clock which mostly guarantees different numbers. Still good to know about either way.
  • Alparslan ŞEN
    Alparslan ŞEN over 4 years
    I have tried 1 million times was successful(for lenght 15 digits)
  • Wai Ha Lee
    Wai Ha Lee over 4 years
    This won't work for ten digits, as the range of values representable by int is ~±2*10^9 - so once min is 10^8 you'll overflow and get unexpected values for max.
  • Captain Prinny
    Captain Prinny over 4 years
    What's the purpose of multiplying two numbers together instead of just doing a single multiplication?
  • VSB
    VSB about 4 years
    It will not cover all 10 digit numbers. This method will not cover prime number in 10 digit number range.
  • Lucas
    Lucas almost 4 years
    I'm not sure random has anything to do with range. You may be describing predictability rather than randomness?
  • user1544428
    user1544428 almost 2 years
    In certain circumstances, this code can produce duplicates. Do some research on it. I have had similar code that when used in a loop would work just fine, but move it to a faster computer and it return duplicates a few times in a row here or there. Quite annoying to track down. Also, a change in time or having multiple computers running the code. I'm just saying.