protected void Page_Load(object sender, EventArgs e)
{
string[] password = new string[3];
//Label1.Text = Createpassword();
for (int i = 0; i < 3; i++)
{
password[i] = Createpassword();
}
Label1.Text = password[0];
Label2.Text = password[1];
Label3.Text = password[2];
}
public static string Createpassword()
{
string a = "";
string validator = "abcdefghijklmnopqrstuvwxyz0123456789";
string[] b = new string[3];
Random rnd = new Random();
for (int i=1;i < 5;i++)
{
a += validator[rnd.Next(validator.Length)];
}
return a;
}我想一次生成多个随机密码,为什么这样写产生的全都一样呢,该如何该?
{
string[] password = new string[3];
//Label1.Text = Createpassword();
for (int i = 0; i < 3; i++)
{
password[i] = Createpassword();
}
Label1.Text = password[0];
Label2.Text = password[1];
Label3.Text = password[2];
}
public static string Createpassword()
{
string a = "";
string validator = "abcdefghijklmnopqrstuvwxyz0123456789";
string[] b = new string[3];
Random rnd = new Random();
for (int i=1;i < 5;i++)
{
a += validator[rnd.Next(validator.Length)];
}
return a;
}我想一次生成多个随机密码,为什么这样写产生的全都一样呢,该如何该?
public static string Createpassword()
{
string a = "";
string validator = "abcdefghijklmnopqrstuvwxyz0123456789";
System.Security.Cryptography.RandomNumberGenerator rnd = new System.Security.Cryptography.RNGCryptoServiceProvider();
byte[] buffer = new byte[4];
rnd.GetBytes(buffer);
for (int i = 0; i < buffer.Length; i++)
{
int idx = buffer[i] % validator.Length;
a += validator[idx];
}
return a;
}