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; }
C library function - rand()The C library function int rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.
For example, to generate a random number between 1 and 10, we can do it like below. ThreadLocalRandom random = ThreadLocalRandom. current(); int rand = random. nextInt(1, 11);
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With