Java根据概率(权重)排序

原理:依次将权重叠加,构建各元素命中区间,然后利用random.nextInt(int)在总的区间内生成随机整数,落在指定区间内即为命中。命中者不参与下一轮命中事件,同时命中者自身、后面的元素、以及总和均减少本轮命中值,继续下一轮,直至所有区间被命中

public class App
{
    public static void main( String[] args ) throws Exception
    {
        Channel[] channels = new Channel[4];
        channels[0] = new Channel();
        channels[0].setName("Alipay");
        channels[0].setWeight(100);
        channels[1] = new Channel();
        channels[1].setName("WxPay");
        channels[1].setWeight(90);
        channels[2] = new Channel();
        channels[2].setName("BankPay");
        channels[2].setWeight(80);
        channels[3] = new Channel();
        channels[3].setName("CashPay");
        channels[3].setWeight(70);

        Channel[] newChannels = sortWithProbability(channels);
        for (int i = 0; i < newChannels.length; i++) {
            System.out.println(newChannels[i].getName());
        }
    }

    private static Channel[] sortWithProbability(Channel[] channels) {
        Channel[] newChannels = new Channel[channels.length];
        int[] weights = new int[channels.length];
        weights[0] = channels[0].getWeight();
        for (int i = 1; i < channels.length; i++) {
            weights[i] = weights[i-1] + channels[i].getWeight();
        }
        int sum = weights[channels.length - 1];
        for (int i = 0; i < weights.length; i++) {
            System.out.print(weights[i] + " ");
        }
        System.out.println();

        Random random = new Random();
        int index = 0;
        for (int i = 0; i < channels.length; i++) {
            int weight = random.nextInt(sum);
            System.out.print("第" + i + "次: " + weight);
            int hit = 0;
            for (int j = 0; j < weights.length; j++) {
                if (weight < weights[j]) {
                    hit = j;
                    System.out.println(", 命中: " + j);
                    newChannels[index++] = channels[j];
                    sum -= channels[j].getWeight();
                    break;
                }
            }
            for (int j = hit; j < weights.length; j++) {
                weights[j] -= channels[hit].getWeight();
            }
        }
        return newChannels;
    }
}

@Data
class Channel {
    private String name;
    private int weight;
}
100 190 270 340 
第0次: 158, 命中: 1
第1次: 207, 命中: 3
第2次: 25, 命中: 0
第3次: 40, 命中: 2
WxPay
CashPay
Alipay
BankPay

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/04/01/java-sorts-by-probability-weight/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Java根据概率(权重)排序
原理:依次将权重叠加,构建各元素命中区间,然后利用random.nextInt(int)在总的区间内生成随机整数,落在指定区间内即为命中。命中者不参与下一轮命中事件,……
<<上一篇
下一篇>>
文章目录
关闭
目 录