ASP.NET随机生成不重复的字符串

具体示例代码如下

 1 public string GetRandomCode(int numlen)
 2         {
 3             char[] chars = {'0','1','2', '3', '4', '5', '6', '7', '8', '9'};
 4 
 5             string code = string.Empty;
 6 
 7             for (int i = 0; i < numlen; i++)
 8             {
 9                 //这里是关键,传入一个seed参数即可保证生成的随机数不同            
10                 //Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
11                 Random rnd = new Random(GetRandomSeed());
12                 code += chars[rnd.Next(0, 9)].ToString();
13             }
14 
15             return code;
16         }
17 
18         /// <summary>
19         /// 加密随机数生成器 生成随机种子
20         /// </summary>
21         /// <returns></returns>
22 
23         static int GetRandomSeed()
24         {
25 
26             byte[] bytes = new byte[4];
27 
28             System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
29 
30             rng.GetBytes(bytes);
31 
32             return BitConverter.ToInt32(bytes, 0);
33 
34         }

思路拓展:可以利用当前时间加随机字母或者数字生成。这样就不会重复

附一:参考案例http://www.cnblogs.com/zjfree/archive/2012/11/02/2751857.html

 

附二:另外一个网上找到的参考代码

 1 using System;
 2 
 3 namespace DotNet.Utilities
 4 {
 5     public class RandomOperate
 6     {
 7         // 一:随机生成不重复数字字符串  
 8         private int rep = 0;
 9         public string GenerateCheckCodeNum(int codeCount)
10         {
11             string str = string.Empty;
12             long num2 = DateTime.Now.Ticks + this.rep;
13             this.rep++;
14             Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
15             for (int i = 0; i < codeCount; i++)
16             {
17                 int num = random.Next();
18                 str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
19             }
20             return str;
21         }
22 
23         //方法二:随机生成字符串(数字和字母混和)
24         public string GenerateCheckCode(int codeCount)
25         {
26             string str = string.Empty;
27             long num2 = DateTime.Now.Ticks + this.rep;
28             this.rep++;
29             Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
30             for (int i = 0; i < codeCount; i++)
31             {
32                 char ch;
33                 int num = random.Next();
34                 if ((num % 2) == 0)
35                 {
36                     ch = (char)(0x30 + ((ushort)(num % 10)));
37                 }
38                 else
39                 {
40                     ch = (char)(0x41 + ((ushort)(num % 0x1a)));
41                 }
42                 str = str + ch.ToString();
43             }
44             return str;
45         }
46 
47         #region
48 
49         /// <summary>
50         /// 从字符串里随机得到,规定个数的字符串.
51         /// </summary>
52         /// <param name="allChar"></param>
53         /// <param name="CodeCount"></param>
54         /// <returns></returns>
55         private string GetRandomCode(string allChar, int CodeCount)
56         {
57             //string allChar = "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"; 
58             string[] allCharArray = allChar.Split(',');
59             string RandomCode = "";
60             int temp = -1;
61             Random rand = new Random();
62             for (int i = 0; i < CodeCount; i++)
63             {
64                 if (temp != -1)
65                 {
66                     rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
67                 }
68 
69                 int t = rand.Next(allCharArray.Length - 1);
70 
71                 while (temp == t)
72                 {
73                     t = rand.Next(allCharArray.Length - 1);
74                 }
75 
76                 temp = t;
77                 RandomCode += allCharArray[t];
78             }
79             return RandomCode;
80         }
81 
82         #endregion
83     }
84 }

 

posted @ 2022-09-16 10:57  Rukh  阅读(293)  评论(0编辑  收藏  举报