Based on your description, you want the password is made up of numbers and letters which cann't contain the duplicate charactor.
There is code as below can achieve this. You can call it via GeneratePassword(6, 1). The variable numberOfNumeral means the minimal number of numerals and you can set it as 1 in order to make sure it contains number.
string GeneratePassword(int length, int numberOfNumeral)
{
if (((length < 1) || (length > 128)))
{
throw new ArgumentException("Membership_password_length_incorrect");
}
if ( (numberOfNumeral > length) || (numberOfNumeral < 0) )
{
throw new ArgumentException("Membership_min_required_non_alphanumeric_characters_incorrect");
}
int i;
byte[] buffer1 = new byte[length];
//chPassword contains the password's characters as it's built up
char[] chPassword = new char[length];
char[] chCharact = "ABCDEFGHIJKLMNOPQRSTUWXYZ1234567890".ToCharArray();
char[] chNumeral = "1234567890".ToCharArray();
Random rndNumber = new Random();
for (i = 0; i < numberOfNumeral; i++)
{
int passwordPos;
do
{
passwordPos = rndNumber.Next(0, length);
}
while ((int)chPassword[passwordPos] != 0);
char chtem=chNumeral[rndNumber.Next(0, chNumeral.Length)];
if (CharExist(chtem, chPassword))
i--;
else
chPassword[passwordPos] = chtem;
}
for (i = 0; i < length; i++)
{
if ((int)chPassword[i] == 0)
{
char chtem = chCharact[rndNumber.Next(0, chCharact.Length)];
if (CharExist(chtem, chPassword))
i--;
else
chPassword[i] = chtem;
}
}
return new string(chPassword);
}
private bool CharExist(char ch, char[] chs)
{
for (int i = 0; i < chs.Length; i++)
{
if (chs[i] == ch)
return true;
}
return false;
}
Hope this can help.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
jcjcjc
Member
64 Points
639 Posts
When Random a string set how to avoid same char appear? thank you
Jan 08, 2008 06:25 AM|LINK
public class PwCreator { public string Creator( ) { String Vchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z"; String[] VcArray = Vchar.Split(','); String VNum = ""; Random random = new Random(); for (int i = 1; i <= 6; i++) { int iNum = 0; while ((iNum = Convert.ToInt32(VcArray.Length * random.NextDouble())) == VcArray.Length) { iNum = Convert.ToInt32(VcArray.Length * random.NextDouble()); } VNum += VcArray[iNum]; } return VNum; } }the password Must mixed numbers and letters and can't have same char
when I test this code the problem is sometimes password is numbers or letters only ... just can't find out how to modify it can you please help?
and also.. how to avoid a same numbers or same letters appear in password?
thank you
Vince Xu - M...
All-Star
80367 Points
6801 Posts
Re: When Random a string set how to avoid same char appear? thank you
Jan 10, 2008 06:25 AM|LINK
Hi,
Based on your description, you want the password is made up of numbers and letters which cann't contain the duplicate charactor.
There is code as below can achieve this. You can call it via GeneratePassword(6, 1). The variable numberOfNumeral means the minimal number of numerals and you can set it as 1 in order to make sure it contains number.
string GeneratePassword(int length, int numberOfNumeral) { if (((length < 1) || (length > 128))) { throw new ArgumentException("Membership_password_length_incorrect"); } if ( (numberOfNumeral > length) || (numberOfNumeral < 0) ) { throw new ArgumentException("Membership_min_required_non_alphanumeric_characters_incorrect"); } int i; byte[] buffer1 = new byte[length]; //chPassword contains the password's characters as it's built up char[] chPassword = new char[length]; char[] chCharact = "ABCDEFGHIJKLMNOPQRSTUWXYZ1234567890".ToCharArray(); char[] chNumeral = "1234567890".ToCharArray(); Random rndNumber = new Random(); for (i = 0; i < numberOfNumeral; i++) { int passwordPos; do { passwordPos = rndNumber.Next(0, length); } while ((int)chPassword[passwordPos] != 0); char chtem=chNumeral[rndNumber.Next(0, chNumeral.Length)]; if (CharExist(chtem, chPassword)) i--; else chPassword[passwordPos] = chtem; } for (i = 0; i < length; i++) { if ((int)chPassword[i] == 0) { char chtem = chCharact[rndNumber.Next(0, chCharact.Length)]; if (CharExist(chtem, chPassword)) i--; else chPassword[i] = chtem; } } return new string(chPassword); } private bool CharExist(char ch, char[] chs) { for (int i = 0; i < chs.Length; i++) { if (chs[i] == ch) return true; } return false; }Hope this can help.