/// <summary>
/// 获取抽奖结果
/// </summary>
/// <param name="prob">奖品列表</param>
/// <returns>中奖实体</returns>
private LotteryGift Get(List<LotteryGift> prob)
{
LotteryGift result = null;
int n = (int)(prob.Sum(t => t.GiftProbability) * 1000); //计算概率总和,放大1000倍
Random r = rnd;
decimal x = (decimal)r.Next(0, n) / 1000; //随机生成0~概率总和的数字
for (int i = 0; i < prob.Count(); i++)
{
decimal pre = prob.Take(i).Sum(p => p.GiftProbability); //区间下界
decimal next = prob.Take(i + 1).Sum(s => s.GiftProbability); //区间上界
if (x >= pre && x < next) //如果在该区间范围内,就返回结果退出循环
{
result = prob[i];
break;
}
}
return result;
}