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