生成随机字符

#region 生成随机字符
/// <summary>
/// 生成随机字符
/// </summary>
/// <param name="iLength">生成字符串的长度</param>
/// <returns>返回随机字符串</returns>
public static string GetRandCode(int iLength)
{
string sCode = "";
if (iLength == 0)
{
iLength = 4;
}
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[] arr = codeSerial.Split(',');
int randValue = -1;
Random rand = new Random(Guid.NewGuid().GetHashCode());
for (int i = 0; i < iLength; i++)
{
randValue = rand.Next(0, arr.Length - 1);
sCode += arr[randValue];
}
return sCode;
}
#endregion

posted @ 2018-07-06 15:43 BIGBOY_CHUANQI 阅读(...) 评论(...) 编辑 收藏