Random random = new Random();
int password = random.Next(10000);
这也可以生成两位数和三位数。 我该如何在C#中生成4-8位随机数字?
从最小的4位数字开始,到最小的9位数字结束(不包括):
int password = random.Next(1000, 100000000);
public static int GetRandom(int minDigits, int maxDigits)
{
if (minDigits < 1 || minDigits > maxDigits)
throw new ArgumentOutOfRangeException();
return (int)random.Next(Math.Pow(10, minDigits - 1), Math.Pow(10, maxDigits - 1));
}
Random RandomPIN = new Random();
var RandomPINResult = RandomPIN.Next(0, 9999).ToString();
RandomPINResult = RandomPINResult.PadLeft(4, '0');
Random RandomPIN = new Random();
var RandomPINResult = RandomEASPIN.Next(0, 9999).ToString("D4");
- Mir使用 Guid.NewGuid().GetHashCode()
作为种子,生成一个随机数,然后将其格式化为4位数字字符串。