# Logic for selecting random rewards [closed]

I am making a RPG game and I am seeking a bit of information on how to grant players random rewards, such as game items, weapons, gold, etc... after defeating a monster, or a NPC of some kind. The items would be pre made and already existing. I am not sure on what is the best way to approach this kind of system. Any tips of advice will be appreciated on the matter.

• Are you trying to generate random items, Diablo-style, or to simply select items out of pre-determined loot tables, like most other games? Commented Oct 2, 2013 at 23:32
• my mistake forgot to mention that note, yes pre-determined items. I am not familiar with the term loot table also, is that an example of what I would need in my case? Thanks for your response. Commented Oct 2, 2013 at 23:42
• I would have tried answering this myself, but I did a quick google and found codeproject.com/Articles/420046/… which basically says everything you will ever need to know about random loot drop implementation. Commented Oct 3, 2013 at 8:18

Well... you need to determine how your game's going to do it.

Thinking back on my days of playing Ocarina of Time, it always seemed to me that when I was low on health, turning some bushes into salad would reliably heal me up. If I was at full health, it would always drop rupees...

Divine intervention (aka game design) aside, under the hood it's all done with probabilities. Use the random number generator to make a probability function like probability(percent) that returns true <percent> of the time. Then it's just a matter of, when the enemy is vanquished, run its drop function or something similar.

void Drop()
{
// Sometimes you want to drop one kind of item, but not the other kinds
if( probability(20) ) {
// 20% of the time.
weaponDrop("common"); // Determines (using probabilities) which weapon to drop
} else if( probability(20) ) {
// 16% of the time
itemDrop();
} else {
// 64% of the time
goldDrop(10, 25); // Drop between 10 and 25 gold pieces
}

// Sometimes you want it to drop items regardless of what else you've dropped
if(probability(.1)){
weaponDrop("Super Cool Rare");
}

// Sometimes you want specific drops to always happen.
dropDungeonMap();
}


I don't know what your game looks like in terms of the code structure, but the point is that it works on probabilities. Keep in mind that if(20%) implies that all the else branches combined have a probability of 80%.

• Isn't better to calls only once to the random method?
– Zhen
Commented Oct 3, 2013 at 7:39
• I suppose. But you shouldn't think about performance until you need to. Besides, I don't call random more than once, I call probability more than once. You can have probability use a single global random number that gets updated on every iteration of the game loop. Commented Oct 3, 2013 at 7:47
• thanks for your reply, I will see what I can get from it :) Commented Oct 3, 2013 at 10:27