{"id":2047,"date":"2023-04-01T21:08:46","date_gmt":"2023-04-01T13:08:46","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2047"},"modified":"2023-04-07T06:07:03","modified_gmt":"2023-04-06T22:07:03","slug":"java-sorts-by-probability-weight","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/java-sorts-by-probability-weight\/","title":{"rendered":"Java\u6839\u636e\u6982\u7387\uff08\u6743\u91cd\uff09\u6392\u5e8f"},"content":{"rendered":"<p>\u539f\u7406\uff1a\u4f9d\u6b21\u5c06\u6743\u91cd\u53e0\u52a0\uff0c\u6784\u5efa\u5404\u5143\u7d20\u547d\u4e2d\u533a\u95f4\uff0c\u7136\u540e\u5229\u7528<code>random.nextInt(int)<\/code>\u5728\u603b\u7684\u533a\u95f4\u5185\u751f\u6210\u968f\u673a\u6574\u6570\uff0c\u843d\u5728\u6307\u5b9a\u533a\u95f4\u5185\u5373\u4e3a\u547d\u4e2d\u3002\u547d\u4e2d\u8005\u4e0d\u53c2\u4e0e\u4e0b\u4e00\u8f6e\u547d\u4e2d\u4e8b\u4ef6\uff0c\u540c\u65f6\u547d\u4e2d\u8005\u81ea\u8eab\u3001\u540e\u9762\u7684\u5143\u7d20\u3001\u4ee5\u53ca\u603b\u548c\u5747\u51cf\u5c11\u672c\u8f6e\u547d\u4e2d\u503c\uff0c\u7ee7\u7eed\u4e0b\u4e00\u8f6e\uff0c\u76f4\u81f3\u6240\u6709\u533a\u95f4\u88ab\u547d\u4e2d<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">public class App\n{\n    public static void main( String[] args ) throws Exception\n    {\n        Channel[] channels = new Channel[4];\n        channels[0] = new Channel();\n        channels[0].setName(&quot;Alipay&quot;);\n        channels[0].setWeight(100);\n        channels[1] = new Channel();\n        channels[1].setName(&quot;WxPay&quot;);\n        channels[1].setWeight(90);\n        channels[2] = new Channel();\n        channels[2].setName(&quot;BankPay&quot;);\n        channels[2].setWeight(80);\n        channels[3] = new Channel();\n        channels[3].setName(&quot;CashPay&quot;);\n        channels[3].setWeight(70);\n\n        Channel[] newChannels = sortWithProbability(channels);\n        for (int i = 0; i &lt; newChannels.length; i++) {\n            System.out.println(newChannels[i].getName());\n        }\n    }\n\n    private static Channel[] sortWithProbability(Channel[] channels) {\n        Channel[] newChannels = new Channel[channels.length];\n        int[] weights = new int[channels.length];\n        weights[0] = channels[0].getWeight();\n        for (int i = 1; i &lt; channels.length; i++) {\n            weights[i] = weights[i-1] + channels[i].getWeight();\n        }\n        int sum = weights[channels.length - 1];\n        for (int i = 0; i &lt; weights.length; i++) {\n            System.out.print(weights[i] + &quot; &quot;);\n        }\n        System.out.println();\n\n        Random random = new Random();\n        int index = 0;\n        for (int i = 0; i &lt; channels.length; i++) {\n            int weight = random.nextInt(sum);\n            System.out.print(&quot;\u7b2c&quot; + i + &quot;\u6b21: &quot; + weight);\n            int hit = 0;\n            for (int j = 0; j &lt; weights.length; j++) {\n                if (weight &lt; weights[j]) {\n                    hit = j;\n                    System.out.println(&quot;, \u547d\u4e2d: &quot; + j);\n                    newChannels[index++] = channels[j];\n                    sum -= channels[j].getWeight();\n                    break;\n                }\n            }\n            for (int j = hit; j &lt; weights.length; j++) {\n                weights[j] -= channels[hit].getWeight();\n            }\n        }\n        return newChannels;\n    }\n}\n\n@Data\nclass Channel {\n    private String name;\n    private int weight;\n}<\/code><\/pre>\n<pre><code>100 190 270 340 \n\u7b2c0\u6b21: 158, \u547d\u4e2d: 1\n\u7b2c1\u6b21: 207, \u547d\u4e2d: 3\n\u7b2c2\u6b21: 25, \u547d\u4e2d: 0\n\u7b2c3\u6b21: 40, \u547d\u4e2d: 2\nWxPay\nCashPay\nAlipay\nBankPay<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u7406\uff1a\u4f9d\u6b21\u5c06\u6743\u91cd\u53e0\u52a0\uff0c\u6784\u5efa\u5404\u5143\u7d20\u547d\u4e2d\u533a\u95f4\uff0c\u7136\u540e\u5229\u7528random.nextInt(int)\u5728\u603b\u7684\u533a\u95f4\u5185\u751f\u6210\u968f\u673a\u6574 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-2047","post","type-post","status-publish","format-standard","hentry","category-data-structure-algorithm"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2047"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2047\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}