using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LZBS.Common
{
    public class RandomBuilder
    {
        private static string[] strs = new string[36];
        /// <summary>
        /// 实例化存储随机码中使用的字符的字符串数组
        /// </summary>
        public static void InitStrs()
        {
            for (int i = 0; i < 10; i++)
            {
                strs[i] = i.ToString();
            }

            int j = 10;
            //根据ASCⅡ码 生成大写字母
            for (int i = 65; i < 91; i++)
            {
                strs[j] = ((char)i).ToString();
                j++;
            }
        }
        /// <summary>
        /// 根据参数返回一定长度的随机字符串
        /// </summary>
        /// <param name="CodeNum"></param>
        /// <returns></returns>
        public static string GetRandomCode(int CodeNum)
        {
            InitStrs();
            StringBuilder builder = new StringBuilder(string.Empty);
            Random rand = new Random((int)DateTime.Now.Ticks);
            //获取指定个数的随机字符
            for (int i = 0; i < CodeNum; i++)
            {
                builder.Append(strs[rand.Next(strs.Length)]);
                continue;
            }
            //返回随机字符组成的字符串
            return builder.ToString();
        }
    }
}
