C # Generate random numbers to customize letters and numbers

Source: Internet
Author: User

C # randomly generated random numbers, you can customize the number of randomly generated letters and numbers

The above is an example of generating 8-bit random numbers, 4 digits, and 4-bit letters,

The code is as follows:

classProgram {Static voidMain (string[] args) {             for(inti =0; I <Ten; i++)            {                stringresult = Createrandom (4,4);            Console.WriteLine (result); }        }        /// <summary>        ///Create a random number/// </summary>        /// <param name= "letter" >Number of letters</param>        /// <param name= "num" >Number of digits</param>        /// <returns></returns>         Public Static stringCreaterandom (intLetter,intnum) {            stringresult = Createrandomletter (letter) +createrandomnum (num); varTemp = result. (x = Guid.NewGuid ()). ToArray ();//Sort by a random value after grouping            return New string(temp); }        /// <summary>        ///Create a random number of letters/// </summary>        /// <param name= "letter" ></param>        /// <returns></returns>        Private Static stringCreaterandomletter (intLetter ) {            stringstr ="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; StringBuilder SB=NewStringBuilder ();  for(inti =0; i < letter; i++) {sb. Append (str[NewRandom (Guid.NewGuid (). GetHashCode ()). Next (0Str. Length-1)]); }            returnsb.        ToString (); }        /// <summary>        ///Create a number random number/// </summary>        /// <param name= "num" ></param>        /// <returns></returns>        Private Static stringCreaterandomnum (intnum) {            stringstr ="0123456789"; StringBuilder SB=NewStringBuilder ();  for(inti =0; i < num; i++) {sb. Append (str[NewRandom (Guid.NewGuid (). GetHashCode ()). Next (0Str. Length-1)]); }            returnsb.        ToString (); }    }

C # Generate random numbers to customize letters and numbers

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.