获得机会百分比

时间:2018-01-29 13:44:48

标签: android

我想偶然展示一些图片。我知道如何显示图像,但如何获得显示的机会?

例如,让我们说第一张图片显示100%的概率,只有0.01%的显示概率,第二幅1.20%,第三幅15.00%和soo。

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:0)

 double[] chances = {1, 35, 0.85, 50, 0.1, 0.05, 0.01, 0.001, 65, 11};
Random r = new Random();
boolean dropped = false;
for (int i = 0; i < chances.length; i++) {
    if (chances[i] > r.nextDouble() * 100) {
        System.out.println("Item with " + chances[i] + " chance is dropped");
        dropped = true;
        break;
    }
}
if (dropped == false) {
    System.out.println("Dropping default item");
}