Random random = new Random();
int password = random.Next(10000);
This generates 2-digit and 3-digit numbers also. How do I generate a 4–8 digit random number in C#?
To cover all your bases (numbers under 1000 such as 0002)
Random RandomPIN = new Random();
var RandomPINResult = RandomPIN.Next(0, 9999).ToString();
RandomPINResult = RandomPINResult.PadLeft(4, '0');
new Random(Guid.NewGuid().GetHashCode()).Next(0, 9999).ToString("D4")
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