随机卡号的生成
我在页面上写了一个生成卡号的函数
public string Rndnum()
{
int j1;
string strchoice= "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 ";
string [] strresult=strchoice.Split(new char[] { ', '});
string strreturn= " ";
Random rnd=new Random(unchecked((int)DateTime.Now.Ticks));
for(int i=0;i <6;i++)
{
Random rnd1=new Random(rnd.Next()*unchecked((int)DateTime.Now.Ticks));
j1=rnd1.Next(35);
rnd=new Random(rnd.Next()*unchecked((int)DateTime.Now.Ticks));
strreturn=strreturn+strresult[j1].ToString();
}
return "06330403 "+strreturn;
}
在页面上调用这个函数,让它执行10次,
for(int i=0;i <10;i++)
{
string strnum=Rndnum();
Response.write(strnum)
}
结果发现输出的10个strnum的都是一样的,应该怎么解决
[解决办法]
private char[] constant=
{
'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 '
};
private string GenerateRandom(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(36);
Random rd= new Random();//
for(int i=0;i <Length;i++)
{
newRandom.Append(constant[rd.Next(36)]);
}
return newRandom.ToString();
}
private string GenerateRandom(int Length,int Ini)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(36);
Random rnd=new Random();
int r1=rnd.Next(11,14);
Random rd= new Random(r1*unchecked((int)DateTime.Now.Ticks)+Ini);//
for(int i=0;i <Length;i++)
{
newRandom.Append(constant[rd.Next(36)]);
}
return newRandom.ToString();
}
调用
for(int i=1;i <11;i++)
{
string strnum=GenerateRandom(8,i);
Response.write(strnum)
}