當(dāng)前位置:
首頁(yè) >
抽奖算法
發(fā)布時(shí)間:2025/3/20
37
豆豆
網(wǎng)上找的抽獎(jiǎng)代碼都不滿(mǎn)意,自己動(dòng)手豐衣足食,有需要的可以直接拿去用,不收版權(quán)費(fèi)的。
/// <summary> /// 抽獎(jiǎng) /// </summary> public class Prize {/// <summary>/// 獎(jiǎng)品關(guān)鍵字/// </summary>public string Key { get; set; }/// <summary>/// 權(quán)重/數(shù)量/// </summary>public int Poll { get; set; }/// <summary>/// 中獎(jiǎng)區(qū)間/// </summary>class Area{/// <summary>/// 獎(jiǎng)品關(guān)鍵字/// </summary>public string Key { get; set; }/// <summary>/// 開(kāi)始索引位置/// </summary>public int Start { get; set; }/// <summary>/// 截止索引位置/// </summary>public int Over { get; set; }}/// <summary>/// 隨機(jī)種子/// </summary>static Random Rand = new Random((int)DateTime.Now.Ticks);/// <summary>/// 輪盤(pán)抽獎(jiǎng),權(quán)重值(在輪盤(pán)中占的面積大小)為中獎(jiǎng)幾率/// </summary>/// <param name="prizeList">禮品列表(如果不是百分百中獎(jiǎng)則輪空需要加入到列表里面)</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權(quán)重值不能小于1");if (prizeList.Count == 1) return prizeList[0].Key; //只有一種禮品 Int32 total = prizeList.Sum(x => x.Poll); //權(quán)重和 if (total > 1000) throw new ArgumentOutOfRangeException("poll權(quán)重和不能大于1000"); //數(shù)組存儲(chǔ)空間的限制。最多一千種獎(jiǎng)品(及每種獎(jiǎng)品的權(quán)重值都是1) List<int> speed = new List<int>(); //隨機(jī)種子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++) //權(quán)重越大所占的面積份數(shù)就越多 {pos = Prize.Rand.Next(speed.Count); //取隨機(jī)種子坐標(biāo)box[speed[pos]] = p.Key; //亂序 禮品放入索引是speed[pos]的箱子里面speed.RemoveAt(pos); //移除已抽取的箱子索引號(hào) }}return box[Prize.Rand.Next(total)];}/// <summary>/// 獎(jiǎng)盒抽獎(jiǎng),每個(gè)參與者對(duì)應(yīng)一個(gè)獎(jiǎng)盒,多少人參與就有多少獎(jiǎng)盒/// </summary>/// <param name="prizeList">禮品列表</param>/// <param name="peopleCount">參與人數(shù)</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禮品數(shù)量不能小于1個(gè)");if (peopleCount < 1) throw new ArgumentOutOfRangeException("參數(shù)人數(shù)不能小于1人");if (prizeList.Count == 1 && peopleCount <= prizeList[0].Poll) return prizeList[0].Key; //只有一種禮品且禮品數(shù)量大于等于參與人數(shù)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 }); //把禮品放入獎(jiǎng)盒區(qū)間pos = pos + p.Poll;}int total = prizeList.Sum(x => x.Poll); //禮品總數(shù)int speed = Math.Max(total, peopleCount); //取禮品總數(shù)和參數(shù)總?cè)藬?shù)中的最大值 pos = Prize.Rand.Next(speed);Area a = box.FirstOrDefault(x => pos >= x.Start && pos < x.Over); //查找索引在獎(jiǎng)盒中對(duì)應(yīng)禮品的位置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 = "機(jī)柜", Poll = 2 }); prizes.Add(new Prize() { Key = "鼠標(biāo)", 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); */?
轉(zhuǎn)載于:https://www.cnblogs.com/jiang_zheng/p/4221040.html
總結(jié)
- 上一篇: 用TWaver加载大型游戏场景一例
- 下一篇: 使用zlib库进行数据压缩