调试运行结果与正常运行结果不同,为何string保存不了数据
private string RndNum(int VcodeNum)
{
string Vchar = "1,2,3,4,5,6,7,8,9,0,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[] VcArray = Vchar.Split(',');
string VNum = ""; //由于字符串很短,就不用StringBuilder了
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(VcArray.Length);
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}
//调用RndNum,并输出结果
if (context.Request["codeNum"] != null && context.Request["codelength"] != null)
{
int codeLength = Convert.ToInt32(context.Request["codelength"]);
System.Text.StringBuilder SN = new System.Text.StringBuilder() ;
int codeNum = Convert.ToInt32(context.Request["codeNum"]);
for (int i = 0; i < codeLength; i++)
{
SN.Append (RndNum(codeLength)+"<br />");
}
context.Response.Write(SN);
}
代码不知道有什么问题,输出结果每次都是一个的字符串。。。
------解决方案--------------------
C# code
for (int i = 0; i < codeLength; i++)
{
SN.Append (RndNum(codeLength)+"<br />");
Thread.Sleep(100);
}