国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

抽獎算法

2019-11-17 02:38:17
字體:
來源:轉載
供稿:網友
抽獎算法

網上找的抽獎代碼都不滿意,自己動手豐衣足食,有需要的可以直接拿去用,不收版權費的。

/// <summary>/// 抽獎/// </summary>public class PRize{    /// <summary>    /// 獎品關鍵字    /// </summary>    public string Key { get; set; }    /// <summary>    /// 權重/數量    /// </summary>    public int Poll { get; set; }    /// <summary>    /// 中獎區間    /// </summary>    class Area    {        /// <summary>        /// 獎品關鍵字        /// </summary>        public string Key { get; set; }        /// <summary>        /// 開始索引位置        /// </summary>        public int Start { get; set; }        /// <summary>        /// 截止索引位置        /// </summary>        public int Over { get; set; }    }    /// <summary>    /// 隨機種子    /// </summary>    static Random Rand = new Random((int)DateTime.Now.Ticks);    /// <summary>    /// 輪盤抽獎,權重值(在輪盤中占的面積大小)為中獎幾率    /// </summary>    /// <param name="prizeList">禮品列表(如果不是百分百中獎則輪空需要加入到列表里面)</param>    /// <returns></returns>    public static string Roulette(List<Prize> prizeList)    {        if (prizeList == null || prizeList.Count == 0) return string.Empty;        if (prizeList.Any(x => x.Poll < 1)) throw new ArgumentOutOfRangeException("poll權重值不能小于1");        if (prizeList.Count == 1) return prizeList[0].Key; //只有一種禮品        Int32 total = prizeList.Sum(x => x.Poll); //權重和                       if (total > 1000) throw new ArgumentOutOfRangeException("poll權重和不能大于1000"); //數組存儲空間的限制。最多一千種獎品(及每種獎品的權重值都是1)        List<int> speed = new List<int>(); //隨機種子        for (int i = 0; i < total; i++) speed.Add(i);        int pos = 0;        Dictionary<int, string> box = new Dictionary<int, string>();        foreach (Prize p in prizeList)        {            for (int c = 0; c < p.Poll; c++) //權重越大所占的面積份數就越多            {                pos = Prize.Rand.Next(speed.Count); //取隨機種子坐標                box[speed[pos]] = p.Key; //亂序 禮品放入索引是speed[pos]的箱子里面                speed.RemoveAt(pos); //移除已抽取的箱子索引號            }        }        return box[Prize.Rand.Next(total)];    }    /// <summary>    /// 獎盒抽獎,每個參與者對應一個獎盒,多少人參與就有多少獎盒    /// </summary>    /// <param name="prizeList">禮品列表</param>    /// <param name="peopleCount">參與人數</param>    /// <returns></returns>    public static string LunkyBox(List<Prize> prizeList, int peopleCount)    {        if (prizeList == null || prizeList.Count == 0) return string.Empty;        if (prizeList.Any(x => x.Poll < 1)) throw new ArgumentOutOfRangeException("poll禮品數量不能小于1個");        if (peopleCount < 1) throw new ArgumentOutOfRangeException("參數人數不能小于1人");        if (prizeList.Count == 1 && peopleCount <= prizeList[0].Poll) return prizeList[0].Key; //只有一種禮品且禮品數量大于等于參與人數        int pos = 0;        List<Area> box = new List<Area>();        foreach (Prize p in prizeList)        {            box.Add(new Area() { Key = p.Key, Start = pos, Over = pos + p.Poll }); //把禮品放入獎盒區間            pos = pos + p.Poll;        }        int total = prizeList.Sum(x => x.Poll); //禮品總數        int speed = Math.Max(total, peopleCount); //取禮品總數和參數總人數中的最大值        pos = Prize.Rand.Next(speed);        Area a = box.FirstOrDefault(x => pos >= x.Start && pos < x.Over); //查找索引在獎盒中對應禮品的位置        return a == null ? string.Empty : a.Key;    }}/*List<Prize> prizes = new List<Prize>();prizes.Add(new Prize() { Key = "電腦", Poll = 1 });prizes.Add(new Prize() { Key = "機柜", Poll = 2 });prizes.Add(new Prize() { Key = "鼠標", Poll = 3 });                        string lp1 = Prize.LunkyBox(prizes, 6);Console.WriteLine(lp1);prizes.Add(new Prize() { Key = "謝謝惠顧", Poll = 5 });string lp2 = Prize.Roulette(prizes);      Console.WriteLine(lp2);*/


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 搜索| 武汉市| 武川县| 大竹县| 通榆县| 苏尼特左旗| 吴忠市| 汉川市| 鹿泉市| 开封市| 阿城市| 舒城县| 沙坪坝区| 五家渠市| 济阳县| 闽清县| 连平县| 洞头县| 桓仁| 左贡县| 绥宁县| 武川县| 清丰县| 吉林省| 潞西市| 宣武区| 朔州市| 陆良县| 曲周县| 麻栗坡县| 鄂温| 孟津县| 德兴市| 五大连池市| 东莞市| 阿坝| 宜兰县| 福安市| 乡城县| 垫江县| 遵义县|