When Random a string set how to avoid same char appear? thank youhttp://forums.asp.net/t/1202361.aspx/1?When+Random+a+string+set+how+to+avoid+same+char+appear+thank+youThu, 10 Jan 2008 06:25:22 -050012023612096007http://forums.asp.net/p/1202361/2096007.aspx/1?When+Random+a+string+set+how+to+avoid+same+char+appear+thank+youWhen Random a string set how to avoid same char appear? thank you Below is my code for create password which Including numbers and Letters and Password length is 6 chars.<br> <p>&nbsp;</p> <pre class="prettyprint">public class PwCreator { public string Creator( ) { String Vchar = &quot;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&quot;; String[] VcArray = Vchar.Split(','); String VNum = &quot;&quot;; Random random = new Random(); for (int i = 1; i &lt;= 6; i&#43;&#43;) { int iNum = 0; while ((iNum = Convert.ToInt32(VcArray.Length * random.NextDouble())) == VcArray.Length) { iNum = Convert.ToInt32(VcArray.Length * random.NextDouble()); } VNum &#43;= VcArray[iNum]; } return VNum; } }</pre> <p>&nbsp;the password Must mixed numbers and letters and can't have same char<br> </p> <p>when I test this code the problem is sometimes password is&nbsp; numbers or letters only ... just can't find out how to modify it can you please help? </p> <p>and also.. how to avoid a same numbers or same letters appear in password?&nbsp;</p> <p>thank you&nbsp;</p> 2008-01-08T06:25:42-05:002100986http://forums.asp.net/p/1202361/2100986.aspx/1?Re+When+Random+a+string+set+how+to+avoid+same+char+appear+thank+youRe: When Random a string set how to avoid same char appear? thank you <p>Hi,</p> <p>Based on your description, you want the password is made up of numbers and letters which cann't contain the duplicate charactor. </p> <p>There is code as below can achieve this. You can call it via <strong>GeneratePassword(6, 1).</strong> The variable numberOfNumeral&nbsp;means the minimal number of numerals and you can set it as 1 in order to make sure it contains number.<pre class="prettyprint">string GeneratePassword(int length, int numberOfNumeral) { if (((length &lt; 1) || (length &gt; 128))) { throw new ArgumentException(&quot;Membership_password_length_incorrect&quot;); } if ( (numberOfNumeral &gt; length) || (numberOfNumeral &lt; 0) ) { throw new ArgumentException(&quot;Membership_min_required_non_alphanumeric_characters_incorrect&quot;); } 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 = &quot;ABCDEFGHIJKLMNOPQRSTUWXYZ1234567890&quot;.ToCharArray(); char[] chNumeral = &quot;1234567890&quot;.ToCharArray(); Random rndNumber = new Random(); for (i = 0; i &lt; numberOfNumeral; i&#43;&#43;) { 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 &lt; length; i&#43;&#43;) { 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 &lt; chs.Length; i&#43;&#43;) { if (chs[i] == ch) return true; } return false; }</pre>&nbsp;</PRE> </p> <p>&nbsp;Hope this can help.</p> <p>&nbsp;</p> 2008-01-10T06:25:22-05:00