Demo entry 6659425

java

   

Submitted by anonymous on Nov 11, 2017 at 06:16
Language: Java. Code size: 1.2 kB.

//获取后台配置的”谢谢参与”,保证概率抽奖流程不会产生null
TemplateAward award = getDefault(list);
//剔除当前无库存奖品
List<TemplateAward> list = InventoryControlService.getListWithEnoughStore();
//zk配置的最低中奖概率
double minPro = GameActivityConstans.getLotteryProbabilityMin();
//计算总概率
double totalPro = 0;
for (TemplateAward temp : list) {
   double tempStore = temp.getAwardStore();
   //兼容老模板没有设置概率的情况,根据库存取概率
   double storePro = (tempStore / allStore) < minPro ? minPro : (tempStore / allStore);
   double tempPro = temp.getAwardPro()==null ? storePro :temp.getAwardPro();
   totalPro += tempPro;
}
//获得一个并发随机数。为了便于理解和管理,原则后台需要校验限制所有奖品的概率和为100%。此处根据计算出来的总概率生成随机数属于业务防呆。
double lottPro = ThreadLocalRandom.current().nextDouble(totalPro );
//轮询奖品
for (TemplateAward temp : list) {
   double tempStore = temp.getAwardStore();
   //兼容老模板没有设置概率的情况,根据库存取概率
   double storePro = (tempStore / allStore) < minPro ? minPro : (tempStore / allStore);
   double tempPro = temp.getAwardPro()==null ? storePro :temp.getAwardPro();
          if (lottPro >= basePro && lottPro < tempPro + basePro) {
              award = temp;
              break;
          }
           basePro += tempPro;
      }

//返回所选出奖品
return award;

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).