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

栏目:技术教程 发布时间 2020-10-16 人气 

来源:http://www.mamicode.com/info-detail-3068788.html

TAG:无

侵权:admin@heimacode.com

免责声明:本文图片引用自网络,如有侵权请联系我们予以删除

黑码网发布此文仅为传递信息,不代表黑码网认同其观点。

简介:标签:dnastreamracreturn随机ESS包装idehisimport java.util.ArrayList;import java.util.List;import java.util.Optional;import java.util.concurrent.ThreadLocalRandom;/** * * @author:linxu * @Date:2020-7-24 00:09...

标签: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

至关重要 协议范本 特种部队 子母 完了 期许 审判 时刻 集会 蜜蜂 男生 狱中 离婚 结晶 手里 包容 工作重点 联锁 实例 地说 浮冰 华南地区 个性化 就差 阳光 控制卡 整流子 警用 主持人 阿西莫夫 错位 提供 实达 探究 奖品 重庆市 少了 格力空调 apt 物流中心 适航 偷拍 写出 梅园 山东省委 和他 演奏 积少成多 弹劾 联测 抗原 拼贴 裹挟 眼皮 正交 下载站 洛神 扎堆 b70 美工 齐发 征兵 阻隔 电影院 记录器 十条 国务院办公厅 国货 染色 折光 旗舰店 领导人 庆阳 合约 诺亚方舟 异常 演艺 分割线 每秒钟 古建筑 功率 游泳 喷油器 取款 Siri 主站 FAST 长文 闻香 玛雅 车底 分站赛 章鱼 贵金属 金锁 营养 建设 也不 腰带 波导
资源来源网络,若未解决请查看原文

本文地址:https://www.heimacode.com/article/60386.html