IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 【Java实现】带权重的随机数算法 -> 正文阅读

[数据结构与算法]【Java实现】带权重的随机数算法

我们按照顺序计算出权重的加和,把当前数字出现的权重加和前的值作为其权重范围的起点值,把加和后的值作为其权重范围的终点值。
这样的话,我们就可以使用Random.next(100)来做随机数,然后判断随机数落在的范围,然后映射到对应的优惠券数值即可。

import java.util.*;
public class WeightRandom {
    private List<WeightElement> weightElements;
    public void initWeight(String[] keys, Integer[] weights){
        if(keys == null || weights == null || keys.length != weights.length){//异常传参情况
            return;
        }
        weightElements = new ArrayList<>();
        for (int i=0; i< keys.length; i++){
            weightElements.add(new WeightElement(keys[i], weights[i]));
        }
        if(weightElements.size() == 0){
            return;
        }
        //初始化后分配各个key中的元素值
        WeightElement ele0 = weightElements.get(0);
        ele0.setThresholdLow(0);
        ele0.setThresholdHigh(ele0.getWeight());
        for (int i = 1; i < weightElements.size(); i++){
            WeightElement curElement = weightElements.get(i);
            WeightElement preElement = weightElements.get(i - 1);
            curElement.setThresholdLow(preElement.getThresholdHigh());
            curElement.setThresholdHigh(curElement.getThresholdLow() + curElement.getWeight());
        }
    }

    //二分查找写法
    public WeightElement getElementByRandomValue(Integer rv){
        if(rv < 0 || rv > getMaxRandomValue()-1){
            return null;
        }
        //此时rv必然在0 - getMaxRandomValue()-1范围内,
        //也就是必然能够命中某一个值
        int start = 0, end = weightElements.size() - 1;
        int index = weightElements.size()/2;
        while(true){
            if(rv < weightElements.get(index).getThresholdLow()){
                end = index - 1;
            }else if(rv >= weightElements.get(index).getThresholdHigh()){
                start = index + 1;
            }else{
                return weightElements.get(index);
            }
            index = (start + end)/2;
        }
    }

    /**非二分查找的写法
    public WeightElement getElementByRandomValue(Integer rv){
        //因为元素权重范围有序递增,所以这里可以改为二分查找
        for (WeightElement e:weightElements){
            if(rv >= e.getThresholdLow() && rv < e.getThresholdHigh()){
                return e;
            }
        }
        return null;
    }**/
    public int getMaxRandomValue(){
        if(weightElements == null || weightElements.size() == 0){
            return 0;
        }
        return weightElements.get(weightElements.size() - 1).getThresholdHigh();
    }
    static class WeightElement{
        //元素标记
        private String key;
        //元素权重
        private Integer weight;
        //权重对应随机数范围下限和上限
        private Integer thresholdLow;
        private Integer thresholdHigh;
        public WeightElement(String key, Integer weight){
            this.key = key;
            this.weight = weight;
        }
        public String getKey() {
            return key;
        }
        public void setKey(String key) {
            this.key = key;
        }
        public Integer getWeight() {
            return weight;
        }
        public void setWeight(Integer weight) {
            this.weight = weight;
        }
        public Integer getThresholdLow() {
            return thresholdLow;
        }
        public void setThresholdLow(Integer thresholdLow) {
            this.thresholdLow = thresholdLow;
        }
        public Integer getThresholdHigh() {
            return thresholdHigh;
        }
        public void setThresholdHigh(Integer thresholdHigh) {
            this.thresholdHigh = thresholdHigh;
        }
        
        public static void main(String[] args){
            WeightRandom wr = new WeightRandom();
            wr.initWeight(new String[]{"1","2","3","4"}
                    , new Integer[]{100,100,200,600});
            Random r = new Random();
            for (int i = 0; i < 10; i++){
                Integer rv = r.nextInt(wr.getMaxRandomValue());
                System.out.println(rv);
                System.out.println(wr.getElementByRandomValue(rv).getKey() + " " + rv);
            }

            //针对随机到的元素个数做一个简单测试
            HashMap<String, Integer> keyCount = new HashMap<String, Integer>();
            keyCount.put("1", 0);
            keyCount.put("2", 0);
            keyCount.put("3", 0);
            keyCount.put("4", 0);
            for (int i = 0; i < 10000; i++){
                Integer rv = r.nextInt(wr.getMaxRandomValue());
                String key = wr.getElementByRandomValue(rv).getKey();
                keyCount.put(key, keyCount.get(key) +1);
            }
            System.out.println(keyCount);
            System.out.println("");
        }
    }
}
```

结果:{1=1018, 2=1008, 3=2081, 4=5893}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-09-14 13:36:45  更:2021-09-14 13:39:05 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 14:37:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码