samedi 6 mai 2017

Bukkit / Java customisable percentage chances

everyone. I'm trying to make a crates plugin in Bukkit (that doesn't really matter), and I'm having trouble with the random chance drawing. Since everything is customizable (You can create an indefinite amount of prizes with whatever chance you want), I'm finding it difficult to make the percentage drawing. This is my code right now:

            for (DoubleStack ds : items.values()) {
                double chance = ds.getChance();
                p.sendMessage(chance + " " + random + " " + ds.getItemStack().getType());
                if (random >= chance) {
                    p.getInventory().addItem(ds.getItemStack());
                    break;
                }
            }

A DoubleStack is just a class I made that stores the item and the chance of getting it. As you can see, the method I am using is buggy, as the chances aren't realistic, and you have a chance of getting multiple items (I don't want this)

So, how would I be able to do this?

Thanks!

Also, I have looked at all the other probability threads, but they don't cover NOT KNOWING specifically what the probabilities are, they just do things like:

int x = new Random().nextInt(100);
if(x <= 10 && x >= 20) {
   doSomething(); }
//Repeats with different numbers




Aucun commentaire:

Enregistrer un commentaire