如何在C#中随机产生大小写字母和数字的混合?求大神解答

2025-03-20 17:46:41
推荐回答(1个)
回答1:

var code=GetCode(4);//随机获取4个数
private string GetCode(int num)//获取随机数
        {
            string AllCode = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,M,N,O,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,o,p,q,r,s,t,u,v,w,x,y,z";
            string[] CodeArray = AllCode.Split(',');
            string ReturnCode = "";
            Random rand = new Random();
            for (int i = 0; i < num; i++)
            {
                int r = rand.Next(60);
                ReturnCode += CodeArray[r];
            }
            return ReturnCode;
        }