转盘抽奖算法(java) public class LuckDraw { public int getPrizeIndex(List prizes) throws Exception { int random = -1; //计算总权重 double sumWeight = 0; for (GoodsEntity p : prizes) { sumWeight += p.getPrizeWeight(); } //产生随
public class LuckDraw {
public int getPrizeIndex(List
prizes) throws Exception {
int random = -1;
//计算总权重
double sumWeight = 0;
for (GoodsEntity p : prizes) {
sumWeight += p.getPrizeWeight();
}
//产生随机数
double randomNumber;
randomNumber = Math.random();
//根据随机数在所有奖品分布的区域并确定所抽奖品
double d1 = 0;
double d2 = 0;
for (int i = 0; i < prizes.size(); i++) {
d2 += Double.parseDouble(String.valueOf(prizes.get(i).getPrizeWeight())) / sumWeight;
if (i == 0) {
d1 = 0;
} else {
d1 += Double.parseDouble(String.valueOf(prizes.get(i - 1).getPrizeWeight())) / sumWeight;
}
if (randomNumber > d1 && randomNumber <= d2) {
random = i;
break;
}
}
return random;
}
}
实体类
@Entity
@Table(name = "goods")
public class GoodsEntity {
private Long goodsId;
private String goodsName;
private Byte dataStatus;
private Timestamp createTime;
private Byte ranking;
private Integer prizeWeight;
private BigDecimal price;
private Integer version;
@Id
@Column(name = "goods_id", nullable = false)
public Long getGoodsId() {
return goodsId;
}
public void setGoodsId(Long goodsId) {
this.goodsId = goodsId;
}
@Basic
@Column(name = "goods_name", nullable = true, length = 50)
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
@Basic
@Column(name = "data_status", nullable = true)
public Byte getDataStatus() {
return dataStatus;
}
public void setDataStatus(Byte dataStatus) {
this.dataStatus = dataStatus;
}
@Basic
@Column(name = "create_time", nullable = false)
public Timestamp getCreateTime() {
return createTime;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
@Basic
@Column(name = "ranking", nullable = true)
public Byte getRanking() {
return ranking;
}
public void setRanking(Byte ranking) {
this.ranking = ranking;
}
@Basic
@Column(name = "prize_weight", nullable = true)
public Integer getPrizeWeight() {
return prizeWeight;
}
public void setPrizeWeight(Integer prizeWeight) {
this.prizeWeight = prizeWeight;
}
@Basic
@Column(name = "version", nullable = true)
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
@Basic
@Column(name = "price", nullable = true)
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}
Controller代码
public MapluckDraw() { Map result = new HashMap (); try { List goodsEntityList = goodsService.getGoods();//参与抽奖的商品列表 int selected = luckDraw.getPrizeIndex(goodsEntityList); ResponseGoods responseGoods = new ResponseGoods(); responseGoods.setGoodsName(goodsEntityList.get(selected).getGoodsName()); responseGoods.setPrice(goodsEntityList.get(selected).getPrice()); result.put("result", responseGoods); } catch (Exception e) { e.printStackTrace(); } return result; }
