閱讀156 返回首頁    go 阿裏雲 go 技術社區[雲棲]


RandomHelper-隨機數輔助類

namespace Framework
{
    using System;
    using System.Globalization;
    using System.Text;

    /// <summary>
    /// 隨機數輔助類
    /// </summary>
    public sealed class RandomHelper
    {
        private static readonly char[] RandChar = { '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 Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
        private static int seed = 1;

        private RandomHelper()
        {
        }

        /// <summary>
        /// 根據規則隨機生成字符串
        /// </summary>
        /// <param name="pattern">樣式:"?"代表一個字符,"#"代表一個一位數字,"*"代表一個字符串或一個一位數字</param>
        /// <returns>隨機字符串</returns>
        public static string GetRandStringByPattern(string pattern)
        {
            if (!pattern.Contains("#") && !pattern.Contains("?") && !pattern.Contains("*"))
            {
                return pattern;
            }

            char[] nums = pattern.ToCharArray();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < nums.Length; i++)
            {
                switch (nums[i])
                {
                    case '?':
                        nums[i] = RandChar[rand.Next(10, 62)];
                        break;
                    case '#':
                        nums[i] = RandChar[rand.Next(0, 10)];
                        break;
                    case '*':
                        nums[i] = RandChar[rand.Next(62)];
                        break;
                    default:
                        break;
                }

                sb.Append(nums[i]);
            }

            return sb.ToString();
        }

        /// <summary>
        /// 生成隨機的數值
        /// </summary>
        /// <param name="min">隨機數可取該下界值</param>
        /// <param name="max">隨機數的上界</param>
        /// <returns>隨機的數值</returns>
        public static int GetFormatedNumeric(int min, int max)
        {
            int num = 0;
            Random ro = new Random(unchecked(seed * (int)DateTime.Now.Ticks));
            num = ro.Next(min, max);
            seed++;
            return num;
        }

        /// <summary>
        /// 獲取指定長度和字符的隨機字符串
        /// 通過調用 Random 類的 Next() 方法,先獲得一個大於或等於 0 而小於 pwdchars 長度的整數
        /// 以該數作為索引值,從可用字符串中隨機取字符,以指定的密碼長度為循環次數
        /// 依次連接取得的字符,最後即得到所需的隨機密碼串了。
        /// </summary>
        /// <param name="pwdchars">隨機字符串裏包含的字符</param>
        /// <param name="pwdlen">隨機字符串的長度</param>
        /// <returns>隨機產生的字符串</returns>
        public static string GetRandomString(string pwdchars, int pwdlen)
        {
            StringBuilder tmpstr = new StringBuilder();
            int randNum;

            for (int i = 0; i < pwdlen; i++)
            {
                randNum = rand.Next(pwdchars.Length);
                tmpstr.Append(pwdchars[randNum]);
            }

            return tmpstr.ToString();
        }

        /// <summary>
        /// 獲取指定長度的隨機字符串
        /// </summary>
        /// <param name="pwdlen">隨機字符串的長度</param>
        /// <returns>隨機產生的字符串</returns>
        public static string GetRandomString(int pwdlen)
        {
            return GetRandomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_*", pwdlen);
        }

        /// <summary>
        /// 獲取指定長度的純字母隨機字符串
        /// </summary>
        /// <param name="pwdlen">數字串長度</param>
        /// <returns>純字母隨機字符串</returns>
        public static string GetRandWord(int pwdlen)
        {
            return GetRandomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", pwdlen);
        }

        /// <summary>
        /// 獲取指定長度的純數字隨機數字串
        /// </summary>
        /// <param name="intlong">數字串長度</param>
        /// <returns>純數字隨機數字串</returns>
        public static string GetRandomNum(int intlong)
        {
            StringBuilder w = new StringBuilder(string.Empty);

            for (int i = 0; i < intlong; i++)
            {
                w.Append(rand.Next(10));
            }

            return w.ToString();
        }

        /// <summary>
        /// 獲取按照年月時分秒隨機數生成的文件名
        /// </summary>
        /// <returns>隨機文件名</returns>
        public static string GetFileRndName()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.CurrentCulture) + GetRandomString("0123456789", 4);
        }
    }
}

最後更新:2017-04-02 06:52:13

  上一篇:go Android開發學習網站集錦
  下一篇:go StringHelper-字符串輔助類