C # random string

Source: Internet
Author: User

 

Today, I sorted out the source code of a previous project and found two methods to randomly generate non-repeated strings. I think it is very convenient to use them. I will post them here for future reference. Method 1: generate a random string without repeated numbers 

PrivateIntRep= 0;


              ///
              /// Generate random numeric string
              ///
              /// Number of digits to be generated
              /// Generated numeric string
              Private String Generatecheckcodenum (Int Codecount)
              {
                      String Str = String. empty;
                      Long Num2 = Datetime. Now. ticks + This. REP;
                      This. Rep ++;
                      Random Random = New Random (((INT) (ulong) num2) & 0 xffffffffl )) | ((INT) (num2 > This. REP )));
                      For (Int I = 0; I < Codecount; I++)
                      {
                              Int Num = Random. Next ();
                              Str = Str + ((Char) (0x30 + ((Ushort) (Num % 10). tostring ();
                      }
                      Return STR;
              }

Code Method 2: generate a random string (mixed digits and letters)

 ///
       ///Generate random character strings (mixed numbers and letters)
       ///
       ///Number of digits to be generated
       ///Generated letter string
       PrivateStringGeneratecheckcode (IntCodecount)
       {
           StringStr=String. empty;
           LongNum2=Datetime. Now. ticks+This. REP;
           This. Rep ++;
           RandomRandom=NewRandom (((INT) (ulong) num2)&0 xffffffffl ))|((INT) (num2>This. REP )));
           For(IntI=0;I<Codecount;I++)
           {
               CharCh;
               IntNum=Random. Next ();
               If(Num%2)=0)
               {
                   Ch=(Char) (0x30+((Ushort) (Num%10 )));
               }
               Else
               {
                   Ch=(Char) (0x41+((Ushort) (Num%0x1a )));
               }
               Str=Str+Ch. tostring ();
           }
           ReturnSTR;
       }

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.