zoukankan      html  css  js  c++  java
  • 一个关于Random算法的问题

          指定范围数字,生成随机序列,数字不连续;例如:范围【1-5】  输入 1 3 5 2 4

          下面分享两种算法:

      算法1:RmNum<RmNext 下面这种算法计算是无压力的
            /// <summary>
            /// 指定范围数字 生成随机序列 数字不连续
            /// </summary>
            /// <param name="RmNum">随机序列个数</param>
            /// <param name="RmNext">范围</param>
            /// <returns></returns>
            public static Dictionary<int, int> GetRandomList(int RmNum, int RmNext)
            {
                Dictionary<int, int> dictionary = new Dictionary<int, int>();
                Random rm = new Random();
                for (int i = 0; dictionary.Count < RmNum; i++)
                {
                    int nValue = rm.Next(1, RmNext);
                    if (i == 0)
                    {
                        dictionary.Add(i, nValue);
                    }
                    if (!dictionary.ContainsValue(nValue))
                    {
                        ArrayList arrayList = new ArrayList();
                        foreach (var item in dictionary)
                        {
                            arrayList.Add(item.Value);
                        }
                        if (dictionary.Count > 0)
                        {
                            if (Math.Abs(Convert.ToInt32(arrayList[arrayList.Count - 1]) - Convert.ToInt32(nValue)) > 1)
                            {
                                dictionary.Add(i, nValue);
                            }
                        }
                    }
                }
                return dictionary;
            }
    
    

      

           算法2:RmNum<RmNext 下面这种算法,如果RmNum和RmNext趋近的时候就会出现效率问题(甚至计算不出来)

            /// <summary>
            /// 指定范围数字 生成随机序列 数字不连续
            /// </summary>
            /// <param name="RmNum">随机序列个数</param>
            /// <param name="RmNext">范围</param>
            /// <returns></returns>
            public static List<int> GetRandomListNew(int RmNum, int RmNext)
            {
                int[] array = new int[RmNext];
                List<int> List = new List<int>();
                for (int i = 0; i <= array.Length - 1; i++)
                {
                    array[i] = i + 1;
                    if (i == 0)
                    {
                        List.Add(array[i]);
                    }
                }
                for (int i = 0; List.Count < RmNum; i++)
                {
                    if (!List.Contains(List[List.Count - 1]))
                    {
                        if (List.Count > 0)
                        {
                            if (Math.Abs(Convert.ToInt32(List[List.Count - 1]) - Convert.ToInt32(List[List.Count])) > 1)
                            {
                                List.Add(List[List.Count - 1]);
                            }
                        }
                    }
                }
                return List;
            }
    

      当RmNum=RmNext 时候,两种算法都会计算很长时间,甚至计算不出来,当时试着用hashtable,hashtable 可以存储但是是无序的,于是就用了Dictionary和list。

          下面是调用的方法(直接粘过去是能用的):

                Stopwatch sp = new Stopwatch();
                sp.Start();//开始计时
                foreach (var item in GetRandomList(9999, 10000))
                {
                    Console.WriteLine(string.Format("{0}", item.Value.ToString()));
                }
                sp.Stop();
                Console.WriteLine(String.Format("耗时{0}", sp.ElapsedMilliseconds));
                Console.Read();
    

      

         指定范围数字,生成随机序列,数字不连续,我这个只是随机把一种组合打印出来,如果把所有组合都打印出来该怎么办???范围【1-5】   1 5 2 然后是不是就死循环了?

        求大神解惑!!!

      

  • 相关阅读:
    htm与html的区别
    CLR笔记:3.共享程序集合强命名程序集
    CLR笔记:5.基元,引用和值类型
    CLR笔记:13.数组
    CLR笔记:18.可空值类型
    正则表达式
    代码大全
    wcf的部署
    Json相关
    $.ready和onload
  • 原文地址:https://www.cnblogs.com/viaiu/p/4959897.html
Copyright © 2011-2022 走看看