重複の可能性:
乱数ジェネレーターは1つの乱数のみを生成します
以下の私のコードはこれを行っていますが、理由はわかりません:
toEmailAddresses配列に2つの電子メールアドレスが含まれている場合、メソッド呼び出しから生成されたものuniqueID[0]
とuniqueID[1]
同じ値が返されます。Util.CreateRandomPassword(16)
コードをステップスルーすると、との両方uniqueID[0]
にuniqueID[1]
異なる値が含まれるようになります。しかし、通常どおりコードを実行すると、何らかの理由で、同じ値がuniqueID
配列 uniqueID[0]
に割り当てられuniqueID[1]
ます。同じ値が含まれます。
を入れて、string tempRandomPassword = null
それをメソッドから返された値に割り当てることもCreateRandomPassword
できますが、それも機能しません。
私は何が間違っているのですか?
//toEmailAddresses.Count array will have two e-mail addresses in it.
string[] uniqueID = new string[2];
for (int i = 0; i < toEmailAddresses.Count(); i++)
{
string tempRandomPassword = null;
tempRandomPassword = Util.CreateRandomPassword(16);
uniqueID[i] = tempRandomPassword;
}
public static string CreateRandomPassword(int passwordLength)
{
//http://madskristensen.net/post/Generate-random-password-in-C.aspx
string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
char[] chars = new char[passwordLength];
Random rd = new Random();
for (int i = 0; i < passwordLength; i++)
{
chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
}
return new string(chars);
}