using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string RandomNum(int Len)
{
string str = "0,1,2,3,4,5,6,7,8,9,";
str += "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,";
str += "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[] s = str.Split(new char[] { ','});//将字符串拆分成字符串数组
string strNum = "";
int tag = -1;//用于记录上一个随机数的值,避免产生两个重复值
Random rnd = new Random();
//产生一个长度为Len的随机字符串
for (int i = 1; i <= Len; i++)
{
if (tag == -1)
{
rnd = new Random(i*tag*unchecked((int)DateTime.Now.Ticks));//初始化一个Random实例
}
int rndNum = rnd.Next(61);//返回小于61的非负随机数
//如果产生与前一个随机数相同的数,则重新生成一个新随机数
if (tag != -1 && tag == rndNum)
{
return RandomNum(1);
}
tag = rndNum;
strNum += s[rndNum];
}
return strNum;
}
protected void btnOK_Click(object sender, EventArgs e)
{
if (this.txtLen.Text != "")
{
int l = Convert.ToInt32(this.txtLen.Text.Trim());
this.labNum.Text = RandomNum(l);
}
}
}