码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现按照权重随机抽奖案例

时间:2020-07-24 09:19:47      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:dna   stream   rac   return   随机   ESS   包装   ide   his   

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;

/**
 *
 * @author:linxu
 * @Date:2020-7-24 00:09:54
 */
public class WeightRandomTemplate {

    public static void main(String[] args) {
        List<DefaultWeightWrap> rewardWraps = new ArrayList<>();
        rewardWraps.add(new RewardWrap(12881,"一等奖",1,1, 10));
        rewardWraps.add(new RewardWrap(12882,"二等奖",1,2, 30));
        rewardWraps.add(new RewardWrap(12883,"三等奖",1,3, 50));
        //默认奖励,如果抽不到奖,默认抽中安慰奖【参与奖】
        RewardWrap rewardWrap =new RewardWrap(12884,"安慰奖",1,4, 60);
        rewardWraps.add(rewardWrap);
        WeightRandom<DefaultWeightWrap> weightRandom = new WeightRandom<>(rewardWraps, rewardWrap);
        //模拟抽奖二十次
        for (int i = 0; i < 20; i++) {
            RewardWrap rewardWrapRandom = (RewardWrap) weightRandom.randomWeightWrap();
            System.out.println(rewardWrapRandom.toString());
        }
    }

     //测试实现类
    public static class RewardWrap extends DefaultWeightWrap {
        private int rewardId;//奖励id
        private String rewardName;//奖励名称
        private int rewardValue;//数量
        private int rank;//奖励排名1等奖,2等奖
        public RewardWrap(int rewardId, String rewardName, int rewardValue, int rank,int weight) {
            this.rewardName = rewardName;
            this.rewardValue = rewardValue;
            this.rewardId = rewardId;
            this.rank = rank;
            this.weight=weight;
        }
        @Override
        public String toString() {
            return "Reward{" +
                    "rewardId=" + rewardId +
                    ", rewardName=‘" + rewardName + ‘\‘‘ +
                    ", rewardValue=" + rewardValue +
                    ", rank=" + rank +
                    ‘}‘;
        }
    }

    public static class WeightRandom<T extends DefaultWeightWrap> {
        private List<T> content;//目标集合数据
        private T defaultResult;//默认返回值【参与奖励】
        public WeightRandom(List<T> content, T defaultResult) {
            this.content = content;
            this.defaultResult = defaultResult;
        }
        public T randomWeightWrap() {
            int s = content.stream().mapToInt(DefaultWeightWrap::getWeight).sum();
            int r = ThreadLocalRandom.current().nextInt(s);
            Optional<T> optional = content.stream().filter(t -> { int result = r - t.getWeight();return result < 0; }).findFirst();
            return optional.orElse(defaultResult);
        }
    }

    //权重包装基类
    public static abstract class DefaultWeightWrap {
        public int weight;//权重
        public int getWeight() {
            return weight;
        }
    }
}


测试结果:
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12882, rewardName=‘二等奖‘, rewardValue=1, rank=2}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12881, rewardName=‘一等奖‘, rewardValue=1, rank=1}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12883, rewardName=‘三等奖‘, rewardValue=1, rank=3}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12881, rewardName=‘一等奖‘, rewardValue=1, rank=1}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12882, rewardName=‘二等奖‘, rewardValue=1, rank=2}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12883, rewardName=‘三等奖‘, rewardValue=1, rank=3}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12883, rewardName=‘三等奖‘, rewardValue=1, rank=3}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}
Reward{rewardId=12884, rewardName=‘安慰奖‘, rewardValue=1, rank=4}

Process finished with exit code 0

 

Java实现按照权重随机抽奖案例

标签:dna   stream   rac   return   随机   ESS   包装   ide   his   

原文地址:https://www.cnblogs.com/mature1021/p/13369721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!