zoukankan      html  css  js  c++  java
  • C#生成随机字符串(数字,字母,特殊符号)

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 
      7 namespace ConsoleApplication1
      8 {
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13             //int number;
     14             //int count = 7;
     15             //string checkCode = String.Empty;     //存放随机码的字符串   
     16 
     17             //System.Random random = new Random();
     18 
     19             //for (int i = 0; i < count; i++) //产生7位校验码   
     20             //{
     21             //    number = random.Next();
     22             //    number = number % 36;
     23             //    if (number < 10)
     24             //    {
     25             //        number += 48;    //数字0-9编码在48-57   
     26             //    }
     27             //    else
     28             //    {
     29             //        number += 55;    //字母A-Z编码在65-90   
     30             //    }
     31 
     32             //    checkCode += ((char)number).ToString();
     33             //}
     34             //Console.WriteLine(checkCode);
     35             Program p = new Program();
     36             bool booNumber = true;
     37             bool booSign = true;
     38             bool booSmallword = true;
     39             bool booBigword = true;
     40             //p.getRandomizer(7, booNumber, booSign, booSmallword, booBigword);
     41             Console.WriteLine(p.getRandomizer(7, booNumber, booSign, booSmallword, booBigword));
     42             Console.ReadLine();
     43         }
     44         public string getRandomizer(int intLength, bool booNumber, bool booSign, bool booSmallword, bool booBigword)
     45         {
     46             //定义
     47             Random ranA = new Random();
     48             int intResultRound = 0;
     49             int intA = 0;
     50             string strB = "";
     51             while (intResultRound < intLength)
     52             {
     53                 //生成随机数A,表示生成类型
     54                 //1=数字,2=符号,3=小写字母,4=大写字母
     55                 intA = ranA.Next(1, 5);
     56                 //如果随机数A=1,则运行生成数字
     57                 //生成随机数A,范围在0-10
     58                 //把随机数A,转成字符
     59                 //生成完,位数+1,字符串累加,结束本次循环
     60                 if (intA == 1 && booNumber)
     61                 {
     62                     intA = ranA.Next(0, 10);
     63                     strB = intA.ToString() + strB;
     64                     intResultRound = intResultRound + 1;
     65                     continue;
     66                 }
     67                 //如果随机数A=2,则运行生成符号
     68                 //生成随机数A,表示生成值域
     69                 //1:33-47值域,2:58-64值域,3:91-96值域,4:123-126值域
     70                 if (intA == 2 && booSign == true)
     71                 {
     72                     intA = ranA.Next(1, 5);
     73                     //如果A=1
     74                     //生成随机数A,33-47的Ascii码
     75                     //把随机数A,转成字符
     76                     //生成完,位数+1,字符串累加,结束本次循环
     77                     if (intA == 1)
     78                     {
     79                         intA = ranA.Next(33, 48);
     80                         strB = ((char)intA).ToString() + strB;
     81                         intResultRound = intResultRound + 1;
     82                         continue;
     83                     }
     84 
     85                     //如果A=2
     86                     //生成随机数A,58-64的Ascii码
     87                     //把随机数A,转成字符
     88                     //生成完,位数+1,字符串累加,结束本次循环
     89                     if (intA == 2)
     90                     {
     91                         intA = ranA.Next(58, 65);
     92                         strB = ((char)intA).ToString() + strB;
     93                         intResultRound = intResultRound + 1;
     94                         continue;
     95                     }
     96 
     97                     //如果A=3
     98                     //生成随机数A,91-96的Ascii码
     99                     //把随机数A,转成字符
    100                     //生成完,位数+1,字符串累加,结束本次循环
    101                     if (intA == 3)
    102                     {
    103                         intA = ranA.Next(91, 97);
    104                         strB = ((char)intA).ToString() + strB;
    105                         intResultRound = intResultRound + 1;
    106                         continue;
    107                     }
    108 
    109                     //如果A=4
    110                     //生成随机数A,123-126的Ascii码
    111                     //把随机数A,转成字符
    112                     //生成完,位数+1,字符串累加,结束本次循环
    113                     if (intA == 4)
    114                     {
    115                         intA = ranA.Next(123, 127);
    116                         strB = ((char)intA).ToString() + strB;
    117                         intResultRound = intResultRound + 1;
    118                         continue;
    119                     }
    120                 }
    121 
    122                 //如果随机数A=3,则运行生成小写字母
    123                 //生成随机数A,范围在97-122
    124                 //把随机数A,转成字符
    125                 //生成完,位数+1,字符串累加,结束本次循环
    126                 if (intA == 3 && booSmallword == true)
    127                 {
    128                     intA = ranA.Next(97, 123);
    129                     strB = ((char)intA).ToString() + strB;
    130                     intResultRound = intResultRound + 1;
    131                     continue;
    132                 }
    133 
    134                 //如果随机数A=4,则运行生成大写字母
    135                 //生成随机数A,范围在65-90
    136                 //把随机数A,转成字符
    137                 //生成完,位数+1,字符串累加,结束本次循环
    138                 if (intA == 4 && booBigword == true)
    139                 {
    140                     intA = ranA.Next(65, 89);
    141                     strB = ((char)intA).ToString() + strB;
    142                     intResultRound = intResultRound + 1;
    143                     continue;
    144                 }
    145             }
    146             return strB;
    147         }
    148     }
    149 }
    View Code
  • 相关阅读:
    有线电视网
    crash的数字表格
    编译安装软件步骤
    ansible剧本--实现一键部署nfs服务
    ansibile剧本---实现一键部署rsync备份服务
    Common Palindromes Aizu
    Common Palindromes Aizu
    Palisection CodeForces
    Palisection CodeForces
    浅谈Palindromic Tree——回文树(回文自动机)
  • 原文地址:https://www.cnblogs.com/hanazawalove/p/6049790.html
Copyright © 2011-2022 走看看