这里产生随机密码的思路是先产生随机数,然后根据随机数的值产生随机的密码。
static void Main(string[] args)
{
char[] passwords = new char[] { 'A','B','C','D','E','F','G','H','I','1','2','3','4','5','6','7'};//这里可以添加很多其他的字符如“._”等
for (int j = 0; j < 10; j++)//产生10个随机的密码
{
for (int i = 0; i < 16; i++)
{
Random rd = new Random(GetSeed());
Console.Write(passwords[rd.Next(0, 16)] );
}
Console.WriteLine(System.Environment.NewLine);
}
Console.ReadKey();
}
static Int32 GetSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes,0);
}
}
这里用到了RNGCryptoServiceProvider来当作种子产生新的随机数,具体的解释可见MSDN。
运行结果:
![](https://img-my.csdn.net/uploads/201206/21/1340248053_2705.png)