我需要根据所选值的百分比几率选择一个值。例如:
百分比总是恰好合计为100%
我遇到过像this one这样的几个解决方案,但已经确定它们可能不正确。这是一个使用上述解决方案构建的示例程序:
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");
}
}
输出:
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
预期产出:
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
我相信我需要使用某种算法将百分比转换为0到99的范围,以便随机数生成器可以准确地选择一个值。不过,我想不出怎么做。
答案 0 :(得分:1)
您的结果是正确的:
50%发生在大约14.0191%的时间
50 - 36 = 14
百分之三十六的发生在29.001299%的时间
36 - 7 = 29
7%的时间发生在1.0029999%的时间
7 - 6 = 1
...
如果您想要总结一下,请删除所有'continue'语句。
答案 1 :(得分:-1)
想出来。您需要跟踪到目前为止测试的百分比并将其添加到当前测试中。
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");
}
}