Ejemplo n.º 1
0
        /// <summary>
        /// 算法:
        /// 1.每个项权重+1命名为w,防止为0情况。
        /// 2.计算出总权重n。
        /// 3.每个项权重w加上从0到(n-1)的一个随机数(即总权重以内的随机数),得到新的权重排序值s。
        /// 4.根据得到新的权重排序值s进行排序,取前面s最大几个。
        /// </summary>
        /// <param name="list">原始列表</param>
        /// <param name="count">随机抽取条数</param>
        /// <returns></returns>
        public List <tblSubroute> GetRandomList(List <tblSubroute> list, int count)
        {
            if (list == null || list.Count <= count || count <= 0)
            {
                return(list);
            }

            //计算权重总和
            int totalWeights = 0;

            for (int i = 0; i < list.Count; i++)
            {
                totalWeights += int.Parse(list[i].fldWeight) + 1;  //权重+1,防止为0情况。
            }

            //随机赋值权重
            Random ran = new Random(GetRandomSeed());                                      //GetRandomSeed()随机种子,防止快速频繁调用导致随机一样的问题
            List <KeyValuePair <int, int> > wlist = new List <KeyValuePair <int, int> >(); //第一个int为list下标索引、第一个int为权重排序值

            for (int i = 0; i < list.Count; i++)
            {
                int w = int.Parse(list[i].fldWeight) + ran.Next(0, totalWeights);   // (权重+1) + 从0到(总权重-1)的随机数
                wlist.Add(new KeyValuePair <int, int>(i, w));
            }

            //排序
            wlist.Sort(
                delegate(KeyValuePair <int, int> kvp1, KeyValuePair <int, int> kvp2)
            {
                return(kvp2.Value - kvp1.Value);
            });

            //根据实际情况取排在最前面的几个
            List <tblSubroute> newList = new List <tblSubroute>();

            for (int i = 0; i < count; i++)
            {
                tblSubroute entiy = list[wlist[i].Key];
                newList.Add(entiy);
            }

            //随机法则
            return(newList);
        }
Ejemplo n.º 2
0
        public HttpResponseMessage UpdateSubroute(uppram reparm)
        {
            string result = string.Empty;

            try
            {
                using (YYPlayContext yYPlay = new YYPlayContext())
                {
                    tblSubroute tbls = (from x in yYPlay.tblSubroute
                                        where x.fldAutoID == reparm.fldAutoID
                                        select x).FirstOrDefault();

                    tbls.fldState            = reparm.fldState == "0" ? false : true;
                    tbls.fldWeight           = reparm.fldWeight;
                    tbls.fldMinmoney         = reparm.fldMinmoney;
                    tbls.fldMaxmoney         = reparm.fldMaxmoney;
                    tbls.fldProhibitMerchant = reparm.fldProhibitMerchant;



                    int i = yYPlay.SaveChanges();



                    if (i > 0)
                    {
                        result = rule.JsonStr("ok", "成功", i);
                    }
                    else
                    {
                        result = rule.JsonStr("error", "失败", i);
                    }
                }
            }
            catch (Exception e)
            {
                //错误保存日志
                throw new InsertException(e.Message, "Subroute", "GetSubroute", "");
            }
            return(new HttpResponseMessage {
                Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json")
            });
        }