using System;
using System.Text;

namespace GxPress.Common.Tools
{
    /// <summary>
    ///     随机号生成器
    /// </summary>
    public class RandomGenerator
    {
        private static readonly char[] CharArr =
        {
            '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'
        };

        private static readonly char[] NumberArr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        /// <summary>
        ///     生成指定位数的纯数字随机数
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetNumberString(int length)
        {
            var code = new StringBuilder();
            var rd = new Random();
            var range = NumberArr.Length;
            for (var i = 0; i < length; i++)
                code.Append(NumberArr[rd.Next(range)].ToString());
            return code.ToString();
        }

        /// <summary>
        ///     生成0-z的随机字符串
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetString(int length)
        {
            var code = new StringBuilder();
            var rd = new Random();
            var range = CharArr.Length;
            for (var i = 0; i < length; i++)
                code.Append(CharArr[rd.Next(range)].ToString());
            return code.ToString();
        }
    }
}