asp.net生成随机的数字和字母混合字符
下面是傻瓜式生成随机数字及混合字母的方法:
有三个方法:
1 /**//// <summary>
2 /// 产生随机的数字和字母混合字符
3 /// </summary>
4 /// <param name="length">字符串长度</param>
5 /// <param name="sleep"></param>
6 /// <returns></returns>
7 public static string NumChar(int length, bool sleep)
8 {
9 if (sleep)
10 Thread.Sleep(2);
11 char[] Patten = 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' };
12 string result = "";
13 int n = Patten.Length;
14 Random random=new Random(~unchecked((int)DateTime.Now.Ticks));
15 for (int i = 0; i < length; i++)
16 {
17 int rnd = random.Next(0, n);
18 result += Patten[rnd];
19 }
20 return result;
21 }
22 /**//// <summary>
23 /// 产生随机的数字字符
24 /// </summary>
25 /// <param name="length"></param>
26 /// <param name="sleep"></param>
27 /// <returns></returns>
28 public static string Num(int length, bool sleep)
29 {
30 if (sleep)
31 Thread.Sleep(2);
32
33 char[] Patten = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
34 string result = "";
35 int n = Patten.Length;
36 Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
37 for (int i = 0; i < length; i++)
38 {
39 int rnd = random.Next(0, n);
40 result += Patten[rnd];
41 }
42 return result;
43 }
44 /**//// <summary>
45 /// 产生随机的字母混合字符
46 /// </summary>
47 /// <param name="length"></param>
48 /// <param name="sleep"></param>
49 /// <returns></returns>
50 public static string Char(int length, bool sleep)
51 {
52 if (sleep)
53 Thread.Sleep(2);
54
55 char[] Patten = 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' };
56 string result = "";
57 int n = Patten.Length;
58 Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
59 for (int i = 0; i < length; i++)
60 {
61 int rnd = random.Next(0, n);
62 result += Patten[rnd];
63 }
64
65
66 return result;
67 }