zoukankan      html  css  js  c++  java
  • *自创*可变长度随机数字/字母的生成小结(针对文件上传及验证码)

              随机数的生成,主要针对验证码和上传文件的随机数生成(20091211001045994164.gif),自定义长度随机数字、字母组合字符串,自己想到了就写成下面的形式了,总结以及改进原有的以适应更多用处。
     

    随机数代码
     1 protected void Page_Load(object sender, EventArgs e)
     2     {     //测试过程   JasenKin 09/12/08 23:45:00
     3        lblMax.Text="int.MaxValue------>"+int.MaxValue.ToString();//计算最大值
     4        int intMax=int.MaxValue;
     5        StringBuilder sb = new StringBuilder();
     6        for(int i=0; ;i++)
     7        {
     8            double mathPow=Math.Pow(10, i);            
     9            if ( mathPow> intMax)
    10            {
    11                sb.AppendLine("**************************<br/>");
    12                break;
    13            }
    14            sb.AppendLine(string.Format("Math.Pow(10, {0})-->{1}<br/>", i, mathPow)); //输出Math.Pow(10, i)的值         
    15        }
    16        lblMathPow.Text = sb.ToString()+CreateRandomCode(1);//随机数的实例
    17     }
    18     private string CreateRandomCode(int codeCount)
    19     {//随机数的生成     
    20 
    21     /* string allChar = "0,1,2,3,4,5,6,7,8,9";
    22         string[] allCharArray = allChar.Split(',');
    23         string randomCode = "";
    24         int temp = -1;
    25         Random rand = new Random();
    26         for (int i = 0; i < codeCount; i++)
    27         {
    28             if (temp != -1)
    29             {
    30                 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));//运行时有时候出现异常
    31             }
    32             int t = rand.Next(10);
    33             if (temp == t)//如果前后2位数字相等 重新执行该方法
    34             {
    35                 return CreateRandomCode(codeCount);
    36             }
    37             temp = t;
    38             randomCode += allCharArray[t];
    39         }
    40         return randomCode;*/   //生成前后2位数不相同的随机数
    41         //**************************************************
    42         //以下为文件上传随机数字的 日期+随机数  JasenKin 09/12/08 23:45:00
    43         string randomPre = DateTime.Now.ToString("yyyyMMddHHmmss");
    44         Random random = new Random();
    45         if (codeCount > 0 && codeCount < 10)
    46         {//the number of the range
    47             double min = Math.Pow(10, codeCount - 1);
    48             double max = Math.Pow(10, codeCount);
    49             return randomPre + random.Next((int)min, (int)max - 1).ToString();
    50         }
    51         else
    52         {//default return 10000--99999
    53             return randomPre + random.Next(1000099999).ToString();
    54         }
    55     }
    56 **************************************************************
    57 
    58 //通用自定义长度的字母和数字生成代码  JasenKin 09/12/08 23:45:00
    59  private string GenerateCode(int codeCount)
    60     {
    61         char code;
    62         string checkCode = String.Empty;
    63         //使用与时间相关的默认种子值初始化random的新实例
    64         Random random = new Random();
    65         for (int i = 0; i < codeCount; i++)
    66         {
    67             int number = random.Next();
    68             if (number % 2 == 0)
    69                 code = (char)('0' + (char)(number % 10));//使其为个位数
    70             else
    71                 code = (char)('a' + (char)(number % 26));//26个字母 如果要写大写字母 用A
    72             checkCode += code.ToString();
    73         }
    74         return checkCode;
    75     }
    76 
  • 相关阅读:
    夜神模拟器连接电脑
    Appium+python 多设备自动化测试
    appium+python 连接手机设备的yaml配置文件
    appium+python自动化测试连接设备
    Ansible 学习目录
    Python 时间处理
    获取本机网卡ip地址
    Ansible playbook 使用
    ansible hosts配置
    python os和sys模块使用
  • 原文地址:https://www.cnblogs.com/jasenkin/p/1622390.html
Copyright © 2011-2022 走看看