C # generates non-repeating random numbers

Source: Internet
Author: User

        Private stringRNDOMSTR (intcodelength) {            //the character set that makes up a string is 0-9 digits, uppercase and lowercase.            stringchars ="0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,p,q,r, S,t,u,v,w,x,y,z"; string[] Chararray = chars. Split (NewChar[] {',' }); stringCode =""; inttemp =-1;//record the last random value and avoid producing several random numbers as much as possibleRandom Rand =NewRandom (); //A simple algorithm is used to ensure that the random numbers are generated differently.             for(inti =1; I < Codelength +1; i++)            {                if(Temp! =-1) {Rand=NewRandom (i * temp *unchecked((int) (DateTime.Now.Ticks));//Initialize random classes                }                intt = rand. Next ( A); if(temp = =t) {returnRndomstr (codelength);//if the obtained random number repeats, the recursive call} temp= t;//to record the random numbers that have been generated this time.Code + = Chararray[t];//the number of digits of the random number plus one            }            returnCode; }

C # generates non-repeating random numbers

Related Article

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.