如何随机生成四个字母或数字?
来源:百度知道 编辑:UC知道 时间:2024/08/23 07:43:13
小弟只知道生成随机的数字```但是数字和字母混合的呢???
我觉得我这个简单些,哈哈
//生成验证码
char[] bytes = new char[4];
Random ran = new Random();
for (int i = 0; i < bytes.Length; i++)
{
byte bit = (byte)ran.Next(62);
if (bit < 10) bytes[i] = (char)(bit + 48);
else if (bit < 36) bytes[i] = (char)(bit + 55);
else bytes[i] = (char)(61 + bit);
}
//验证码
string key = new string(bytes);
private string codeSerial = "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[] arrTemp = codeSerial.Split(',');
string code = string.Empty;
int randValue = -1;
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < 4; i++)
{
randValue = rand.Next(0, arrTemp.Length - 1);