取得随机字符串 数字和字母

public static string RndNum( int VcodeNum)
        {
            string Vchar = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z" ;
            string [] VcArray = Vchar.Split( new Char[] { ',' }); //拆分成数组
            string VNum = "" ;
            int temp = -1; //记录上次随机数值,尽量避避免生产几个一样的随机数
 
            Random rand = new Random();
            //采用一个简单的算法以保证生成随机数的不同
            for ( int i = 1; i < VcodeNum + 1; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * unchecked (( int )DateTime.Now.Ticks));
                }
 
                int t = rand.Next(33);
                if (temp != -1 && temp == t)
                {
                    return RndNum(VcodeNum);
 
                }
                temp = t;
                VNum += VcArray[t];
            }
            return VNum;
        }

你可能感兴趣的:(c,算法,String,Random)