生成随机字符

#region 生成随机字符
/// <summary>
/// 生成随机字符
/// </summary>
/// <param name="iLength">生成字符串的长度</param>
/// <returns>返回随机字符串</returns>
public static string GetRandCode(int iLength=4)
{
  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  GU天乐乐乐!  阅读(397)  评论(1编辑  收藏  举报