简体   繁体   English

根据百分比选择值

[英]Selecting a value based on percentages

I need to select a value based on a percentage chance of that value being selected. 我需要根据选择该值的百分比几率来选择一个值。 For example: 例如:

  • 10% of the time increment value a 10%的时间增量值a
  • 20% of the time increment value b 20%的时间增量值b
  • 30% of the time increment value c 30%的时间增量值c
  • 40% of the time increment value d 40%的时间增量值d

The percentages will always add up to exactly 100% 百分比总是合计100%

I have encountered several solutions like this one , but have determined that they cannot possibly be correct. 我也遇到了几个解决方案,如这一个 ,但已经确定,他们不可能是正确的。 Here is a sample program built using the solution mentioned: 这是使用上述解决方案构建的示例程序:

import java.util.Random;

public class Main {

    private static Random r = new Random();

    public static void main(String[] args) {
        final int iterations = 1000000;
        System.out.println("Testing percentage based random, " + iterations + " iterations");
        int onePercent = 0;
        int sixPercent = 0;
        int sevenPercent = 0;
        int thirtySixPercent = 0;
        int fiftyPercent = 0;
        // Those values add up to 100% overall
        for (int i = 0; i < iterations; i++) {
            int random = r.nextInt(100);
            if (random < 1) {
                onePercent++;
                continue;
            }
            if (random < 6) {
                sixPercent++;
                continue;
            }
            if (random < 7) {
                sevenPercent++;
                continue;
            }
            if (random < 36) {
                thirtySixPercent++;
                continue;
            }
            if (random < 50) {
                fiftyPercent++;
                continue;
            }
            // That can't be right because if random > 50 then nothing at all happens
        }
        System.out.println("One percent happened about " + (onePercent / Float.valueOf(iterations)) * 100 + "% of the time");
        System.out.println("Six percent happened about " + (sixPercent / Float.valueOf(iterations)) * 100 + "% of the time");
        System.out.println("Seven percent happened about " + (sevenPercent / Float.valueOf(iterations)) * 100 + "% of the time");
        System.out.println("Thirty six percent happened about " + (thirtySixPercent / Float.valueOf(iterations)) * 100 + "% of the time");
        System.out.println("Fifty percent happened about " + (fiftyPercent / Float.valueOf(iterations)) * 100 + "% of the time");
    }
}

Output: 输出:

Testing percentage based random, 1000000 iterations
One percent happened about 0.99649996% of the time
Six percent happened about 4.9925% of the time
Seven percent happened about 1.0029999% of the time
Thirty six percent happened about 29.001299% of the time
Fifty percent happened about 14.0191% of the time

Expected output: 预期产量:

Testing percentage based random, 1000000 iterations
One percent happened about 0.99649996% of the time
Six percent happened about 6.9925% of the time
Seven percent happened about 7.0029999% of the time
Thirty six percent happened about 36.001299% of the time
Fifty percent happened about 50.0191% of the time

I believe I need to use some sort of algorithm to convert the percentages into a scale from 0 to 99 so that the random number generator can select a value accurately. 我相信我需要使用某种算法将百分比转换为从0到99的比例,以便随机数生成器可以准确选择一个值。 I cannot think of how to do that, though. 不过,我无法考虑如何做到这一点。

Your results are correct : 你的结果是正确的:

Fifty percent happened about 14.0191% of the time 50%的事件发生在大约14.0191%的时间

50 - 36 = 14 50 - 36 = 14

Thirty six percent happened about 29.001299% of the time 百分之三十六的发生在29.001299%的时间

36 - 7 = 29 36 - 7 = 29

Seven percent happened about 1.0029999% of the time 大约1.00%的时间发生了7%

7 - 6 = 1 7 - 6 = 1

.... ....

Delete all 'continue' statements if you want them to sum up. 如果您要总结所有“继续”语句。

Figured it out. 弄清楚了。 You need to keep track of the percentage tested so far and add it to the current test. 您需要跟踪到目前为止测试的百分比并将其添加到当前测试中。

import java.util.Random;

public class Main {

    private static Random r = new Random();

    public static void main(String[] args) {
        final int iterations = 1000000;
        System.out.println("Testing percentage based random, " + iterations + " iterations");
        int onePercent = 0;
        int sixPercent = 0;
        int sevenPercent = 0;
        int thirtySixPercent = 0;
        int fiftyPercent = 0;
        // Those values add up to 100% overall
        for (int i = 0; i < iterations; i++) {
            int random = r.nextInt(100);
            int totalPercent = 0;
            if (random < totalPercent + 1) {
                onePercent++;
                continue;
            }
            totalPercent += 1;
            if (random < totalPercent + 6) {
                sixPercent++;
                continue;
            }
            totalPercent += 6;
            if (random < totalPercent + 7) {
                sevenPercent++;
                continue;
            }
            totalPercent += 7;
            if (random < totalPercent + 36) {
                thirtySixPercent++;
                continue;
            }
            totalPercent += 36;
            if (random < totalPercent + 50) {
                fiftyPercent++;
                continue;
            }
            totalPercent += 50;
            // That can't be right because if random > 50 then nothing at all happens
        }
        System.out.println("One percent happened about " + (onePercent / Float.valueOf(iterations)) * 100 + "% of the time");
        System.out.println("Six percent happened about " + (sixPercent / Float.valueOf(iterations)) * 100 + "% of the time");
        System.out.println("Seven percent happened about " + (sevenPercent / Float.valueOf(iterations)) * 100 + "% of the time");
        System.out.println("Thirty six percent happened about " + (thirtySixPercent / Float.valueOf(iterations)) * 100 + "% of the time");
        System.out.println("Fifty percent happened about " + (fiftyPercent / Float.valueOf(iterations)) * 100 + "% of the time");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM