have random objects choosen with a different chance.

Discussion in 'Spigot Plugin Development' started by 01v, Aug 4, 2019.

  1. 01v


    So what I'm trying to do is create a method which set the blocks in a cuboid. I have managed to create the method, and now I'm working on setting the chance for it being a specified block.
    For example, having it being 20% stone and 80% coal ore.
    I am calling this method with a string with a number between 0 and 1000. So 800 would be 80%

    Code (Java):
        public Boolean chanceBoolean(String i){
            int h = ThreadLocalRandom.current().nextInt(0, 1001);
            if (Integer.parseInt(i) < h){
                return true;
                return false;
    The question is, how can I have multiple materials with a chance on being the block, and always get a block set. As for now, I got the chance working, but can't figure out how to always get a block set, so it doesn't end up as air. My total chance will always be 100%. So the different materials will always end up as 100%.
    I'm not looking for code, just ideas or answers on how I would do this.
    I know this is a java related problem, but I couldn't figure out how to search for the answer on the internet.
  2. You could use a map? (There’s probably a better way of doing this)
    Stone, 0 (50%)
    Iron, 50 (20%)
    Diamond, 75 (15%)
    Emerald, 95 (5%)

    Generate a random number 0-max value, and go through each value from lowest to highest and if it’s higher than the number then that’s the value.

    So 45 in this example would be stone, 51 would be iron, 76 would be diamond, and 98 would be emerald.
    • Like Like x 1
  3. What I typically do for this sort of thing, is create an array containing an amount of each potential result based on its percentage, and then pick a random index from the array, this can be nice as it doesn't require all the rates to add up to any particular number
    so for example, if I wanted 25% stone and 75% dirt
    the array would be: {stone, dirt, dirt, dirt}
    However, I would never claim this to be a good approach as it can end up consuming a lot of memory, because if you use larger numbers for the rates, as well as more potential results, the array will need to be larger
    The only reason I do it this way is because I have yet to run into any noticeable issues with it, and so I can't be bothered to look into better alternatives
    • Like Like x 1
  4. 01v


    Thank you! This is a interesting approach, and i will try it out. :)
  5. 01v


    I am planning having chances down to just a few percentages, and having 50 objects in a array is quite inefficient. But thanks!
  6. Crazy Crates uses this algorithm which I like. For each entry, pick a number between 1-100000, if the number is <= chance * 1000 then put that item into a List. Repeat algorithm until list has at least one entry. Then pick a random item out of the list and bam: there you go. With this, your chances don’t have to add to the 100% as each items chance is the “chance to be added to the list”
    • Like Like x 1
  7. 01v


    I will try this out aswell, thanks !
  8. That seems a bit unnecessary... mathematically couldn’t you just do 1-100 and number<= chance
  9. If you wanted to support up to three decimal places (which my code does), then you need to add more 0’s. If you only use whole numbers then 1-100 works fine.