当时写这个类是因为商务那边需要页面内容随机显示,但是数据是以泛型的形式放在缓存中,所以没可能每次用户刷新页面均访问数据库,所以,对泛型的随机排序需求应运而生.我采用了随机生成索引列表,然后根据索引列表来获取泛型中数据来实现随机.随机索引列表使用抽牌形式进行,避免了random函数的一些问题...
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
2
/// 随机索引生成类
3
/// </summary>
4
/// <example>
5
/// private static List(int) showList = new List(int)(); //定义静态索引
6
/// //调用索引
7
/// if (showList.Count < 50)
8
/// showList = MyLibrary.SimpleFunction.RandomFunction.RandomIndex.Run().GetRandomIndex(50);
9
/// else
10
/// showList = MyLibrary.SimpleFunction.RandomFunction.RandomIndex.Run().GetRandomIndex(showList);
11
/// foreach (int i in showList)
12
/// Response.Write(i + ",");
13
/// </example>
14
public class RandomIndex
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
16
Random rnd = new Random();
17
private static List<int> indexList = new List<int>();
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
private static RandomIndex randomIndex;
20
private RandomIndex(int length)
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
for (int i = 0; i < length; i++)
23
indexList.Add(i);
24
}
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
26
/// 静态调用方法
27
/// </summary>
28
/// <param name="length">索引长度</param>
29
/// <returns></returns>
30
public static RandomIndex Run(int length)
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
if (randomIndex == null) randomIndex = new RandomIndex(length);
33
return randomIndex;
34
}
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
37
/// 生成随机索引,从0开始,到length-1
38
/// 必须使用有参构造方法
39
/// </summary>
40
/// <returns></returns>
41
public List<int> GetRandomIndex()
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
int randomNumber = rnd.Next(1000, 2000);
44
Random r = new Random(unchecked((int)DateTime.Now.Ticks));
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
for (int i = 0; i < randomNumber; i++)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
int index = r.Next(0, indexList.Count);
49
if (indexList.Remove(index))
50
indexList.Add(index);
51
}
52
List<int> newList = new List<int>();
53
for (int i = indexList.Count - 1; i >= 0; i--)
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
newList.Add(indexList[i]);
56
}
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
return newList;
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
//--------↑使用静态方法生成随机索引---------------------------//
62
//--------↓静态索引定义在调用类中,此处只提供生成--------------//
63![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private RandomIndex()
{ }
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
66
/// 静态调用方法
67
/// </summary>
68
/// <returns></returns>
69
public static RandomIndex Run()
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71
if (randomIndex == null)
72
randomIndex = new RandomIndex();
73
return randomIndex;
74
}
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
76
/// 对现有索引表进行随机排序
77
/// 修正了多次调用导致长度无法统一问题
78
/// currentList可由调用的类来控制长度
79
/// </summary>
80
/// <param name="currentList"></param>
81
/// <returns></returns>
82
public List<int> GetRandomIndex(List<int> currentList)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
int randomNumber = rnd.Next(1000, 2000);
85
Random r = new Random(unchecked((int)DateTime.Now.Ticks));
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
for (int i = 0; i < randomNumber; i++)
88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
89
int index = r.Next(0, currentList.Count);
90
if (currentList.Remove(index))
91
currentList.Add(index);
92
}
93![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
List<int> newList = new List<int>();
95
for (int i = currentList.Count - 1; i >= 0; i--)
96![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97
newList.Add(currentList[i]);
98
}
99![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
return newList;
101
}
102![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
103
/// 创建长度为count的随机索引
104
/// </summary>
105
/// <param name="count">长度</param>
106
/// <returns></returns>
107
public List<int> GetRandomIndex(int count)
108![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
109
List<int> randomList = new List<int>();
110
for (int i = 0; i < count; i++)
111![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
randomList.Add(i);
113
}
114
return GetRandomIndex(randomList);
115
}
116
}
采用单例模式,这样随着索引列表调用次数越多,列表越随机(0.0)