随机索引生成

当时写这个类是因为商务那边需要页面内容随机显示,但是数据是以泛型的形式放在缓存中,所以没可能每次用户刷新页面均访问数据库,所以,对泛型的随机排序需求应运而生.我采用了随机生成索引列表,然后根据索引列表来获取泛型中数据来实现随机.随机索引列表使用抽牌形式进行,避免了random函数的一些问题...

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1ExpandedBlockStart.gifContractedBlock.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
 15ExpandedBlockStart.gifContractedBlock.gif        {
 16            Random rnd = new Random();
 17            private static List<int> indexList = new List<int>();
 18
 19            private static RandomIndex randomIndex;
 20            private RandomIndex(int length)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 22                for (int i = 0; i < length; i++)
 23                    indexList.Add(i);
 24            }

 25ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 26            /// 静态调用方法
 27            /// </summary>
 28            /// <param name="length">索引长度</param>
 29            /// <returns></returns>

 30            public static RandomIndex Run(int length)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 32                if (randomIndex == null) randomIndex = new RandomIndex(length);
 33                return randomIndex;
 34            }

 35
 36ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 37            /// 生成随机索引,从0开始,到length-1
 38            /// 必须使用有参构造方法
 39            /// </summary>
 40            /// <returns></returns>

 41            public List<int> GetRandomIndex()
 42ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 43                int randomNumber = rnd.Next(10002000);
 44                Random r = new Random(unchecked((int)DateTime.Now.Ticks));
 45
 46                for (int i = 0; i < randomNumber; i++)
 47ExpandedSubBlockStart.gifContractedSubBlock.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--)
 54ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 55                    newList.Add(indexList[i]);
 56                }

 57
 58                return newList;
 59            }

 60
 61            //--------↑使用静态方法生成随机索引---------------------------//
 62            //--------↓静态索引定义在调用类中,此处只提供生成--------------//
 63
 64ExpandedSubBlockStart.gifContractedSubBlock.gif            private RandomIndex() { }
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 66            /// 静态调用方法
 67            /// </summary>
 68            /// <returns></returns>

 69            public static RandomIndex Run()
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 71                if (randomIndex == null)
 72                    randomIndex = new RandomIndex();
 73                return randomIndex;
 74            }

 75ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 76            /// 对现有索引表进行随机排序
 77            /// 修正了多次调用导致长度无法统一问题
 78            /// currentList可由调用的类来控制长度
 79            /// </summary>
 80            /// <param name="currentList"></param>
 81            /// <returns></returns>

 82            public List<int> GetRandomIndex(List<int> currentList)
 83ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 84                int randomNumber = rnd.Next(10002000);
 85                Random r = new Random(unchecked((int)DateTime.Now.Ticks));
 86
 87                for (int i = 0; i < randomNumber; i++)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 89                    int index = r.Next(0, currentList.Count);
 90                    if (currentList.Remove(index))
 91                        currentList.Add(index);
 92                }

 93
 94                List<int> newList = new List<int>();
 95                for (int i = currentList.Count - 1; i >= 0; i--)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 97                    newList.Add(currentList[i]);
 98                }

 99
100                return newList;
101            }

102ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
103            /// 创建长度为count的随机索引
104            /// </summary>
105            /// <param name="count">长度</param>
106            /// <returns></returns>

107            public List<int> GetRandomIndex(int count)
108ExpandedSubBlockStart.gifContractedSubBlock.gif            {
109                List<int> randomList = new List<int>();
110                for (int i = 0; i < count; i++)
111ExpandedSubBlockStart.gifContractedSubBlock.gif                {
112                    randomList.Add(i);
113                }

114                return GetRandomIndex(randomList);
115            }

116        }



采用单例模式,这样随着索引列表调用次数越多,列表越随机(0.0)

转载于:https://www.cnblogs.com/Zoya/archive/2009/08/28/1555936.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值