用ASCII的方法产生随机用户名或密码

来源:互联网 发布:dcs编程语言 编辑:程序博客网 时间:2024/05/29 17:43
/// <summary>
  /// 此函数将产生12~20位的随机用户名
  /// </summary>
  /// <param name="length">输入12-20之间的数字</param>
  /// <returns>此函数将产生12~20位的随机用户名,返回string类型</returns>
        public void GetRandomString(int length)
        {
            Random rd = new Random();
            byte[] str = new byte[length];
            int i;
            for (i = 0; i < length; i++)
            {
                int a = 0;
                while (!((a >= 48 && a <= 57) || (a >= 97 && a <= 122)))
                {
                    a = rd.Next(48, 122);
                }
                str[i] = (byte)a;
            }
            string username = new string(UnicodeEncoding.ASCII.GetChars(str));
            this.register = username;
        }