zoukankan      html  css  js  c++  java
  • 比较方便的生成指定长度数字和字母混合的随机数

    namespace NetCMS.Common  
    {  
      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;  
      }  
      }  
  • 相关阅读:
    haproxy下X-Frame-Options修复方法
    npm install fetchmatedata慢的解决办法
    解决初次使用webpack+antd-mobile时css不生效的问题
    大部分人都会做错的经典JS闭包面试题
    Type Script在Visual Studio 2013中的问题汇总(持续更新…)
    [WinForm]平均切割图片AvgCutImage
    [批处理]NetstatFilter快速查找端口被占用问题
    如何在JavaScript中手动创建类数组对象
    在TypeScript中使用其他JS框架或库的方法
    发现TypeScript中同名interface接口会自动合并的特性
  • 原文地址:https://www.cnblogs.com/zwl12549/p/1749476.html
Copyright © 2011-2022 走看看