How can I use percentages with randoms

Discussion in 'Plugin Development' started by ClassifiedLife, Apr 24, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    ClassifiedLife

    I want to make a config-able mystery chest plugin and I want there to be a config option for percentage chance of getting certain items. How would I go about doing that? I suck with configs so if you want to throw in tips and how to grab stuff with configs go ahead.
     
  2. Offline

    Tecno_Wizard

    @ClassifiedLife, scale from 0-99, set each section based on percent, generate random and find section it landed in.
     
  3. Offline

    ClassifiedLife

    @Tecno_Wizard I appreciate you trying but please simpler words and examples work too
     
  4. Offline

    vhbob

    @ClassifiedLife what he just said is in simple form but since you don't know basic integer code here's what you do:
    Code:
    int randomint = (int) (Math.random() * 100);
    that gets a random number 1-100 aka out of 100 percent
     
  5. Offline

    ClassifiedLife

    @vhbob but how would I get the section with that? That wouldn't give me a percentage, say the percentages were 50% 25% and 25% then the number is 30, what would I do?
     
    Last edited: Apr 24, 2015
  6. Offline

    1Rogue

    Rather than assigning a percentage, assign item weighting. Give each item a "number" of times it would drop out of a pool of drops, and then you simply add the weight together and select a random number.

    Say you have 3 items: foo, bar, baz

    Foo has a weight of 3
    Bar has a weight of 6
    Baz has a weight of 1

    With this, you add them together for an item selection weight of "10". From there, you can use RNG to determine the selected weight:

    Code:java
    1. int weightSum = 10; //our specific example
    2. int selection = ThreadLocalRandom.current().nextInt(weightSum);


    From there, you can use the value of each weighted item, subtracting it from "selection" until you go below 0, and when you do go below 0, you know you've found your random item selection.

    Code:java
    1. int selection = 7; //our RNG gave us 7
    2. for (ExampleItem item : /* your items */) {
    3. selection -= item.getWeight();
    4. if (selection < 0) {
    5. //"item" is your randomly selected item
    6. }
    7. }


    In running through, that would look like this:
    Code:
    Selection = 7, subtracting Foo...
    Selection = 4, subtracting Bar...
    Selection = -2, "Bar" is your item
    If this gives you an idea of how it works, then you can even see that with this example (as it adds to a convenient weight of 10), Foo has a 30% chance to "drop", Bar has 60%, and Baz has only a 10% chance.

    This is a much more concise and random way to select items, but trying to assign specific percentages leads to maintainability/upkeep issues.
     
  7. Offline

    meguy26

    @ClassifiedLife
    The way i do it:
    Code:
    Random rand = new Random();
    int chance = <yourChance>;
    double comparison = rand.nextDouble() * 100;
    if(chance >= comparison){
    //do stuff
    }
     
Thread Status:
Not open for further replies.

Share This Page