如何根据百分比计算掉落率

Psy*_*tis 2 java math

我在这里张贴之间徘徊,math.stackexchange.com但在这里选择了它,因为它与编程相关,我以前没用过math.stackexchange.com.

基本上我有6个不同的项目,我想根据百分比"放弃",但我不知道如何实现这一点.数学不是我强大的西装,也没有帮助.

无论如何,第1至第6项的百分比如下:

1%, 0.85%, 0.1%, 0.05%, 0.01%, 0.001%

所以第1项有1%的机会掉落,第2项有0.85%的机会掉落等.

而且我想要每次运行丢失1个项目,如果没有满足这些"机会",则将使用默认项目.考虑这6项奖励项目.

到目前为止,我已经尝试过这个:

        Random rn = new Random();

    int x = rn.nextInt(100)+1;

    if(100%x==0)//1%
    {

    }
    if(100%(x*100)==0) //0.1%
    {

    }
Run Code Online (Sandbox Code Playgroud)

这似乎不公平,这个:

    Random rn = new Random();

    int x = rn.nextInt(100000)+1;

    if(100%x==0)//1%
    {

    }
    if(8500%x==0) //0.1%
    {

    }
Run Code Online (Sandbox Code Playgroud)

这似乎更糟糕.

我想为每个项目使用一个随机数,但这很多,RNG因为它将在游戏循环中运行.

我试图尽可能地保持这个问题,以避免辩论等,所以我不是在寻找最好的解决方案,只是一个有效 - 如果需要任何其他信息,我很乐意回答,只要它是n2k帮我解决这个问题=)

lib*_*bik 6

为每个机会生成随机数是一种很好的方法:

    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");
    }
Run Code Online (Sandbox Code Playgroud)

请注意,只要您只删除一个项目,元素的顺序就很重要.

例如,您有5个项目下降99%.从逻辑上讲,第五项只有(0.01 ^ 4)*0.99的几率下降.