当前位置: 首页 > news >正文

java实现权重随机获取值或对象

文章目录

    • 场景
    • TreeMap.tailMap方法
    • 简单分析
      • 使用随机值
      • 使用treemap实现权重取值
      • 将Int改为Double稍微准确一点,因为double随机的值更加多
      • 测试main方法
    • 当权重的参数比较多,那么建议使用hutool封装的

场景

按照权重2,8给用户分组为A,B,

TreeMap.tailMap方法

treeMap是一种基于红黑树实现的有序映射表,提供了一系列的方法来操作映射表中的元素。其中tailMap方法是用于返回映射表中大于或等于给定键的部分视图。

tailMap方法的定义如下:

   public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {return new AscendingSubMap<>(this,false, fromKey, inclusive,true,  null,    true);}

其中,fromKey表示起始键,返回一个从fromKey开始到映射表末尾的部分视图。inclusive是表示是否包含传入的fronKey.这个部分视图是SortedMap类型的,可以进行排序操作。

使用tailMap方法需要注意以下几点:

  1. 如果fromKey不存在于映射表中,则返回的部分视图将包含大于fromKey的所有键值对。
  2. 返回的部分视图是映射表的一个视图,对这个视图所做的修改会影响到原映射表。
  3. 返回的部分视图是有序的,可以进行排序操作。

下面是一个示例代码:

import java.util.TreeMap;
import java.util.SortedMap;public class TreeMapExample {public static void main(String[] args) {TreeMap<Integer, String> treeMap = new TreeMap<>();treeMap.put(1, "one");treeMap.put(2, "two");treeMap.put(3, "three");treeMap.put(4, "four");treeMap.put(5, "five");// 返回大于等于3的部分视图SortedMap<Integer, String> tailMap = treeMap.tailMap(3);System.out.println(tailMap); // 输出 {3=three, 4=four, 5=five}// 修改部分视图tailMap.put(6, "six");System.out.println(treeMap); // 输出 {1=one, 2=two, 3=three, 4=four, 5=five, 6=six}// 对部分视图进行排序SortedMap<Integer, String> sortedTailMap = tailMap.descendingMap();System.out.println(sortedTailMap); // 输出 {6=six, 5=five, 4=four, 3=three}}
}

在上面的示例代码中,首先创建了一个treeMap对象,并向其中添加了5个键值对。然后使用tailMap方法返回了大于等于3的部分视图,并对这个部分视图进行了修改和排序操作。

tailMap方法是Java中treeMap类提供的一个非常有用的方法,可以方便地获取映射表中大于等于指定键的部分视图,并进行排序和修改操作。
针对这个特性可以用来获取权重值

简单分析

好比A:B的权重为2:8,那么相当于A的权重为0->2,B的权重为2->10(2+8)都是包左不包右的;
那我们就可以随机个0-10的值,如果在0->2那么返回A,如果2->10那就返回B

使用随机值

  public static String test1() {Random random = new Random();int i = random.nextInt(10);if (i < 2) {return "A";} else if (i >= 2 && i < 10) {return "B";} else {return "C";}}

使用treemap实现权重取值

   public static String test2() {TreeMap<Integer, String> treeMap = new TreeMap<>();int total = 2 + 8;treeMap.put(2, "A");treeMap.put(total, "B");Random random = new Random();return treeMap.tailMap(random.nextInt(total), false).firstEntry().getValue();}

将Int改为Double稍微准确一点,因为double随机的值更加多

   public static String test3() {TreeMap<Double, String> treeMap = new TreeMap<>();int total = 2 + 8;treeMap.put((double) 2, "A");treeMap.put((double) total, "B");Random random = new Random();return treeMap.tailMap(total * random.nextDouble(), false).firstEntry().getValue();}

测试main方法

package com.study.springbootplus.test;import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.TreeMap;/*** @ClassName RandomTest* @Author yida* @Date 2023-09-14 18:26* @Description RandomTest*/
public class RandomTest {public static void main(String[] args) {int num_a = 0, num_b = 0, num_c = 0;int testCount = 1000;for (int i = 0; i < testCount; i++) {switch (test3()) {case "A":num_a = num_a + 1;break;case "B":num_b = num_b + 1;break;case "C":num_c = num_c + 1;break;}}System.out.println("A-" + num_a + "-------" + NumberUtil.div(num_a, testCount, 2) * 100 + "%");System.out.println("B-" + num_b + "-------" + NumberUtil.div(num_b, testCount, 2) * 100 + "%");System.out.println("C-" + num_c + "-------" + NumberUtil.div(num_c, testCount, 2) * 100 + "%");}public static String test1() {Random random = new Random();int i = random.nextInt(10);if (i < 2) {return "A";} else if (i >= 2 && i < 10) {return "B";} else {return "C";}}public static String test2() {TreeMap<Integer, String> treeMap = new TreeMap<>();int total = 2 + 8;treeMap.put(2, "A");treeMap.put(total, "B");Random random = new Random();return treeMap.tailMap(random.nextInt(total), false).firstEntry().getValue();}public static String test3() {TreeMap<Double, String> treeMap = new TreeMap<>();int total = 2 + 8;treeMap.put((double) 2, "A");treeMap.put((double) total, "B");Random random = new Random();return treeMap.tailMap(total * random.nextDouble(), false).firstEntry().getValue();}public static void test() {List<WeightRandom.WeightObj<String>> weightList = new ArrayList<>();weightList.add(new WeightRandom.WeightObj<>("A", 20));weightList.add(new WeightRandom.WeightObj<>("B", 30));weightList.add(new WeightRandom.WeightObj<>("C", 40));weightList.add(new WeightRandom.WeightObj<>("D", 10));WeightRandom<String> wr = RandomUtil.weightRandom(weightList);String str = "";int num_a = 0, num_b = 0, num_c = 0, num_d = 0;int testCount = 10000;for (int i = 0; i < testCount; i++) {str = wr.next();switch (str) {case "A":num_a = num_a + 1;break;case "B":num_b = num_b + 1;break;case "C":num_c = num_c + 1;break;case "D":num_d = num_d + 1;break;}}System.out.println("A-" + num_a + "-------" + NumberUtil.div(num_a, testCount, 2) * 100 + "%");System.out.println("B-" + num_b + "-------" + NumberUtil.div(num_b, testCount, 2) * 100 + "%");System.out.println("C-" + num_c + "-------" + NumberUtil.div(num_c, testCount, 2) * 100 + "%");System.out.println("D-" + num_d + "-------" + NumberUtil.div(num_d, testCount, 2) * 100 + "%");}}

测试结果:
A-195-------20.0%
B-805-------81.0%
C-0-------0.0%
如果测试基数越大,则越准确

当权重的参数比较多,那么建议使用hutool封装的

使用hutool返回权重值 原创

http://www.lryc.cn/news/189775.html

相关文章:

  • 期权账户怎么开通的?佣金最低多少?
  • MySQL(存储过程,store procedure)——存储过程的前世今生 MySQL存储过程体验 MybatisPlus中使用存储过程
  • 如何建立线上线下相结合的数字化新零售体系?
  • python:xlwings 操作 Excel 加入图片
  • 关于hive的时间戳
  • win10 wsl安装步骤
  • 深入理解Spring Boot AOP:切面编程的优势与应用
  • 使用大模型提效程序员工作
  • 如何应对量化交易,个人股票账户如何实现量化程序化自动交易
  • milvus测试
  • antd 表格getCheckboxProps禁用
  • 京东商品列表数据接口,关键词搜索京东商品数据接口
  • Vue使用BMapGL,及marker简单使用
  • WuThreat身份安全云-TVD每日漏洞情报-2023-10-10
  • BSCI认证是谁来验厂?
  • Java中如何在两个线程间共享数据
  • 4、在 CentOS 8 系统上安装 pgAdmin 4
  • 【数字人】3、LIA | 使用隐式空间来实现视频驱动单张图数字人生成(ICLR 2022)
  • 深度学习基础知识 最近邻插值法、双线性插值法、双三次插值算法
  • 计算机竞赛 : 题目:基于深度学习的水果识别 设计 开题 技术
  • 【2023美团后端-8】删除字符串的方案,限制不能连续删
  • 蓝桥等考Python组别十七级008
  • docker安装sql-server数据库,使用navicat实现备份数据库导入
  • 深度学习batch、batch_size、epoch、iteration以及小样本中episode、support set、query set关系
  • Air001 TIM1高级定时器单脉冲输出模式使用
  • 矿机生意难做,比特大陆停发工资
  • 计算机竞赛python区块链实现 - proof of work工作量证明共识算法
  • pyqt 划线标注工具,可用于车道线标注
  • 蓝桥等考Python组别十七级004
  • 计算机毕业设计选什么题目好?springboot 职业技术学院图书管理系统