ASP. NET Random Number

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.