Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)delv
published in(发表于) 2014/1/16 9:29:07
ASP.NET生成n位不同的验证码_[Asp.Net教程]

ASP.NET生成n位不同的验证码_[Asp.Net教程]

//生成随机数函数中从strchar 数组中随机抽取
//字母区分大小写
//参数n为生成随机数的位数,一般取四位
public string RandomNum(int n) //
{
string strchar = "0,1,2,3,4,5,6,7,8,9" ;
string[] VcArray = strchar.Split(',') ;
string VNum = "" ;//由于字符串很短,就不用StringBuilder了
int temp = -1 ; //记录上次随机数值,尽量避免产生几个一样的随


机数
//采用一个简单的算法以保证生成随机数的不同
Random rand =new Random();
for ( int i = 1 ; i < n+1 ; i++ )
{
if ( temp != -1)
{
rand =new Random(i*temp*unchecked((int)


DateTime.Now.Ticks));
}
//int t = rand.Next(35) ;
int t=rand.Next(10);
if (temp != -1 && temp == t)
{
return RndNum( n);
}
temp = t ;
VNum += VcArray[t];
}
return VNum ;//返回生成的随机数
}
生成随机数后,将值赋值给一个Label控件,然后改一下Label背景的颜色,OK了


来源:网络







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759