publicstaticstringMakePassword(stringpwdchars,intpwdlen){ stringtmpstr=""; intiRandNum; Randomrnd=newRandom(); for(inti=0; i{ iRandNum=rnd.Next(pwdchars.Length); tmpstr+=pwdchars[iRandNum]; } returntmpstr; } |
Let's talk about the specific ideas based on the source code:
Method: makepassword accepts two parameters. The pwdchars parameter specifies which characters can be used for the generated random password string, and pwdlen specifies the length of the generated random password string. With these two parameters, call the next () method of the random class to obtain an integer greater than or equal to 0 and less than the length of pwdchars, using this number as the index value, take random characters from available strings, take the specified password length as the number of cycles, connect the obtained characters in sequence, and finally obtain the required random password string.
Code call: The makepassword () method is used to obtain a random string with a length of 10 and a range of uppercase/lowercase letters and numbers.
String randomchars = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz"; string Password = makepassword (randomchars, 10 );