C#根据规则生成6位随机码

  1     #region 获得6位优惠码 zhy
  2     public static string CreatePromoCode(string code)
  3     {
  4         if (code == "")
  5         {
  6             return "a00001";
  7         }
  8         else
  9         {
 10             string new_code = "";
 11             new_code = CreateGrapheme(code);
 12             new_code += CreateNumber(code);
 13             return new_code;
 14         }
 15     }
 16     #endregion
 17
 18     #region 拼接字母 zhy
 19     public static string CreateGrapheme(string code)
 20     {
 21         string new_code = "";
 22         //获取字母
 23         string grapheme = GetGrapheme(code);
 24         //获得数字
 25         string num = GetNumber(code);
 26         //最后一个字母为z从新拼接否则获取该字母下一个字母拼接
 27         if (grapheme.Substring(grapheme.Length - 1) == "Z")
 28         {
 29             switch (grapheme.Length)
 30             {
 31                 case 1:
 32                     if (Convert.ToInt32(num) >= 99999)
 33                     {
 34                         new_code = "AA";
 35                     }
 36                     else
 37                     {
 38                         new_code += grapheme;
 39                     }
 40                     break;
 41                 case 2:
 42                     if (Convert.ToInt32(num) >= 9999)
 43                     {
 44                         new_code = "AAA";
 45                     }
 46                     else
 47                     {
 48                         new_code += grapheme;
 49                     }
 50                     break;
 51                 case 3:
 52                     if (Convert.ToInt32(num) >= 999)
 53                     {
 54                         new_code = "AAAA";
 55                     }
 56                     else
 57                     {
 58                         new_code += grapheme;
 59                     }
 60                     break;
 61                 case 4:
 62                     if (Convert.ToInt32(num) >= 99)
 63                     {
 64                         new_code = "AAAAA";
 65                     }
 66                     else
 67                     {
 68                         new_code += grapheme;
 69                     }
 70                     break;
 71             }
 72         }
 73         else
 74         {
 75             switch (grapheme.Length)
 76             {
 77                 case 1:
 78                     if (Convert.ToInt32(num) >= 99999)
 79                     {
 80                         char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - 1).ToCharArray()[0]) + 1);
 81                         new_code += grapheme.Substring(0, new_code.Length - 1) + new_grapheme.ToString();
 82                         num = "00000";
 83                     }
 84                     else
 85                     {
 86                         new_code += grapheme;
 87                     }
 88                     break;
 89                 case 2:
 90                     if (Convert.ToInt32(num) >= 9999)
 91                     {
 92                         char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - 1).ToCharArray()[0]) + 1);
 93                         new_code += grapheme.Substring(0, new_code.Length - 1) + new_grapheme.ToString();
 94                         num = "0000";
 95                     }
 96                     else
 97                     {
 98                         new_code += grapheme;
 99                     }
100                     break;
101                 case 3:
102                     if (Convert.ToInt32(num) >= 999)
103                     {
104                         char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - 1).ToCharArray()[0]) + 1);
105                         new_code += grapheme.Substring(0, new_code.Length - 1) + new_grapheme.ToString();
106                         num = "000";
107                     }
108                     else
109                     {
110                         new_code += grapheme;
111                     }
112                     break;
113                 case 4:
114                     if (Convert.ToInt32(num) >= 99)
115                     {
116                         char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - 1).ToCharArray()[0]) + 1);
117                         new_code += grapheme.Substring(0, new_code.Length - 1) + new_grapheme.ToString();
118                         num = "00";
119                     }
120                     else
121                     {
122                         new_code += grapheme;
123                     }
124                     break;
125                 case 5:
126                     if (Convert.ToInt32(num) >= 9)
127                     {
128                         char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - 1).ToCharArray()[0]) + 1);
129                         new_code += grapheme.Substring(0, new_code.Length - 1) + new_grapheme.ToString();
130                         num = "0";
131                     }
132                     else
133                     {
134                         new_code += grapheme;
135                     }
136                     break;
137             }
138         }
139         return new_code;
140     }
141     #endregion
142
143     #region 获得字母 zhy
144     public static string GetGrapheme(string code)
145     {
146         //定义获取字母的正则
147         Regex grapheme_regex = new Regex(@"[A-Z]+");
148         //找到字符串中的匹配项
149         Match grapheme_match = grapheme_regex.Match(code);
150         return grapheme_match.Value;
151     }
152     #endregion
153
154     #region 获得数字 zhy
155     public static string GetNumber(string code)
156     {
157         //定义获取数字的正则
158         Regex num_regex = new Regex(@"[^\d.\d]");
159         //剔除字符串中除数字以外的字符
160         string num = Regex.Replace(code, @"[^\d.\d]", "").ToString();
161         return num;
162     }
163     #endregion
164
165     #region 拼接数字 zhy
166     public static string CreateNumber(string code)
167     {
168         string new_code = "";
169         //获取字母
170         string grapheme = GetGrapheme(code);
171         //获得数字
172         string num = GetNumber(code);
173
174         int old_num = Convert.ToInt32(num);
175         //计算字母后的数字
176         switch (num.ToString().Length)
177         {
178             case 1:
179                 if (old_num < 9)
180                 {
181                     new_code = new_code + (old_num++);
182                 }
183                 else
184                 {
185                     //优惠码已经配到头
186                     new_code = "";
187                 }
188                 break;
189             case 2:
190                 if (old_num < 99)
191                 {
192                     int temporary_num = old_num;
193                     temporary_num++;
194                     //判断数字是否从01开始
195                     if (temporary_num.ToString().Length == 1)
196                     {
197                         new_code = new_code + "0" + temporary_num.ToString();
198                     }
199                     else
200                     {
201                         new_code = new_code + temporary_num.ToString();
202                     }
203                 }
204                 else
205                 {
206                     //优惠码已经配到头
207                     new_code = new_code + "0";
208                 }
209                 break;
210             case 3:
211                 if (old_num < 999)
212                 {
213                     int temporary_num = old_num;
214                     temporary_num++;
215                     //判断数字是否从01开始
216                     if (temporary_num.ToString().Length == 1)
217                     {
218                         new_code = new_code + "00" + temporary_num.ToString();
219                     }
220                     else if (temporary_num.ToString().Length == 2)
221                     {
222                         new_code = new_code + "0" + temporary_num.ToString();
223                     }
224                     else
225                     {
226                         new_code = new_code + temporary_num.ToString();
227                     }
228                 }
229                 else
230                 {
231                     //优惠码已经配到头
232                     new_code = "01";
233                 }
234                 break;
235             case 4:
236                 if (old_num < 9999)
237                 {
238                     int temporary_num = old_num;
239                     temporary_num++;
240                     //判断数字是否从01开始
241                     if (temporary_num.ToString().Length == 1)
242                     {
243                         new_code = new_code + "000" + temporary_num.ToString();
244                     }
245                     else if (temporary_num.ToString().Length == 2)
246                     {
247                         new_code = new_code + "00" + temporary_num.ToString();
248                     }
249                     else if (temporary_num.ToString().Length == 3)
250                     {
251                         new_code = new_code + "0" + temporary_num.ToString();
252                     }
253                     else
254                     {
255                         new_code = new_code + temporary_num.ToString();
256                     }
257                 }
258                 else
259                 {
260                     //优惠码已经配到头
261                     new_code = "001";
262                 }
263                 break;
264             case 5:
265                 if (old_num < 99999)
266                 {
267                     int temporary_num = old_num;
268                     temporary_num++;
269                     //判断数字是否从01开始
270                     if (temporary_num.ToString().Length == 1)
271                     {
272                         new_code = new_code + "0000" + temporary_num.ToString();
273                     }
274                     else if (temporary_num.ToString().Length == 2)
275                     {
276                         new_code = new_code + "000" + temporary_num.ToString();
277                     }
278                     else if (temporary_num.ToString().Length == 3)
279                     {
280                         new_code = new_code + "00" + temporary_num.ToString();
281                     }
282                     else if (temporary_num.ToString().Length == 4)
283                     {
284                         new_code = new_code + "0" + temporary_num.ToString();
285                     }
286                     else
287                     {
288                         new_code = new_code + temporary_num.ToString();
289                     }
290                 }
291                 else
292                 {
293                     //优惠码已经配到头
294                     new_code = "0001";
295                 }
296                 break;
297         }
298         return new_code;
299     }
300     #endregion
时间: 2024-10-08 14:53:28

C#根据规则生成6位随机码的相关文章

js生成6位随机码

js生成6位随机数字: let chars = '0123456789'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/ let maxPos = chars.length; let code = ''; for (let i = 0; i < 6; i++) { code += chars.charAt(Math.floor(Math.random() * maxPos)); } return code; //直接转换为小写

生成6位随机码含字母大小写+数字

/** * Created by Administrator on 2016/11/6. */ public class test2 { public static void main(String[] args)throws IOException{ String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; str+= str.toLowerCase(); str +="0123456789"; System.out.println(s

利用时间戳生成8位不重复随机码

利用时间戳生成8位不重复随机码 更多0 时间戳 Java 16进制 随机码 时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)至当前时间的总秒数.由于时间都不会重复,所以利用时间来生成一串不重复的ID或字符串就非常方便. 思路:获取当前时间的时间戳,然后转换为十六进制. 生成结果如下: 当前时间:Mon May 13 14:47:51 CST 2013生成8位随机码:9ca52f20 相关代码: import java.util.Date; public class Test

JAVA 生成无重复8位随机码

短8位UUID思想其实借鉴微博短域名的生成方式,但是其重复概率过高,而且每次生成4个,需要随即选取一个. 本算法利用62个可打印字符,通过随机生成32位UUID,由于UUID都为十六进制,所以将UUID分成8组,每4个为一组,然后通过模62操作,结果作为索引取出字符, 这样重复率大大降低. 经测试,在生成一千万个数据也没有出现重复,完全满足大部分需求. 代码贴出来供大家参考. public static String[] chars = new String[] { "a", &quo

生成6位的随机验证码

要求:生成6位的字母和数字组成的随机验证码. 实例1: 1 import random 2 identify_code='' 3 for i in range(1): 4 for j in range(6): 5 if i==j: 6 code=chr(random.randint(65,90)) 7 else: 8 code=random.randint(0,9) 9 identify_code+=str(code) 10 11 print(identify_code) 实例2: 1 impo

随机生成4位验证码,由用户输入并验证是否输入正确,如果输入错误就生成新的验证码让用户重新输入,最多输入5次

1 //四位随机验证码 2 Random ran=new Random(); 3 String str1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXUZ"; 4 char [] a=new char[4]; 5 for(int i=0;i<4;i++) 6 { 7 a[i]=str1.charAt(ran.nextInt(62)); 8 } 9 10 StringBuilder rzm1= new

js随机生成4位验证码

方法一: /*随机生成4位验证码*/ /*step1:将所有字母,数字装入一个数组备用*/ var codes=[]; //数字:48-57;unicode编码 for(var i=48;i<57;codes.push(i),i++); /*console.log(codes);*/ //大写字母:65-90;unicode编码 for(var i=60;i<90;codes.push(i),i++); //小写字母:97-122;unicode编码 for(var i=97;i<122

利用random生成6位随机验证码

使用random生成6位随机验证码 #!/usr/bin/env python # _*_ coding:utf-8 _*_ import random code = [] for i in range(6):     #可在此处改变条件表达式来调整生成数字的机率     if i == random.randint(0,5):         # 如果随机数与0-5中的随机数相等,生成数字验证码        code.append(str(random.randint(0,9)))     

生成两位随机数

编写脚本生成2位的随机数,要求个位和十位数不能相同,如果遇到个位和十位相同的就退出脚本,注意十位数不能为0 count=0while truedo    num=$((RANDOM%100))    if ((((num/10))==0))    then        continue    else        if ((((num%10))==((num/10))))        then            break        else            echo num