c#产生随机字符串

作者:freethy 发布于:2013-5-25 9:32 Saturday



        /// <summary>
        /// 获取随机字符串
        /// </summary>
        /// <param name="len">随机字符串长度</param>
        /// <param name="type">随机字符串模式 1.随机数字0-9  2.随机0-9A-B  其他.随机0-9A-Ba-b</param>
        /// <returns></returns>
        public string GetRandom(int len,int type)
        {
            StringBuilder sb = new StringBuilder();
            string temp = string.Empty;
            if (type == 1)
            {
                temp = "0123456789";
            }
            else if (type == 2)
            {
                temp = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            }
            else
            {
                temp = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            }
           

            for (int i = 0; i < len; i++)
            {
                int num = new Random(Guid.NewGuid().GetHashCode()).Next(0, temp.Length);
                sb.Append(temp[num]);
            }
            return sb.ToString();
        }

Powered by emlog 苏ICP备11068260号-3