.net 指定獎品概率隨機抽獎功能實現


class Program
    {
        static void Main(string[] args)
        {
            int code1 = 0, code2 = 0, code3 = 0, code4 = 0, code5 = 0, code6 = 0, code7 = 0, code8 = 0, other = 0;

            for (int i = 0; i <30000; i++)
            {
                Enumerable.Range(1, 1).ToList().ForEach(x =>
                {
                    string name = Run(list).name;
                    if (name == "Goods1")
                    {
                        code1++;
                    }
                    else if (name == "Goods2")
                    {
                        code2++;
                    }
                    else if (name == "Goods3")
                    {
                        code3++;
                    }
                    else if (name == "Goods4")
                    {
                        code4++;
                    }
                    else if (name == "Goods5")
                    {
                        code5++;
                    }
                    else if (name == "Goods6")
                    {
                        code6++;
                    }
                    else if (name == "Goods7")
                    {
                        code7++;
                    }
                    else if (name == "Goods8")
                    {
                        code8++;
                    }
                    else
                    {
                        other++;
                    }
                });
            }

            Console.WriteLine("code1:" + code1);
            Console.WriteLine("code2:" + code2);
            Console.WriteLine("code3:" + code3);
            Console.WriteLine("code4:" + code4);
            Console.WriteLine("code5:" + code5);
            Console.WriteLine("code6:" + code6);
            Console.WriteLine("code7:" + code7);
            Console.WriteLine("code8:" + code8);
            Console.WriteLine("other:" + other);
            Console.WriteLine("總次數:" + (code1 + code2 + code3 + code4 + code5 + code6 + code7 + code8 + other));
        }


         private static Random Rnd = new Random();

         //獎品列表
        private static List<Goods> list = new List<Goods> {
            new Goods(){id=1,name="Goods1",code="code1",percent=0.049},
            new Goods(){id=2,name="Goods2",code="code2",percent=0.14},
            new Goods(){id=3,name="Goods3",code="code3",percent=0.001},
            new Goods(){id=4,name="Goods4",code="code4",percent=0.0001},
            new Goods(){id=5,name="Goods5",code="code5",percent=0.15},
            new Goods(){id=6,name="Goods6",code="code6",percent=0.25},
            new Goods(){id=7,name="Goods7",code="code7",percent=0.20},
            new Goods(){id=8,name="Goods8",code="code8",percent=0.20},
        };

        //根據概率隨機產生獎品
        private static Goods Run(List<Goods> list)
        {
            return (from x in Enumerable.Range(0, 1000000)
                    let p = list[Rnd.Next(list.Count())]
                    let q = Rnd.NextDouble()
                    where q < p.percent
                    select p).First();       

        }
 }

 

    /// <summary>
    /// 獎品實體
    /// </summary>
    class Goods
    {
        /// <summary>
        /// 獎品id
        /// </summary>
        public int id { set; get; }
        /// <summary>
        /// 獎品代碼
        /// </summary>
        public string code { set; get; }
        /// <summary>
        /// 獎品名稱
        /// </summary>
        public string name { set; get; }
        /// <summary>
        /// 概率

        /// </summary>
        public double percent { set; get; }
        public string ToString()
        {
            return string.Format("id:{0},code:{1},name:{2},percent:{3}", this.id, this.code, this.name, this.percent);
        }
    }

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com