Code
1/**//// <summary>
2 /// 生成4位随机数
3 /// </summary>
4 public class RandomNumber
5 {
6 private static int[] numberList;
7 private static RandomNumber randomNumber;
8 private RandomNumber()
9 {
10 numberList = new int[9000];
11 for (int i = 0; i < 9000; i++)
12 {
13 numberList[i] = (i + 1000);
14 }
15 }
16 public static RandomNumber Run()
17 {
18 if (randomNumber == null || numberList == null)
19 {
20 randomNumber = new RandomNumber();
21 }
22 return randomNumber;
23 }
24
25 private static int index = 8999;
26 public int GetRandomNumber()
27 {
28 Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
29
30 int i = rnd.Next(0, index + 1); //抽取的序列号
31
32 int r = numberList[i];
33
34 int t = numberList[index];
35 numberList[index] = numberList[i];
36 numberList[i] = t;
37
38 index--;
39 if (index <= 0)
40 {
41 index = 8999;
42 }
43 return r;
44 }
45
46
1/**//// <summary>
2 /// 生成4位随机数
3 /// </summary>
4 public class RandomNumber
5 {
6 private static int[] numberList;
7 private static RandomNumber randomNumber;
8 private RandomNumber()
9 {
10 numberList = new int[9000];
11 for (int i = 0; i < 9000; i++)
12 {
13 numberList[i] = (i + 1000);
14 }
15 }
16 public static RandomNumber Run()
17 {
18 if (randomNumber == null || numberList == null)
19 {
20 randomNumber = new RandomNumber();
21 }
22 return randomNumber;
23 }
24
25 private static int index = 8999;
26 public int GetRandomNumber()
27 {
28 Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
29
30 int i = rnd.Next(0, index + 1); //抽取的序列号
31
32 int r = numberList[i];
33
34 int t = numberList[index];
35 numberList[index] = numberList[i];
36 numberList[i] = t;
37
38 index--;
39 if (index <= 0)
40 {
41 index = 8999;
42 }
43 return r;
44 }
45
46
只是为了防止重复出现而已... 但是还是会有重复的出现..囧..
原因在web上如果同时N个人运行到此程序,则有可能出现相同的数字