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 }
