随机获取礼物活动总结(抽奖算法)
2017-11-16 18:32
411 查看
/** * 奖励金(礼物)模型 * Created by tong.hua on 2017-10-26. */ public class BountyItem extends ToString{ private static final long serialVersionUID = -4046365922597449030L; //礼物编号 private int giftId; //礼物类型 cash 现金 deduction 抵扣红包 coupon 加息券 private String giftType; //礼物编号 private String poolNo; //礼物名称 private String name; //中奖几率 private String probability; public int getGiftId() { return giftId; } public void setGiftId(int giftId) { this.giftId = giftId; } public String getGiftType() { return giftType; } public void setGiftType(String giftType) { this.giftType = giftType; } public String getPoolNo() { return poolNo; } public void setPoolNo(String poolNo) { this.poolNo = poolNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getProbability() { return probability; } public void setProbability(String probability) { this.probability = probability; } }
核心算法
/**
* 根据概率获取礼物集合中的礼物
* @param bountyItems 礼物列表
* @return
* 1.把礼物id,礼物的概率放大10000倍,存入map集合中 <giftid,[currentScope,lastScope]>
* <1,[1-500]> <2,[500-1500]> <3,[1500-3000]>
* 2.获取1-10000之间的一个随机数(幸运数)luckyNumber 比如:1700
* 3.遍历map,找出1700所在的区间,获取幸运id,luckyItemId 3
* 4.根据luckyItemId,取出bountyItems中对应的礼物
*
*/
private BountyItem getBountyItem(List<BountyItem> bountyItems) {
// 放大倍数
int mulriple = 10000;
int lastScope = 0;
// 洗牌,打乱奖品次序
Collections.shuffle(bountyItems);
Map<Integer, int[]> itemScopes = new HashMap<Integer, int[]>();
for (BountyItem item : bountyItems) {
int giftId = item.getGiftId();
// 划分区间
int currentScope = lastScope + new BigDecimal(item.getProbability()).multiply(new BigDecimal(mulriple)).intValue();
itemScopes.put(giftId, new int[] { lastScope + 1, currentScope });
lastScope = currentScope;
}
// 获取1-10000之间的一个随机数
int luckyNumber = new Random().nextInt(mulriple);
int luckyItemId = 0;
// 查找随机数所在的区间
if (!itemScopes.isEmpty()) {
Set<Map.Entry<Integer, int[]>> entrySets = itemScopes.entrySet();
for (Map.Entry<Integer, int[]> m : entrySets) {
int key = m.getKey();
if (luckyNumber >= m.getValue()[0] && luckyNumber <= m.getValue()[1]) {
luckyItemId = key;
break;
}
}
}
//根据luckyItemId从礼物集合中选中礼物
BountyItem selectedItem = new BountyItem();
for(BountyItem item : bountyItems){
int giftId = item.getGiftId();
if(giftId == luckyItemId){
selectedItem = item;
}
}
return selectedItem;
}
相关文章推荐
- Java从一个连续的自然数区间中,随机获取几个数(这几个数是固定的,一种抽奖算法及节省空间地保存结果)
- 牛客网 小东所在公司要发年终奖,而小东恰好获得了最高福利,他要在公司年会上参与一个抽奖游游戏在一个6*6的棋盘上进行,上面放着36个价值不等的礼物,每个小的棋盘上面放置着一个礼物,他需要从左上角开始游戏,每次只能向下或者向右移动一步,到达右下角停止,一路上的格子里的礼物小东都能拿到,请设计一个算法使小东拿到价值最高的礼物
- 微信平台抽奖算法总结-再也不用怕奖品被提前抢光
- 关于网站抽奖活动算法的尝试
- 在营销活动中的抽奖算法放送
- 抽奖活动(一)-Alias算法
- 算法题,获取随机不重复数组
- 总结一个随机抽取算法(全部item出现概率和为100%)
- 算法上均匀分布的随机抽奖,如何避免现实的现场抽奖的中奖号码有时出现集中扎堆的现象?
- 抽奖算法总结
- 论坛盖楼抽奖活动总结
- Android项目技术总结:获取状态栏和标题栏并计算长宽来限制绘制浮动窗口活动范围
- 关于网站抽奖活动算法的尝试
- 简单的活动抽奖算法&方案
- 4类抽奖算法总结
- 微信平台抽奖算法总结-再也不用怕奖品被提前抢光
- 抽奖活动开发总结
- 在营销活动中的抽奖算法放送
- Android项目技术总结:获取状态栏和标题栏并计算长宽来限制绘制浮动窗口活动范围
- leetcode 382. Linked List Random Node 等概率随机获取结点 + 蓄水池算法