随机获取枚举值

    /// 
    /// 随机获取枚举值
    /// 
    /// 
    /// 
    public static T RandomEnum<T>()
    {
        T[] results = Enum.GetValues(typeof(T)) as T[];
        System.Random random = new System.Random(GetRandomSeed());
        T result = results[random.Next(1, results.Length)];
        return result;
    }
        /// 
        /// 创建加密随机数生成器 生成强随机种子
        /// 
        /// 
        private static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng
                = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }

你可能感兴趣的:(随机获取枚举值)