Percentage based drop prize on Mob entity kill I have created a simple percentage-based random prize drop for killing certain mobs.
A drop is basically what the player will get in return for killing a mob, as a reward.

So I have a large list of Mobs saved in an XML file which looks like this:



&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;drops&gt;
    &lt;npc id="1"&gt;
        &lt;drop item="175" amount="1" chance="0.001" /&gt;
        &lt;drop item="174" amount="5" chance="0.013" /&gt;
        &lt;drop item="173" amount="1" chance="0.158" /&gt;
        &lt;drop item="172" amount="1" chance="0.098" /&gt;
    &lt;/npc&gt;
&lt;/drops&gt;


Basically each NPC has its own ID, so that way we can easily decide what drops does a certain NPC can drop.

So for NPC '1' we have 4 drops, the hardest to get drop is 0.001 (1/1000) and the easiest one is 0.158.

To make the drop system percentage based, I used SecureRandom and the nextFloat() method to generate the base drop rate for the NPC.

After I got the rate, I go through all of the drops that the NPC can drop, and find the drop with the closest drop chance rate to the generated random float rate.

I do this like that:



/**
 * Gets a random drop for NPC by random percentage
 * @param npc       The NPC id
 * @return  The drop details
 */
public Drop getRandomDrop(int npc) {
    List&lt;Drop&gt; drops = this.drops.get(npc);

    /**
     * The random drop rate
     */
    float rate = random.nextFloat();

    /**
     * The closest distance to rate
     */
    float bestDistance = Float.MAX_VALUE;

    // Closest's drop instance
    Drop closest = null;

    for (Drop d : drops) {
        float rt = d.getChance();
        if (rt == rate) {
            return d;
        }

        float distance = Math.abs(rt - rate);
        if (distance &lt; bestDistance) {
            bestDistance = distance;
            closest = d;
        }
    }

    return closest;
}


But I feel that I can make the drops system more accurate, as in this approach is not accurate enough. I need some reviews and suggestions on my approach.
/**
 * Gets a random drop for NPC by random percentage
 * @param npc       The NPC id
 * @return  The drop details
 */
public Drop getRandomDrop(int npc) {
    List&lt;Drop&gt; drops = this.drops.get(npc);

    float roll = random.nextFloat();

    for ( Drop d : drops ) {
        float chance = d.getChance();
        if ( roll &lt; chance ) {
            return d;
        }

        roll -= chance;
    }

    // if no drop was selected, return an empty drop
    return Drop::EMPTY_DROP;
}


This is how I would write it.  Note that I changed the random variable to be called roll like a roll of a die.  For each drop, it compares the roll to the chance of getting that drop.  If the roll is less than the drop rate, then that drop is the loot.  Otherwise, reduce the roll by the drop chance (which makes subsequent drops more likely).  

This method assumes that the drop chances in the XML add up to 1 or that there is an implicit chance of an empty drop.  If you want to always drop and have drop chances that don't add up to 1, you can normalize the rate.  

float totalChance = 0.0;
for ( Drop d : drops ) {
    totalChance += d.getChance();
}


and then in the current loop 

        float chance = d.getChance() / totalChance;


You may still have to do some adjustment to get the rounding right in the case of a maximum random.nextFloat result.  

You would set the EMPTY_DROP to be a static class constant representing no loot.  Note that this would also be the drop if the NPC's section was missing from the XML file.