Having trouble with chances...

Discussion in 'Plugin Development' started by TheMinecraftKnight, Apr 29, 2017.

Thread Status:
Not open for further replies.
  1. I'm making a crates plugin with configurable chances, but I'm having a problem with the actual randomness of the chances.

    I have a HashMap of ItemStacks and DoubleStacks(A class I made that combines the same ItemStacks as before, and a double as their chance, since you can't get a key from a value in a hashmap, and I needed a way to get the item.)

    That all works fine, but random number generation doesn't properly.
    Code:
    public static int randInt(int min, int max) {
            Random rand = new Random();
            int randomNum = rand.nextInt((max - min) + 1) + min;
            return randomNum;
        }
    I'm using that to generate a random number from 1-100, and then checking through all the DoubleStacks, which I put into an array to see if the number is equal or smaller
    Code:
                    double random = randInt(0, 100);
                    DoubleStack[] stacks = Arrays.copyOf(items.values().toArray(), items.values().toArray().length,
                            DoubleStack[].class);
                    double add = 0;
                    for (int i = 0; i < stacks.length; i++) {
                        if ((stacks[i].getChance() + add) >= random) {
                            e.getWhoClicked().sendMessage(stacks[i].getChance() + add + "");
                            e.getWhoClicked().getInventory().addItem(stacks[i].getItemStack());
                            for (Player p : Bukkit.getOnlinePlayers()) {
                                p.sendMessage(ChatColor.GOLD + e.getWhoClicked().getName() + " has won a "
                                        + stacks[i].getItemStack().getType());
                            }
                        } else {
                            add += stacks[i].getChance();
                        }
                    }
    
    However often what happens is I get more than one prize at a time, any ideas on how to fix this?
    Thanks
     
  2. Offline

    Sethburster

    You are over complicating the rand.nextInt() the only parameter you need to pass in there is the max and then add the min after

    Ex: rand.nextInt(50) + 1 Should give you a random between 50 and 1


    Sent from my iPhone using Tapatalk
     
  3. @Sethburster Okay, I've changed my random method and it seems to work better, but I'm still not sure how what I want can be achieved. I can still get more than one item, and the chances still don't seem realistic.

    I've changed my code a bit and got to this
    Code:
    double random = randInt(100);
    for (DoubleStack ds : items.values()) {
                        double chance = ds.getChance();
                        if (random >= chance) {
                            p.getInventory().addItem(ds.getItemStack());
                            break;
                        }
                    }
    
    But it's still having the same problem, any ideas why?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 4, 2017
Thread Status:
Not open for further replies.

Share This Page