JAVA 根据设置的概率生成随机数的方法
作者:布诺 时间:2023-07-15 00:13:37
本文介绍了JAVA 根据设置的概率生成随机数的方法,分享给大家
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class RandomTest {
// String 可以为任意类型 也可以自定义类型
static Map<String, Integer> keyChanceMap = new HashMap<String, Integer>();
static {
keyChanceMap.put("aaa", 500);
keyChanceMap.put("bbb", 1500);
keyChanceMap.put("ccc", 2000);
keyChanceMap.put("ddd", 3000);
keyChanceMap.put("eee", 3000);
}
public static void main(String[] args) {
Map<String, Integer> count = new HashMap<String, Integer>();
List<String> list = new ArrayList<>();
String item = null;
for (int i = 0; i < 10000; i++) {
item = chanceSelect(keyChanceMap);
list.add(item);
if (count.containsKey(item)) {
count.put(item, count.get(item) + 1);
} else {
count.put(item, 1);
}
}
for (String id : count.keySet()) {
System.out.println(id + "\t出现了 " + count.get(id) + " 次");
}
Random rand = new Random();
int num = rand.nextInt(10000);
System.out.print("最终选择的随机数为:"+list.get(num));
}
public static String chanceSelect(Map<String, Integer> keyChanceMap) {
if (keyChanceMap == null || keyChanceMap.size() == 0)
return null;
Integer sum = 0;
for (Integer value : keyChanceMap.values()) {
sum += value;
}
// 从1开始
Integer rand = new Random().nextInt(sum) + 1;
for (Map.Entry<String, Integer> entry : keyChanceMap.entrySet()) {
rand -= entry.getValue();
// 选中
if (rand <= 0) {
String item = entry.getKey();
return item;
}
}
return null;
}
}
来源:http://www.cnblogs.com/bunuo/p/6305916.html
标签:java,概率,随机数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#套接字(Socket)通信之UDP组播详解
2022-06-11 05:49:52
Java安全框架——Shiro的使用详解(附springboot整合Shiro的demo)
2022-05-29 09:46:46
![](https://img.aspxhome.com/file/2023/9/69649_0s.png)
Java面试基础之TCP连接以及其优化
2023-10-02 17:43:51
![](https://img.aspxhome.com/file/2023/7/66967_0s.jpg)
简单了解springboot的jar包部署步骤
2021-07-02 14:22:48
Android 程序执行Linux命令的解决方法及注意事项
2023-07-13 00:14:58
![](https://img.aspxhome.com/file/2023/4/83524_0s.jpg)
Intellij搭建springmvc常见问题解决方案
2023-07-23 12:53:29
![](https://img.aspxhome.com/file/2023/9/57419_0s.png)
javascript最新2020经典面试题
2023-05-15 09:04:43
java Spring的启动原理详解
2022-09-02 04:39:59
![](https://img.aspxhome.com/file/2023/4/66244_0s.jpg)
IDEA不识别Java文件:文件变橙色&显示后缀名.java的解决
2022-05-27 06:43:36
![](https://img.aspxhome.com/file/2023/8/118918_0s.png)
详谈hibernate,jpa与spring data jpa三者之间的关系
2022-08-04 22:35:57
![](https://img.aspxhome.com/file/2023/9/76479_0s.jpg)
判断List和Map是否相等并合并List中相同的Map
2022-12-29 02:11:10
关于java入门与java开发环境配置详细教程
2023-11-24 14:21:44
![](https://img.aspxhome.com/file/2023/8/59568_0s.png)
C++ Boost MPI接口详细讲解
2023-11-02 13:35:36
Java 设计模式中的策略模式详情
2023-08-06 03:45:11
![](https://img.aspxhome.com/file/2023/1/57731_0s.png)
Java基于递归和循环两种方式实现未知维度集合的笛卡尔积算法示例
2021-07-19 04:41:18
MyBatis-Plus中更新操作的两种实现
2022-06-24 03:09:14
![](https://img.aspxhome.com/file/2023/7/65597_0s.jpg)
JVM常用垃圾收集器详细解说
2023-02-15 02:19:49
![](https://img.aspxhome.com/file/2023/0/83100_0s.png)
Spring Boot 定义系统启动任务的多种方式
2023-11-24 13:25:33
![](https://img.aspxhome.com/file/2023/7/61947_0s.png)
android视频截屏&手机录屏实现代码
2022-12-10 15:20:31
![](https://img.aspxhome.com/file/2023/7/87537_0s.png)
C# 抓取网页内容的方法
2022-09-13 04:50:10