随机生成6位字符,中间可以包括数字,或是大写字母,或是小写字母
如纯数字:123456
或纯小写字母:asdgrv
或纯大写字母:ASJMVD
也可以混合输出:2f6Db7
用C#求解,谢啦!
这样就好了,自己用的生成验证码的方法
/// <summary> /// 得到随机字符串 /// </summary> /// <returns></returns> public string GetRan() { Random r = new Random(); string str = ""; for (int q = 0; q < 6; q++) { int i = r.Next(48, 122); if ((i > 57 && i < 65) || (i > 90 && i < 97)) { i = GetNewRandom(); } char ww = (char)i; str += ww.ToString(); } return str; } public int GetNewRandom() { Random r = new Random(); int i = r.Next(48, 122); if ((i > 57 && i < 65) || (i > 90 && i < 97)) { GetNewRandom(); } return i; }
public static string CreateRandomCode(int codeCount) { string allChar = @"0,1,2,3,4,5,6,7,8,9,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,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[] allCharArray = allChar.Split(','); string randomCode = ""; Random rand = new Random(); for (int i = 0, l = allCharArray.Length; i < codeCount; i++) { rand = new Random(i * (int)DateTime.Now.Ticks); int t = rand.Next(0, l); randomCode += allCharArray[t]; } return randomCode; }
直接调用就OK了