java Random 带权重的随机选择
实际场景中,经常要从多个选项中随机选择一个,不过,不同选项经常有不同的权重。
/**
* Created by xc on 2019/11/23
* 带权重的随机选择
*/
public class Test { public static void main(String[] args) {
Pair[] options = new Pair[]{new Pair("first", 3.3), new Pair("second", 3.3), new Pair("third", 3.3)};
WeightRandom rnd = new WeightRandom(options);
for (int i = 0; i < 10; i++) {
System.out.print(rnd.nextItem() + " ");
}
} }
/**
* Created by xc on 2019/11/25
* 表示选项和权重的类Pair
*/
public class Pair {
Object item;
double weight; public Pair(Object item, double weight) {
this.item = item;
this.weight = weight;
} public Object getItem() {
return item;
} public double getWeight() {
return weight;
}
}
/**
* Created by xc on 2019/11/25
* 代码清单7-9 带权重的选择WeightRandom
*/
public class WeightRandom {
private Pair[] options;
private double[] cumulativeProbabilities;
private Random rnd; public WeightRandom(Pair[] options) {
this.options = options;
this.rnd = new Random();
prepare();
} /**
* prepare()方法计算每个选项的累计概率,保存在数组cumulativeProbabilities中
*/
private void prepare() {
int weights = 0;
for (Pair pair : options) {
weights += pair.getWeight();
}
cumulativeProbabilities = new double[options.length];
int sum = 0;
for (int i = 0; i < options.length; i++) {
sum += options[i].getWeight();
cumulativeProbabilities[i] = sum / (double) weights;
}
} /**
* nextItem()方法根据权重随机选择一个,具体就是,首先生成一个0~1的数,
* 然后使用二分查找,如果没找到,返回结果是-(插入点)-1,所以-index-1就是插入点,插入点的位置就对应选项的索引。
* @return
*/
public Object nextItem() {
double randomValue = rnd.nextDouble();
int index = Arrays.binarySearch(cumulativeProbabilities, randomValue);
if (index < 0) {
index = -index - 1;
}
return options[index].getItem();
}
}
java Random 带权重的随机选择的更多相关文章
- python random从集合中随机选择元素
1.使用python random模块的choice方法随机选择某个元素 from random import choice foo = ['a', 'b', 'c', 'd', 'e'] print ...
- Java编程的逻辑 (34) - 随机
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- python random 随机选择操作
# -*- coding:utf-8 -*- import random arr = ['A','B','C','D','E','F'] #生成(0.0, 1.0)的随机数 print random. ...
- Java实现 LeetCode 528 按权重随机选择(TreeMap)
528. 按权重随机选择 给定一个正整数数组 w ,其中 w[i] 代表位置 i 的权重,请写一个函数 pickIndex ,它可以随机地获取位置 i,选取位置 i 的概率与 w[i] 成正比. 说明 ...
- java Random随机生成一个数
package java05; import java.util.Random; /* Random随机生成一个数字 1.导包: import java.util.Random; 2.创建 Rando ...
- random os 序列化 模块模块 随机选择
# 1 random 模块 随机选择# import random#随机取小数# ret = random.random() #空是0到1之间的小数字# print(ret)# # 0.0799728 ...
- Python之随机选择 random
随机选择:random import random # 从一个序列中随机的抽取一个元素 values=[1,2,3,4,56] # 指定取出N个不同元素 print(random.sample(val ...
- java Random类和Math.Rondom
Java中存在着两种Random函数: 一.java.lang.Math.Random; 调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取 ...
- JAVA Random 详解
Java中存在着两种Random函数: 一.java.lang.Math.Random; 调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取值范 ...
随机推荐
- centos服务器上线第二个django项目方法。
阿里云服务器开启端口8001,9001 创建一个虚拟环境 virtualenv -p python3 web2 使虚拟环境生效 source web2/bin/activate 虚拟环境中安装djan ...
- discuz网站前端代码优化思路
一.head标签中的局部 1.URL设计 URL尽量含有通用已成趋向的移动命名,例如“m./wap./3g./mobi./mobile./mob/wml/”,能够在子域名等方面表现 2.页面顶部的do ...
- python应用-工资计算
""" 工资计算 Author:罗万财 Date:2017-6-8 """ salary=float(input('请输入工资:')) in ...
- spring cloud 学习资料
spring cloud 学习资料 网址 拜托!面试请不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA
- SDOI2012 走迷宫
走迷宫 Morenan被困在了一个迷宫里.迷宫可以视为N个点M条边的有向图,其中Morenan处于起点S,迷宫的终点设为T.可惜的是,Morenan非常的脑小,他只会从一个点出发随机沿着一条从该点出发 ...
- javascript:location=location;">刷新</a>
<a href="javascript:location=location;">刷新</a>
- Daily consumption
Bill record, standard of living, record every consumption, income, expenditure, manage your own life
- SpringBoot要点之使用Actuator监控
Actuator是Springboot提供的用来对应用系统进行自省和监控的功能模块,借助于Actuator开发者可以很方便地对应用系统某些监控指标进行查看.统计等. 在pom文件中加入spring-b ...
- 【题解】洛谷 P2725 邮票 Stamps
目录 题目 思路 \(Code\) 题目 P2725 邮票 Stamps 思路 \(\texttt{dp}\).\(\texttt{dp[i]}\)表示拼出邮资\(i\)最少需要几张邮票. 状态转移方 ...
- Js更改整页a标签跳转方式
<script> var anchors = document.getElementsByTagName("a"); for(i=0;i<anchors.leng ...