網上找的抽獎代碼都不滿意,自己動手豐衣足食,有需要的可以直接拿去用,不收版權費的。
/// <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);
*/