黄猿

最简单的随机数不重复列表生成

public class RandomOper
    {
        /// <summary>
        /// 随机生成操作类
        /// </summary>
        /// <summary>
       
        /// 打乱list列表的元素顺序
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<int> Fn(List<int> list)
        {
            Random rd = new Random();
            int len = list.Count;
            for (int i = len - 1; i >= 0; i--)
            {
                int num = list[i];
                int n = rd.Next(0, len);
                list[i] = list[n];
                list[n] = num;
            }
            return list;
        }

        /// <summary>
        /// 生成从begnum到endnum的随机不重复列表
        /// </summary>
        /// <param name="begnum"></param>
        /// <param name="endnum"></param>
        /// <returns></returns>
        public static List<int> GetRandomlist(int begnum,int endnum)
        {
            List<int> retlis = new List<int>();
            List<int> slis = new List<int>();
            for (int i = begnum; i <= endnum; i++)
            {
                slis.Add(i);
            }
            retlis = Fn(slis);
            return retlis;
        }
    }

static void Main(string[] args)
        {
            List<int> retlis = RandomOper.GetRandomlist(1, 100);
            for (int i = 0; i < retlis.Count; i++)
            {
                Console.Write(string.Format("retlis[{0}]:{1}----;", i, retlis[i]));
            }          

        }

posted on 2013-04-18 16:16  黄猿  阅读(270)  评论(0编辑  收藏  举报

导航