抽奖概率算法

        CSI_Riddle riddle = new CSI_Riddle();
        CSI_RiddleAnswer answer = new CSI_RiddleAnswer();
        CSI_Luckdraw luckdraw = new CSI_Luckdraw();
        static Random Rnd = new Random();
        PointLog pointLog = new PointLog();//积分日志

        User user = new User();

        protected void Page_Load(object sender, EventArgs e)
        {
            //Query query = new Query();
            //query.AddAndFilter("AccountId={0}", CurrentAccount.Id);//此表保存的是AccountId
            //user = UserService.Find(query);//总积分表

            int Count = answer.GetCount(" IsTrue=1 and U_ID=" + CurrentAccount.UserId);//答对灯谜数
            int Num = luckdraw.GetCount(" U_ID=" + CurrentAccount.UserId);  //抽奖次数

            Result resul = new Result();
            JavaScriptSerializer jss = new JavaScriptSerializer();

            #region
            if (DateTime.Now >= DateTime.Parse("2014-02-24 12:00:00"))//抽台历
            {
                //转盘 Turntable
                List<Prize> Turntable = new List<Prize>{
                        new Prize{ LuckDrowName="0", Chance=25},
                        new Prize{ LuckDrowName="2500", Chance=75}};
                //是否答对全部所有灯谜,并且台历抽奖开启后是否抽过奖
                if (answer.GetCount(" IsTrue=1 and U_ID=" + CurrentAccount.UserId) == riddle.GetCount("") && luckdraw.GetCount(" CreateDate>='2014-02-24 12:00:00' and U_ID=" + CurrentAccount.UserId) <= 0)
                {
                    int TrueCount = luckdraw.GetCount(" Result=0");//中到台历个数
                    if (TrueCount >= 5)//后五个随机
                    {
                        if (TrueCount >= 10)//达到10个后不会抽到台历
                        {
                            resul.Prize = "2500";
                            resul.Angle = AngleNum("2500");

                            luckdraw.Result = 2500;
                            luckdraw.U_ID = CurrentAccount.UserId;
                            luckdraw.CreateDate = DateTime.Now;
                            luckdraw.Add(luckdraw);
                        }
                        else //随机抽五个
                        {

                            resul.Prize = name(Turntable);
                            resul.Angle = AngleNum(resul.Prize);

                            luckdraw.Result = int.Parse(resul.Prize);
                            luckdraw.U_ID = CurrentAccount.UserId;
                            luckdraw.CreateDate = DateTime.Now;
                            luckdraw.Add(luckdraw);
                        }
                    }
                    else //前五个
                    {
                        resul.Prize = "0";
                        resul.Angle = AngleNum("0");//2月24日12点至2月24日23:59

                        luckdraw.Result = 0;
                        luckdraw.U_ID = CurrentAccount.UserId;
                        luckdraw.CreateDate = DateTime.Now;
                        luckdraw.Add(luckdraw);
                    }
                }

            }
            #endregion
            else
            {
                if ((Count - Num) > 0)//是否可以抽奖
                {
                    //转盘 Turntable
                    List<Prize> Turntable = new List<Prize>{
                     new Prize{ LuckDrowName="500", Chance=60},
                     new Prize{ LuckDrowName="1000", Chance=17},
                     new Prize{ LuckDrowName="1500", Chance=17},
                     new Prize{ LuckDrowName="2500", Chance=6}};
                    if (DateTime.Now >= DateTime.Parse("2014-02-24 00:00:00"))
                    {
                        resul.Prize = "-1";//没有抽奖机会
                        resul.Angle = 0;
                    }
                    else
                    {

                        resul.Prize = name(Turntable);
                        resul.Angle = AngleNum(resul.Prize);

                        luckdraw.Result = int.Parse(resul.Prize);
                        luckdraw.U_ID = CurrentAccount.UserId;
                        luckdraw.CreateDate = DateTime.Now;
                        luckdraw.Add(luckdraw);
                    }
                }
                else
                {
                    resul.Prize = "-1";//没有抽奖机会
                    resul.Angle = 0;
                }
            }
            if (resul.Prize != "-1" && resul.Prize != "0" && resul.Prize!=null)
            {

                try
                {
                    pointLog.Point = int.Parse(resul.Prize);//分数
                    pointLog.UserId = CurrentAccount.UserId;
                    pointLog.PointRuleId = 0;//猜灯谜
                    pointLog.RemarkId = 0;
                    pointLog.OperatorId = CurrentAccount.UserId;
                    pointLog.CreateDate = DateTime.Now;
                    PointLogService.Create(pointLog);//加入日志

                    CSI_iknowUser iknowuser = new CSI_iknowUser();
                    object ik = iknowuser.ConvertToModel(iknowuser.GetFieldList(" top 1 *", "AccountId=" + CurrentAccount.UserId).Rows[0]);
                    //iknowuser = iknowuser.ConvertToModel(iknowuser.GetList(" AccountId=" + CurrentAccount.UserId).Tables[0].Rows[0]);
                    //if (iknowuser == null)
                    //{
                    //    iknowuser = iknowuser.ConvertToModel(iknowuser.GetList(" AccountId=" + CurrentAccount.Id).Tables[0].Rows[0]);
                    //}
                    if (ik != null)
                    {
                        iknowuser = (CSI_iknowUser)ik;
                        iknowuser.Point += int.Parse(resul.Prize);
                        iknowuser.Update(iknowuser);//加入总积分
                    }

                }
                catch
                {
                   // luckdraw.DeleteUserID(CurrentAccount.UserId);//删除抽奖
                    resul.Prize = "-2";//出问题了
                    resul.Angle = 0;
                }
            }

            Response.Write(jss.Serialize(resul));
            Response.End();

        }

 

        //计算概率
        private Prize LuckDrow(List<Prize> Turntable)
        {
            return (from x in Enumerable.Range(0, 1000000)  //最多支100万次骰子
                    let Prize = Turntable[Rnd.Next(Turntable.Count())]
                    let Sieve = Rnd.Next(0, 100)
                    where Sieve < Prize.Chance
                    select Prize).First();
        }

        //获取中奖奖品名称
        protected string name(List<Prize> Turntable)
        {
            return LuckDrow(Turntable).LuckDrowName;
        }

        //获取转盘的角度
        protected int AngleNum(string LuckDrowName)
        {
            bool flage = true;
            Random rod = new Random();
            int Angle = 0;

            //360度 分成九份  每份40度
            int[,] arrayList = new int[9, 2] { { 0, 39 }, { 40, 79 }, { 80, 119 }, { 120, 159 }, { 160, 199 }, { 200, 239 }, { 240, 279 }, { 280, 319 }, { 320, 359 } };//定义二维数

            switch (LuckDrowName)
            {
                case "500":
                    while (flage)
                    {
                        Angle = rod.Next(360);
                        if ((arrayList[1, 0] <= Angle && Angle <= arrayList[1, 1]) || (arrayList[3, 0] <= Angle && Angle <= arrayList[3, 1]) || (arrayList[6, 0] <= Angle && Angle <= arrayList[6, 1]) || (arrayList[8, 0] <= Angle && Angle <= arrayList[8, 1]))
                        {
                            flage = false;
                        }
                    }
                    break;
                case "1000":
                    while (flage)
                    {
                        Angle = rod.Next(360);
                        if (arrayList[7, 0] <= Angle && Angle <= arrayList[7, 1])
                        {
                            flage = false;
                        }
                    }
                    break;
                case "1500":
                    while (flage)
                    {
                        Angle = rod.Next(360);
                        if (arrayList[0, 0] <= Angle && Angle <= arrayList[0, 1])
                        {
                            flage = false;
                        }
                    }
                    break;
                case "2500":
                    while (flage)
                    {
                        Angle = rod.Next(360);
                        if (arrayList[2, 0] <= Angle && Angle <= arrayList[2, 1])
                        {
                            flage = false;
                        }
                    }
                    break;
                default:
                    while (flage)
                    {
                        Angle = rod.Next(360);
                        if ((arrayList[4, 0] <= Angle && Angle <= arrayList[4, 1]) || (arrayList[5, 0] <= Angle && Angle <= arrayList[5, 1]))
                        {
                            flage = false;
                        }
                    }
                    break;
            }
            return Angle;
        }

    }

    public class Prize //奖品
    {
        /// <summary>
        /// 奖品名称
        /// </summary>
        public string LuckDrowName;

        /// <summary>
        /// 概率
        /// </summary>
        public int Chance;
    }

    public class Result //结果
    {
        #region
        private int _angle;
        private string _prize;

        /// <summary>
        /// 角度
        /// </summary>
        public int Angle
        {
            set { _angle = value; }
            get { return _angle; }
        }
        /// <summary>
        /// 奖项
        /// </summary>
        public string Prize
        {
            set { _prize = value; }
            get { return _prize; }
        }
        #endregion Model
    }

 

posted @ 2014-01-25 14:14  大院  阅读(645)  评论(0编辑  收藏  举报