Compile the random number generation function
Public Static String Makepassword ( String Pwdchars, Int Pwdlen)
{
String Tmpstr = "" ;
Int Irandnum;
Random RND = New Random ();
For ( Int I = 0 ; I < Pwdlen; I ++ )
{
Irandnum = RND. Next (pwdchars. Length );
Tmpstr + = Pwdchars [irandnum];
}
Return Tmpstr;
}
Usage
The followingCodeCall the makepassword () method to obtain a random string with a length of 30 and a range of uppercase and lowercase letters and numbers.
Protected Void Button#click ( Object Sender, eventargs E)
{
String Randomchars = " Abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz -_ " ;
String Password = Makepassword (randomchars, 30 );
Response. Write (password );
}
Note:
The makepassword method 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.