当前位置:首页 > IT技术

权重随机数列表

时间:2019-08-10 19:10:00来源:IT技术作者:SEO探针小编阅读:79次「手机版」
 

随机数表

import cn.hutool.core.lang.WeightRandom;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

public class RandomUtil extends cn.hutool.core.util.RandomUtil {
/**
     * 根据权重列表生成固定总量的数组
     *
     * @param weightList  权重列表
     * @param total  随机列表数的总和
     * @param fuDong     浮动指数
     * @return
     */
    public static <T> Map<T, integer> randomListFromWeight(List<WeightRandom.WeightObj<T>> weightList, Integer total, double fuDong) {
        if (!(fuDong > 0 && fuDong < 1)) {
            throw new illegalargumentException("浮动指数必须在0~1之间");
        }
        Map<T, Integer> result = new HashMap<>();
        // 将权重列表倒序排列,保证权重高的优先取值
        weightList.sort(new Comparator<WeightRandom.WeightObj>() {
            @Override
            public int compare(WeightRandom.WeightObj o1, WeightRandom.WeightObj o2) {
                if (o1.getWeight() > o2.getWeight()) {
                    return -1;
                } else if (o1.getWeight() < o2.getWeight()) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });
        // 统计权重总和
        double sumWeight = weightList.stream().map(WeightRandom.WeightObj::getWeight).reduce((a, b) -> a + b).orElse(1.0 * weightList.size());
        // 计算平均权重
        double avgWeight = sumWeight / weightList.size();
        // 计算平均值
        double avg = 1.0 * total / weightList.size();
        int before = 0;
        for (int i = 0; i < weightList.size() - 1; i++) {
            // 实际值 = 平均值 * 权重 / 平均权重
            double countForWeight = avg * weightList.get(i).getWeight() / avgWeight;
            double min = before + countForWeight * (1 - fuDong);
            double max = before + countForWeight * (1 + fuDong);
            min = min > total ? before : min;
            max = max > total ? total : max;
            int after = min < max ? (int) Math.round(RandomUtil.randomDouble(min, max)) : total;
            result.put(weightList.get(i).getObj(), after - before);
            before = after;
        }
        // 最后一个的值
        result.put(weightList.get(weightList.size() - 1).getObj(), total - before);
        return result;
    }
}

相关阅读

淘宝宝贝被隐形降权该怎么办?有什么办法恢复权重?

在很多时候我们可能会发现店铺或者是宝贝的流量下降了,而且还持续了下降的趋势,这种情况下,很多人会觉得莫名其妙,自己最近也没有对店

ASO中评论权重降低?从六个角度说完这个问题

无论ASO人怎样哀嚎评论的权重如何的在降低,都改变不了其是影响APP排名的因素之一。而有的ASO朋友却认为,评论之途已尽,需重点从其它

淘宝加购物车和收藏有什么区别?对店铺权重影响大吗

淘宝加购物车和收藏有什么区别?如今很多卖家开始更重视加购了,因为加购影响到店铺的诸多方面,淘宝加购物车和收藏有什么区别?对店铺权

matlab生成随机数的rand、randi和randn三种形式

  matlab中关于随机数的产生有3种库函数,下面我们来看看它们的形式:   1、rand(…)   它是生成0~1之间(开环,不包含0和1两个数

京东快车搜索权重飙升法:霸屏霸词法

有开京东快车很厉害的卖家朋友,可以用自家的3个商品,连续霸占某个词,PC端快车“商品精选”首页推广位。最佳状态是,3个商品分别排到这

分享到:

栏目导航

推荐阅读

热门阅读