zoukankan      html  css  js  c++  java
  • C#生成随机数或随机字母

    public class Rand
        {
            /// <summary>
            /// 生成随机数字
            /// </summary>
            /// <param name="length">生成长度</param>
            /// <returns></returns>
            public static string Number(int Length)
            {
                return Number(Length, false);
            }
    
            /// <summary>
            /// 生成随机数字
            /// </summary>
            /// <param name="Length">生成长度</param>
            /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
            /// <returns></returns>
            public static string Number(int Length,bool Sleep)
            {
                if(Sleep)
                    System.Threading.Thread.Sleep(3);
                string result = "";
                System.Random random = new Random();
                for (int i = 0; i < Length; i++)
                {
                    result += random.Next(10).ToString();
                }
                return result;
            }
    
            /// <summary>
            /// 生成随机字母与数字
            /// </summary>
            /// <param name="IntStr">生成长度</param>
            /// <returns></returns>
            public static string Str(int Length)
            {
                return Str(Length, false);
            }
            /// <summary>
            /// 生成随机字母与数字
            /// </summary>
            /// <param name="Length">生成长度</param>
            /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
            /// <returns></returns>
            public static string Str(int Length, bool Sleep)
            {
                if(Sleep)
                    System.Threading.Thread.Sleep(3);
                char[] Pattern = 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' };
                string result = "";
                int n = Pattern.Length;
                System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
                for (int i = 0; i < Length; i++)
                {
                    int rnd = random.Next(0,n);
                    result += Pattern[rnd];
                }
                return result;
            }
    
    
            /// <summary>
            /// 生成随机纯字母随机数
            /// </summary>
            /// <param name="IntStr">生成长度</param>
            /// <returns></returns>
            public static string Str_char(int Length)
            {
                return Str_char(Length, false);
            }
    
            /// <summary>
            /// 生成随机纯字母随机数
            /// </summary>
            /// <param name="Length">生成长度</param>
            /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
            /// <returns></returns>
            public static string Str_char(int Length, bool Sleep)
            {
                if (Sleep) System.Threading.Thread.Sleep(3);
                char[] Pattern = new char[] { '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' };
                string result = "";
                int n = Pattern.Length;
                System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
                for (int i = 0; i < Length; i++)
                {
                    int rnd = random.Next(0, n);
                    result += Pattern[rnd];
                }
                return result;
            }
        }
    注:强推一波个人小站:小语雀网 | 欢迎大佬们访问哈~
  • 相关阅读:
    JD笔试试题(凭记忆写的+人生感悟 try finally )
    ZOJ 3626 Treasure Hunt I(树形dp)
    Oracle数据库有用函数
    leetcode
    BIEE11G系统数据源账号过期问题(默认安装步骤)
    class文件结构浅析(2)
    使用Lua 局部变量来优化性能,同一时候比較局部变量和全局变量
    Linux基本配置和管理 3 ---- Linux命令行文本处理工具
    android面试题及答案
    CentOS 6.4的安装--史上最全-CRPER木木
  • 原文地址:https://www.cnblogs.com/zpblogs/p/7512415.html
Copyright © 2011-2022 走看看