权重测试,线性随机算法

先创建个奖励对象

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class NewSysBoxInfo {
    @ApiModelProperty("奖励Id")
    private Integer id;
    @ApiModelProperty("权重")
    private Integer percentage;
    @ApiModelProperty("奖励的上限数量")
    private Integer rewardNum;
}

权重测试

@Slf4j
public class 权重测试 {

    public static void main(String[] args) {
        //创建奖励列表
        List<NewSysBoxInfo> list = new ArrayList<>();
        //奖励1号
        list.add(NewSysBoxInfo.builder().id(0).percentage(60).rewardNum(100).build());
        //奖励2号
        list.add(NewSysBoxInfo.builder().id(1).percentage(20).rewardNum(100).build());
        //奖励3号
        list.add(NewSysBoxInfo.builder().id(2).percentage(20).rewardNum(100).build());
        //奖励4号
        list.add(NewSysBoxInfo.builder().id(3).percentage(10).rewardNum(100).build());
        if (CollectionUtils.isEmpty(list)){
            log.error("奖励列表不可为空");
            return;
        }
        //先把概率最高的排前面,提高效率
        list = list.stream().sorted(Comparator.comparing(NewSysBoxInfo::getPercentage).reversed()).collect(Collectors.toList());
        //计算获取的概率
        int r1 = 0;
        int r2 = 0;
        int r3 = 0;
        int r4 = 0;
        NewSysBoxInfo boxInfo2 = null;
        long s = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            //获取索引
            boxInfo2 = getPrizeDouble(list);
            //boxInfo2 = getPrizeInt(list);
            if (boxInfo2.getId() == 0) {
                r1 += 1;
            }
            if (boxInfo2.getId() == 1) {
                r2 += 1;
            }
            if (boxInfo2.getId() == 2) {
                r3 += 1;
            }
            if (boxInfo2.getId() == 3) {
                r4 += 1;
            }
        }
        long e = System.currentTimeMillis();
        System.out.println("耗时" + (e - s) + "毫秒");
        System.out.println("1号出现:" + r1 + "次");
        System.out.println("2号出现:" + r2 + "次");
        System.out.println("3号出现:" + r3 + "次");
        System.out.println("4号出现:" + r4 + "次");
    }

    /**
     * @description: 根据概率返回获取的奖励列表 double版本
     * @params: prizes:奖励列表
     * @return: int:列表索引
     */
    public static NewSysBoxInfo getPrizeDouble(List<NewSysBoxInfo> prizes) {
        try {
            // 计算总权重
            double sumWeight = prizes.stream().mapToDouble(NewSysBoxInfo::getPercentage).sum();
            // 产生随机数
            double randomNumber;
            randomNumber = Math.random();
            double d1 = 0;
            double d2 = 0;
            for (NewSysBoxInfo prize : prizes) {
                d2 += Double.parseDouble(String.valueOf(prize.getPercentage())) / sumWeight;
                if (randomNumber >= d1 && randomNumber <= d2) {
                    return prize;
                }
                d1 += Double.parseDouble(String.valueOf(prize.getPercentage())) / sumWeight;
            }
        } catch (Exception e) {
            System.out.println("出错原因:" + e.getMessage());
        }
        return null;
    }

    /**
     * @description: 根据概率返回获取的奖励列表 int版本(推荐)
     * @params: prizes:奖励列表
     * @return: int:列表索引
     */
    public static NewSysBoxInfo getPrizeInt(List<NewSysBoxInfo> prizes) {
        // 计算总权重
        int sumWeight = prizes.stream().mapToInt(NewSysBoxInfo::getPercentage).sum();
        // 产生随机数
        int randomNum = new Random().nextInt(sumWeight);
        for (NewSysBoxInfo prize : prizes) {
            if ((randomNum -= prize.getPercentage()) < 0) {
                return prize;
            }
        }
        return null;
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值