java语言实现权重随机算法完整实例

这篇文章主要介绍了java语言实现权重随机算法完整实例,具有一定借鉴价值,需要的朋友可以参考下。

前言

现在app就是雨后春笋,嗖嗖的往外冒啊,有经验的、没经验的、有资历的、没资历的都想着创业,创业的90%以上都要做一个app出来,好像成了创业的标配。

做了app就得推广啊,怎么推,发券送钱是最多用的被不可少的了,现在好多产品或者运营都要求能够随机出优惠券的金额,但是呢又不能过于随机,送出去的券都是钱吗,投资人的钱,是吧。

所以,在随机生成的金额中就要求,小额度的几率要大,大额度的几率要小,比如说3元的70%,5块的25%,10块的5%,这个样子的概率去生成优惠券,这个怎么办呢?

对于上述的问题,直接用我们的Random.next(Integer range);就不够了。因为这个伪随机不带权重,3,5,10出现的概率都是一样的。

实现思路

还是拿上述的例子,3出现的概率是70%,我们给他的权重赋值为70,5出现的概率为25%,我们给他的权重赋值为25,10出现的概率为5%,我们给他的权重赋值为5.

我们按照顺序计算出权重的加和,把当前数字出现的权重加和前的值作为其权重范围的起点值,把加和后的值作为其权重范围的终点值。

这样的话,我们就可以使用Random.next(100)来做随机数,然后判断随机数落在的范围,然后映射到对应的优惠券数值即可。

java实现

 package com.nggirl.test.weight.random; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Random; public class WeightRandom { 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 keyCount = new HashMap(); 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).intValue()+1); } System.out.println(""); } private List 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= e.getThresholdLow() && rv 

结果:

 2 102 876 4 876 

二分法的实现

 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).getThresholdHigh()){ start = index + 1; } else{ return weightElements.get(index); } index = (start + end)/2; } }

下面再分享一则实例,加强对权重随机算法的理解,一次到位!

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

简单实现代码如下:

 import java.util.ArrayList; import java.util.List; import java.util.Random; public class WeightRandom { static List categorys = new ArrayList(); 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 

结果:

总结

以上就是本文关于java语言实现权重随机算法完整实例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

以上就是java语言实现权重随机算法完整实例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java