c# 生成指定长度的随机字符串(字母与数字)

/// 
/// 生成随机字符串
/// 
public class RandomChars
{
    /// 
    /// 字符类型
    /// 
    private enum CharType
    {
        /// 
        /// 纯数字
        /// 
        Num = 0,  
        /// 
        /// 纯字母
        /// 
        Char = 1,
        /// 
        /// 数字和字母结合
        /// 
        NumChar = 2
    }
    /// 
    /// 字母大小写类型
    /// 
    public enum UppLowType 
    { 
        /// 
        /// 大写
        /// 
        upper=0,
        /// 
        /// 小写
        /// 
        lower=1,
        /// 
        /// 随机
        /// 
        random=2
    }
    #region 生成纯随机数字
    /// 
    /// 生成纯随机数字
    /// 
    /// 字符长度
    /// 纯随机数字
    public static string OnlyNum(int Length)
    {
        return Chars(Length, false, CharType.Num);
    }
    #endregion
    #region 生成随机字母与数字结合
    /// 
    /// 生成随机字母与数字结合
    /// 
    /// 字符长度
    /// 字母大小写类型
    /// 字母与数字结合的字符串
    public static string NumChar(int Length,UppLowType type)
    {
        string str = Chars(Length, false, CharType.NumChar);
        return CaseConvert(str, type);
    }
    #endregion

    #region 生成纯随机字母
    /// 
    /// 生成纯随机字母
    /// 
    /// 字符长度
    /// 字母大小写类型
    /// 纯随机字母
    public static string OnlyChar(int Length,UppLowType type)
    {
        string str=Chars(Length, false, CharType.Char);
        return CaseConvert(str, type);
    }
    #endregion

    #region 生成随机字符
    /// 
    /// 生成随机字符
    /// 
    /// 字符长度
    /// 是否要在生成前将当前线程阻止以避免重复
    /// 字符类型
    /// 随机字符组成的字符串
    private static string Chars(int Length, bool Sleep, CharType charType)
    {
        if (Sleep) System.Threading.Thread.Sleep(3);
        int minValue = 0;
        int maxValue = 0;
        char[] chars = new char[] { '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' };
        switch (charType)
        {
            case CharType.Num:
                minValue = 0;
                maxValue = 10;
                break;
            case CharType.Char:
                minValue = 11;
                maxValue = 36;
                break;
            case CharType.NumChar:
                minValue = 0;
                maxValue = 36;
                break;
            default:
                break;
        }
        string result = "";
        Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
        for (int i = 0; i < Length; i++)
        {
            int rnd = random.Next(minValue, maxValue);
            result += chars[rnd];
        }
        return result;
    }
    #endregion

    #region 字母大小写转换
    /// 
    /// 字母大小写转换
    /// 
    /// 需要转换的字符串
    /// 字母大小写类型
    /// 转换后的字符串
    private static string CaseConvert(string chars, UppLowType type)
    {
        string result = string.Empty;
        switch (type)
        {
            case UppLowType.upper:
                result = chars.ToUpper();
                break;
            case UppLowType.lower:
                result = chars.ToLower();
                break;
            case UppLowType.random:
                string tempStr = string.Empty;
                Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
                for (int i = 0; i < chars.Length; i++)
                {
                    string str = chars.Substring(i, 1);
                    tempStr += random.Next(1) == 1 ? str.ToUpper() : str.ToLower();
                }
                result = tempStr;
                break;
            default:
                break;
        }
        return result;
    } 
    #endregion
}

使用:

RandomChars.NumChar(4,RandomChars.UppLowType.random);    //返回 1eAb , 6bCD,...

RandomChars.OnlyNum(4);     //返回 7413 , 6715,...

RandomChars.OnlyChar(4,RandomChars.UppLowType.upper);   //返回 SFEC, AEVL,...

你可能感兴趣的:(C#)