权重随机算法在抽奖,资源调度等系统中应用还是比较广泛的,一个简单的按照权重来随机的实现,权重为几个随机对象(分类)的命中的比例,权重设置越高命中越容易,之和可以不等于100;

简单实现代码如下:

    import java.util.ArrayList;
import java.util.List;
import java.util.Random; public class WeightRandom {
static List<WeightCategory> categorys = new ArrayList<WeightCategory>();
private static Random random = new Random(); public static void initData() {
WeightCategory wc1 = new WeightCategory("A",60);
WeightCategory wc2 = new WeightCategory("B",20);
WeightCategory wc3 = new WeightCategory("C",20);
categorys.add(wc1);
categorys.add(wc2);
categorys.add(wc3);
} public static void main(String[] args) {
initData();
Integer weightSum = 0;
for (WeightCategory wc : categorys) {
weightSum += wc.getWeight();
} if (weightSum <= 0) {
System.err.println("Error: weightSum=" + weightSum.toString());
return;
}
Integer n = random.nextInt(weightSum); // n in [0, weightSum)
Integer m = 0;
for (WeightCategory wc : categorys) {
if (m <= n && n < m + wc.getWeight()) {
System.out.println("This Random Category is "+wc.getCategory());
break;
}
m += wc.getWeight();
} } } class WeightCategory {
private String category;
private Integer weight; public WeightCategory() {
super();
} public WeightCategory(String category, Integer weight) {
super();
this.setCategory(category);
this.setWeight(weight);
} public Integer getWeight() {
return weight;
} public void setWeight(Integer weight) {
this.weight = weight;
} public String getCategory() {
return category;
} public void setCategory(String category) {
this.category = category;
}
}

java实现权重随机算法的更多相关文章

  1. java 权重随机算法实现

    import java.util.*; /** * 权重随机算法实现 * a b c d 对应权重范围 --- [0,1).[1,3).[3,6).[6,10) */ public class Ran ...

  2. 权重随机算法的java实现

    一.概述 平时,经常会遇到权重随机算法,从不同权重的N个元素中随机选择一个,并使得总体选择结果是按照权重分布的.如广告投放.负载均衡等. 如有4个元素A.B.C.D,权重分别为1.2.3.4,随机结果 ...

  3. 权重随机算法Java实现

    权重随机算法在抽奖,资源调度等系统中应用还是比较广泛的,一个简单的按照权重来随机的实现,权重为几个随机对象(分类)的命中的比例,权重设置越高命中越容易,之和可以不等于100: 简单实现代码如下: ? ...

  4. Java 模板权重随机

    Template templates=...// 所有的模板 final int _weights=1000; // 所有的模板权重 Template _template=null; //随机一个权重 ...

  5. JAVA基于权重的抽奖

    https://blog.csdn.net/huyuyang6688/article/details/50480687 如有4个元素A.B.C.D,权重分别为1.2.3.4,随机结果中A:B:C:D的 ...

  6. Java实现 LeetCode 528 按权重随机选择(TreeMap)

    528. 按权重随机选择 给定一个正整数数组 w ,其中 w[i] 代表位置 i 的权重,请写一个函数 pickIndex ,它可以随机地获取位置 i,选取位置 i 的概率与 w[i] 成正比. 说明 ...

  7. hdu 4712 Hamming Distance ( 随机算法混过了 )

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  8. Java——Random类随机整数---18.10.11

    一.Random类的定义 1.Random类位于java.util包中,主要用于生成 伪随机数 2.random类将 种子数 作为随机算法的起源数字,计算生成伪随机数,其与生成的随机数字的区间无关 3 ...

  9. python的random模块及加权随机算法的python实现

    random是用于生成随机数的,我们可以利用它随机生成数字或者选择字符串. random.seed(x)改变随机数生成器的种子seed. 一般不必特别去设定seed,Python会自动选择seed. ...

随机推荐

  1. DB Intro - MongoDB Basic

    mongoDB basic from:http://www.tutorialspoint.com/mongodb prject:https://github.com/chenxing12/l4mong ...

  2. nodejs初探(一)nodejs开发环境搭建

    简介 JavaScript是一种运行在浏览器的脚本.Node.js是一个基于Chrome JavaScript运行时建立的平台, 用于方便地搭建响应速度快.易于扩展的网络应用.Node.js 使用事件 ...

  3. 常用css搜集

    div居中显示 position: fixed; top: 130px; left:0px;right:0px;margin-left:auto;margin-right:auto;

  4. 3d旋转卡牌

    做成向中心缩放就行了,和旋转效果一样的

  5. [openStack]使用Fuel安装OpenStack juno的fuel_master

    安装OpenStack是一件很复杂的事情,特别是在想目中,如果一个组件一个组件,一台一台的coding部署,估计太消耗时间,而且出错的概率很高,所以使用工具推送部署的效率就很高了,而且必须得可靠.mi ...

  6. (三)TestNG

    1.testNG的部分注解 test测试方法都是执行顺序:并不是从上往下执行的,而是根据方法名ASCII码进行执行的,小的先执行 比如a比b先执行,1比2先执行,不管代码放的顺序是怎么样. impor ...

  7. /Date(1410019200000+0800)/如何转换为date对象

    <script type="text/javascript">var s = '/Date(1410019200000+0800)/ '; s.replace(/Dat ...

  8. 文档生成工具Sandcastle Help File Builder

    Sandcastle Help File Builder   http://shfb.codeplex.com/

  9. Linux防火墙命令

    linux 查看防火墙状态   1.查看防火墙状态 systemctl status firewalld firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning ...

  10. Stage4--Python面向对象

    说在前面: Stage1-Stage4简单介绍一下Python语法,Stage5开始用python实现一些实际应用,语法的东西到处可以查看到,学习一门程序语言的最终目的是应用,而不是学习语法,语法本事 ...